Index: third_party/WebKit/Source/core/loader/LinkLoader.cpp |
diff --git a/third_party/WebKit/Source/core/loader/LinkLoader.cpp b/third_party/WebKit/Source/core/loader/LinkLoader.cpp |
index 6222b9e52dee076954592c3b538f3ecfefde7b0a..3489d87ace9a2aff0afb73b8eed0b885622d31ee 100644 |
--- a/third_party/WebKit/Source/core/loader/LinkLoader.cpp |
+++ b/third_party/WebKit/Source/core/loader/LinkLoader.cpp |
@@ -31,6 +31,8 @@ |
#include "core/loader/LinkLoader.h" |
+#include "core/css/MediaList.h" |
+#include "core/css/MediaQueryEvaluator.h" |
#include "core/dom/Document.h" |
#include "core/fetch/FetchInitiatorTypeNames.h" |
#include "core/fetch/FetchRequest.h" |
@@ -40,6 +42,7 @@ |
#include "core/frame/UseCounter.h" |
#include "core/html/CrossOriginAttribute.h" |
#include "core/html/LinkRelAttribute.h" |
+#include "core/html/parser/HTMLPreloadScanner.h" |
#include "core/inspector/ConsoleMessage.h" |
#include "core/loader/DocumentLoader.h" |
#include "core/loader/LinkHeader.h" |
@@ -247,7 +250,7 @@ static bool isSupportedType(Resource::Type resourceType, const String& mimeType) |
} |
static Resource* preloadIfNeeded(const LinkRelAttribute& relAttribute, const KURL& href, Document& document, const String& as, const String& mimeType, |
- CrossOriginAttributeValue crossOrigin, LinkCaller caller, bool& errorOccurred) |
+ const String& media, CrossOriginAttributeValue crossOrigin, LinkCaller caller, bool& errorOccurred, ViewportDescription* viewportDescription) |
{ |
if (!document.loader() || !relAttribute.isLinkPreload()) |
return nullptr; |
@@ -259,6 +262,17 @@ static Resource* preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR |
return nullptr; |
} |
+ if (!media.isEmpty()) { |
+ RefPtrWillBeRawPtr<MediaValues> mediaValues = MediaValues::createDynamicIfFrameExists(document.frame()); |
+ if (viewportDescription) |
+ mediaValues->overrideViewportDimensions(viewportDescription->maxWidth.getFloatValue(), viewportDescription->maxHeight.getFloatValue()); |
+ |
+ // Preload only if media matches |
+ RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(media); |
+ MediaQueryEvaluator evaluator(*mediaValues); |
+ if (!evaluator.eval(mediaQueries.get())) |
+ return nullptr; |
+ } |
if (caller == LinkCalledFromHeader) |
UseCounter::count(document, UseCounter::LinkHeaderPreload); |
Resource::Type resourceType; |
@@ -287,9 +301,10 @@ static Resource* preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR |
return document.loader()->startPreload(resourceType, linkRequest); |
} |
-void LinkLoader::loadLinksFromHeader(const String& headerValue, const KURL& baseURL, Document* document, const NetworkHintsInterface& networkHintsInterface, CanLoadResources canLoadResources) |
+void LinkLoader::loadLinksFromHeader(const String& headerValue, const KURL& baseURL, Document* document, const NetworkHintsInterface& networkHintsInterface, |
+ CanLoadResources canLoadResources, ViewportDescriptionWrapper* viewportDescriptionWrapper) |
{ |
- if (!document) |
+ if (!document || headerValue.isEmpty()) |
return; |
LinkHeaderSet headerSet(headerValue); |
for (auto& header : headerSet) { |
@@ -307,14 +322,17 @@ void LinkLoader::loadLinksFromHeader(const String& headerValue, const KURL& base |
} |
if (canLoadResources != DoNotLoadResources) { |
bool errorOccurred = false; |
- if (RuntimeEnabledFeatures::linkPreloadEnabled()) |
- preloadIfNeeded(relAttribute, url, *document, header.as(), header.mimeType(), header.crossOrigin(), LinkCalledFromHeader, errorOccurred); |
+ if (RuntimeEnabledFeatures::linkPreloadEnabled()) { |
+ ViewportDescription* viewportDescription = (viewportDescriptionWrapper && viewportDescriptionWrapper->set) ? &(viewportDescriptionWrapper->description) : nullptr; |
+ preloadIfNeeded(relAttribute, url, *document, header.as(), header.mimeType(), header.media(), header.crossOrigin(), LinkCalledFromHeader, errorOccurred, viewportDescription); |
+ } |
} |
// TODO(yoav): Add more supported headers as needed. |
} |
} |
-bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttributeValue crossOrigin, const String& type, const String& as, const KURL& href, Document& document, const NetworkHintsInterface& networkHintsInterface) |
+bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttributeValue crossOrigin, const String& type, |
+ const String& as, const String& media, const KURL& href, Document& document, const NetworkHintsInterface& networkHintsInterface) |
{ |
// TODO(yoav): Do all links need to load only after they're in document??? |
@@ -326,7 +344,7 @@ bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, CrossOriginAttri |
bool errorOccurred = false; |
if (m_client->shouldLoadLink()) |
- createLinkPreloadResourceClient(preloadIfNeeded(relAttribute, href, document, as, type, crossOrigin, LinkCalledFromMarkup, errorOccurred)); |
+ createLinkPreloadResourceClient(preloadIfNeeded(relAttribute, href, document, as, type, media, crossOrigin, LinkCalledFromMarkup, errorOccurred, nullptr)); |
if (errorOccurred) |
m_linkLoadingErrorTimer.startOneShot(0, BLINK_FROM_HERE); |