| 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef HTMLDocumentParser_h | 26 #ifndef HTMLDocumentParser_h |
| 27 #define HTMLDocumentParser_h | 27 #define HTMLDocumentParser_h |
| 28 | 28 |
| 29 #include <memory> | |
| 30 #include "core/CoreExport.h" | 29 #include "core/CoreExport.h" |
| 31 #include "core/dom/ParserContentPolicy.h" | 30 #include "core/dom/ParserContentPolicy.h" |
| 32 #include "core/dom/ScriptableDocumentParser.h" | 31 #include "core/dom/ScriptableDocumentParser.h" |
| 33 #include "core/html/parser/BackgroundHTMLInputStream.h" | 32 #include "core/html/parser/BackgroundHTMLInputStream.h" |
| 34 #include "core/html/parser/HTMLInputStream.h" | 33 #include "core/html/parser/HTMLInputStream.h" |
| 35 #include "core/html/parser/HTMLParserOptions.h" | 34 #include "core/html/parser/HTMLParserOptions.h" |
| 36 #include "core/html/parser/HTMLParserReentryPermit.h" | 35 #include "core/html/parser/HTMLParserReentryPermit.h" |
| 37 #include "core/html/parser/HTMLParserScriptRunnerHost.h" | 36 #include "core/html/parser/HTMLParserScriptRunnerHost.h" |
| 38 #include "core/html/parser/HTMLPreloadScanner.h" | 37 #include "core/html/parser/HTMLPreloadScanner.h" |
| 39 #include "core/html/parser/HTMLSourceTracker.h" | 38 #include "core/html/parser/HTMLSourceTracker.h" |
| 40 #include "core/html/parser/HTMLToken.h" | 39 #include "core/html/parser/HTMLToken.h" |
| 41 #include "core/html/parser/HTMLTokenizer.h" | 40 #include "core/html/parser/HTMLTokenizer.h" |
| 42 #include "core/html/parser/HTMLTreeBuilderSimulator.h" | 41 #include "core/html/parser/HTMLTreeBuilderSimulator.h" |
| 43 #include "core/html/parser/ParserSynchronizationPolicy.h" | 42 #include "core/html/parser/ParserSynchronizationPolicy.h" |
| 44 #include "core/html/parser/PreloadRequest.h" | 43 #include "core/html/parser/PreloadRequest.h" |
| 45 #include "core/html/parser/TextResourceDecoder.h" | 44 #include "core/html/parser/TextResourceDecoder.h" |
| 46 #include "core/html/parser/XSSAuditor.h" | 45 #include "core/html/parser/XSSAuditor.h" |
| 47 #include "core/html/parser/XSSAuditorDelegate.h" | 46 #include "core/html/parser/XSSAuditorDelegate.h" |
| 48 #include "wtf/Deque.h" | 47 #include "wtf/Deque.h" |
| 49 #include "wtf/RefPtr.h" | 48 #include "wtf/RefPtr.h" |
| 50 #include "wtf/WeakPtr.h" | 49 #include "wtf/WeakPtr.h" |
| 51 #include "wtf/text/TextPosition.h" | 50 #include "wtf/text/TextPosition.h" |
| 51 #include <memory> |
| 52 | 52 |
| 53 namespace blink { | 53 namespace blink { |
| 54 | 54 |
| 55 class BackgroundHTMLParser; | 55 class BackgroundHTMLParser; |
| 56 class CompactHTMLToken; | 56 class CompactHTMLToken; |
| 57 class Document; | 57 class Document; |
| 58 class DocumentEncodingData; | 58 class DocumentEncodingData; |
| 59 class DocumentFragment; | 59 class DocumentFragment; |
| 60 class Element; | 60 class Element; |
| 61 class HTMLDocument; | 61 class HTMLDocument; |
| 62 class HTMLParserScheduler; | 62 class HTMLParserScheduler; |
| 63 class HTMLParserScriptRunner; | 63 class HTMLParserScriptRunner; |
| 64 class HTMLPreloadScanner; | 64 class HTMLPreloadScanner; |
| 65 class HTMLResourcePreloader; | 65 class HTMLResourcePreloader; |
| 66 class HTMLTreeBuilder; | 66 class HTMLTreeBuilder; |
| 67 class SegmentedString; | 67 class SegmentedString; |
| 68 class TokenizedChunkQueue; | 68 class TokenizedChunkQueue; |
| 69 class DocumentWriteEvaluator; | 69 class DocumentWriteEvaluator; |
| 70 | 70 |
| 71 class CORE_EXPORT HTMLDocumentParser : public ScriptableDocumentParser, | 71 class CORE_EXPORT HTMLDocumentParser : public ScriptableDocumentParser, |
| 72 private HTMLParserScriptRunnerHost { | 72 private HTMLParserScriptRunnerHost { |
| 73 USING_GARBAGE_COLLECTED_MIXIN(HTMLDocumentParser); | 73 USING_GARBAGE_COLLECTED_MIXIN(HTMLDocumentParser); |
| 74 USING_PRE_FINALIZER(HTMLDocumentParser, dispose); |
| 74 | 75 |
| 75 public: | 76 public: |
| 76 static HTMLDocumentParser* create( | 77 static HTMLDocumentParser* create( |
| 77 HTMLDocument& document, | 78 HTMLDocument& document, |
| 78 ParserSynchronizationPolicy backgroundParsingPolicy) { | 79 ParserSynchronizationPolicy backgroundParsingPolicy) { |
| 79 return new HTMLDocumentParser(document, backgroundParsingPolicy); | 80 return new HTMLDocumentParser(document, backgroundParsingPolicy); |
| 80 } | 81 } |
| 81 ~HTMLDocumentParser() override; | 82 ~HTMLDocumentParser() override; |
| 82 DECLARE_VIRTUAL_TRACE(); | 83 DECLARE_VIRTUAL_TRACE(); |
| 83 | 84 |
| 85 // TODO(alexclarke): Remove when background parser goes away. |
| 86 void dispose(); |
| 87 |
| 84 // Exposed for HTMLParserScheduler | 88 // Exposed for HTMLParserScheduler |
| 85 void resumeParsingAfterYield(); | 89 void resumeParsingAfterYield(); |
| 86 | 90 |
| 87 static void parseDocumentFragment( | 91 static void parseDocumentFragment( |
| 88 const String&, | 92 const String&, |
| 89 DocumentFragment*, | 93 DocumentFragment*, |
| 90 Element* contextElement, | 94 Element* contextElement, |
| 91 ParserContentPolicy = AllowScriptingContent); | 95 ParserContentPolicy = AllowScriptingContent); |
| 92 | 96 |
| 93 // Exposed for testing. | 97 // Exposed for testing. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 132 |
| 129 static constexpr int noPendingToken = -1; | 133 static constexpr int noPendingToken = -1; |
| 130 }; | 134 }; |
| 131 void notifyPendingTokenizedChunks(); | 135 void notifyPendingTokenizedChunks(); |
| 132 void didReceiveEncodingDataFromBackgroundParser(const DocumentEncodingData&); | 136 void didReceiveEncodingDataFromBackgroundParser(const DocumentEncodingData&); |
| 133 | 137 |
| 134 void appendBytes(const char* bytes, size_t length) override; | 138 void appendBytes(const char* bytes, size_t length) override; |
| 135 void flush() final; | 139 void flush() final; |
| 136 void setDecoder(std::unique_ptr<TextResourceDecoder>) final; | 140 void setDecoder(std::unique_ptr<TextResourceDecoder>) final; |
| 137 | 141 |
| 138 TokenizedChunkQueue* tokenizedChunkQueue() { | |
| 139 return m_tokenizedChunkQueue.get(); | |
| 140 } | |
| 141 | |
| 142 protected: | 142 protected: |
| 143 void insert(const SegmentedString&) final; | 143 void insert(const SegmentedString&) final; |
| 144 void append(const String&) override; | 144 void append(const String&) override; |
| 145 void finish() final; | 145 void finish() final; |
| 146 | 146 |
| 147 HTMLDocumentParser(HTMLDocument&, ParserSynchronizationPolicy); | 147 HTMLDocumentParser(HTMLDocument&, ParserSynchronizationPolicy); |
| 148 HTMLDocumentParser(DocumentFragment*, | 148 HTMLDocumentParser(DocumentFragment*, |
| 149 Element* contextElement, | 149 Element* contextElement, |
| 150 ParserContentPolicy); | 150 ParserContentPolicy); |
| 151 | 151 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 182 | 182 |
| 183 // HTMLParserScriptRunnerHost | 183 // HTMLParserScriptRunnerHost |
| 184 void notifyScriptLoaded(PendingScript*) final; | 184 void notifyScriptLoaded(PendingScript*) final; |
| 185 HTMLInputStream& inputStream() final { return m_input; } | 185 HTMLInputStream& inputStream() final { return m_input; } |
| 186 bool hasPreloadScanner() const final { | 186 bool hasPreloadScanner() const final { |
| 187 return m_preloadScanner.get() && !shouldUseThreading(); | 187 return m_preloadScanner.get() && !shouldUseThreading(); |
| 188 } | 188 } |
| 189 void appendCurrentInputStreamToPreloadScannerAndScan() final; | 189 void appendCurrentInputStreamToPreloadScannerAndScan() final; |
| 190 | 190 |
| 191 void startBackgroundParser(); | 191 void startBackgroundParser(); |
| 192 void stopBackgroundParser(); |
| 192 void validateSpeculations(std::unique_ptr<TokenizedChunk> lastChunk); | 193 void validateSpeculations(std::unique_ptr<TokenizedChunk> lastChunk); |
| 193 void discardSpeculationsAndResumeFrom( | 194 void discardSpeculationsAndResumeFrom( |
| 194 std::unique_ptr<TokenizedChunk> lastChunk, | 195 std::unique_ptr<TokenizedChunk> lastChunk, |
| 195 std::unique_ptr<HTMLToken>, | 196 std::unique_ptr<HTMLToken>, |
| 196 std::unique_ptr<HTMLTokenizer>); | 197 std::unique_ptr<HTMLTokenizer>); |
| 197 size_t processTokenizedChunkFromBackgroundParser( | 198 size_t processTokenizedChunkFromBackgroundParser( |
| 198 std::unique_ptr<TokenizedChunk>); | 199 std::unique_ptr<TokenizedChunk>); |
| 199 void pumpPendingSpeculations(); | 200 void pumpPendingSpeculations(); |
| 200 | 201 |
| 201 bool canTakeNextToken(); | 202 bool canTakeNextToken(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 | 240 |
| 240 std::unique_ptr<HTMLToken> m_token; | 241 std::unique_ptr<HTMLToken> m_token; |
| 241 std::unique_ptr<HTMLTokenizer> m_tokenizer; | 242 std::unique_ptr<HTMLTokenizer> m_tokenizer; |
| 242 Member<HTMLParserScriptRunner> m_scriptRunner; | 243 Member<HTMLParserScriptRunner> m_scriptRunner; |
| 243 Member<HTMLTreeBuilder> m_treeBuilder; | 244 Member<HTMLTreeBuilder> m_treeBuilder; |
| 244 | 245 |
| 245 std::unique_ptr<HTMLPreloadScanner> m_preloadScanner; | 246 std::unique_ptr<HTMLPreloadScanner> m_preloadScanner; |
| 246 // A scanner used only for input provided to the insert() method. | 247 // A scanner used only for input provided to the insert() method. |
| 247 std::unique_ptr<HTMLPreloadScanner> m_insertionPreloadScanner; | 248 std::unique_ptr<HTMLPreloadScanner> m_insertionPreloadScanner; |
| 248 | 249 |
| 250 RefPtr<WebTaskRunner> m_loadingTaskRunner; |
| 249 Member<HTMLParserScheduler> m_parserScheduler; | 251 Member<HTMLParserScheduler> m_parserScheduler; |
| 250 HTMLSourceTracker m_sourceTracker; | 252 HTMLSourceTracker m_sourceTracker; |
| 251 TextPosition m_textPosition; | 253 TextPosition m_textPosition; |
| 252 XSSAuditor m_xssAuditor; | 254 XSSAuditor m_xssAuditor; |
| 253 XSSAuditorDelegate m_xssAuditorDelegate; | 255 XSSAuditorDelegate m_xssAuditorDelegate; |
| 254 | 256 |
| 255 // FIXME: m_lastChunkBeforePause, m_tokenizer, m_token, and m_input should be | 257 // FIXME: m_lastChunkBeforePause, m_tokenizer, m_token, and m_input should be |
| 256 // combined into a single state object so they can be set and cleared together | 258 // combined into a single state object so they can be set and cleared together |
| 257 // and passed between threads together. | 259 // and passed between threads together. |
| 258 std::unique_ptr<TokenizedChunk> m_lastChunkBeforePause; | 260 std::unique_ptr<TokenizedChunk> m_lastChunkBeforePause; |
| 259 Deque<std::unique_ptr<TokenizedChunk>> m_speculations; | 261 Deque<std::unique_ptr<TokenizedChunk>> m_speculations; |
| 260 Member<BackgroundHTMLParser> m_backgroundParser; | 262 WeakPtrFactory<HTMLDocumentParser> m_weakFactory; |
| 263 WeakPtr<BackgroundHTMLParser> m_backgroundParser; |
| 261 Member<HTMLResourcePreloader> m_preloader; | 264 Member<HTMLResourcePreloader> m_preloader; |
| 262 PreloadRequestStream m_queuedPreloads; | 265 PreloadRequestStream m_queuedPreloads; |
| 263 Vector<String> m_queuedDocumentWriteScripts; | 266 Vector<String> m_queuedDocumentWriteScripts; |
| 264 RefPtr<TokenizedChunkQueue> m_tokenizedChunkQueue; | 267 RefPtr<TokenizedChunkQueue> m_tokenizedChunkQueue; |
| 265 std::unique_ptr<DocumentWriteEvaluator> m_evaluator; | 268 std::unique_ptr<DocumentWriteEvaluator> m_evaluator; |
| 266 | 269 |
| 267 // If this is non-null, then there is a meta CSP token somewhere in the | 270 // If this is non-null, then there is a meta CSP token somewhere in the |
| 268 // speculation buffer. Preloads will be deferred until a token matching this | 271 // speculation buffer. Preloads will be deferred until a token matching this |
| 269 // pointer is parsed and the CSP policy is applied. Note that this pointer | 272 // pointer is parsed and the CSP policy is applied. Note that this pointer |
| 270 // tracks the *last* meta token in the speculation buffer, so it overestimates | 273 // tracks the *last* meta token in the speculation buffer, so it overestimates |
| 271 // how long to defer preloads. This is for simplicity, as the alternative | 274 // how long to defer preloads. This is for simplicity, as the alternative |
| 272 // would require keeping track of token positions of preload requests. | 275 // would require keeping track of token positions of preload requests. |
| 273 CompactHTMLToken* m_pendingCSPMetaToken; | 276 CompactHTMLToken* m_pendingCSPMetaToken; |
| 274 | 277 |
| 275 TaskHandle m_resumeParsingTaskHandle; | 278 TaskHandle m_resumeParsingTaskHandle; |
| 276 | 279 |
| 277 bool m_shouldUseThreading; | 280 bool m_shouldUseThreading; |
| 278 bool m_endWasDelayed; | 281 bool m_endWasDelayed; |
| 282 bool m_haveBackgroundParser; |
| 279 bool m_tasksWereSuspended; | 283 bool m_tasksWereSuspended; |
| 280 unsigned m_pumpSessionNestingLevel; | 284 unsigned m_pumpSessionNestingLevel; |
| 281 unsigned m_pumpSpeculationsSessionNestingLevel; | 285 unsigned m_pumpSpeculationsSessionNestingLevel; |
| 282 bool m_isParsingAtLineNumber; | 286 bool m_isParsingAtLineNumber; |
| 283 bool m_triedLoadingLinkHeaders; | 287 bool m_triedLoadingLinkHeaders; |
| 284 bool m_addedPendingStylesheetInBody; | 288 bool m_addedPendingStylesheetInBody; |
| 285 bool m_isWaitingForStylesheets; | 289 bool m_isWaitingForStylesheets; |
| 286 }; | 290 }; |
| 287 | 291 |
| 288 } // namespace blink | 292 } // namespace blink |
| 289 | 293 |
| 290 #endif // HTMLDocumentParser_h | 294 #endif // HTMLDocumentParser_h |
| OLD | NEW |