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

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

Issue 1586563014: Add <link rel=preload> onload support for scripts and styles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: USING_FAST_MALLOC 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(const 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 void LinkLoader::createLinkPreloadResourceClient(ResourcePtr<Resource> resource)
188 { 192 {
189 if (!document.loader()) 193 if (!resource)
190 return; 194 return;
191 195 switch (resource->type()) {
192 if (relAttribute.isLinkPreload()) { 196 case Resource::Image:
193 UseCounter::count(document, UseCounter::LinkRelPreload); 197 break;
194 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); 198 case Resource::Script:
195 if (!href.isValid() || href.isEmpty()) { 199 m_linkPreloadResourceClient = LinkPreloadScriptResourceClient::create(th is, toScriptResource(resource.get()));
196 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource , WarningMessageLevel, String("<link rel=preload> has an invalid `href` value")) ); 200 break;
197 return; 201 case Resource::CSSStyleSheet:
198 } 202 m_linkPreloadResourceClient = LinkPreloadStyleResourceClient::create(thi s, toCSSStyleSheetResource(resource.get()));
199 Resource::Type type = LinkLoader::getTypeFromAsAttribute(as, &document); 203 break;
200 ResourceRequest resourceRequest(document.completeURL(href)); 204 // TODO(yoav): add support for everything below.
201 ResourceFetcher::determineRequestContext(resourceRequest, type, false); 205 case Resource::Font:
202 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link) ; 206 break;
203 207 case Resource::Media:
204 linkRequest.setPriority(document.fetcher()->loadPriority(type, linkReque st)); 208 break;
205 Settings* settings = document.settings(); 209 case Resource::TextTrack:
206 if (settings && settings->logPreload()) 210 break;
207 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource , DebugMessageLevel, String("Preload triggered for " + href.host() + href.path() ))); 211 case Resource::Raw:
208 linkRequest.setForPreload(true); 212 case Resource::LinkSubresource:
209 linkRequest.setAvoidBlockingOnLoad(true); 213 break;
210 document.loader()->startPreload(type, linkRequest); 214 default:
215 ASSERT_NOT_REACHED();
211 } 216 }
212 } 217 }
213 218
219 static ResourcePtr<Resource> preloadIfNeeded(const LinkRelAttribute& relAttribut e, const KURL& href, Document& document, const String& as)
220 {
221 if (!document.loader() || !relAttribute.isLinkPreload())
222 return nullptr;
223
224 UseCounter::count(document, UseCounter::LinkRelPreload);
225 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled());
226 if (!href.isValid() || href.isEmpty()) {
227 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> has an invalid `href` value")));
228 return nullptr;
229 }
230 Resource::Type type = LinkLoader::getTypeFromAsAttribute(as, &document);
231 ResourceRequest resourceRequest(document.completeURL(href));
232 ResourceFetcher::determineRequestContext(resourceRequest, type, false);
233 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link);
234
235 linkRequest.setPriority(document.fetcher()->loadPriority(type, linkRequest)) ;
236 Settings* settings = document.settings();
237 if (settings && settings->logPreload())
238 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, De bugMessageLevel, String("Preload triggered for " + href.host() + href.path())));
239 linkRequest.setForPreload(true);
240 linkRequest.setAvoidBlockingOnLoad(true);
241 return document.loader()->startPreload(type, linkRequest);
242 }
243
214 bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* documen t, const NetworkHintsInterface& networkHintsInterface, CanLoadResources canLoadR esources) 244 bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* documen t, const NetworkHintsInterface& networkHintsInterface, CanLoadResources canLoadR esources)
215 { 245 {
216 if (!document) 246 if (!document)
217 return false; 247 return false;
218 LinkHeaderSet headerSet(headerValue); 248 LinkHeaderSet headerSet(headerValue);
219 for (auto& header : headerSet) { 249 for (auto& header : headerSet) {
220 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty()) 250 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty())
221 return false; 251 return false;
222 252
223 LinkRelAttribute relAttribute(header.rel()); 253 LinkRelAttribute relAttribute(header.rel());
(...skipping 17 matching lines...) Expand all
241 { 271 {
242 // TODO(yoav): Do all links need to load only after they're in document??? 272 // TODO(yoav): Do all links need to load only after they're in document???
243 273
244 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt tributeValue. crbug.com/486689 274 // 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. 275 // FIXME(crbug.com/463266): We're ignoring type here. Maybe we shouldn't.
246 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin kCalledFromMarkup); 276 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin kCalledFromMarkup);
247 277
248 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn terface, LinkCalledFromMarkup); 278 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn terface, LinkCalledFromMarkup);
249 279
250 if (m_client->shouldLoadLink()) 280 if (m_client->shouldLoadLink())
251 preloadIfNeeded(relAttribute, href, document, as); 281 createLinkPreloadResourceClient(preloadIfNeeded(relAttribute, href, docu ment, as));
252 282
253 // FIXME(crbug.com/323096): Should take care of import. 283 // FIXME(crbug.com/323096): Should take care of import.
254 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource()) && h ref.isValid() && document.frame()) { 284 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource()) && h ref.isValid() && document.frame()) {
255 if (!m_client->shouldLoadLink()) 285 if (!m_client->shouldLoadLink())
256 return false; 286 return false;
257 Resource::Type type = Resource::LinkPrefetch; 287 Resource::Type type = Resource::LinkPrefetch;
258 if (relAttribute.isLinkSubresource()) { 288 if (relAttribute.isLinkSubresource()) {
259 type = Resource::LinkSubresource; 289 type = Resource::LinkSubresource;
260 UseCounter::count(document, UseCounter::LinkRelSubresource); 290 UseCounter::count(document, UseCounter::LinkRelSubresource);
261 } else { 291 } else {
(...skipping 27 matching lines...) Expand all
289 // atomic (dns prefetch). 319 // atomic (dns prefetch).
290 if (m_prerender) { 320 if (m_prerender) {
291 m_prerender->cancel(); 321 m_prerender->cancel();
292 m_prerender.clear(); 322 m_prerender.clear();
293 } 323 }
294 } 324 }
295 325
296 DEFINE_TRACE(LinkLoader) 326 DEFINE_TRACE(LinkLoader)
297 { 327 {
298 visitor->trace(m_prerender); 328 visitor->trace(m_prerender);
329 visitor->trace(m_linkPreloadResourceClient);
299 ResourceOwner<Resource, ResourceClient>::trace(visitor); 330 ResourceOwner<Resource, ResourceClient>::trace(visitor);
300 } 331 }
301 332
302 } 333 }
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/loader/LinkLoader.h ('k') | third_party/WebKit/Source/core/loader/LinkPreloadResourceClients.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698