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

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

Issue 1738133002: Add support for media attribute on link (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 9 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 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * 29 *
30 */ 30 */
31 31
32 #include "core/loader/LinkLoader.h" 32 #include "core/loader/LinkLoader.h"
33 33
34 #include "core/css/MediaList.h"
35 #include "core/css/MediaQueryEvaluator.h"
34 #include "core/dom/Document.h" 36 #include "core/dom/Document.h"
35 #include "core/fetch/FetchInitiatorTypeNames.h" 37 #include "core/fetch/FetchInitiatorTypeNames.h"
36 #include "core/fetch/FetchRequest.h" 38 #include "core/fetch/FetchRequest.h"
37 #include "core/fetch/LinkFetchResource.h" 39 #include "core/fetch/LinkFetchResource.h"
38 #include "core/fetch/ResourceFetcher.h" 40 #include "core/fetch/ResourceFetcher.h"
39 #include "core/frame/Settings.h" 41 #include "core/frame/Settings.h"
40 #include "core/frame/UseCounter.h" 42 #include "core/frame/UseCounter.h"
41 #include "core/html/CrossOriginAttribute.h" 43 #include "core/html/CrossOriginAttribute.h"
42 #include "core/html/LinkRelAttribute.h" 44 #include "core/html/LinkRelAttribute.h"
45 #include "core/html/parser/HTMLPreloadScanner.h"
43 #include "core/inspector/ConsoleMessage.h" 46 #include "core/inspector/ConsoleMessage.h"
44 #include "core/loader/DocumentLoader.h" 47 #include "core/loader/DocumentLoader.h"
45 #include "core/loader/LinkHeader.h" 48 #include "core/loader/LinkHeader.h"
46 #include "core/loader/NetworkHintsInterface.h" 49 #include "core/loader/NetworkHintsInterface.h"
47 #include "core/loader/PrerenderHandle.h" 50 #include "core/loader/PrerenderHandle.h"
48 #include "platform/MIMETypeRegistry.h" 51 #include "platform/MIMETypeRegistry.h"
49 #include "platform/Prerender.h" 52 #include "platform/Prerender.h"
50 #include "platform/RuntimeEnabledFeatures.h" 53 #include "platform/RuntimeEnabledFeatures.h"
51 #include "platform/network/NetworkHints.h" 54 #include "platform/network/NetworkHints.h"
52 #include "public/platform/WebPrerender.h" 55 #include "public/platform/WebPrerender.h"
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 case Resource::Raw: 243 case Resource::Raw:
241 case Resource::LinkPreload: 244 case Resource::LinkPreload:
242 return true; 245 return true;
243 default: 246 default:
244 ASSERT_NOT_REACHED(); 247 ASSERT_NOT_REACHED();
245 } 248 }
246 return false; 249 return false;
247 } 250 }
248 251
249 static Resource* preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR L& href, Document& document, const String& as, const String& mimeType, 252 static Resource* preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR L& href, Document& document, const String& as, const String& mimeType,
250 CrossOriginAttributeValue crossOrigin, LinkCaller caller, bool& errorOccurre d) 253 const String& media, CrossOriginAttributeValue crossOrigin, LinkCaller calle r, bool& errorOccurred, ViewportDescription* viewportDescription)
251 { 254 {
252 if (!document.loader() || !relAttribute.isLinkPreload()) 255 if (!document.loader() || !relAttribute.isLinkPreload())
253 return nullptr; 256 return nullptr;
254 257
255 UseCounter::count(document, UseCounter::LinkRelPreload); 258 UseCounter::count(document, UseCounter::LinkRelPreload);
256 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); 259 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled());
257 if (!href.isValid() || href.isEmpty()) { 260 if (!href.isValid() || href.isEmpty()) {
258 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> has an invalid `href` value"))); 261 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> has an invalid `href` value")));
259 return nullptr; 262 return nullptr;
260 } 263 }
261 264
265 if (!media.isEmpty()) {
266 RefPtrWillBeRawPtr<MediaValues> mediaValues = MediaValues::createDynamic IfFrameExists(document.frame());
267 if (viewportDescription)
268 mediaValues->overrideViewportDimensions(viewportDescription->maxWidt h.getFloatValue(), viewportDescription->maxHeight.getFloatValue());
269
270 // Preload only if media matches
271 RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(m edia);
272 MediaQueryEvaluator evaluator(*mediaValues);
273 if (!evaluator.eval(mediaQueries.get()))
274 return nullptr;
275 }
262 if (caller == LinkCalledFromHeader) 276 if (caller == LinkCalledFromHeader)
263 UseCounter::count(document, UseCounter::LinkHeaderPreload); 277 UseCounter::count(document, UseCounter::LinkHeaderPreload);
264 Resource::Type resourceType; 278 Resource::Type resourceType;
265 if (!LinkLoader::getResourceTypeFromAsAttribute(as, resourceType)) { 279 if (!LinkLoader::getResourceTypeFromAsAttribute(as, resourceType)) {
266 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> must have a valid `as` value"))); 280 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> must have a valid `as` value")));
267 errorOccurred = true; 281 errorOccurred = true;
268 return nullptr; 282 return nullptr;
269 } 283 }
270 284
271 if (!isSupportedType(resourceType, mimeType)) { 285 if (!isSupportedType(resourceType, mimeType)) {
272 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> has an unsupported `type` value")) ); 286 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa rningMessageLevel, String("<link rel=preload> has an unsupported `type` value")) );
273 return nullptr; 287 return nullptr;
274 } 288 }
275 ResourceRequest resourceRequest(document.completeURL(href)); 289 ResourceRequest resourceRequest(document.completeURL(href));
276 ResourceFetcher::determineRequestContext(resourceRequest, resourceType, fals e); 290 ResourceFetcher::determineRequestContext(resourceRequest, resourceType, fals e);
277 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link); 291 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link);
278 292
279 linkRequest.setPriority(document.fetcher()->loadPriority(resourceType, linkR equest)); 293 linkRequest.setPriority(document.fetcher()->loadPriority(resourceType, linkR equest));
280 if (crossOrigin != CrossOriginAttributeNotSet) 294 if (crossOrigin != CrossOriginAttributeNotSet)
281 linkRequest.setCrossOriginAccessControl(document.getSecurityOrigin(), cr ossOrigin); 295 linkRequest.setCrossOriginAccessControl(document.getSecurityOrigin(), cr ossOrigin);
282 Settings* settings = document.settings(); 296 Settings* settings = document.settings();
283 if (settings && settings->logPreload()) 297 if (settings && settings->logPreload())
284 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, De bugMessageLevel, String("Preload triggered for " + href.host() + href.path()))); 298 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, De bugMessageLevel, String("Preload triggered for " + href.host() + href.path())));
285 linkRequest.setForPreload(true); 299 linkRequest.setForPreload(true);
286 linkRequest.setLinkPreload(true); 300 linkRequest.setLinkPreload(true);
287 return document.loader()->startPreload(resourceType, linkRequest); 301 return document.loader()->startPreload(resourceType, linkRequest);
288 } 302 }
289 303
290 void LinkLoader::loadLinksFromHeader(const String& headerValue, const KURL& base URL, Document* document, const NetworkHintsInterface& networkHintsInterface, Can LoadResources canLoadResources) 304 void LinkLoader::loadLinksFromHeader(const String& headerValue, const KURL& base URL, Document* document, const NetworkHintsInterface& networkHintsInterface,
305 CanLoadResources canLoadResources, ViewportDescriptionWrapper* viewportDescr iptionWrapper)
291 { 306 {
292 if (!document) 307 if (!document || headerValue.isEmpty())
293 return; 308 return;
294 LinkHeaderSet headerSet(headerValue); 309 LinkHeaderSet headerSet(headerValue);
295 for (auto& header : headerSet) { 310 for (auto& header : headerSet) {
296 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty()) 311 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty())
297 continue; 312 continue;
298 313
299 LinkRelAttribute relAttribute(header.rel()); 314 LinkRelAttribute relAttribute(header.rel());
300 KURL url(baseURL, header.url()); 315 KURL url(baseURL, header.url());
301 if (canLoadResources != OnlyLoadResources) { 316 if (canLoadResources != OnlyLoadResources) {
302 if (RuntimeEnabledFeatures::linkHeaderEnabled()) 317 if (RuntimeEnabledFeatures::linkHeaderEnabled())
303 dnsPrefetchIfNeeded(relAttribute, url, *document, networkHintsIn terface, LinkCalledFromHeader); 318 dnsPrefetchIfNeeded(relAttribute, url, *document, networkHintsIn terface, LinkCalledFromHeader);
304 319
305 if (RuntimeEnabledFeatures::linkPreconnectEnabled()) 320 if (RuntimeEnabledFeatures::linkPreconnectEnabled())
306 preconnectIfNeeded(relAttribute, url, *document, header.crossOri gin(), networkHintsInterface, LinkCalledFromHeader); 321 preconnectIfNeeded(relAttribute, url, *document, header.crossOri gin(), networkHintsInterface, LinkCalledFromHeader);
307 } 322 }
308 if (canLoadResources != DoNotLoadResources) { 323 if (canLoadResources != DoNotLoadResources) {
309 bool errorOccurred = false; 324 bool errorOccurred = false;
310 if (RuntimeEnabledFeatures::linkPreloadEnabled()) 325 if (RuntimeEnabledFeatures::linkPreloadEnabled()) {
311 preloadIfNeeded(relAttribute, url, *document, header.as(), heade r.mimeType(), header.crossOrigin(), LinkCalledFromHeader, errorOccurred); 326 ViewportDescription* viewportDescription = (viewportDescriptionW rapper && viewportDescriptionWrapper->set) ? &(viewportDescriptionWrapper->descr iption) : nullptr;
327 preloadIfNeeded(relAttribute, url, *document, header.as(), heade r.mimeType(), header.media(), header.crossOrigin(), LinkCalledFromHeader, errorO ccurred, viewportDescription);
328 }
312 } 329 }
313 // TODO(yoav): Add more supported headers as needed. 330 // TODO(yoav): Add more supported headers as needed.
314 } 331 }
315 } 332 }
316 333
317 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri buteValue crossOrigin, const String& type, const String& as, const KURL& href, D ocument& document, const NetworkHintsInterface& networkHintsInterface) 334 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri buteValue crossOrigin, const String& type,
335 const String& as, const String& media, const KURL& href, Document& document, const NetworkHintsInterface& networkHintsInterface)
318 { 336 {
319 // TODO(yoav): Do all links need to load only after they're in document??? 337 // TODO(yoav): Do all links need to load only after they're in document???
320 338
321 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt tributeValue. crbug.com/486689 339 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt tributeValue. crbug.com/486689
322 // FIXME(crbug.com/463266): We're ignoring type here, for everything but pre load. Maybe we shouldn't. 340 // FIXME(crbug.com/463266): We're ignoring type here, for everything but pre load. Maybe we shouldn't.
323 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin kCalledFromMarkup); 341 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin kCalledFromMarkup);
324 342
325 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn terface, LinkCalledFromMarkup); 343 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn terface, LinkCalledFromMarkup);
326 344
327 bool errorOccurred = false; 345 bool errorOccurred = false;
328 if (m_client->shouldLoadLink()) 346 if (m_client->shouldLoadLink())
329 createLinkPreloadResourceClient(preloadIfNeeded(relAttribute, href, docu ment, as, type, crossOrigin, LinkCalledFromMarkup, errorOccurred)); 347 createLinkPreloadResourceClient(preloadIfNeeded(relAttribute, href, docu ment, as, type, media, crossOrigin, LinkCalledFromMarkup, errorOccurred, nullptr ));
330 if (errorOccurred) 348 if (errorOccurred)
331 m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE); 349 m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE);
332 350
333 if (href.isEmpty() || !href.isValid()) 351 if (href.isEmpty() || !href.isValid())
334 released(); 352 released();
335 353
336 // FIXME(crbug.com/323096): Should take care of import. 354 // FIXME(crbug.com/323096): Should take care of import.
337 if (relAttribute.isLinkPrefetch() && href.isValid() && document.frame()) { 355 if (relAttribute.isLinkPrefetch() && href.isValid() && document.frame()) {
338 if (!m_client->shouldLoadLink()) 356 if (!m_client->shouldLoadLink())
339 return false; 357 return false;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 392
375 DEFINE_TRACE(LinkLoader) 393 DEFINE_TRACE(LinkLoader)
376 { 394 {
377 visitor->trace(m_client); 395 visitor->trace(m_client);
378 visitor->trace(m_prerender); 396 visitor->trace(m_prerender);
379 visitor->trace(m_linkPreloadResourceClient); 397 visitor->trace(m_linkPreloadResourceClient);
380 ResourceOwner<Resource, ResourceClient>::trace(visitor); 398 ResourceOwner<Resource, ResourceClient>::trace(visitor);
381 } 399 }
382 400
383 } // namespace blink 401 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698