Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: third_party/WebKit/Source/core/loader/LinkLoader.cpp

Issue 1577073005: Add <link rel=preload> onload support for scripts and styles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Defeatedly removed image :/ Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 ASSERT_UNUSED(timer, timer == &m_linkLoadTimer); 83 ASSERT_UNUSED(timer, timer == &m_linkLoadTimer);
84 m_client->linkLoaded(); 84 m_client->linkLoaded();
85 } 85 }
86 86
87 void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>* timer) 87 void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>* timer)
88 { 88 {
89 ASSERT_UNUSED(timer, timer == &m_linkLoadingErrorTimer); 89 ASSERT_UNUSED(timer, timer == &m_linkLoadingErrorTimer);
90 m_client->linkLoadingErrored(); 90 m_client->linkLoadingErrored();
91 } 91 }
92 92
93 void LinkLoader::triggerEvents(Resource* resource)
94 {
95 if (resource->errorOccurred())
96 m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE);
97 else
98 m_linkLoadTimer.startOneShot(0, BLINK_FROM_HERE);
99 }
100
93 void LinkLoader::notifyFinished(Resource* resource) 101 void LinkLoader::notifyFinished(Resource* resource)
94 { 102 {
95 ASSERT(this->resource() == resource); 103 ASSERT(this->resource() == resource);
96 104
97 if (resource->errorOccurred()) 105 triggerEvents(resource);
98 m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE);
99 else
100 m_linkLoadTimer.startOneShot(0, BLINK_FROM_HERE);
101
102 clearResource(); 106 clearResource();
103 } 107 }
104 108
105 void LinkLoader::didStartPrerender() 109 void LinkLoader::didStartPrerender()
106 { 110 {
107 m_client->didStartLinkPrerender(); 111 m_client->didStartLinkPrerender();
108 } 112 }
109 113
110 void LinkLoader::didStopPrerender() 114 void LinkLoader::didStopPrerender()
111 { 115 {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 if (equalIgnoringCase(as, "font")) 181 if (equalIgnoringCase(as, "font"))
178 return Resource::Font; 182 return Resource::Font;
179 if (equalIgnoringCase(as, "track")) 183 if (equalIgnoringCase(as, "track"))
180 return Resource::TextTrack; 184 return Resource::TextTrack;
181 if (document && !as.isEmpty()) 185 if (document && !as.isEmpty())
182 document->addConsoleMessage(ConsoleMessage::create(OtherMessageSource, W arningMessageLevel, String("<link rel=preload> must have a valid `as` value"))); 186 document->addConsoleMessage(ConsoleMessage::create(OtherMessageSource, W arningMessageLevel, String("<link rel=preload> must have a valid `as` value")));
183 // TODO(yoav): Is this correct? If as is missing or invalid, it should be su bject to "connect-src" CSP directives. 187 // TODO(yoav): Is this correct? If as is missing or invalid, it should be su bject to "connect-src" CSP directives.
184 return Resource::LinkSubresource; 188 return Resource::LinkSubresource;
185 } 189 }
186 190
187 static void preloadIfNeeded(const LinkRelAttribute& relAttribute, const KURL& hr ef, Document& document, const String& as) 191 static void createLinkPreloadResourceClient(ResourcePtr<Resource> resource, Link Loader* 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
188 { 192 {
189 if (!document.loader()) 193 if (!linkLoader || !resource)
194 return;
195 OwnPtr<LinkPreloadResourceClient> resourceClient = nullptr;
196 switch (type) {
197 case Resource::Image:
198 break;
199 case Resource::Script:
200 resourceClient = LinkPreloadScriptResourceClient::create(linkLoader, res ource.get());
201 break;
202 case Resource::CSSStyleSheet:
203 resourceClient = LinkPreloadStyleResourceClient::create(linkLoader, reso urce.get());
204 break;
205 // TODO(yoav): add support for everything below.
206 case Resource::Font:
207 break;
208 case Resource::Media:
209 break;
210 case Resource::TextTrack:
211 break;
212 case Resource::LinkSubresource:
213 break;
214 default:
215 ASSERT_NOT_REACHED();
216 }
217
218 linkLoader->setPreloadResourceClient(resourceClient.release());
219 }
220
221 static void preloadIfNeeded(const LinkRelAttribute& relAttribute, const KURL& hr ef, Document& document, const String& as, LinkLoader* linkLoader)
222 {
223 if (!document.loader() || !relAttribute.isLinkPreload())
190 return; 224 return;
191 225
192 if (relAttribute.isLinkPreload()) { 226 UseCounter::count(document, UseCounter::LinkRelPreload);
193 UseCounter::count(document, UseCounter::LinkRelPreload); 227 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled());
194 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); 228 if (!href.isValid() || href.isEmpty()) {
195 if (!href.isValid() || href.isEmpty()) { 229 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> has an invalid `href` value")));
196 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource , WarningMessageLevel, String("<link rel=preload> has an invalid `href` value")) ); 230 return;
197 return; 231 }
198 } 232 Resource::Type type = LinkLoader::getTypeFromAsAttribute(as, &document);
199 Resource::Type type = LinkLoader::getTypeFromAsAttribute(as, &document); 233 ResourceRequest resourceRequest(document.completeURL(href));
200 ResourceRequest resourceRequest(document.completeURL(href)); 234 ResourceFetcher::determineRequestContext(resourceRequest, type, false);
201 ResourceFetcher::determineRequestContext(resourceRequest, type, false); 235 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link);
202 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link) ;
203 236
204 linkRequest.setPriority(document.fetcher()->loadPriority(type, linkReque st)); 237 linkRequest.setPriority(document.fetcher()->loadPriority(type, linkRequest)) ;
205 Settings* settings = document.settings(); 238 Settings* settings = document.settings();
206 if (settings && settings->logPreload()) 239 if (settings && settings->logPreload())
207 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource , DebugMessageLevel, String("Preload triggered for " + href.host() + href.path() ))); 240 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, De bugMessageLevel, String("Preload triggered for " + href.host() + href.path())));
208 linkRequest.setForPreload(true); 241 linkRequest.setForPreload(true);
209 linkRequest.setAvoidBlockingOnLoad(true); 242 linkRequest.setAvoidBlockingOnLoad(true);
210 document.loader()->startPreload(type, linkRequest); 243 createLinkPreloadResourceClient(document.loader()->startPreload(type, linkRe quest, 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
211 }
212 } 244 }
213 245
214 bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* documen t, const NetworkHintsInterface& networkHintsInterface, CanLoadResources canLoadR esources) 246 bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* documen t, const NetworkHintsInterface& networkHintsInterface, CanLoadResources canLoadR esources)
215 { 247 {
216 if (!document) 248 if (!document)
217 return false; 249 return false;
218 LinkHeaderSet headerSet(headerValue); 250 LinkHeaderSet headerSet(headerValue);
219 for (auto& header : headerSet) { 251 for (auto& header : headerSet) {
220 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty()) 252 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty())
221 return false; 253 return false;
222 254
223 LinkRelAttribute relAttribute(header.rel()); 255 LinkRelAttribute relAttribute(header.rel());
224 KURL url = document->completeURL(header.url()); 256 KURL url = document->completeURL(header.url());
225 if (canLoadResources == DoNotLoadResources) { 257 if (canLoadResources == DoNotLoadResources) {
226 if (RuntimeEnabledFeatures::linkHeaderEnabled()) 258 if (RuntimeEnabledFeatures::linkHeaderEnabled())
227 dnsPrefetchIfNeeded(relAttribute, url, *document, networkHintsIn terface, LinkCalledFromHeader); 259 dnsPrefetchIfNeeded(relAttribute, url, *document, networkHintsIn terface, LinkCalledFromHeader);
228 260
229 if (RuntimeEnabledFeatures::linkPreconnectEnabled()) 261 if (RuntimeEnabledFeatures::linkPreconnectEnabled())
230 preconnectIfNeeded(relAttribute, url, *document, header.crossOri gin(), networkHintsInterface, LinkCalledFromHeader); 262 preconnectIfNeeded(relAttribute, url, *document, header.crossOri gin(), networkHintsInterface, LinkCalledFromHeader);
231 } else { 263 } else {
232 if (RuntimeEnabledFeatures::linkPreloadEnabled()) 264 if (RuntimeEnabledFeatures::linkPreloadEnabled())
233 preloadIfNeeded(relAttribute, url, *document, header.as()); 265 preloadIfNeeded(relAttribute, url, *document, header.as(), nullp tr);
234 } 266 }
235 // TODO(yoav): Add more supported headers as needed. 267 // TODO(yoav): Add more supported headers as needed.
236 } 268 }
237 return true; 269 return true;
238 } 270 }
239 271
240 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri buteValue crossOrigin, const String& type, const String& as, const KURL& href, D ocument& document, const NetworkHintsInterface& networkHintsInterface) 272 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri buteValue crossOrigin, const String& type, const String& as, const KURL& href, D ocument& document, const NetworkHintsInterface& networkHintsInterface)
241 { 273 {
242 // TODO(yoav): Do all links need to load only after they're in document??? 274 // TODO(yoav): Do all links need to load only after they're in document???
243 275
244 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt tributeValue. crbug.com/486689 276 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt tributeValue. crbug.com/486689
245 // FIXME(crbug.com/463266): We're ignoring type here. Maybe we shouldn't. 277 // FIXME(crbug.com/463266): We're ignoring type here. Maybe we shouldn't.
246 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin kCalledFromMarkup); 278 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin kCalledFromMarkup);
247 279
248 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn terface, LinkCalledFromMarkup); 280 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn terface, LinkCalledFromMarkup);
249 281
250 if (m_client->shouldLoadLink()) 282 if (m_client->shouldLoadLink())
251 preloadIfNeeded(relAttribute, href, document, as); 283 preloadIfNeeded(relAttribute, href, document, as, this);
252 284
253 // FIXME(crbug.com/323096): Should take care of import. 285 // FIXME(crbug.com/323096): Should take care of import.
254 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource()) && h ref.isValid() && document.frame()) { 286 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource()) && h ref.isValid() && document.frame()) {
255 if (!m_client->shouldLoadLink()) 287 if (!m_client->shouldLoadLink())
256 return false; 288 return false;
257 Resource::Type type = Resource::LinkPrefetch; 289 Resource::Type type = Resource::LinkPrefetch;
258 if (relAttribute.isLinkSubresource()) { 290 if (relAttribute.isLinkSubresource()) {
259 type = Resource::LinkSubresource; 291 type = Resource::LinkSubresource;
260 UseCounter::count(document, UseCounter::LinkRelSubresource); 292 UseCounter::count(document, UseCounter::LinkRelSubresource);
261 } else { 293 } else {
(...skipping 27 matching lines...) Expand all
289 // atomic (dns prefetch). 321 // atomic (dns prefetch).
290 if (m_prerender) { 322 if (m_prerender) {
291 m_prerender->cancel(); 323 m_prerender->cancel();
292 m_prerender.clear(); 324 m_prerender.clear();
293 } 325 }
294 } 326 }
295 327
296 DEFINE_TRACE(LinkLoader) 328 DEFINE_TRACE(LinkLoader)
297 { 329 {
298 visitor->trace(m_prerender); 330 visitor->trace(m_prerender);
331 visitor->trace(m_linkPreloadResourceClient);
299 } 332 }
300 333
301 } 334 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698