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

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

Issue 1152043005: Add <link rel=preconnect> support to the HTMLPreloadScanner (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Added HTTP family test Created 5 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 | 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::createOffMai nThread(attributeValue); 104 RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::createOffMai nThread(attributeValue);
105 MediaQueryEvaluator mediaQueryEvaluator(mediaValues); 105 MediaQueryEvaluator mediaQueryEvaluator(mediaValues);
106 return mediaQueryEvaluator.eval(mediaQueries.get()); 106 return mediaQueryEvaluator.eval(mediaQueries.get());
107 } 107 }
108 108
109 class TokenPreloadScanner::StartTagScanner { 109 class TokenPreloadScanner::StartTagScanner {
110 public: 110 public:
111 StartTagScanner(const StringImpl* tagImpl, PassRefPtr<MediaValues> mediaValu es) 111 StartTagScanner(const StringImpl* tagImpl, PassRefPtr<MediaValues> mediaValu es)
112 : m_tagImpl(tagImpl) 112 : m_tagImpl(tagImpl)
113 , m_linkIsStyleSheet(false) 113 , m_linkIsStyleSheet(false)
114 , m_linkIsPreconnect(false)
114 , m_matchedMediaAttribute(true) 115 , m_matchedMediaAttribute(true)
115 , m_inputIsImage(false) 116 , m_inputIsImage(false)
116 , m_sourceSize(0) 117 , m_sourceSize(0)
117 , m_sourceSizeSet(false) 118 , m_sourceSizeSet(false)
118 , m_isCORSEnabled(false) 119 , m_isCORSEnabled(false)
119 , m_defer(FetchRequest::NoDefer) 120 , m_defer(FetchRequest::NoDefer)
120 , m_allowCredentials(DoNotAllowStoredCredentials) 121 , m_allowCredentials(DoNotAllowStoredCredentials)
121 , m_mediaValues(mediaValues) 122 , m_mediaValues(mediaValues)
122 { 123 {
123 ASSERT(m_mediaValues->isCached()); 124 ASSERT(m_mediaValues->isCached());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 void handlePictureSourceURL(String& sourceURL) 162 void handlePictureSourceURL(String& sourceURL)
162 { 163 {
163 if (match(m_tagImpl, sourceTag) && m_matchedMediaAttribute && sourceURL. isEmpty()) 164 if (match(m_tagImpl, sourceTag) && m_matchedMediaAttribute && sourceURL. isEmpty())
164 sourceURL = m_srcsetImageCandidate.toString(); 165 sourceURL = m_srcsetImageCandidate.toString();
165 else if (match(m_tagImpl, imgTag) && !sourceURL.isEmpty()) 166 else if (match(m_tagImpl, imgTag) && !sourceURL.isEmpty())
166 setUrlToLoad(sourceURL, AllowURLReplacement); 167 setUrlToLoad(sourceURL, AllowURLReplacement);
167 } 168 }
168 169
169 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source, const ClientHintsPreferences& clientHintsPrefer ences) 170 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source, const ClientHintsPreferences& clientHintsPrefer ences)
170 { 171 {
171 if (!shouldPreload() || !m_matchedMediaAttribute) 172 PreloadRequest::RequestType requestType = PreloadRequest::RequestTypePre load;
173 if (shouldPreconnect())
174 requestType = PreloadRequest::RequestTypePreconnect;
175 else if (!shouldPreload() || !m_matchedMediaAttribute)
172 return nullptr; 176 return nullptr;
173 177
174 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn()); 178 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn());
175 FetchRequest::ResourceWidth resourceWidth; 179 FetchRequest::ResourceWidth resourceWidth;
176 if (m_sourceSizeSet && m_srcsetImageCandidate.resourceWidth() != Uniniti alizedDescriptor) { 180 if (m_sourceSizeSet && m_srcsetImageCandidate.resourceWidth() != Uniniti alizedDescriptor) {
177 resourceWidth.width = m_sourceSize; 181 resourceWidth.width = m_sourceSize;
178 resourceWidth.isSet = true; 182 resourceWidth.isSet = true;
179 } 183 }
180 OwnPtr<PreloadRequest> request = PreloadRequest::create(initiatorFor(m_t agImpl), position, m_urlToLoad, predictedBaseURL, resourceType(), resourceWidth, clientHintsPreferences); 184 OwnPtr<PreloadRequest> request = PreloadRequest::create(initiatorFor(m_t agImpl), position, m_urlToLoad, predictedBaseURL, resourceType(), resourceWidth, clientHintsPreferences, requestType);
181 if (isCORSEnabled()) 185 if (isCORSEnabled())
182 request->setCrossOriginEnabled(allowStoredCredentials()); 186 request->setCrossOriginEnabled(allowStoredCredentials());
183 request->setCharset(charset()); 187 request->setCharset(charset());
184 request->setDefer(m_defer); 188 request->setDefer(m_defer);
185 return request.release(); 189 return request.release();
186 } 190 }
187 191
188 private: 192 private:
189 template<typename NameType> 193 template<typename NameType>
190 void processScriptAttribute(const NameType& attributeName, const String& att ributeValue) 194 void processScriptAttribute(const NameType& attributeName, const String& att ributeValue)
(...skipping 28 matching lines...) Expand all
219 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_media Values->devicePixelRatio(), m_sourceSize, m_srcsetAttributeValue); 223 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_media Values->devicePixelRatio(), m_sourceSize, m_srcsetAttributeValue);
220 setUrlToLoad(bestFitSourceForImageAttributes(m_mediaValues->devi cePixelRatio(), m_sourceSize, m_imgSrcUrl, m_srcsetImageCandidate), AllowURLRepl acement); 224 setUrlToLoad(bestFitSourceForImageAttributes(m_mediaValues->devi cePixelRatio(), m_sourceSize, m_imgSrcUrl, m_srcsetImageCandidate), AllowURLRepl acement);
221 } 225 }
222 } 226 }
223 } 227 }
224 228
225 template<typename NameType> 229 template<typename NameType>
226 void processLinkAttribute(const NameType& attributeName, const String& attri buteValue) 230 void processLinkAttribute(const NameType& attributeName, const String& attri buteValue)
227 { 231 {
228 // FIXME - Don't set rel/media/crossorigin multiple times. 232 // FIXME - Don't set rel/media/crossorigin multiple times.
229 if (match(attributeName, hrefAttr)) 233 if (match(attributeName, hrefAttr)) {
230 setUrlToLoad(attributeValue, DisallowURLReplacement); 234 setUrlToLoad(attributeValue, DisallowURLReplacement);
231 else if (match(attributeName, relAttr)) 235 } else if (match(attributeName, relAttr)) {
232 m_linkIsStyleSheet = relAttributeIsStyleSheet(attributeValue); 236 LinkRelAttribute rel(attributeValue);
233 else if (match(attributeName, mediaAttr)) 237 m_linkIsStyleSheet = rel.isStyleSheet() && !rel.isAlternate() && rel .iconType() == InvalidIcon && !rel.isDNSPrefetch();
238 m_linkIsPreconnect = rel.isPreconnect();
239 } else if (match(attributeName, mediaAttr)) {
234 m_matchedMediaAttribute = mediaAttributeMatches(*m_mediaValues, attr ibuteValue); 240 m_matchedMediaAttribute = mediaAttributeMatches(*m_mediaValues, attr ibuteValue);
235 else if (match(attributeName, crossoriginAttr)) 241 } else if (match(attributeName, crossoriginAttr)) {
236 setCrossOriginAllowed(attributeValue); 242 setCrossOriginAllowed(attributeValue);
243 }
237 } 244 }
238 245
239 template<typename NameType> 246 template<typename NameType>
240 void processInputAttribute(const NameType& attributeName, const String& attr ibuteValue) 247 void processInputAttribute(const NameType& attributeName, const String& attr ibuteValue)
241 { 248 {
242 // FIXME - Don't set type multiple times. 249 // FIXME - Don't set type multiple times.
243 if (match(attributeName, srcAttr)) 250 if (match(attributeName, srcAttr))
244 setUrlToLoad(attributeValue, DisallowURLReplacement); 251 setUrlToLoad(attributeValue, DisallowURLReplacement);
245 else if (match(attributeName, typeAttr)) 252 else if (match(attributeName, typeAttr))
246 m_inputIsImage = equalIgnoringCase(attributeValue, InputTypeNames::i mage); 253 m_inputIsImage = equalIgnoringCase(attributeValue, InputTypeNames::i mage);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 else if (match(m_tagImpl, linkTag)) 291 else if (match(m_tagImpl, linkTag))
285 processLinkAttribute(attributeName, attributeValue); 292 processLinkAttribute(attributeName, attributeValue);
286 else if (match(m_tagImpl, inputTag)) 293 else if (match(m_tagImpl, inputTag))
287 processInputAttribute(attributeName, attributeValue); 294 processInputAttribute(attributeName, attributeValue);
288 else if (match(m_tagImpl, sourceTag)) 295 else if (match(m_tagImpl, sourceTag))
289 processSourceAttribute(attributeName, attributeValue); 296 processSourceAttribute(attributeName, attributeValue);
290 else if (match(m_tagImpl, videoTag)) 297 else if (match(m_tagImpl, videoTag))
291 processVideoAttribute(attributeName, attributeValue); 298 processVideoAttribute(attributeName, attributeValue);
292 } 299 }
293 300
294 static bool relAttributeIsStyleSheet(const String& attributeValue)
295 {
296 LinkRelAttribute rel(attributeValue);
297 return rel.isStyleSheet() && !rel.isAlternate() && rel.iconType() == Inv alidIcon && !rel.isDNSPrefetch();
298 }
299
300 void setUrlToLoad(const String& value, URLReplacement replacement) 301 void setUrlToLoad(const String& value, URLReplacement replacement)
301 { 302 {
302 // We only respect the first src/href, per HTML5: 303 // We only respect the first src/href, per HTML5:
303 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenizat ion.html#attribute-name-state 304 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenizat ion.html#attribute-name-state
304 if (replacement == DisallowURLReplacement && !m_urlToLoad.isEmpty()) 305 if (replacement == DisallowURLReplacement && !m_urlToLoad.isEmpty())
305 return; 306 return;
306 String url = stripLeadingAndTrailingHTMLSpaces(value); 307 String url = stripLeadingAndTrailingHTMLSpaces(value);
307 if (url.isEmpty()) 308 if (url.isEmpty())
308 return; 309 return;
309 m_urlToLoad = url; 310 m_urlToLoad = url;
310 } 311 }
311 312
312 const String& charset() const 313 const String& charset() const
313 { 314 {
314 // FIXME: Its not clear that this if is needed, the loader probably igno res charset for image requests anyway. 315 // FIXME: Its not clear that this if is needed, the loader probably igno res charset for image requests anyway.
315 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag)) 316 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag))
316 return emptyString(); 317 return emptyString();
317 return m_charset; 318 return m_charset;
318 } 319 }
319 320
320 Resource::Type resourceType() const 321 Resource::Type resourceType() const
321 { 322 {
322 if (match(m_tagImpl, scriptTag)) 323 if (match(m_tagImpl, scriptTag))
323 return Resource::Script; 324 return Resource::Script;
324 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag) || (match(m_t agImpl, inputTag) && m_inputIsImage)) 325 if (match(m_tagImpl, imgTag) || match(m_tagImpl, videoTag) || (match(m_t agImpl, inputTag) && m_inputIsImage))
325 return Resource::Image; 326 return Resource::Image;
326 if (match(m_tagImpl, linkTag) && m_linkIsStyleSheet) 327 if (match(m_tagImpl, linkTag) && m_linkIsStyleSheet)
327 return Resource::CSSStyleSheet; 328 return Resource::CSSStyleSheet;
329 if (m_linkIsPreconnect)
330 return Resource::Raw;
328 ASSERT_NOT_REACHED(); 331 ASSERT_NOT_REACHED();
329 return Resource::Raw; 332 return Resource::Raw;
330 } 333 }
331 334
335 bool shouldPreconnect() const
336 {
337 return match(m_tagImpl, linkTag) && m_linkIsPreconnect && !m_urlToLoad.i sEmpty();
338 }
339
332 bool shouldPreload() const 340 bool shouldPreload() const
333 { 341 {
334 if (m_urlToLoad.isEmpty()) 342 if (m_urlToLoad.isEmpty())
335 return false; 343 return false;
336 if (match(m_tagImpl, linkTag) && !m_linkIsStyleSheet) 344 if (match(m_tagImpl, linkTag) && !m_linkIsStyleSheet)
337 return false; 345 return false;
338 if (match(m_tagImpl, inputTag) && !m_inputIsImage) 346 if (match(m_tagImpl, inputTag) && !m_inputIsImage)
339 return false; 347 return false;
340 return true; 348 return true;
341 } 349 }
(...skipping 25 matching lines...) Expand all
367 bool defer() const 375 bool defer() const
368 { 376 {
369 return m_defer; 377 return m_defer;
370 } 378 }
371 379
372 const StringImpl* m_tagImpl; 380 const StringImpl* m_tagImpl;
373 String m_urlToLoad; 381 String m_urlToLoad;
374 ImageCandidate m_srcsetImageCandidate; 382 ImageCandidate m_srcsetImageCandidate;
375 String m_charset; 383 String m_charset;
376 bool m_linkIsStyleSheet; 384 bool m_linkIsStyleSheet;
385 bool m_linkIsPreconnect;
377 bool m_matchedMediaAttribute; 386 bool m_matchedMediaAttribute;
378 bool m_inputIsImage; 387 bool m_inputIsImage;
379 String m_imgSrcUrl; 388 String m_imgSrcUrl;
380 String m_srcsetAttributeValue; 389 String m_srcsetAttributeValue;
381 float m_sourceSize; 390 float m_sourceSize;
382 bool m_sourceSizeSet; 391 bool m_sourceSizeSet;
383 bool m_isCORSEnabled; 392 bool m_isCORSEnabled;
384 FetchRequest::DeferOption m_defer; 393 FetchRequest::DeferOption m_defer;
385 StoredCredentials m_allowCredentials; 394 StoredCredentials m_allowCredentials;
386 RefPtr<MediaValues> m_mediaValues; 395 RefPtr<MediaValues> m_mediaValues;
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 mediaValues = givenMediaValues; 609 mediaValues = givenMediaValues;
601 else 610 else
602 mediaValues = MediaValuesCached::create(*document); 611 mediaValues = MediaValuesCached::create(*document);
603 ASSERT(mediaValues->isSafeToSendToAnotherThread()); 612 ASSERT(mediaValues->isSafeToSendToAnotherThread());
604 defaultViewportMinWidth = document->viewportDefaultMinWidth(); 613 defaultViewportMinWidth = document->viewportDefaultMinWidth();
605 viewportMetaZeroValuesQuirk = document->settings() && document->settings()-> viewportMetaZeroValuesQuirk(); 614 viewportMetaZeroValuesQuirk = document->settings() && document->settings()-> viewportMetaZeroValuesQuirk();
606 viewportMetaEnabled = document->settings() && document->settings()->viewport MetaEnabled(); 615 viewportMetaEnabled = document->settings() && document->settings()->viewport MetaEnabled();
607 } 616 }
608 617
609 } 618 }
OLDNEW
« no previous file with comments | « no previous file | Source/core/html/parser/HTMLPreloadScannerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698