Files
mozilla45esr/dom/base/nsNodeInfoManager.cpp
roytam1 ac0651448a import changes from tenfourfox: https://github.com/classilla/tenfourfox/compare/966d8cab820858bcda2fa8e945ed14b66d9e83f9...e7767cdaa56ee060b14702ed64ae594958fcb1dd
- issue #385: unconditionally disable JS source compression (6a91f9666)
- #359: security.pki.sha1_enforcement_level -> 1 (966d8cab8)
- for #375: M1349940 M1355595 M1334635 M1353708 M1354782 M1354781 M1349742 M1345315 M1356655 M1356663 M1357638 M1358809 M1307134 M1331342 M1220466 (964aa7e7c)
- #378: ensure remaining users of faulty LZ4 module never execute (922a32738)
- #387: pulled nsGenConList.* with relevant deps up to mozilla-central (b1831fc75)
- prerequisite for #375: M1347759 (8e58b5c8f)
- #375: M1348278 (8d665811d)
- #368: pref on Brotli (7da09aa4e)
- #375: M1330912 (sync patch only) (bea84e507)
- #375: M1342719 M1342720 (f4015e0c1)
- Bug 1071646 - Light refactoring of lexical binding helpers in Parser. (r=jorendorff) (167ad8564)
- Bug 1071646 - Make functions block-scoped in JS and implement Annex B semantics for compatibility. (r=jorendorff) (25844de1b)
- Bug 1071646 - Support labelled function declarations in sloppy mode per Annex B.3.2. (r=jorendorff) (37bf861a9)
- Bug 1071646 - Introduce JSOP_BINDVAR to support Annex B.3.3.3. (r=jorendorff) (884bd2ab5)
- Bug 1071646 - Cast ParseNode to Definition using as<T>. (r=jorendorff) (feba46e3f)
- Bug 1071646 - Support JSOP_BINDVAR in Baseline. (r=jandem) (92b6a3b48)
- #372: merge changeset 299512 (c28302281)
- Bug 1071646 - Support JSOP_BINDVAR in Ion. (r=jandem) (d05f77998)
- Bug 1231758 - Fix bogus assertion in BCE for Annex B function assignment. (r=jorendorff) (a11dcf0cc)
- Bug 1234717 - Fix upvar TDZ for block-scoped functions. (r=jorendorff) (2ec542b5a)
- Bug 1071646 - Forgot to commit tests on backout and relanding cycle. (r=me) (069a26bf9)
- #372: merge changeset 303861 (745f7e7a2)
- Bug 1235590 - Allow redeclaring block-scoped functions and warn about deprecation for now. (r=jorendorff) (8a51cc1f9)
- Bug 1243793 - Fix handling of labels when emitting hoisted function definitions. (r=jorendorff) (c0ffd70f9)
- Bug 1236875 - Fix BytecodeEmitter::atBodyLevel() for modules r=efaust (5b2c22f62)
- Bug 1304641 - TraceLogger: Also throw compartment mismatch error when running in the jits, r=bbouvier (717134569)
- Bug 1198833 - Variable redeclaration should be a syntax error r=shu (ac6710b0f)
- #393, Bug 1135377 - Part 1: Implement RegExp unicode flag. r=till, f=anba (d00063089)
- #393, Bug 1135377 - Part 2: Parse RegExp unicode character in non-CharacterClass. r=till, f=anba (c85a176bd)
- #393, Bug 1135377 - Part 3: Parse RegExp unicode character in CharacterClass. r=till, f=anba (68f44ec41)
- #393, Bug 1135377 - Part 4: Support everything Atom in RegExp with unicode flag. r=till, f=anba (4e924a688)
- #393, Bug 1135377 - Part 5: Support CharacterClassEscape in RegExp with unicode flag. r=till, f=anba (45a4712b0)
- #393, Bug 1135377 - Part 6: Support ignoreCase for BMP in RegExp with unicode flag. r=till, f=anba (cf744e9d3)
- #393, Bug 1135377 - Part 7: Support ignoreCase for non-BMP in RegExp with unicode flag. r=till, f=anba (f31a9f9e8)
- #393, Bug 1135377 - Part 8: Disallow extended pattern in RegExp with unicode flag. r=till, f=anba (c05db4075)
- #393, Bug 1135377 - Part 9: Use RegExp unicode flag in String.prototype.{match,replace,split}. r=till, f=anba (8636a96b7)
- #393, Bug 1135377 - Part 10: Decrement index when it points trail surrogate that has corresponding lead surrogate. r=till, f=anba (522d06ab3)
- #393, Bug 1135377 - Part 11: Support back reference with unicode flag. r=till, f=anba (122e41a28)
- #393, Bug 1279467 - Fix null handling in RegExp character class with unicode flag. r=till (741f170e6)
- #393: bustage fix (3e39760c8)
- #393, Bug 1281739 - Do not match K, S, k, s, KELVIN SIGN, LATIN SMALL LETTER LONG S with \W in unicode ignoreCase RegExp. r=till (3c5d4bab8)
- #375: M1233101 (a6a0b895f)
- #375, #391: branch hint malloc and recursion checks, clean up MAsm type barrier spooge (a008a1978)
- closes #380: fix toSource() tests (f9de6783b)
- #394: fix and enable ES7 exponentiation operator (6dc5f54dc)
- #396: M1342009, plus refactor to PLDHashTable a la M1352888 (06fcc091f)
- #375: M1347634 1352235 (aa6b13c59)
- #375: M1351303 (f7badd24e)
- #375: M1287277 M1259677; temporarily disable M1351303 due to crashes (75d95f15b)
- #375: M1363423 M1273828 M1364661 (e7767cdaa)
2018-05-31 14:43:16 +08:00

456 lines
12 KiB
C++

/* -*- 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/DebugOnly.h"
#include "mozilla/dom/NodeInfo.h"
#include "mozilla/dom/NodeInfoInlines.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsIAtom.h"
#include "nsIDocument.h"
#include "nsIPrincipal.h"
#include "nsIURI.h"
#include "nsContentUtils.h"
#include "nsReadableUtils.h"
#include "nsGkAtoms.h"
#include "nsComponentManagerUtils.h"
#include "nsLayoutStatics.h"
#include "nsBindingManager.h"
#include "nsHashKeys.h"
#include "nsCCUncollectableMarker.h"
#include "nsNameSpaceManager.h"
#include "nsDocument.h"
#include "nsNullPrincipal.h"
using namespace mozilla;
using mozilla::dom::NodeInfo;
#include "mozilla/Logging.h"
static LazyLogModule gNodeInfoManagerLeakPRLog("NodeInfoManagerLeak");
PLHashNumber
nsNodeInfoManager::GetNodeInfoInnerHashValue(const void *key)
{
MOZ_ASSERT(key, "Null key passed to NodeInfo::GetHashValue!");
auto *node = reinterpret_cast<const NodeInfo::NodeInfoInner*>(key);
return node->mName ? node->mName->hash() : HashString(*(node->mNameString));
}
int
nsNodeInfoManager::NodeInfoInnerKeyCompare(const void *key1, const void *key2)
{
MOZ_ASSERT(key1 && key2, "Null key passed to NodeInfoInnerKeyCompare!");
auto *node1 = reinterpret_cast<const NodeInfo::NodeInfoInner*>(key1);
auto *node2 = reinterpret_cast<const NodeInfo::NodeInfoInner*>(key2);
if (node1->mPrefix != node2->mPrefix ||
node1->mNamespaceID != node2->mNamespaceID ||
node1->mNodeType != node2->mNodeType ||
node1->mExtraName != node2->mExtraName) {
return 0;
}
if (node1->mName) {
if (node2->mName) {
return (node1->mName == node2->mName);
}
return (node1->mName->Equals(*(node2->mNameString)));
}
if (node2->mName) {
return (node2->mName->Equals(*(node1->mNameString)));
}
return (node1->mNameString->Equals(*(node2->mNameString)));
}
static void* PR_CALLBACK
AllocTable(void* pool, size_t size)
{
return malloc(size);
}
static void PR_CALLBACK
FreeTable(void* pool, void* item)
{
free(item);
}
static PLHashEntry* PR_CALLBACK
AllocEntry(void* pool, const void* key)
{
return (PLHashEntry*)malloc(sizeof(PLHashEntry));
}
static void PR_CALLBACK
FreeEntry(void* pool, PLHashEntry* he, unsigned flag)
{
if (flag == HT_FREE_ENTRY) {
free(he);
}
}
static PLHashAllocOps allocOps =
{ AllocTable, FreeTable, AllocEntry, FreeEntry };
nsNodeInfoManager::nsNodeInfoManager()
: mDocument(nullptr),
mNonDocumentNodeInfos(0),
mTextNodeInfo(nullptr),
mCommentNodeInfo(nullptr),
mDocumentNodeInfo(nullptr),
mRecentlyUsedNodeInfos{}
{
nsLayoutStatics::AddRef();
if (gNodeInfoManagerLeakPRLog)
MOZ_LOG(gNodeInfoManagerLeakPRLog, LogLevel::Debug,
("NODEINFOMANAGER %p created", this));
mNodeInfoHash = PL_NewHashTable(32, GetNodeInfoInnerHashValue,
NodeInfoInnerKeyCompare,
PL_CompareValues, &allocOps, nullptr);
}
nsNodeInfoManager::~nsNodeInfoManager()
{
if (mNodeInfoHash)
PL_HashTableDestroy(mNodeInfoHash);
// Note: mPrincipal may be null here if we never got inited correctly
mPrincipal = nullptr;
mBindingManager = 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(mBindingManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsNodeInfoManager, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsNodeInfoManager, Release)
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsNodeInfoManager)
if (tmp->mDocument) {
return NS_CYCLE_COLLECTION_PARTICIPANT(nsDocument)->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(nsDocument)->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(nsDocument)->CanSkipThis(tmp->mDocument);
}
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
nsresult
nsNodeInfoManager::Init(nsIDocument *aDocument)
{
NS_ENSURE_TRUE(mNodeInfoHash, NS_ERROR_OUT_OF_MEMORY);
NS_PRECONDITION(!mPrincipal,
"Being inited when we already have a principal?");
mPrincipal = nsNullPrincipal::Create();
NS_ENSURE_TRUE(mPrincipal, NS_ERROR_FAILURE);
if (aDocument) {
mBindingManager = new nsBindingManager(aDocument);
}
mDefaultPrincipal = mPrincipal;
mDocument = aDocument;
if (gNodeInfoManagerLeakPRLog)
MOZ_LOG(gNodeInfoManagerLeakPRLog, LogLevel::Debug,
("NODEINFOMANAGER %p Init document=%p", this, aDocument));
return NS_OK;
}
// static
int
nsNodeInfoManager::DropNodeInfoDocument(PLHashEntry *he, int hashIndex, void *arg)
{
static_cast<mozilla::dom::NodeInfo*>(he->value)->mDocument = nullptr;
return HT_ENUMERATE_NEXT;
}
void
nsNodeInfoManager::DropDocumentReference()
{
if (mBindingManager) {
mBindingManager->DropDocumentReference();
}
// This is probably not needed anymore.
PL_HashTableEnumerateEntries(mNodeInfoHash, DropNodeInfoDocument, nullptr);
NS_ASSERTION(!mNonDocumentNodeInfos, "Shouldn't have non-document nodeinfos!");
mDocument = nullptr;
}
already_AddRefed<mozilla::dom::NodeInfo>
nsNodeInfoManager::GetNodeInfo(nsIAtom *aName, nsIAtom *aPrefix,
int32_t aNamespaceID, uint16_t aNodeType,
nsIAtom* aExtraName /* = nullptr */)
{
CheckValidNodeInfo(aNodeType, aName, aNamespaceID, aExtraName);
NodeInfo::NodeInfoInner tmpKey(aName, aPrefix, aNamespaceID, aNodeType,
aExtraName);
uint32_t index =
GetNodeInfoInnerHashValue(&tmpKey) % RECENTLY_USED_NODEINFOS_SIZE;
NodeInfo* ni = mRecentlyUsedNodeInfos[index];
if (ni && NodeInfoInnerKeyCompare(&(ni->mInner), &tmpKey)) {
RefPtr<NodeInfo> nodeInfo = ni;
return nodeInfo.forget();
}
void *node = PL_HashTableLookup(mNodeInfoHash, &tmpKey);
if (node) {
RefPtr<NodeInfo> nodeInfo = static_cast<NodeInfo*>(node);
mRecentlyUsedNodeInfos[index] = nodeInfo;
return nodeInfo.forget();
}
RefPtr<NodeInfo> newNodeInfo =
new NodeInfo(aName, aPrefix, aNamespaceID, aNodeType, aExtraName, this);
DebugOnly<PLHashEntry*> he =
PL_HashTableAdd(mNodeInfoHash, &newNodeInfo->mInner, newNodeInfo);
MOZ_ASSERT(he, "PL_HashTableAdd() failed");
// Have to do the swap thing, because already_AddRefed<nsNodeInfo>
// doesn't cast to already_AddRefed<mozilla::dom::NodeInfo>
++mNonDocumentNodeInfos;
if (mNonDocumentNodeInfos == 1) {
NS_IF_ADDREF(mDocument);
}
mRecentlyUsedNodeInfos[index] = newNodeInfo;
return newNodeInfo.forget();
}
nsresult
nsNodeInfoManager::GetNodeInfo(const nsAString& aName, nsIAtom *aPrefix,
int32_t aNamespaceID, uint16_t aNodeType,
NodeInfo** aNodeInfo)
{
#ifdef DEBUG
{
nsCOMPtr<nsIAtom> nameAtom = do_GetAtom(aName);
CheckValidNodeInfo(aNodeType, nameAtom, aNamespaceID, nullptr);
}
#endif
NodeInfo::NodeInfoInner tmpKey(aName, aPrefix, aNamespaceID, aNodeType);
uint32_t index =
GetNodeInfoInnerHashValue(&tmpKey) % RECENTLY_USED_NODEINFOS_SIZE;
NodeInfo* ni = mRecentlyUsedNodeInfos[index];
if (ni && NodeInfoInnerKeyCompare(&(ni->mInner), &tmpKey)) {
RefPtr<NodeInfo> nodeInfo = ni;
nodeInfo.forget(aNodeInfo);
return NS_OK;
}
void *node = PL_HashTableLookup(mNodeInfoHash, &tmpKey);
if (node) {
RefPtr<NodeInfo> nodeInfo = static_cast<NodeInfo*>(node);
mRecentlyUsedNodeInfos[index] = nodeInfo;
nodeInfo.forget(aNodeInfo);
return NS_OK;
}
nsCOMPtr<nsIAtom> nameAtom = do_GetAtom(aName);
NS_ENSURE_TRUE(nameAtom, NS_ERROR_OUT_OF_MEMORY);
RefPtr<NodeInfo> newNodeInfo =
new NodeInfo(nameAtom, aPrefix, aNamespaceID, aNodeType, nullptr, this);
NS_ENSURE_TRUE(newNodeInfo, NS_ERROR_OUT_OF_MEMORY);
PLHashEntry *he;
he = PL_HashTableAdd(mNodeInfoHash, &newNodeInfo->mInner, newNodeInfo);
NS_ENSURE_TRUE(he, NS_ERROR_FAILURE);
++mNonDocumentNodeInfos;
if (mNonDocumentNodeInfos == 1) {
NS_IF_ADDREF(mDocument);
}
mRecentlyUsedNodeInfos[index] = newNodeInfo;
newNodeInfo.forget(aNodeInfo);
return NS_OK;
}
nsresult
nsNodeInfoManager::GetNodeInfo(const nsAString& aName, nsIAtom *aPrefix,
const nsAString& aNamespaceURI,
uint16_t aNodeType,
NodeInfo** aNodeInfo)
{
int32_t nsid = kNameSpaceID_None;
if (!aNamespaceURI.IsEmpty()) {
nsresult rv = nsContentUtils::NameSpaceManager()->
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,
nsIDOMNode::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, nsIDOMNode::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, nsIDOMNode::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::SetDocumentPrincipal(nsIPrincipal *aPrincipal)
{
mPrincipal = nullptr;
if (!aPrincipal) {
aPrincipal = mDefaultPrincipal;
}
NS_ASSERTION(aPrincipal, "Must have principal by this point!");
mPrincipal = aPrincipal;
}
void
nsNodeInfoManager::RemoveNodeInfo(NodeInfo *aNodeInfo)
{
NS_PRECONDITION(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;
}
}
uint32_t index =
GetNodeInfoInnerHashValue(&aNodeInfo->mInner) % RECENTLY_USED_NODEINFOS_SIZE;
if (mRecentlyUsedNodeInfos[index] == aNodeInfo) {
mRecentlyUsedNodeInfos[index] = nullptr;
}
#ifdef DEBUG
bool ret =
#endif
PL_HashTableRemove(mNodeInfoHash, &aNodeInfo->mInner);
NS_POSTCONDITION(ret, "Can't find mozilla::dom::NodeInfo to remove!!!");
}