2017-06-20 16:57:08 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
/*
|
2019-04-03 10:02:34 +03:00
|
|
|
* Entry for the Document or ShadowRoot's identifier map.
|
2017-06-20 16:57:08 +03:00
|
|
|
*/
|
|
|
|
|
2019-01-08 13:15:55 +03:00
|
|
|
#ifndef mozilla_IdentifierMapEntry_h
|
|
|
|
#define mozilla_IdentifierMapEntry_h
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
#include "PLDHashTable.h"
|
|
|
|
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/Move.h"
|
2019-04-03 10:02:34 +03:00
|
|
|
#include "mozilla/dom/TreeOrderedArray.h"
|
2017-06-20 16:57:08 +03:00
|
|
|
#include "mozilla/net/ReferrerPolicy.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2018-04-16 16:18:48 +03:00
|
|
|
#include "nsHashKeys.h"
|
2017-06-20 16:57:08 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
|
|
|
|
class nsIContent;
|
2017-12-20 08:46:01 +03:00
|
|
|
class nsContentList;
|
|
|
|
class nsBaseContentList;
|
2017-06-20 16:57:08 +03:00
|
|
|
|
2018-04-16 16:18:48 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2019-01-08 13:15:55 +03:00
|
|
|
class Document;
|
2018-04-16 16:18:48 +03:00
|
|
|
class Element;
|
|
|
|
} // namespace dom
|
|
|
|
|
2017-06-20 16:57:08 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2019-01-08 13:15:55 +03:00
|
|
|
* the ID's IdentifierMapEntry, the document.all result is released too.
|
2017-06-20 16:57:08 +03:00
|
|
|
* Perhaps the document.all results should have their own hashtable
|
|
|
|
* in nsHTMLDocument.
|
|
|
|
*/
|
2019-01-08 13:15:55 +03:00
|
|
|
class IdentifierMapEntry : public PLDHashEntryHdr {
|
|
|
|
typedef dom::Document Document;
|
|
|
|
typedef dom::Element Element;
|
|
|
|
typedef net::ReferrerPolicy ReferrerPolicy;
|
2017-12-20 08:46:01 +03:00
|
|
|
|
|
|
|
/**
|
2019-01-02 16:05:23 +03:00
|
|
|
* @see Document::IDTargetObserver, this is just here to avoid include hell.
|
2017-12-20 08:46:01 +03:00
|
|
|
*/
|
|
|
|
typedef bool (*IDTargetObserver)(Element* aOldElement, Element* aNewelement,
|
|
|
|
void* aData);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-20 16:57:08 +03:00
|
|
|
public:
|
|
|
|
struct AtomOrString {
|
2017-10-03 01:05:19 +03:00
|
|
|
MOZ_IMPLICIT AtomOrString(nsAtom* aAtom) : mAtom(aAtom) {}
|
2017-06-20 16:57:08 +03:00
|
|
|
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) {}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> mAtom;
|
2017-06-20 16:57:08 +03:00
|
|
|
const nsString mString;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef const AtomOrString& KeyType;
|
|
|
|
typedef const AtomOrString* KeyTypePointer;
|
|
|
|
|
2019-01-08 13:15:55 +03:00
|
|
|
explicit IdentifierMapEntry(const AtomOrString& aKey);
|
|
|
|
explicit IdentifierMapEntry(const AtomOrString* aKey);
|
|
|
|
IdentifierMapEntry(IdentifierMapEntry&& aOther);
|
|
|
|
~IdentifierMapEntry();
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
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) {
|
2019-01-08 13:15:55 +03:00
|
|
|
return aKey->mAtom ? aKey->mAtom->hash() : HashString(aKey->mString);
|
2017-06-20 16:57:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum { ALLOW_MEMMOVE = false };
|
|
|
|
|
|
|
|
void AddNameElement(nsINode* aDocument, Element* aElement);
|
|
|
|
void RemoveNameElement(Element* aElement);
|
|
|
|
bool IsEmpty();
|
|
|
|
nsBaseContentList* GetNameContentList() { return mNameContentList; }
|
2017-12-20 08:46:01 +03:00
|
|
|
bool HasNameElement() const;
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the element if we know the element associated with this
|
|
|
|
* id. Otherwise returns null.
|
|
|
|
*/
|
2019-04-03 10:02:34 +03:00
|
|
|
Element* GetIdElement() { return mIdContentList->SafeElementAt(0); }
|
|
|
|
|
2017-06-20 16:57:08 +03:00
|
|
|
/**
|
|
|
|
* Returns the list of all elements associated with this id.
|
|
|
|
*/
|
|
|
|
const nsTArray<Element*>& GetIdElements() const { return mIdContentList; }
|
2019-04-03 10:02:34 +03:00
|
|
|
|
2017-06-20 16:57:08 +03:00
|
|
|
/**
|
|
|
|
* If this entry has a non-null image element set (using SetImageElement),
|
|
|
|
* the image element will be returned, otherwise the same as GetIdElement().
|
|
|
|
*/
|
2019-04-03 10:02:34 +03:00
|
|
|
Element* GetImageIdElement() {
|
|
|
|
return mImageElement ? mImageElement.get() : GetIdElement();
|
|
|
|
}
|
|
|
|
|
2017-06-20 16:57:08 +03:00
|
|
|
/**
|
|
|
|
* This can fire ID change callbacks.
|
|
|
|
*/
|
2018-10-29 18:56:22 +03:00
|
|
|
void AddIdElement(Element* aElement);
|
2017-06-20 16:57:08 +03:00
|
|
|
/**
|
|
|
|
* 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; }
|
2017-12-20 08:46:01 +03:00
|
|
|
void AddContentChangeCallback(IDTargetObserver aCallback, void* aData,
|
2017-06-20 16:57:08 +03:00
|
|
|
bool aForImage);
|
2017-12-20 08:46:01 +03:00
|
|
|
void RemoveContentChangeCallback(IDTargetObserver aCallback, void* aData,
|
2017-06-20 16:57:08 +03:00
|
|
|
bool aForImage);
|
|
|
|
|
|
|
|
void Traverse(nsCycleCollectionTraversalCallback* aCallback);
|
|
|
|
|
|
|
|
struct ChangeCallback {
|
2017-12-20 08:46:01 +03:00
|
|
|
IDTargetObserver mCallback;
|
2017-06-20 16:57:08 +03:00
|
|
|
void* mData;
|
|
|
|
bool mForImage;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ChangeCallbackEntry : public PLDHashEntryHdr {
|
|
|
|
typedef const ChangeCallback KeyType;
|
|
|
|
typedef const ChangeCallback* KeyTypePointer;
|
|
|
|
|
|
|
|
explicit ChangeCallbackEntry(const ChangeCallback* aKey) : mKey(*aKey) {}
|
Bug 1415980 - make hash keys movable and not copyable; r=erahm
Everything that goes in a PLDHashtable (and its derivatives, like
nsTHashtable) needs to inherit from PLDHashEntryHdr. But through a lack
of enforcement, copy constructors for these derived classes didn't
explicitly invoke the copy constructor for PLDHashEntryHdr (and the
compiler didn't invoke the copy constructor for us). Instead,
PLDHashTable explicitly copied around the bits that the copy constructor
would have.
The current setup has two problems:
1) Derived classes should be using move construction, not copy
construction, since anything that's shuffling hash table keys/entries
around will be using move construction.
2) Derived classes should take responsibility for transferring bits of
superclass state around, and not rely on something else to handle that.
The second point is not a huge problem for PLDHashTable (PLDHashTable
only has to copy PLDHashEntryHdr's bits in a single place), but future
hash table implementations that might move entries around more
aggressively would have to insert compensation code all over the
place. Additionally, if moving entries is implemented via memcpy (which
is quite common), PLDHashTable copying around bits *again* is
inefficient.
Let's fix all these problems in one go, by:
1) Explicitly declaring the set of constructors that PLDHashEntryHdr
implements (and does not implement). In particular, the copy
constructor is deleted, so any derived classes that attempt to make
themselves copyable will be detected at compile time: the compiler
will complain that the superclass type is not copyable.
This change on its own will result in many compiler errors, so...
2) Change any derived classes to implement move constructors instead of
copy constructors. Note that some of these move constructors are,
strictly speaking, unnecessary, since the relevant classes are moved
via memcpy in nsTHashtable and its derivatives.
2018-09-20 18:20:36 +03:00
|
|
|
ChangeCallbackEntry(ChangeCallbackEntry&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mKey(std::move(aOther.mKey)) {}
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
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) {
|
2019-01-08 13:15:55 +03:00
|
|
|
return HashGeneric(aKey->mCallback, aKey->mData);
|
2017-06-20 16:57:08 +03:00
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
ChangeCallback mKey;
|
|
|
|
};
|
|
|
|
|
2019-01-08 13:15:55 +03:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
private:
|
2019-01-08 13:15:55 +03:00
|
|
|
IdentifierMapEntry(const IdentifierMapEntry& aOther) = delete;
|
|
|
|
IdentifierMapEntry& operator=(const IdentifierMapEntry& aOther) = delete;
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
void FireChangeCallbacks(Element* aOldElement, Element* aNewElement,
|
|
|
|
bool aImageOnly = false);
|
|
|
|
|
|
|
|
AtomOrString mKey;
|
2019-04-03 10:02:34 +03:00
|
|
|
dom::TreeOrderedArray<Element> mIdContentList;
|
2017-06-20 16:57:08 +03:00
|
|
|
RefPtr<nsBaseContentList> mNameContentList;
|
|
|
|
nsAutoPtr<nsTHashtable<ChangeCallbackEntry> > mChangeCallbacks;
|
|
|
|
RefPtr<Element> mImageElement;
|
|
|
|
};
|
|
|
|
|
2019-01-08 13:15:55 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // #ifndef mozilla_IdentifierMapEntry_h
|