OLD | NEW |
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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 tokenizerStateForContextElement(contextElement, reportErrors, m_options)); | 118 tokenizerStateForContextElement(contextElement, reportErrors, m_options)); |
119 m_xssAuditor.initForFragment(); | 119 m_xssAuditor.initForFragment(); |
120 } | 120 } |
121 | 121 |
122 HTMLDocumentParser::HTMLDocumentParser(Document& document, | 122 HTMLDocumentParser::HTMLDocumentParser(Document& document, |
123 ParserContentPolicy contentPolicy, | 123 ParserContentPolicy contentPolicy, |
124 ParserSynchronizationPolicy syncPolicy) | 124 ParserSynchronizationPolicy syncPolicy) |
125 : ScriptableDocumentParser(document, contentPolicy), | 125 : ScriptableDocumentParser(document, contentPolicy), |
126 m_options(&document), | 126 m_options(&document), |
127 m_reentryPermit(HTMLParserReentryPermit::create()), | 127 m_reentryPermit(HTMLParserReentryPermit::create()), |
128 m_token(syncPolicy == ForceSynchronousParsing ? wrapUnique(new HTMLToken) | 128 m_token(syncPolicy == ForceSynchronousParsing |
129 : nullptr), | 129 ? WTF::wrapUnique(new HTMLToken) |
| 130 : nullptr), |
130 m_tokenizer(syncPolicy == ForceSynchronousParsing | 131 m_tokenizer(syncPolicy == ForceSynchronousParsing |
131 ? HTMLTokenizer::create(m_options) | 132 ? HTMLTokenizer::create(m_options) |
132 : nullptr), | 133 : nullptr), |
133 m_loadingTaskRunner( | 134 m_loadingTaskRunner( |
134 TaskRunnerHelper::get(TaskType::Networking, &document)->clone()), | 135 TaskRunnerHelper::get(TaskType::Networking, &document)->clone()), |
135 m_parserScheduler( | 136 m_parserScheduler( |
136 syncPolicy == AllowAsynchronousParsing | 137 syncPolicy == AllowAsynchronousParsing |
137 ? HTMLParserScheduler::create(this, m_loadingTaskRunner.get()) | 138 ? HTMLParserScheduler::create(this, m_loadingTaskRunner.get()) |
138 : nullptr), | 139 : nullptr), |
139 m_xssAuditorDelegate(&document), | 140 m_xssAuditorDelegate(&document), |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 } | 452 } |
452 DEFINE_STATIC_LOCAL(CustomCountHistogram, discardedTokenCountHistogram, | 453 DEFINE_STATIC_LOCAL(CustomCountHistogram, discardedTokenCountHistogram, |
453 ("Parser.DiscardedTokenCount", 1, 100000, 50)); | 454 ("Parser.DiscardedTokenCount", 1, 100000, 50)); |
454 discardedTokenCountHistogram.count(discardedTokenCount); | 455 discardedTokenCountHistogram.count(discardedTokenCount); |
455 | 456 |
456 m_speculations.clear(); | 457 m_speculations.clear(); |
457 m_pendingCSPMetaToken = nullptr; | 458 m_pendingCSPMetaToken = nullptr; |
458 m_queuedPreloads.clear(); | 459 m_queuedPreloads.clear(); |
459 | 460 |
460 std::unique_ptr<BackgroundHTMLParser::Checkpoint> checkpoint = | 461 std::unique_ptr<BackgroundHTMLParser::Checkpoint> checkpoint = |
461 wrapUnique(new BackgroundHTMLParser::Checkpoint); | 462 WTF::wrapUnique(new BackgroundHTMLParser::Checkpoint); |
462 checkpoint->parser = m_weakFactory.createWeakPtr(); | 463 checkpoint->parser = m_weakFactory.createWeakPtr(); |
463 checkpoint->token = std::move(token); | 464 checkpoint->token = std::move(token); |
464 checkpoint->tokenizer = std::move(tokenizer); | 465 checkpoint->tokenizer = std::move(tokenizer); |
465 checkpoint->treeBuilderState = | 466 checkpoint->treeBuilderState = |
466 HTMLTreeBuilderSimulator::stateFor(m_treeBuilder.get()); | 467 HTMLTreeBuilderSimulator::stateFor(m_treeBuilder.get()); |
467 checkpoint->inputCheckpoint = lastChunkBeforeScript->inputCheckpoint; | 468 checkpoint->inputCheckpoint = lastChunkBeforeScript->inputCheckpoint; |
468 checkpoint->preloadScannerCheckpoint = | 469 checkpoint->preloadScannerCheckpoint = |
469 lastChunkBeforeScript->preloadScannerCheckpoint; | 470 lastChunkBeforeScript->preloadScannerCheckpoint; |
470 checkpoint->unparsedInput = m_input.current().toString().isolatedCopy(); | 471 checkpoint->unparsedInput = m_input.current().toString().isolatedCopy(); |
471 // FIXME: This should be passed in instead of cleared. | 472 // FIXME: This should be passed in instead of cleared. |
472 m_input.current().clear(); | 473 m_input.current().clear(); |
473 | 474 |
474 ASSERT(checkpoint->unparsedInput.isSafeToSendToAnotherThread()); | 475 ASSERT(checkpoint->unparsedInput.isSafeToSendToAnotherThread()); |
475 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::resumeFrom, | 476 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::resumeFrom, |
476 m_backgroundParser, passed(std::move(checkpoint))); | 477 m_backgroundParser, |
| 478 WTF::passed(std::move(checkpoint))); |
477 } | 479 } |
478 | 480 |
479 size_t HTMLDocumentParser::processTokenizedChunkFromBackgroundParser( | 481 size_t HTMLDocumentParser::processTokenizedChunkFromBackgroundParser( |
480 std::unique_ptr<TokenizedChunk> popChunk) { | 482 std::unique_ptr<TokenizedChunk> popChunk) { |
481 TRACE_EVENT_WITH_FLOW0( | 483 TRACE_EVENT_WITH_FLOW0( |
482 "blink,loading", | 484 "blink,loading", |
483 "HTMLDocumentParser::processTokenizedChunkFromBackgroundParser", | 485 "HTMLDocumentParser::processTokenizedChunkFromBackgroundParser", |
484 popChunk.get(), TRACE_EVENT_FLAG_FLOW_IN); | 486 popChunk.get(), TRACE_EVENT_FLAG_FLOW_IN); |
485 AutoReset<bool> hasLineNumber(&m_isParsingAtLineNumber, true); | 487 AutoReset<bool> hasLineNumber(&m_isParsingAtLineNumber, true); |
486 | 488 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 void HTMLDocumentParser::insert(const SegmentedString& source) { | 777 void HTMLDocumentParser::insert(const SegmentedString& source) { |
776 if (isStopped()) | 778 if (isStopped()) |
777 return; | 779 return; |
778 | 780 |
779 TRACE_EVENT1("blink", "HTMLDocumentParser::insert", "source_length", | 781 TRACE_EVENT1("blink", "HTMLDocumentParser::insert", "source_length", |
780 source.length()); | 782 source.length()); |
781 | 783 |
782 if (!m_tokenizer) { | 784 if (!m_tokenizer) { |
783 ASSERT(!inPumpSession()); | 785 ASSERT(!inPumpSession()); |
784 ASSERT(m_haveBackgroundParser || wasCreatedByScript()); | 786 ASSERT(m_haveBackgroundParser || wasCreatedByScript()); |
785 m_token = wrapUnique(new HTMLToken); | 787 m_token = WTF::wrapUnique(new HTMLToken); |
786 m_tokenizer = HTMLTokenizer::create(m_options); | 788 m_tokenizer = HTMLTokenizer::create(m_options); |
787 } | 789 } |
788 | 790 |
789 SegmentedString excludedLineNumberSource(source); | 791 SegmentedString excludedLineNumberSource(source); |
790 excludedLineNumberSource.setExcludeLineNumbers(); | 792 excludedLineNumberSource.setExcludeLineNumbers(); |
791 m_input.insertAtCurrentInsertionPoint(excludedLineNumberSource); | 793 m_input.insertAtCurrentInsertionPoint(excludedLineNumberSource); |
792 pumpTokenizerIfPossible(); | 794 pumpTokenizerIfPossible(); |
793 | 795 |
794 if (isWaitingForScripts()) { | 796 if (isWaitingForScripts()) { |
795 // Check the document.write() output with a separate preload scanner as | 797 // Check the document.write() output with a separate preload scanner as |
(...skipping 18 matching lines...) Expand all Loading... |
814 // when background parser goes away. | 816 // when background parser goes away. |
815 if (document()->frame() && document()->frame()->frameScheduler()) | 817 if (document()->frame() && document()->frame()->frameScheduler()) |
816 document()->frame()->frameScheduler()->setDocumentParsingInBackground(true); | 818 document()->frame()->frameScheduler()->setDocumentParsingInBackground(true); |
817 | 819 |
818 // Make sure that a resolver is set up, so that the correct viewport | 820 // Make sure that a resolver is set up, so that the correct viewport |
819 // dimensions will be fed to the background parser and preload scanner. | 821 // dimensions will be fed to the background parser and preload scanner. |
820 if (document()->loader()) | 822 if (document()->loader()) |
821 document()->ensureStyleResolver(); | 823 document()->ensureStyleResolver(); |
822 | 824 |
823 std::unique_ptr<BackgroundHTMLParser::Configuration> config = | 825 std::unique_ptr<BackgroundHTMLParser::Configuration> config = |
824 wrapUnique(new BackgroundHTMLParser::Configuration); | 826 WTF::wrapUnique(new BackgroundHTMLParser::Configuration); |
825 config->options = m_options; | 827 config->options = m_options; |
826 config->parser = m_weakFactory.createWeakPtr(); | 828 config->parser = m_weakFactory.createWeakPtr(); |
827 config->xssAuditor = wrapUnique(new XSSAuditor); | 829 config->xssAuditor = WTF::wrapUnique(new XSSAuditor); |
828 config->xssAuditor->init(document(), &m_xssAuditorDelegate); | 830 config->xssAuditor->init(document(), &m_xssAuditorDelegate); |
829 | 831 |
830 config->decoder = takeDecoder(); | 832 config->decoder = takeDecoder(); |
831 config->tokenizedChunkQueue = m_tokenizedChunkQueue.get(); | 833 config->tokenizedChunkQueue = m_tokenizedChunkQueue.get(); |
832 if (document()->settings()) { | 834 if (document()->settings()) { |
833 if (document()->settings()->backgroundHtmlParserOutstandingTokenLimit()) | 835 if (document()->settings()->backgroundHtmlParserOutstandingTokenLimit()) |
834 config->outstandingTokenLimit = | 836 config->outstandingTokenLimit = |
835 document()->settings()->backgroundHtmlParserOutstandingTokenLimit(); | 837 document()->settings()->backgroundHtmlParserOutstandingTokenLimit(); |
836 if (document()->settings()->backgroundHtmlParserPendingTokenLimit()) | 838 if (document()->settings()->backgroundHtmlParserPendingTokenLimit()) |
837 config->pendingTokenLimit = | 839 config->pendingTokenLimit = |
838 document()->settings()->backgroundHtmlParserPendingTokenLimit(); | 840 document()->settings()->backgroundHtmlParserPendingTokenLimit(); |
839 config->shouldCoalesceChunks = | 841 config->shouldCoalesceChunks = |
840 document()->settings()->parseHTMLOnMainThreadCoalesceChunks(); | 842 document()->settings()->parseHTMLOnMainThreadCoalesceChunks(); |
841 } | 843 } |
842 | 844 |
843 ASSERT(config->xssAuditor->isSafeToSendToAnotherThread()); | 845 ASSERT(config->xssAuditor->isSafeToSendToAnotherThread()); |
844 | 846 |
845 // The background parser is created on the main thread, but may otherwise | 847 // The background parser is created on the main thread, but may otherwise |
846 // only be used from the parser thread. | 848 // only be used from the parser thread. |
847 m_backgroundParser = BackgroundHTMLParser::create( | 849 m_backgroundParser = BackgroundHTMLParser::create( |
848 std::move(config), m_loadingTaskRunner->clone()); | 850 std::move(config), m_loadingTaskRunner->clone()); |
849 // TODO(csharrison): This is a hack to initialize MediaValuesCached on the | 851 // TODO(csharrison): This is a hack to initialize MediaValuesCached on the |
850 // correct thread. We should get rid of it. | 852 // correct thread. We should get rid of it. |
851 postTaskToLookaheadParser( | 853 postTaskToLookaheadParser( |
852 Synchronous, &BackgroundHTMLParser::init, m_backgroundParser, | 854 Synchronous, &BackgroundHTMLParser::init, m_backgroundParser, |
853 document()->url(), passed(CachedDocumentParameters::create(document())), | 855 document()->url(), |
| 856 WTF::passed(CachedDocumentParameters::create(document())), |
854 MediaValuesCached::MediaValuesCachedData(*document())); | 857 MediaValuesCached::MediaValuesCachedData(*document())); |
855 } | 858 } |
856 | 859 |
857 void HTMLDocumentParser::stopBackgroundParser() { | 860 void HTMLDocumentParser::stopBackgroundParser() { |
858 ASSERT(shouldUseThreading()); | 861 ASSERT(shouldUseThreading()); |
859 ASSERT(m_haveBackgroundParser); | 862 ASSERT(m_haveBackgroundParser); |
860 | 863 |
861 if (m_haveBackgroundParser && document()->frame() && | 864 if (m_haveBackgroundParser && document()->frame() && |
862 document()->frame()->frameScheduler()) | 865 document()->frame()->frameScheduler()) |
863 document()->frame()->frameScheduler()->setDocumentParsingInBackground( | 866 document()->frame()->frameScheduler()->setDocumentParsingInBackground( |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 m_input.closeWithoutMarkingEndOfFile(); | 990 m_input.closeWithoutMarkingEndOfFile(); |
988 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::finish, | 991 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::finish, |
989 m_backgroundParser); | 992 m_backgroundParser); |
990 return; | 993 return; |
991 } | 994 } |
992 | 995 |
993 if (!m_tokenizer) { | 996 if (!m_tokenizer) { |
994 ASSERT(!m_token); | 997 ASSERT(!m_token); |
995 // We're finishing before receiving any data. Rather than booting up the | 998 // We're finishing before receiving any data. Rather than booting up the |
996 // background parser just to spin it down, we finish parsing synchronously. | 999 // background parser just to spin it down, we finish parsing synchronously. |
997 m_token = wrapUnique(new HTMLToken); | 1000 m_token = WTF::wrapUnique(new HTMLToken); |
998 m_tokenizer = HTMLTokenizer::create(m_options); | 1001 m_tokenizer = HTMLTokenizer::create(m_options); |
999 } | 1002 } |
1000 | 1003 |
1001 // We're not going to get any more data off the network, so we tell the input | 1004 // We're not going to get any more data off the network, so we tell the input |
1002 // stream we've reached the end of file. finish() can be called more than | 1005 // stream we've reached the end of file. finish() can be called more than |
1003 // once, if the first time does not call end(). | 1006 // once, if the first time does not call end(). |
1004 if (!m_input.haveSeenEndOfFile()) | 1007 if (!m_input.haveSeenEndOfFile()) |
1005 m_input.markEndOfFile(); | 1008 m_input.markEndOfFile(); |
1006 | 1009 |
1007 attemptToEnd(); | 1010 attemptToEnd(); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1139 | 1142 |
1140 void HTMLDocumentParser::appendBytes(const char* data, size_t length) { | 1143 void HTMLDocumentParser::appendBytes(const char* data, size_t length) { |
1141 if (!length || isStopped()) | 1144 if (!length || isStopped()) |
1142 return; | 1145 return; |
1143 | 1146 |
1144 if (shouldUseThreading()) { | 1147 if (shouldUseThreading()) { |
1145 double bytesReceivedTime = monotonicallyIncreasingTimeMS(); | 1148 double bytesReceivedTime = monotonicallyIncreasingTimeMS(); |
1146 if (!m_haveBackgroundParser) | 1149 if (!m_haveBackgroundParser) |
1147 startBackgroundParser(); | 1150 startBackgroundParser(); |
1148 | 1151 |
1149 std::unique_ptr<Vector<char>> buffer = makeUnique<Vector<char>>(length); | 1152 std::unique_ptr<Vector<char>> buffer = |
| 1153 WTF::makeUnique<Vector<char>>(length); |
1150 memcpy(buffer->data(), data, length); | 1154 memcpy(buffer->data(), data, length); |
1151 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("blink.debug"), | 1155 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("blink.debug"), |
1152 "HTMLDocumentParser::appendBytes", "size", (unsigned)length); | 1156 "HTMLDocumentParser::appendBytes", "size", (unsigned)length); |
1153 | 1157 |
1154 LookaheadParserTaskSynchrony policy = | 1158 LookaheadParserTaskSynchrony policy = |
1155 document()->settings() && | 1159 document()->settings() && |
1156 document()->settings()->parseHTMLOnMainThreadSyncTokenize() | 1160 document()->settings()->parseHTMLOnMainThreadSyncTokenize() |
1157 ? Synchronous | 1161 ? Synchronous |
1158 : Asynchronous; | 1162 : Asynchronous; |
1159 postTaskToLookaheadParser( | 1163 postTaskToLookaheadParser( |
1160 policy, &BackgroundHTMLParser::appendRawBytesFromMainThread, | 1164 policy, &BackgroundHTMLParser::appendRawBytesFromMainThread, |
1161 m_backgroundParser, passed(std::move(buffer)), bytesReceivedTime); | 1165 m_backgroundParser, WTF::passed(std::move(buffer)), bytesReceivedTime); |
1162 return; | 1166 return; |
1163 } | 1167 } |
1164 | 1168 |
1165 DecodedDataDocumentParser::appendBytes(data, length); | 1169 DecodedDataDocumentParser::appendBytes(data, length); |
1166 } | 1170 } |
1167 | 1171 |
1168 void HTMLDocumentParser::flush() { | 1172 void HTMLDocumentParser::flush() { |
1169 // If we've got no decoder, we never received any data. | 1173 // If we've got no decoder, we never received any data. |
1170 if (isDetached() || needsDecoder()) | 1174 if (isDetached() || needsDecoder()) |
1171 return; | 1175 return; |
1172 | 1176 |
1173 if (shouldUseThreading()) { | 1177 if (shouldUseThreading()) { |
1174 // In some cases, flush() is called without any invocation of appendBytes. | 1178 // In some cases, flush() is called without any invocation of appendBytes. |
1175 // Fallback to synchronous parsing in that case. | 1179 // Fallback to synchronous parsing in that case. |
1176 if (!m_haveBackgroundParser) { | 1180 if (!m_haveBackgroundParser) { |
1177 m_shouldUseThreading = false; | 1181 m_shouldUseThreading = false; |
1178 m_token = wrapUnique(new HTMLToken); | 1182 m_token = WTF::wrapUnique(new HTMLToken); |
1179 m_tokenizer = HTMLTokenizer::create(m_options); | 1183 m_tokenizer = HTMLTokenizer::create(m_options); |
1180 DecodedDataDocumentParser::flush(); | 1184 DecodedDataDocumentParser::flush(); |
1181 return; | 1185 return; |
1182 } | 1186 } |
1183 | 1187 |
1184 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::flush, | 1188 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::flush, |
1185 m_backgroundParser); | 1189 m_backgroundParser); |
1186 } else { | 1190 } else { |
1187 DecodedDataDocumentParser::flush(); | 1191 DecodedDataDocumentParser::flush(); |
1188 } | 1192 } |
1189 } | 1193 } |
1190 | 1194 |
1191 void HTMLDocumentParser::setDecoder( | 1195 void HTMLDocumentParser::setDecoder( |
1192 std::unique_ptr<TextResourceDecoder> decoder) { | 1196 std::unique_ptr<TextResourceDecoder> decoder) { |
1193 ASSERT(decoder); | 1197 ASSERT(decoder); |
1194 DecodedDataDocumentParser::setDecoder(std::move(decoder)); | 1198 DecodedDataDocumentParser::setDecoder(std::move(decoder)); |
1195 | 1199 |
1196 if (m_haveBackgroundParser) | 1200 if (m_haveBackgroundParser) { |
1197 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::setDecoder, | 1201 postTaskToLookaheadParser(Asynchronous, &BackgroundHTMLParser::setDecoder, |
1198 m_backgroundParser, passed(takeDecoder())); | 1202 m_backgroundParser, WTF::passed(takeDecoder())); |
| 1203 } |
1199 } | 1204 } |
1200 | 1205 |
1201 void HTMLDocumentParser::documentElementAvailable() { | 1206 void HTMLDocumentParser::documentElementAvailable() { |
1202 TRACE_EVENT0("blink,loader", "HTMLDocumentParser::documentElementAvailable"); | 1207 TRACE_EVENT0("blink,loader", "HTMLDocumentParser::documentElementAvailable"); |
1203 DCHECK(document()->documentElement()); | 1208 DCHECK(document()->documentElement()); |
1204 fetchQueuedPreloads(); | 1209 fetchQueuedPreloads(); |
1205 } | 1210 } |
1206 | 1211 |
1207 std::unique_ptr<HTMLPreloadScanner> HTMLDocumentParser::createPreloadScanner() { | 1212 std::unique_ptr<HTMLPreloadScanner> HTMLDocumentParser::createPreloadScanner() { |
1208 return HTMLPreloadScanner::create( | 1213 return HTMLPreloadScanner::create( |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1306 case Asynchronous: | 1311 case Asynchronous: |
1307 m_loadingTaskRunner->postTask( | 1312 m_loadingTaskRunner->postTask( |
1308 BLINK_FROM_HERE, | 1313 BLINK_FROM_HERE, |
1309 WTF::bind(function, std::forward<Ps>(parameters)...)); | 1314 WTF::bind(function, std::forward<Ps>(parameters)...)); |
1310 return; | 1315 return; |
1311 } | 1316 } |
1312 NOTREACHED(); | 1317 NOTREACHED(); |
1313 } | 1318 } |
1314 | 1319 |
1315 } // namespace blink | 1320 } // namespace blink |
OLD | NEW |