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

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

Issue 2018353002: Avoid downloading unsupported `<source>` types in HTMLPreloadScanner (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix CSS preloading issue tests revealed Created 4 years, 6 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 MediaQueryEvaluator mediaQueryEvaluator(mediaValues); 132 MediaQueryEvaluator mediaQueryEvaluator(mediaValues);
133 return mediaQueryEvaluator.eval(mediaQueries); 133 return mediaQueryEvaluator.eval(mediaQueries);
134 } 134 }
135 135
136 class TokenPreloadScanner::StartTagScanner { 136 class TokenPreloadScanner::StartTagScanner {
137 STACK_ALLOCATED(); 137 STACK_ALLOCATED();
138 public: 138 public:
139 StartTagScanner(const StringImpl* tagImpl, MediaValuesCached* mediaValues) 139 StartTagScanner(const StringImpl* tagImpl, MediaValuesCached* mediaValues)
140 : m_tagImpl(tagImpl) 140 : m_tagImpl(tagImpl)
141 , m_linkIsStyleSheet(false) 141 , m_linkIsStyleSheet(false)
142 , m_linkTypeIsMissingOrSupportedStyleSheet(true)
143 , m_linkIsPreconnect(false) 142 , m_linkIsPreconnect(false)
144 , m_linkIsPreload(false) 143 , m_linkIsPreload(false)
145 , m_linkIsImport(false) 144 , m_linkIsImport(false)
146 , m_matchedMediaAttribute(true) 145 , m_matched(true)
147 , m_inputIsImage(false) 146 , m_inputIsImage(false)
148 , m_sourceSize(0) 147 , m_sourceSize(0)
149 , m_sourceSizeSet(false) 148 , m_sourceSizeSet(false)
150 , m_defer(FetchRequest::NoDefer) 149 , m_defer(FetchRequest::NoDefer)
151 , m_crossOrigin(CrossOriginAttributeNotSet) 150 , m_crossOrigin(CrossOriginAttributeNotSet)
152 , m_mediaValues(mediaValues) 151 , m_mediaValues(mediaValues)
153 , m_referrerPolicySet(false) 152 , m_referrerPolicySet(false)
154 , m_referrerPolicy(ReferrerPolicyDefault) 153 , m_referrerPolicy(ReferrerPolicyDefault)
155 { 154 {
156 if (match(m_tagImpl, imgTag) 155 if (match(m_tagImpl, imgTag)
(...skipping 28 matching lines...) Expand all
185 void processAttributes(const Vector<CompactHTMLToken::Attribute>& attributes ) 184 void processAttributes(const Vector<CompactHTMLToken::Attribute>& attributes )
186 { 185 {
187 if (!m_tagImpl) 186 if (!m_tagImpl)
188 return; 187 return;
189 for (const CompactHTMLToken::Attribute& htmlTokenAttribute : attributes) 188 for (const CompactHTMLToken::Attribute& htmlTokenAttribute : attributes)
190 processAttribute(htmlTokenAttribute.name(), htmlTokenAttribute.value ()); 189 processAttribute(htmlTokenAttribute.name(), htmlTokenAttribute.value ());
191 } 190 }
192 191
193 void handlePictureSourceURL(PictureData& pictureData) 192 void handlePictureSourceURL(PictureData& pictureData)
194 { 193 {
195 if (match(m_tagImpl, sourceTag) && m_matchedMediaAttribute && pictureDat a.sourceURL.isEmpty()) { 194 if (match(m_tagImpl, sourceTag) && m_matched && pictureData.sourceURL.is Empty()) {
196 pictureData.sourceURL = m_srcsetImageCandidate.toString(); 195 pictureData.sourceURL = m_srcsetImageCandidate.toString();
197 pictureData.sourceSizeSet = m_sourceSizeSet; 196 pictureData.sourceSizeSet = m_sourceSizeSet;
198 pictureData.sourceSize = m_sourceSize; 197 pictureData.sourceSize = m_sourceSize;
199 pictureData.picked = true; 198 pictureData.picked = true;
200 } else if (match(m_tagImpl, imgTag) && !pictureData.sourceURL.isEmpty()) { 199 } else if (match(m_tagImpl, imgTag) && !pictureData.sourceURL.isEmpty()) {
201 setUrlToLoad(pictureData.sourceURL, AllowURLReplacement); 200 setUrlToLoad(pictureData.sourceURL, AllowURLReplacement);
202 } 201 }
203 } 202 }
204 203
205 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source, const ClientHintsPreferences& clientHintsPrefer ences, const PictureData& pictureData, const ReferrerPolicy documentReferrerPoli cy) 204 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source, const ClientHintsPreferences& clientHintsPrefer ences, const PictureData& pictureData, const ReferrerPolicy documentReferrerPoli cy)
206 { 205 {
207 PreloadRequest::RequestType requestType = PreloadRequest::RequestTypePre load; 206 PreloadRequest::RequestType requestType = PreloadRequest::RequestTypePre load;
208 if (shouldPreconnect()) { 207 if (shouldPreconnect()) {
209 requestType = PreloadRequest::RequestTypePreconnect; 208 requestType = PreloadRequest::RequestTypePreconnect;
210 } else { 209 } else {
211 if (isLinkRelPreload()) { 210 if (isLinkRelPreload()) {
212 requestType = PreloadRequest::RequestTypeLinkRelPreload; 211 requestType = PreloadRequest::RequestTypeLinkRelPreload;
213 } 212 }
214 if (!shouldPreload() || !m_matchedMediaAttribute) { 213 if (!shouldPreload()) {
215 return nullptr; 214 return nullptr;
216 } 215 }
217 } 216 }
218 217
219 218
220 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn()); 219 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn());
221 FetchRequest::ResourceWidth resourceWidth; 220 FetchRequest::ResourceWidth resourceWidth;
222 float sourceSize = m_sourceSize; 221 float sourceSize = m_sourceSize;
223 bool sourceSizeSet = m_sourceSizeSet; 222 bool sourceSizeSet = m_sourceSizeSet;
224 if (pictureData.picked) { 223 if (pictureData.picked) {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 // FIXME - Don't set rel/media/crossorigin multiple times. 299 // FIXME - Don't set rel/media/crossorigin multiple times.
301 if (match(attributeName, hrefAttr)) { 300 if (match(attributeName, hrefAttr)) {
302 setUrlToLoad(attributeValue, DisallowURLReplacement); 301 setUrlToLoad(attributeValue, DisallowURLReplacement);
303 } else if (match(attributeName, relAttr)) { 302 } else if (match(attributeName, relAttr)) {
304 LinkRelAttribute rel(attributeValue); 303 LinkRelAttribute rel(attributeValue);
305 m_linkIsStyleSheet = rel.isStyleSheet() && !rel.isAlternate() && rel .getIconType() == InvalidIcon && !rel.isDNSPrefetch(); 304 m_linkIsStyleSheet = rel.isStyleSheet() && !rel.isAlternate() && rel .getIconType() == InvalidIcon && !rel.isDNSPrefetch();
306 m_linkIsPreconnect = rel.isPreconnect(); 305 m_linkIsPreconnect = rel.isPreconnect();
307 m_linkIsPreload = rel.isLinkPreload(); 306 m_linkIsPreload = rel.isLinkPreload();
308 m_linkIsImport = rel.isImport(); 307 m_linkIsImport = rel.isImport();
309 } else if (match(attributeName, mediaAttr)) { 308 } else if (match(attributeName, mediaAttr)) {
310 m_matchedMediaAttribute = mediaAttributeMatches(*m_mediaValues, attr ibuteValue); 309 m_matched &= mediaAttributeMatches(*m_mediaValues, attributeValue);
311 } else if (match(attributeName, crossoriginAttr)) { 310 } else if (match(attributeName, crossoriginAttr)) {
312 setCrossOrigin(attributeValue); 311 setCrossOrigin(attributeValue);
313 } else if (match(attributeName, asAttr)) { 312 } else if (match(attributeName, asAttr)) {
314 m_asAttributeValue = attributeValue; 313 m_asAttributeValue = attributeValue;
315 } else if (match(attributeName, typeAttr)) { 314 } else if (match(attributeName, typeAttr)) {
316 m_linkTypeIsMissingOrSupportedStyleSheet = MIMETypeRegistry::isSuppo rtedStyleSheetMIMEType(ContentType(attributeValue).type()); 315 m_matched &= MIMETypeRegistry::isSupportedStyleSheetMIMEType(Content Type(attributeValue).type());
317 } 316 }
318 } 317 }
319 318
320 template<typename NameType> 319 template<typename NameType>
321 void processInputAttribute(const NameType& attributeName, const String& attr ibuteValue) 320 void processInputAttribute(const NameType& attributeName, const String& attr ibuteValue)
322 { 321 {
323 // FIXME - Don't set type multiple times. 322 // FIXME - Don't set type multiple times.
324 if (match(attributeName, srcAttr)) 323 if (match(attributeName, srcAttr))
325 setUrlToLoad(attributeValue, DisallowURLReplacement); 324 setUrlToLoad(attributeValue, DisallowURLReplacement);
326 else if (match(attributeName, typeAttr)) 325 else if (match(attributeName, typeAttr))
327 m_inputIsImage = equalIgnoringCase(attributeValue, InputTypeNames::i mage); 326 m_inputIsImage = equalIgnoringCase(attributeValue, InputTypeNames::i mage);
328 } 327 }
329 328
330 template<typename NameType> 329 template<typename NameType>
331 void processSourceAttribute(const NameType& attributeName, const String& att ributeValue) 330 void processSourceAttribute(const NameType& attributeName, const String& att ributeValue)
332 { 331 {
333 if (match(attributeName, srcsetAttr) && m_srcsetImageCandidate.isEmpty() ) { 332 if (match(attributeName, srcsetAttr) && m_srcsetImageCandidate.isEmpty() ) {
334 m_srcsetAttributeValue = attributeValue; 333 m_srcsetAttributeValue = attributeValue;
335 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_mediaValu es->devicePixelRatio(), m_sourceSize, attributeValue); 334 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_mediaValu es->devicePixelRatio(), m_sourceSize, attributeValue);
336 } else if (match(attributeName, sizesAttr) && !m_sourceSizeSet) { 335 } else if (match(attributeName, sizesAttr) && !m_sourceSizeSet) {
337 m_sourceSize = SizesAttributeParser(m_mediaValues, attributeValue).l ength(); 336 m_sourceSize = SizesAttributeParser(m_mediaValues, attributeValue).l ength();
338 m_sourceSizeSet = true; 337 m_sourceSizeSet = true;
339 if (!m_srcsetImageCandidate.isEmpty()) { 338 if (!m_srcsetImageCandidate.isEmpty()) {
340 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_media Values->devicePixelRatio(), m_sourceSize, m_srcsetAttributeValue); 339 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_media Values->devicePixelRatio(), m_sourceSize, m_srcsetAttributeValue);
341 } 340 }
342 } else if (match(attributeName, mediaAttr)) { 341 } else if (match(attributeName, mediaAttr)) {
343 // FIXME - Don't match media multiple times. 342 // FIXME - Don't match media multiple times.
344 m_matchedMediaAttribute = mediaAttributeMatches(*m_mediaValues, attr ibuteValue); 343 m_matched &= mediaAttributeMatches(*m_mediaValues, attributeValue);
344 } else if (match(attributeName, typeAttr)) {
345 m_matched &= MIMETypeRegistry::isSupportedImagePrefixedMIMEType(Cont entType(attributeValue).type());
345 } 346 }
346 } 347 }
347 348
348 template<typename NameType> 349 template<typename NameType>
349 void processVideoAttribute(const NameType& attributeName, const String& attr ibuteValue) 350 void processVideoAttribute(const NameType& attributeName, const String& attr ibuteValue)
350 { 351 {
351 if (match(attributeName, posterAttr)) 352 if (match(attributeName, posterAttr))
352 setUrlToLoad(attributeValue, DisallowURLReplacement); 353 setUrlToLoad(attributeValue, DisallowURLReplacement);
353 } 354 }
354 355
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 421
421 bool isLinkRelPreload() const 422 bool isLinkRelPreload() const
422 { 423 {
423 return match(m_tagImpl, linkTag) && m_linkIsPreload && !m_urlToLoad.isEm pty(); 424 return match(m_tagImpl, linkTag) && m_linkIsPreload && !m_urlToLoad.isEm pty();
424 } 425 }
425 426
426 bool shouldPreload() const 427 bool shouldPreload() const
427 { 428 {
428 if (m_urlToLoad.isEmpty()) 429 if (m_urlToLoad.isEmpty())
429 return false; 430 return false;
431 if (!m_matched)
432 return false;
430 if (match(m_tagImpl, linkTag) && !m_linkIsStyleSheet && !m_linkIsImport && !m_linkIsPreload) 433 if (match(m_tagImpl, linkTag) && !m_linkIsStyleSheet && !m_linkIsImport && !m_linkIsPreload)
431 return false; 434 return false;
432 if (match(m_tagImpl, linkTag) && m_linkIsStyleSheet && !m_linkTypeIsMiss ingOrSupportedStyleSheet)
433 return false;
434 if (match(m_tagImpl, inputTag) && !m_inputIsImage) 435 if (match(m_tagImpl, inputTag) && !m_inputIsImage)
435 return false; 436 return false;
436 return true; 437 return true;
437 } 438 }
438 439
439 void setCrossOrigin(const String& corsSetting) 440 void setCrossOrigin(const String& corsSetting)
440 { 441 {
441 m_crossOrigin = crossOriginAttributeValue(corsSetting); 442 m_crossOrigin = crossOriginAttributeValue(corsSetting);
442 } 443 }
443 444
444 void setDefer(FetchRequest::DeferOption defer) 445 void setDefer(FetchRequest::DeferOption defer)
445 { 446 {
446 m_defer = defer; 447 m_defer = defer;
447 } 448 }
448 449
449 bool defer() const 450 bool defer() const
450 { 451 {
451 return m_defer; 452 return m_defer;
452 } 453 }
453 454
454 const StringImpl* m_tagImpl; 455 const StringImpl* m_tagImpl;
455 String m_urlToLoad; 456 String m_urlToLoad;
456 ImageCandidate m_srcsetImageCandidate; 457 ImageCandidate m_srcsetImageCandidate;
457 String m_charset; 458 String m_charset;
458 bool m_linkIsStyleSheet; 459 bool m_linkIsStyleSheet;
459 bool m_linkTypeIsMissingOrSupportedStyleSheet;
460 bool m_linkIsPreconnect; 460 bool m_linkIsPreconnect;
461 bool m_linkIsPreload; 461 bool m_linkIsPreload;
462 bool m_linkIsImport; 462 bool m_linkIsImport;
463 bool m_matchedMediaAttribute; 463 bool m_matched;
464 bool m_inputIsImage; 464 bool m_inputIsImage;
465 String m_imgSrcUrl; 465 String m_imgSrcUrl;
466 String m_srcsetAttributeValue; 466 String m_srcsetAttributeValue;
467 String m_asAttributeValue; 467 String m_asAttributeValue;
468 float m_sourceSize; 468 float m_sourceSize;
469 bool m_sourceSizeSet; 469 bool m_sourceSizeSet;
470 FetchRequest::DeferOption m_defer; 470 FetchRequest::DeferOption m_defer;
471 CrossOriginAttributeValue m_crossOrigin; 471 CrossOriginAttributeValue m_crossOrigin;
472 Member<MediaValuesCached> m_mediaValues; 472 Member<MediaValuesCached> m_mediaValues;
473 bool m_referrerPolicySet; 473 bool m_referrerPolicySet;
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 ASSERT(document); 803 ASSERT(document);
804 doHtmlPreloadScanning = !document->settings() || document->settings()->doHtm lPreloadScanning(); 804 doHtmlPreloadScanning = !document->settings() || document->settings()->doHtm lPreloadScanning();
805 doDocumentWritePreloadScanning = doHtmlPreloadScanning && document->frame() && document->frame()->isMainFrame(); 805 doDocumentWritePreloadScanning = doHtmlPreloadScanning && document->frame() && document->frame()->isMainFrame();
806 defaultViewportMinWidth = document->viewportDefaultMinWidth(); 806 defaultViewportMinWidth = document->viewportDefaultMinWidth();
807 viewportMetaZeroValuesQuirk = document->settings() && document->settings()-> viewportMetaZeroValuesQuirk(); 807 viewportMetaZeroValuesQuirk = document->settings() && document->settings()-> viewportMetaZeroValuesQuirk();
808 viewportMetaEnabled = document->settings() && document->settings()->viewport MetaEnabled(); 808 viewportMetaEnabled = document->settings() && document->settings()->viewport MetaEnabled();
809 referrerPolicy = document->getReferrerPolicy(); 809 referrerPolicy = document->getReferrerPolicy();
810 } 810 }
811 811
812 } // namespace blink 812 } // 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