Add a main resource type to the memory cache
authorjaphet@chromium.org <japhet@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Oct 2012 20:05:17 +0000 (20:05 +0000)
committerjaphet@chromium.org <japhet@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Oct 2012 20:05:17 +0000 (20:05 +0000)
https://bugs.webkit.org/show_bug.cgi?id=99864

Reviewed by Adam Barth.

No new tests, no functionality change.

* inspector/InspectorPageAgent.cpp:
(WebCore::hasTextContent):
(WebCore::InspectorPageAgent::cachedResourceType):
* loader/SubresourceLoader.cpp:
(WebCore::SubresourceLoader::willSendRequest):
(WebCore::SubresourceLoader::didReceiveResponse):
(WebCore::SubresourceLoader::didFail):
* loader/cache/CachedRawResource.cpp:
(WebCore::CachedRawResource::CachedRawResource):
(WebCore::CachedRawResource::addAdditionalRequestHeaders):
(WebCore):
(WebCore::CachedRawResource::setShouldBufferData):
(WebCore::CachedRawResource::loader):
(WebCore::CachedRawResource::clear):
* loader/cache/CachedRawResource.h:
(WebCore):
(CachedRawResource):
* loader/cache/CachedResource.cpp:
(WebCore::defaultPriorityForResourceType):
(WebCore::cachedResourceTypeToTargetType):
(WebCore::CachedResource::updateResourceRequest):
(WebCore):
* loader/cache/CachedResource.h:
(WebCore::CachedResource::setResourceError):
(WebCore::CachedResource::resourceError):
(CachedResource):
(WebCore::CachedResource::ignoreForRequestCount):
* loader/cache/CachedResourceLoader.cpp:
(WebCore::createResource):
(WebCore::CachedResourceLoader::requestRawResource):
(WebCore::CachedResourceLoader::checkInsecureContent):
(WebCore::CachedResourceLoader::canRequest):
(WebCore::CachedResourceLoader::determineRevalidationPolicy):
* loader/cache/CachedResourceLoader.h:
(CachedResourceLoader):
* platform/network/ResourceLoadPriority.h:
* platform/network/cf/ResourceRequestCFNet.h:
(WebCore::toResourceLoadPriority):
(WebCore::toHTTPPipeliningPriority):

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

Source/WebCore/ChangeLog
Source/WebCore/inspector/InspectorPageAgent.cpp
Source/WebCore/loader/SubresourceLoader.cpp
Source/WebCore/loader/cache/CachedRawResource.cpp
Source/WebCore/loader/cache/CachedRawResource.h
Source/WebCore/loader/cache/CachedResource.cpp
Source/WebCore/loader/cache/CachedResource.h
Source/WebCore/loader/cache/CachedResourceLoader.cpp
Source/WebCore/loader/cache/CachedResourceLoader.h
Source/WebCore/platform/network/ResourceLoadPriority.h
Source/WebCore/platform/network/cf/ResourceRequestCFNet.h

index e11bc9e..d57668a 100644 (file)
@@ -1,3 +1,52 @@
+2012-10-25  Nate Chapin  <japhet@chromium.org>
+
+        Add a main resource type to the memory cache
+        https://bugs.webkit.org/show_bug.cgi?id=99864
+
+        Reviewed by Adam Barth.
+
+        No new tests, no functionality change.
+
+        * inspector/InspectorPageAgent.cpp:
+        (WebCore::hasTextContent):
+        (WebCore::InspectorPageAgent::cachedResourceType):
+        * loader/SubresourceLoader.cpp:
+        (WebCore::SubresourceLoader::willSendRequest):
+        (WebCore::SubresourceLoader::didReceiveResponse):
+        (WebCore::SubresourceLoader::didFail):
+        * loader/cache/CachedRawResource.cpp:
+        (WebCore::CachedRawResource::CachedRawResource):
+        (WebCore::CachedRawResource::addAdditionalRequestHeaders):
+        (WebCore):
+        (WebCore::CachedRawResource::setShouldBufferData):
+        (WebCore::CachedRawResource::loader):
+        (WebCore::CachedRawResource::clear):
+        * loader/cache/CachedRawResource.h:
+        (WebCore):
+        (CachedRawResource):
+        * loader/cache/CachedResource.cpp:
+        (WebCore::defaultPriorityForResourceType):
+        (WebCore::cachedResourceTypeToTargetType):
+        (WebCore::CachedResource::updateResourceRequest):
+        (WebCore):
+        * loader/cache/CachedResource.h:
+        (WebCore::CachedResource::setResourceError):
+        (WebCore::CachedResource::resourceError):
+        (CachedResource):
+        (WebCore::CachedResource::ignoreForRequestCount):
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::createResource):
+        (WebCore::CachedResourceLoader::requestRawResource):
+        (WebCore::CachedResourceLoader::checkInsecureContent):
+        (WebCore::CachedResourceLoader::canRequest):
+        (WebCore::CachedResourceLoader::determineRevalidationPolicy):
+        * loader/cache/CachedResourceLoader.h:
+        (CachedResourceLoader):
+        * platform/network/ResourceLoadPriority.h:
+        * platform/network/cf/ResourceRequestCFNet.h:
+        (WebCore::toResourceLoadPriority):
+        (WebCore::toHTTPPipeliningPriority):
+
 2012-10-25  Ojan Vafai  <ojan@chromium.org>
 
         [V8] Add histograms to measure V8 work done during window close and navigation
index 329f89c..53793a3 100644 (file)
@@ -139,7 +139,7 @@ static bool prepareCachedResourceBuffer(CachedResource* cachedResource, bool* ha
 static bool hasTextContent(CachedResource* cachedResource)
 {
     InspectorPageAgent::ResourceType type = InspectorPageAgent::cachedResourceType(*cachedResource);
-    return type == InspectorPageAgent::StylesheetResource || type == InspectorPageAgent::ScriptResource || type == InspectorPageAgent::XHRResource;
+    return type == InspectorPageAgent::DocumentResource || type == InspectorPageAgent::StylesheetResource || type == InspectorPageAgent::ScriptResource || type == InspectorPageAgent::XHRResource;
 }
 
 static PassRefPtr<TextResourceDecoder> createXHRTextDecoder(const String& mimeType, const String& textEncodingName)
@@ -308,6 +308,8 @@ InspectorPageAgent::ResourceType InspectorPageAgent::cachedResourceType(const Ca
         return InspectorPageAgent::ScriptResource;
     case CachedResource::RawResource:
         return InspectorPageAgent::XHRResource;
+    case CachedResource::MainResource:
+        return InspectorPageAgent::DocumentResource;
     default:
         break;
     }
index 2585142..50fa5e5 100644 (file)
@@ -147,6 +147,8 @@ void SubresourceLoader::willSendRequest(ResourceRequest& newRequest, const Resou
         return;
 
     ResourceLoader::willSendRequest(newRequest, redirectResponse);
+    if (newRequest.isNull())
+        cancel();
 }
 
 void SubresourceLoader::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
@@ -183,7 +185,9 @@ void SubresourceLoader::didReceiveResponse(const ResourceResponse& response)
         return;
     ResourceLoader::didReceiveResponse(response);
 
-    if (response.isMultipart()) {
+    // FIXME: Main resources have a different set of rules for multipart than images do.
+    // Hopefully we can merge those 2 paths.
+    if (response.isMultipart() && m_resource->type() != CachedResource::MainResource) {
         m_loadingMultipartContent = true;
 
         // We don't count multiParts in a CachedResourceLoader's request count
@@ -284,6 +288,7 @@ void SubresourceLoader::didFail(const ResourceError& error)
     RefPtr<SubresourceLoader> protect(this);
     CachedResourceHandle<CachedResource> protectResource(m_resource);
     m_state = Finishing;
+    m_resource->setResourceError(error);
     if (error.isTimeout())
         m_resource->error(CachedResource::TimeoutError);
     else
index 194b15f..b0d8c96 100644 (file)
@@ -36,8 +36,8 @@
 
 namespace WebCore {
 
-CachedRawResource::CachedRawResource(ResourceRequest& resourceRequest)
-    : CachedResource(resourceRequest, RawResource)
+CachedRawResource::CachedRawResource(ResourceRequest& resourceRequest, Type type)
+    : CachedResource(resourceRequest, type)
     , m_identifier(0)
 {
 }
@@ -129,6 +129,11 @@ void CachedRawResource::setDefersLoading(bool defers)
         m_loader->setDefersLoading(defers);
 }
 
+void CachedRawResource::setShouldBufferData(DataBufferingPolicy shouldBufferData)
+{
+    m_options.shouldBufferData = shouldBufferData;
+}
+
 static bool shouldIgnoreHeaderForCacheReuse(AtomicString headerName)
 {
     // FIXME: This list of headers that don't affect cache policy almost certainly isn't complete.
@@ -184,6 +189,19 @@ bool CachedRawResource::canReuse(const ResourceRequest& newRequest) const
     return true;
 }
 
+SubresourceLoader* CachedRawResource::loader() const
+{
+    return m_loader.get();
+}
+
+void CachedRawResource::clear()
+{
+    m_data.clear();
+    setEncodedSize(0);
+    if (m_loader)
+        m_loader->clearResourceData();
+}
+
 void CachedRawResource::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
 {
     MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CachedResourceRaw);
index 6d2e970..358f504 100644 (file)
 namespace WebCore {
 class CachedRawResourceCallback;
 class CachedRawResourceClient;
+class SubresourceLoader;
 
 class CachedRawResource : public CachedResource {
 public:
-    CachedRawResource(ResourceRequest&);
+    CachedRawResource(ResourceRequest&, Type);
 
     // FIXME: AssociatedURLLoader shouldn't be a DocumentThreadableLoader and therefore shouldn't
     // use CachedRawResource. However, it is, and it needs to be able to defer loading.
     // This can be fixed by splitting CORS preflighting out of DocumentThreacableLoader.
     virtual void setDefersLoading(bool);
+
+    virtual void setShouldBufferData(DataBufferingPolicy);
     
     // FIXME: This is exposed for the InpsectorInstrumentation for preflights in DocumentThreadableLoader. It's also really lame.
     unsigned long identifier() const { return m_identifier; }
 
+    SubresourceLoader* loader() const;
+    void clear();
+
     bool canReuse(const ResourceRequest&) const;
 
     virtual void reportMemoryUsage(MemoryObjectInfo*) const OVERRIDE;
index 12c727c..201d45e 100644 (file)
@@ -63,35 +63,37 @@ namespace WebCore {
 static ResourceLoadPriority defaultPriorityForResourceType(CachedResource::Type type)
 {
     switch (type) {
-        case CachedResource::CSSStyleSheet:
-            return ResourceLoadPriorityHigh;
-        case CachedResource::Script:
-        case CachedResource::FontResource:
-        case CachedResource::RawResource:
-            return ResourceLoadPriorityMedium;
-        case CachedResource::ImageResource:
-            return ResourceLoadPriorityLow;
+    case CachedResource::MainResource:
+        return ResourceLoadPriorityVeryHigh;
+    case CachedResource::CSSStyleSheet:
+        return ResourceLoadPriorityHigh;
+    case CachedResource::Script:
+    case CachedResource::FontResource:
+    case CachedResource::RawResource:
+        return ResourceLoadPriorityMedium;
+    case CachedResource::ImageResource:
+        return ResourceLoadPriorityLow;
 #if ENABLE(XSLT)
-        case CachedResource::XSLStyleSheet:
-            return ResourceLoadPriorityHigh;
+    case CachedResource::XSLStyleSheet:
+        return ResourceLoadPriorityHigh;
 #endif
 #if ENABLE(SVG)
-        case CachedResource::SVGDocumentResource:
-            return ResourceLoadPriorityLow;
+    case CachedResource::SVGDocumentResource:
+        return ResourceLoadPriorityLow;
 #endif
 #if ENABLE(LINK_PREFETCH)
-        case CachedResource::LinkPrefetch:
-            return ResourceLoadPriorityVeryLow;
-        case CachedResource::LinkSubresource:
-            return ResourceLoadPriorityVeryLow;
+    case CachedResource::LinkPrefetch:
+        return ResourceLoadPriorityVeryLow;
+    case CachedResource::LinkSubresource:
+        return ResourceLoadPriorityVeryLow;
 #endif
 #if ENABLE(VIDEO_TRACK)
-        case CachedResource::TextTrackResource:
-            return ResourceLoadPriorityLow;
+    case CachedResource::TextTrackResource:
+        return ResourceLoadPriorityLow;
 #endif
 #if ENABLE(CSS_SHADERS)
-        case CachedResource::ShaderResource:
-            return ResourceLoadPriorityMedium;
+    case CachedResource::ShaderResource:
+        return ResourceLoadPriorityMedium;
 #endif
     }
     ASSERT_NOT_REACHED();
@@ -102,6 +104,8 @@ static ResourceLoadPriority defaultPriorityForResourceType(CachedResource::Type
 static ResourceRequest::TargetType cachedResourceTypeToTargetType(CachedResource::Type type)
 {
     switch (type) {
+    case CachedResource::MainResource:
+        return ResourceRequest::TargetIsMainFrame;
     case CachedResource::CSSStyleSheet:
 #if ENABLE(XSLT)
     case CachedResource::XSLStyleSheet:
@@ -277,7 +281,9 @@ void CachedResource::load(CachedResourceLoader* cachedResourceLoader, const Reso
         m_resourceRequest.setHTTPHeaderField("Purpose", "prefetch");
 #endif
     m_resourceRequest.setPriority(loadPriority());
-    addAdditionalRequestHeaders(cachedResourceLoader);
+
+    if (type() != MainResource)
+        addAdditionalRequestHeaders(cachedResourceLoader);
 
 #if USE(PLATFORM_STRATEGIES)
     m_loader = platformStrategies()->loaderStrategy()->resourceLoadScheduler()->scheduleSubresourceLoad(cachedResourceLoader->frame(), this, m_resourceRequest, m_resourceRequest.priority(), options);
index fac8845..87fa9e7 100644 (file)
@@ -61,6 +61,7 @@ class CachedResource {
     
 public:
     enum Type {
+        MainResource,
         ImageResource,
         CSSStyleSheet,
         Script,
@@ -104,6 +105,9 @@ public:
     virtual void data(PassRefPtr<ResourceBuffer> data, bool allDataReceived);
     virtual void error(CachedResource::Status);
 
+    void setResourceError(const ResourceError& error) { m_error = error; }
+    const ResourceError& resourceError() const { return m_error; }
+
     virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return false; }
 
     ResourceRequest& resourceRequest() { return m_resourceRequest; }
@@ -152,7 +156,7 @@ public:
     virtual bool isImage() const { return false; }
     bool ignoreForRequestCount() const
     {
-        return false
+        return type() == MainResource
 #if ENABLE(LINK_PREFETCH)
             || type() == LinkPrefetch
             || type() == LinkSubresource
@@ -257,8 +261,6 @@ public:
 protected:
     virtual void checkNotify();
 
-    virtual void addAdditionalRequestHeaders(CachedResourceLoader*);
-
     void setEncodedSize(unsigned);
     void setDecodedSize(unsigned);
     void didAccessDecodedData(double timeStamp);
@@ -305,10 +307,13 @@ private:
     double currentAge() const;
     double freshnessLifetime() const;
 
+    void addAdditionalRequestHeaders(CachedResourceLoader*);
     void failBeforeStarting();
 
     RefPtr<CachedMetadata> m_cachedMetadata;
 
+    ResourceError m_error;
+
     double m_lastDecodedAccessTime; // Used as a "thrash guard" in the cache
     double m_loadFinishTime;
 
index 4e96f44..1565c6c 100644 (file)
@@ -87,7 +87,8 @@ static CachedResource* createResource(CachedResource::Type type, ResourceRequest
     case CachedResource::FontResource:
         return new CachedFont(request);
     case CachedResource::RawResource:
-        return new CachedRawResource(request);
+    case CachedResource::MainResource:
+        return new CachedRawResource(request, type);
 #if ENABLE(XSLT)
     case CachedResource::XSLStyleSheet:
         return new CachedXSLStyleSheet(request);
@@ -246,6 +247,11 @@ CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestRawResource
     return static_cast<CachedRawResource*>(requestResource(CachedResource::RawResource, request).get());
 }
 
+CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestMainResource(CachedResourceRequest& request)
+{
+    return static_cast<CachedRawResource*>(requestResource(CachedResource::MainResource, request).get());
+}
+
 bool CachedResourceLoader::checkInsecureContent(CachedResource::Type type, const KURL& url) const
 {
     switch (type) {
@@ -280,6 +286,7 @@ bool CachedResourceLoader::checkInsecureContent(CachedResource::Type type, const
         }
         break;
     }
+    case CachedResource::MainResource:
 #if ENABLE(LINK_PREFETCH)
     case CachedResource::LinkPrefetch:
     case CachedResource::LinkSubresource:
@@ -292,11 +299,7 @@ bool CachedResourceLoader::checkInsecureContent(CachedResource::Type type, const
 
 bool CachedResourceLoader::canRequest(CachedResource::Type type, const KURL& url, bool forPreload)
 {
-    // FIXME: When we can load main resources through CachedResourceLoader, we'll need to allow for null document() here.
-    if (!document())
-        return false;
-
-    if (!document()->securityOrigin()->canDisplay(url)) {
+    if (document() && !document()->securityOrigin()->canDisplay(url)) {
         if (!forPreload)
             FrameLoader::reportLocalLoadFailed(document()->frame(), url.string());
         LOG(ResourceLoading, "CachedResourceLoader::requestResource URL was not allowed by SecurityOrigin::canDisplay");
@@ -307,6 +310,7 @@ bool CachedResourceLoader::canRequest(CachedResource::Type type, const KURL& url
     // types of resources, like Images, Scripts, and CSS, can be loaded from
     // any URL.
     switch (type) {
+    case CachedResource::MainResource:
     case CachedResource::ImageResource:
     case CachedResource::CSSStyleSheet:
     case CachedResource::Script:
@@ -374,6 +378,7 @@ bool CachedResourceLoader::canRequest(CachedResource::Type type, const KURL& url
             return false;
         break;
     }
+    case CachedResource::MainResource:
     case CachedResource::RawResource:
 #if ENABLE(LINK_PREFETCH)
     case CachedResource::LinkPrefetch:
@@ -518,8 +523,11 @@ CachedResourceLoader::RevalidationPolicy CachedResourceLoader::determineRevalida
         return Reload;
     }
 
+    if (existingResource->type() == CachedResource::MainResource)
+        return Reload;
+
     if (existingResource->type() == CachedResource::RawResource && !static_cast<CachedRawResource*>(existingResource)->canReuse(request))
-         return Reload;
+        return Reload;
 
     // Certain requests (e.g., XHRs) might have manually set headers that require revalidation.
     // FIXME: In theory, this should be a Revalidate case. In practice, the MemoryCache revalidation path assumes a whole bunch
index 0fcb1e3..e2e2ae4 100644 (file)
@@ -79,6 +79,7 @@ public:
     CachedResourceHandle<CachedScript> requestScript(CachedResourceRequest&);
     CachedResourceHandle<CachedFont> requestFont(CachedResourceRequest&);
     CachedResourceHandle<CachedRawResource> requestRawResource(CachedResourceRequest&);
+    CachedResourceHandle<CachedRawResource> requestMainResource(CachedResourceRequest&);
 
 #if ENABLE(SVG)
     CachedResourceHandle<CachedSVGDocument> requestSVGDocument(CachedResourceRequest&);
index 45238d7..5a7104f 100644 (file)
@@ -35,8 +35,9 @@ enum ResourceLoadPriority {
     ResourceLoadPriorityLow,
     ResourceLoadPriorityMedium,
     ResourceLoadPriorityHigh,
+    ResourceLoadPriorityVeryHigh,
     ResourceLoadPriorityLowest = ResourceLoadPriorityVeryLow,
-    ResourceLoadPriorityHighest = ResourceLoadPriorityHigh,
+    ResourceLoadPriorityHighest = ResourceLoadPriorityVeryHigh,
 };
 
 }
index 987508f..919b236 100644 (file)
@@ -54,6 +54,8 @@ inline ResourceLoadPriority toResourceLoadPriority(int priority)
         return ResourceLoadPriorityMedium;
     case 3:
         return ResourceLoadPriorityHigh;
+    case 4:
+        return ResourceLoadPriorityVeryHigh;
     default:
         ASSERT_NOT_REACHED();
         return ResourceLoadPriorityLowest;
@@ -73,6 +75,8 @@ inline int toHTTPPipeliningPriority(ResourceLoadPriority priority)
         return 2;
     case ResourceLoadPriorityHigh:
         return 3;
+    case ResourceLoadPriorityVeryHigh:
+        return 4;
     }
 
     ASSERT_NOT_REACHED();