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

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

Issue 1291613010: Implement referrerpolicy attribute for img elements (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: yoav comments Created 5 years, 4 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 , m_linkIsPreconnect(false) 116 , m_linkIsPreconnect(false)
117 , m_linkIsImport(false) 117 , m_linkIsImport(false)
118 , m_matchedMediaAttribute(true) 118 , m_matchedMediaAttribute(true)
119 , m_inputIsImage(false) 119 , m_inputIsImage(false)
120 , m_sourceSize(0) 120 , m_sourceSize(0)
121 , m_sourceSizeSet(false) 121 , m_sourceSizeSet(false)
122 , m_isCORSEnabled(false) 122 , m_isCORSEnabled(false)
123 , m_defer(FetchRequest::NoDefer) 123 , m_defer(FetchRequest::NoDefer)
124 , m_allowCredentials(DoNotAllowStoredCredentials) 124 , m_allowCredentials(DoNotAllowStoredCredentials)
125 , m_mediaValues(mediaValues) 125 , m_mediaValues(mediaValues)
126 , m_referrerPolicy(ReferrerPolicyDefault)
126 { 127 {
127 ASSERT(m_mediaValues->isCached()); 128 ASSERT(m_mediaValues->isCached());
128 if (match(m_tagImpl, imgTag) 129 if (match(m_tagImpl, imgTag)
129 || match(m_tagImpl, sourceTag)) { 130 || match(m_tagImpl, sourceTag)) {
130 m_sourceSize = SizesAttributeParser(m_mediaValues, String()).length( ); 131 m_sourceSize = SizesAttributeParser(m_mediaValues, String()).length( );
131 return; 132 return;
132 } 133 }
133 if ( !match(m_tagImpl, inputTag) 134 if ( !match(m_tagImpl, inputTag)
134 && !match(m_tagImpl, linkTag) 135 && !match(m_tagImpl, linkTag)
135 && !match(m_tagImpl, scriptTag) 136 && !match(m_tagImpl, scriptTag)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 if (match(m_tagImpl, sourceTag) && m_matchedMediaAttribute && pictureDat a.sourceURL.isEmpty()) { 168 if (match(m_tagImpl, sourceTag) && m_matchedMediaAttribute && pictureDat a.sourceURL.isEmpty()) {
168 pictureData.sourceURL = m_srcsetImageCandidate.toString(); 169 pictureData.sourceURL = m_srcsetImageCandidate.toString();
169 pictureData.sourceSizeSet = m_sourceSizeSet; 170 pictureData.sourceSizeSet = m_sourceSizeSet;
170 pictureData.sourceSize = m_sourceSize; 171 pictureData.sourceSize = m_sourceSize;
171 pictureData.picked = true; 172 pictureData.picked = true;
172 } else if (match(m_tagImpl, imgTag) && !pictureData.sourceURL.isEmpty()) { 173 } else if (match(m_tagImpl, imgTag) && !pictureData.sourceURL.isEmpty()) {
173 setUrlToLoad(pictureData.sourceURL, AllowURLReplacement); 174 setUrlToLoad(pictureData.sourceURL, AllowURLReplacement);
174 } 175 }
175 } 176 }
176 177
177 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source, const ClientHintsPreferences& clientHintsPrefer ences, const PictureData& pictureData, const ReferrerPolicy referrerPolicy) 178 PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL , const SegmentedString& source, const ClientHintsPreferences& clientHintsPrefer ences, const PictureData& pictureData, const ReferrerPolicy documentReferrerPoli cy)
178 { 179 {
179 PreloadRequest::RequestType requestType = PreloadRequest::RequestTypePre load; 180 PreloadRequest::RequestType requestType = PreloadRequest::RequestTypePre load;
180 if (shouldPreconnect()) 181 if (shouldPreconnect())
181 requestType = PreloadRequest::RequestTypePreconnect; 182 requestType = PreloadRequest::RequestTypePreconnect;
182 else if (!shouldPreload() || !m_matchedMediaAttribute) 183 else if (!shouldPreload() || !m_matchedMediaAttribute)
183 return nullptr; 184 return nullptr;
184 185
185 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn()); 186 TextPosition position = TextPosition(source.currentLine(), source.curren tColumn());
186 FetchRequest::ResourceWidth resourceWidth; 187 FetchRequest::ResourceWidth resourceWidth;
187 float sourceSize = m_sourceSize; 188 float sourceSize = m_sourceSize;
188 bool sourceSizeSet = m_sourceSizeSet; 189 bool sourceSizeSet = m_sourceSizeSet;
189 if (pictureData.picked) { 190 if (pictureData.picked) {
190 sourceSizeSet = pictureData.sourceSizeSet; 191 sourceSizeSet = pictureData.sourceSizeSet;
191 sourceSize = pictureData.sourceSize; 192 sourceSize = pictureData.sourceSize;
192 } 193 }
193 if (sourceSizeSet) { 194 if (sourceSizeSet) {
194 resourceWidth.width = sourceSize; 195 resourceWidth.width = sourceSize;
195 resourceWidth.isSet = true; 196 resourceWidth.isSet = true;
196 } 197 }
197 198
199 // The element's 'referrerpolicy' attribute (if present) takes
200 // precedence over the document's referrer policy.
201 ReferrerPolicy referrerPolicy = m_referrerPolicy != ReferrerPolicyDefaul t ? m_referrerPolicy : documentReferrerPolicy;
198 OwnPtr<PreloadRequest> request = PreloadRequest::create(initiatorFor(m_t agImpl), position, m_urlToLoad, predictedBaseURL, resourceType(), referrerPolicy , resourceWidth, clientHintsPreferences, requestType); 202 OwnPtr<PreloadRequest> request = PreloadRequest::create(initiatorFor(m_t agImpl), position, m_urlToLoad, predictedBaseURL, resourceType(), referrerPolicy , resourceWidth, clientHintsPreferences, requestType);
199 if (isCORSEnabled()) 203 if (isCORSEnabled())
200 request->setCrossOriginEnabled(allowStoredCredentials()); 204 request->setCrossOriginEnabled(allowStoredCredentials());
201 request->setCharset(charset()); 205 request->setCharset(charset());
202 request->setDefer(m_defer); 206 request->setDefer(m_defer);
203 return request.release(); 207 return request.release();
204 } 208 }
205 209
206 private: 210 private:
207 template<typename NameType> 211 template<typename NameType>
(...skipping 22 matching lines...) Expand all
230 m_srcsetAttributeValue = attributeValue; 234 m_srcsetAttributeValue = attributeValue;
231 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_mediaValu es->devicePixelRatio(), m_sourceSize, attributeValue); 235 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_mediaValu es->devicePixelRatio(), m_sourceSize, attributeValue);
232 setUrlToLoad(bestFitSourceForImageAttributes(m_mediaValues->devicePi xelRatio(), m_sourceSize, m_imgSrcUrl, m_srcsetImageCandidate), AllowURLReplacem ent); 236 setUrlToLoad(bestFitSourceForImageAttributes(m_mediaValues->devicePi xelRatio(), m_sourceSize, m_imgSrcUrl, m_srcsetImageCandidate), AllowURLReplacem ent);
233 } else if (match(attributeName, sizesAttr) && !m_sourceSizeSet) { 237 } else if (match(attributeName, sizesAttr) && !m_sourceSizeSet) {
234 m_sourceSize = SizesAttributeParser(m_mediaValues, attributeValue).l ength(); 238 m_sourceSize = SizesAttributeParser(m_mediaValues, attributeValue).l ength();
235 m_sourceSizeSet = true; 239 m_sourceSizeSet = true;
236 if (!m_srcsetImageCandidate.isEmpty()) { 240 if (!m_srcsetImageCandidate.isEmpty()) {
237 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_media Values->devicePixelRatio(), m_sourceSize, m_srcsetAttributeValue); 241 m_srcsetImageCandidate = bestFitSourceForSrcsetAttribute(m_media Values->devicePixelRatio(), m_sourceSize, m_srcsetAttributeValue);
238 setUrlToLoad(bestFitSourceForImageAttributes(m_mediaValues->devi cePixelRatio(), m_sourceSize, m_imgSrcUrl, m_srcsetImageCandidate), AllowURLRepl acement); 242 setUrlToLoad(bestFitSourceForImageAttributes(m_mediaValues->devi cePixelRatio(), m_sourceSize, m_imgSrcUrl, m_srcsetImageCandidate), AllowURLRepl acement);
239 } 243 }
244 } else if (match(attributeName, referrerpolicyAttr) && !attributeValue.i sNull()) {
245 ReferrerPolicy parsedPolicy;
246 if (SecurityPolicy::referrerPolicyFromString(attributeValue, &parsed Policy)) {
247 m_referrerPolicy = parsedPolicy;
248 } else {
249 m_referrerPolicy = ReferrerPolicyDefault;
250 }
Yoav Weiss 2015/08/18 21:09:21 I believe we can replace the last 6 lines with `Se
estark 2015/08/19 02:53:22 Done.
240 } 251 }
241 } 252 }
242 253
243 template<typename NameType> 254 template<typename NameType>
244 void processLinkAttribute(const NameType& attributeName, const String& attri buteValue) 255 void processLinkAttribute(const NameType& attributeName, const String& attri buteValue)
245 { 256 {
246 // FIXME - Don't set rel/media/crossorigin multiple times. 257 // FIXME - Don't set rel/media/crossorigin multiple times.
247 if (match(attributeName, hrefAttr)) { 258 if (match(attributeName, hrefAttr)) {
248 setUrlToLoad(attributeValue, DisallowURLReplacement); 259 setUrlToLoad(attributeValue, DisallowURLReplacement);
249 } else if (match(attributeName, relAttr)) { 260 } else if (match(attributeName, relAttr)) {
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 bool m_matchedMediaAttribute; 415 bool m_matchedMediaAttribute;
405 bool m_inputIsImage; 416 bool m_inputIsImage;
406 String m_imgSrcUrl; 417 String m_imgSrcUrl;
407 String m_srcsetAttributeValue; 418 String m_srcsetAttributeValue;
408 float m_sourceSize; 419 float m_sourceSize;
409 bool m_sourceSizeSet; 420 bool m_sourceSizeSet;
410 bool m_isCORSEnabled; 421 bool m_isCORSEnabled;
411 FetchRequest::DeferOption m_defer; 422 FetchRequest::DeferOption m_defer;
412 StoredCredentials m_allowCredentials; 423 StoredCredentials m_allowCredentials;
413 RefPtrWillBeMember<MediaValues> m_mediaValues; 424 RefPtrWillBeMember<MediaValues> m_mediaValues;
425 ReferrerPolicy m_referrerPolicy;
414 }; 426 };
415 427
416 TokenPreloadScanner::TokenPreloadScanner(const KURL& documentURL, PassOwnPtr<Cac hedDocumentParameters> documentParameters) 428 TokenPreloadScanner::TokenPreloadScanner(const KURL& documentURL, PassOwnPtr<Cac hedDocumentParameters> documentParameters)
417 : m_documentURL(documentURL) 429 : m_documentURL(documentURL)
418 , m_inStyle(false) 430 , m_inStyle(false)
419 , m_inPicture(false) 431 , m_inPicture(false)
420 , m_isAppCacheEnabled(false) 432 , m_isAppCacheEnabled(false)
421 , m_isCSPEnabled(false) 433 , m_isCSPEnabled(false)
422 , m_templateCount(0) 434 , m_templateCount(0)
423 , m_documentParameters(documentParameters) 435 , m_documentParameters(documentParameters)
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 if (match(tagImpl, pictureTag)) { 598 if (match(tagImpl, pictureTag)) {
587 m_inPicture = true; 599 m_inPicture = true;
588 m_pictureData = PictureData(); 600 m_pictureData = PictureData();
589 return; 601 return;
590 } 602 }
591 603
592 StartTagScanner scanner(tagImpl, m_documentParameters->mediaValues); 604 StartTagScanner scanner(tagImpl, m_documentParameters->mediaValues);
593 scanner.processAttributes(token.attributes()); 605 scanner.processAttributes(token.attributes());
594 if (m_inPicture) 606 if (m_inPicture)
595 scanner.handlePictureSourceURL(m_pictureData); 607 scanner.handlePictureSourceURL(m_pictureData);
608
Yoav Weiss 2015/08/18 21:09:21 Nit: spurious new line
estark 2015/08/19 02:53:22 Done.
596 OwnPtr<PreloadRequest> request = scanner.createPreloadRequest(m_predicte dBaseElementURL, source, m_clientHintsPreferences, m_pictureData, m_documentPara meters->referrerPolicy); 609 OwnPtr<PreloadRequest> request = scanner.createPreloadRequest(m_predicte dBaseElementURL, source, m_clientHintsPreferences, m_pictureData, m_documentPara meters->referrerPolicy);
597 if (request) 610 if (request)
598 requests.append(request.release()); 611 requests.append(request.release());
599 return; 612 return;
600 } 613 }
601 default: { 614 default: {
602 return; 615 return;
603 } 616 }
604 } 617 }
605 } 618 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 else 674 else
662 mediaValues = MediaValuesCached::create(*document); 675 mediaValues = MediaValuesCached::create(*document);
663 ASSERT(mediaValues->isSafeToSendToAnotherThread()); 676 ASSERT(mediaValues->isSafeToSendToAnotherThread());
664 defaultViewportMinWidth = document->viewportDefaultMinWidth(); 677 defaultViewportMinWidth = document->viewportDefaultMinWidth();
665 viewportMetaZeroValuesQuirk = document->settings() && document->settings()-> viewportMetaZeroValuesQuirk(); 678 viewportMetaZeroValuesQuirk = document->settings() && document->settings()-> viewportMetaZeroValuesQuirk();
666 viewportMetaEnabled = document->settings() && document->settings()->viewport MetaEnabled(); 679 viewportMetaEnabled = document->settings() && document->settings()->viewport MetaEnabled();
667 referrerPolicy = ReferrerPolicyDefault; 680 referrerPolicy = ReferrerPolicyDefault;
668 } 681 }
669 682
670 } 683 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698