Changeset 195770 in webkit


Ignore:
Timestamp:
Jan 28, 2016 11:52:57 AM (8 years ago)
Author:
jer.noble@apple.com
Message:

Allow CachedResourceLoader clients to opt out of the MemoryCache.
https://bugs.webkit.org/show_bug.cgi?id=153549

Reviewed by Darin Adler.

Add a flag to ResourceLoaderOptions which allow loader clients to opt out of having
resources stored in the MemoryCache.

  • loader/ResourceLoaderOptions.h:

(WebCore::ResourceLoaderOptions::ResourceLoaderOptions):
(WebCore::ResourceLoaderOptions::cachingPolicy):

Existing clients will have to add the (default) AllowCaching flag when they create a
ResourceLoaderOptions object.

  • loader/DocumentLoader.cpp:

(WebCore::DocumentLoader::startLoadingMainResource):

  • loader/MediaResourceLoader.cpp:

(WebCore::MediaResourceLoader::start):

  • loader/NetscapePlugInStreamLoader.cpp:

(WebCore::NetscapePlugInStreamLoader::NetscapePlugInStreamLoader):
(WebCore::ResourceLoaderOptions::setCachingPolicy):

  • loader/icon/IconLoader.cpp:

(WebCore::IconLoader::startLoading):

  • platform/graphics/avfoundation/objc/WebCoreAVFResourceLoader.mm:

(WebCore::WebCoreAVFResourceLoader::startLoading):

Every time the CachedResource or CachedResourceLoader accesses the MemoryCache, check
to see whether the resource or the request have allowed caching before adding resources
to, removing resources from, or sourcing resource data from the MemoryCache.

  • loader/cache/CachedResource.cpp:

(WebCore::CachedResource::~CachedResource):
(WebCore::CachedResource::failBeforeStarting):
(WebCore::CachedResource::addClientToSet):
(WebCore::CachedResource::removeClient):
(WebCore::CachedResource::setDecodedSize):
(WebCore::CachedResource::setEncodedSize):
(WebCore::CachedResource::didAccessDecodedData):

  • loader/cache/CachedResource.h:

(WebCore::CachedResource::allowsCaching):

  • loader/cache/CachedResourceLoader.cpp:

(WebCore::CachedResourceLoader::requestUserCSSStyleSheet):
(WebCore::CachedResourceLoader::requestResource):
(WebCore::CachedResourceLoader::revalidateResource):
(WebCore::CachedResourceLoader::loadResource):
(WebCore::CachedResourceLoader::printPreloadStats):
(WebCore::CachedResourceLoader::defaultCachedResourceOptions):

  • loader/cache/CachedResourceRequest.h:

(WebCore::CachedResourceRequest::allowsCaching):

Location:
trunk/Source/WebCore
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r195769 r195770  
     12016-01-27  Jer Noble  <jer.noble@apple.com>
     2
     3        Allow CachedResourceLoader clients to opt out of the MemoryCache.
     4        https://bugs.webkit.org/show_bug.cgi?id=153549
     5
     6        Reviewed by Darin Adler.
     7
     8        Add a flag to ResourceLoaderOptions which allow loader clients to opt out of having
     9        resources stored in the MemoryCache.
     10
     11        * loader/ResourceLoaderOptions.h:
     12        (WebCore::ResourceLoaderOptions::ResourceLoaderOptions):
     13        (WebCore::ResourceLoaderOptions::cachingPolicy):
     14
     15        Existing clients will have to add the (default) AllowCaching flag when they create a
     16        ResourceLoaderOptions object.
     17
     18        * loader/DocumentLoader.cpp:
     19        (WebCore::DocumentLoader::startLoadingMainResource):
     20        * loader/MediaResourceLoader.cpp:
     21        (WebCore::MediaResourceLoader::start):
     22        * loader/NetscapePlugInStreamLoader.cpp:
     23        (WebCore::NetscapePlugInStreamLoader::NetscapePlugInStreamLoader):
     24        (WebCore::ResourceLoaderOptions::setCachingPolicy):
     25        * loader/icon/IconLoader.cpp:
     26        (WebCore::IconLoader::startLoading):
     27        * platform/graphics/avfoundation/objc/WebCoreAVFResourceLoader.mm:
     28        (WebCore::WebCoreAVFResourceLoader::startLoading):
     29
     30        Every time the CachedResource or CachedResourceLoader accesses the MemoryCache, check
     31        to see whether the resource or the request have allowed caching before adding resources
     32        to, removing resources from, or sourcing resource data from the MemoryCache.
     33
     34        * loader/cache/CachedResource.cpp:
     35        (WebCore::CachedResource::~CachedResource):
     36        (WebCore::CachedResource::failBeforeStarting):
     37        (WebCore::CachedResource::addClientToSet):
     38        (WebCore::CachedResource::removeClient):
     39        (WebCore::CachedResource::setDecodedSize):
     40        (WebCore::CachedResource::setEncodedSize):
     41        (WebCore::CachedResource::didAccessDecodedData):
     42        * loader/cache/CachedResource.h:
     43        (WebCore::CachedResource::allowsCaching):
     44        * loader/cache/CachedResourceLoader.cpp:
     45        (WebCore::CachedResourceLoader::requestUserCSSStyleSheet):
     46        (WebCore::CachedResourceLoader::requestResource):
     47        (WebCore::CachedResourceLoader::revalidateResource):
     48        (WebCore::CachedResourceLoader::loadResource):
     49        (WebCore::CachedResourceLoader::printPreloadStats):
     50        (WebCore::CachedResourceLoader::defaultCachedResourceOptions):
     51        * loader/cache/CachedResourceRequest.h:
     52        (WebCore::CachedResourceRequest::allowsCaching):
     53
    1542016-01-28  Enrica Casucci  <enrica@apple.com>
    255
  • trunk/Source/WebCore/loader/DocumentLoader.cpp

    r195452 r195770  
    14761476    request.makeUnconditional();
    14771477
    1478     static NeverDestroyed<ResourceLoaderOptions> mainResourceLoadOptions(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, IncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading);
     1478    static NeverDestroyed<ResourceLoaderOptions> mainResourceLoadOptions(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, IncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching);
    14791479    CachedResourceRequest cachedResourceRequest(request, mainResourceLoadOptions);
    14801480    cachedResourceRequest.setInitiator(*this);
  • trunk/Source/WebCore/loader/MediaResourceLoader.cpp

    r195452 r195770  
    6262
    6363    // ContentSecurityPolicyImposition::DoPolicyCheck is a placeholder value. It does not affect the request since Content Security Policy does not apply to raw resources.
    64     CachedResourceRequest cacheRequest(request, ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, bufferingPolicy, allowCredentials, DoNotAskClientForCrossOriginCredentials, ClientDidNotRequestCredentials, DoSecurityCheck, corsPolicy, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading));
     64    CachedResourceRequest cacheRequest(request, ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, bufferingPolicy, allowCredentials, DoNotAskClientForCrossOriginCredentials, ClientDidNotRequestCredentials, DoSecurityCheck, corsPolicy, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching));
    6565
    6666    if (!m_crossOriginMode.isNull())
  • trunk/Source/WebCore/loader/NetscapePlugInStreamLoader.cpp

    r195010 r195770  
    4444// See <https://bugs.webkit.org/show_bug.cgi?id=146663>.
    4545NetscapePlugInStreamLoader::NetscapePlugInStreamLoader(Frame* frame, NetscapePlugInStreamLoaderClient* client)
    46     : ResourceLoader(frame, ResourceLoaderOptions(SendCallbacks, SniffContent, DoNotBufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading))
     46    : ResourceLoader(frame, ResourceLoaderOptions(SendCallbacks, SniffContent, DoNotBufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching))
    4747    , m_client(client)
    4848{
  • trunk/Source/WebCore/loader/ResourceLoaderOptions.h

    r195010 r195770  
    7777};
    7878
     79enum class CachingPolicy : uint8_t {
     80    AllowCaching,
     81    DisallowCaching
     82};
     83
    7984struct ResourceLoaderOptions {
    8085    ResourceLoaderOptions()
     
    9196    }
    9297
    93     ResourceLoaderOptions(SendCallbackPolicy sendLoadCallbacks, ContentSniffingPolicy sniffContent, DataBufferingPolicy dataBufferingPolicy, StoredCredentials allowCredentials, ClientCredentialPolicy credentialPolicy, CredentialRequest credentialRequest, SecurityCheckPolicy securityCheck, RequestOriginPolicy requestOriginPolicy, CertificateInfoPolicy certificateInfoPolicy, ContentSecurityPolicyImposition contentSecurityPolicyImposition, DefersLoadingPolicy defersLoadingPolicy)
     98    ResourceLoaderOptions(SendCallbackPolicy sendLoadCallbacks, ContentSniffingPolicy sniffContent, DataBufferingPolicy dataBufferingPolicy, StoredCredentials allowCredentials, ClientCredentialPolicy credentialPolicy, CredentialRequest credentialRequest, SecurityCheckPolicy securityCheck, RequestOriginPolicy requestOriginPolicy, CertificateInfoPolicy certificateInfoPolicy, ContentSecurityPolicyImposition contentSecurityPolicyImposition, DefersLoadingPolicy defersLoadingPolicy, CachingPolicy cachingPolicy)
    9499        : m_sendLoadCallbacks(sendLoadCallbacks)
    95100        , m_sniffContent(sniffContent)
     
    103108        , m_contentSecurityPolicyImposition(contentSecurityPolicyImposition)
    104109        , m_defersLoadingPolicy(defersLoadingPolicy)
     110        , m_cachingPolicy(cachingPolicy)
    105111    {
    106112    }
     
    128134    DefersLoadingPolicy defersLoadingPolicy() const { return m_defersLoadingPolicy; }
    129135    void setDefersLoadingPolicy(DefersLoadingPolicy defersLoadingPolicy) { m_defersLoadingPolicy = defersLoadingPolicy; }
     136    CachingPolicy cachingPolicy() const { return m_cachingPolicy; }
     137    void setCachingPolicy(CachingPolicy cachingPolicy) { m_cachingPolicy = cachingPolicy; }
    130138
    131139    unsigned m_sendLoadCallbacks : 1;
     
    140148    ContentSecurityPolicyImposition m_contentSecurityPolicyImposition { ContentSecurityPolicyImposition::DoPolicyCheck };
    141149    DefersLoadingPolicy m_defersLoadingPolicy { DefersLoadingPolicy::AllowDefersLoading };
     150    CachingPolicy m_cachingPolicy { CachingPolicy::AllowCaching };
    142151};
    143152
  • trunk/Source/WebCore/loader/cache/CachedResource.cpp

    r195452 r195770  
    160160    ASSERT(!inCache());
    161161    ASSERT(!m_deleted);
    162     ASSERT(url().isNull() || MemoryCache::singleton().resourceForRequest(resourceRequest(), sessionID()) != this);
     162    ASSERT(url().isNull() || !allowsCaching() || MemoryCache::singleton().resourceForRequest(resourceRequest(), sessionID()) != this);
    163163
    164164#ifndef NDEBUG
     
    175175    // FIXME: What if resources in other frames were waiting for this revalidation?
    176176    LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
    177     if (m_resourceToRevalidate)
     177    if (allowsCaching() && m_resourceToRevalidate)
    178178        MemoryCache::singleton().revalidationFailed(*this);
    179179    error(CachedResource::LoadError);
     
    453453            m_preloadResult = PreloadReferenced;
    454454    }
    455     if (!hasClients() && inCache())
     455    if (allowsCaching() && !hasClients() && inCache())
    456456        MemoryCache::singleton().addToLiveResourcesSize(*this);
    457457
     
    484484
    485485    bool deleted = deleteIfPossible();
    486     if (!deleted && !hasClients()) {
     486    if (allowsCaching() && !deleted && !hasClients()) {
    487487        auto& memoryCache = MemoryCache::singleton();
    488488        if (inCache()) {
     
    542542    // The object must be moved to a different queue, since its size has been changed.
    543543    // Remove before updating m_decodedSize, so we find the resource in the correct LRU list.
    544     if (inCache())
     544    if (allowsCaching() && inCache())
    545545        MemoryCache::singleton().removeFromLRUList(*this);
    546546   
    547547    m_decodedSize = size;
    548548   
    549     if (inCache()) {
     549    if (allowsCaching() && inCache()) {
    550550        auto& memoryCache = MemoryCache::singleton();
    551551        // Now insert into the new LRU list.
     
    579579    // The object must be moved to a different queue, since its size has been changed.
    580580    // Remove before updating m_encodedSize, so we find the resource in the correct LRU list.
    581     if (inCache())
     581    if (allowsCaching() && inCache())
    582582        MemoryCache::singleton().removeFromLRUList(*this);
    583583
    584584    m_encodedSize = size;
    585585
    586     if (inCache()) {
     586    if (allowsCaching() && inCache()) {
    587587        auto& memoryCache = MemoryCache::singleton();
    588588        memoryCache.insertInLRUList(*this);
     
    595595    m_lastDecodedAccessTime = timeStamp;
    596596   
    597     if (inCache()) {
     597    if (allowsCaching() && inCache()) {
    598598        auto& memoryCache = MemoryCache::singleton();
    599599        if (memoryCache.inLiveDecodedResourcesList(*this)) {
  • trunk/Source/WebCore/loader/cache/CachedResource.h

    r195430 r195770  
    219219    bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks() == SendCallbacks; }
    220220    DataBufferingPolicy dataBufferingPolicy() const { return m_options.dataBufferingPolicy(); }
     221
     222    bool allowsCaching() const { return m_options.cachingPolicy() == CachingPolicy::AllowCaching; }
    221223   
    222224    virtual void destroyDecodedData() { }
  • trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp

    r195010 r195770  
    222222
    223223    auto& memoryCache = MemoryCache::singleton();
    224     if (CachedResource* existing = memoryCache.resourceForRequest(request.resourceRequest(), sessionID())) {
    225         if (is<CachedCSSStyleSheet>(*existing))
    226             return downcast<CachedCSSStyleSheet>(existing);
    227         memoryCache.remove(*existing);
    228     }
     224    if (request.allowsCaching()) {
     225        if (CachedResource* existing = memoryCache.resourceForRequest(request.resourceRequest(), sessionID())) {
     226            if (is<CachedCSSStyleSheet>(*existing))
     227                return downcast<CachedCSSStyleSheet>(existing);
     228            memoryCache.remove(*existing);
     229        }
     230    }
     231
    229232    if (url.string() != request.resourceRequest().url())
    230233        request.mutableResourceRequest().setURL(url);
     
    232235    CachedResourceHandle<CachedCSSStyleSheet> userSheet = new CachedCSSStyleSheet(request.resourceRequest(), request.charset(), sessionID());
    233236
    234     memoryCache.add(*userSheet);
     237    if (request.allowsCaching())
     238        memoryCache.add(*userSheet);
    235239    // FIXME: loadResource calls setOwningCachedResourceLoader() if the resource couldn't be added to cache. Does this function need to call it, too?
    236240
    237     userSheet->load(*this, ResourceLoaderOptions(DoNotSendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::SkipPolicyCheck, DefersLoadingPolicy::AllowDefersLoading));
     241    userSheet->load(*this, ResourceLoaderOptions(DoNotSendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::SkipPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching));
    238242   
    239243    return userSheet;
     
    556560
    557561    auto& memoryCache = MemoryCache::singleton();
    558     if (memoryCache.disabled()) {
     562    if (request.allowsCaching() && memoryCache.disabled()) {
    559563        DocumentResourceMap::iterator it = m_documentResources.find(url.string());
    560564        if (it != m_documentResources.end()) {
     
    571575#endif
    572576
    573     resource = memoryCache.resourceForRequest(request.resourceRequest(), sessionID());
     577    if (request.allowsCaching())
     578        resource = memoryCache.resourceForRequest(request.resourceRequest(), sessionID());
    574579
    575580    logMemoryCacheResourceRequest(frame(), resource ? DiagnosticLoggingKeys::inMemoryCacheKey() : DiagnosticLoggingKeys::notInMemoryCacheKey());
     
    609614        // We don't support immediate loads, but we do support immediate failure.
    610615        if (resource->errorOccurred()) {
    611             if (resource->inCache())
     616            if (resource->allowsCaching() && resource->inCache())
    612617                memoryCache.remove(*resource);
    613618            return nullptr;
     
    637642    ASSERT(!resource->resourceToRevalidate());
    638643    ASSERT(resource->sessionID() == sessionID());
     644    ASSERT(resource->allowsCaching());
    639645
    640646    CachedResourceHandle<CachedResource> newResource = createResource(resource->type(), resource->resourceRequest(), resource->encoding(), resource->sessionID());
     
    662668    CachedResourceHandle<CachedResource> resource = createResource(type, request.mutableResourceRequest(), request.charset(), sessionID());
    663669
    664     if (!memoryCache.add(*resource))
     670    if (request.allowsCaching() && !memoryCache.add(*resource))
    665671        resource->setOwningCachedResourceLoader(this);
    666672#if ENABLE(RESOURCE_TIMING)
     
    11521158        }
    11531159       
    1154         if (resource->errorOccurred())
     1160        if (resource->errorOccurred() && resource->preloadResult() == CachedResource::PreloadNotReferenced)
    11551161            MemoryCache::singleton().remove(resource);
    11561162       
     
    11701176const ResourceLoaderOptions& CachedResourceLoader::defaultCachedResourceOptions()
    11711177{
    1172     static ResourceLoaderOptions options(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading);
     1178    static ResourceLoaderOptions options(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching);
    11731179    return options;
    11741180}
  • trunk/Source/WebCore/loader/cache/CachedResourceRequest.h

    r186390 r195770  
    6161    void setInitiator(const AtomicString& name);
    6262    const AtomicString& initiatorName() const;
     63    bool allowsCaching() const { return m_options.cachingPolicy() == CachingPolicy::AllowCaching; }
    6364
    6465    void setInitiator(DocumentLoader&);
  • trunk/Source/WebCore/loader/icon/IconLoader.cpp

    r195010 r195770  
    6060
    6161    // ContentSecurityPolicyImposition::DoPolicyCheck is a placeholder value. It does not affect the request since Content Security Policy does not apply to raw resources.
    62     CachedResourceRequest request(ResourceRequest(m_frame.loader().icon().url()), ResourceLoaderOptions(SendCallbacks, SniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForAnyCredentials, ClientDidNotRequestCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading));
     62    CachedResourceRequest request(ResourceRequest(m_frame.loader().icon().url()), ResourceLoaderOptions(SendCallbacks, SniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForAnyCredentials, ClientDidNotRequestCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching));
    6363
    6464    request.mutableResourceRequest().setPriority(ResourceLoadPriority::Low);
  • trunk/Source/WebCore/platform/graphics/avfoundation/objc/WebCoreAVFResourceLoader.mm

    r195764 r195770  
    6969
    7070    // ContentSecurityPolicyImposition::DoPolicyCheck is a placeholder value. It does not affect the request since Content Security Policy does not apply to raw resources.
    71     CachedResourceRequest request(nsRequest, ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, ClientDidNotRequestCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading));
     71    CachedResourceRequest request(nsRequest, ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, ClientDidNotRequestCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::DisallowCaching));
    7272
    7373    request.mutableResourceRequest().setPriority(ResourceLoadPriority::Low);
Note: See TracChangeset for help on using the changeset viewer.