Update MemoryCache code to use more references instead of pointers
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 31 Jan 2015 00:04:36 +0000 (00:04 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 31 Jan 2015 00:04:36 +0000 (00:04 +0000)
https://bugs.webkit.org/show_bug.cgi?id=141099

Reviewed by Andreas Kling.
Source/WebKit2:

* WebProcess/ResourceCache/WebResourceCacheManager.cpp:
(WebKit::WebResourceCacheManager::clearCacheForOrigin):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@179421 268f45cc-cd09-0410-ab3c-d52691b4dbfc

16 files changed:
Source/WebCore/ChangeLog
Source/WebCore/WebCore.exp.in
Source/WebCore/html/DOMURL.cpp
Source/WebCore/inspector/InspectorResourceAgent.cpp
Source/WebCore/loader/DocumentLoader.cpp
Source/WebCore/loader/SubresourceLoader.cpp
Source/WebCore/loader/cache/CachedImage.cpp
Source/WebCore/loader/cache/CachedResource.cpp
Source/WebCore/loader/cache/CachedResourceHandle.h
Source/WebCore/loader/cache/CachedResourceLoader.cpp
Source/WebCore/loader/cache/CachedResourceLoader.h
Source/WebCore/loader/cache/MemoryCache.cpp
Source/WebCore/loader/cache/MemoryCache.h
Source/WebCore/platform/graphics/MaskImageOperation.cpp
Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/ResourceCache/WebResourceCacheManager.cpp

index e5f0c43..dcf1527 100644 (file)
@@ -1,3 +1,10 @@
+2015-01-30  Chris Dumez  <cdumez@apple.com>
+
+        Update MemoryCache code to use more references instead of pointers
+        https://bugs.webkit.org/show_bug.cgi?id=141099
+
+        Reviewed by Andreas Kling.
+
 2015-01-30  Brian J. Burg  <burg@cs.washington.edu>
 
         Web Inspector: ASSERT in InspectorTimelineAgent::internalStop
index 02f8587..db410a6 100644 (file)
@@ -196,7 +196,7 @@ __ZN7WebCore11MemoryCache15addImageToCacheEP7CGImageRKNS_3URLERKN3WTF6StringE
 __ZN7WebCore11MemoryCache18resourceForRequestERKNS_15ResourceRequestENS_9SessionIDE
 __ZN7WebCore11MemoryCache19getOriginsWithCacheERN3WTF7HashSetINS1_6RefPtrINS_14SecurityOriginEEENS_18SecurityOriginHashENS1_10HashTraitsIS5_EEEE
 __ZN7WebCore11MemoryCache20removeImageFromCacheERKNS_3URLERKN3WTF6StringE
-__ZN7WebCore11MemoryCache25removeResourcesWithOriginEPNS_14SecurityOriginE
+__ZN7WebCore11MemoryCache25removeResourcesWithOriginERNS_14SecurityOriginE
 __ZN7WebCore11PageOverlay15setNeedsDisplayERKNS_7IntRectE
 __ZN7WebCore11PageOverlay15setNeedsDisplayEv
 __ZN7WebCore11PageOverlay20stopFadeOutAnimationEv
index d459f57..1aa55a8 100644 (file)
@@ -118,7 +118,7 @@ void DOMURL::revokeObjectURL(ScriptExecutionContext* scriptExecutionContext, con
 #if ENABLE(CACHE_PARTITIONING)
     request.setDomainForCachePartition(scriptExecutionContext->topOrigin()->domainForCachePartition());
 #endif
-    MemoryCache::removeRequestFromSessionCaches(scriptExecutionContext, request);
+    MemoryCache::removeRequestFromSessionCaches(*scriptExecutionContext, request);
 
     scriptExecutionContext->publicURLManager().revoke(url);
 }
index b39dc4b..120ffe8 100644 (file)
@@ -710,7 +710,7 @@ void InspectorResourceAgent::replayXHR(ErrorString&, const String& requestId)
 
     CachedResource* cachedResource = memoryCache().resourceForRequest(request, m_pageAgent->page()->sessionID());
     if (cachedResource)
-        memoryCache().remove(cachedResource);
+        memoryCache().remove(*cachedResource);
 
     xhr->open(xhrReplayData->method(), xhrReplayData->url(), xhrReplayData->async(), IGNORE_EXCEPTION);
     for (const auto& header : xhrReplayData->headers())
index caa5127..c1671bf 100644 (file)
@@ -448,7 +448,7 @@ void DocumentLoader::finishedLoading(double finishTime)
     // and deny the appcache the chance to intercept it in the future, so remove from the memory cache.
     if (m_frame) {
         if (m_mainResource && m_frame->document()->hasManifest())
-            memoryCache().remove(m_mainResource.get());
+            memoryCache().remove(*m_mainResource);
     }
     m_applicationCacheHost->finishedLoadingMainResource();
 }
@@ -599,7 +599,7 @@ void DocumentLoader::responseReceived(CachedResource* resource, const ResourceRe
     // The memory cache doesn't understand the application cache or its caching rules. So if a main resource is served
     // from the application cache, ensure we don't save the result for future use.
     if (willLoadFallback)
-        memoryCache().remove(m_mainResource.get());
+        memoryCache().remove(*m_mainResource);
 
     if (willLoadFallback)
         return;
index aa5c2cd..b322fd1 100644 (file)
@@ -159,7 +159,7 @@ void SubresourceLoader::willSendRequest(ResourceRequest& newRequest, const Resou
         // Doing so would have us reusing the resource from the first request if the second request's revalidation succeeds.
         if (newRequest.isConditional() && m_resource->resourceToRevalidate() && newRequest.url() != m_resource->resourceToRevalidate()->response().url()) {
             newRequest.makeUnconditional();
-            memoryCache().revalidationFailed(m_resource);
+            memoryCache().revalidationFailed(*m_resource);
             if (m_frame)
                 m_frame->mainFrame().diagnosticLoggingClient().logDiagnosticMessageWithResult(DiagnosticLoggingKeys::cachedResourceRevalidationKey(), emptyString(), DiagnosticLoggingResultFail);
         }
@@ -207,7 +207,7 @@ void SubresourceLoader::didReceiveResponse(const ResourceResponse& response)
             // 304 Not modified / Use local copy
             // Existing resource is ok, just use it updating the expiration time.
             m_resource->setResponse(response);
-            memoryCache().revalidationSucceeded(m_resource, response);
+            memoryCache().revalidationSucceeded(*m_resource, response);
             if (m_frame)
                 m_frame->mainFrame().diagnosticLoggingClient().logDiagnosticMessageWithResult(DiagnosticLoggingKeys::cachedResourceRevalidationKey(), emptyString(), DiagnosticLoggingResultPass);
             if (!reachedTerminalState())
@@ -215,7 +215,7 @@ void SubresourceLoader::didReceiveResponse(const ResourceResponse& response)
             return;
         }
         // Did not get 304 response, continue as a regular resource load.
-        memoryCache().revalidationFailed(m_resource);
+        memoryCache().revalidationFailed(*m_resource);
         if (m_frame)
             m_frame->mainFrame().diagnosticLoggingClient().logDiagnosticMessageWithResult(DiagnosticLoggingKeys::cachedResourceRevalidationKey(), emptyString(), DiagnosticLoggingResultFail);
     }
@@ -385,10 +385,10 @@ void SubresourceLoader::didFail(const ResourceError& error)
     CachedResourceHandle<CachedResource> protectResource(m_resource);
     m_state = Finishing;
     if (m_resource->resourceToRevalidate())
-        memoryCache().revalidationFailed(m_resource);
+        memoryCache().revalidationFailed(*m_resource);
     m_resource->setResourceError(error);
     if (!m_resource->isPreloaded())
-        memoryCache().remove(m_resource);
+        memoryCache().remove(*m_resource);
     m_resource->error(CachedResource::LoadError);
     cleanupForError(error);
     notifyDone();
@@ -418,9 +418,9 @@ void SubresourceLoader::willCancel(const ResourceError& error)
     m_state = Finishing;
 #endif
     if (m_resource->resourceToRevalidate())
-        memoryCache().revalidationFailed(m_resource);
+        memoryCache().revalidationFailed(*m_resource);
     m_resource->setResourceError(error);
-    memoryCache().remove(m_resource);
+    memoryCache().remove(*m_resource);
 }
 
 void SubresourceLoader::didCancel(const ResourceError&)
index 50b532d..a7c3770 100644 (file)
@@ -381,7 +381,7 @@ void CachedImage::addIncrementalDataBuffer(SharedBuffer& data)
         // Image decoding failed. Either we need more image data or the image data is malformed.
         error(errorOccurred() ? status() : DecodeError);
         if (inCache())
-            memoryCache().remove(this);
+            memoryCache().remove(*this);
         return;
     }
 
@@ -422,7 +422,7 @@ void CachedImage::finishLoading(SharedBuffer* data)
         // Image decoding failed; the image data is malformed.
         error(errorOccurred() ? status() : DecodeError);
         if (inCache())
-            memoryCache().remove(this);
+            memoryCache().remove(*this);
         return;
     }
 
index 025c5e2..bfef85b 100644 (file)
@@ -170,7 +170,7 @@ CachedResource::~CachedResource()
 #endif
 
     if (m_owningCachedResourceLoader)
-        m_owningCachedResourceLoader->removeCachedResource(this);
+        m_owningCachedResourceLoader->removeCachedResource(*this);
 }
 
 void CachedResource::failBeforeStarting()
@@ -178,7 +178,7 @@ void CachedResource::failBeforeStarting()
     // FIXME: What if resources in other frames were waiting for this revalidation?
     LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
     if (m_resourceToRevalidate) 
-        memoryCache().revalidationFailed(this); 
+        memoryCache().revalidationFailed(*this);
     error(CachedResource::LoadError);
 }
 
@@ -415,7 +415,7 @@ bool CachedResource::addClientToSet(CachedResourceClient* client)
             m_preloadResult = PreloadReferenced;
     }
     if (!hasClients() && inCache())
-        memoryCache().addToLiveResourcesSize(this);
+        memoryCache().addToLiveResourcesSize(*this);
 
     if ((m_type == RawResource || m_type == MainResource) && !m_response.isNull() && !m_proxyResource) {
         // Certain resources (especially XHRs and main resources) do crazy things if an asynchronous load returns
@@ -447,8 +447,8 @@ void CachedResource::removeClient(CachedResourceClient* client)
     bool deleted = deleteIfPossible();
     if (!deleted && !hasClients()) {
         if (inCache()) {
-            memoryCache().removeFromLiveResourcesSize(this);
-            memoryCache().removeFromLiveDecodedResourcesList(this);
+            memoryCache().removeFromLiveResourcesSize(*this);
+            memoryCache().removeFromLiveDecodedResourcesList(*this);
         }
         if (!m_switchingClientsToRevalidatedResource)
             allClientsRemoved();
@@ -458,7 +458,7 @@ void CachedResource::removeClient(CachedResourceClient* client)
             // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
             // "... History buffers MAY store such responses as part of their normal operation."
             // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
-            memoryCache().remove(this);
+            memoryCache().remove(*this);
         }
         memoryCache().prune();
     }
@@ -499,13 +499,13 @@ void CachedResource::setDecodedSize(unsigned size)
     // The object must be moved to a different queue, since its size has been changed.
     // Remove before updating m_decodedSize, so we find the resource in the correct LRU list.
     if (inCache())
-        memoryCache().removeFromLRUList(this);
+        memoryCache().removeFromLRUList(*this);
     
     m_decodedSize = size;
    
     if (inCache()) { 
         // Now insert into the new LRU list.
-        memoryCache().insertInLRUList(this);
+        memoryCache().insertInLRUList(*this);
         
         // Insert into or remove from the live decoded list if necessary.
         // When inserting into the LiveDecodedResourcesList it is possible
@@ -516,9 +516,9 @@ void CachedResource::setDecodedSize(unsigned size)
         // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
         bool inLiveDecodedResourcesList = memoryCache().inLiveDecodedResourcesList(*this);
         if (m_decodedSize && !inLiveDecodedResourcesList && hasClients())
-            memoryCache().insertInLiveDecodedResourcesList(this);
+            memoryCache().insertInLiveDecodedResourcesList(*this);
         else if (!m_decodedSize && inLiveDecodedResourcesList)
-            memoryCache().removeFromLiveDecodedResourcesList(this);
+            memoryCache().removeFromLiveDecodedResourcesList(*this);
 
         // Update the cache's size totals.
         memoryCache().adjustSize(hasClients(), delta);
@@ -535,12 +535,12 @@ void CachedResource::setEncodedSize(unsigned size)
     // The object must be moved to a different queue, since its size has been changed.
     // Remove before updating m_encodedSize, so we find the resource in the correct LRU list.
     if (inCache())
-        memoryCache().removeFromLRUList(this);
+        memoryCache().removeFromLRUList(*this);
 
     m_encodedSize = size;
 
     if (inCache()) { 
-        memoryCache().insertInLRUList(this);
+        memoryCache().insertInLRUList(*this);
         memoryCache().adjustSize(hasClients(), delta);
     }
 }
@@ -551,8 +551,8 @@ void CachedResource::didAccessDecodedData(double timeStamp)
     
     if (inCache()) {
         if (memoryCache().inLiveDecodedResourcesList(*this)) {
-            memoryCache().removeFromLiveDecodedResourcesList(this);
-            memoryCache().insertInLiveDecodedResourcesList(this);
+            memoryCache().removeFromLiveDecodedResourcesList(*this);
+            memoryCache().insertInLiveDecodedResourcesList(*this);
         }
         memoryCache().prune();
     }
index ce73922..8677939 100644 (file)
@@ -68,6 +68,7 @@ public:
 
     R* get() const { return reinterpret_cast<R*>(CachedResourceHandleBase::get()); }
     R* operator->() const { return get(); }
+    R& operator*() const { ASSERT(get()); return *get(); }
 
     CachedResourceHandle& operator=(R* res) { setResource(res); return *this; } 
     CachedResourceHandle& operator=(const CachedResourceHandle& o) { setResource(o.get()); return *this; }
index 95cc505..3dca1ae 100644 (file)
@@ -218,14 +218,14 @@ CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestUserCSSSt
     if (CachedResource* existing = memoryCache().resourceForRequest(request.resourceRequest(), sessionID())) {
         if (is<CachedCSSStyleSheet>(*existing))
             return downcast<CachedCSSStyleSheet>(existing);
-        memoryCache().remove(existing);
+        memoryCache().remove(*existing);
     }
     if (url.string() != request.resourceRequest().url())
         request.mutableResourceRequest().setURL(url);
 
     CachedResourceHandle<CachedCSSStyleSheet> userSheet = new CachedCSSStyleSheet(request.resourceRequest(), request.charset(), sessionID());
 
-    memoryCache().add(userSheet.get());
+    memoryCache().add(*userSheet);
     // FIXME: loadResource calls setOwningCachedResourceLoader() if the resource couldn't be added to cache. Does this function need to call it, too?
 
     userSheet->load(*this, ResourceLoaderOptions(DoNotSendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo));
@@ -489,7 +489,7 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(Cache
     const RevalidationPolicy policy = determineRevalidationPolicy(type, request.mutableResourceRequest(), request.forPreload(), resource.get(), request.defer());
     switch (policy) {
     case Reload:
-        memoryCache().remove(resource.get());
+        memoryCache().remove(*resource);
         FALLTHROUGH;
     case Load:
         if (resource)
@@ -505,7 +505,7 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(Cache
         if (!shouldContinueAfterNotifyingLoadedFromMemoryCache(request, resource.get()))
             return nullptr;
         logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::usedKey());
-        memoryCache().resourceAccessed(resource.get());
+        memoryCache().resourceAccessed(*resource);
         break;
     }
 
@@ -521,7 +521,7 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(Cache
         // We don't support immediate loads, but we do support immediate failure.
         if (resource->errorOccurred()) {
             if (resource->inCache())
-                memoryCache().remove(resource.get());
+                memoryCache().remove(*resource);
             return nullptr;
         }
     }
@@ -555,8 +555,8 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::revalidateResource(co
     LOG(ResourceLoading, "Resource %p created to revalidate %p", newResource.get(), resource);
     newResource->setResourceToRevalidate(resource);
     
-    memoryCache().remove(resource);
-    memoryCache().add(newResource.get());
+    memoryCache().remove(*resource);
+    memoryCache().add(*newResource);
 #if ENABLE(RESOURCE_TIMING)
     storeResourceTimingInitiatorInformation(resource, request);
 #else
@@ -573,7 +573,7 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::loadResource(CachedRe
 
     CachedResourceHandle<CachedResource> resource = createResource(type, request.mutableResourceRequest(), request.charset(), sessionID());
 
-    if (!memoryCache().add(resource.get()))
+    if (!memoryCache().add(*resource))
         resource->setOwningCachedResourceLoader(this);
 #if ENABLE(RESOURCE_TIMING)
     storeResourceTimingInitiatorInformation(resource, request);
@@ -789,22 +789,22 @@ CachePolicy CachedResourceLoader::cachePolicy(CachedResource::Type type) const
     }
 }
 
-void CachedResourceLoader::removeCachedResource(CachedResource* resource) const
+void CachedResourceLoader::removeCachedResource(CachedResource& resource)
 {
 #ifndef NDEBUG
-    DocumentResourceMap::iterator it = m_documentResources.find(resource->url());
+    DocumentResourceMap::iterator it = m_documentResources.find(resource.url());
     if (it != m_documentResources.end())
-        ASSERT(it->value.get() == resource);
+        ASSERT(it->value.get() == &resource);
 #endif
-    m_documentResources.remove(resource->url());
+    m_documentResources.remove(resource.url());
 }
 
-void CachedResourceLoader::addCachedResource(CachedResource* resource)
+void CachedResourceLoader::addCachedResource(CachedResource& resource)
 {
-    m_documentResources.set(resource->url(), resource);
+    m_documentResources.set(resource.url(), &resource);
 
     if (!memoryCache().add(resource))
-        resource->setOwningCachedResourceLoader(this);
+        resource.setOwningCachedResourceLoader(this);
 }
 
 void CachedResourceLoader::loadDone(CachedResource* resource, bool shouldPerformPostLoadActions)
@@ -985,13 +985,11 @@ void CachedResourceLoader::clearPreloads()
     if (!m_preloads)
         return;
 
-    ListHashSet<CachedResource*>::iterator end = m_preloads->end();
-    for (ListHashSet<CachedResource*>::iterator it = m_preloads->begin(); it != end; ++it) {
-        CachedResource* res = *it;
-        res->decreasePreloadCount();
-        bool deleted = res->deleteIfPossible();
-        if (!deleted && res->preloadResult() == CachedResource::PreloadNotReferenced)
-            memoryCache().remove(res);
+    for (auto* resource : *m_preloads) {
+        resource->decreasePreloadCount();
+        bool deleted = resource->deleteIfPossible();
+        if (!deleted && resource->preloadResult() == CachedResource::PreloadNotReferenced)
+            memoryCache().remove(*resource);
     }
     m_preloads = nullptr;
 }
index 1fd570b..817e360 100644 (file)
@@ -113,8 +113,8 @@ public:
     void clearDocumentLoader() { m_documentLoader = 0; }
     SessionID sessionID() const;
 
-    void removeCachedResource(CachedResource*) const;
-    void addCachedResource(CachedResource*);
+    void removeCachedResource(CachedResource&);
+    void addCachedResource(CachedResource&);
 
     void loadDone(CachedResource*, bool shouldPerformPostLoadActions = true);
 
index a50806b..b5b50cd 100644 (file)
@@ -99,81 +99,82 @@ URL MemoryCache::removeFragmentIdentifierIfNeeded(const URL& originalURL)
     return url;
 }
 
-bool MemoryCache::add(CachedResource* resource)
+bool MemoryCache::add(CachedResource& resource)
 {
     if (disabled())
         return false;
 
     ASSERT(WTF::isMainThread());
 
-    CachedResourceMap& resources = getSessionMap(resource->sessionID());
+    CachedResourceMap& resources = getSessionMap(resource.sessionID());
 #if ENABLE(CACHE_PARTITIONING)
-    CachedResourceItem* originMap = resources.get(resource->url());
+    CachedResourceItem* originMap = resources.get(resource.url());
     if (!originMap) {
         originMap = new CachedResourceItem;
-        resources.set(resource->url(), adoptPtr(originMap));
+        resources.set(resource.url(), adoptPtr(originMap));
     }
-    originMap->set(resource->cachePartition(), resource);
+    originMap->set(resource.cachePartition(), &resource);
 #else
-    resources.set(resource->url(), resource);
+    resources.set(resource.url(), &resource);
 #endif
-    resource->setInCache(true);
+    resource.setInCache(true);
     
     resourceAccessed(resource);
     
-    LOG(ResourceLoading, "MemoryCache::add Added '%s', resource %p\n", resource->url().string().latin1().data(), resource);
+    LOG(ResourceLoading, "MemoryCache::add Added '%s', resource %p\n", resource.url().string().latin1().data(), &resource);
     return true;
 }
 
-void MemoryCache::revalidationSucceeded(CachedResource* revalidatingResource, const ResourceResponse& response)
+// FIXME: This should take a reference.
+void MemoryCache::revalidationSucceeded(CachedResource& revalidatingResource, const ResourceResponse& response)
 {
-    CachedResource* resource = revalidatingResource->resourceToRevalidate();
-    ASSERT(resource);
-    ASSERT(!resource->inCache());
-    ASSERT(resource->isLoaded());
-    ASSERT(revalidatingResource->inCache());
+    ASSERT(revalidatingResource.resourceToRevalidate());
+    CachedResource& resource = *revalidatingResource.resourceToRevalidate();
+    ASSERT(!resource.inCache());
+    ASSERT(resource.isLoaded());
+    ASSERT(revalidatingResource.inCache());
 
     // Calling remove() can potentially delete revalidatingResource, which we use
     // below. This mustn't be the case since revalidation means it is loaded
     // and so canDelete() is false.
-    ASSERT(!revalidatingResource->canDelete());
+    ASSERT(!revalidatingResource.canDelete());
 
     remove(revalidatingResource);
 
-    CachedResourceMap& resources = getSessionMap(resource->sessionID());
+    CachedResourceMap& resources = getSessionMap(resource.sessionID());
 #if ENABLE(CACHE_PARTITIONING)
-    ASSERT(!resources.get(resource->url()) || !resources.get(resource->url())->get(resource->cachePartition()));
-    CachedResourceItem* originMap = resources.get(resource->url());
+    ASSERT(!resources.get(resource.url()) || !resources.get(resource.url())->get(resource.cachePartition()));
+    CachedResourceItem* originMap = resources.get(resource.url());
     if (!originMap) {
         originMap = new CachedResourceItem;
-        resources.set(resource->url(), adoptPtr(originMap));
+        resources.set(resource.url(), adoptPtr(originMap));
     }
-    originMap->set(resource->cachePartition(), resource);
+    originMap->set(resource.cachePartition(), &resource);
 #else
-    ASSERT(!resources.get(resource->url()));
-    resources.set(resource->url(), resource);
+    ASSERT(!resources.get(resource.url()));
+    resources.set(resource.url(), &resource);
 #endif
-    resource->setInCache(true);
-    resource->updateResponseAfterRevalidation(response);
+    resource.setInCache(true);
+    resource.updateResponseAfterRevalidation(response);
     insertInLRUList(resource);
-    int delta = resource->size();
-    if (resource->decodedSize() && resource->hasClients())
+    int delta = resource.size();
+    if (resource.decodedSize() && resource.hasClients())
         insertInLiveDecodedResourcesList(resource);
     if (delta)
-        adjustSize(resource->hasClients(), delta);
+        adjustSize(resource.hasClients(), delta);
     
-    revalidatingResource->switchClientsToRevalidatedResource();
-    ASSERT(!revalidatingResource->m_deleted);
+    revalidatingResource.switchClientsToRevalidatedResource();
+    ASSERT(!revalidatingResource.m_deleted);
     // this deletes the revalidating resource
-    revalidatingResource->clearResourceToRevalidate();
+    revalidatingResource.clearResourceToRevalidate();
 }
 
-void MemoryCache::revalidationFailed(CachedResource* revalidatingResource)
+void MemoryCache::revalidationFailed(CachedResource& revalidatingResource)
 {
     ASSERT(WTF::isMainThread());
-    LOG(ResourceLoading, "Revalidation failed for %p", revalidatingResource);
-    ASSERT(revalidatingResource->resourceToRevalidate());
-    revalidatingResource->clearResourceToRevalidate();
+    LOG(ResourceLoading, "Revalidation failed for %p", &revalidatingResource);
+    ASSERT(revalidatingResource.resourceToRevalidate());
+    revalidatingResource.clearResourceToRevalidate();
 }
 
 CachedResource* MemoryCache::resourceForURL(const URL& resourceURL, SessionID sessionID)
@@ -193,7 +194,7 @@ CachedResource* MemoryCache::resourceForRequestImpl(const ResourceRequest& reque
 
 #if ENABLE(CACHE_PARTITIONING)
     CachedResourceItem* item = resources.get(url);
-    CachedResource* resource = 0;
+    CachedResource* resource = nullptr;
     if (item)
         resource = item->get(request.cachePartition());
 #else
@@ -245,7 +246,7 @@ bool MemoryCache::addImageToCache(NativeImagePtr image, const URL& url, const St
 #if ENABLE(CACHE_PARTITIONING)
     cachedImage->resourceRequest().setDomainForCachePartition(domainForCachePartition);
 #endif
-    return add(cachedImage.release());
+    return add(*cachedImage.release());
 }
 
 void MemoryCache::removeImageFromCache(const URL& url, const String& domainForCachePartition)
@@ -266,7 +267,7 @@ void MemoryCache::removeImageFromCache(const URL& url, const String& domainForCa
 
     // A resource exists and is not a manually cached image, so just remove it.
     if (!is<CachedImage>(*resource) || !downcast<CachedImage>(*resource).isManuallyCached()) {
-        remove(resource);
+        remove(*resource);
         return;
     }
 
@@ -383,7 +384,7 @@ void MemoryCache::pruneDeadResourcesToSize(unsigned targetSize)
             CachedResourceHandle<CachedResource> previous = current->m_prevInAllResourcesList;
             ASSERT(!previous || previous->inCache());
             if (!current->hasClients() && !current->isPreloaded() && !current->isCacheValidator()) {
-                remove(current);
+                remove(*current);
                 if (targetSize && m_deadSize <= targetSize)
                     return;
             }
@@ -411,73 +412,73 @@ void MemoryCache::setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, un
     prune();
 }
 
-void MemoryCache::remove(CachedResource* resource)
+void MemoryCache::remove(CachedResource& resource)
 {
     ASSERT(WTF::isMainThread());
-    LOG(ResourceLoading, "Evicting resource %p for '%s' from cache", resource, resource->url().string().latin1().data());
+    LOG(ResourceLoading, "Evicting resource %p for '%s' from cache", &resource, resource.url().string().latin1().data());
     // The resource may have already been removed by someone other than our caller,
     // who needed a fresh copy for a reload. See <http://bugs.webkit.org/show_bug.cgi?id=12479#c6>.
-    CachedResourceMap& resources = getSessionMap(resource->sessionID());
-    if (resource->inCache()) {
+    CachedResourceMap& resources = getSessionMap(resource.sessionID());
+    if (resource.inCache()) {
         // Remove from the resource map.
 #if ENABLE(CACHE_PARTITIONING)
-        CachedResourceItem* item = resources.get(resource->url());
+        CachedResourceItem* item = resources.get(resource.url());
         if (item) {
-            item->remove(resource->cachePartition());
+            item->remove(resource.cachePartition());
             if (!item->size())
-                resources.remove(resource->url());
+                resources.remove(resource.url());
         }
 #else
-        resources.remove(resource->url());
+        resources.remove(resource.url());
 #endif
-        resource->setInCache(false);
+        resource.setInCache(false);
 
         // Remove from the appropriate LRU list.
         removeFromLRUList(resource);
         removeFromLiveDecodedResourcesList(resource);
-        adjustSize(resource->hasClients(), -static_cast<int>(resource->size()));
+        adjustSize(resource.hasClients(), -static_cast<int>(resource.size()));
     } else
 #if ENABLE(CACHE_PARTITIONING)
-        ASSERT(!resources.get(resource->url()) || resources.get(resource->url())->get(resource->cachePartition()) != resource);
+        ASSERT(!resources.get(resource.url()) || resources.get(resource.url())->get(resource.cachePartition()) != &resource);
 #else
-        ASSERT(resources.get(resource->url()) != resource);
+        ASSERT(resources.get(resource.url()) != &resource);
 #endif
 
-    resource->deleteIfPossible();
+    resource.deleteIfPossible();
 }
 
-MemoryCache::LRUList* MemoryCache::lruListFor(CachedResource* resource)
+MemoryCache::LRUList* MemoryCache::lruListFor(CachedResource& resource)
 {
-    unsigned accessCount = std::max(resource->accessCount(), 1U);
-    unsigned queueIndex = WTF::fastLog2(resource->size() / accessCount);
+    unsigned accessCount = std::max(resource.accessCount(), 1U);
+    unsigned queueIndex = WTF::fastLog2(resource.size() / accessCount);
 #ifndef NDEBUG
-    resource->m_lruIndex = queueIndex;
+    resource.m_lruIndex = queueIndex;
 #endif
     if (m_allResources.size() <= queueIndex)
         m_allResources.grow(queueIndex + 1);
     return &m_allResources[queueIndex];
 }
 
-void MemoryCache::removeFromLRUList(CachedResource* resource)
+void MemoryCache::removeFromLRUList(CachedResource& resource)
 {
     // If we've never been accessed, then we're brand new and not in any list.
-    if (resource->accessCount() == 0)
+    if (!resource.accessCount())
         return;
 
 #if !ASSERT_DISABLED
-    unsigned oldListIndex = resource->m_lruIndex;
+    unsigned oldListIndex = resource.m_lruIndex;
 #endif
 
     LRUList* list = lruListFor(resource);
 
 #if !ASSERT_DISABLED
     // Verify that the list we got is the list we want.
-    ASSERT(resource->m_lruIndex == oldListIndex);
+    ASSERT(resource.m_lruIndex == oldListIndex);
 
     // Verify that we are in fact in this list.
     bool found = false;
     for (CachedResource* current = list->m_head; current; current = current->m_nextInAllResourcesList) {
-        if (current == resource) {
+        if (current == &resource) {
             found = true;
             break;
         }
@@ -485,49 +486,49 @@ void MemoryCache::removeFromLRUList(CachedResource* resource)
     ASSERT(found);
 #endif
 
-    CachedResource* next = resource->m_nextInAllResourcesList;
-    CachedResource* prev = resource->m_prevInAllResourcesList;
+    CachedResource* next = resource.m_nextInAllResourcesList;
+    CachedResource* prev = resource.m_prevInAllResourcesList;
     
-    if (next == 0 && prev == 0 && list->m_head != resource)
+    if (!next && !prev && list->m_head != &resource)
         return;
     
-    resource->m_nextInAllResourcesList = 0;
-    resource->m_prevInAllResourcesList = 0;
+    resource.m_nextInAllResourcesList = nullptr;
+    resource.m_prevInAllResourcesList = nullptr;
     
     if (next)
         next->m_prevInAllResourcesList = prev;
-    else if (list->m_tail == resource)
+    else if (list->m_tail == &resource)
         list->m_tail = prev;
 
     if (prev)
         prev->m_nextInAllResourcesList = next;
-    else if (list->m_head == resource)
+    else if (list->m_head == &resource)
         list->m_head = next;
 }
 
-void MemoryCache::insertInLRUList(CachedResource* resource)
+void MemoryCache::insertInLRUList(CachedResource& resource)
 {
     // Make sure we aren't in some list already.
-    ASSERT(!resource->m_nextInAllResourcesList && !resource->m_prevInAllResourcesList);
-    ASSERT(resource->inCache());
-    ASSERT(resource->accessCount() > 0);
+    ASSERT(!resource.m_nextInAllResourcesList && !resource.m_prevInAllResourcesList);
+    ASSERT(resource.inCache());
+    ASSERT(resource.accessCount() > 0);
     
     LRUList* list = lruListFor(resource);
 
-    resource->m_nextInAllResourcesList = list->m_head;
+    resource.m_nextInAllResourcesList = list->m_head;
     if (list->m_head)
-        list->m_head->m_prevInAllResourcesList = resource;
-    list->m_head = resource;
+        list->m_head->m_prevInAllResourcesList = &resource;
+    list->m_head = &resource;
     
-    if (!resource->m_nextInAllResourcesList)
-        list->m_tail = resource;
+    if (!resource.m_nextInAllResourcesList)
+        list->m_tail = &resource;
         
 #if !ASSERT_DISABLED
     // Verify that we are in now in the list like we should be.
     list = lruListFor(resource);
     bool found = false;
     for (CachedResource* current = list->m_head; current; current = current->m_nextInAllResourcesList) {
-        if (current == resource) {
+        if (current == &resource) {
             found = true;
             break;
         }
@@ -537,33 +538,33 @@ void MemoryCache::insertInLRUList(CachedResource* resource)
 
 }
 
-void MemoryCache::resourceAccessed(CachedResource* resource)
+void MemoryCache::resourceAccessed(CachedResource& resource)
 {
-    ASSERT(resource->inCache());
+    ASSERT(resource.inCache());
     
     // Need to make sure to remove before we increase the access count, since
     // the queue will possibly change.
     removeFromLRUList(resource);
     
     // If this is the first time the resource has been accessed, adjust the size of the cache to account for its initial size.
-    if (!resource->accessCount())
-        adjustSize(resource->hasClients(), resource->size());
+    if (!resource.accessCount())
+        adjustSize(resource.hasClients(), resource.size());
     
     // Add to our access count.
-    resource->increaseAccessCount();
+    resource.increaseAccessCount();
     
     // Now insert into the new queue.
     insertInLRUList(resource);
 }
 
-void MemoryCache::removeResourcesWithOrigin(SecurityOrigin* origin)
+void MemoryCache::removeResourcesWithOrigin(SecurityOrigin& origin)
 {
     Vector<CachedResource*> resourcesWithOrigin;
 
     for (auto& resources : m_sessionResources) {
         CachedResourceMap::iterator e = resources.value->end();
 #if ENABLE(CACHE_PARTITIONING)
-        String originPartition = ResourceRequest::partitionName(origin->host());
+        String originPartition = ResourceRequest::partitionName(origin.host());
 #endif
 
         for (CachedResourceMap::iterator it = resources.value->begin(); it != e; ++it) {
@@ -581,7 +582,7 @@ void MemoryCache::removeResourcesWithOrigin(SecurityOrigin* origin)
                 RefPtr<SecurityOrigin> resourceOrigin = SecurityOrigin::createFromString(resource->url());
                 if (!resourceOrigin)
                     continue;
-                if (resourceOrigin->equal(origin))
+                if (resourceOrigin->equal(&origin))
                     resourcesWithOrigin.append(resource);
 #if ENABLE(CACHE_PARTITIONING)
             }
@@ -589,8 +590,8 @@ void MemoryCache::removeResourcesWithOrigin(SecurityOrigin* origin)
         }
     }
 
-    for (size_t i = 0; i < resourcesWithOrigin.size(); ++i)
-        remove(resourcesWithOrigin[i]);
+    for (auto* resource : resourcesWithOrigin)
+        remove(*resource);
 }
 
 void MemoryCache::getOriginsWithCache(SecurityOriginSet& origins)
@@ -613,28 +614,28 @@ void MemoryCache::getOriginsWithCache(SecurityOriginSet& origins)
     }
 }
 
-void MemoryCache::removeFromLiveDecodedResourcesList(CachedResource* resource)
+void MemoryCache::removeFromLiveDecodedResourcesList(CachedResource& resource)
 {
-    m_liveDecodedResources.remove(resource);
+    m_liveDecodedResources.remove(&resource);
 }
 
-void MemoryCache::insertInLiveDecodedResourcesList(CachedResource* resource)
+void MemoryCache::insertInLiveDecodedResourcesList(CachedResource& resource)
 {
     // Make sure we aren't in the list already.
-    ASSERT(!m_liveDecodedResources.contains(resource));
-    m_liveDecodedResources.add(resource);
+    ASSERT(!m_liveDecodedResources.contains(&resource));
+    m_liveDecodedResources.add(&resource);
 }
 
-void MemoryCache::addToLiveResourcesSize(CachedResource* resource)
+void MemoryCache::addToLiveResourcesSize(CachedResource& resource)
 {
-    m_liveSize += resource->size();
-    m_deadSize -= resource->size();
+    m_liveSize += resource.size();
+    m_deadSize -= resource.size();
 }
 
-void MemoryCache::removeFromLiveResourcesSize(CachedResource* resource)
+void MemoryCache::removeFromLiveResourcesSize(CachedResource& resource)
 {
-    m_liveSize -= resource->size();
-    m_deadSize += resource->size();
+    m_liveSize -= resource.size();
+    m_deadSize += resource.size();
 }
 
 void MemoryCache::adjustSize(bool live, int delta)
@@ -648,30 +649,29 @@ void MemoryCache::adjustSize(bool live, int delta)
     }
 }
 
-void MemoryCache::removeRequestFromSessionCaches(ScriptExecutionContext* context, const ResourceRequest& request)
+void MemoryCache::removeRequestFromSessionCaches(ScriptExecutionContext& context, const ResourceRequest& request)
 {
-    ASSERT(context);
-    if (is<WorkerGlobalScope>(*context)) {
+    if (is<WorkerGlobalScope>(context)) {
         CrossThreadResourceRequestData* requestData = request.copyData().leakPtr();
-        downcast<WorkerGlobalScope>(*context).thread().workerLoaderProxy().postTaskToLoader([requestData] (ScriptExecutionContext& context) {
+        downcast<WorkerGlobalScope>(context).thread().workerLoaderProxy().postTaskToLoader([requestData] (ScriptExecutionContext& context) {
             OwnPtr<ResourceRequest> request(ResourceRequest::adopt(adoptPtr(requestData)));
-            MemoryCache::removeRequestFromSessionCaches(&context, *request);
+            MemoryCache::removeRequestFromSessionCaches(context, *request);
         });
         return;
     }
 
     for (auto& resources : memoryCache().m_sessionResources) {
         if (CachedResource* resource = memoryCache().resourceForRequestImpl(request, *resources.value))
-            memoryCache().remove(resource);
+            memoryCache().remove(*resource);
     }
 }
 
-void MemoryCache::TypeStatistic::addResource(CachedResource* o)
+void MemoryCache::TypeStatistic::addResource(CachedResource& resource)
 {
     count++;
-    size += o->size();
-    liveSize += o->hasClients() ? o->size() : 0;
-    decodedSize += o->decodedSize();
+    size += resource.size();
+    liveSize += resource.hasClients() ? resource.size() : 0;
+    decodedSize += resource.decodedSize();
 }
 
 MemoryCache::Statistics MemoryCache::getStatistics()
@@ -683,11 +683,11 @@ MemoryCache::Statistics MemoryCache::getStatistics()
         for (CachedResourceMap::iterator i = resources.value->begin(); i != e; ++i) {
 #if ENABLE(CACHE_PARTITIONING)
             for (CachedResourceItem::iterator itemIterator = i->value->begin(); itemIterator != i->value->end(); ++itemIterator) {
-                CachedResource* resource = itemIterator->value;
+                CachedResource& resource = *itemIterator->value;
 #else
-                CachedResource* resource = i->value;
+                CachedResource& resource = *i->value;
 #endif
-                switch (resource->type()) {
+                switch (resource.type()) {
                 case CachedResource::ImageResource:
                     stats.images.addResource(resource);
                     break;
@@ -726,17 +726,17 @@ void MemoryCache::setDisabled(bool disabled)
         return;
 
     for (;;) {
-        SessionCachedResourceMap::iterator sessionIterator = m_sessionResources.begin();
+        auto sessionIterator = m_sessionResources.begin();
         if (sessionIterator == m_sessionResources.end())
             break;
-        CachedResourceMap::iterator outerIterator = sessionIterator->value->begin();
+        auto outerIterator = sessionIterator->value->begin();
         if (outerIterator == sessionIterator->value->end())
             break;
 #if ENABLE(CACHE_PARTITIONING)
-        CachedResourceItem::iterator innerIterator = outerIterator->value->begin();
-        remove(innerIterator->value);
+        auto innerIterator = outerIterator->value->begin();
+        remove(*innerIterator->value);
 #else
-        remove(outerIterator->value);
+        remove(*outerIterator->value);
 #endif
     }
 }
index 523ed4a..91cf7d3 100644 (file)
@@ -78,7 +78,7 @@ public:
         { 
         }
 
-        void addResource(CachedResource*);
+        void addResource(CachedResource&);
     };
     
     struct Statistics {
@@ -92,13 +92,13 @@ public:
     WEBCORE_EXPORT CachedResource* resourceForURL(const URL&, SessionID = SessionID::defaultSessionID());
     WEBCORE_EXPORT CachedResource* resourceForRequest(const ResourceRequest&, SessionID);
 
-    bool add(CachedResource*);
-    void remove(CachedResource*);
+    bool add(CachedResource&);
+    void remove(CachedResource&);
 
     static URL removeFragmentIdentifierIfNeeded(const URL& originalURL);
     
-    void revalidationSucceeded(CachedResource* revalidatingResource, const ResourceResponse&);
-    void revalidationFailed(CachedResource* revalidatingResource);
+    void revalidationSucceeded(CachedResource& revalidatingResource, const ResourceResponse&);
+    void revalidationFailed(CachedResource& revalidatingResource);
     
     // Sets the cache's memory capacities, in bytes. These will hold only approximately, 
     // since the decoded cost of resources like scripts and stylesheets is not known.
@@ -120,29 +120,29 @@ public:
     std::chrono::milliseconds deadDecodedDataDeletionInterval() const { return m_deadDecodedDataDeletionInterval; }
 
     // Calls to put the cached resource into and out of LRU lists.
-    void insertInLRUList(CachedResource*);
-    void removeFromLRUList(CachedResource*);
+    void insertInLRUList(CachedResource&);
+    void removeFromLRUList(CachedResource&);
 
     // Called to adjust the cache totals when a resource changes size.
     void adjustSize(bool live, int delta);
 
     // Track decoded resources that are in the cache and referenced by a Web page.
-    void insertInLiveDecodedResourcesList(CachedResource*);
-    void removeFromLiveDecodedResourcesList(CachedResource*);
+    void insertInLiveDecodedResourcesList(CachedResource&);
+    void removeFromLiveDecodedResourcesList(CachedResource&);
 
-    void addToLiveResourcesSize(CachedResource*);
-    void removeFromLiveResourcesSize(CachedResource*);
+    void addToLiveResourcesSize(CachedResource&);
+    void removeFromLiveResourcesSize(CachedResource&);
 
-    static void removeRequestFromSessionCaches(ScriptExecutionContext*, const ResourceRequest&);
+    static void removeRequestFromSessionCaches(ScriptExecutionContext&, const ResourceRequest&);
 
     // Function to collect cache statistics for the caches window in the Safari Debug menu.
     WEBCORE_EXPORT Statistics getStatistics();
     
-    void resourceAccessed(CachedResource*);
+    void resourceAccessed(CachedResource&);
     bool inLiveDecodedResourcesList(CachedResource& resource) const { return m_liveDecodedResources.contains(&resource); }
 
     typedef HashSet<RefPtr<SecurityOrigin>> SecurityOriginSet;
-    WEBCORE_EXPORT void removeResourcesWithOrigin(SecurityOrigin*);
+    WEBCORE_EXPORT void removeResourcesWithOrigin(SecurityOrigin&);
     WEBCORE_EXPORT void getOriginsWithCache(SecurityOriginSet& origins);
 
 #if USE(CG)
@@ -176,7 +176,7 @@ private:
     MemoryCache();
     ~MemoryCache(); // Not implemented to make sure nobody accidentally calls delete -- WebCore does not delete singletons.
 
-    LRUList* lruListFor(CachedResource*);
+    LRUList* lruListFor(CachedResource&);
 #ifndef NDEBUG
     void dumpStats();
     void dumpLRULists(bool includeLive) const;
index 4a7acd8..4299a40 100644 (file)
@@ -218,7 +218,7 @@ void MaskImageOperation::notifyFinished(CachedResource* resource)
                 // Let the cached resource loader of the document which requested this mask keep a handle to this
                 // cached image to ensure it only gets deleted when it should.
                 if (m_cachedResourceLoader.get())
-                    m_cachedResourceLoader->addCachedResource(m_styleImage->cachedImage());
+                    m_cachedResourceLoader->addCachedResource(*m_styleImage->cachedImage());
             }
             
             // Destroy the current SVG document as its no longer needed
index aa3cdb9..a859618 100644 (file)
@@ -1,5 +1,15 @@
 2015-01-30  Chris Dumez  <cdumez@apple.com>
 
+        Update MemoryCache code to use more references instead of pointers
+        https://bugs.webkit.org/show_bug.cgi?id=141099
+
+        Reviewed by Andreas Kling.
+
+        * WebProcess/ResourceCache/WebResourceCacheManager.cpp:
+        (WebKit::WebResourceCacheManager::clearCacheForOrigin):
+
+2015-01-30  Chris Dumez  <cdumez@apple.com>
+
         Rename shared() static member functions to singleton() for singleton classes.
         https://bugs.webkit.org/show_bug.cgi?id=141088
 
index 24fef19..db82cea 100644 (file)
@@ -114,7 +114,7 @@ void WebResourceCacheManager::clearCacheForOrigin(const SecurityOriginData& orig
     if (!origin)
         return;
 
-    memoryCache().removeResourcesWithOrigin(origin.get());
+    memoryCache().removeResourcesWithOrigin(*origin);
 
 #if USE(CFURLCACHE)
     if (resourceCachesToClear != InMemoryResourceCachesOnly) {