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

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

Issue 201813002: Enable Media query evaluation in the preload scanner (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Removed GC transition types and commented code Created 6 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) 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 13 matching lines...) Expand all
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28 #include "config.h" 28 #include "config.h"
29 #include "core/html/parser/HTMLPreloadScanner.h" 29 #include "core/html/parser/HTMLPreloadScanner.h"
30 30
31 #include "HTMLNames.h" 31 #include "HTMLNames.h"
32 #include "InputTypeNames.h" 32 #include "InputTypeNames.h"
33 #include "RuntimeEnabledFeatures.h" 33 #include "RuntimeEnabledFeatures.h"
34 #include "core/css/MediaList.h"
35 #include "core/css/MediaQueryEvaluator.h"
34 #include "core/html/LinkRelAttribute.h" 36 #include "core/html/LinkRelAttribute.h"
35 #include "core/html/parser/HTMLParserIdioms.h" 37 #include "core/html/parser/HTMLParserIdioms.h"
36 #include "core/html/parser/HTMLSrcsetParser.h" 38 #include "core/html/parser/HTMLSrcsetParser.h"
37 #include "core/html/parser/HTMLTokenizer.h" 39 #include "core/html/parser/HTMLTokenizer.h"
38 #include "platform/TraceEvent.h" 40 #include "platform/TraceEvent.h"
39 #include "wtf/MainThread.h" 41 #include "wtf/MainThread.h"
40 42
41 namespace WebCore { 43 namespace WebCore {
42 44
43 using namespace HTMLNames; 45 using namespace HTMLNames;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 if (match(tagImpl, inputTag)) 85 if (match(tagImpl, inputTag))
84 return inputTag.localName(); 86 return inputTag.localName();
85 if (match(tagImpl, linkTag)) 87 if (match(tagImpl, linkTag))
86 return linkTag.localName(); 88 return linkTag.localName();
87 if (match(tagImpl, scriptTag)) 89 if (match(tagImpl, scriptTag))
88 return scriptTag.localName(); 90 return scriptTag.localName();
89 ASSERT_NOT_REACHED(); 91 ASSERT_NOT_REACHED();
90 return emptyString(); 92 return emptyString();
91 } 93 }
92 94
95 static bool mediaAttributeMatches(const MediaValues* mediaValues, const String& attributeValue)
96 {
97 RefPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(attributeValue);
98 MediaQueryEvaluator mediaQueryEvaluator("screen", mediaValues, false);
99 bool ret = mediaQueryEvaluator.eval(mediaQueries.get());
100 return ret;
abarth-chromium 2014/03/24 19:43:31 I'd skip |ret| and just return the result of the f
101 }
102
93 class TokenPreloadScanner::StartTagScanner { 103 class TokenPreloadScanner::StartTagScanner {
94 public: 104 public:
95 StartTagScanner(const StringImpl* tagImpl, float deviceScaleFactor) 105 StartTagScanner(const StringImpl* tagImpl, float deviceScaleFactor, PassRefP tr<MediaValues> mediaValues)
96 : m_tagImpl(tagImpl) 106 : m_tagImpl(tagImpl)
97 , m_linkIsStyleSheet(false) 107 , m_linkIsStyleSheet(false)
108 , m_mediaMatch(true)
abarth-chromium 2014/03/24 19:43:31 We can probably come up with a slightly better nam
98 , m_inputIsImage(false) 109 , m_inputIsImage(false)
99 , m_deviceScaleFactor(deviceScaleFactor) 110 , m_deviceScaleFactor(deviceScaleFactor)
100 , m_encounteredImgSrc(false) 111 , m_encounteredImgSrc(false)
101 , m_isCORSEnabled(false) 112 , m_isCORSEnabled(false)
102 , m_allowCredentials(DoNotAllowStoredCredentials) 113 , m_allowCredentials(DoNotAllowStoredCredentials)
114 , m_mediaValues(mediaValues)
103 { 115 {
104 if (!match(m_tagImpl, imgTag) 116 if (!match(m_tagImpl, imgTag)
105 && !match(m_tagImpl, inputTag) 117 && !match(m_tagImpl, inputTag)
106 && !match(m_tagImpl, linkTag) 118 && !match(m_tagImpl, linkTag)
107 && !match(m_tagImpl, scriptTag)) 119 && !match(m_tagImpl, scriptTag))
108 m_tagImpl = 0; 120 m_tagImpl = 0;
109 } 121 }
110 122
111 enum URLReplacement { 123 enum URLReplacement {
112 AllowURLReplacement, 124 AllowURLReplacement,
(...skipping 15 matching lines...) Expand all
128 void processAttributes(const Vector<CompactHTMLToken::Attribute>& attributes ) 140 void processAttributes(const Vector<CompactHTMLToken::Attribute>& attributes )
129 { 141 {
130 if (!m_tagImpl) 142 if (!m_tagImpl)
131 return; 143 return;
132 for (Vector<CompactHTMLToken::Attribute>::const_iterator iter = attribut es.begin(); iter != attributes.end(); ++iter) 144 for (Vector<CompactHTMLToken::Attribute>::const_iterator iter = attribut es.begin(); iter != attributes.end(); ++iter)
133 processAttribute(iter->name, iter->value); 145 processAttribute(iter->name, iter->value);
134 } 146 }
135 147
136 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source) 148 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source)
137 { 149 {
138 if (!shouldPreload()) 150 if (!shouldPreload() || !m_mediaMatch)
139 return nullptr; 151 return nullptr;
140 152
141 TRACE_EVENT_INSTANT1("net", "PreloadRequest", "url", m_urlToLoad.ascii() ); 153 TRACE_EVENT_INSTANT1("net", "PreloadRequest", "url", m_urlToLoad.ascii() );
142 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn()); 154 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn());
143 OwnPtr<PreloadRequest> request = PreloadRequest::create(initiatorFor(m_t agImpl), position, m_urlToLoad, predictedBaseURL, resourceType(), m_mediaAttribu te); 155 OwnPtr<PreloadRequest> request = PreloadRequest::create(initiatorFor(m_t agImpl), position, m_urlToLoad, predictedBaseURL, resourceType());
144 if (isCORSEnabled()) 156 if (isCORSEnabled())
145 request->setCrossOriginEnabled(allowStoredCredentials()); 157 request->setCrossOriginEnabled(allowStoredCredentials());
146 request->setCharset(charset()); 158 request->setCharset(charset());
147 return request.release(); 159 return request.release();
148 } 160 }
149 161
150 private: 162 private:
163
abarth-chromium 2014/03/24 19:43:31 This change looks spurious
151 template<typename NameType> 164 template<typename NameType>
152 void processAttribute(const NameType& attributeName, const String& attribute Value) 165 void processAttribute(const NameType& attributeName, const String& attribute Value)
153 { 166 {
154 if (match(attributeName, charsetAttr)) 167 if (match(attributeName, charsetAttr))
155 m_charset = attributeValue; 168 m_charset = attributeValue;
156 169
157 if (match(m_tagImpl, scriptTag)) { 170 if (match(m_tagImpl, scriptTag)) {
158 if (match(attributeName, srcAttr)) 171 if (match(attributeName, srcAttr))
159 setUrlToLoad(attributeValue, DisallowURLReplacement); 172 setUrlToLoad(attributeValue, DisallowURLReplacement);
160 else if (match(attributeName, crossoriginAttr)) 173 else if (match(attributeName, crossoriginAttr))
161 setCrossOriginAllowed(attributeValue); 174 setCrossOriginAllowed(attributeValue);
162 } else if (match(m_tagImpl, imgTag)) { 175 } else if (match(m_tagImpl, imgTag)) {
163 if (match(attributeName, srcAttr) && !m_encounteredImgSrc) { 176 if (match(attributeName, srcAttr) && !m_encounteredImgSrc) {
164 m_encounteredImgSrc = true; 177 m_encounteredImgSrc = true;
165 setUrlToLoad(bestFitSourceForImageAttributes(m_deviceScaleFactor , attributeValue, m_srcsetImageCandidate), AllowURLReplacement); 178 setUrlToLoad(bestFitSourceForImageAttributes(m_deviceScaleFactor , attributeValue, m_srcsetImageCandidate), AllowURLReplacement);
166 } else if (match(attributeName, crossoriginAttr)) { 179 } else if (match(attributeName, crossoriginAttr)) {
167 setCrossOriginAllowed(attributeValue); 180 setCrossOriginAllowed(attributeValue);
168 } else if (RuntimeEnabledFeatures::srcsetEnabled() 181 } else if (RuntimeEnabledFeatures::srcsetEnabled()
169 && match(attributeName, srcsetAttr) 182 && match(attributeName, srcsetAttr)
170 && m_srcsetImageCandidate.isEmpty()) { 183 && m_srcsetImageCandidate.isEmpty()) {
171 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_devic eScaleFactor, attributeValue); 184 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_devic eScaleFactor, attributeValue);
172 setUrlToLoad(bestFitSourceForImageAttributes(m_deviceScaleFactor , m_urlToLoad, m_srcsetImageCandidate), AllowURLReplacement); 185 setUrlToLoad(bestFitSourceForImageAttributes(m_deviceScaleFactor , m_urlToLoad, m_srcsetImageCandidate), AllowURLReplacement);
173 } 186 }
174 } else if (match(m_tagImpl, linkTag)) { 187 } else if (match(m_tagImpl, linkTag)) {
175 if (match(attributeName, hrefAttr)) 188 if (match(attributeName, hrefAttr))
176 setUrlToLoad(attributeValue, DisallowURLReplacement); 189 setUrlToLoad(attributeValue, DisallowURLReplacement);
177 else if (match(attributeName, relAttr)) 190 else if (match(attributeName, relAttr))
178 m_linkIsStyleSheet = relAttributeIsStyleSheet(attributeValue); 191 m_linkIsStyleSheet = relAttributeIsStyleSheet(attributeValue);
179 else if (match(attributeName, mediaAttr)) 192 else if (match(attributeName, mediaAttr))
180 m_mediaAttribute = attributeValue; 193 m_mediaMatch = mediaAttributeMatches(m_mediaValues.get(), attrib uteValue);
181 else if (match(attributeName, crossoriginAttr)) 194 else if (match(attributeName, crossoriginAttr))
182 setCrossOriginAllowed(attributeValue); 195 setCrossOriginAllowed(attributeValue);
183 } else if (match(m_tagImpl, inputTag)) { 196 } else if (match(m_tagImpl, inputTag)) {
184 if (match(attributeName, srcAttr)) 197 if (match(attributeName, srcAttr))
185 setUrlToLoad(attributeValue, DisallowURLReplacement); 198 setUrlToLoad(attributeValue, DisallowURLReplacement);
186 else if (match(attributeName, typeAttr)) 199 else if (match(attributeName, typeAttr))
187 m_inputIsImage = equalIgnoringCase(attributeValue, InputTypeName s::image); 200 m_inputIsImage = equalIgnoringCase(attributeValue, InputTypeName s::image);
188 } 201 }
189 } 202 }
190 203
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 m_allowCredentials = AllowStoredCredentials; 267 m_allowCredentials = AllowStoredCredentials;
255 else 268 else
256 m_allowCredentials = DoNotAllowStoredCredentials; 269 m_allowCredentials = DoNotAllowStoredCredentials;
257 } 270 }
258 271
259 const StringImpl* m_tagImpl; 272 const StringImpl* m_tagImpl;
260 String m_urlToLoad; 273 String m_urlToLoad;
261 ImageCandidate m_srcsetImageCandidate; 274 ImageCandidate m_srcsetImageCandidate;
262 String m_charset; 275 String m_charset;
263 bool m_linkIsStyleSheet; 276 bool m_linkIsStyleSheet;
264 String m_mediaAttribute; 277 bool m_mediaMatch;
265 bool m_inputIsImage; 278 bool m_inputIsImage;
266 float m_deviceScaleFactor; 279 float m_deviceScaleFactor;
267 bool m_encounteredImgSrc; 280 bool m_encounteredImgSrc;
268 bool m_isCORSEnabled; 281 bool m_isCORSEnabled;
269 StoredCredentials m_allowCredentials; 282 StoredCredentials m_allowCredentials;
283 RefPtr<MediaValues> m_mediaValues;
270 }; 284 };
271 285
272 TokenPreloadScanner::TokenPreloadScanner(const KURL& documentURL, float deviceSc aleFactor) 286 TokenPreloadScanner::TokenPreloadScanner(const KURL& documentURL, float deviceSc aleFactor, PassRefPtr<MediaValues> mediaValues)
273 : m_documentURL(documentURL) 287 : m_documentURL(documentURL)
274 , m_inStyle(false) 288 , m_inStyle(false)
275 , m_deviceScaleFactor(deviceScaleFactor) 289 , m_deviceScaleFactor(deviceScaleFactor)
276 , m_templateCount(0) 290 , m_templateCount(0)
291 , m_mediaValues(mediaValues)
277 { 292 {
278 } 293 }
279 294
280 TokenPreloadScanner::~TokenPreloadScanner() 295 TokenPreloadScanner::~TokenPreloadScanner()
281 { 296 {
282 } 297 }
283 298
284 TokenPreloadScannerCheckpoint TokenPreloadScanner::createCheckpoint() 299 TokenPreloadScannerCheckpoint TokenPreloadScanner::createCheckpoint()
285 { 300 {
286 TokenPreloadScannerCheckpoint checkpoint = m_checkpoints.size(); 301 TokenPreloadScannerCheckpoint checkpoint = m_checkpoints.size();
287 m_checkpoints.append(Checkpoint(m_predictedBaseElementURL, m_inStyle, m_temp lateCount)); 302 m_checkpoints.append(Checkpoint(m_predictedBaseElementURL, m_inStyle, m_temp lateCount));
288 return checkpoint; 303 return checkpoint;
289 } 304 }
290 305
306
abarth-chromium 2014/03/24 19:43:31 ditto
291 void TokenPreloadScanner::rewindTo(TokenPreloadScannerCheckpoint checkpointIndex ) 307 void TokenPreloadScanner::rewindTo(TokenPreloadScannerCheckpoint checkpointIndex )
292 { 308 {
293 ASSERT(checkpointIndex < m_checkpoints.size()); // If this ASSERT fires, che ckpointIndex is invalid. 309 ASSERT(checkpointIndex < m_checkpoints.size()); // If this ASSERT fires, che ckpointIndex is invalid.
294 const Checkpoint& checkpoint = m_checkpoints[checkpointIndex]; 310 const Checkpoint& checkpoint = m_checkpoints[checkpointIndex];
295 m_predictedBaseElementURL = checkpoint.predictedBaseElementURL; 311 m_predictedBaseElementURL = checkpoint.predictedBaseElementURL;
296 m_inStyle = checkpoint.inStyle; 312 m_inStyle = checkpoint.inStyle;
297 m_templateCount = checkpoint.templateCount; 313 m_templateCount = checkpoint.templateCount;
298 m_cssScanner.reset(); 314 m_cssScanner.reset();
299 m_checkpoints.clear(); 315 m_checkpoints.clear();
300 } 316 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 return; 362 return;
347 } 363 }
348 if (match(tagImpl, baseTag)) { 364 if (match(tagImpl, baseTag)) {
349 // The first <base> element is the one that wins. 365 // The first <base> element is the one that wins.
350 if (!m_predictedBaseElementURL.isEmpty()) 366 if (!m_predictedBaseElementURL.isEmpty())
351 return; 367 return;
352 updatePredictedBaseURL(token); 368 updatePredictedBaseURL(token);
353 return; 369 return;
354 } 370 }
355 371
356 StartTagScanner scanner(tagImpl, m_deviceScaleFactor); 372 StartTagScanner scanner(tagImpl, m_deviceScaleFactor, m_mediaValues);
357 scanner.processAttributes(token.attributes()); 373 scanner.processAttributes(token.attributes());
358 OwnPtr<PreloadRequest> request = scanner.createPreloadRequest(m_predicte dBaseElementURL, source); 374 OwnPtr<PreloadRequest> request = scanner.createPreloadRequest(m_predicte dBaseElementURL, source);
359 if (request) 375 if (request)
360 requests.append(request.release()); 376 requests.append(request.release());
361 return; 377 return;
362 } 378 }
363 default: { 379 default: {
364 return; 380 return;
365 } 381 }
366 } 382 }
367 } 383 }
368 384
369 template<typename Token> 385 template<typename Token>
370 void TokenPreloadScanner::updatePredictedBaseURL(const Token& token) 386 void TokenPreloadScanner::updatePredictedBaseURL(const Token& token)
371 { 387 {
372 ASSERT(m_predictedBaseElementURL.isEmpty()); 388 ASSERT(m_predictedBaseElementURL.isEmpty());
373 if (const typename Token::Attribute* hrefAttribute = token.getAttributeItem( hrefAttr)) 389 if (const typename Token::Attribute* hrefAttribute = token.getAttributeItem( hrefAttr))
374 m_predictedBaseElementURL = KURL(m_documentURL, stripLeadingAndTrailingH TMLSpaces(hrefAttribute->value)).copy(); 390 m_predictedBaseElementURL = KURL(m_documentURL, stripLeadingAndTrailingH TMLSpaces(hrefAttribute->value)).copy();
375 } 391 }
376 392
377 HTMLPreloadScanner::HTMLPreloadScanner(const HTMLParserOptions& options, const K URL& documentURL, float deviceScaleFactor) 393 HTMLPreloadScanner::HTMLPreloadScanner(const HTMLParserOptions& options, const K URL& documentURL, float deviceScaleFactor, PassRefPtr<MediaValues> mediaValues)
378 : m_scanner(documentURL, deviceScaleFactor) 394 : m_scanner(documentURL, deviceScaleFactor, mediaValues)
379 , m_tokenizer(HTMLTokenizer::create(options)) 395 , m_tokenizer(HTMLTokenizer::create(options))
380 { 396 {
381 } 397 }
382 398
383 HTMLPreloadScanner::~HTMLPreloadScanner() 399 HTMLPreloadScanner::~HTMLPreloadScanner()
384 { 400 {
385 } 401 }
386 402
387 void HTMLPreloadScanner::appendToEnd(const SegmentedString& source) 403 void HTMLPreloadScanner::appendToEnd(const SegmentedString& source)
388 { 404 {
(...skipping 14 matching lines...) Expand all
403 if (m_token.type() == HTMLToken::StartTag) 419 if (m_token.type() == HTMLToken::StartTag)
404 m_tokenizer->updateStateFor(attemptStaticStringCreation(m_token.name (), Likely8Bit)); 420 m_tokenizer->updateStateFor(attemptStaticStringCreation(m_token.name (), Likely8Bit));
405 m_scanner.scan(m_token, m_source, requests); 421 m_scanner.scan(m_token, m_source, requests);
406 m_token.clear(); 422 m_token.clear();
407 } 423 }
408 424
409 preloader->takeAndPreload(requests); 425 preloader->takeAndPreload(requests);
410 } 426 }
411 427
412 } 428 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698