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

Unified Diff: third_party/WebKit/Source/core/html/parser/CSSPreloadScanner.cpp

Issue 1976463003: Preload scan external CSS for @import (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: preload @import Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/html/parser/CSSPreloadScanner.cpp
diff --git a/third_party/WebKit/Source/core/html/parser/CSSPreloadScanner.cpp b/third_party/WebKit/Source/core/html/parser/CSSPreloadScanner.cpp
index 0b8fb30bd64ccb2e3673a74f60f4b014c2742a74..1cdeb2137816a9d2d3bf6d7a49233fee3bd689bb 100644
--- a/third_party/WebKit/Source/core/html/parser/CSSPreloadScanner.cpp
+++ b/third_party/WebKit/Source/core/html/parser/CSSPreloadScanner.cpp
@@ -27,8 +27,14 @@
#include "core/html/parser/CSSPreloadScanner.h"
+#include "core/dom/Document.h"
+#include "core/fetch/CSSStyleSheetResource.h"
#include "core/fetch/FetchInitiatorTypeNames.h"
+#include "core/frame/Settings.h"
#include "core/html/parser/HTMLParserIdioms.h"
+#include "core/html/parser/HTMLResourcePreloader.h"
+#include "core/loader/DocumentLoader.h"
+#include "platform/Histogram.h"
#include "platform/text/SegmentedString.h"
#include <memory>
@@ -234,4 +240,68 @@ void CSSPreloadScanner::emitRule(const SegmentedString& source)
m_ruleValue.clear();
}
+CSSPreloaderResourceClient::CSSPreloaderResourceClient(Resource* resource, HTMLResourcePreloader* preloader)
+ : m_preloader(preloader)
+{
+ DCHECK(resource->getType() == Resource::Type::CSSStyleSheet);
+ setResource(static_cast<CSSStyleSheetResource*>(resource), Resource::DontMarkAsReferenced);
hiroshige 2016/07/26 09:57:10 We can use toCSSStyleSheetResource() instead of st
Charlie Harrison 2016/07/26 16:31:52 Done.
+}
+
+CSSPreloaderResourceClient::~CSSPreloaderResourceClient()
+{
+}
+
+void CSSPreloaderResourceClient::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CSSStyleSheetResource*)
+{
+ clearResource();
+}
+
+// Only attach for one appendData call, as that's where most imports will likely
+// be (according to spec).
+void CSSPreloaderResourceClient::didAppendFirstData(const CSSStyleSheetResource* resource)
+{
+ if (m_preloader)
+ scanCSS(resource);
+ clearResource();
+}
+
+void CSSPreloaderResourceClient::scanCSS(const CSSStyleSheetResource* resource)
+{
+ DCHECK(m_preloader);
+ CSSPreloadScanner cssPreloadScanner;
+ PreloadRequestStream preloads;
+ // Passing an empty SegmentedString here results in PreloadRequest with no
+ // file/line information.
+ // TODO(csharrison): If this becomes an issue the CSSPreloadScanner may be
+ // augmented to take care of this case without performing an additional
+ // copy.
+ const String& chunk = resource->decodedText();
+ if (!chunk.isNull()) {
+ cssPreloadScanner.scan(chunk, SegmentedString(), preloads, resource->response().url());
+ }
+ fetchPreloads(preloads);
+}
+
+void CSSPreloaderResourceClient::fetchPreloads(PreloadRequestStream& preloads)
+{
+ if (preloads.size()) {
+ m_preloader->document()->loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebLoadingBehaviorCSSPreloadFound);
+ }
+
+ Settings* settings = m_preloader->document()->settings();
+ DCHECK(settings);
+ if (settings->cssExternalScannerPreload()) {
+ int currentPreloadCount = m_preloader->countPreloads();
+ m_preloader->takeAndPreload(preloads);
+ DEFINE_STATIC_LOCAL(CustomCountHistogram, cssImportHistogram, ("PreloadScanner.ExternalCSS.PreloadCount", 1, 100, 50));
+ cssImportHistogram.count(m_preloader->countPreloads() - currentPreloadCount);
+ }
+}
+
+DEFINE_TRACE(CSSPreloaderResourceClient)
+{
+ visitor->trace(m_preloader);
+ ResourceOwner<CSSStyleSheetResource>::trace(visitor);
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698