1 | /* |
2 | * Copyright (C) 2010 Google, Inc. All Rights Reserved. |
3 | * Copyright (C) 2015 Apple Inc. All Rights Reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include "HTMLInputStream.h" |
30 | #include "HTMLScriptRunnerHost.h" |
31 | #include "HTMLSourceTracker.h" |
32 | #include "HTMLTokenizer.h" |
33 | #include "PendingScriptClient.h" |
34 | #include "ScriptableDocumentParser.h" |
35 | #include "XSSAuditor.h" |
36 | #include "XSSAuditorDelegate.h" |
37 | |
38 | namespace WebCore { |
39 | |
40 | class DocumentFragment; |
41 | class Element; |
42 | class HTMLDocument; |
43 | class HTMLParserScheduler; |
44 | class HTMLPreloadScanner; |
45 | class HTMLScriptRunner; |
46 | class HTMLTreeBuilder; |
47 | class HTMLResourcePreloader; |
48 | class PumpSession; |
49 | |
50 | class HTMLDocumentParser : public ScriptableDocumentParser, private HTMLScriptRunnerHost, private PendingScriptClient { |
51 | WTF_MAKE_FAST_ALLOCATED; |
52 | public: |
53 | static Ref<HTMLDocumentParser> create(HTMLDocument&); |
54 | virtual ~HTMLDocumentParser(); |
55 | |
56 | static void parseDocumentFragment(const String&, DocumentFragment&, Element& contextElement, ParserContentPolicy = AllowScriptingContent); |
57 | |
58 | // For HTMLParserScheduler. |
59 | void resumeParsingAfterYield(); |
60 | |
61 | // For HTMLTreeBuilder. |
62 | HTMLTokenizer& tokenizer(); |
63 | TextPosition textPosition() const final; |
64 | |
65 | protected: |
66 | explicit HTMLDocumentParser(HTMLDocument&); |
67 | |
68 | void insert(SegmentedString&&) final; |
69 | void append(RefPtr<StringImpl>&&) override; |
70 | void finish() override; |
71 | |
72 | HTMLTreeBuilder& treeBuilder(); |
73 | |
74 | private: |
75 | HTMLDocumentParser(DocumentFragment&, Element& contextElement, ParserContentPolicy); |
76 | static Ref<HTMLDocumentParser> create(DocumentFragment&, Element& contextElement, ParserContentPolicy); |
77 | |
78 | // DocumentParser |
79 | void detach() final; |
80 | bool hasInsertionPoint() final; |
81 | bool processingData() const final; |
82 | void prepareToStopParsing() final; |
83 | void stopParsing() final; |
84 | bool isWaitingForScripts() const override; |
85 | bool isExecutingScript() const final; |
86 | bool hasScriptsWaitingForStylesheets() const final; |
87 | void executeScriptsWaitingForStylesheets() final; |
88 | void suspendScheduledTasks() final; |
89 | void resumeScheduledTasks() final; |
90 | |
91 | bool shouldAssociateConsoleMessagesWithTextPosition() const final; |
92 | |
93 | // HTMLScriptRunnerHost |
94 | void watchForLoad(PendingScript&) final; |
95 | void stopWatchingForLoad(PendingScript&) final; |
96 | HTMLInputStream& inputStream() final; |
97 | bool hasPreloadScanner() const final; |
98 | void appendCurrentInputStreamToPreloadScannerAndScan() final; |
99 | |
100 | // PendingScriptClient |
101 | void notifyFinished(PendingScript&) final; |
102 | |
103 | Document* contextForParsingSession(); |
104 | |
105 | enum SynchronousMode { AllowYield, ForceSynchronous }; |
106 | void pumpTokenizer(SynchronousMode); |
107 | bool pumpTokenizerLoop(SynchronousMode, bool parsingFragment, PumpSession&); |
108 | void pumpTokenizerIfPossible(SynchronousMode); |
109 | void constructTreeFromHTMLToken(HTMLTokenizer::TokenPtr&); |
110 | |
111 | void runScriptsForPausedTreeBuilder(); |
112 | void resumeParsingAfterScriptExecution(); |
113 | |
114 | void attemptToEnd(); |
115 | void endIfDelayed(); |
116 | void attemptToRunDeferredScriptsAndEnd(); |
117 | void end(); |
118 | |
119 | bool isParsingFragment() const; |
120 | bool isScheduledForResume() const; |
121 | bool inPumpSession() const; |
122 | bool shouldDelayEnd() const; |
123 | |
124 | void didBeginYieldingParser() final; |
125 | void didEndYieldingParser() final; |
126 | |
127 | HTMLParserOptions m_options; |
128 | HTMLInputStream m_input; |
129 | |
130 | HTMLTokenizer m_tokenizer; |
131 | std::unique_ptr<HTMLScriptRunner> m_scriptRunner; |
132 | std::unique_ptr<HTMLTreeBuilder> m_treeBuilder; |
133 | std::unique_ptr<HTMLPreloadScanner> m_preloadScanner; |
134 | std::unique_ptr<HTMLPreloadScanner> m_insertionPreloadScanner; |
135 | std::unique_ptr<HTMLParserScheduler> m_parserScheduler; |
136 | HTMLSourceTracker m_sourceTracker; |
137 | TextPosition m_textPosition; |
138 | XSSAuditor m_xssAuditor; |
139 | XSSAuditorDelegate m_xssAuditorDelegate; |
140 | |
141 | std::unique_ptr<HTMLResourcePreloader> m_preloader; |
142 | |
143 | bool m_endWasDelayed { false }; |
144 | unsigned m_pumpSessionNestingLevel { 0 }; |
145 | }; |
146 | |
147 | inline HTMLTokenizer& HTMLDocumentParser::tokenizer() |
148 | { |
149 | return m_tokenizer; |
150 | } |
151 | |
152 | inline HTMLInputStream& HTMLDocumentParser::inputStream() |
153 | { |
154 | return m_input; |
155 | } |
156 | |
157 | inline bool HTMLDocumentParser::hasPreloadScanner() const |
158 | { |
159 | return m_preloadScanner.get(); |
160 | } |
161 | |
162 | inline HTMLTreeBuilder& HTMLDocumentParser::treeBuilder() |
163 | { |
164 | ASSERT(m_treeBuilder); |
165 | return *m_treeBuilder; |
166 | } |
167 | |
168 | } // namespace WebCore |
169 | |