Chromium Code Reviews| Index: third_party/WebKit/Source/core/loader/LinkLoader.cpp |
| diff --git a/third_party/WebKit/Source/core/loader/LinkLoader.cpp b/third_party/WebKit/Source/core/loader/LinkLoader.cpp |
| index 7db397a9e10fbb0e983f1319cb5d23ea401af82e..4a35e9a473e2d575ac5ebd0aeab4f59302863d05 100644 |
| --- a/third_party/WebKit/Source/core/loader/LinkLoader.cpp |
| +++ b/third_party/WebKit/Source/core/loader/LinkLoader.cpp |
| @@ -90,15 +90,19 @@ void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>* timer) |
| m_client->linkLoadingErrored(); |
| } |
| -void LinkLoader::notifyFinished(Resource* resource) |
| +void LinkLoader::triggerEvents(Resource* resource) |
| { |
| - ASSERT(this->resource() == resource); |
| - |
| if (resource->errorOccurred()) |
| m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE); |
| else |
| m_linkLoadTimer.startOneShot(0, BLINK_FROM_HERE); |
| +} |
| + |
| +void LinkLoader::notifyFinished(Resource* resource) |
| +{ |
| + ASSERT(this->resource() == resource); |
| + triggerEvents(resource); |
| clearResource(); |
| } |
| @@ -184,31 +188,59 @@ Resource::Type LinkLoader::getTypeFromAsAttribute(const String& as, Document* do |
| return Resource::LinkSubresource; |
| } |
| -static void preloadIfNeeded(const LinkRelAttribute& relAttribute, const KURL& href, Document& document, const String& as) |
| +static void createLinkPreloadResourceClient(ResourcePtr<Resource> resource, LinkLoader* linkLoader, Resource::Type type) |
|
Nate Chapin
2016/01/15 18:53:58
This could either return the LinkPreloadResourceCl
Yoav Weiss
2016/01/15 21:40:25
yeah, adding both this and preloadIfNeeded as stat
|
| { |
| - if (!document.loader()) |
| + if (!linkLoader || !resource) |
| return; |
| + OwnPtr<LinkPreloadResourceClient> resourceClient = nullptr; |
| + switch (type) { |
| + case Resource::Image: |
| + break; |
| + case Resource::Script: |
| + resourceClient = LinkPreloadScriptResourceClient::create(linkLoader, resource.get()); |
| + break; |
| + case Resource::CSSStyleSheet: |
| + resourceClient = LinkPreloadStyleResourceClient::create(linkLoader, resource.get()); |
| + break; |
| + // TODO(yoav): add support for everything below. |
| + case Resource::Font: |
| + break; |
| + case Resource::Media: |
| + break; |
| + case Resource::TextTrack: |
| + break; |
| + case Resource::LinkSubresource: |
| + break; |
| + default: |
| + ASSERT_NOT_REACHED(); |
| + } |
| - if (relAttribute.isLinkPreload()) { |
| - UseCounter::count(document, UseCounter::LinkRelPreload); |
| - ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); |
| - if (!href.isValid() || href.isEmpty()) { |
| - document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, WarningMessageLevel, String("<link rel=preload> has an invalid `href` value"))); |
| - return; |
| - } |
| - Resource::Type type = LinkLoader::getTypeFromAsAttribute(as, &document); |
| - ResourceRequest resourceRequest(document.completeURL(href)); |
| - ResourceFetcher::determineRequestContext(resourceRequest, type, false); |
| - FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link); |
| + linkLoader->setPreloadResourceClient(resourceClient.release()); |
| +} |
| - linkRequest.setPriority(document.fetcher()->loadPriority(type, linkRequest)); |
| - Settings* settings = document.settings(); |
| - if (settings && settings->logPreload()) |
| - document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, DebugMessageLevel, String("Preload triggered for " + href.host() + href.path()))); |
| - linkRequest.setForPreload(true); |
| - linkRequest.setAvoidBlockingOnLoad(true); |
| - document.loader()->startPreload(type, linkRequest); |
| +static void preloadIfNeeded(const LinkRelAttribute& relAttribute, const KURL& href, Document& document, const String& as, LinkLoader* linkLoader) |
| +{ |
| + if (!document.loader() || !relAttribute.isLinkPreload()) |
| + return; |
| + |
| + UseCounter::count(document, UseCounter::LinkRelPreload); |
| + ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); |
| + if (!href.isValid() || href.isEmpty()) { |
| + document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, WarningMessageLevel, String("<link rel=preload> has an invalid `href` value"))); |
| + return; |
| } |
| + Resource::Type type = LinkLoader::getTypeFromAsAttribute(as, &document); |
| + ResourceRequest resourceRequest(document.completeURL(href)); |
| + ResourceFetcher::determineRequestContext(resourceRequest, type, false); |
| + FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link); |
| + |
| + linkRequest.setPriority(document.fetcher()->loadPriority(type, linkRequest)); |
| + Settings* settings = document.settings(); |
| + if (settings && settings->logPreload()) |
| + document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, DebugMessageLevel, String("Preload triggered for " + href.host() + href.path()))); |
| + linkRequest.setForPreload(true); |
| + linkRequest.setAvoidBlockingOnLoad(true); |
| + createLinkPreloadResourceClient(document.loader()->startPreload(type, linkRequest, linkLoader), linkLoader, type); |
|
Nate Chapin
2016/01/15 18:53:58
Maybe do this after preloadIfNeeded is called in l
Yoav Weiss
2016/01/15 21:40:25
not sure what you mean by that. Can you elaborate?
Nate Chapin
2016/01/15 21:52:44
createLinkPreloadResourceClient() only does work i
|
| } |
| bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* document, const NetworkHintsInterface& networkHintsInterface, CanLoadResources canLoadResources) |
| @@ -230,7 +262,7 @@ bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* documen |
| preconnectIfNeeded(relAttribute, url, *document, header.crossOrigin(), networkHintsInterface, LinkCalledFromHeader); |
| } else { |
| if (RuntimeEnabledFeatures::linkPreloadEnabled()) |
| - preloadIfNeeded(relAttribute, url, *document, header.as()); |
| + preloadIfNeeded(relAttribute, url, *document, header.as(), nullptr); |
| } |
| // TODO(yoav): Add more supported headers as needed. |
| } |
| @@ -248,7 +280,7 @@ bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri |
| preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsInterface, LinkCalledFromMarkup); |
| if (m_client->shouldLoadLink()) |
| - preloadIfNeeded(relAttribute, href, document, as); |
| + preloadIfNeeded(relAttribute, href, document, as, this); |
| // FIXME(crbug.com/323096): Should take care of import. |
| if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource()) && href.isValid() && document.frame()) { |
| @@ -296,6 +328,7 @@ void LinkLoader::released() |
| DEFINE_TRACE(LinkLoader) |
| { |
| visitor->trace(m_prerender); |
| + visitor->trace(m_linkPreloadResourceClient); |
| } |
| } |