| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 * | 29 * |
| 30 */ | 30 */ |
| 31 | 31 |
| 32 #include "core/loader/LinkLoader.h" | 32 #include "core/loader/LinkLoader.h" |
| 33 | 33 |
| 34 #include "core/css/MediaList.h" |
| 35 #include "core/css/MediaQueryEvaluator.h" |
| 34 #include "core/dom/Document.h" | 36 #include "core/dom/Document.h" |
| 35 #include "core/fetch/FetchInitiatorTypeNames.h" | 37 #include "core/fetch/FetchInitiatorTypeNames.h" |
| 36 #include "core/fetch/FetchRequest.h" | 38 #include "core/fetch/FetchRequest.h" |
| 37 #include "core/fetch/LinkFetchResource.h" | 39 #include "core/fetch/LinkFetchResource.h" |
| 38 #include "core/fetch/ResourceFetcher.h" | 40 #include "core/fetch/ResourceFetcher.h" |
| 39 #include "core/frame/Settings.h" | 41 #include "core/frame/Settings.h" |
| 40 #include "core/frame/UseCounter.h" | 42 #include "core/frame/UseCounter.h" |
| 41 #include "core/html/CrossOriginAttribute.h" | 43 #include "core/html/CrossOriginAttribute.h" |
| 42 #include "core/html/LinkRelAttribute.h" | 44 #include "core/html/LinkRelAttribute.h" |
| 45 #include "core/html/parser/HTMLPreloadScanner.h" |
| 43 #include "core/inspector/ConsoleMessage.h" | 46 #include "core/inspector/ConsoleMessage.h" |
| 44 #include "core/loader/DocumentLoader.h" | 47 #include "core/loader/DocumentLoader.h" |
| 45 #include "core/loader/LinkHeader.h" | 48 #include "core/loader/LinkHeader.h" |
| 46 #include "core/loader/NetworkHintsInterface.h" | 49 #include "core/loader/NetworkHintsInterface.h" |
| 47 #include "core/loader/PrerenderHandle.h" | 50 #include "core/loader/PrerenderHandle.h" |
| 48 #include "platform/MIMETypeRegistry.h" | 51 #include "platform/MIMETypeRegistry.h" |
| 49 #include "platform/Prerender.h" | 52 #include "platform/Prerender.h" |
| 50 #include "platform/RuntimeEnabledFeatures.h" | 53 #include "platform/RuntimeEnabledFeatures.h" |
| 51 #include "platform/network/NetworkHints.h" | 54 #include "platform/network/NetworkHints.h" |
| 52 #include "public/platform/WebPrerender.h" | 55 #include "public/platform/WebPrerender.h" |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 case Resource::Raw: | 242 case Resource::Raw: |
| 240 case Resource::LinkPreload: | 243 case Resource::LinkPreload: |
| 241 return true; | 244 return true; |
| 242 default: | 245 default: |
| 243 ASSERT_NOT_REACHED(); | 246 ASSERT_NOT_REACHED(); |
| 244 } | 247 } |
| 245 return false; | 248 return false; |
| 246 } | 249 } |
| 247 | 250 |
| 248 static Resource* preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR
L& href, Document& document, const String& as, const String& mimeType, | 251 static Resource* preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR
L& href, Document& document, const String& as, const String& mimeType, |
| 249 CrossOriginAttributeValue crossOrigin, LinkCaller caller, bool& errorOccurre
d) | 252 const String& media, CrossOriginAttributeValue crossOrigin, LinkCaller calle
r, bool& errorOccurred, ViewportDescription* viewportDescription) |
| 250 { | 253 { |
| 251 if (!document.loader() || !relAttribute.isLinkPreload()) | 254 if (!document.loader() || !relAttribute.isLinkPreload()) |
| 252 return nullptr; | 255 return nullptr; |
| 253 | 256 |
| 254 UseCounter::count(document, UseCounter::LinkRelPreload); | 257 UseCounter::count(document, UseCounter::LinkRelPreload); |
| 255 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); | 258 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); |
| 256 if (!href.isValid() || href.isEmpty()) { | 259 if (!href.isValid() || href.isEmpty()) { |
| 257 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa
rningMessageLevel, String("<link rel=preload> has an invalid `href` value"))); | 260 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa
rningMessageLevel, String("<link rel=preload> has an invalid `href` value"))); |
| 258 return nullptr; | 261 return nullptr; |
| 259 } | 262 } |
| 260 | 263 |
| 264 if (!media.isEmpty()) { |
| 265 RefPtrWillBeRawPtr<MediaValues> mediaValues = MediaValues::createDynamic
IfFrameExists(document.frame()); |
| 266 if (viewportDescription) |
| 267 mediaValues->overrideViewportDimensions(viewportDescription->maxWidt
h.getFloatValue(), viewportDescription->maxHeight.getFloatValue()); |
| 268 |
| 269 // Preload only if media matches |
| 270 RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(m
edia); |
| 271 MediaQueryEvaluator evaluator(*mediaValues); |
| 272 if (!evaluator.eval(mediaQueries.get())) |
| 273 return nullptr; |
| 274 } |
| 261 if (caller == LinkCalledFromHeader) | 275 if (caller == LinkCalledFromHeader) |
| 262 UseCounter::count(document, UseCounter::LinkHeaderPreload); | 276 UseCounter::count(document, UseCounter::LinkHeaderPreload); |
| 263 Resource::Type resourceType; | 277 Resource::Type resourceType; |
| 264 if (!LinkLoader::getResourceTypeFromAsAttribute(as, resourceType)) { | 278 if (!LinkLoader::getResourceTypeFromAsAttribute(as, resourceType)) { |
| 265 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa
rningMessageLevel, String("<link rel=preload> must have a valid `as` value"))); | 279 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa
rningMessageLevel, String("<link rel=preload> must have a valid `as` value"))); |
| 266 errorOccurred = true; | 280 errorOccurred = true; |
| 267 return nullptr; | 281 return nullptr; |
| 268 } | 282 } |
| 269 | 283 |
| 270 if (!isSupportedType(resourceType, mimeType)) { | 284 if (!isSupportedType(resourceType, mimeType)) { |
| 271 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa
rningMessageLevel, String("<link rel=preload> has an unsupported `type` value"))
); | 285 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, Wa
rningMessageLevel, String("<link rel=preload> has an unsupported `type` value"))
); |
| 272 return nullptr; | 286 return nullptr; |
| 273 } | 287 } |
| 274 ResourceRequest resourceRequest(document.completeURL(href)); | 288 ResourceRequest resourceRequest(document.completeURL(href)); |
| 275 ResourceFetcher::determineRequestContext(resourceRequest, resourceType, fals
e); | 289 ResourceFetcher::determineRequestContext(resourceRequest, resourceType, fals
e); |
| 276 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link); | 290 FetchRequest linkRequest(resourceRequest, FetchInitiatorTypeNames::link); |
| 277 | 291 |
| 278 linkRequest.setPriority(document.fetcher()->loadPriority(resourceType, linkR
equest)); | 292 linkRequest.setPriority(document.fetcher()->loadPriority(resourceType, linkR
equest)); |
| 279 if (crossOrigin != CrossOriginAttributeNotSet) | 293 if (crossOrigin != CrossOriginAttributeNotSet) |
| 280 linkRequest.setCrossOriginAccessControl(document.getSecurityOrigin(), cr
ossOrigin); | 294 linkRequest.setCrossOriginAccessControl(document.getSecurityOrigin(), cr
ossOrigin); |
| 281 Settings* settings = document.settings(); | 295 Settings* settings = document.settings(); |
| 282 if (settings && settings->logPreload()) | 296 if (settings && settings->logPreload()) |
| 283 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, De
bugMessageLevel, String("Preload triggered for " + href.host() + href.path()))); | 297 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource, De
bugMessageLevel, String("Preload triggered for " + href.host() + href.path()))); |
| 284 linkRequest.setForPreload(true); | 298 linkRequest.setForPreload(true); |
| 285 linkRequest.setLinkPreload(true); | 299 linkRequest.setLinkPreload(true); |
| 286 return document.loader()->startPreload(resourceType, linkRequest); | 300 return document.loader()->startPreload(resourceType, linkRequest); |
| 287 } | 301 } |
| 288 | 302 |
| 289 bool LinkLoader::loadLinkFromHeader(const String& headerValue, const KURL& baseU
RL, Document* document, const NetworkHintsInterface& networkHintsInterface, CanL
oadResources canLoadResources) | 303 bool LinkLoader::loadLinkFromHeader(const String& headerValue, const KURL& baseU
RL, Document* document, const NetworkHintsInterface& networkHintsInterface, |
| 304 CanLoadResources canLoadResources, ViewportDescriptionWrapper* viewportDescr
iptionWrapper) |
| 290 { | 305 { |
| 291 if (!document) | 306 if (!document || headerValue.isEmpty()) |
| 292 return false; | 307 return false; |
| 293 LinkHeaderSet headerSet(headerValue); | 308 LinkHeaderSet headerSet(headerValue); |
| 294 for (auto& header : headerSet) { | 309 for (auto& header : headerSet) { |
| 295 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty()) | 310 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty()) |
| 296 return false; | 311 return false; |
| 297 | 312 |
| 298 LinkRelAttribute relAttribute(header.rel()); | 313 LinkRelAttribute relAttribute(header.rel()); |
| 299 KURL url(baseURL, header.url()); | 314 KURL url(baseURL, header.url()); |
| 300 if (canLoadResources != OnlyLoadResources) { | 315 if (canLoadResources != OnlyLoadResources) { |
| 301 if (RuntimeEnabledFeatures::linkHeaderEnabled()) | 316 if (RuntimeEnabledFeatures::linkHeaderEnabled()) |
| 302 dnsPrefetchIfNeeded(relAttribute, url, *document, networkHintsIn
terface, LinkCalledFromHeader); | 317 dnsPrefetchIfNeeded(relAttribute, url, *document, networkHintsIn
terface, LinkCalledFromHeader); |
| 303 | 318 |
| 304 if (RuntimeEnabledFeatures::linkPreconnectEnabled()) | 319 if (RuntimeEnabledFeatures::linkPreconnectEnabled()) |
| 305 preconnectIfNeeded(relAttribute, url, *document, header.crossOri
gin(), networkHintsInterface, LinkCalledFromHeader); | 320 preconnectIfNeeded(relAttribute, url, *document, header.crossOri
gin(), networkHintsInterface, LinkCalledFromHeader); |
| 306 } | 321 } |
| 307 if (canLoadResources != DoNotLoadResources) { | 322 if (canLoadResources != DoNotLoadResources) { |
| 308 bool errorOccurred = false; | 323 bool errorOccurred = false; |
| 309 if (RuntimeEnabledFeatures::linkPreloadEnabled()) | 324 if (RuntimeEnabledFeatures::linkPreloadEnabled()) { |
| 310 preloadIfNeeded(relAttribute, url, *document, header.as(), heade
r.mimeType(), header.crossOrigin(), LinkCalledFromHeader, errorOccurred); | 325 ViewportDescription* viewportDescription = (viewportDescriptionW
rapper && viewportDescriptionWrapper->set) ? &(viewportDescriptionWrapper->descr
iption) : nullptr; |
| 326 preloadIfNeeded(relAttribute, url, *document, header.as(), heade
r.mimeType(), header.media(), header.crossOrigin(), LinkCalledFromHeader, errorO
ccurred, viewportDescription); |
| 327 } |
| 311 } | 328 } |
| 312 // TODO(yoav): Add more supported headers as needed. | 329 // TODO(yoav): Add more supported headers as needed. |
| 313 } | 330 } |
| 314 return true; | 331 return true; |
| 315 } | 332 } |
| 316 | 333 |
| 317 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri
buteValue crossOrigin, const String& type, const String& as, const KURL& href, D
ocument& document, const NetworkHintsInterface& networkHintsInterface) | 334 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri
buteValue crossOrigin, const String& type, |
| 335 const String& as, const String& media, const KURL& href, Document& document,
const NetworkHintsInterface& networkHintsInterface) |
| 318 { | 336 { |
| 319 // TODO(yoav): Do all links need to load only after they're in document??? | 337 // TODO(yoav): Do all links need to load only after they're in document??? |
| 320 | 338 |
| 321 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt
tributeValue. crbug.com/486689 | 339 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt
tributeValue. crbug.com/486689 |
| 322 // FIXME(crbug.com/463266): We're ignoring type here, for everything but pre
load. Maybe we shouldn't. | 340 // FIXME(crbug.com/463266): We're ignoring type here, for everything but pre
load. Maybe we shouldn't. |
| 323 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin
kCalledFromMarkup); | 341 dnsPrefetchIfNeeded(relAttribute, href, document, networkHintsInterface, Lin
kCalledFromMarkup); |
| 324 | 342 |
| 325 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn
terface, LinkCalledFromMarkup); | 343 preconnectIfNeeded(relAttribute, href, document, crossOrigin, networkHintsIn
terface, LinkCalledFromMarkup); |
| 326 | 344 |
| 327 bool errorOccurred = false; | 345 bool errorOccurred = false; |
| 328 if (m_client->shouldLoadLink()) | 346 if (m_client->shouldLoadLink()) |
| 329 createLinkPreloadResourceClient(preloadIfNeeded(relAttribute, href, docu
ment, as, type, crossOrigin, LinkCalledFromMarkup, errorOccurred)); | 347 createLinkPreloadResourceClient(preloadIfNeeded(relAttribute, href, docu
ment, as, type, media, crossOrigin, LinkCalledFromMarkup, errorOccurred, nullptr
)); |
| 330 if (errorOccurred) | 348 if (errorOccurred) |
| 331 m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE); | 349 m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE); |
| 332 | 350 |
| 333 if (href.isEmpty() || !href.isValid()) | 351 if (href.isEmpty() || !href.isValid()) |
| 334 released(); | 352 released(); |
| 335 | 353 |
| 336 // FIXME(crbug.com/323096): Should take care of import. | 354 // FIXME(crbug.com/323096): Should take care of import. |
| 337 if (relAttribute.isLinkPrefetch() && href.isValid() && document.frame()) { | 355 if (relAttribute.isLinkPrefetch() && href.isValid() && document.frame()) { |
| 338 if (!m_client->shouldLoadLink()) | 356 if (!m_client->shouldLoadLink()) |
| 339 return false; | 357 return false; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 | 392 |
| 375 DEFINE_TRACE(LinkLoader) | 393 DEFINE_TRACE(LinkLoader) |
| 376 { | 394 { |
| 377 visitor->trace(m_client); | 395 visitor->trace(m_client); |
| 378 visitor->trace(m_prerender); | 396 visitor->trace(m_prerender); |
| 379 visitor->trace(m_linkPreloadResourceClient); | 397 visitor->trace(m_linkPreloadResourceClient); |
| 380 ResourceOwner<Resource, ResourceClient>::trace(visitor); | 398 ResourceOwner<Resource, ResourceClient>::trace(visitor); |
| 381 } | 399 } |
| 382 | 400 |
| 383 } // namespace blink | 401 } // namespace blink |
| OLD | NEW |