Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/html/nsHTMLDocument.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
#ifndef nsHTMLDocument_h___
7
#define nsHTMLDocument_h___
8
9
#include "mozilla/Attributes.h"
10
#include "nsDocument.h"
11
#include "nsIHTMLDocument.h"
12
#include "nsIHTMLCollection.h"
13
#include "nsIScriptElement.h"
14
#include "nsTArray.h"
15
16
#include "PLDHashTable.h"
17
#include "nsIHttpChannel.h"
18
#include "nsThreadUtils.h"
19
#include "nsICommandManager.h"
20
#include "mozilla/dom/HTMLSharedElement.h"
21
#include "mozilla/dom/BindingDeclarations.h"
22
23
class nsIURI;
24
class nsIDocShell;
25
class nsICachingChannel;
26
class nsIWyciwygChannel;
27
class nsILoadGroup;
28
29
namespace mozilla {
30
namespace dom {
31
class HTMLAllCollection;
32
} // namespace dom
33
} // namespace mozilla
34
35
class nsHTMLDocument : public nsDocument,
36
                       public nsIHTMLDocument
37
{
38
public:
39
  using nsDocument::SetDocumentURI;
40
  using nsDocument::GetPlugins;
41
42
  nsHTMLDocument();
43
  virtual nsresult Init() override;
44
45
  NS_DECL_ISUPPORTS_INHERITED
46
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHTMLDocument, nsDocument)
47
48
  // nsIDocument
49
  virtual void Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) override;
50
  virtual void ResetToURI(nsIURI* aURI, nsILoadGroup* aLoadGroup,
51
                          nsIPrincipal* aPrincipal) override;
52
53
  virtual nsresult StartDocumentLoad(const char* aCommand,
54
                                     nsIChannel* aChannel,
55
                                     nsILoadGroup* aLoadGroup,
56
                                     nsISupports* aContainer,
57
                                     nsIStreamListener **aDocListener,
58
                                     bool aReset = true,
59
                                     nsIContentSink* aSink = nullptr) override;
60
  virtual void StopDocumentLoad() override;
61
62
  virtual void BeginLoad() override;
63
  virtual void EndLoad() override;
64
65
  // nsIHTMLDocument
66
  virtual void SetCompatibilityMode(nsCompatibility aMode) override;
67
68
  virtual bool IsWriting() override
69
0
  {
70
0
    return mWriteLevel != uint32_t(0);
71
0
  }
72
73
  virtual nsIContent* GetUnfocusedKeyEventTarget() override;
74
75
  nsContentList* GetExistingForms() const
76
0
  {
77
0
    return mForms;
78
0
  }
79
80
  mozilla::dom::HTMLAllCollection* All();
81
82
  // Returns whether an object was found for aName.
83
  bool ResolveName(JSContext* aCx, const nsAString& aName,
84
                   JS::MutableHandle<JS::Value> aRetval, mozilla::ErrorResult& aError);
85
86
  virtual void AddedForm() override;
87
  virtual void RemovedForm() override;
88
  virtual int32_t GetNumFormsSynchronous() override;
89
  virtual void TearingDownEditor() override;
90
  virtual void SetIsXHTML(bool aXHTML) override
91
0
  {
92
0
    mType = (aXHTML ? eXHTML : eHTML);
93
0
  }
94
  virtual void SetDocWriteDisabled(bool aDisabled) override
95
0
  {
96
0
    mDisableDocWrite = aDisabled;
97
0
  }
98
99
  nsresult ChangeContentEditableCount(nsIContent *aElement, int32_t aChange) override;
100
  void DeferredContentEditableCountChange(nsIContent *aElement);
101
102
  virtual EditingState GetEditingState() override
103
0
  {
104
0
    return mEditingState;
105
0
  }
106
107
  virtual void DisableCookieAccess() override
108
0
  {
109
0
    mDisableCookieAccess = true;
110
0
  }
111
112
  class nsAutoEditingState {
113
  public:
114
    nsAutoEditingState(nsHTMLDocument* aDoc, EditingState aState)
115
      : mDoc(aDoc), mSavedState(aDoc->mEditingState)
116
0
    {
117
0
      aDoc->mEditingState = aState;
118
0
    }
119
0
    ~nsAutoEditingState() {
120
0
      mDoc->mEditingState = mSavedState;
121
0
    }
122
  private:
123
    nsHTMLDocument* mDoc;
124
    EditingState    mSavedState;
125
  };
126
  friend class nsAutoEditingState;
127
128
  void EndUpdate() override;
129
130
  virtual void SetMayStartLayout(bool aMayStartLayout) override;
131
132
  virtual nsresult SetEditingState(EditingState aState) override;
133
134
  virtual nsresult Clone(mozilla::dom::NodeInfo*, nsINode** aResult) const override;
135
136
  virtual void RemovedFromDocShell() override;
137
  using mozilla::dom::DocumentOrShadowRoot::GetElementById;
138
139
  virtual void DocAddSizeOfExcludingThis(nsWindowSizes& aWindowSizes) const override;
140
  // DocAddSizeOfIncludingThis is inherited from nsIDocument.
141
142
  virtual bool WillIgnoreCharsetOverride() override;
143
144
  // WebIDL API
145
  virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
146
    override;
147
  void GetDomain(nsAString& aDomain);
148
  void SetDomain(const nsAString& aDomain, mozilla::ErrorResult& rv);
149
  bool IsRegistrableDomainSuffixOfOrEqualTo(const nsAString& aHostSuffixString,
150
                                            const nsACString& aOrigHost);
151
  void GetCookie(nsAString& aCookie, mozilla::ErrorResult& rv);
152
  void SetCookie(const nsAString& aCookie, mozilla::ErrorResult& rv);
153
  void NamedGetter(JSContext* cx, const nsAString& aName, bool& aFound,
154
                   JS::MutableHandle<JSObject*> aRetval,
155
                   mozilla::ErrorResult& rv)
156
0
  {
157
0
    JS::Rooted<JS::Value> v(cx);
158
0
    if ((aFound = ResolveName(cx, aName, &v, rv))) {
159
0
      aRetval.set(v.toObjectOrNull());
160
0
    }
161
0
  }
162
  void GetSupportedNames(nsTArray<nsString>& aNames);
163
  already_AddRefed<nsIDocument> Open(JSContext* cx,
164
                                     const mozilla::dom::Optional<nsAString>& /* unused */,
165
                                     const nsAString& aReplace,
166
                                     mozilla::ErrorResult& aError);
167
  already_AddRefed<nsPIDOMWindowOuter>
168
  Open(JSContext* cx,
169
       const nsAString& aURL,
170
       const nsAString& aName,
171
       const nsAString& aFeatures,
172
       bool aReplace,
173
       mozilla::ErrorResult& rv);
174
  void Close(mozilla::ErrorResult& rv);
175
  void Write(JSContext* cx, const mozilla::dom::Sequence<nsString>& aText,
176
             mozilla::ErrorResult& rv);
177
  void Writeln(JSContext* cx, const mozilla::dom::Sequence<nsString>& aText,
178
               mozilla::ErrorResult& rv);
179
  void GetDesignMode(nsAString& aDesignMode);
180
  void SetDesignMode(const nsAString& aDesignMode,
181
                     nsIPrincipal& aSubjectPrincipal,
182
                     mozilla::ErrorResult& rv);
183
  void SetDesignMode(const nsAString& aDesignMode,
184
                     const mozilla::Maybe<nsIPrincipal*>& aSubjectPrincipal,
185
                     mozilla::ErrorResult& rv);
186
  bool ExecCommand(const nsAString& aCommandID, bool aDoShowUI,
187
                   const nsAString& aValue,
188
                   nsIPrincipal& aSubjectPrincipal,
189
                   mozilla::ErrorResult& rv);
190
  bool QueryCommandEnabled(const nsAString& aCommandID,
191
                           nsIPrincipal& aSubjectPrincipal,
192
                           mozilla::ErrorResult& rv);
193
  bool QueryCommandIndeterm(const nsAString& aCommandID,
194
                            mozilla::ErrorResult& rv);
195
  bool QueryCommandState(const nsAString& aCommandID, mozilla::ErrorResult& rv);
196
  bool QueryCommandSupported(const nsAString& aCommandID,
197
                             mozilla::dom::CallerType aCallerType);
198
  void QueryCommandValue(const nsAString& aCommandID, nsAString& aValue,
199
                         mozilla::ErrorResult& rv);
200
  void GetFgColor(nsAString& aFgColor);
201
  void SetFgColor(const nsAString& aFgColor);
202
  void GetLinkColor(nsAString& aLinkColor);
203
  void SetLinkColor(const nsAString& aLinkColor);
204
  void GetVlinkColor(nsAString& aAvlinkColor);
205
  void SetVlinkColor(const nsAString& aVlinkColor);
206
  void GetAlinkColor(nsAString& aAlinkColor);
207
  void SetAlinkColor(const nsAString& aAlinkColor);
208
  void GetBgColor(nsAString& aBgColor);
209
  void SetBgColor(const nsAString& aBgColor);
210
  void Clear() const
211
0
  {
212
0
    // Deprecated
213
0
  }
214
  void CaptureEvents();
215
  void ReleaseEvents();
216
  // We're picking up GetLocation from Document
217
  already_AddRefed<mozilla::dom::Location> GetLocation() const
218
0
  {
219
0
    return nsIDocument::GetLocation();
220
0
  }
221
222
  static bool MatchFormControls(Element* aElement, int32_t aNamespaceID,
223
                                nsAtom* aAtom, void* aData);
224
225
  void GetFormsAndFormControls(nsContentList** aFormList,
226
                               nsContentList** aFormControlList);
227
228
  void UserInteractionForTesting();
229
230
protected:
231
  ~nsHTMLDocument();
232
233
  nsresult GetBodySize(int32_t* aWidth,
234
                       int32_t* aHeight);
235
236
  nsIContent *MatchId(nsIContent *aContent, const nsAString& aId);
237
238
  static void DocumentWriteTerminationFunc(nsISupports *aRef);
239
240
  already_AddRefed<nsIURI> GetDomainURI();
241
  already_AddRefed<nsIURI> CreateInheritingURIForHost(const nsACString& aHostString);
242
  already_AddRefed<nsIURI> RegistrableDomainSuffixOfInternal(const nsAString& aHostSuffixString,
243
                                                             nsIURI* aOrigHost);
244
245
246
  void WriteCommon(JSContext *cx, const nsAString& aText,
247
                   bool aNewlineTerminate, mozilla::ErrorResult& aRv);
248
  // A version of WriteCommon used by WebIDL bindings
249
  void WriteCommon(JSContext *cx,
250
                   const mozilla::dom::Sequence<nsString>& aText,
251
                   bool aNewlineTerminate,
252
                   mozilla::ErrorResult& rv);
253
254
  nsresult CreateAndAddWyciwygChannel(void);
255
  nsresult RemoveWyciwygChannel(void);
256
257
  // This should *ONLY* be used in GetCookie/SetCookie.
258
  already_AddRefed<nsIChannel> CreateDummyChannelForCookies(nsIURI* aCodebaseURI);
259
260
  /**
261
   * Like IsEditingOn(), but will flush as needed first.
262
   */
263
  bool IsEditingOnAfterFlush();
264
265
  void *GenerateParserKey(void);
266
267
  // A helper class to keep nsContentList objects alive for a short period of
268
  // time. Note, when the final Release is called on an nsContentList object, it
269
  // removes itself from MutationObserver list.
270
  class ContentListHolder : public mozilla::Runnable
271
  {
272
  public:
273
    ContentListHolder(nsHTMLDocument* aDocument,
274
                      nsContentList* aFormList,
275
                      nsContentList* aFormControlList)
276
      : mozilla::Runnable("ContentListHolder")
277
      , mDocument(aDocument)
278
      , mFormList(aFormList)
279
      , mFormControlList(aFormControlList)
280
0
    {
281
0
    }
282
283
    ~ContentListHolder()
284
0
    {
285
0
      MOZ_ASSERT(!mDocument->mContentListHolder ||
286
0
                 mDocument->mContentListHolder == this);
287
0
      mDocument->mContentListHolder = nullptr;
288
0
    }
289
290
    RefPtr<nsHTMLDocument> mDocument;
291
    RefPtr<nsContentList> mFormList;
292
    RefPtr<nsContentList> mFormControlList;
293
  };
294
295
  friend class ContentListHolder;
296
  ContentListHolder* mContentListHolder;
297
298
  RefPtr<mozilla::dom::HTMLAllCollection> mAll;
299
300
  /** # of forms in the document, synchronously set */
301
  int32_t mNumForms;
302
303
  static uint32_t gWyciwygSessionCnt;
304
305
  static void TryHintCharset(nsIContentViewer* aContentViewer,
306
                             int32_t& aCharsetSource,
307
                             NotNull<const Encoding*>& aEncoding);
308
  void TryUserForcedCharset(nsIContentViewer* aCv,
309
                            nsIDocShell*  aDocShell,
310
                            int32_t& aCharsetSource,
311
                            NotNull<const Encoding*>& aEncoding);
312
  static void TryCacheCharset(nsICachingChannel* aCachingChannel,
313
                              int32_t& aCharsetSource,
314
                              NotNull<const Encoding*>& aEncoding);
315
  void TryParentCharset(nsIDocShell*  aDocShell,
316
                        int32_t& charsetSource,
317
                        NotNull<const Encoding*>& aEncoding);
318
  void TryTLD(int32_t& aCharsetSource, NotNull<const Encoding*>& aCharset);
319
  void TryFallback(int32_t& aCharsetSource,
320
                   NotNull<const Encoding*>& aEncoding);
321
322
  // Override so we can munge the charset on our wyciwyg channel as needed.
323
  virtual void
324
    SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding) override;
325
326
  // Tracks if we are currently processing any document.write calls (either
327
  // implicit or explicit). Note that if a write call writes out something which
328
  // would block the parser, then mWriteLevel will be incorrect until the parser
329
  // finishes processing that script.
330
  uint32_t mWriteLevel;
331
332
  // Load flags of the document's channel
333
  uint32_t mLoadFlags;
334
335
  bool mTooDeepWriteRecursion;
336
337
  bool mDisableDocWrite;
338
339
  bool mWarnedWidthHeight;
340
341
  nsCOMPtr<nsIWyciwygChannel> mWyciwygChannel;
342
343
  /* Midas implementation */
344
  nsresult   GetMidasCommandManager(nsICommandManager** aCommandManager);
345
346
  nsCOMPtr<nsICommandManager> mMidasCommandManager;
347
348
  nsresult TurnEditingOff();
349
  nsresult EditingStateChanged();
350
  void MaybeEditingStateChanged();
351
352
  uint32_t mContentEditableCount;
353
  EditingState mEditingState;
354
355
  // When false, the .cookies property is completely disabled
356
  bool mDisableCookieAccess;
357
358
  /**
359
   * Temporary flag that is set in EndUpdate() to ignore
360
   * MaybeEditingStateChanged() script runners from a nested scope.
361
   */
362
  bool mPendingMaybeEditingStateChanged;
363
};
364
365
inline nsHTMLDocument*
366
nsIDocument::AsHTMLDocument()
367
0
{
368
0
  MOZ_ASSERT(IsHTMLOrXHTML());
369
0
  return static_cast<nsHTMLDocument*>(this);
370
0
}
371
372
#define NS_HTML_DOCUMENT_INTERFACE_TABLE_BEGIN(_class)                        \
373
    NS_DOCUMENT_INTERFACE_TABLE_BEGIN(_class)                                 \
374
    NS_INTERFACE_TABLE_ENTRY(_class, nsIHTMLDocument)
375
376
#endif /* nsHTMLDocument_h___ */