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

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: Review comments 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/html/parser/HTMLPreloadScannerTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "core/html/HTMLMetaElement.h" 44 #include "core/html/HTMLMetaElement.h"
45 #include "core/html/LinkRelAttribute.h" 45 #include "core/html/LinkRelAttribute.h"
46 #include "core/html/parser/HTMLParserIdioms.h" 46 #include "core/html/parser/HTMLParserIdioms.h"
47 #include "core/html/parser/HTMLSrcsetParser.h" 47 #include "core/html/parser/HTMLSrcsetParser.h"
48 #include "core/html/parser/HTMLTokenizer.h" 48 #include "core/html/parser/HTMLTokenizer.h"
49 #include "core/loader/LinkLoader.h" 49 #include "core/loader/LinkLoader.h"
50 #include "platform/Histogram.h" 50 #include "platform/Histogram.h"
51 #include "platform/instrumentation/tracing/TraceEvent.h" 51 #include "platform/instrumentation/tracing/TraceEvent.h"
52 #include "platform/network/mime/ContentType.h" 52 #include "platform/network/mime/ContentType.h"
53 #include "platform/network/mime/MIMETypeRegistry.h" 53 #include "platform/network/mime/MIMETypeRegistry.h"
54 #include "wtf/Optional.h"
54 #include <memory> 55 #include <memory>
55 56
56 namespace blink { 57 namespace blink {
57 58
58 namespace { 59 namespace {
59 60
60 // When adding values to this enum, update histograms.xml as well. 61 // When adding values to this enum, update histograms.xml as well.
61 enum DocumentWriteGatedEvaluation { 62 enum DocumentWriteGatedEvaluation {
62 GatedEvaluationScriptTooLong, 63 GatedEvaluationScriptTooLong,
63 GatedEvaluationNoLikelyScript, 64 GatedEvaluationNoLikelyScript,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 if (match(tagImpl, imgTag)) 114 if (match(tagImpl, imgTag))
114 return imgTag.localName(); 115 return imgTag.localName();
115 if (match(tagImpl, inputTag)) 116 if (match(tagImpl, inputTag))
116 return inputTag.localName(); 117 return inputTag.localName();
117 if (match(tagImpl, linkTag)) 118 if (match(tagImpl, linkTag))
118 return linkTag.localName(); 119 return linkTag.localName();
119 if (match(tagImpl, scriptTag)) 120 if (match(tagImpl, scriptTag))
120 return scriptTag.localName(); 121 return scriptTag.localName();
121 if (match(tagImpl, videoTag)) 122 if (match(tagImpl, videoTag))
122 return videoTag.localName(); 123 return videoTag.localName();
123 ASSERT_NOT_REACHED(); 124 NOTREACHED();
124 return emptyString(); 125 return emptyString();
125 } 126 }
126 127
127 static bool mediaAttributeMatches(const MediaValuesCached& mediaValues, 128 static bool mediaAttributeMatches(const MediaValuesCached& mediaValues,
128 const String& attributeValue) { 129 const String& attributeValue) {
129 MediaQuerySet* mediaQueries = MediaQuerySet::create(attributeValue); 130 MediaQuerySet* mediaQueries = MediaQuerySet::create(attributeValue);
130 MediaQueryEvaluator mediaQueryEvaluator(mediaValues); 131 MediaQueryEvaluator mediaQueryEvaluator(mediaValues);
131 return mediaQueryEvaluator.eval(mediaQueries); 132 return mediaQueryEvaluator.eval(mediaQueries);
132 } 133 }
133 134
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 } 199 }
199 200
200 std::unique_ptr<PreloadRequest> createPreloadRequest( 201 std::unique_ptr<PreloadRequest> createPreloadRequest(
201 const KURL& predictedBaseURL, 202 const KURL& predictedBaseURL,
202 const SegmentedString& source, 203 const SegmentedString& source,
203 const ClientHintsPreferences& clientHintsPreferences, 204 const ClientHintsPreferences& clientHintsPreferences,
204 const PictureData& pictureData, 205 const PictureData& pictureData,
205 const ReferrerPolicy documentReferrerPolicy) { 206 const ReferrerPolicy documentReferrerPolicy) {
206 PreloadRequest::RequestType requestType = 207 PreloadRequest::RequestType requestType =
207 PreloadRequest::RequestTypePreload; 208 PreloadRequest::RequestTypePreload;
209 WTF::Optional<Resource::Type> type;
208 if (shouldPreconnect()) { 210 if (shouldPreconnect()) {
209 requestType = PreloadRequest::RequestTypePreconnect; 211 requestType = PreloadRequest::RequestTypePreconnect;
210 } else { 212 } else {
211 if (isLinkRelPreload()) { 213 if (isLinkRelPreload()) {
212 requestType = PreloadRequest::RequestTypeLinkRelPreload; 214 requestType = PreloadRequest::RequestTypeLinkRelPreload;
215 type = resourceTypeForLinkPreload();
216 if (type == WTF::nullopt)
217 return nullptr;
213 } 218 }
214 if (!shouldPreload()) { 219 if (!shouldPreload(type)) {
215 return nullptr; 220 return nullptr;
216 } 221 }
217 } 222 }
218 223
219 TextPosition position = 224 TextPosition position =
220 TextPosition(source.currentLine(), source.currentColumn()); 225 TextPosition(source.currentLine(), source.currentColumn());
221 FetchRequest::ResourceWidth resourceWidth; 226 FetchRequest::ResourceWidth resourceWidth;
222 float sourceSize = m_sourceSize; 227 float sourceSize = m_sourceSize;
223 bool sourceSizeSet = m_sourceSizeSet; 228 bool sourceSizeSet = m_sourceSizeSet;
224 if (pictureData.picked) { 229 if (pictureData.picked) {
225 sourceSizeSet = pictureData.sourceSizeSet; 230 sourceSizeSet = pictureData.sourceSizeSet;
226 sourceSize = pictureData.sourceSize; 231 sourceSize = pictureData.sourceSize;
227 } 232 }
228 if (sourceSizeSet) { 233 if (sourceSizeSet) {
229 resourceWidth.width = sourceSize; 234 resourceWidth.width = sourceSize;
230 resourceWidth.isSet = true; 235 resourceWidth.isSet = true;
231 } 236 }
232 237
233 Resource::Type type; 238 if (type == WTF::nullopt)
234 if (!resourceType(type)) 239 type = resourceType();
235 return nullptr;
236 240
237 // The element's 'referrerpolicy' attribute (if present) takes precedence 241 // The element's 'referrerpolicy' attribute (if present) takes precedence
238 // over the document's referrer policy. 242 // over the document's referrer policy.
239 ReferrerPolicy referrerPolicy = (m_referrerPolicy != ReferrerPolicyDefault) 243 ReferrerPolicy referrerPolicy = (m_referrerPolicy != ReferrerPolicyDefault)
240 ? m_referrerPolicy 244 ? m_referrerPolicy
241 : documentReferrerPolicy; 245 : documentReferrerPolicy;
242 auto request = PreloadRequest::createIfNeeded( 246 auto request = PreloadRequest::createIfNeeded(
243 initiatorFor(m_tagImpl), position, m_urlToLoad, predictedBaseURL, type, 247 initiatorFor(m_tagImpl), position, m_urlToLoad, predictedBaseURL,
244 referrerPolicy, resourceWidth, clientHintsPreferences, requestType); 248 type.value(), referrerPolicy, resourceWidth, clientHintsPreferences,
249 requestType);
245 if (!request) 250 if (!request)
246 return nullptr; 251 return nullptr;
247 252
248 request->setCrossOrigin(m_crossOrigin); 253 request->setCrossOrigin(m_crossOrigin);
249 request->setNonce(m_nonce); 254 request->setNonce(m_nonce);
250 request->setCharset(charset()); 255 request->setCharset(charset());
251 request->setDefer(m_defer); 256 request->setDefer(m_defer);
252 request->setIntegrityMetadata(m_integrityMetadata); 257 request->setIntegrityMetadata(m_integrityMetadata);
253 258
254 return request; 259 return request;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 m_linkIsImport = rel.isImport(); 347 m_linkIsImport = rel.isImport();
343 } else if (match(attributeName, mediaAttr)) { 348 } else if (match(attributeName, mediaAttr)) {
344 m_matched &= mediaAttributeMatches(*m_mediaValues, attributeValue); 349 m_matched &= mediaAttributeMatches(*m_mediaValues, attributeValue);
345 } else if (match(attributeName, crossoriginAttr)) { 350 } else if (match(attributeName, crossoriginAttr)) {
346 setCrossOrigin(attributeValue); 351 setCrossOrigin(attributeValue);
347 } else if (match(attributeName, nonceAttr)) { 352 } else if (match(attributeName, nonceAttr)) {
348 setNonce(attributeValue); 353 setNonce(attributeValue);
349 } else if (match(attributeName, asAttr)) { 354 } else if (match(attributeName, asAttr)) {
350 m_asAttributeValue = attributeValue.lower(); 355 m_asAttributeValue = attributeValue.lower();
351 } else if (match(attributeName, typeAttr)) { 356 } else if (match(attributeName, typeAttr)) {
352 m_matched &= MIMETypeRegistry::isSupportedStyleSheetMIMEType( 357 m_typeAttributeValue = attributeValue;
353 ContentType(attributeValue).type());
354 } else if (!m_referrerPolicySet && 358 } else if (!m_referrerPolicySet &&
355 match(attributeName, referrerpolicyAttr) && 359 match(attributeName, referrerpolicyAttr) &&
356 !attributeValue.isNull()) { 360 !attributeValue.isNull()) {
357 m_referrerPolicySet = true; 361 m_referrerPolicySet = true;
358 SecurityPolicy::referrerPolicyFromString(attributeValue, 362 SecurityPolicy::referrerPolicyFromString(attributeValue,
359 &m_referrerPolicy); 363 &m_referrerPolicy);
360 } 364 }
361 } 365 }
362 366
363 template <typename NameType> 367 template <typename NameType>
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 } 440 }
437 441
438 const String& charset() const { 442 const String& charset() const {
439 // FIXME: Its not clear that this if is needed, the loader probably ignores 443 // FIXME: Its not clear that this if is needed, the loader probably ignores
440 // charset for image requests anyway. 444 // charset for image requests anyway.
441 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag)) 445 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag))
442 return emptyString(); 446 return emptyString();
443 return m_charset; 447 return m_charset;
444 } 448 }
445 449
446 bool resourceType(Resource::Type& type) const { 450 WTF::Optional<Resource::Type> resourceTypeForLinkPreload() const {
451 DCHECK(m_linkIsPreload);
452 return LinkLoader::getResourceTypeFromAsAttribute(m_asAttributeValue);
453 }
454
455 Resource::Type resourceType() const {
447 if (match(m_tagImpl, scriptTag)) { 456 if (match(m_tagImpl, scriptTag)) {
448 type = Resource::Script; 457 return Resource::Script;
449 } else if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag) || 458 } else if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag) ||
450 (match(m_tagImpl, inputTag) && m_inputIsImage)) { 459 (match(m_tagImpl, inputTag) && m_inputIsImage)) {
451 type = Resource::Image; 460 return Resource::Image;
452 } else if (match(m_tagImpl, linkTag) && m_linkIsStyleSheet) { 461 } else if (match(m_tagImpl, linkTag) && m_linkIsStyleSheet) {
453 type = Resource::CSSStyleSheet; 462 return Resource::CSSStyleSheet;
454 } else if (m_linkIsPreconnect) { 463 } else if (m_linkIsPreconnect) {
455 type = Resource::Raw; 464 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) { 465 } else if (match(m_tagImpl, linkTag) && m_linkIsImport) {
460 type = Resource::ImportResource; 466 return Resource::ImportResource;
461 } else {
462 ASSERT_NOT_REACHED();
463 } 467 }
464 return true; 468 NOTREACHED();
469 return Resource::Raw;
465 } 470 }
466 471
467 bool shouldPreconnect() const { 472 bool shouldPreconnect() const {
468 return match(m_tagImpl, linkTag) && m_linkIsPreconnect && 473 return match(m_tagImpl, linkTag) && m_linkIsPreconnect &&
469 !m_urlToLoad.isEmpty(); 474 !m_urlToLoad.isEmpty();
470 } 475 }
471 476
472 bool isLinkRelPreload() const { 477 bool isLinkRelPreload() const {
473 return match(m_tagImpl, linkTag) && m_linkIsPreload && 478 return match(m_tagImpl, linkTag) && m_linkIsPreload &&
474 !m_urlToLoad.isEmpty(); 479 !m_urlToLoad.isEmpty();
475 } 480 }
476 481
477 bool shouldPreload() const { 482 bool shouldPreloadLink(WTF::Optional<Resource::Type>& type) const {
483 if (m_linkIsStyleSheet) {
484 return m_typeAttributeValue.isEmpty() ||
485 MIMETypeRegistry::isSupportedStyleSheetMIMEType(
486 ContentType(m_typeAttributeValue).type());
487 } else if (m_linkIsPreload) {
488 if (m_typeAttributeValue.isEmpty())
489 return true;
490 String typeFromAttribute = ContentType(m_typeAttributeValue).type();
491 if ((type == Resource::Font &&
492 !MIMETypeRegistry::isSupportedFontMIMEType(typeFromAttribute)) ||
493 (type == Resource::Image &&
494 !MIMETypeRegistry::isSupportedImagePrefixedMIMEType(
495 typeFromAttribute)) ||
496 (type == Resource::CSSStyleSheet &&
497 !MIMETypeRegistry::isSupportedStyleSheetMIMEType(
498 typeFromAttribute))) {
499 return false;
500 }
501 } else if (!m_linkIsImport) {
502 return false;
503 }
504
505 return true;
506 }
507
508 bool shouldPreload(WTF::Optional<Resource::Type>& type) const {
478 if (m_urlToLoad.isEmpty()) 509 if (m_urlToLoad.isEmpty())
479 return false; 510 return false;
480 if (!m_matched) 511 if (!m_matched)
481 return false; 512 return false;
482 if (match(m_tagImpl, linkTag) && !m_linkIsStyleSheet && !m_linkIsImport && 513 if (match(m_tagImpl, linkTag))
483 !m_linkIsPreload) 514 return shouldPreloadLink(type);
484 return false;
485 if (match(m_tagImpl, inputTag) && !m_inputIsImage) 515 if (match(m_tagImpl, inputTag) && !m_inputIsImage)
486 return false; 516 return false;
487 if (match(m_tagImpl, scriptTag) && 517 if (match(m_tagImpl, scriptTag) &&
488 !ScriptLoader::isValidScriptTypeAndLanguage( 518 !ScriptLoader::isValidScriptTypeAndLanguage(
489 m_typeAttributeValue, m_languageAttributeValue, 519 m_typeAttributeValue, m_languageAttributeValue,
490 ScriptLoader::AllowLegacyTypeInTypeAttribute)) { 520 ScriptLoader::AllowLegacyTypeInTypeAttribute)) {
491 return false; 521 return false;
492 } 522 }
493 return true; 523 return true;
494 } 524 }
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 defaultViewportMinWidth = document->viewportDefaultMinWidth(); 922 defaultViewportMinWidth = document->viewportDefaultMinWidth();
893 viewportMetaZeroValuesQuirk = 923 viewportMetaZeroValuesQuirk =
894 document->settings() && 924 document->settings() &&
895 document->settings()->getViewportMetaZeroValuesQuirk(); 925 document->settings()->getViewportMetaZeroValuesQuirk();
896 viewportMetaEnabled = 926 viewportMetaEnabled =
897 document->settings() && document->settings()->getViewportMetaEnabled(); 927 document->settings() && document->settings()->getViewportMetaEnabled();
898 referrerPolicy = document->getReferrerPolicy(); 928 referrerPolicy = document->getReferrerPolicy();
899 } 929 }
900 930
901 } // namespace blink 931 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/html/parser/HTMLPreloadScannerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698