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