Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/parser/html/nsHtml5TreeOpExecutor.h
Line
Count
Source (jump to first uncovered line)
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 "nsAtom.h"
9
#include "nsTraceRefcnt.h"
10
#include "nsHtml5TreeOperation.h"
11
#include "nsHtml5SpeculativeLoad.h"
12
#include "nsTArray.h"
13
#include "nsContentSink.h"
14
#include "nsNodeInfoManager.h"
15
#include "nsHtml5DocumentMode.h"
16
#include "nsIScriptElement.h"
17
#include "nsIParser.h"
18
#include "nsAHtml5TreeOpSink.h"
19
#include "nsHtml5TreeOpStage.h"
20
#include "nsIURI.h"
21
#include "nsTHashtable.h"
22
#include "nsHashKeys.h"
23
#include "mozilla/LinkedList.h"
24
#include "nsHtml5DocumentBuilder.h"
25
#include "mozilla/net/ReferrerPolicy.h"
26
27
class nsHtml5Parser;
28
class nsHtml5StreamParser;
29
class nsIContent;
30
class nsIDocument;
31
32
class nsHtml5TreeOpExecutor final
33
  : public nsHtml5DocumentBuilder
34
  , public nsIContentSink
35
  , public nsAHtml5TreeOpSink
36
  , public mozilla::LinkedListElement<nsHtml5TreeOpExecutor>
37
{
38
  friend class nsHtml5FlushLoopGuard;
39
  typedef mozilla::net::ReferrerPolicy ReferrerPolicy;
40
  using Encoding = mozilla::Encoding;
41
  template<typename T>
42
  using NotNull = mozilla::NotNull<T>;
43
44
public:
45
  NS_DECL_ISUPPORTS_INHERITED
46
47
private:
48
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
49
  static uint32_t sAppendBatchMaxSize;
50
  static uint32_t sAppendBatchSlotsExamined;
51
  static uint32_t sAppendBatchExaminations;
52
  static uint32_t sLongestTimeOffTheEventLoop;
53
  static uint32_t sTimesFlushLoopInterrupted;
54
#endif
55
56
  /**
57
   * Whether EOF needs to be suppressed
58
   */
59
  bool mSuppressEOF;
60
61
  bool mReadingFromStage;
62
  nsTArray<nsHtml5TreeOperation> mOpQueue;
63
  nsHtml5StreamParser* mStreamParser;
64
65
  /**
66
   * URLs already preloaded/preloading.
67
   */
68
  nsTHashtable<nsCStringHashKey> mPreloadedURLs;
69
70
  nsCOMPtr<nsIURI> mSpeculationBaseURI;
71
72
  /**
73
   * Speculative referrer policy
74
   */
75
  ReferrerPolicy mSpeculationReferrerPolicy;
76
77
  nsCOMPtr<nsIURI> mViewSourceBaseURI;
78
79
  /**
80
   * Whether the parser has started
81
   */
82
  bool mStarted;
83
84
  nsHtml5TreeOpStage mStage;
85
86
  bool mRunFlushLoopOnStack;
87
88
  bool mCallContinueInterruptedParsingIfEnabled;
89
90
  /**
91
   * Whether this executor has already complained about matters related
92
   * to character encoding declarations.
93
   */
94
  bool mAlreadyComplainedAboutCharset;
95
96
public:
97
  nsHtml5TreeOpExecutor();
98
99
protected:
100
  virtual ~nsHtml5TreeOpExecutor();
101
102
public:
103
  // nsIContentSink
104
105
  /**
106
   * Unimplemented. For interface compat only.
107
   */
108
  NS_IMETHOD WillParse() override;
109
110
  /**
111
   *
112
   */
113
  NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode) override;
114
115
  /**
116
   * Emits EOF.
117
   */
118
  NS_IMETHOD DidBuildModel(bool aTerminated) override;
119
120
  /**
121
   * Forwards to nsContentSink
122
   */
123
  NS_IMETHOD WillInterrupt() override;
124
125
  /**
126
   * Unimplemented. For interface compat only.
127
   */
128
  NS_IMETHOD WillResume() override;
129
130
  /**
131
   * Sets the parser.
132
   */
133
  NS_IMETHOD SetParser(nsParserBase* aParser) override;
134
135
  /**
136
   * No-op for backwards compat.
137
   */
138
  virtual void FlushPendingNotifications(mozilla::FlushType aType) override;
139
140
  /**
141
   * Don't call. For interface compat only.
142
   */
143
  virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override
144
0
  {
145
0
    MOZ_ASSERT_UNREACHABLE("No one should call this.");
146
0
  }
147
148
  /**
149
   * Returns the document.
150
   */
151
  virtual nsISupports* GetTarget() override;
152
153
  virtual void ContinueInterruptedParsingAsync() override;
154
155
0
  bool IsScriptExecuting() override { return IsScriptExecutingImpl(); }
156
157
  // Not from interface
158
159
  void SetStreamParser(nsHtml5StreamParser* aStreamParser)
160
0
  {
161
0
    mStreamParser = aStreamParser;
162
0
  }
163
164
  void InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState,
165
                                     int32_t aLine);
166
167
  bool IsScriptEnabled();
168
169
  virtual nsresult MarkAsBroken(nsresult aReason) override;
170
171
  void StartLayout(bool* aInterrupted);
172
173
  void PauseDocUpdate(bool* aInterrupted);
174
175
  void FlushSpeculativeLoads();
176
177
  void RunFlushLoop();
178
179
  nsresult FlushDocumentWrite();
180
181
  void MaybeSuspend();
182
183
  void Start();
184
185
  void NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding,
186
                            int32_t aSource,
187
                            uint32_t aLineNumber);
188
189
  void MaybeComplainAboutCharset(const char* aMsgId,
190
                                 bool aError,
191
                                 uint32_t aLineNumber);
192
193
  void ComplainAboutBogusProtocolCharset(nsIDocument* aDoc);
194
195
0
  bool HasStarted() { return mStarted; }
196
197
0
  bool IsFlushing() { return mFlushState >= eInFlush; }
198
199
#ifdef DEBUG
200
  bool IsInFlushLoop() { return mRunFlushLoopOnStack; }
201
#endif
202
203
  void RunScript(nsIContent* aScriptElement);
204
205
  /**
206
   * Flush the operations from the tree operations from the argument
207
   * queue unconditionally. (This is for the main thread case.)
208
   */
209
  virtual void MoveOpsFrom(nsTArray<nsHtml5TreeOperation>& aOpQueue) override;
210
211
  void ClearOpQueue();
212
213
  void RemoveFromStartOfOpQueue(size_t aNumberOfOpsToRemove);
214
215
0
  inline size_t OpQueueLength() { return mOpQueue.Length(); }
216
217
0
  nsHtml5TreeOpStage* GetStage() { return &mStage; }
218
219
0
  void StartReadingFromStage() { mReadingFromStage = true; }
220
221
  void StreamEnded();
222
223
#ifdef DEBUG
224
  void AssertStageEmpty() { mStage.AssertEmpty(); }
225
#endif
226
227
  nsIURI* GetViewSourceBaseURI();
228
229
  void PreloadScript(const nsAString& aURL,
230
                     const nsAString& aCharset,
231
                     const nsAString& aType,
232
                     const nsAString& aCrossOrigin,
233
                     const nsAString& aIntegrity,
234
                     bool aScriptFromHead,
235
                     bool aAsync,
236
                     bool aDefer,
237
                     bool aNoModule);
238
239
  void PreloadStyle(const nsAString& aURL,
240
                    const nsAString& aCharset,
241
                    const nsAString& aCrossOrigin,
242
                    const nsAString& aReferrerPolicy,
243
                    const nsAString& aIntegrity);
244
245
  void PreloadImage(const nsAString& aURL,
246
                    const nsAString& aCrossOrigin,
247
                    const nsAString& aSrcset,
248
                    const nsAString& aSizes,
249
                    const nsAString& aImageReferrerPolicy);
250
251
  void PreloadOpenPicture();
252
253
  void PreloadEndPicture();
254
255
  void PreloadPictureSource(const nsAString& aSrcset,
256
                            const nsAString& aSizes,
257
                            const nsAString& aType,
258
                            const nsAString& aMedia);
259
260
  void SetSpeculationBase(const nsAString& aURL);
261
262
  void SetSpeculationReferrerPolicy(ReferrerPolicy aReferrerPolicy);
263
  void SetSpeculationReferrerPolicy(const nsAString& aReferrerPolicy);
264
265
  void AddSpeculationCSP(const nsAString& aCSP);
266
267
  void AddBase(const nsAString& aURL);
268
269
private:
270
  nsHtml5Parser* GetParser();
271
272
  bool IsExternalViewSource();
273
274
  /**
275
   * Get a nsIURI for an nsString if the URL hasn't been preloaded yet.
276
   */
277
  already_AddRefed<nsIURI> ConvertIfNotPreloadedYet(const nsAString& aURL);
278
279
  /**
280
   * The base URI we would use for current preload operations
281
   */
282
  nsIURI* BaseURIForPreload();
283
284
  /**
285
   * Returns true if we haven't preloaded this URI yet, and adds it to the
286
   * list of preloaded URIs
287
   */
288
  bool ShouldPreloadURI(nsIURI* aURI);
289
};
290
291
#endif // nsHtml5TreeOpExecutor_h