|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef nsHtml5TreeOpExecutor_h |
|
6 #define nsHtml5TreeOpExecutor_h |
|
7 |
|
8 #include "nsIAtom.h" |
|
9 #include "nsIContent.h" |
|
10 #include "nsIDocument.h" |
|
11 #include "nsTraceRefcnt.h" |
|
12 #include "nsHtml5TreeOperation.h" |
|
13 #include "nsHtml5SpeculativeLoad.h" |
|
14 #include "nsTArray.h" |
|
15 #include "nsContentSink.h" |
|
16 #include "nsNodeInfoManager.h" |
|
17 #include "nsHtml5DocumentMode.h" |
|
18 #include "nsIScriptElement.h" |
|
19 #include "nsIParser.h" |
|
20 #include "nsAHtml5TreeOpSink.h" |
|
21 #include "nsHtml5TreeOpStage.h" |
|
22 #include "nsIURI.h" |
|
23 #include "nsTHashtable.h" |
|
24 #include "nsHashKeys.h" |
|
25 #include "mozilla/LinkedList.h" |
|
26 #include "nsHtml5DocumentBuilder.h" |
|
27 |
|
28 class nsHtml5Parser; |
|
29 class nsHtml5TreeBuilder; |
|
30 class nsHtml5Tokenizer; |
|
31 class nsHtml5StreamParser; |
|
32 |
|
33 class nsHtml5TreeOpExecutor : public nsHtml5DocumentBuilder, |
|
34 public nsIContentSink, |
|
35 public nsAHtml5TreeOpSink, |
|
36 public mozilla::LinkedListElement<nsHtml5TreeOpExecutor> |
|
37 { |
|
38 friend class nsHtml5FlushLoopGuard; |
|
39 |
|
40 public: |
|
41 NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW |
|
42 NS_DECL_ISUPPORTS_INHERITED |
|
43 |
|
44 private: |
|
45 static bool sExternalViewSource; |
|
46 #ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH |
|
47 static uint32_t sAppendBatchMaxSize; |
|
48 static uint32_t sAppendBatchSlotsExamined; |
|
49 static uint32_t sAppendBatchExaminations; |
|
50 static uint32_t sLongestTimeOffTheEventLoop; |
|
51 static uint32_t sTimesFlushLoopInterrupted; |
|
52 #endif |
|
53 |
|
54 /** |
|
55 * Whether EOF needs to be suppressed |
|
56 */ |
|
57 bool mSuppressEOF; |
|
58 |
|
59 bool mReadingFromStage; |
|
60 nsTArray<nsHtml5TreeOperation> mOpQueue; |
|
61 nsHtml5StreamParser* mStreamParser; |
|
62 |
|
63 /** |
|
64 * URLs already preloaded/preloading. |
|
65 */ |
|
66 nsTHashtable<nsCStringHashKey> mPreloadedURLs; |
|
67 |
|
68 nsCOMPtr<nsIURI> mSpeculationBaseURI; |
|
69 |
|
70 nsCOMPtr<nsIURI> mViewSourceBaseURI; |
|
71 |
|
72 /** |
|
73 * Whether the parser has started |
|
74 */ |
|
75 bool mStarted; |
|
76 |
|
77 nsHtml5TreeOpStage mStage; |
|
78 |
|
79 bool mRunFlushLoopOnStack; |
|
80 |
|
81 bool mCallContinueInterruptedParsingIfEnabled; |
|
82 |
|
83 /** |
|
84 * Whether this executor has already complained about matters related |
|
85 * to character encoding declarations. |
|
86 */ |
|
87 bool mAlreadyComplainedAboutCharset; |
|
88 |
|
89 public: |
|
90 |
|
91 nsHtml5TreeOpExecutor(); |
|
92 virtual ~nsHtml5TreeOpExecutor(); |
|
93 |
|
94 // nsIContentSink |
|
95 |
|
96 /** |
|
97 * Unimplemented. For interface compat only. |
|
98 */ |
|
99 NS_IMETHOD WillParse(); |
|
100 |
|
101 /** |
|
102 * |
|
103 */ |
|
104 NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode); |
|
105 |
|
106 /** |
|
107 * Emits EOF. |
|
108 */ |
|
109 NS_IMETHOD DidBuildModel(bool aTerminated); |
|
110 |
|
111 /** |
|
112 * Forwards to nsContentSink |
|
113 */ |
|
114 NS_IMETHOD WillInterrupt(); |
|
115 |
|
116 /** |
|
117 * Unimplemented. For interface compat only. |
|
118 */ |
|
119 NS_IMETHOD WillResume(); |
|
120 |
|
121 /** |
|
122 * Sets the parser. |
|
123 */ |
|
124 NS_IMETHOD SetParser(nsParserBase* aParser); |
|
125 |
|
126 /** |
|
127 * No-op for backwards compat. |
|
128 */ |
|
129 virtual void FlushPendingNotifications(mozFlushType aType); |
|
130 |
|
131 /** |
|
132 * Don't call. For interface compat only. |
|
133 */ |
|
134 NS_IMETHOD SetDocumentCharset(nsACString& aCharset) { |
|
135 NS_NOTREACHED("No one should call this."); |
|
136 return NS_ERROR_NOT_IMPLEMENTED; |
|
137 } |
|
138 |
|
139 /** |
|
140 * Returns the document. |
|
141 */ |
|
142 virtual nsISupports *GetTarget(); |
|
143 |
|
144 virtual void ContinueInterruptedParsingAsync(); |
|
145 |
|
146 // XXX Does anyone need this? |
|
147 nsIDocShell* GetDocShell() |
|
148 { |
|
149 return mDocShell; |
|
150 } |
|
151 |
|
152 bool IsScriptExecuting() |
|
153 { |
|
154 return IsScriptExecutingImpl(); |
|
155 } |
|
156 |
|
157 // Not from interface |
|
158 |
|
159 void SetStreamParser(nsHtml5StreamParser* aStreamParser) |
|
160 { |
|
161 mStreamParser = aStreamParser; |
|
162 } |
|
163 |
|
164 void InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState, int32_t aLine); |
|
165 |
|
166 bool IsScriptEnabled(); |
|
167 |
|
168 virtual nsresult MarkAsBroken(nsresult aReason); |
|
169 |
|
170 void StartLayout(); |
|
171 |
|
172 void FlushSpeculativeLoads(); |
|
173 |
|
174 void RunFlushLoop(); |
|
175 |
|
176 nsresult FlushDocumentWrite(); |
|
177 |
|
178 void MaybeSuspend(); |
|
179 |
|
180 void Start(); |
|
181 |
|
182 void NeedsCharsetSwitchTo(const char* aEncoding, |
|
183 int32_t aSource, |
|
184 uint32_t aLineNumber); |
|
185 |
|
186 void MaybeComplainAboutCharset(const char* aMsgId, |
|
187 bool aError, |
|
188 uint32_t aLineNumber); |
|
189 |
|
190 void ComplainAboutBogusProtocolCharset(nsIDocument* aDoc); |
|
191 |
|
192 bool IsComplete() |
|
193 { |
|
194 return !mParser; |
|
195 } |
|
196 |
|
197 bool HasStarted() |
|
198 { |
|
199 return mStarted; |
|
200 } |
|
201 |
|
202 bool IsFlushing() |
|
203 { |
|
204 return mFlushState >= eInFlush; |
|
205 } |
|
206 |
|
207 #ifdef DEBUG |
|
208 bool IsInFlushLoop() |
|
209 { |
|
210 return mRunFlushLoopOnStack; |
|
211 } |
|
212 #endif |
|
213 |
|
214 void RunScript(nsIContent* aScriptElement); |
|
215 |
|
216 /** |
|
217 * Flush the operations from the tree operations from the argument |
|
218 * queue unconditionally. (This is for the main thread case.) |
|
219 */ |
|
220 virtual void MoveOpsFrom(nsTArray<nsHtml5TreeOperation>& aOpQueue); |
|
221 |
|
222 nsHtml5TreeOpStage* GetStage() |
|
223 { |
|
224 return &mStage; |
|
225 } |
|
226 |
|
227 void StartReadingFromStage() |
|
228 { |
|
229 mReadingFromStage = true; |
|
230 } |
|
231 |
|
232 void StreamEnded(); |
|
233 |
|
234 #ifdef DEBUG |
|
235 void AssertStageEmpty() |
|
236 { |
|
237 mStage.AssertEmpty(); |
|
238 } |
|
239 #endif |
|
240 |
|
241 nsIURI* GetViewSourceBaseURI(); |
|
242 |
|
243 void PreloadScript(const nsAString& aURL, |
|
244 const nsAString& aCharset, |
|
245 const nsAString& aType, |
|
246 const nsAString& aCrossOrigin, |
|
247 bool aScriptFromHead); |
|
248 |
|
249 void PreloadStyle(const nsAString& aURL, const nsAString& aCharset, |
|
250 const nsAString& aCrossOrigin); |
|
251 |
|
252 void PreloadImage(const nsAString& aURL, const nsAString& aCrossOrigin); |
|
253 |
|
254 void SetSpeculationBase(const nsAString& aURL); |
|
255 |
|
256 static void InitializeStatics(); |
|
257 |
|
258 private: |
|
259 nsHtml5Parser* GetParser(); |
|
260 |
|
261 bool IsExternalViewSource(); |
|
262 |
|
263 /** |
|
264 * Get a nsIURI for an nsString if the URL hasn't been preloaded yet. |
|
265 */ |
|
266 already_AddRefed<nsIURI> ConvertIfNotPreloadedYet(const nsAString& aURL); |
|
267 |
|
268 }; |
|
269 |
|
270 #endif // nsHtml5TreeOpExecutor_h |