| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. | 2 * Copyright (C) 2013 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 15 matching lines...) Expand all Loading... |
| 26 #include "core/html/parser/BackgroundHTMLParser.h" | 26 #include "core/html/parser/BackgroundHTMLParser.h" |
| 27 | 27 |
| 28 #include "core/HTMLNames.h" | 28 #include "core/HTMLNames.h" |
| 29 #include "core/html/parser/HTMLDocumentParser.h" | 29 #include "core/html/parser/HTMLDocumentParser.h" |
| 30 #include "core/html/parser/TextResourceDecoder.h" | 30 #include "core/html/parser/TextResourceDecoder.h" |
| 31 #include "core/html/parser/XSSAuditor.h" | 31 #include "core/html/parser/XSSAuditor.h" |
| 32 #include "platform/ThreadSafeFunctional.h" | 32 #include "platform/ThreadSafeFunctional.h" |
| 33 #include "platform/TraceEvent.h" | 33 #include "platform/TraceEvent.h" |
| 34 #include "public/platform/Platform.h" | 34 #include "public/platform/Platform.h" |
| 35 #include "public/platform/WebTaskRunner.h" | 35 #include "public/platform/WebTaskRunner.h" |
| 36 #include "wtf/PtrUtil.h" |
| 36 #include "wtf/text/TextPosition.h" | 37 #include "wtf/text/TextPosition.h" |
| 38 #include <memory> |
| 37 | 39 |
| 38 namespace blink { | 40 namespace blink { |
| 39 | 41 |
| 40 // On a network with high latency and high bandwidth, using a device | 42 // On a network with high latency and high bandwidth, using a device |
| 41 // with a fast CPU, we could end up speculatively tokenizing | 43 // with a fast CPU, we could end up speculatively tokenizing |
| 42 // the whole document, well ahead of when the main-thread actually needs it. | 44 // the whole document, well ahead of when the main-thread actually needs it. |
| 43 // This is a waste of memory (and potentially time if the speculation fails). | 45 // This is a waste of memory (and potentially time if the speculation fails). |
| 44 // So we limit our outstanding tokens arbitrarily to 10,000. | 46 // So we limit our outstanding tokens arbitrarily to 10,000. |
| 45 // Our maximal memory spent speculating will be approximately: | 47 // Our maximal memory spent speculating will be approximately: |
| 46 // (defaultOutstandingTokenLimit + defaultPendingTokenLimit) * | 48 // (defaultOutstandingTokenLimit + defaultPendingTokenLimit) * |
| (...skipping 26 matching lines...) Expand all Loading... |
| 73 } | 75 } |
| 74 | 76 |
| 75 static void checkThatXSSInfosAreSafeToSendToAnotherThread(const XSSInfoStream& i
nfos) | 77 static void checkThatXSSInfosAreSafeToSendToAnotherThread(const XSSInfoStream& i
nfos) |
| 76 { | 78 { |
| 77 for (size_t i = 0; i < infos.size(); ++i) | 79 for (size_t i = 0; i < infos.size(); ++i) |
| 78 ASSERT(infos[i]->isSafeToSendToAnotherThread()); | 80 ASSERT(infos[i]->isSafeToSendToAnotherThread()); |
| 79 } | 81 } |
| 80 | 82 |
| 81 #endif | 83 #endif |
| 82 | 84 |
| 83 void BackgroundHTMLParser::start(PassRefPtr<WeakReference<BackgroundHTMLParser>>
reference, PassOwnPtr<Configuration> config, const KURL& documentURL, PassOwnPt
r<CachedDocumentParameters> cachedDocumentParameters, const MediaValuesCached::M
ediaValuesCachedData& mediaValuesCachedData, PassOwnPtr<WebTaskRunner> loadingTa
skRunner) | 85 void BackgroundHTMLParser::start(PassRefPtr<WeakReference<BackgroundHTMLParser>>
reference, std::unique_ptr<Configuration> config, const KURL& documentURL, std:
:unique_ptr<CachedDocumentParameters> cachedDocumentParameters, const MediaValue
sCached::MediaValuesCachedData& mediaValuesCachedData, std::unique_ptr<WebTaskRu
nner> loadingTaskRunner) |
| 84 { | 86 { |
| 85 new BackgroundHTMLParser(reference, std::move(config), documentURL, std::mov
e(cachedDocumentParameters), mediaValuesCachedData, std::move(loadingTaskRunner)
); | 87 new BackgroundHTMLParser(reference, std::move(config), documentURL, std::mov
e(cachedDocumentParameters), mediaValuesCachedData, std::move(loadingTaskRunner)
); |
| 86 // Caller must free by calling stop(). | 88 // Caller must free by calling stop(). |
| 87 } | 89 } |
| 88 | 90 |
| 89 BackgroundHTMLParser::Configuration::Configuration() | 91 BackgroundHTMLParser::Configuration::Configuration() |
| 90 : outstandingTokenLimit(defaultOutstandingTokenLimit) | 92 : outstandingTokenLimit(defaultOutstandingTokenLimit) |
| 91 , pendingTokenLimit(defaultPendingTokenLimit) | 93 , pendingTokenLimit(defaultPendingTokenLimit) |
| 92 { | 94 { |
| 93 } | 95 } |
| 94 | 96 |
| 95 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT
MLParser>> reference, PassOwnPtr<Configuration> config, const KURL& documentURL,
PassOwnPtr<CachedDocumentParameters> cachedDocumentParameters, const MediaValue
sCached::MediaValuesCachedData& mediaValuesCachedData, PassOwnPtr<WebTaskRunner>
loadingTaskRunner) | 97 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT
MLParser>> reference, std::unique_ptr<Configuration> config, const KURL& documen
tURL, std::unique_ptr<CachedDocumentParameters> cachedDocumentParameters, const
MediaValuesCached::MediaValuesCachedData& mediaValuesCachedData, std::unique_ptr
<WebTaskRunner> loadingTaskRunner) |
| 96 : m_weakFactory(reference, this) | 98 : m_weakFactory(reference, this) |
| 97 , m_token(adoptPtr(new HTMLToken)) | 99 , m_token(wrapUnique(new HTMLToken)) |
| 98 , m_tokenizer(HTMLTokenizer::create(config->options)) | 100 , m_tokenizer(HTMLTokenizer::create(config->options)) |
| 99 , m_treeBuilderSimulator(config->options) | 101 , m_treeBuilderSimulator(config->options) |
| 100 , m_options(config->options) | 102 , m_options(config->options) |
| 101 , m_outstandingTokenLimit(config->outstandingTokenLimit) | 103 , m_outstandingTokenLimit(config->outstandingTokenLimit) |
| 102 , m_parser(config->parser) | 104 , m_parser(config->parser) |
| 103 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) | 105 , m_pendingTokens(wrapUnique(new CompactHTMLTokenStream)) |
| 104 , m_pendingTokenLimit(config->pendingTokenLimit) | 106 , m_pendingTokenLimit(config->pendingTokenLimit) |
| 105 , m_xssAuditor(std::move(config->xssAuditor)) | 107 , m_xssAuditor(std::move(config->xssAuditor)) |
| 106 , m_preloadScanner(adoptPtr(new TokenPreloadScanner(documentURL, std::move(c
achedDocumentParameters), mediaValuesCachedData))) | 108 , m_preloadScanner(wrapUnique(new TokenPreloadScanner(documentURL, std::move
(cachedDocumentParameters), mediaValuesCachedData))) |
| 107 , m_decoder(std::move(config->decoder)) | 109 , m_decoder(std::move(config->decoder)) |
| 108 , m_loadingTaskRunner(std::move(loadingTaskRunner)) | 110 , m_loadingTaskRunner(std::move(loadingTaskRunner)) |
| 109 , m_parsedChunkQueue(config->parsedChunkQueue.release()) | 111 , m_parsedChunkQueue(config->parsedChunkQueue.release()) |
| 110 , m_startingScript(false) | 112 , m_startingScript(false) |
| 111 { | 113 { |
| 112 ASSERT(m_outstandingTokenLimit > 0); | 114 ASSERT(m_outstandingTokenLimit > 0); |
| 113 ASSERT(m_pendingTokenLimit > 0); | 115 ASSERT(m_pendingTokenLimit > 0); |
| 114 ASSERT(m_outstandingTokenLimit >= m_pendingTokenLimit); | 116 ASSERT(m_outstandingTokenLimit >= m_pendingTokenLimit); |
| 115 } | 117 } |
| 116 | 118 |
| 117 BackgroundHTMLParser::~BackgroundHTMLParser() | 119 BackgroundHTMLParser::~BackgroundHTMLParser() |
| 118 { | 120 { |
| 119 } | 121 } |
| 120 | 122 |
| 121 void BackgroundHTMLParser::appendRawBytesFromParserThread(const char* data, int
dataLength) | 123 void BackgroundHTMLParser::appendRawBytesFromParserThread(const char* data, int
dataLength) |
| 122 { | 124 { |
| 123 ASSERT(m_decoder); | 125 ASSERT(m_decoder); |
| 124 updateDocument(m_decoder->decode(data, dataLength)); | 126 updateDocument(m_decoder->decode(data, dataLength)); |
| 125 } | 127 } |
| 126 | 128 |
| 127 void BackgroundHTMLParser::appendRawBytesFromMainThread(PassOwnPtr<Vector<char>>
buffer) | 129 void BackgroundHTMLParser::appendRawBytesFromMainThread(std::unique_ptr<Vector<c
har>> buffer) |
| 128 { | 130 { |
| 129 ASSERT(m_decoder); | 131 ASSERT(m_decoder); |
| 130 updateDocument(m_decoder->decode(buffer->data(), buffer->size())); | 132 updateDocument(m_decoder->decode(buffer->data(), buffer->size())); |
| 131 } | 133 } |
| 132 | 134 |
| 133 void BackgroundHTMLParser::appendDecodedBytes(const String& input) | 135 void BackgroundHTMLParser::appendDecodedBytes(const String& input) |
| 134 { | 136 { |
| 135 ASSERT(!m_input.current().isClosed()); | 137 ASSERT(!m_input.current().isClosed()); |
| 136 m_input.append(input); | 138 m_input.append(input); |
| 137 pumpTokenizer(); | 139 pumpTokenizer(); |
| 138 } | 140 } |
| 139 | 141 |
| 140 void BackgroundHTMLParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder) | 142 void BackgroundHTMLParser::setDecoder(std::unique_ptr<TextResourceDecoder> decod
er) |
| 141 { | 143 { |
| 142 ASSERT(decoder); | 144 ASSERT(decoder); |
| 143 m_decoder = std::move(decoder); | 145 m_decoder = std::move(decoder); |
| 144 } | 146 } |
| 145 | 147 |
| 146 void BackgroundHTMLParser::flush() | 148 void BackgroundHTMLParser::flush() |
| 147 { | 149 { |
| 148 ASSERT(m_decoder); | 150 ASSERT(m_decoder); |
| 149 updateDocument(m_decoder->flush()); | 151 updateDocument(m_decoder->flush()); |
| 150 } | 152 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 161 BLINK_FROM_HERE, | 163 BLINK_FROM_HERE, |
| 162 threadSafeBind(&HTMLDocumentParser::didReceiveEncodingDataFromBackgr
oundParser, m_parser, encodingData)); | 164 threadSafeBind(&HTMLDocumentParser::didReceiveEncodingDataFromBackgr
oundParser, m_parser, encodingData)); |
| 163 } | 165 } |
| 164 | 166 |
| 165 if (decodedData.isEmpty()) | 167 if (decodedData.isEmpty()) |
| 166 return; | 168 return; |
| 167 | 169 |
| 168 appendDecodedBytes(decodedData); | 170 appendDecodedBytes(decodedData); |
| 169 } | 171 } |
| 170 | 172 |
| 171 void BackgroundHTMLParser::resumeFrom(PassOwnPtr<Checkpoint> checkpoint) | 173 void BackgroundHTMLParser::resumeFrom(std::unique_ptr<Checkpoint> checkpoint) |
| 172 { | 174 { |
| 173 m_parser = checkpoint->parser; | 175 m_parser = checkpoint->parser; |
| 174 m_token = std::move(checkpoint->token); | 176 m_token = std::move(checkpoint->token); |
| 175 m_tokenizer = std::move(checkpoint->tokenizer); | 177 m_tokenizer = std::move(checkpoint->tokenizer); |
| 176 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); | 178 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); |
| 177 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); | 179 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); |
| 178 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); | 180 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); |
| 179 m_startingScript = false; | 181 m_startingScript = false; |
| 180 m_parsedChunkQueue->clear(); | 182 m_parsedChunkQueue->clear(); |
| 181 pumpTokenizer(); | 183 pumpTokenizer(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 sendTokensToMainThread(); | 235 sendTokensToMainThread(); |
| 234 break; | 236 break; |
| 235 } | 237 } |
| 236 | 238 |
| 237 if (m_xssAuditor->isEnabled()) | 239 if (m_xssAuditor->isEnabled()) |
| 238 m_sourceTracker.end(m_input.current(), m_tokenizer.get(), *m_token); | 240 m_sourceTracker.end(m_input.current(), m_tokenizer.get(), *m_token); |
| 239 | 241 |
| 240 { | 242 { |
| 241 TextPosition position = TextPosition(m_input.current().currentLine()
, m_input.current().currentColumn()); | 243 TextPosition position = TextPosition(m_input.current().currentLine()
, m_input.current().currentColumn()); |
| 242 | 244 |
| 243 if (OwnPtr<XSSInfo> xssInfo = m_xssAuditor->filterToken(FilterTokenR
equest(*m_token, m_sourceTracker, m_tokenizer->shouldAllowCDATA()))) { | 245 if (std::unique_ptr<XSSInfo> xssInfo = m_xssAuditor->filterToken(Fil
terTokenRequest(*m_token, m_sourceTracker, m_tokenizer->shouldAllowCDATA()))) { |
| 244 xssInfo->m_textPosition = position; | 246 xssInfo->m_textPosition = position; |
| 245 m_pendingXSSInfos.append(std::move(xssInfo)); | 247 m_pendingXSSInfos.append(std::move(xssInfo)); |
| 246 } | 248 } |
| 247 | 249 |
| 248 CompactHTMLToken token(m_token.get(), position); | 250 CompactHTMLToken token(m_token.get(), position); |
| 249 | 251 |
| 250 bool shouldEvaluateForDocumentWrite = false; | 252 bool shouldEvaluateForDocumentWrite = false; |
| 251 m_preloadScanner->scan(token, m_input.current(), m_pendingPreloads,
&m_viewportDescription, &shouldEvaluateForDocumentWrite); | 253 m_preloadScanner->scan(token, m_input.current(), m_pendingPreloads,
&m_viewportDescription, &shouldEvaluateForDocumentWrite); |
| 252 | 254 |
| 253 simulatedToken = m_treeBuilderSimulator.simulate(token, m_tokenizer.
get()); | 255 simulatedToken = m_treeBuilderSimulator.simulate(token, m_tokenizer.
get()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 280 { | 282 { |
| 281 if (m_pendingTokens->isEmpty()) | 283 if (m_pendingTokens->isEmpty()) |
| 282 return; | 284 return; |
| 283 | 285 |
| 284 #if ENABLE(ASSERT) | 286 #if ENABLE(ASSERT) |
| 285 checkThatTokensAreSafeToSendToAnotherThread(m_pendingTokens.get()); | 287 checkThatTokensAreSafeToSendToAnotherThread(m_pendingTokens.get()); |
| 286 checkThatPreloadsAreSafeToSendToAnotherThread(m_pendingPreloads); | 288 checkThatPreloadsAreSafeToSendToAnotherThread(m_pendingPreloads); |
| 287 checkThatXSSInfosAreSafeToSendToAnotherThread(m_pendingXSSInfos); | 289 checkThatXSSInfosAreSafeToSendToAnotherThread(m_pendingXSSInfos); |
| 288 #endif | 290 #endif |
| 289 | 291 |
| 290 OwnPtr<HTMLDocumentParser::ParsedChunk> chunk = adoptPtr(new HTMLDocumentPar
ser::ParsedChunk); | 292 std::unique_ptr<HTMLDocumentParser::ParsedChunk> chunk = wrapUnique(new HTML
DocumentParser::ParsedChunk); |
| 291 TRACE_EVENT_WITH_FLOW0("blink,loading", "BackgroundHTMLParser::sendTokensToM
ainThread", chunk.get(), TRACE_EVENT_FLAG_FLOW_OUT); | 293 TRACE_EVENT_WITH_FLOW0("blink,loading", "BackgroundHTMLParser::sendTokensToM
ainThread", chunk.get(), TRACE_EVENT_FLAG_FLOW_OUT); |
| 292 chunk->preloads.swap(m_pendingPreloads); | 294 chunk->preloads.swap(m_pendingPreloads); |
| 293 if (m_viewportDescription.set) | 295 if (m_viewportDescription.set) |
| 294 chunk->viewport = m_viewportDescription; | 296 chunk->viewport = m_viewportDescription; |
| 295 chunk->xssInfos.swap(m_pendingXSSInfos); | 297 chunk->xssInfos.swap(m_pendingXSSInfos); |
| 296 chunk->tokenizerState = m_tokenizer->getState(); | 298 chunk->tokenizerState = m_tokenizer->getState(); |
| 297 chunk->treeBuilderState = m_treeBuilderSimulator.state(); | 299 chunk->treeBuilderState = m_treeBuilderSimulator.state(); |
| 298 chunk->inputCheckpoint = m_input.createCheckpoint(m_pendingTokens->size()); | 300 chunk->inputCheckpoint = m_input.createCheckpoint(m_pendingTokens->size()); |
| 299 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); | 301 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); |
| 300 chunk->tokens = std::move(m_pendingTokens); | 302 chunk->tokens = std::move(m_pendingTokens); |
| 301 chunk->startingScript = m_startingScript; | 303 chunk->startingScript = m_startingScript; |
| 302 chunk->likelyDocumentWriteScriptIndices.swap(m_likelyDocumentWriteScriptIndi
ces); | 304 chunk->likelyDocumentWriteScriptIndices.swap(m_likelyDocumentWriteScriptIndi
ces); |
| 303 m_startingScript = false; | 305 m_startingScript = false; |
| 304 | 306 |
| 305 bool isEmpty = m_parsedChunkQueue->enqueue(std::move(chunk)); | 307 bool isEmpty = m_parsedChunkQueue->enqueue(std::move(chunk)); |
| 306 if (isEmpty) { | 308 if (isEmpty) { |
| 307 m_loadingTaskRunner->postTask( | 309 m_loadingTaskRunner->postTask( |
| 308 BLINK_FROM_HERE, | 310 BLINK_FROM_HERE, |
| 309 threadSafeBind(&HTMLDocumentParser::notifyPendingParsedChunks, m_par
ser)); | 311 threadSafeBind(&HTMLDocumentParser::notifyPendingParsedChunks, m_par
ser)); |
| 310 } | 312 } |
| 311 | 313 |
| 312 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); | 314 m_pendingTokens = wrapUnique(new CompactHTMLTokenStream); |
| 313 } | 315 } |
| 314 | 316 |
| 315 } // namespace blink | 317 } // namespace blink |
| OLD | NEW |