Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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/. */
5 #ifndef nsHtml5TreeOpExecutor_h
6 #define nsHtml5TreeOpExecutor_h
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"
28 class nsHtml5Parser;
29 class nsHtml5TreeBuilder;
30 class nsHtml5Tokenizer;
31 class nsHtml5StreamParser;
33 class nsHtml5TreeOpExecutor : public nsHtml5DocumentBuilder,
34 public nsIContentSink,
35 public nsAHtml5TreeOpSink,
36 public mozilla::LinkedListElement<nsHtml5TreeOpExecutor>
37 {
38 friend class nsHtml5FlushLoopGuard;
40 public:
41 NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
42 NS_DECL_ISUPPORTS_INHERITED
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
54 /**
55 * Whether EOF needs to be suppressed
56 */
57 bool mSuppressEOF;
59 bool mReadingFromStage;
60 nsTArray<nsHtml5TreeOperation> mOpQueue;
61 nsHtml5StreamParser* mStreamParser;
63 /**
64 * URLs already preloaded/preloading.
65 */
66 nsTHashtable<nsCStringHashKey> mPreloadedURLs;
68 nsCOMPtr<nsIURI> mSpeculationBaseURI;
70 nsCOMPtr<nsIURI> mViewSourceBaseURI;
72 /**
73 * Whether the parser has started
74 */
75 bool mStarted;
77 nsHtml5TreeOpStage mStage;
79 bool mRunFlushLoopOnStack;
81 bool mCallContinueInterruptedParsingIfEnabled;
83 /**
84 * Whether this executor has already complained about matters related
85 * to character encoding declarations.
86 */
87 bool mAlreadyComplainedAboutCharset;
89 public:
91 nsHtml5TreeOpExecutor();
92 virtual ~nsHtml5TreeOpExecutor();
94 // nsIContentSink
96 /**
97 * Unimplemented. For interface compat only.
98 */
99 NS_IMETHOD WillParse();
101 /**
102 *
103 */
104 NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
106 /**
107 * Emits EOF.
108 */
109 NS_IMETHOD DidBuildModel(bool aTerminated);
111 /**
112 * Forwards to nsContentSink
113 */
114 NS_IMETHOD WillInterrupt();
116 /**
117 * Unimplemented. For interface compat only.
118 */
119 NS_IMETHOD WillResume();
121 /**
122 * Sets the parser.
123 */
124 NS_IMETHOD SetParser(nsParserBase* aParser);
126 /**
127 * No-op for backwards compat.
128 */
129 virtual void FlushPendingNotifications(mozFlushType aType);
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 }
139 /**
140 * Returns the document.
141 */
142 virtual nsISupports *GetTarget();
144 virtual void ContinueInterruptedParsingAsync();
146 // XXX Does anyone need this?
147 nsIDocShell* GetDocShell()
148 {
149 return mDocShell;
150 }
152 bool IsScriptExecuting()
153 {
154 return IsScriptExecutingImpl();
155 }
157 // Not from interface
159 void SetStreamParser(nsHtml5StreamParser* aStreamParser)
160 {
161 mStreamParser = aStreamParser;
162 }
164 void InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState, int32_t aLine);
166 bool IsScriptEnabled();
168 virtual nsresult MarkAsBroken(nsresult aReason);
170 void StartLayout();
172 void FlushSpeculativeLoads();
174 void RunFlushLoop();
176 nsresult FlushDocumentWrite();
178 void MaybeSuspend();
180 void Start();
182 void NeedsCharsetSwitchTo(const char* aEncoding,
183 int32_t aSource,
184 uint32_t aLineNumber);
186 void MaybeComplainAboutCharset(const char* aMsgId,
187 bool aError,
188 uint32_t aLineNumber);
190 void ComplainAboutBogusProtocolCharset(nsIDocument* aDoc);
192 bool IsComplete()
193 {
194 return !mParser;
195 }
197 bool HasStarted()
198 {
199 return mStarted;
200 }
202 bool IsFlushing()
203 {
204 return mFlushState >= eInFlush;
205 }
207 #ifdef DEBUG
208 bool IsInFlushLoop()
209 {
210 return mRunFlushLoopOnStack;
211 }
212 #endif
214 void RunScript(nsIContent* aScriptElement);
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);
222 nsHtml5TreeOpStage* GetStage()
223 {
224 return &mStage;
225 }
227 void StartReadingFromStage()
228 {
229 mReadingFromStage = true;
230 }
232 void StreamEnded();
234 #ifdef DEBUG
235 void AssertStageEmpty()
236 {
237 mStage.AssertEmpty();
238 }
239 #endif
241 nsIURI* GetViewSourceBaseURI();
243 void PreloadScript(const nsAString& aURL,
244 const nsAString& aCharset,
245 const nsAString& aType,
246 const nsAString& aCrossOrigin,
247 bool aScriptFromHead);
249 void PreloadStyle(const nsAString& aURL, const nsAString& aCharset,
250 const nsAString& aCrossOrigin);
252 void PreloadImage(const nsAString& aURL, const nsAString& aCrossOrigin);
254 void SetSpeculationBase(const nsAString& aURL);
256 static void InitializeStatics();
258 private:
259 nsHtml5Parser* GetParser();
261 bool IsExternalViewSource();
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);
268 };
270 #endif // nsHtml5TreeOpExecutor_h