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

Unified Diff: Source/core/html/HTMLLinkElement.cpp

Issue 166633002: Prefetch @import files from CSS files. Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fixed according to Yoav´s comments Created 5 years, 11 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
« no previous file with comments | « Source/core/html/HTMLLinkElement.h ('k') | Source/core/html/parser/CSSPreloadScanner.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/html/HTMLLinkElement.cpp
diff --git a/Source/core/html/HTMLLinkElement.cpp b/Source/core/html/HTMLLinkElement.cpp
index 6b6d8b195ff4c55adb09468cb5ede4836628fbea..e039960f2479721a6dc1e9b43396b6e3b88d70a8 100644
--- a/Source/core/html/HTMLLinkElement.cpp
+++ b/Source/core/html/HTMLLinkElement.cpp
@@ -46,6 +46,7 @@
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "core/html/LinkManifest.h"
#include "core/html/imports/LinkImport.h"
+#include "core/html/parser/CompactHTMLToken.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/rendering/style/StyleInheritedData.h"
@@ -480,12 +481,19 @@ PassOwnPtrWillBeRawPtr<LinkStyle> LinkStyle::create(HTMLLinkElement* owner)
LinkStyle::LinkStyle(HTMLLinkElement* owner)
: LinkResource(owner)
+ , m_token(adoptPtr(new HTMLToken))
+ , m_tokenizer(HTMLTokenizer::create(m_options))
+ , m_preloadScanner(adoptPtr(new CSSPreloadScanner))
+ , m_preloadLength(0)
, m_disabledState(Unset)
, m_pendingSheetType(None)
, m_loading(false)
, m_firedLoad(false)
, m_loadedSheet(false)
{
+ // The tokenizer uses RAWTEXTState to identify the origin of a request.
+ // This state is also used while handling CSS prefetch in the html parser.
+ m_tokenizer->setState(HTMLTokenizer::RAWTEXTState);
}
LinkStyle::~LinkStyle()
@@ -554,6 +562,43 @@ void LinkStyle::setCSSStyleSheet(const String& href, const KURL& baseURL, const
const_cast<CSSStyleSheetResource*>(cachedStyleSheet)->saveParsedStyleSheet(styleSheet);
}
+void LinkStyle::dataReceived(const CSSStyleSheetResource* cachedStyleSheet)
+{
+ // An absence of preload scanner means we are done with @import statements.
+ if (!m_preloadScanner)
+ return;
+
+ PreloadRequestStream pendingPreloads;
+ String sheetText = cachedStyleSheet->sheetText();
+ if (sheetText.length() == m_preloadLength)
+ return;
+
+ m_input.append(sheetText.substring(m_preloadLength));
+
+ while (m_tokenizer->nextToken(m_input.current(), *m_token)) {
+ CompactHTMLToken token(m_token.get(), TextPosition(m_input.current().currentLine(), m_input.current().currentColumn()));
+
+ m_preloadScanner->scan(token.data(), m_input.current(), pendingPreloads);
+ m_token->clear();
+ }
+
+ m_preloadLength = sheetText.length();
+
+ OwnPtr<HTMLResourcePreloader> preloader = HTMLResourcePreloader::create(document());
+ preloader->takeAndPreload(pendingPreloads);
+ if (m_preloadScanner->isDoneParsingImports()) {
+ // Note: Although dataRecieved will be called for each packet of data
+ // returned from the network, @import statements are always at the top
+ // of the CSS file. As soon as we reach a part of the CSS file which
+ // is not an @import we can delete our preloader and ignore the rest of
+ // file. If we ever make CSS preloading more complicated (to look for
+ // URLs other than @imports) we will need to re-think this logic.
+ m_preloadScanner.clear();
+ m_tokenizer.clear();
+ m_token.clear();
+ }
+}
+
bool LinkStyle::sheetLoaded()
{
if (!styleSheetIsLoading()) {
« no previous file with comments | « Source/core/html/HTMLLinkElement.h ('k') | Source/core/html/parser/CSSPreloadScanner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698