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/. */
7 #ifndef mozilla_dom_IDTracker_h_
8 #define mozilla_dom_IDTracker_h_
10 #include "mozilla/Attributes.h"
11 #include "nsIObserver.h"
12 #include "nsThreadUtils.h"
18 class nsIReferrerInfo
;
20 namespace mozilla::dom
{
23 class DocumentOrShadowRoot
;
27 * Class to track what element is referenced by a given ID.
29 * To use it, call one of the Reset methods to set it up to watch a given ID.
30 * Call get() anytime to determine the referenced element (which may be null if
31 * the element isn't found). When the element changes, ElementChanged
32 * will be called, so subclass this class if you want to receive that
33 * notification. ElementChanged runs at safe-for-script time, i.e. outside
34 * of the content update. Call Unlink() if you want to stop watching
35 * for changes (get() will then return null).
37 * By default this is a single-shot tracker --- i.e., when ElementChanged
38 * fires, we will automatically stop tracking. get() will continue to return
39 * the changed-to element.
40 * Override IsPersistent to return true if you want to keep tracking after
45 using Element
= mozilla::dom::Element
;
52 * Find which element, if any, is referenced.
54 Element
* get() { return mElement
; }
57 * Set up a reference to another element, identified by the fragment
58 * identifier in aURI. If aURI identifies an element in a document that is
59 * not aFrom's document, then an ExternalResourceLoad object will be created
60 * to load and store that document in the background as a resource document
61 * (until we, and any other observers, no longer observe it).
63 * This can be called multiple times with different URIs to change which
64 * element is being tracked, but these changes do not trigger ElementChanged.
66 * @param aFrom The source element that has made the reference to aURI.
67 * @param aURI A URI containing a fragment identifier that identifies the
69 * @param aReferrerInfo The referrerInfo for the source element. Needed if
70 * the referenced element is in an external resource document.
71 * @param aReferenceImage Whether the reference comes from a -moz-element
72 * property (that is, we're creating a reference an "image element", which
73 * is subject to the document's mozSetImageElement overriding mechanism).
75 void ResetToURIWithFragmentID(nsIContent
* aFrom
, nsIURI
* aURI
,
76 nsIReferrerInfo
* aReferrerInfo
,
77 bool aReferenceImage
= false);
80 * A variation on ResetToURIWithFragmentID() to set up a reference that
81 * consists only of a fragment identifier, referencing an element in the same
84 * @param aFrom The source element that is making the reference.
85 * @param aLocalRef The fragment identifier that identifies the target
86 * element. Must begin with "#".
88 void ResetToLocalFragmentID(Element
& aFrom
, const nsAString
& aLocalRef
);
91 * A variation on ResetToURIWithFragmentID() to set up a reference that
92 * consists of a pre-parsed ID, referencing an element in the same document
95 * @param aFrom The source element that is making the reference.
96 * @param aID The ID of the target element.
98 void ResetToID(Element
& aFrom
, nsAtom
* aID
);
101 * Clears the reference. ElementChanged is not triggered. get() will return
106 void Traverse(nsCycleCollectionTraversalCallback
* aCB
);
110 * Override this to be notified of element changes. Don't forget
111 * to call this superclass method to change mElement. This is called
112 * at script-runnable time.
114 virtual void ElementChanged(Element
* aFrom
, Element
* aTo
);
117 * Override this to convert from a single-shot notification to
118 * a persistent notification.
120 virtual bool IsPersistent() { return false; }
123 * Set ourselves up with our new document. Note that aDocument might be
124 * null. Either aWatch must be false or aRef must be empty.
126 void HaveNewDocumentOrShadowRoot(DocumentOrShadowRoot
*, bool aWatch
,
127 const nsString
& aRef
);
130 static bool Observe(Element
* aOldElement
, Element
* aNewElement
, void* aData
);
132 class Notification
: public nsISupports
{
134 virtual void SetTo(Element
* aTo
) = 0;
135 virtual void Clear() { mTarget
= nullptr; }
136 virtual ~Notification() = default;
139 explicit Notification(IDTracker
* aTarget
) : mTarget(aTarget
) {
140 MOZ_ASSERT(aTarget
, "Must have a target");
145 class ChangeNotification
: public mozilla::Runnable
, public Notification
{
147 ChangeNotification(IDTracker
* aTarget
, Element
* aFrom
, Element
* aTo
);
149 // We need to actually declare all of nsISupports, because
150 // Notification inherits from it but doesn't declare it.
151 NS_DECL_ISUPPORTS_INHERITED
152 NS_IMETHOD
Run() override
{
154 mTarget
->mPendingNotification
= nullptr;
155 mTarget
->ElementChanged(mFrom
, mTo
);
159 void SetTo(Element
* aTo
) override
;
160 void Clear() override
;
163 virtual ~ChangeNotification();
165 RefPtr
<Element
> mFrom
;
168 friend class ChangeNotification
;
170 class DocumentLoadNotification
: public Notification
, public nsIObserver
{
172 DocumentLoadNotification(IDTracker
* aTarget
, const nsString
& aRef
)
173 : Notification(aTarget
) {
174 if (!mTarget
->IsPersistent()) {
182 virtual ~DocumentLoadNotification() = default;
184 virtual void SetTo(Element
* aTo
) override
{}
188 friend class DocumentLoadNotification
;
190 DocumentOrShadowRoot
* GetWatchDocOrShadowRoot() const;
192 RefPtr
<nsAtom
> mWatchID
;
194 mWatchDocumentOrShadowRoot
; // Always a `DocumentOrShadowRoot`.
195 RefPtr
<Element
> mElement
;
196 RefPtr
<Notification
> mPendingNotification
;
197 bool mReferencingImage
= false;
200 inline void ImplCycleCollectionUnlink(IDTracker
& aField
) { aField
.Unlink(); }
202 inline void ImplCycleCollectionTraverse(
203 nsCycleCollectionTraversalCallback
& aCallback
, IDTracker
& aField
,
204 const char* aName
, uint32_t aFlags
= 0) {
205 aField
.Traverse(&aCallback
);
208 } // namespace mozilla::dom
210 #endif /* mozilla_dom_IDTracker_h_ */