From 5377605d6355f26cb08a4b8d5e43130599f3ac56 Mon Sep 17 00:00:00 2001 From: wolfbeast Date: Mon, 25 May 2020 20:28:36 +0200 Subject: Issue #1564 - Split off nsIdentifierMapEntry in its own header + Fix dependency fallout from removing nsDocument.h from ShadowRoot.h --- dom/base/nsIdentifierMapEntry.h | 170 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 dom/base/nsIdentifierMapEntry.h (limited to 'dom/base/nsIdentifierMapEntry.h') diff --git a/dom/base/nsIdentifierMapEntry.h b/dom/base/nsIdentifierMapEntry.h new file mode 100644 index 000000000..fce506cef --- /dev/null +++ b/dom/base/nsIdentifierMapEntry.h @@ -0,0 +1,170 @@ +/* -*- 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/. */ + +/* + * Base class for all our document implementations. + */ + +#ifndef nsIdentifierMapEntry_h +#define nsIdentifierMapEntry_h + +#include "PLDHashTable.h" + +#include "mozilla/MemoryReporting.h" +#include "mozilla/Move.h" +#include "mozilla/dom/Element.h" +#include "mozilla/net/ReferrerPolicy.h" + +#include "nsCOMArray.h" +#include "nsCOMPtr.h" +#include "nsContentList.h" +#include "nsIAtom.h" +#include "nsIDocument.h" +#include "nsTArray.h" +#include "nsTHashtable.h" + +class nsIContent; + +/** + * Right now our identifier map entries contain information for 'name' + * and 'id' mappings of a given string. This is so that + * nsHTMLDocument::ResolveName only has to do one hash lookup instead + * of two. It's not clear whether this still matters for performance. + * + * We also store the document.all result list here. This is mainly so that + * when all elements with the given ID are removed and we remove + * the ID's nsIdentifierMapEntry, the document.all result is released too. + * Perhaps the document.all results should have their own hashtable + * in nsHTMLDocument. + */ +class nsIdentifierMapEntry : public nsStringHashKey +{ +public: + typedef mozilla::dom::Element Element; + typedef mozilla::net::ReferrerPolicy ReferrerPolicy; + + explicit nsIdentifierMapEntry(const nsAString& aKey) : + nsStringHashKey(&aKey), mNameContentList(nullptr) + { + } + explicit nsIdentifierMapEntry(const nsAString* aKey) : + nsStringHashKey(aKey), mNameContentList(nullptr) + { + } + nsIdentifierMapEntry(const nsIdentifierMapEntry& aOther) : + nsStringHashKey(&aOther.GetKey()) + { + NS_ERROR("Should never be called"); + } + ~nsIdentifierMapEntry(); + + void AddNameElement(nsINode* aDocument, Element* aElement); + void RemoveNameElement(Element* aElement); + bool IsEmpty(); + nsBaseContentList* GetNameContentList() { + return mNameContentList; + } + bool HasNameElement() const { + return mNameContentList && mNameContentList->Length() != 0; + } + + /** + * Returns the element if we know the element associated with this + * id. Otherwise returns null. + */ + Element* GetIdElement(); + /** + * Returns the list of all elements associated with this id. + */ + const nsTArray& GetIdElements() const { + return mIdContentList; + } + /** + * If this entry has a non-null image element set (using SetImageElement), + * the image element will be returned, otherwise the same as GetIdElement(). + */ + Element* GetImageIdElement(); + /** + * Append all the elements with this id to aElements + */ + void AppendAllIdContent(nsCOMArray* aElements); + /** + * This can fire ID change callbacks. + * @return true if the content could be added, false if we failed due + * to OOM. + */ + bool AddIdElement(Element* aElement); + /** + * This can fire ID change callbacks. + */ + void RemoveIdElement(Element* aElement); + /** + * Set the image element override for this ID. This will be returned by + * GetIdElement(true) if non-null. + */ + void SetImageElement(Element* aElement); + bool HasIdElementExposedAsHTMLDocumentProperty(); + + bool HasContentChangeCallback() { return mChangeCallbacks != nullptr; } + void AddContentChangeCallback(nsIDocument::IDTargetObserver aCallback, + void* aData, bool aForImage); + void RemoveContentChangeCallback(nsIDocument::IDTargetObserver aCallback, + void* aData, bool aForImage); + + /** + * Remove all elements and notify change listeners. + */ + void ClearAndNotify(); + + void Traverse(nsCycleCollectionTraversalCallback* aCallback); + + struct ChangeCallback { + nsIDocument::IDTargetObserver mCallback; + void* mData; + bool mForImage; + }; + + struct ChangeCallbackEntry : public PLDHashEntryHdr { + typedef const ChangeCallback KeyType; + typedef const ChangeCallback* KeyTypePointer; + + explicit ChangeCallbackEntry(const ChangeCallback* aKey) : + mKey(*aKey) { } + ChangeCallbackEntry(const ChangeCallbackEntry& toCopy) : + mKey(toCopy.mKey) { } + + KeyType GetKey() const { return mKey; } + bool KeyEquals(KeyTypePointer aKey) const { + return aKey->mCallback == mKey.mCallback && + aKey->mData == mKey.mData && + aKey->mForImage == mKey.mForImage; + } + + static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; } + static PLDHashNumber HashKey(KeyTypePointer aKey) + { + return mozilla::HashGeneric(aKey->mCallback, aKey->mData); + } + enum { ALLOW_MEMMOVE = true }; + + ChangeCallback mKey; + }; + + size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; + +private: + void FireChangeCallbacks(Element* aOldElement, Element* aNewElement, + bool aImageOnly = false); + + // empty if there are no elements with this ID. + // The elements are stored as weak pointers. + nsTArray mIdContentList; + RefPtr mNameContentList; + nsAutoPtr > mChangeCallbacks; + RefPtr mImageElement; +}; + +#endif // #ifndef nsIdentifierMapEntry_h -- cgit v1.2.3 From a4c5e74b0b48ade0c1b50b3905670a9d86422453 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Sat, 13 Jun 2020 08:17:56 -0400 Subject: Bug 1217436 - Make nsIdentifierMapEntry::mIdContentList an AutoTArray to save an allocation Tag #1375 --- dom/base/nsIdentifierMapEntry.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'dom/base/nsIdentifierMapEntry.h') diff --git a/dom/base/nsIdentifierMapEntry.h b/dom/base/nsIdentifierMapEntry.h index fce506cef..ea60d3322 100644 --- a/dom/base/nsIdentifierMapEntry.h +++ b/dom/base/nsIdentifierMapEntry.h @@ -54,13 +54,18 @@ public: nsStringHashKey(aKey), mNameContentList(nullptr) { } - nsIdentifierMapEntry(const nsIdentifierMapEntry& aOther) : - nsStringHashKey(&aOther.GetKey()) + nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther) : + nsStringHashKey(&aOther.GetKey()), + mIdContentList(mozilla::Move(aOther.mIdContentList)), + mNameContentList(aOther.mNameContentList.forget()), + mChangeCallbacks(aOther.mChangeCallbacks.forget()), + mImageElement(aOther.mImageElement.forget()) { - NS_ERROR("Should never be called"); } ~nsIdentifierMapEntry(); + enum { ALLOW_MEMMOVE = false }; + void AddNameElement(nsINode* aDocument, Element* aElement); void RemoveNameElement(Element* aElement); bool IsEmpty(); @@ -156,12 +161,15 @@ public: size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; private: + nsIdentifierMapEntry(const nsIdentifierMapEntry& aOther) = delete; + nsIdentifierMapEntry& operator=(const nsIdentifierMapEntry& aOther) = delete; + void FireChangeCallbacks(Element* aOldElement, Element* aNewElement, bool aImageOnly = false); // empty if there are no elements with this ID. // The elements are stored as weak pointers. - nsTArray mIdContentList; + AutoTArray mIdContentList; RefPtr mNameContentList; nsAutoPtr > mChangeCallbacks; RefPtr mImageElement; -- cgit v1.2.3 From 35754dd1af72abcae49edd2eeb76855d999a4e6d Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Sat, 13 Jun 2020 08:18:46 -0400 Subject: Bug 1355787 - nsIdentifierMapEntry should let one to use either strings or atoms as keys to avoid slow string assignments when possible. Tag #1375 --- dom/base/nsIdentifierMapEntry.h | 72 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 6 deletions(-) (limited to 'dom/base/nsIdentifierMapEntry.h') diff --git a/dom/base/nsIdentifierMapEntry.h b/dom/base/nsIdentifierMapEntry.h index ea60d3322..41b8b4a83 100644 --- a/dom/base/nsIdentifierMapEntry.h +++ b/dom/base/nsIdentifierMapEntry.h @@ -40,22 +40,45 @@ class nsIContent; * Perhaps the document.all results should have their own hashtable * in nsHTMLDocument. */ -class nsIdentifierMapEntry : public nsStringHashKey +class nsIdentifierMapEntry : public PLDHashEntryHdr { public: + struct AtomOrString + { + MOZ_IMPLICIT AtomOrString(nsIAtom* aAtom) : mAtom(aAtom) {} + MOZ_IMPLICIT AtomOrString(const nsAString& aString) : mString(aString) {} + AtomOrString(const AtomOrString& aOther) + : mAtom(aOther.mAtom) + , mString(aOther.mString) + { + } + + AtomOrString(AtomOrString&& aOther) + : mAtom(aOther.mAtom.forget()) + , mString(aOther.mString) + { + } + + nsCOMPtr mAtom; + const nsString mString; + }; + + typedef const AtomOrString& KeyType; + typedef const AtomOrString* KeyTypePointer; + typedef mozilla::dom::Element Element; typedef mozilla::net::ReferrerPolicy ReferrerPolicy; - explicit nsIdentifierMapEntry(const nsAString& aKey) : - nsStringHashKey(&aKey), mNameContentList(nullptr) + explicit nsIdentifierMapEntry(const AtomOrString& aKey) + : mKey(aKey) { } - explicit nsIdentifierMapEntry(const nsAString* aKey) : - nsStringHashKey(aKey), mNameContentList(nullptr) + explicit nsIdentifierMapEntry(const AtomOrString* aKey) + : mKey(aKey ? *aKey : nullptr) { } nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther) : - nsStringHashKey(&aOther.GetKey()), + mKey(mozilla::Move(aOther.GetKey())), mIdContentList(mozilla::Move(aOther.mIdContentList)), mNameContentList(aOther.mNameContentList.forget()), mChangeCallbacks(aOther.mChangeCallbacks.forget()), @@ -64,6 +87,42 @@ public: } ~nsIdentifierMapEntry(); + KeyType GetKey() const { return mKey; } + + nsString GetKeyAsString() const + { + if (mKey.mAtom) { + return nsAtomString(mKey.mAtom); + } + + return mKey.mString; + } + + bool KeyEquals(const KeyTypePointer aOtherKey) const + { + if (mKey.mAtom) { + if (aOtherKey->mAtom) { + return mKey.mAtom == aOtherKey->mAtom; + } + + return mKey.mAtom->Equals(aOtherKey->mString); + } + + if (aOtherKey->mAtom) { + return aOtherKey->mAtom->Equals(mKey.mString); + } + + return mKey.mString.Equals(aOtherKey->mString); + } + + static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } + + static PLDHashNumber HashKey(const KeyTypePointer aKey) + { + return aKey->mAtom ? + aKey->mAtom->hash() : mozilla::HashString(aKey->mString); + } + enum { ALLOW_MEMMOVE = false }; void AddNameElement(nsINode* aDocument, Element* aElement); @@ -167,6 +226,7 @@ private: void FireChangeCallbacks(Element* aOldElement, Element* aNewElement, bool aImageOnly = false); + AtomOrString mKey; // empty if there are no elements with this ID. // The elements are stored as weak pointers. AutoTArray mIdContentList; -- cgit v1.2.3 From 43725c7264ca3f63de348d1d1596ce1fe9e64d2d Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Sat, 13 Jun 2020 08:21:48 -0400 Subject: Bug 1426494 - Share more code between nsIDocument and ShadowRoot Tag #1375 --- dom/base/nsIdentifierMapEntry.h | 54 ++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 28 deletions(-) (limited to 'dom/base/nsIdentifierMapEntry.h') diff --git a/dom/base/nsIdentifierMapEntry.h b/dom/base/nsIdentifierMapEntry.h index 41b8b4a83..119a7e453 100644 --- a/dom/base/nsIdentifierMapEntry.h +++ b/dom/base/nsIdentifierMapEntry.h @@ -15,18 +15,24 @@ #include "mozilla/MemoryReporting.h" #include "mozilla/Move.h" -#include "mozilla/dom/Element.h" #include "mozilla/net/ReferrerPolicy.h" #include "nsCOMArray.h" #include "nsCOMPtr.h" -#include "nsContentList.h" #include "nsIAtom.h" -#include "nsIDocument.h" #include "nsTArray.h" #include "nsTHashtable.h" +#include "nsHashKeys.h" class nsIContent; +class nsContentList; +class nsBaseContentList; + +namespace mozilla { +namespace dom { + class Element; +} // namespace dom +} // namespace mozilla /** * Right now our identifier map entries contain information for 'name' @@ -42,6 +48,16 @@ class nsIContent; */ class nsIdentifierMapEntry : public PLDHashEntryHdr { + typedef mozilla::dom::Element Element; + typedef mozilla::net::ReferrerPolicy ReferrerPolicy; + + /** + * @see nsIDocument::IDTargetObserver, this is just here to avoid include + * hell. + */ + typedef bool (* IDTargetObserver)(Element* aOldElement, + Element* aNewelement, void* aData); + public: struct AtomOrString { @@ -66,25 +82,9 @@ public: typedef const AtomOrString& KeyType; typedef const AtomOrString* KeyTypePointer; - typedef mozilla::dom::Element Element; - typedef mozilla::net::ReferrerPolicy ReferrerPolicy; - - explicit nsIdentifierMapEntry(const AtomOrString& aKey) - : mKey(aKey) - { - } - explicit nsIdentifierMapEntry(const AtomOrString* aKey) - : mKey(aKey ? *aKey : nullptr) - { - } - nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther) : - mKey(mozilla::Move(aOther.GetKey())), - mIdContentList(mozilla::Move(aOther.mIdContentList)), - mNameContentList(aOther.mNameContentList.forget()), - mChangeCallbacks(aOther.mChangeCallbacks.forget()), - mImageElement(aOther.mImageElement.forget()) - { - } + explicit nsIdentifierMapEntry(const AtomOrString& aKey); + explicit nsIdentifierMapEntry(const AtomOrString* aKey); + nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther); ~nsIdentifierMapEntry(); KeyType GetKey() const { return mKey; } @@ -131,9 +131,7 @@ public: nsBaseContentList* GetNameContentList() { return mNameContentList; } - bool HasNameElement() const { - return mNameContentList && mNameContentList->Length() != 0; - } + bool HasNameElement() const; /** * Returns the element if we know the element associated with this @@ -173,9 +171,9 @@ public: bool HasIdElementExposedAsHTMLDocumentProperty(); bool HasContentChangeCallback() { return mChangeCallbacks != nullptr; } - void AddContentChangeCallback(nsIDocument::IDTargetObserver aCallback, + void AddContentChangeCallback(IDTargetObserver aCallback, void* aData, bool aForImage); - void RemoveContentChangeCallback(nsIDocument::IDTargetObserver aCallback, + void RemoveContentChangeCallback(IDTargetObserver aCallback, void* aData, bool aForImage); /** @@ -186,7 +184,7 @@ public: void Traverse(nsCycleCollectionTraversalCallback* aCallback); struct ChangeCallback { - nsIDocument::IDTargetObserver mCallback; + IDTargetObserver mCallback; void* mData; bool mForImage; }; -- cgit v1.2.3 From a680bdc637e0393aaa08d575c66f7166b788b443 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Wed, 23 Sep 2020 13:55:00 +0000 Subject: Issue #1656 - Part 1: Nuke most vim config lines in the tree. Since these are just interpreted comments, there's 0 impact on actual code. This removes all lines that match /* vim: set(.*)tw=80: */ with S&R -- there are a few others scattered around which will be removed manually in a second part. --- dom/base/nsIdentifierMapEntry.h | 1 - 1 file changed, 1 deletion(-) (limited to 'dom/base/nsIdentifierMapEntry.h') diff --git a/dom/base/nsIdentifierMapEntry.h b/dom/base/nsIdentifierMapEntry.h index 119a7e453..a43a80c3b 100644 --- a/dom/base/nsIdentifierMapEntry.h +++ b/dom/base/nsIdentifierMapEntry.h @@ -1,5 +1,4 @@ /* -*- 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/. */ -- cgit v1.2.3