Remove CachedResourceRequest::mutableResourceRequest
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 13 Oct 2016 10:34:15 +0000 (10:34 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 13 Oct 2016 10:34:15 +0000 (10:34 +0000)
https://bugs.webkit.org/show_bug.cgi?id=163277

Patch by Youenn Fablet <youenn@apple.com> on 2016-10-13
Reviewed by Sam Weinig.

No change of behavior.

Removing CachedResourceRequest::mutableResourceRequest requires call sites to either update the ResourceRequest
before creating the CachedResourceRequest or to add methods at CachedResourceRequest.

Adding CachedResourceRequest::releaseResourceRequest for CachedResource constructor.

Most new CachedResourceRequest methods are used by CachedResourceLoader which is passed a CachedResourceRequest.
This allows code in CachedResourceLoader to be easier to read.

* css/CSSImageSetValue.cpp:
(WebCore::CSSImageSetValue::loadBestFitImage):
* css/CSSImageValue.cpp:
(WebCore::CSSImageValue::loadImage):
* dom/ScriptElement.cpp:
(WebCore::ScriptElement::requestScriptWithCache):
* loader/CrossOriginAccessControl.cpp:
(WebCore::createAccessControlPreflightRequest):
* loader/CrossOriginAccessControl.h:
* loader/CrossOriginPreflightChecker.cpp:
(WebCore::CrossOriginPreflightChecker::startPreflight):
(WebCore::CrossOriginPreflightChecker::doPreflight):
* loader/DocumentThreadableLoader.cpp:
(WebCore::DocumentThreadableLoader::loadRequest):
* loader/MediaResourceLoader.cpp:
(WebCore::MediaResourceLoader::requestResource):
* loader/cache/CachedResource.cpp:
(WebCore::CachedResource::CachedResource):
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::requestImage):
(WebCore::CachedResourceLoader::requestUserCSSStyleSheet):
(WebCore::CachedResourceLoader::prepareFetch):
(WebCore::CachedResourceLoader::updateHTTPRequestHeaders):
(WebCore::CachedResourceLoader::requestResource):
(WebCore::acceptHeaderValueFromType): Deleted.
(WebCore::updateRequestAccordingCacheMode): Deleted.
* loader/cache/CachedResourceRequest.cpp:
(WebCore::CachedResourceRequest::setAsPotentiallyCrossOrigin):
(WebCore::CachedResourceRequest::updateForAccessControl):
(WebCore::CachedResourceRequest::upgradeInsecureRequestIfNeeded):
(WebCore::CachedResourceRequest::setDomainForCachePartition):
(WebCore::acceptHeaderValueFromType):
(WebCore::CachedResourceRequest::setAcceptHeaderIfNone):
(WebCore::CachedResourceRequest::updateAccordingCacheMode):
(WebCore::CachedResourceRequest::removeFragmentIdentifierIfNeeded):
(WebCore::CachedResourceRequest::applyBlockedStatus):
* loader/cache/CachedResourceRequest.h:
(WebCore::CachedResourceRequest::releaseResourceRequest):
(WebCore::CachedResourceRequest::setCachingPolicy):
(WebCore::CachedResourceRequest::mutableResourceRequest): Deleted.
(WebCore::CachedResourceRequest::setCacheModeToNoStore): Deleted.
* loader/icon/IconLoader.cpp:
(WebCore::IconLoader::startLoading):
* platform/graphics/avfoundation/cf/WebCoreAVCFResourceLoader.cpp:
(WebCore::WebCoreAVCFResourceLoader::startLoading):
* platform/graphics/avfoundation/objc/WebCoreAVFResourceLoader.mm:
(WebCore::WebCoreAVFResourceLoader::startLoading):

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

16 files changed:
Source/WebCore/ChangeLog
Source/WebCore/css/CSSImageSetValue.cpp
Source/WebCore/css/CSSImageValue.cpp
Source/WebCore/dom/ScriptElement.cpp
Source/WebCore/loader/CrossOriginAccessControl.cpp
Source/WebCore/loader/CrossOriginAccessControl.h
Source/WebCore/loader/CrossOriginPreflightChecker.cpp
Source/WebCore/loader/DocumentThreadableLoader.cpp
Source/WebCore/loader/MediaResourceLoader.cpp
Source/WebCore/loader/cache/CachedResource.cpp
Source/WebCore/loader/cache/CachedResourceLoader.cpp
Source/WebCore/loader/cache/CachedResourceRequest.cpp
Source/WebCore/loader/cache/CachedResourceRequest.h
Source/WebCore/loader/icon/IconLoader.cpp
Source/WebCore/platform/graphics/avfoundation/cf/WebCoreAVCFResourceLoader.cpp
Source/WebCore/platform/graphics/avfoundation/objc/WebCoreAVFResourceLoader.mm

index eb594a7..b5077d1 100644 (file)
@@ -1,3 +1,68 @@
+2016-10-13  Youenn Fablet  <youenn@apple.com>
+
+        Remove CachedResourceRequest::mutableResourceRequest
+        https://bugs.webkit.org/show_bug.cgi?id=163277
+
+        Reviewed by Sam Weinig.
+
+        No change of behavior.
+
+        Removing CachedResourceRequest::mutableResourceRequest requires call sites to either update the ResourceRequest
+        before creating the CachedResourceRequest or to add methods at CachedResourceRequest.
+
+        Adding CachedResourceRequest::releaseResourceRequest for CachedResource constructor.
+
+        Most new CachedResourceRequest methods are used by CachedResourceLoader which is passed a CachedResourceRequest.
+        This allows code in CachedResourceLoader to be easier to read.
+
+        * css/CSSImageSetValue.cpp:
+        (WebCore::CSSImageSetValue::loadBestFitImage):
+        * css/CSSImageValue.cpp:
+        (WebCore::CSSImageValue::loadImage):
+        * dom/ScriptElement.cpp:
+        (WebCore::ScriptElement::requestScriptWithCache):
+        * loader/CrossOriginAccessControl.cpp:
+        (WebCore::createAccessControlPreflightRequest):
+        * loader/CrossOriginAccessControl.h:
+        * loader/CrossOriginPreflightChecker.cpp:
+        (WebCore::CrossOriginPreflightChecker::startPreflight):
+        (WebCore::CrossOriginPreflightChecker::doPreflight):
+        * loader/DocumentThreadableLoader.cpp:
+        (WebCore::DocumentThreadableLoader::loadRequest):
+        * loader/MediaResourceLoader.cpp:
+        (WebCore::MediaResourceLoader::requestResource):
+        * loader/cache/CachedResource.cpp:
+        (WebCore::CachedResource::CachedResource):
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::CachedResourceLoader::requestImage):
+        (WebCore::CachedResourceLoader::requestUserCSSStyleSheet):
+        (WebCore::CachedResourceLoader::prepareFetch):
+        (WebCore::CachedResourceLoader::updateHTTPRequestHeaders):
+        (WebCore::CachedResourceLoader::requestResource):
+        (WebCore::acceptHeaderValueFromType): Deleted.
+        (WebCore::updateRequestAccordingCacheMode): Deleted.
+        * loader/cache/CachedResourceRequest.cpp:
+        (WebCore::CachedResourceRequest::setAsPotentiallyCrossOrigin):
+        (WebCore::CachedResourceRequest::updateForAccessControl):
+        (WebCore::CachedResourceRequest::upgradeInsecureRequestIfNeeded):
+        (WebCore::CachedResourceRequest::setDomainForCachePartition):
+        (WebCore::acceptHeaderValueFromType):
+        (WebCore::CachedResourceRequest::setAcceptHeaderIfNone):
+        (WebCore::CachedResourceRequest::updateAccordingCacheMode):
+        (WebCore::CachedResourceRequest::removeFragmentIdentifierIfNeeded):
+        (WebCore::CachedResourceRequest::applyBlockedStatus):
+        * loader/cache/CachedResourceRequest.h:
+        (WebCore::CachedResourceRequest::releaseResourceRequest):
+        (WebCore::CachedResourceRequest::setCachingPolicy):
+        (WebCore::CachedResourceRequest::mutableResourceRequest): Deleted.
+        (WebCore::CachedResourceRequest::setCacheModeToNoStore): Deleted.
+        * loader/icon/IconLoader.cpp:
+        (WebCore::IconLoader::startLoading):
+        * platform/graphics/avfoundation/cf/WebCoreAVCFResourceLoader.cpp:
+        (WebCore::WebCoreAVCFResourceLoader::startLoading):
+        * platform/graphics/avfoundation/objc/WebCoreAVFResourceLoader.mm:
+        (WebCore::WebCoreAVFResourceLoader::startLoading):
+
 2016-10-13  Antti Koivisto  <antti@apple.com>
 
         Support scoped style for user agent shadow trees
index dd14e71..5a9c222 100644 (file)
@@ -90,6 +90,8 @@ CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor()
 std::pair<CachedImage*, float> CSSImageSetValue::loadBestFitImage(CachedResourceLoader& loader, const ResourceLoaderOptions& options)
 {
     Document* document = loader.document();
+    ASSERT(document);
+
     updateDeviceScaleFactor(*document);
 
     if (!m_accessedBestFitImage) {
@@ -101,10 +103,9 @@ std::pair<CachedImage*, float> CSSImageSetValue::loadBestFitImage(CachedResource
         ImageWithScale image = bestImageForScaleFactor();
         CachedResourceRequest request(ResourceRequest(document->completeURL(image.imageURL)), options);
         request.setInitiator(cachedResourceRequestInitiators().css);
-        if (options.mode == FetchOptions::Mode::Cors) {
-            ASSERT(document->securityOrigin());
-            updateRequestForAccessControl(request.mutableResourceRequest(), *document->securityOrigin(), options.allowCredentials);
-        }
+        if (options.mode == FetchOptions::Mode::Cors)
+            request.updateForAccessControl(*document);
+
         m_cachedImage = loader.requestImage(WTFMove(request));
         m_bestFitImageScaleFactor = image.scaleFactor;
     }
index 9dd8ef2..2f2a8dc 100644 (file)
@@ -72,8 +72,8 @@ CachedImage* CSSImageValue::loadImage(CachedResourceLoader& loader, const Resour
             request.setInitiator(m_initiatorName);
 
         if (options.mode == FetchOptions::Mode::Cors) {
-            ASSERT(loader.document()->securityOrigin());
-            updateRequestForAccessControl(request.mutableResourceRequest(), *loader.document()->securityOrigin(), options.allowCredentials);
+            ASSERT(loader.document());
+            request.updateForAccessControl(*loader.document());
         }
         m_cachedImage = loader.requestImage(WTFMove(request));
     }
index 2b69581..22cf852 100644 (file)
@@ -293,8 +293,7 @@ CachedResourceHandle<CachedScript> ScriptElement::requestScriptWithCache(const U
 
     CachedResourceRequest request(ResourceRequest(sourceURL), options);
     request.setAsPotentiallyCrossOrigin(m_element.attributeWithoutSynchronization(HTMLNames::crossoriginAttr), document);
-
-    document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request.mutableResourceRequest(), ContentSecurityPolicy::InsecureRequestType::Load);
+    request.upgradeInsecureRequestIfNeeded(document);
 
     request.setCharset(scriptCharset());
     request.setInitiator(&element());
index b45e5eb..4fedc4a 100644 (file)
@@ -103,13 +103,15 @@ void updateRequestForAccessControl(ResourceRequest& request, SecurityOrigin& sec
     request.setHTTPOrigin(securityOrigin.toString());
 }
 
-ResourceRequest createAccessControlPreflightRequest(const ResourceRequest& request, SecurityOrigin& securityOrigin)
+ResourceRequest createAccessControlPreflightRequest(const ResourceRequest& request, SecurityOrigin& securityOrigin, const String& referrer)
 {
     ResourceRequest preflightRequest(request.url());
     updateRequestForAccessControl(preflightRequest, securityOrigin, DoNotAllowStoredCredentials);
     preflightRequest.setHTTPMethod("OPTIONS");
     preflightRequest.setHTTPHeaderField(HTTPHeaderName::AccessControlRequestMethod, request.httpMethod());
     preflightRequest.setPriority(request.priority());
+    if (!referrer.isNull())
+        preflightRequest.setHTTPReferrer(referrer);
 
     const HTTPHeaderMap& requestHeaderFields = request.httpHeaderFields();
 
index b7239dc..2888201 100644 (file)
@@ -45,7 +45,7 @@ bool isOnAccessControlSimpleRequestHeaderWhitelist(HTTPHeaderName, const String&
 bool isOnAccessControlResponseHeaderWhitelist(const String&);
 
 void updateRequestForAccessControl(ResourceRequest&, SecurityOrigin&, StoredCredentials);
-ResourceRequest createAccessControlPreflightRequest(const ResourceRequest&, SecurityOrigin&);
+ResourceRequest createAccessControlPreflightRequest(const ResourceRequest&, SecurityOrigin&, const String&);
 
 bool isValidCrossOriginRedirectionURL(const URL&);
 void cleanRedirectedRequestForAccessControl(ResourceRequest&);
index 6b65042..0c8a98a 100644 (file)
@@ -109,13 +109,10 @@ void CrossOriginPreflightChecker::startPreflight()
     options.redirect = FetchOptions::Redirect::Manual;
     options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
 
-    CachedResourceRequest preflightRequest(createAccessControlPreflightRequest(m_request, m_loader.securityOrigin()), options);
+    CachedResourceRequest preflightRequest(createAccessControlPreflightRequest(m_request, m_loader.securityOrigin(), m_loader.referrer()), options);
     if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
         preflightRequest.setInitiator(m_loader.options().initiator);
 
-    if (!m_loader.referrer().isNull())
-        preflightRequest.mutableResourceRequest().setHTTPReferrer(m_loader.referrer());
-
     ASSERT(!m_resource);
     m_resource = m_loader.document().cachedResourceLoader().requestRawResource(WTFMove(preflightRequest));
     if (m_resource)
@@ -127,14 +124,11 @@ void CrossOriginPreflightChecker::doPreflight(DocumentThreadableLoader& loader,
     if (!loader.document().frame())
         return;
 
-    ResourceRequest preflightRequest = createAccessControlPreflightRequest(request, loader.securityOrigin());
+    ResourceRequest preflightRequest = createAccessControlPreflightRequest(request, loader.securityOrigin(), loader.referrer());
     ResourceError error;
     ResourceResponse response;
     RefPtr<SharedBuffer> data;
 
-    if (!loader.referrer().isNull())
-        preflightRequest.setHTTPReferrer(loader.referrer());
-
     unsigned identifier = loader.document().frame()->loader().loadResourceSynchronously(preflightRequest, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, error, response, data);
 
     if (!error.isNull()) {
index 4b4aad5..df328f3 100644 (file)
@@ -380,10 +380,10 @@ void DocumentThreadableLoader::loadRequest(ResourceRequest&& request, SecurityCh
         options.clientCredentialPolicy = m_sameOriginRequest ? ClientCredentialPolicy::MayAskClientForCredentials : ClientCredentialPolicy::CannotAskClientForCredentials;
         options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
 
+        request.setAllowCookies(m_options.allowCredentials == AllowStoredCredentials);
         CachedResourceRequest newRequest(WTFMove(request), options);
         if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
             newRequest.setInitiator(m_options.initiator);
-        newRequest.mutableResourceRequest().setAllowCookies(m_options.allowCredentials == AllowStoredCredentials);
         newRequest.setOrigin(&securityOrigin());
 
         ASSERT(!m_resource);
index 6474e56..c9f5f7a 100644 (file)
@@ -65,19 +65,17 @@ RefPtr<PlatformMediaResource> MediaResourceLoader::requestResource(ResourceReque
     DataBufferingPolicy bufferingPolicy = options & LoadOption::BufferData ? WebCore::BufferData : WebCore::DoNotBufferData;
     auto cachingPolicy = options & LoadOption::DisallowCaching ? CachingPolicy::DisallowCaching : CachingPolicy::AllowCaching;
 
+    request.setRequester(ResourceRequest::Requester::Media);
+#if HAVE(AVFOUNDATION_LOADER_DELEGATE) && PLATFORM(MAC)
+    // FIXME: Workaround for <rdar://problem/26071607>. We are not able to do CORS checking on 304 responses because they are usually missing the headers we need.
+    if (!m_crossOriginMode.isNull())
+        request.makeUnconditional();
+#endif
+
     // FIXME: Skip Content Security Policy check if the element that initiated this request is in a user-agent shadow tree. See <https://bugs.webkit.org/show_bug.cgi?id=155505>.
     CachedResourceRequest cacheRequest(WTFMove(request), ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, bufferingPolicy, AllowStoredCredentials, ClientCredentialPolicy::MayAskClientForCredentials, FetchOptions::Credentials::Include, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, cachingPolicy));
-
     cacheRequest.setAsPotentiallyCrossOrigin(m_crossOriginMode, *m_document);
 
-    cacheRequest.mutableResourceRequest().setRequester(ResourceRequest::Requester::Media);
-#if HAVE(AVFOUNDATION_LOADER_DELEGATE) && PLATFORM(MAC)
-    // FIXME: Workaround for <rdar://problem/26071607>. We are not able to do CORS checking on 304 responses because they
-    // are usually missing the headers we need.
-    if (cacheRequest.options().mode == FetchOptions::Mode::Cors)
-        cacheRequest.mutableResourceRequest().makeUnconditional();
-#endif
-
     CachedResourceHandle<CachedRawResource> resource = m_document->cachedResourceLoader().requestMedia(WTFMove(cacheRequest));
     if (!resource)
         return nullptr;
index ff462ac..42d7b52 100644 (file)
@@ -115,7 +115,7 @@ static std::chrono::milliseconds deadDecodedDataDeletionIntervalForResourceType(
 DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("CachedResource"));
 
 CachedResource::CachedResource(CachedResourceRequest&& request, Type type, SessionID sessionID)
-    : m_resourceRequest(WTFMove(request.mutableResourceRequest()))
+    : m_resourceRequest(request.releaseResourceRequest())
     , m_options(request.options())
     , m_decodedDataDeletionTimer(*this, &CachedResource::destroyDecodedData, deadDecodedDataDeletionIntervalForResourceType(type))
     , m_sessionID(sessionID)
index 52c6d40..eb45002 100644 (file)
@@ -51,7 +51,6 @@
 #include "FrameLoaderClient.h"
 #include "HTMLElement.h"
 #include "HTMLFrameOwnerElement.h"
-#include "HTTPHeaderValues.h"
 #include "LoaderStrategy.h"
 #include "LocalizedStrings.h"
 #include "Logging.h"
@@ -182,7 +181,7 @@ CachedResourceHandle<CachedImage> CachedResourceLoader::requestImage(CachedResou
     if (Frame* frame = this->frame()) {
         if (frame->loader().pageDismissalEventBeingDispatched() != FrameLoader::PageDismissalType::None) {
             if (Document* document = frame->document())
-                document->contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request.mutableResourceRequest(), ContentSecurityPolicy::InsecureRequestType::Load);
+                request.upgradeInsecureRequestIfNeeded(*document);
             URL requestURL = request.resourceRequest().url();
             if (requestURL.isValid() && canRequest(CachedResource::ImageResource, requestURL, request, ForPreload::No))
                 PingLoader::loadImage(*frame, requestURL);
@@ -219,10 +218,9 @@ CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestCSSStyleS
 
 CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestUserCSSStyleSheet(CachedResourceRequest&& request)
 {
-    URL url = MemoryCache::removeFragmentIdentifierIfNeeded(request.resourceRequest().url());
-
 #if ENABLE(CACHE_PARTITIONING)
-    request.mutableResourceRequest().setDomainForCachePartition(document()->topOrigin()->domainForCachePartition());
+    ASSERT(document());
+    request.setDomainForCachePartition(*document());
 #endif
 
     auto& memoryCache = MemoryCache::singleton();
@@ -234,8 +232,7 @@ CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestUserCSSSt
         }
     }
 
-    if (url.string() != request.resourceRequest().url())
-        request.mutableResourceRequest().setURL(url);
+    request.removeFragmentIdentifierIfNeeded();
 
     CachedResourceHandle<CachedCSSStyleSheet> userSheet = new CachedCSSStyleSheet(WTFMove(request), sessionID());
 
@@ -633,27 +630,6 @@ static inline void logMemoryCacheResourceRequest(Frame* frame, const String& des
         frame->page()->diagnosticLoggingClient().logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceRequestKey(), description, value, ShouldSample::Yes);
 }
 
-static inline String acceptHeaderValueFromType(CachedResource::Type type)
-{
-    switch (type) {
-    case CachedResource::Type::MainResource:
-        return ASCIILiteral("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
-    case CachedResource::Type::ImageResource:
-        return ASCIILiteral("image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5");
-    case CachedResource::Type::CSSStyleSheet:
-        return ASCIILiteral("text/css,*/*;q=0.1");
-    case CachedResource::Type::SVGDocumentResource:
-        return ASCIILiteral("image/svg+xml");
-#if ENABLE(XSLT)
-    case CachedResource::Type::XSLStyleSheet:
-        // FIXME: This should accept more general xml formats */*+xml, image/svg+xml for example.
-        return ASCIILiteral("text/xml,application/xml,application/xhtml+xml,text/xsl,application/rss+xml,application/atom+xml");
-#endif
-    default:
-        return ASCIILiteral("*/*");
-    }
-}
-
 void CachedResourceLoader::prepareFetch(CachedResource::Type type, CachedResourceRequest& request)
 {
     // Implementing step 1 to 7 of https://fetch.spec.whatwg.org/#fetching
@@ -661,60 +637,23 @@ void CachedResourceLoader::prepareFetch(CachedResource::Type type, CachedResourc
     if (!request.origin() && document())
         request.setOrigin(document()->securityOrigin());
 
-    if (!request.resourceRequest().hasHTTPHeader(HTTPHeaderName::Accept))
-        request.mutableResourceRequest().setHTTPHeaderField(HTTPHeaderName::Accept, acceptHeaderValueFromType(type));
+    request.setAcceptHeaderIfNone(type);
 
     // Accept-Language value is handled in underlying port-specific code.
     // FIXME: Decide whether to support client hints
 }
 
-static inline void updateRequestAccordingCacheMode(CachedResourceRequest& request)
-{
-    if (request.options().cache == FetchOptions::Cache::Default
-            && (request.resourceRequest().hasHTTPHeaderField(HTTPHeaderName::IfModifiedSince)
-                || request.resourceRequest().hasHTTPHeaderField(HTTPHeaderName::IfNoneMatch)
-                || request.resourceRequest().hasHTTPHeaderField(HTTPHeaderName::IfUnmodifiedSince)
-                || request.resourceRequest().hasHTTPHeaderField(HTTPHeaderName::IfMatch)
-                || request.resourceRequest().hasHTTPHeaderField(HTTPHeaderName::IfRange)))
-        request.setCacheModeToNoStore();
-
-    switch (request.options().cache) {
-    case FetchOptions::Cache::NoCache:
-        request.mutableResourceRequest().setCachePolicy(ReloadIgnoringCacheData);
-        request.mutableResourceRequest().addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::CacheControl, HTTPHeaderValues::maxAge0());
-        break;
-    case FetchOptions::Cache::NoStore:
-        request.setCachingPolicy(CachingPolicy::DisallowCaching);
-        request.mutableResourceRequest().setCachePolicy(ReloadIgnoringCacheData);
-        request.mutableResourceRequest().addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::Pragma, HTTPHeaderValues::noCache());
-        request.mutableResourceRequest().addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::CacheControl, HTTPHeaderValues::noCache());
-        break;
-    case FetchOptions::Cache::Reload:
-        request.mutableResourceRequest().setCachePolicy(ReloadIgnoringCacheData);
-        request.mutableResourceRequest().addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::Pragma, HTTPHeaderValues::noCache());
-        request.mutableResourceRequest().addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::CacheControl, HTTPHeaderValues::noCache());
-        break;
-    case FetchOptions::Cache::Default:
-        break;
-    case FetchOptions::Cache::ForceCache:
-        request.mutableResourceRequest().setCachePolicy(ReturnCacheDataElseLoad);
-        break;
-    case FetchOptions::Cache::OnlyIfCached:
-        request.mutableResourceRequest().setCachePolicy(ReturnCacheDataDontLoad);
-        break;
-    }
-}
 
-void CachedResourceLoader::updateHTTPRequestHeaders(CachedResourceRequest& resourceRequest)
+void CachedResourceLoader::updateHTTPRequestHeaders(CachedResourceRequest& request)
 {
     // Implementing steps 10 to 12 of https://fetch.spec.whatwg.org/#http-network-or-cache-fetch
-    updateRequestAccordingCacheMode(resourceRequest);
+    request.updateAccordingCacheMode();
 }
 
 CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(CachedResource::Type type, CachedResourceRequest&& request, ForPreload forPreload, DeferOption defer)
 {
     if (Document* document = this->document())
-        document->contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request.mutableResourceRequest(), ContentSecurityPolicy::InsecureRequestType::Load);
+        request.upgradeInsecureRequestIfNeeded(*document);
 
     URL url = request.resourceRequest().url();
 
@@ -738,16 +677,16 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(Cache
 
 #if ENABLE(CONTENT_EXTENSIONS)
     if (frame() && frame()->mainFrame().page() && m_documentLoader) {
-        auto& resourceRequest = request.mutableResourceRequest();
+        const auto& resourceRequest = request.resourceRequest();
         auto blockedStatus = frame()->mainFrame().page()->userContentProvider().processContentExtensionRulesForLoad(resourceRequest.url(), toResourceType(type), *m_documentLoader);
-        applyBlockedStatusToRequest(blockedStatus, resourceRequest);
+        request.applyBlockedStatus(blockedStatus);
         if (blockedStatus.blockedLoad) {
             RELEASE_LOG_IF_ALLOWED("requestResource: Resource blocked by content blocker (frame = %p)", frame());
             if (type == CachedResource::Type::MainResource) {
                 auto resource = createResource(type, WTFMove(request), sessionID());
                 ASSERT(resource);
                 resource->error(CachedResource::Status::LoadError);
-                resource->setResourceError(ResourceError(ContentExtensions::WebKitContentBlockerDomain, 0, request.resourceRequest().url(), WEB_UI_STRING("The URL was blocked by a content blocker", "WebKitErrorBlockedByContentBlocker description")));
+                resource->setResourceError(ResourceError(ContentExtensions::WebKitContentBlockerDomain, 0, resourceRequest.url(), WEB_UI_STRING("The URL was blocked by a content blocker", "WebKitErrorBlockedByContentBlocker description")));
                 return resource;
             }
             return nullptr;
@@ -784,7 +723,7 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(Cache
     CachedResourceHandle<CachedResource> resource;
 #if ENABLE(CACHE_PARTITIONING)
     if (document())
-        request.mutableResourceRequest().setDomainForCachePartition(document()->topOrigin()->domainForCachePartition());
+        request.setDomainForCachePartition(*document());
 #endif
 
     if (request.allowsCaching())
index 257e4d5..0c3406f 100644 (file)
 #include "CachedResourceRequest.h"
 
 #include "CachedResourceLoader.h"
+#include "ContentExtensionActions.h"
 #include "CrossOriginAccessControl.h"
 #include "Document.h"
 #include "Element.h"
+#include "HTTPHeaderValues.h"
+#include "MemoryCache.h"
 #include <wtf/NeverDestroyed.h>
 
 namespace WebCore {
@@ -78,7 +81,115 @@ void CachedResourceRequest::setAsPotentiallyCrossOrigin(const String& mode, Docu
     m_options.credentials = equalLettersIgnoringASCIICase(mode, "use-credentials") ? FetchOptions::Credentials::Include : FetchOptions::Credentials::SameOrigin;
     m_options.allowCredentials = equalLettersIgnoringASCIICase(mode, "use-credentials") ? AllowStoredCredentials : DoNotAllowStoredCredentials;
 
-    updateRequestForAccessControl(m_resourceRequest, *document.securityOrigin(), m_options.allowCredentials);
+    WebCore::updateRequestForAccessControl(m_resourceRequest, *document.securityOrigin(), m_options.allowCredentials);
 }
 
+void CachedResourceRequest::updateForAccessControl(Document& document)
+{
+    ASSERT(m_options.mode == FetchOptions::Mode::Cors);
+    ASSERT(document.securityOrigin());
+
+    m_origin = document.securityOrigin();
+    WebCore::updateRequestForAccessControl(m_resourceRequest, *document.securityOrigin(), m_options.allowCredentials);
+}
+
+void CachedResourceRequest::upgradeInsecureRequestIfNeeded(Document& document)
+{
+    URL url = m_resourceRequest.url();
+
+    ASSERT(document.contentSecurityPolicy());
+    document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(url, ContentSecurityPolicy::InsecureRequestType::Load);
+
+    if (url == m_resourceRequest.url())
+        return;
+
+    m_resourceRequest.setURL(url);
+}
+
+#if ENABLE(CACHE_PARTITIONING)
+void CachedResourceRequest::setDomainForCachePartition(Document& document)
+{
+    ASSERT(document.topOrigin());
+    m_resourceRequest.setDomainForCachePartition(document.topOrigin()->domainForCachePartition());
+}
+#endif
+
+static inline String acceptHeaderValueFromType(CachedResource::Type type)
+{
+    switch (type) {
+    case CachedResource::Type::MainResource:
+        return ASCIILiteral("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
+    case CachedResource::Type::ImageResource:
+        return ASCIILiteral("image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5");
+    case CachedResource::Type::CSSStyleSheet:
+        return ASCIILiteral("text/css,*/*;q=0.1");
+    case CachedResource::Type::SVGDocumentResource:
+        return ASCIILiteral("image/svg+xml");
+#if ENABLE(XSLT)
+    case CachedResource::Type::XSLStyleSheet:
+        // FIXME: This should accept more general xml formats */*+xml, image/svg+xml for example.
+        return ASCIILiteral("text/xml,application/xml,application/xhtml+xml,text/xsl,application/rss+xml,application/atom+xml");
+#endif
+    default:
+        return ASCIILiteral("*/*");
+    }
+}
+
+void CachedResourceRequest::setAcceptHeaderIfNone(CachedResource::Type type)
+{
+    if (!m_resourceRequest.hasHTTPHeader(HTTPHeaderName::Accept))
+        m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::Accept, acceptHeaderValueFromType(type));
+}
+
+void CachedResourceRequest::updateAccordingCacheMode()
+{
+    if (m_options.cache == FetchOptions::Cache::Default
+        && (m_resourceRequest.hasHTTPHeaderField(HTTPHeaderName::IfModifiedSince)
+            || m_resourceRequest.hasHTTPHeaderField(HTTPHeaderName::IfNoneMatch)
+            || m_resourceRequest.hasHTTPHeaderField(HTTPHeaderName::IfUnmodifiedSince)
+            || m_resourceRequest.hasHTTPHeaderField(HTTPHeaderName::IfMatch)
+            || m_resourceRequest.hasHTTPHeaderField(HTTPHeaderName::IfRange)))
+        m_options.cache = FetchOptions::Cache::NoStore;
+
+    switch (m_options.cache) {
+    case FetchOptions::Cache::NoCache:
+        m_resourceRequest.setCachePolicy(ReloadIgnoringCacheData);
+        m_resourceRequest.addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::CacheControl, HTTPHeaderValues::maxAge0());
+        break;
+    case FetchOptions::Cache::NoStore:
+        m_options.cachingPolicy = CachingPolicy::DisallowCaching;
+        m_resourceRequest.setCachePolicy(ReloadIgnoringCacheData);
+        m_resourceRequest.addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::Pragma, HTTPHeaderValues::noCache());
+        m_resourceRequest.addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::CacheControl, HTTPHeaderValues::noCache());
+        break;
+    case FetchOptions::Cache::Reload:
+        m_resourceRequest.setCachePolicy(ReloadIgnoringCacheData);
+        m_resourceRequest.addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::Pragma, HTTPHeaderValues::noCache());
+        m_resourceRequest.addHTTPHeaderFieldIfNotPresent(HTTPHeaderName::CacheControl, HTTPHeaderValues::noCache());
+        break;
+    case FetchOptions::Cache::Default:
+        break;
+    case FetchOptions::Cache::ForceCache:
+        m_resourceRequest.setCachePolicy(ReturnCacheDataElseLoad);
+        break;
+    case FetchOptions::Cache::OnlyIfCached:
+        m_resourceRequest.setCachePolicy(ReturnCacheDataDontLoad);
+        break;
+    }
+}
+
+void CachedResourceRequest::removeFragmentIdentifierIfNeeded()
+{
+    URL url = MemoryCache::removeFragmentIdentifierIfNeeded(m_resourceRequest.url());
+    if (url.string() != m_resourceRequest.url())
+        m_resourceRequest.setURL(url);
+}
+
+#if ENABLE(CONTENT_EXTENSIONS)
+void CachedResourceRequest::applyBlockedStatus(const ContentExtensions::BlockedStatus& blockedStatus)
+{
+    ContentExtensions::applyBlockedStatusToRequest(blockedStatus, m_resourceRequest);
+}
+#endif
+
 } // namespace WebCore
index a1377dc..b28ad03 100644 (file)
@@ -26,6 +26,7 @@
 #ifndef CachedResourceRequest_h
 #define CachedResourceRequest_h
 
+#include "CachedResource.h"
 #include "DocumentLoader.h"
 #include "Element.h"
 #include "ResourceLoadPriority.h"
 #include <wtf/text/AtomicString.h>
 
 namespace WebCore {
+
+namespace ContentExtensions {
+struct BlockedStatus;
+}
 class Document;
 
 class CachedResourceRequest {
 public:
     CachedResourceRequest(ResourceRequest&&, const ResourceLoaderOptions&, Optional<ResourceLoadPriority> = Nullopt, String&& charset = String());
 
-    ResourceRequest& mutableResourceRequest() { return m_resourceRequest; }
+    ResourceRequest&& releaseResourceRequest() { return WTFMove(m_resourceRequest); }
     const ResourceRequest& resourceRequest() const { return m_resourceRequest; }
     const String& charset() const { return m_charset; }
     void setCharset(const String& charset) { m_charset = charset; }
@@ -53,15 +58,26 @@ public:
     void setInitiator(const AtomicString& name);
     const AtomicString& initiatorName() const;
     bool allowsCaching() const { return m_options.cachingPolicy == CachingPolicy::AllowCaching; }
-    void setCachingPolicy(CachingPolicy policy) { m_options.cachingPolicy = policy; }
+    void setCachingPolicy(CachingPolicy policy) { m_options.cachingPolicy = policy;  }
 
     void setAsPotentiallyCrossOrigin(const String&, Document&);
+    void updateForAccessControl(Document&);
+
+    void upgradeInsecureRequestIfNeeded(Document&);
+    void setAcceptHeaderIfNone(CachedResource::Type);
+    void updateAccordingCacheMode();
+    void removeFragmentIdentifierIfNeeded();
+#if ENABLE(CONTENT_EXTENSIONS)
+    void applyBlockedStatus(const ContentExtensions::BlockedStatus&);
+#endif
+#if ENABLE(CACHE_PARTITIONING)
+    void setDomainForCachePartition(Document&);
+#endif
+
     void setOrigin(RefPtr<SecurityOrigin>&& origin) { ASSERT(!m_origin); m_origin = WTFMove(origin); }
     RefPtr<SecurityOrigin> releaseOrigin() { return WTFMove(m_origin); }
     SecurityOrigin* origin() const { return m_origin.get(); }
 
-    void setCacheModeToNoStore() { m_options.cache = FetchOptions::Cache::NoStore; }
-
 private:
     ResourceRequest m_resourceRequest;
     String m_charset;
index 3bad8bf..f9a629a 100644 (file)
@@ -58,10 +58,12 @@ void IconLoader::startLoading()
     if (m_resource || !m_frame.document())
         return;
 
+    ResourceRequest resourceRequest(m_frame.loader().icon().url());
+    resourceRequest.setPriority(ResourceLoadPriority::Low);
+
     // ContentSecurityPolicyImposition::DoPolicyCheck is a placeholder value. It does not affect the request since Content Security Policy does not apply to raw resources.
-    CachedResourceRequest request(ResourceRequest(m_frame.loader().icon().url()), ResourceLoaderOptions(SendCallbacks, SniffContent, BufferData, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions::Credentials::Omit, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching));
+    CachedResourceRequest request(WTFMove(resourceRequest), ResourceLoaderOptions(SendCallbacks, SniffContent, BufferData, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions::Credentials::Omit, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching));
 
-    request.mutableResourceRequest().setPriority(ResourceLoadPriority::Low);
     request.setInitiator(cachedResourceRequestInitiators().icon);
 
     m_resource = m_frame.document()->cachedResourceLoader().requestRawResource(WTFMove(request));
index 40f80f6..05fd026 100644 (file)
@@ -70,10 +70,12 @@ void WebCoreAVCFResourceLoader::startLoading()
 
     RetainPtr<CFURLRequestRef> urlRequest = AVCFAssetResourceLoadingRequestGetURLRequest(m_avRequest.get());
 
+    ResourceRequest resourceRequest(urlRequest.get());
+    resourceRequest.setPriority(ResourceLoadPriority::Low);
+
     // ContentSecurityPolicyImposition::DoPolicyCheck is a placeholder value. It does not affect the request since Content Security Policy does not apply to raw resources.
-    CachedResourceRequest request(ResourceRequest(urlRequest.get()), ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions::Credentials::Omit, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::DisallowCaching));
+    CachedResourceRequest request(WTFMove(resourceRequest), ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions::Credentials::Omit, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::DisallowCaching));
 
-    request.mutableResourceRequest().setPriority(ResourceLoadPriority::Low);
     CachedResourceLoader* loader = m_parent->player()->cachedResourceLoader();
     m_resource = loader ? loader->requestRawResource(WTFMove(request)) : 0;
     if (m_resource)
index 5012d86..08de639 100644 (file)
@@ -67,10 +67,12 @@ void WebCoreAVFResourceLoader::startLoading()
 
     NSURLRequest *nsRequest = [m_avRequest.get() request];
 
+    ResourceRequest resourceRequest(nsRequest);
+    resourceRequest.setPriority(ResourceLoadPriority::Low);
+
     // FIXME: Skip Content Security Policy check if the element that inititated this request
     // is in a user-agent shadow tree. See <https://bugs.webkit.org/show_bug.cgi?id=155505>.
-    CachedResourceRequest request(nsRequest, ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions::Credentials::Omit, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::DisallowCaching));
-    request.mutableResourceRequest().setPriority(ResourceLoadPriority::Low);
+    CachedResourceRequest request(WTFMove(resourceRequest), ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions::Credentials::Omit, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::DisallowCaching));
     if (auto* loader = m_parent->player()->cachedResourceLoader())
         m_resource = loader->requestMedia(WTFMove(request));