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

Side by Side Diff: third_party/WebKit/Source/core/html/parser/HTMLPreloadScanner.cpp

Issue 2620993002: Fix HTMLPreloadScanner handling of type in link preload. (Closed)
Patch Set: Created 3 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) 2008 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009 Torch Mobile, Inc. http://www.torchmobile.com/ 3 * Copyright (C) 2009 Torch Mobile, Inc. http://www.torchmobile.com/
4 * Copyright (C) 2010 Google Inc. All Rights Reserved. 4 * Copyright (C) 2010 Google Inc. All Rights Reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 if (match(tagImpl, imgTag)) 113 if (match(tagImpl, imgTag))
114 return imgTag.localName(); 114 return imgTag.localName();
115 if (match(tagImpl, inputTag)) 115 if (match(tagImpl, inputTag))
116 return inputTag.localName(); 116 return inputTag.localName();
117 if (match(tagImpl, linkTag)) 117 if (match(tagImpl, linkTag))
118 return linkTag.localName(); 118 return linkTag.localName();
119 if (match(tagImpl, scriptTag)) 119 if (match(tagImpl, scriptTag))
120 return scriptTag.localName(); 120 return scriptTag.localName();
121 if (match(tagImpl, videoTag)) 121 if (match(tagImpl, videoTag))
122 return videoTag.localName(); 122 return videoTag.localName();
123 ASSERT_NOT_REACHED(); 123 NOTREACHED();
124 return emptyString(); 124 return emptyString();
125 } 125 }
126 126
127 static bool mediaAttributeMatches(const MediaValuesCached& mediaValues, 127 static bool mediaAttributeMatches(const MediaValuesCached& mediaValues,
128 const String& attributeValue) { 128 const String& attributeValue) {
129 MediaQuerySet* mediaQueries = MediaQuerySet::create(attributeValue); 129 MediaQuerySet* mediaQueries = MediaQuerySet::create(attributeValue);
130 MediaQueryEvaluator mediaQueryEvaluator(mediaValues); 130 MediaQueryEvaluator mediaQueryEvaluator(mediaValues);
131 return mediaQueryEvaluator.eval(mediaQueries); 131 return mediaQueryEvaluator.eval(mediaQueries);
132 } 132 }
133 133
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 } 198 }
199 199
200 std::unique_ptr<PreloadRequest> createPreloadRequest( 200 std::unique_ptr<PreloadRequest> createPreloadRequest(
201 const KURL& predictedBaseURL, 201 const KURL& predictedBaseURL,
202 const SegmentedString& source, 202 const SegmentedString& source,
203 const ClientHintsPreferences& clientHintsPreferences, 203 const ClientHintsPreferences& clientHintsPreferences,
204 const PictureData& pictureData, 204 const PictureData& pictureData,
205 const ReferrerPolicy documentReferrerPolicy) { 205 const ReferrerPolicy documentReferrerPolicy) {
206 PreloadRequest::RequestType requestType = 206 PreloadRequest::RequestType requestType =
207 PreloadRequest::RequestTypePreload; 207 PreloadRequest::RequestTypePreload;
208 Optional<Resource::Type> type;
Charlie Harrison 2017/01/10 14:35:06 #include "wtf/Optional.h"
Yoav Weiss 2017/01/11 05:14:36 Added
208 if (shouldPreconnect()) { 209 if (shouldPreconnect()) {
209 requestType = PreloadRequest::RequestTypePreconnect; 210 requestType = PreloadRequest::RequestTypePreconnect;
210 } else { 211 } else {
211 if (isLinkRelPreload()) { 212 if (isLinkRelPreload()) {
212 requestType = PreloadRequest::RequestTypeLinkRelPreload; 213 requestType = PreloadRequest::RequestTypeLinkRelPreload;
214 type = resourceTypeForLinkPreload();
215 if (type == WTF::nullopt)
216 return nullptr;
213 } 217 }
214 if (!shouldPreload()) { 218 if (!shouldPreload(type)) {
215 return nullptr; 219 return nullptr;
216 } 220 }
217 } 221 }
218 222
219 TextPosition position = 223 TextPosition position =
220 TextPosition(source.currentLine(), source.currentColumn()); 224 TextPosition(source.currentLine(), source.currentColumn());
221 FetchRequest::ResourceWidth resourceWidth; 225 FetchRequest::ResourceWidth resourceWidth;
222 float sourceSize = m_sourceSize; 226 float sourceSize = m_sourceSize;
223 bool sourceSizeSet = m_sourceSizeSet; 227 bool sourceSizeSet = m_sourceSizeSet;
224 if (pictureData.picked) { 228 if (pictureData.picked) {
225 sourceSizeSet = pictureData.sourceSizeSet; 229 sourceSizeSet = pictureData.sourceSizeSet;
226 sourceSize = pictureData.sourceSize; 230 sourceSize = pictureData.sourceSize;
227 } 231 }
228 if (sourceSizeSet) { 232 if (sourceSizeSet) {
229 resourceWidth.width = sourceSize; 233 resourceWidth.width = sourceSize;
230 resourceWidth.isSet = true; 234 resourceWidth.isSet = true;
231 } 235 }
232 236
233 Resource::Type type; 237 if (type == WTF::nullopt)
234 if (!resourceType(type)) 238 type = resourceType();
235 return nullptr;
236 239
237 // The element's 'referrerpolicy' attribute (if present) takes precedence 240 // The element's 'referrerpolicy' attribute (if present) takes precedence
238 // over the document's referrer policy. 241 // over the document's referrer policy.
239 ReferrerPolicy referrerPolicy = (m_referrerPolicy != ReferrerPolicyDefault) 242 ReferrerPolicy referrerPolicy = (m_referrerPolicy != ReferrerPolicyDefault)
240 ? m_referrerPolicy 243 ? m_referrerPolicy
241 : documentReferrerPolicy; 244 : documentReferrerPolicy;
242 auto request = PreloadRequest::createIfNeeded( 245 auto request = PreloadRequest::createIfNeeded(
243 initiatorFor(m_tagImpl), position, m_urlToLoad, predictedBaseURL, type, 246 initiatorFor(m_tagImpl), position, m_urlToLoad, predictedBaseURL,
244 referrerPolicy, resourceWidth, clientHintsPreferences, requestType); 247 type.value(), referrerPolicy, resourceWidth, clientHintsPreferences,
248 requestType);
245 if (!request) 249 if (!request)
246 return nullptr; 250 return nullptr;
247 251
248 request->setCrossOrigin(m_crossOrigin); 252 request->setCrossOrigin(m_crossOrigin);
249 request->setNonce(m_nonce); 253 request->setNonce(m_nonce);
250 request->setCharset(charset()); 254 request->setCharset(charset());
251 request->setDefer(m_defer); 255 request->setDefer(m_defer);
252 request->setIntegrityMetadata(m_integrityMetadata); 256 request->setIntegrityMetadata(m_integrityMetadata);
253 257
254 return request; 258 return request;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 m_linkIsImport = rel.isImport(); 346 m_linkIsImport = rel.isImport();
343 } else if (match(attributeName, mediaAttr)) { 347 } else if (match(attributeName, mediaAttr)) {
344 m_matched &= mediaAttributeMatches(*m_mediaValues, attributeValue); 348 m_matched &= mediaAttributeMatches(*m_mediaValues, attributeValue);
345 } else if (match(attributeName, crossoriginAttr)) { 349 } else if (match(attributeName, crossoriginAttr)) {
346 setCrossOrigin(attributeValue); 350 setCrossOrigin(attributeValue);
347 } else if (match(attributeName, nonceAttr)) { 351 } else if (match(attributeName, nonceAttr)) {
348 setNonce(attributeValue); 352 setNonce(attributeValue);
349 } else if (match(attributeName, asAttr)) { 353 } else if (match(attributeName, asAttr)) {
350 m_asAttributeValue = attributeValue.lower(); 354 m_asAttributeValue = attributeValue.lower();
351 } else if (match(attributeName, typeAttr)) { 355 } else if (match(attributeName, typeAttr)) {
352 m_matched &= MIMETypeRegistry::isSupportedStyleSheetMIMEType( 356 m_typeAttributeValue = attributeValue;
353 ContentType(attributeValue).type());
354 } else if (!m_referrerPolicySet && 357 } else if (!m_referrerPolicySet &&
355 match(attributeName, referrerpolicyAttr) && 358 match(attributeName, referrerpolicyAttr) &&
356 !attributeValue.isNull()) { 359 !attributeValue.isNull()) {
357 m_referrerPolicySet = true; 360 m_referrerPolicySet = true;
358 SecurityPolicy::referrerPolicyFromString(attributeValue, 361 SecurityPolicy::referrerPolicyFromString(attributeValue,
359 &m_referrerPolicy); 362 &m_referrerPolicy);
360 } 363 }
361 } 364 }
362 365
363 template <typename NameType> 366 template <typename NameType>
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 } 439 }
437 440
438 const String& charset() const { 441 const String& charset() const {
439 // FIXME: Its not clear that this if is needed, the loader probably ignores 442 // FIXME: Its not clear that this if is needed, the loader probably ignores
440 // charset for image requests anyway. 443 // charset for image requests anyway.
441 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag)) 444 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag))
442 return emptyString(); 445 return emptyString();
443 return m_charset; 446 return m_charset;
444 } 447 }
445 448
446 bool resourceType(Resource::Type& type) const { 449 Optional<Resource::Type> resourceTypeForLinkPreload() const {
450 DCHECK(m_linkIsPreload);
451 return LinkLoader::getResourceTypeFromAsAttribute(m_asAttributeValue);
452 }
453
454 Resource::Type resourceType() const {
447 if (match(m_tagImpl, scriptTag)) { 455 if (match(m_tagImpl, scriptTag)) {
448 type = Resource::Script; 456 return Resource::Script;
449 } else if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag) || 457 } else if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag) ||
450 (match(m_tagImpl, inputTag) && m_inputIsImage)) { 458 (match(m_tagImpl, inputTag) && m_inputIsImage)) {
451 type = Resource::Image; 459 return Resource::Image;
452 } else if (match(m_tagImpl, linkTag) && m_linkIsStyleSheet) { 460 } else if (match(m_tagImpl, linkTag) && m_linkIsStyleSheet) {
453 type = Resource::CSSStyleSheet; 461 return Resource::CSSStyleSheet;
454 } else if (m_linkIsPreconnect) { 462 } else if (m_linkIsPreconnect) {
455 type = Resource::Raw; 463 return Resource::Raw;
456 } else if (m_linkIsPreload) {
457 if (!LinkLoader::getResourceTypeFromAsAttribute(m_asAttributeValue, type))
458 return false;
459 } else if (match(m_tagImpl, linkTag) && m_linkIsImport) { 464 } else if (match(m_tagImpl, linkTag) && m_linkIsImport) {
460 type = Resource::ImportResource; 465 return Resource::ImportResource;
461 } else {
462 ASSERT_NOT_REACHED();
463 } 466 }
464 return true; 467 NOTREACHED();
468 return Resource::Raw;
465 } 469 }
466 470
467 bool shouldPreconnect() const { 471 bool shouldPreconnect() const {
468 return match(m_tagImpl, linkTag) && m_linkIsPreconnect && 472 return match(m_tagImpl, linkTag) && m_linkIsPreconnect &&
469 !m_urlToLoad.isEmpty(); 473 !m_urlToLoad.isEmpty();
470 } 474 }
471 475
472 bool isLinkRelPreload() const { 476 bool isLinkRelPreload() const {
473 return match(m_tagImpl, linkTag) && m_linkIsPreload && 477 return match(m_tagImpl, linkTag) && m_linkIsPreload &&
474 !m_urlToLoad.isEmpty(); 478 !m_urlToLoad.isEmpty();
475 } 479 }
476 480
477 bool shouldPreload() const { 481 bool shouldPreloadLink(Optional<Resource::Type>& type) const {
482 if (m_linkIsStyleSheet) {
483 if (!m_typeAttributeValue.isEmpty() &&
Charlie Harrison 2017/01/10 14:35:06 This inner if could be rewritten as return m_type
Yoav Weiss 2017/01/11 05:14:36 done
484 !MIMETypeRegistry::isSupportedStyleSheetMIMEType(
485 ContentType(m_typeAttributeValue).type()))
486 return false;
487 } else if (m_linkIsPreload) {
488 if (!m_typeAttributeValue.isEmpty()) {
Charlie Harrison 2017/01/10 14:35:06 optional: It might make sense to also have an earl
Yoav Weiss 2017/01/11 05:14:36 done
489 String typeFromAttribute = ContentType(m_typeAttributeValue).type();
490 if (type == Resource::Font &&
Charlie Harrison 2017/01/10 14:35:06 These three conditions could be combined to: if (
Yoav Weiss 2017/01/11 05:14:36 Not really, as the isSupported* function call in e
491 !MIMETypeRegistry::isSupportedFontMIMEType(typeFromAttribute)) {
492 return false;
493 }
494 if (type == Resource::Image &&
495 !MIMETypeRegistry::isSupportedImagePrefixedMIMEType(
496 typeFromAttribute)) {
497 return false;
498 }
499 if (type == Resource::CSSStyleSheet &&
500 !MIMETypeRegistry::isSupportedStyleSheetMIMEType(
501 typeFromAttribute)) {
502 return false;
503 }
504 }
505 } else if (!m_linkIsImport) {
506 return false;
507 }
508
509 return true;
510 }
511
512 bool shouldPreload(Optional<Resource::Type>& type) const {
478 if (m_urlToLoad.isEmpty()) 513 if (m_urlToLoad.isEmpty())
479 return false; 514 return false;
480 if (!m_matched) 515 if (!m_matched)
481 return false; 516 return false;
482 if (match(m_tagImpl, linkTag) && !m_linkIsStyleSheet && !m_linkIsImport && 517 if (match(m_tagImpl, linkTag))
483 !m_linkIsPreload) 518 return shouldPreloadLink(type);
484 return false;
485 if (match(m_tagImpl, inputTag) && !m_inputIsImage) 519 if (match(m_tagImpl, inputTag) && !m_inputIsImage)
486 return false; 520 return false;
487 if (match(m_tagImpl, scriptTag) && 521 if (match(m_tagImpl, scriptTag) &&
488 !ScriptLoader::isValidScriptTypeAndLanguage( 522 !ScriptLoader::isValidScriptTypeAndLanguage(
489 m_typeAttributeValue, m_languageAttributeValue, 523 m_typeAttributeValue, m_languageAttributeValue,
490 ScriptLoader::AllowLegacyTypeInTypeAttribute)) { 524 ScriptLoader::AllowLegacyTypeInTypeAttribute)) {
491 return false; 525 return false;
492 } 526 }
493 return true; 527 return true;
494 } 528 }
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 defaultViewportMinWidth = document->viewportDefaultMinWidth(); 926 defaultViewportMinWidth = document->viewportDefaultMinWidth();
893 viewportMetaZeroValuesQuirk = 927 viewportMetaZeroValuesQuirk =
894 document->settings() && 928 document->settings() &&
895 document->settings()->getViewportMetaZeroValuesQuirk(); 929 document->settings()->getViewportMetaZeroValuesQuirk();
896 viewportMetaEnabled = 930 viewportMetaEnabled =
897 document->settings() && document->settings()->getViewportMetaEnabled(); 931 document->settings() && document->settings()->getViewportMetaEnabled();
898 referrerPolicy = document->getReferrerPolicy(); 932 referrerPolicy = document->getReferrerPolicy();
899 } 933 }
900 934
901 } // namespace blink 935 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698