Chromium Code Reviews| 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..3938c61151e3c30630bd470c2951040553d07d5f 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> |
|
Yoav Weiss
2017/02/09 10:39:00
Do you know why is <memory> included both here and
Charlie Harrison
2017/02/13 22:55:31
It is for unique_ptr
Yoav Weiss
2017/02/14 06:45:47
Yeah, but I'd love to see it *only* in the h, and
Charlie Harrison
2017/02/15 03:00:07
Oh, no that is completely fair. I completely misre
|
| #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 { |
| @@ -86,20 +88,20 @@ static void checkThatXSSInfosAreSafeToSendToAnotherThread( |
| #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)), |
| @@ -143,7 +147,10 @@ void BackgroundHTMLParser::appendRawBytesFromMainThread( |
| 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) { |
| @@ -160,7 +167,9 @@ void BackgroundHTMLParser::setDecoder( |
| void BackgroundHTMLParser::flush() { |
| ASSERT(m_decoder); |
| - updateDocument(m_decoder->flush()); |
| + 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)))); |
|
Yoav Weiss
2017/02/09 10:39:00
Is this switch from sync to async calls related to
Charlie Harrison
2017/02/13 22:55:32
No, I'm also moving the postTasks from outside the
Yoav Weiss
2017/02/14 06:45:47
You can leave it as part of this patch
|
| +} |
| + |
| +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() { |
| @@ -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 |