2015-05-03 12:32:37 -07: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: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2008-06-24 18:55:43 -07:00
|
|
|
|
|
|
|
#ifndef NSREFERENCEDELEMENT_H_
|
|
|
|
#define NSREFERENCEDELEMENT_H_
|
|
|
|
|
2013-05-29 13:43:41 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-05-14 10:04:51 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2008-06-24 18:55:43 -07:00
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
class nsIURI;
|
|
|
|
|
|
|
|
/**
|
2010-05-14 10:04:51 -07:00
|
|
|
* Class to track what element is referenced by a given ID.
|
2008-06-24 18:55:43 -07:00
|
|
|
*
|
2008-07-01 19:57:43 -07:00
|
|
|
* To use it, call Reset() to set it up to watch a given URI. Call get()
|
|
|
|
* anytime to determine the referenced element (which may be null if
|
2010-05-14 10:04:51 -07:00
|
|
|
* the element isn't found). When the element changes, ElementChanged
|
2008-07-01 19:57:43 -07:00
|
|
|
* will be called, so subclass this class if you want to receive that
|
2010-05-14 10:04:51 -07:00
|
|
|
* notification. ElementChanged runs at safe-for-script time, i.e. outside
|
2008-07-01 19:57:43 -07:00
|
|
|
* of the content update. Call Unlink() if you want to stop watching
|
|
|
|
* for changes (get() will then return null).
|
|
|
|
*
|
2010-05-14 10:04:51 -07:00
|
|
|
* By default this is a single-shot tracker --- i.e., when ElementChanged
|
2008-07-01 19:57:43 -07:00
|
|
|
* fires, we will automatically stop tracking. get() will continue to return
|
|
|
|
* the changed-to element.
|
2011-10-17 07:59:28 -07:00
|
|
|
* Override IsPersistent to return true if you want to keep tracking after
|
2008-07-01 19:57:43 -07:00
|
|
|
* the first change.
|
2008-06-24 18:55:43 -07:00
|
|
|
*/
|
|
|
|
class nsReferencedElement {
|
|
|
|
public:
|
2010-05-14 10:04:51 -07:00
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
|
2008-06-24 18:55:43 -07:00
|
|
|
nsReferencedElement() {}
|
|
|
|
~nsReferencedElement() {
|
|
|
|
Unlink();
|
|
|
|
}
|
2008-07-01 19:57:43 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find which element, if any, is referenced.
|
|
|
|
*/
|
2010-05-14 10:04:51 -07:00
|
|
|
Element* get() { return mElement; }
|
2008-06-24 18:55:43 -07:00
|
|
|
|
2008-07-01 19:57:43 -07:00
|
|
|
/**
|
|
|
|
* Set up the reference. This can be called multiple times to
|
|
|
|
* change which reference is being tracked, but these changes
|
2010-05-14 10:04:51 -07:00
|
|
|
* do not trigger ElementChanged.
|
2008-07-01 19:57:43 -07:00
|
|
|
* @param aFrom the source element for context
|
|
|
|
* @param aURI the URI containing a hash-reference to the element
|
|
|
|
* @param aWatch if false, then we do not set up the notifications to track
|
2010-05-14 10:04:51 -07:00
|
|
|
* changes, so ElementChanged won't fire and get() will always return the same
|
2008-07-01 19:57:43 -07:00
|
|
|
* value, the current element for the ID.
|
2010-08-13 06:35:36 -07:00
|
|
|
* @param aReferenceImage whether the ID references image elements which are
|
|
|
|
* subject to the document's mozSetImageElement overriding mechanism.
|
2008-07-01 19:57:43 -07:00
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
void Reset(nsIContent* aFrom, nsIURI* aURI, bool aWatch = true,
|
|
|
|
bool aReferenceImage = false);
|
2010-01-12 12:00:49 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A variation on Reset() to set up a reference that consists of the ID of
|
|
|
|
* an element in the same document as aFrom.
|
|
|
|
* @param aFrom the source element for context
|
|
|
|
* @param aID the ID of the element
|
|
|
|
* @param aWatch if false, then we do not set up the notifications to track
|
2010-05-14 10:04:51 -07:00
|
|
|
* changes, so ElementChanged won't fire and get() will always return the same
|
2010-01-12 12:00:49 -08:00
|
|
|
* value, the current element for the ID.
|
|
|
|
*/
|
|
|
|
void ResetWithID(nsIContent* aFrom, const nsString& aID,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aWatch = true);
|
2010-01-12 12:00:49 -08:00
|
|
|
|
2008-07-01 19:57:43 -07:00
|
|
|
/**
|
2010-05-14 10:04:51 -07:00
|
|
|
* Clears the reference. ElementChanged is not triggered. get() will return
|
2008-07-01 19:57:43 -07:00
|
|
|
* null.
|
|
|
|
*/
|
2008-06-24 18:55:43 -07:00
|
|
|
void Unlink();
|
|
|
|
|
|
|
|
void Traverse(nsCycleCollectionTraversalCallback* aCB);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
2010-05-14 10:04:51 -07:00
|
|
|
* Override this to be notified of element changes. Don't forget
|
|
|
|
* to call this superclass method to change mElement. This is called
|
2008-07-01 19:57:43 -07:00
|
|
|
* at script-runnable time.
|
2008-06-24 18:55:43 -07:00
|
|
|
*/
|
2010-05-14 10:04:51 -07:00
|
|
|
virtual void ElementChanged(Element* aFrom, Element* aTo) {
|
|
|
|
mElement = aTo;
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override this to convert from a single-shot notification to
|
|
|
|
* a persistent notification.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool IsPersistent() { return false; }
|
2008-09-28 12:16:15 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set ourselves up with our new document. Note that aDocument might be
|
|
|
|
* null. Either aWatch must be false or aRef must be empty.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
void HaveNewDocument(nsIDocument* aDocument, bool aWatch,
|
2008-09-28 12:16:15 -07:00
|
|
|
const nsString& aRef);
|
2008-06-24 18:55:43 -07:00
|
|
|
|
|
|
|
private:
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool Observe(Element* aOldElement,
|
2010-05-14 10:04:51 -07:00
|
|
|
Element* aNewElement, void* aData);
|
2008-06-24 18:55:43 -07:00
|
|
|
|
2008-09-28 12:16:15 -07:00
|
|
|
class Notification : public nsISupports {
|
2008-06-24 18:55:43 -07:00
|
|
|
public:
|
2010-05-14 10:04:51 -07:00
|
|
|
virtual void SetTo(Element* aTo) = 0;
|
2012-07-30 07:20:58 -07:00
|
|
|
virtual void Clear() { mTarget = nullptr; }
|
2008-09-28 12:16:15 -07:00
|
|
|
virtual ~Notification() {}
|
|
|
|
protected:
|
2014-08-08 06:49:39 -07:00
|
|
|
explicit Notification(nsReferencedElement* aTarget)
|
2008-09-28 12:16:15 -07:00
|
|
|
: mTarget(aTarget)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aTarget, "Must have a target");
|
|
|
|
}
|
|
|
|
nsReferencedElement* mTarget;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChangeNotification : public nsRunnable,
|
|
|
|
public Notification
|
|
|
|
{
|
|
|
|
public:
|
2010-05-14 10:04:51 -07:00
|
|
|
ChangeNotification(nsReferencedElement* aTarget,
|
|
|
|
Element* aFrom, Element* aTo)
|
2008-09-28 12:16:15 -07:00
|
|
|
: Notification(aTarget), mFrom(aFrom), mTo(aTo)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2015-03-21 09:28:04 -07:00
|
|
|
NS_IMETHOD Run() override {
|
2008-06-24 18:55:43 -07:00
|
|
|
if (mTarget) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mTarget->mPendingNotification = nullptr;
|
2010-05-14 10:04:51 -07:00
|
|
|
mTarget->ElementChanged(mFrom, mTo);
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void SetTo(Element* aTo) override { mTo = aTo; }
|
|
|
|
virtual void Clear() override
|
2008-09-28 12:16:15 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
Notification::Clear(); mFrom = nullptr; mTo = nullptr;
|
2008-09-28 12:16:15 -07:00
|
|
|
}
|
|
|
|
protected:
|
2014-07-08 14:23:16 -07:00
|
|
|
virtual ~ChangeNotification() {}
|
|
|
|
|
2010-07-30 20:44:45 -07:00
|
|
|
nsRefPtr<Element> mFrom;
|
|
|
|
nsRefPtr<Element> mTo;
|
2008-06-24 18:55:43 -07:00
|
|
|
};
|
2008-09-28 12:16:15 -07:00
|
|
|
friend class ChangeNotification;
|
|
|
|
|
|
|
|
class DocumentLoadNotification : public Notification,
|
|
|
|
public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DocumentLoadNotification(nsReferencedElement* aTarget,
|
|
|
|
const nsString& aRef) :
|
|
|
|
Notification(aTarget)
|
|
|
|
{
|
|
|
|
if (!mTarget->IsPersistent()) {
|
|
|
|
mRef = aRef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
private:
|
2014-06-24 19:09:15 -07:00
|
|
|
virtual ~DocumentLoadNotification() {}
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void SetTo(Element* aTo) override { }
|
2008-06-24 18:55:43 -07:00
|
|
|
|
2008-09-28 12:16:15 -07:00
|
|
|
nsString mRef;
|
|
|
|
};
|
|
|
|
friend class DocumentLoadNotification;
|
|
|
|
|
2008-06-24 18:55:43 -07:00
|
|
|
nsCOMPtr<nsIAtom> mWatchID;
|
|
|
|
nsCOMPtr<nsIDocument> mWatchDocument;
|
2010-07-30 20:44:45 -07:00
|
|
|
nsRefPtr<Element> mElement;
|
2008-06-24 18:55:43 -07:00
|
|
|
nsRefPtr<Notification> mPendingNotification;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mReferencingImage;
|
2008-06-24 18:55:43 -07:00
|
|
|
};
|
|
|
|
|
2014-09-10 03:18:57 -07:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(nsReferencedElement& aField)
|
|
|
|
{
|
|
|
|
aField.Unlink();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsReferencedElement& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
aField.Traverse(&aCallback);
|
|
|
|
}
|
|
|
|
|
2008-06-24 18:55:43 -07:00
|
|
|
#endif /*NSREFERENCEDELEMENT_H_*/
|