| Index: third_party/WebKit/Source/core/html/parser/BackgroundHTMLParser.cpp
|
| diff --git a/third_party/WebKit/Source/core/html/parser/BackgroundHTMLParser.cpp b/third_party/WebKit/Source/core/html/parser/BackgroundHTMLParser.cpp
|
| index 2a325cf5f10f5a93e38f2c6dc56a259455450a44..540e0a370de828a6beabdd35efa8af3a18f7b616 100644
|
| --- a/third_party/WebKit/Source/core/html/parser/BackgroundHTMLParser.cpp
|
| +++ b/third_party/WebKit/Source/core/html/parser/BackgroundHTMLParser.cpp
|
| @@ -25,7 +25,10 @@
|
|
|
| #include "core/html/parser/BackgroundHTMLParser.h"
|
|
|
| +#include <memory>
|
| #include "core/HTMLNames.h"
|
| +#include "core/dom/Document.h"
|
| +#include "core/dom/TaskRunnerHelper.h"
|
| #include "core/html/parser/HTMLDocumentParser.h"
|
| #include "core/html/parser/TextResourceDecoder.h"
|
| #include "core/html/parser/XSSAuditor.h"
|
| @@ -38,7 +41,6 @@
|
| #include "wtf/Functional.h"
|
| #include "wtf/PtrUtil.h"
|
| #include "wtf/text/TextPosition.h"
|
| -#include <memory>
|
|
|
| namespace blink {
|
|
|
| @@ -69,37 +71,37 @@ using namespace HTMLNames;
|
| static void checkThatTokensAreSafeToSendToAnotherThread(
|
| const CompactHTMLTokenStream* tokens) {
|
| for (size_t i = 0; i < tokens->size(); ++i)
|
| - ASSERT(tokens->at(i).isSafeToSendToAnotherThread());
|
| + DCHECK(tokens->at(i).isSafeToSendToAnotherThread());
|
| }
|
|
|
| static void checkThatPreloadsAreSafeToSendToAnotherThread(
|
| const PreloadRequestStream& preloads) {
|
| for (size_t i = 0; i < preloads.size(); ++i)
|
| - ASSERT(preloads[i]->isSafeToSendToAnotherThread());
|
| + DCHECK(preloads[i]->isSafeToSendToAnotherThread());
|
| }
|
|
|
| static void checkThatXSSInfosAreSafeToSendToAnotherThread(
|
| const XSSInfoStream& infos) {
|
| for (size_t i = 0; i < infos.size(); ++i)
|
| - ASSERT(infos[i]->isSafeToSendToAnotherThread());
|
| + DCHECK(infos[i]->isSafeToSendToAnotherThread());
|
| }
|
|
|
| #endif
|
|
|
| -WeakPtr<BackgroundHTMLParser> BackgroundHTMLParser::create(
|
| - std::unique_ptr<Configuration> config,
|
| - RefPtr<WebTaskRunner> loadingTaskRunner) {
|
| - auto* backgroundParser =
|
| - new BackgroundHTMLParser(std::move(config), std::move(loadingTaskRunner));
|
| - return backgroundParser->m_weakFactory.createWeakPtr();
|
| +BackgroundHTMLParser* BackgroundHTMLParser::create(
|
| + HTMLDocumentParser* parser,
|
| + Document& document,
|
| + std::unique_ptr<Configuration> config) {
|
| + return new BackgroundHTMLParser(
|
| + parser, std::move(config),
|
| + TaskRunnerHelper::get(TaskType::Networking, &document),
|
| + WTF::makeUnique<TokenPreloadScanner>(
|
| + document.url(), CachedDocumentParameters::create(&document),
|
| + MediaValuesCached::MediaValuesCachedData(document)));
|
| }
|
|
|
| -void BackgroundHTMLParser::init(
|
| - const KURL& documentURL,
|
| - std::unique_ptr<CachedDocumentParameters> cachedDocumentParameters,
|
| - const MediaValuesCached::MediaValuesCachedData& mediaValuesCachedData) {
|
| - m_preloadScanner.reset(new TokenPreloadScanner(
|
| - documentURL, std::move(cachedDocumentParameters), mediaValuesCachedData));
|
| +DEFINE_TRACE(BackgroundHTMLParser) {
|
| + visitor->trace(m_parser);
|
| }
|
|
|
| BackgroundHTMLParser::Configuration::Configuration()
|
| @@ -108,18 +110,20 @@ BackgroundHTMLParser::Configuration::Configuration()
|
| shouldCoalesceChunks(false) {}
|
|
|
| BackgroundHTMLParser::BackgroundHTMLParser(
|
| + HTMLDocumentParser* parser,
|
| std::unique_ptr<Configuration> config,
|
| - RefPtr<WebTaskRunner> loadingTaskRunner)
|
| - : m_weakFactory(this),
|
| - m_token(WTF::wrapUnique(new HTMLToken)),
|
| + RefPtr<WebTaskRunner> loadingTaskRunner,
|
| + std::unique_ptr<TokenPreloadScanner> scanner)
|
| + : m_token(WTF::wrapUnique(new HTMLToken)),
|
| m_tokenizer(HTMLTokenizer::create(config->options)),
|
| m_treeBuilderSimulator(config->options),
|
| m_options(config->options),
|
| m_outstandingTokenLimit(config->outstandingTokenLimit),
|
| - m_parser(config->parser),
|
| + m_parser(parser),
|
| m_pendingTokens(WTF::wrapUnique(new CompactHTMLTokenStream)),
|
| m_pendingTokenLimit(config->pendingTokenLimit),
|
| m_xssAuditor(std::move(config->xssAuditor)),
|
| + m_preloadScanner(std::move(scanner)),
|
| m_decoder(std::move(config->decoder)),
|
| m_loadingTaskRunner(std::move(loadingTaskRunner)),
|
| m_tokenizedChunkQueue(std::move(config->tokenizedChunkQueue)),
|
| @@ -128,9 +132,9 @@ BackgroundHTMLParser::BackgroundHTMLParser(
|
| m_startingScript(false),
|
| m_lastBytesReceivedTime(0.0),
|
| m_shouldCoalesceChunks(config->shouldCoalesceChunks) {
|
| - ASSERT(m_outstandingTokenLimit > 0);
|
| - ASSERT(m_pendingTokenLimit > 0);
|
| - ASSERT(m_outstandingTokenLimit >= m_pendingTokenLimit);
|
| + DCHECK_GT(m_outstandingTokenLimit, 0u);
|
| + DCHECK_GT(m_pendingTokenLimit, 0u);
|
| + DCHECK_GE(m_outstandingTokenLimit, m_pendingTokenLimit);
|
| }
|
|
|
| BackgroundHTMLParser::~BackgroundHTMLParser() {}
|
| @@ -138,29 +142,34 @@ BackgroundHTMLParser::~BackgroundHTMLParser() {}
|
| void BackgroundHTMLParser::appendRawBytesFromMainThread(
|
| std::unique_ptr<Vector<char>> buffer,
|
| double bytesReceivedTime) {
|
| - ASSERT(m_decoder);
|
| + DCHECK(m_decoder);
|
| m_lastBytesReceivedTime = bytesReceivedTime;
|
| DEFINE_STATIC_LOCAL(CustomCountHistogram, queueDelay,
|
| ("Parser.AppendBytesDelay", 1, 5000, 50));
|
| queueDelay.count(monotonicallyIncreasingTimeMS() - bytesReceivedTime);
|
| - updateDocument(m_decoder->decode(buffer->data(), buffer->size()));
|
| + m_loadingTaskRunner->postTask(
|
| + BLINK_FROM_HERE,
|
| + WTF::bind(&BackgroundHTMLParser::updateDocument, wrapPersistent(this),
|
| + m_decoder->decode(buffer->data(), buffer->size())));
|
| }
|
|
|
| void BackgroundHTMLParser::appendDecodedBytes(const String& input) {
|
| - ASSERT(!m_input.current().isClosed());
|
| + DCHECK(!m_input.current().isClosed());
|
| m_input.append(input);
|
| pumpTokenizer();
|
| }
|
|
|
| void BackgroundHTMLParser::setDecoder(
|
| std::unique_ptr<TextResourceDecoder> decoder) {
|
| - ASSERT(decoder);
|
| + DCHECK(decoder);
|
| m_decoder = std::move(decoder);
|
| }
|
|
|
| void BackgroundHTMLParser::flush() {
|
| - ASSERT(m_decoder);
|
| - updateDocument(m_decoder->flush());
|
| + DCHECK(m_decoder);
|
| + m_loadingTaskRunner->postTask(
|
| + BLINK_FROM_HERE, WTF::bind(&BackgroundHTMLParser::updateDocument,
|
| + wrapPersistent(this), m_decoder->flush()));
|
| }
|
|
|
| void BackgroundHTMLParser::updateDocument(const String& decodedData) {
|
| @@ -170,9 +179,7 @@ void BackgroundHTMLParser::updateDocument(const String& decodedData) {
|
| m_lastSeenEncodingData = encodingData;
|
|
|
| m_xssAuditor->setEncoding(encodingData.encoding());
|
| - runOnMainThread(
|
| - &HTMLDocumentParser::didReceiveEncodingDataFromBackgroundParser,
|
| - m_parser, encodingData);
|
| + m_parser->didReceiveEncodingDataFromBackgroundParser(encodingData);
|
| }
|
|
|
| if (decodedData.isEmpty())
|
| @@ -182,7 +189,14 @@ void BackgroundHTMLParser::updateDocument(const String& decodedData) {
|
| }
|
|
|
| void BackgroundHTMLParser::resumeFrom(std::unique_ptr<Checkpoint> checkpoint) {
|
| - m_parser = checkpoint->parser;
|
| + m_loadingTaskRunner->postTask(
|
| + BLINK_FROM_HERE,
|
| + WTF::bind(&BackgroundHTMLParser::onResumeFrom, wrapPersistent(this),
|
| + WTF::passed(std::move(checkpoint))));
|
| +}
|
| +
|
| +void BackgroundHTMLParser::onResumeFrom(
|
| + std::unique_ptr<Checkpoint> checkpoint) {
|
| m_token = std::move(checkpoint->token);
|
| m_tokenizer = std::move(checkpoint->tokenizer);
|
| m_treeBuilderSimulator.setState(checkpoint->treeBuilderState);
|
| @@ -199,16 +213,20 @@ void BackgroundHTMLParser::startedChunkWithCheckpoint(
|
| // Note, we should not have to worry about the index being invalid as messages
|
| // from the main thread will be processed in FIFO order.
|
| m_input.invalidateCheckpointsBefore(inputCheckpoint);
|
| - pumpTokenizer();
|
| + m_loadingTaskRunner->postTask(
|
| + BLINK_FROM_HERE,
|
| + WTF::bind(&BackgroundHTMLParser::pumpTokenizer, wrapPersistent(this)));
|
| }
|
|
|
| -void BackgroundHTMLParser::finish() {
|
| +void BackgroundHTMLParser::onFinish() {
|
| markEndOfFile();
|
| pumpTokenizer();
|
| }
|
|
|
| -void BackgroundHTMLParser::stop() {
|
| - delete this;
|
| +void BackgroundHTMLParser::finish() {
|
| + m_loadingTaskRunner->postTask(
|
| + BLINK_FROM_HERE,
|
| + WTF::bind(&BackgroundHTMLParser::onFinish, wrapPersistent(this)));
|
| }
|
|
|
| void BackgroundHTMLParser::forcePlaintextForTextDocument() {
|
| @@ -220,7 +238,7 @@ void BackgroundHTMLParser::forcePlaintextForTextDocument() {
|
| }
|
|
|
| void BackgroundHTMLParser::markEndOfFile() {
|
| - ASSERT(!m_input.current().isClosed());
|
| + DCHECK(!m_input.current().isClosed());
|
| m_input.append(String(&kEndOfFileMarker, 1));
|
| m_input.close();
|
| }
|
| @@ -302,8 +320,7 @@ void BackgroundHTMLParser::pumpTokenizer() {
|
| }
|
|
|
| if (!m_shouldCoalesceChunks && shouldNotifyMainThread) {
|
| - runOnMainThread(&HTMLDocumentParser::notifyPendingTokenizedChunks,
|
| - m_parser);
|
| + m_parser->notifyPendingTokenizedChunks();
|
| shouldNotifyMainThread = false;
|
| }
|
| }
|
| @@ -312,8 +329,7 @@ void BackgroundHTMLParser::pumpTokenizer() {
|
| // anything expensive (extensions, scripts) take up time on the main thread. A
|
| // busy main thread can cause preload delays.
|
| if (shouldNotifyMainThread) {
|
| - runOnMainThread(&HTMLDocumentParser::notifyPendingTokenizedChunks,
|
| - m_parser);
|
| + m_parser->notifyPendingTokenizedChunks();
|
| }
|
| }
|
|
|
| @@ -368,21 +384,4 @@ bool BackgroundHTMLParser::queueChunkForMainThread() {
|
| return isEmpty;
|
| }
|
|
|
| -// If the background parser is already running on the main thread, then it is
|
| -// not necessary to post a task to the main thread to run asynchronously. The
|
| -// main parser deals with chunking up its own work.
|
| -// TODO(csharrison): This is a pretty big hack because we don't actually need a
|
| -// CrossThreadClosure in these cases. This is just experimental.
|
| -template <typename FunctionType, typename... Ps>
|
| -void BackgroundHTMLParser::runOnMainThread(FunctionType function,
|
| - Ps&&... parameters) {
|
| - if (isMainThread()) {
|
| - (*WTF::bind(function, std::forward<Ps>(parameters)...))();
|
| - } else {
|
| - m_loadingTaskRunner->postTask(
|
| - BLINK_FROM_HERE,
|
| - crossThreadBind(function, std::forward<Ps>(parameters)...));
|
| - }
|
| -}
|
| -
|
| } // namespace blink
|
|
|