summaryrefslogtreecommitdiffstats
path: root/dom/base/nsNodeInfoManager.cpp
blob: 47b1636f2a080462b785d13fc93520b7c0a22d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * A class for handing out nodeinfos and ensuring sharing of them as needed.
 */

#include "nsNodeInfoManager.h"

#include "mozilla/BasePrincipal.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/NodeInfo.h"
#include "mozilla/dom/NodeInfoInlines.h"
#include "mozilla/dom/DocGroup.h"
#include "mozilla/NullPrincipal.h"
#include "mozilla/StaticPrefs_dom.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsAtom.h"
#include "nsIPrincipal.h"
#include "nsContentUtils.h"
#include "nsReadableUtils.h"
#include "nsGkAtoms.h"
#include "nsComponentManagerUtils.h"
#include "nsLayoutStatics.h"
#include "nsHashKeys.h"
#include "nsCCUncollectableMarker.h"
#include "nsNameSpaceManager.h"
#include "nsWindowSizes.h"

using namespace mozilla;
using mozilla::dom::NodeInfo;

#include "mozilla/Logging.h"

static LazyLogModule gNodeInfoManagerLeakPRLog("NodeInfoManagerLeak");
static const uint32_t kInitialNodeInfoHashSize = 32;

nsNodeInfoManager::nsNodeInfoManager(mozilla::dom::Document* aDocument,
                                     nsIPrincipal* aPrincipal)
    : mNodeInfoHash(kInitialNodeInfoHashSize),
      mDocument(aDocument),
      mNonDocumentNodeInfos(0),
      mTextNodeInfo(nullptr),
      mCommentNodeInfo(nullptr),
      mDocumentNodeInfo(nullptr),
      mRecentlyUsedNodeInfos(),
      mArena(nullptr) {
  nsLayoutStatics::AddRef();

  if (aPrincipal) {
    mPrincipal = aPrincipal;
  } else {
    mPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
  }
  mDefaultPrincipal = mPrincipal;

  if (gNodeInfoManagerLeakPRLog) {
    MOZ_LOG(gNodeInfoManagerLeakPRLog, LogLevel::Debug,
            ("NODEINFOMANAGER %p created,  document=%p", this, aDocument));
  }
}

nsNodeInfoManager::~nsNodeInfoManager() {
  // Note: mPrincipal may be null here if we never got inited correctly
  mPrincipal = nullptr;

  mArena = nullptr;

  if (gNodeInfoManagerLeakPRLog)
    MOZ_LOG(gNodeInfoManagerLeakPRLog, LogLevel::Debug,
            ("NODEINFOMANAGER %p destroyed", this));

  nsLayoutStatics::Release();
}

NS_IMPL_CYCLE_COLLECTION_CLASS(nsNodeInfoManager)

NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsNodeInfoManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsNodeInfoManager)
  if (tmp->mNonDocumentNodeInfos) {
    NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mDocument)
  }
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsNodeInfoManager)
  if (tmp->mDocument) {
    return NS_CYCLE_COLLECTION_PARTICIPANT(mozilla::dom::Document)
        ->CanSkip(tmp->mDocument, aRemovingAllowed);
  }
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END

NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsNodeInfoManager)
  if (tmp->mDocument) {
    return NS_CYCLE_COLLECTION_PARTICIPANT(mozilla::dom::Document)
        ->CanSkipInCC(tmp->mDocument);
  }
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END

NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsNodeInfoManager)
  if (tmp->mDocument) {
    return NS_CYCLE_COLLECTION_PARTICIPANT(mozilla::dom::Document)
        ->CanSkipThis(tmp->mDocument);
  }
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END

void nsNodeInfoManager::DropDocumentReference() {
  // This is probably not needed anymore.
  for (const auto& entry : mNodeInfoHash.Values()) {
    entry->mDocument = nullptr;
  }

  NS_ASSERTION(!mNonDocumentNodeInfos,
               "Shouldn't have non-document nodeinfos!");
  mDocument = nullptr;
}

already_AddRefed<mozilla::dom::NodeInfo> nsNodeInfoManager::GetNodeInfo(
    nsAtom* aName, nsAtom* aPrefix, int32_t aNamespaceID, uint16_t aNodeType,
    nsAtom* aExtraName /* = nullptr */) {
  CheckValidNodeInfo(aNodeType, aName, aNamespaceID, aExtraName);

  NodeInfo::NodeInfoInner tmpKey(aName, aPrefix, aNamespaceID, aNodeType,
                                 aExtraName);

  auto p = mRecentlyUsedNodeInfos.Lookup(tmpKey);
  if (p) {
    RefPtr<NodeInfo> nodeInfo = p.Data();
    return nodeInfo.forget();
  }

  // We don't use WithEntryHandle here as that would end up storing the
  // temporary key instead of using `mInner`.
  RefPtr<NodeInfo> nodeInfo = mNodeInfoHash.Get(&tmpKey);
  if (!nodeInfo) {
    ++mNonDocumentNodeInfos;
    if (mNonDocumentNodeInfos == 1) {
      NS_IF_ADDREF(mDocument);
    }

    nodeInfo =
        new NodeInfo(aName, aPrefix, aNamespaceID, aNodeType, aExtraName, this);
    mNodeInfoHash.InsertOrUpdate(&nodeInfo->mInner, nodeInfo);
  }

  // Have to do the swap thing, because already_AddRefed<nsNodeInfo>
  // doesn't cast to already_AddRefed<mozilla::dom::NodeInfo>
  p.Set(nodeInfo);
  return nodeInfo.forget();
}

nsresult nsNodeInfoManager::GetNodeInfo(const nsAString& aName, nsAtom* aPrefix,
                                        int32_t aNamespaceID,
                                        uint16_t aNodeType,
                                        NodeInfo** aNodeInfo) {
  // TODO(erahm): Combine this with the atom version.
#ifdef DEBUG
  {
    RefPtr<nsAtom> nameAtom = NS_Atomize(aName);
    CheckValidNodeInfo(aNodeType, nameAtom, aNamespaceID, nullptr);
  }
#endif

  NodeInfo::NodeInfoInner tmpKey(aName, aPrefix, aNamespaceID, aNodeType);

  auto p = mRecentlyUsedNodeInfos.Lookup(tmpKey);
  if (p) {
    RefPtr<NodeInfo> nodeInfo = p.Data();
    nodeInfo.forget(aNodeInfo);
    return NS_OK;
  }

  RefPtr<NodeInfo> nodeInfo = mNodeInfoHash.Get(&tmpKey);
  if (!nodeInfo) {
    ++mNonDocumentNodeInfos;
    if (mNonDocumentNodeInfos == 1) {
      NS_IF_ADDREF(mDocument);
    }

    RefPtr<nsAtom> nameAtom = NS_Atomize(aName);
    nodeInfo =
        new NodeInfo(nameAtom, aPrefix, aNamespaceID, aNodeType, nullptr, this);
    mNodeInfoHash.InsertOrUpdate(&nodeInfo->mInner, nodeInfo);
  }

  p.Set(nodeInfo);
  nodeInfo.forget(aNodeInfo);

  return NS_OK;
}

nsresult nsNodeInfoManager::GetNodeInfo(const nsAString& aName, nsAtom* aPrefix,
                                        const nsAString& aNamespaceURI,
                                        uint16_t aNodeType,
                                        NodeInfo** aNodeInfo) {
  int32_t nsid = kNameSpaceID_None;

  if (!aNamespaceURI.IsEmpty()) {
    nsresult rv = nsNameSpaceManager::GetInstance()->RegisterNameSpace(
        aNamespaceURI, nsid);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return GetNodeInfo(aName, aPrefix, nsid, aNodeType, aNodeInfo);
}

already_AddRefed<NodeInfo> nsNodeInfoManager::GetTextNodeInfo() {
  RefPtr<mozilla::dom::NodeInfo> nodeInfo;

  if (!mTextNodeInfo) {
    nodeInfo = GetNodeInfo(nsGkAtoms::textTagName, nullptr, kNameSpaceID_None,
                           nsINode::TEXT_NODE, nullptr);
    // Hold a weak ref; the nodeinfo will let us know when it goes away
    mTextNodeInfo = nodeInfo;
  } else {
    nodeInfo = mTextNodeInfo;
  }

  return nodeInfo.forget();
}

already_AddRefed<NodeInfo> nsNodeInfoManager::GetCommentNodeInfo() {
  RefPtr<NodeInfo> nodeInfo;

  if (!mCommentNodeInfo) {
    nodeInfo = GetNodeInfo(nsGkAtoms::commentTagName, nullptr,
                           kNameSpaceID_None, nsINode::COMMENT_NODE, nullptr);
    // Hold a weak ref; the nodeinfo will let us know when it goes away
    mCommentNodeInfo = nodeInfo;
  } else {
    nodeInfo = mCommentNodeInfo;
  }

  return nodeInfo.forget();
}

already_AddRefed<NodeInfo> nsNodeInfoManager::GetDocumentNodeInfo() {
  RefPtr<NodeInfo> nodeInfo;

  if (!mDocumentNodeInfo) {
    NS_ASSERTION(mDocument, "Should have mDocument!");
    nodeInfo = GetNodeInfo(nsGkAtoms::documentNodeName, nullptr,
                           kNameSpaceID_None, nsINode::DOCUMENT_NODE, nullptr);
    // Hold a weak ref; the nodeinfo will let us know when it goes away
    mDocumentNodeInfo = nodeInfo;

    --mNonDocumentNodeInfos;
    if (!mNonDocumentNodeInfos) {
      mDocument->Release();  // Don't set mDocument to null!
    }
  } else {
    nodeInfo = mDocumentNodeInfo;
  }

  return nodeInfo.forget();
}

void* nsNodeInfoManager::Allocate(size_t aSize) {
  if (!mHasAllocated) {
    if (mozilla::StaticPrefs::dom_arena_allocator_enabled_AtStartup()) {
      if (!mArena) {
        mozilla::dom::DocGroup* docGroup = GetDocument()->GetDocGroupOrCreate();
        if (docGroup) {
          MOZ_ASSERT(!GetDocument()->HasChildren());
          mArena = docGroup->ArenaAllocator();
        }
      }
#ifdef DEBUG
      else {
        mozilla::dom::DocGroup* docGroup = GetDocument()->GetDocGroup();
        MOZ_ASSERT(docGroup);
        MOZ_ASSERT(mArena == docGroup->ArenaAllocator());
      }
#endif
    }
    mHasAllocated = true;
  }

#ifdef DEBUG
  if (!mozilla::StaticPrefs::dom_arena_allocator_enabled_AtStartup()) {
    MOZ_ASSERT(!mArena, "mArena should not set if the pref is not on");
  };
#endif

  if (mArena) {
    return mArena->Allocate(aSize);
  }
  return malloc(aSize);
}

void nsNodeInfoManager::SetArenaAllocator(mozilla::dom::DOMArena* aArena) {
  MOZ_DIAGNOSTIC_ASSERT_IF(mArena, mArena == aArena);
  MOZ_DIAGNOSTIC_ASSERT(!mHasAllocated);
  MOZ_DIAGNOSTIC_ASSERT(
      mozilla::StaticPrefs::dom_arena_allocator_enabled_AtStartup());

  if (!mArena) {
    mArena = aArena;
  }
}

void nsNodeInfoManager::SetDocumentPrincipal(nsIPrincipal* aPrincipal) {
  mPrincipal = nullptr;
  if (!aPrincipal) {
    aPrincipal = mDefaultPrincipal;
  }

  NS_ASSERTION(aPrincipal, "Must have principal by this point!");
  MOZ_DIAGNOSTIC_ASSERT(!nsContentUtils::IsExpandedPrincipal(aPrincipal),
                        "Documents shouldn't have an expanded principal");

  mPrincipal = aPrincipal;
}

void nsNodeInfoManager::RemoveNodeInfo(NodeInfo* aNodeInfo) {
  MOZ_ASSERT(aNodeInfo, "Trying to remove null nodeinfo from manager!");

  if (aNodeInfo == mDocumentNodeInfo) {
    mDocumentNodeInfo = nullptr;
    mDocument = nullptr;
  } else {
    if (--mNonDocumentNodeInfos == 0) {
      if (mDocument) {
        // Note, whoever calls this method should keep NodeInfoManager alive,
        // even if mDocument gets deleted.
        mDocument->Release();
      }
    }
    // Drop weak reference if needed
    if (aNodeInfo == mTextNodeInfo) {
      mTextNodeInfo = nullptr;
    } else if (aNodeInfo == mCommentNodeInfo) {
      mCommentNodeInfo = nullptr;
    }
  }

  mRecentlyUsedNodeInfos.Remove(aNodeInfo->mInner);
  DebugOnly<bool> ret = mNodeInfoHash.Remove(&aNodeInfo->mInner);
  MOZ_ASSERT(ret, "Can't find mozilla::dom::NodeInfo to remove!!!");
}

static bool IsSystemOrAddonOrAboutPrincipal(nsIPrincipal* aPrincipal) {
  return aPrincipal->IsSystemPrincipal() ||
         BasePrincipal::Cast(aPrincipal)->AddonPolicy() ||
         // NOTE: about:blank and about:srcdoc inherit the principal of their
         // parent, so aPrincipal->SchemeIs("about") returns false for them.
         aPrincipal->SchemeIs("about");
}

bool nsNodeInfoManager::InternalSVGEnabled() {
  MOZ_ASSERT(!mSVGEnabled, "Caller should use the cached mSVGEnabled!");

  // If the svg.disabled pref. is true, convert all SVG nodes into
  // disabled SVG nodes by swapping the namespace.
  nsNameSpaceManager* nsmgr = nsNameSpaceManager::GetInstance();
  nsCOMPtr<nsILoadInfo> loadInfo;
  bool SVGEnabled = false;

  if (nsmgr && !nsmgr->mSVGDisabled) {
    SVGEnabled = true;
  } else {
    nsCOMPtr<nsIChannel> channel = mDocument->GetChannel();
    // We don't have a channel for SVGs constructed inside a SVG script
    if (channel) {
      loadInfo = channel->LoadInfo();
    }
  }

  // We allow SVG (regardless of the pref) if this is a system or add-on
  // principal or about: page, or if this load was requested for a system or
  // add-on principal or about: page (e.g. a remote image being served as part
  // of system or add-on UI or about: page)
  bool conclusion =
      (SVGEnabled || IsSystemOrAddonOrAboutPrincipal(mPrincipal) ||
       (loadInfo &&
        (loadInfo->GetExternalContentPolicyType() ==
             ExtContentPolicy::TYPE_IMAGE ||
         loadInfo->GetExternalContentPolicyType() ==
             ExtContentPolicy::TYPE_OTHER) &&
        (IsSystemOrAddonOrAboutPrincipal(loadInfo->GetLoadingPrincipal()) ||
         IsSystemOrAddonOrAboutPrincipal(loadInfo->TriggeringPrincipal()))));
  mSVGEnabled = Some(conclusion);
  return conclusion;
}

bool nsNodeInfoManager::InternalMathMLEnabled() {
  MOZ_ASSERT(!mMathMLEnabled, "Caller should use the cached mMathMLEnabled!");

  // If the mathml.disabled pref. is true, convert all MathML nodes into
  // disabled MathML nodes by swapping the namespace.
  nsNameSpaceManager* nsmgr = nsNameSpaceManager::GetInstance();
  bool conclusion =
      ((nsmgr && !nsmgr->mMathMLDisabled) || mPrincipal->IsSystemPrincipal());
  mMathMLEnabled = Some(conclusion);
  return conclusion;
}

void nsNodeInfoManager::AddSizeOfIncludingThis(nsWindowSizes& aSizes) const {
  aSizes.mDOMSizes.mDOMOtherSize += aSizes.mState.mMallocSizeOf(this);

  // Measurement of the following members may be added later if DMD finds it
  // is worthwhile:
  // - mNodeInfoHash
}