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

Side by Side Diff: third_party/WebKit/Source/core/html/parser/HTMLDocumentParser.cpp

Issue 2242223003: Preload tokens even if a <meta> csp tag is found (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: less refactoring Created 4 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) 2010 Google, Inc. All Rights Reserved. 2 * Copyright (C) 2010 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 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 , m_reentryPermit(HTMLParserReentryPermit::create()) 113 , m_reentryPermit(HTMLParserReentryPermit::create())
114 , m_token(syncPolicy == ForceSynchronousParsing ? wrapUnique(new HTMLToken) : nullptr) 114 , m_token(syncPolicy == ForceSynchronousParsing ? wrapUnique(new HTMLToken) : nullptr)
115 , m_tokenizer(syncPolicy == ForceSynchronousParsing ? HTMLTokenizer::create( m_options) : nullptr) 115 , m_tokenizer(syncPolicy == ForceSynchronousParsing ? HTMLTokenizer::create( m_options) : nullptr)
116 , m_loadingTaskRunner(TaskRunnerHelper::getLoadingTaskRunner(&document)->clo ne()) 116 , m_loadingTaskRunner(TaskRunnerHelper::getLoadingTaskRunner(&document)->clo ne())
117 , m_parserScheduler(syncPolicy == AllowAsynchronousParsing ? HTMLParserSched uler::create(this, m_loadingTaskRunner.get()) : nullptr) 117 , m_parserScheduler(syncPolicy == AllowAsynchronousParsing ? HTMLParserSched uler::create(this, m_loadingTaskRunner.get()) : nullptr)
118 , m_xssAuditorDelegate(&document) 118 , m_xssAuditorDelegate(&document)
119 , m_weakFactory(this) 119 , m_weakFactory(this)
120 , m_preloader(HTMLResourcePreloader::create(document)) 120 , m_preloader(HTMLResourcePreloader::create(document))
121 , m_tokenizedChunkQueue(TokenizedChunkQueue::create()) 121 , m_tokenizedChunkQueue(TokenizedChunkQueue::create())
122 , m_evaluator(DocumentWriteEvaluator::create(document)) 122 , m_evaluator(DocumentWriteEvaluator::create(document))
123 , m_pendingCSPMetaTag(nullptr)
123 , m_shouldUseThreading(syncPolicy == AllowAsynchronousParsing) 124 , m_shouldUseThreading(syncPolicy == AllowAsynchronousParsing)
124 , m_endWasDelayed(false) 125 , m_endWasDelayed(false)
125 , m_haveBackgroundParser(false) 126 , m_haveBackgroundParser(false)
126 , m_tasksWereSuspended(false) 127 , m_tasksWereSuspended(false)
127 , m_pumpSessionNestingLevel(0) 128 , m_pumpSessionNestingLevel(0)
128 , m_pumpSpeculationsSessionNestingLevel(0) 129 , m_pumpSpeculationsSessionNestingLevel(0)
129 , m_isParsingAtLineNumber(false) 130 , m_isParsingAtLineNumber(false)
130 , m_triedLoadingLinkHeaders(false) 131 , m_triedLoadingLinkHeaders(false)
131 { 132 {
132 ASSERT(shouldUseThreading() || (m_token && m_tokenizer)); 133 ASSERT(shouldUseThreading() || (m_token && m_tokenizer));
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 // ApplicationCache is initialized. Note: link rel preloads don't follow 308 // ApplicationCache is initialized. Note: link rel preloads don't follow
308 // this policy per the spec. These directives should initiate a fetch as 309 // this policy per the spec. These directives should initiate a fetch as
309 // fast as possible. 310 // fast as possible.
310 if (!m_triedLoadingLinkHeaders && document()->loader() && !pendingChunks.isE mpty()) { 311 if (!m_triedLoadingLinkHeaders && document()->loader() && !pendingChunks.isE mpty()) {
311 // Note that on commit, the loader dispatched preloads for all the 312 // Note that on commit, the loader dispatched preloads for all the
312 // non-media links. 313 // non-media links.
313 document()->loader()->dispatchLinkHeaderPreloads(&pendingChunks.first()- >viewport, LinkLoader::OnlyLoadMedia); 314 document()->loader()->dispatchLinkHeaderPreloads(&pendingChunks.first()- >viewport, LinkLoader::OnlyLoadMedia);
314 m_triedLoadingLinkHeaders = true; 315 m_triedLoadingLinkHeaders = true;
315 } 316 }
316 317
317 if (!document()->documentElement()) { 318 // Defer preloads if any of the chunks contains a <meta> csp tag.
319 for (auto& chunk : pendingChunks) {
320 if (chunk->pendingCSPMetaTag)
321 m_pendingCSPMetaTag = chunk->pendingCSPMetaTag;
322 }
323
324 if (m_pendingCSPMetaTag || !document()->documentElement()) {
318 PreloadRequestStream linkRelPreloads; 325 PreloadRequestStream linkRelPreloads;
319 for (auto& chunk : pendingChunks) { 326 for (auto& chunk : pendingChunks) {
320 for (auto& request : chunk->preloads) { 327 for (auto& request : chunk->preloads) {
321 if (request->isLinkRelPreload()) 328 // Link rel preloads don't need to wait for AppCache but they
329 // should probably wait for CSP.
330 if (!m_pendingCSPMetaTag && request->isLinkRelPreload())
322 linkRelPreloads.append(std::move(request)); 331 linkRelPreloads.append(std::move(request));
323 else 332 else
324 m_queuedPreloads.append(std::move(request)); 333 m_queuedPreloads.append(std::move(request));
325 } 334 }
326 for (auto& index : chunk->likelyDocumentWriteScriptIndices) { 335 for (auto& index : chunk->likelyDocumentWriteScriptIndices) {
327 const CompactHTMLToken& token = chunk->tokens->at(index); 336 const CompactHTMLToken& token = chunk->tokens->at(index);
328 ASSERT(token.type() == HTMLToken::TokenType::Character); 337 ASSERT(token.type() == HTMLToken::TokenType::Character);
329 m_queuedDocumentWriteScripts.append(token.data()); 338 m_queuedDocumentWriteScripts.append(token.data());
330 } 339 }
331 } 340 }
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 break; 491 break;
483 } 492 }
484 493
485 m_textPosition = it->textPosition(); 494 m_textPosition = it->textPosition();
486 495
487 constructTreeFromCompactHTMLToken(*it); 496 constructTreeFromCompactHTMLToken(*it);
488 497
489 if (isStopped()) 498 if (isStopped())
490 break; 499 break;
491 500
501 // Preloads were queued if there was a <meta> csp token in a tokenized
502 // chunk.
503 if (m_pendingCSPMetaTag && it == m_pendingCSPMetaTag) {
504 m_pendingCSPMetaTag = nullptr;
505 fetchQueuedPreloads();
506 }
507
492 if (isWaitingForScripts()) { 508 if (isWaitingForScripts()) {
493 ASSERT(it + 1 == tokens->end()); // The </script> is assumed to be t he last token of this bunch. 509 ASSERT(it + 1 == tokens->end()); // The </script> is assumed to be t he last token of this bunch.
494 runScriptsForPausedTreeBuilder(); 510 runScriptsForPausedTreeBuilder();
495 validateSpeculations(std::move(chunk)); 511 validateSpeculations(std::move(chunk));
496 break; 512 break;
497 } 513 }
498 514
499 if (it->type() == HTMLToken::EndOfFile) { 515 if (it->type() == HTMLToken::EndOfFile) {
500 ASSERT(it + 1 == tokens->end()); // The EOF is assumed to be the las t token of this bunch. 516 ASSERT(it + 1 == tokens->end()); // The EOF is assumed to be the las t token of this bunch.
501 ASSERT(m_speculations.isEmpty()); // There should never be any chunk s after the EOF. 517 ASSERT(m_speculations.isEmpty()); // There should never be any chunk s after the EOF.
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 DecodedDataDocumentParser::setDecoder(std::move(decoder)); 1108 DecodedDataDocumentParser::setDecoder(std::move(decoder));
1093 1109
1094 if (m_haveBackgroundParser) 1110 if (m_haveBackgroundParser)
1095 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::setDecode r, m_backgroundParser, passed(takeDecoder())); 1111 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::setDecode r, m_backgroundParser, passed(takeDecoder()));
1096 } 1112 }
1097 1113
1098 void HTMLDocumentParser::documentElementAvailable() 1114 void HTMLDocumentParser::documentElementAvailable()
1099 { 1115 {
1100 TRACE_EVENT0("blink,loader", "HTMLDocumentParser::documentElementAvailable") ; 1116 TRACE_EVENT0("blink,loader", "HTMLDocumentParser::documentElementAvailable") ;
1101 DCHECK(document()->documentElement()); 1117 DCHECK(document()->documentElement());
1102 if (!m_queuedPreloads.isEmpty()) 1118 fetchQueuedPreloads();
1103 m_preloader->takeAndPreload(m_queuedPreloads);
1104
1105 for (const String& scriptSource : m_queuedDocumentWriteScripts) {
1106 evaluateAndPreloadScriptForDocumentWrite(scriptSource);
1107 }
1108
1109 m_queuedDocumentWriteScripts.clear();
1110 } 1119 }
1111 1120
1112 std::unique_ptr<HTMLPreloadScanner> HTMLDocumentParser::createPreloadScanner() 1121 std::unique_ptr<HTMLPreloadScanner> HTMLDocumentParser::createPreloadScanner()
1113 { 1122 {
1114 return HTMLPreloadScanner::create( 1123 return HTMLPreloadScanner::create(
1115 m_options, 1124 m_options,
1116 document()->url(), 1125 document()->url(),
1117 CachedDocumentParameters::create(document()), 1126 CachedDocumentParameters::create(document()),
1118 MediaValuesCached::MediaValuesCachedData(*document())); 1127 MediaValuesCached::MediaValuesCachedData(*document()));
1119 } 1128 }
1120 1129
1130 void HTMLDocumentParser::fetchQueuedPreloads()
1131 {
1132 if (m_pendingCSPMetaTag || !document()->documentElement())
1133 return;
1134
1135 if (!m_queuedPreloads.isEmpty())
1136 m_preloader->takeAndPreload(m_queuedPreloads);
1137
1138 for (const String& scriptSource : m_queuedDocumentWriteScripts) {
1139 evaluateAndPreloadScriptForDocumentWrite(scriptSource);
1140 }
1141
1142 m_queuedDocumentWriteScripts.clear();
1143 }
1144
1121 void HTMLDocumentParser::evaluateAndPreloadScriptForDocumentWrite(const String& source) 1145 void HTMLDocumentParser::evaluateAndPreloadScriptForDocumentWrite(const String& source)
1122 { 1146 {
1123 if (!m_evaluator->shouldEvaluate(source)) 1147 if (!m_evaluator->shouldEvaluate(source))
1124 return; 1148 return;
1125 document()->loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebL oadingBehaviorDocumentWriteEvaluator); 1149 document()->loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebL oadingBehaviorDocumentWriteEvaluator);
1126 if (!RuntimeEnabledFeatures::documentWriteEvaluatorEnabled()) 1150 if (!RuntimeEnabledFeatures::documentWriteEvaluatorEnabled())
1127 return; 1151 return;
1128 TRACE_EVENT0("blink", "HTMLDocumentParser::evaluateAndPreloadScriptForDocume ntWrite"); 1152 TRACE_EVENT0("blink", "HTMLDocumentParser::evaluateAndPreloadScriptForDocume ntWrite");
1129 1153
1130 double initializeStartTime = monotonicallyIncreasingTimeMS(); 1154 double initializeStartTime = monotonicallyIncreasingTimeMS();
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 (*WTF::bind(function, std::forward<Ps>(parameters)...))(); 1196 (*WTF::bind(function, std::forward<Ps>(parameters)...))();
1173 return; 1197 return;
1174 case Asynchronous: 1198 case Asynchronous:
1175 m_loadingTaskRunner->postTask(BLINK_FROM_HERE, WTF::bind(function, std:: forward<Ps>(parameters)...)); 1199 m_loadingTaskRunner->postTask(BLINK_FROM_HERE, WTF::bind(function, std:: forward<Ps>(parameters)...));
1176 return; 1200 return;
1177 } 1201 }
1178 NOTREACHED(); 1202 NOTREACHED();
1179 } 1203 }
1180 1204
1181 } // namespace blink 1205 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698