mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-17 15:25:52 +00:00
Bug 342252: Turn nsDocumentObserverList into a more generic class. r/sr=bz
This commit is contained in:
parent
e2531b25c0
commit
a99966c50d
@ -139,6 +139,7 @@ CPPSRCS = \
|
||||
nsSyncLoadService.cpp \
|
||||
nsTextFragment.cpp \
|
||||
nsTextNode.cpp \
|
||||
nsTObserverArray.cpp \
|
||||
nsTreeWalker.cpp \
|
||||
nsXMLContentSerializer.cpp \
|
||||
nsXMLHttpRequest.cpp \
|
||||
|
@ -633,73 +633,6 @@ nsDOMImplementation::Init(nsIURI* aDocumentURI, nsIURI* aBaseURI,
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PRBool
|
||||
nsDocumentObserverList::PrependElement(nsIDocumentObserver* aObserver)
|
||||
{
|
||||
PRBool prepended = mObservers.InsertElementAt(aObserver, 0);
|
||||
|
||||
// This introduces an inconsistency -- forward iterators will not see the new
|
||||
// element, while backwards ones will. That's kinda inherent in the
|
||||
// different iteration orders, though.
|
||||
if (prepended) {
|
||||
for (Iterator* iter = mIterators; iter; iter = iter->mNext) {
|
||||
iter->mPosition++;
|
||||
}
|
||||
}
|
||||
|
||||
return prepended;
|
||||
}
|
||||
|
||||
PRBool
|
||||
nsDocumentObserverList::RemoveElement(nsIDocumentObserver* aElement)
|
||||
{
|
||||
PRInt32 index = mObservers.IndexOf(aElement);
|
||||
if (index == -1) {
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
PRBool removed =
|
||||
#endif
|
||||
mObservers.RemoveElementAt(index);
|
||||
NS_ASSERTION(removed, "How could we fail to remove by index?");
|
||||
|
||||
for (Iterator* iter = mIterators; iter; iter = iter->mNext) {
|
||||
// If iter->mPosition == index then forward iterators are safe, since in
|
||||
// that case the position is not affected by the removal; all that's
|
||||
// affected is what element is at that position. Backward iterators,
|
||||
// however, need to decrement mPosition in that case.
|
||||
if (iter->mPosition > index ||
|
||||
(iter->mPosition == index && iter->mStep < 0)) {
|
||||
iter->mPosition--;
|
||||
}
|
||||
}
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
nsDocumentObserverList::Clear()
|
||||
{
|
||||
mObservers.Clear();
|
||||
|
||||
// Reset all iterators to a bogus position so they don't return
|
||||
// anything next time they're called.
|
||||
for (Iterator* iter = mIterators; iter; iter = iter->mNext) {
|
||||
iter->mPosition = -1;
|
||||
}
|
||||
}
|
||||
|
||||
nsIDocumentObserver*
|
||||
nsDocumentObserverList::Iterator::GetNext()
|
||||
{
|
||||
nsIDocumentObserver* ret =
|
||||
NS_STATIC_CAST(nsIDocumentObserver*,
|
||||
mList.mObservers.SafeElementAt(mPosition));
|
||||
mPosition += mStep;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ==================================================================
|
||||
// =
|
||||
// ==================================================================
|
||||
@ -895,7 +828,7 @@ nsDocument::Init()
|
||||
mBindingManager = bindingManager;
|
||||
|
||||
// The binding manager must always be the first observer of the document.
|
||||
mObservers.PrependElement(bindingManager);
|
||||
mObservers.PrependObserver(bindingManager);
|
||||
|
||||
mOnloadBlocker = new nsOnloadBlocker();
|
||||
NS_ENSURE_TRUE(mOnloadBlocker, NS_ERROR_OUT_OF_MEMORY);
|
||||
@ -2302,9 +2235,9 @@ nsDocument::GetScriptLoader()
|
||||
void
|
||||
nsDocument::AddObserver(nsIDocumentObserver* aObserver)
|
||||
{
|
||||
// XXX Make sure the observer isn't already in the list
|
||||
// Make sure the observer isn't already in the list
|
||||
if (!mObservers.Contains(aObserver)) {
|
||||
mObservers.AppendElement(aObserver);
|
||||
mObservers.AppendObserver(aObserver);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2316,7 +2249,7 @@ nsDocument::RemoveObserver(nsIDocumentObserver* aObserver)
|
||||
// observers from the list. This is not a big deal, since we
|
||||
// don't hold a live reference to the observers.
|
||||
if (!mInDestructor) {
|
||||
return mObservers.RemoveElement(aObserver);
|
||||
return mObservers.RemoveObserver(aObserver);
|
||||
}
|
||||
|
||||
return mObservers.Contains(aObserver);
|
||||
|
@ -88,6 +88,7 @@
|
||||
#include "nsILayoutHistoryState.h"
|
||||
#include "nsIRequest.h"
|
||||
#include "nsILoadGroup.h"
|
||||
#include "nsTObserverArray.h"
|
||||
|
||||
// Put these here so all document impls get them automatically
|
||||
#include "nsHTMLStyleSheet.h"
|
||||
@ -262,94 +263,6 @@ private:
|
||||
~nsOnloadBlocker() {}
|
||||
};
|
||||
|
||||
/**
|
||||
* nsDocumentObserverList is the list of nsIDocumentObservers for a document.
|
||||
* It doesn't allow direct reading of the list; all access must take place
|
||||
* through stack-allocated nsDocumentObserverList::ForwardIterator or
|
||||
* nsDocumentObserverList::ReverseIterator objects.
|
||||
*/
|
||||
class nsDocumentObserverList
|
||||
{
|
||||
public:
|
||||
nsDocumentObserverList() :
|
||||
mIterators(nsnull)
|
||||
{}
|
||||
|
||||
class Iterator;
|
||||
friend class Iterator;
|
||||
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
nsIDocumentObserver* GetNext();
|
||||
|
||||
protected:
|
||||
Iterator(PRInt32 aStep, nsDocumentObserverList& aList) :
|
||||
mPosition(aStep > 0 ? 0 : aList.mObservers.Count() - 1),
|
||||
mStep(aStep),
|
||||
mList(aList),
|
||||
mNext(aList.mIterators)
|
||||
{
|
||||
NS_ASSERTION(mStep == 1 || mStep == -1, "Invalid step size");
|
||||
aList.mIterators = this;
|
||||
}
|
||||
|
||||
~Iterator() {
|
||||
NS_ASSERTION(mList.mIterators == this, "Destroyed out of order?");
|
||||
mList.mIterators = mNext;
|
||||
}
|
||||
|
||||
friend class nsDocumentObserverList;
|
||||
|
||||
// Our current position in mObservers
|
||||
PRInt32 mPosition;
|
||||
|
||||
private:
|
||||
// Which direction to move in
|
||||
PRInt32 mStep;
|
||||
|
||||
// The observer array to work with
|
||||
nsDocumentObserverList& mList;
|
||||
|
||||
// Our next iterator.
|
||||
Iterator* mNext;
|
||||
};
|
||||
|
||||
class ForwardIterator : public Iterator
|
||||
{
|
||||
public:
|
||||
ForwardIterator(nsDocumentObserverList& aList) :
|
||||
Iterator(1, aList)
|
||||
{}
|
||||
};
|
||||
|
||||
class ReverseIterator : public Iterator
|
||||
{
|
||||
public:
|
||||
ReverseIterator(nsDocumentObserverList& aList) :
|
||||
Iterator(-1, aList)
|
||||
{}
|
||||
};
|
||||
|
||||
PRBool PrependElement(nsIDocumentObserver* aObserver);
|
||||
|
||||
PRInt32 Contains(nsIDocumentObserver* aPossibleObserver) const {
|
||||
return mObservers.IndexOf(aPossibleObserver) != -1;
|
||||
}
|
||||
|
||||
PRBool AppendElement(nsIDocumentObserver* aElement) {
|
||||
return mObservers.AppendElement(aElement);
|
||||
}
|
||||
|
||||
PRBool RemoveElement(nsIDocumentObserver* aElement);
|
||||
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
nsAutoVoidArray mObservers;
|
||||
Iterator* mIterators;
|
||||
};
|
||||
|
||||
// Base class for our document implementations.
|
||||
//
|
||||
// Note that this class *implements* nsIDOMXMLDocument, but it's not
|
||||
@ -774,7 +687,8 @@ protected:
|
||||
// If you change this, update ContentAppended/Inserted/Removed accordingly.
|
||||
#define NS_DOCUMENT_NOTIFY_OBSERVERS(func_, params_) \
|
||||
do { \
|
||||
nsDocumentObserverList::ReverseIterator iter_(mObservers); \
|
||||
nsTObserverArray<nsIDocumentObserver>::ReverseIterator \
|
||||
iter_(mObservers); \
|
||||
nsCOMPtr<nsIDocumentObserver> obs_; \
|
||||
while ((obs_ = iter_.GetNext())) { \
|
||||
obs_ -> func_ params_ ; \
|
||||
@ -783,7 +697,8 @@ protected:
|
||||
|
||||
#define NS_DOCUMENT_FORWARD_NOTIFY_OBSERVERS(func_, params_) \
|
||||
do { \
|
||||
nsDocumentObserverList::ForwardIterator iter_(mObservers); \
|
||||
nsTObserverArray<nsIDocumentObserver>::ForwardIterator \
|
||||
iter_(mObservers); \
|
||||
nsCOMPtr<nsIDocumentObserver> obs_; \
|
||||
while ((obs_ = iter_.GetNext())) { \
|
||||
obs_ -> func_ params_ ; \
|
||||
@ -814,8 +729,8 @@ protected:
|
||||
nsCOMArray<nsIStyleSheet> mStyleSheets;
|
||||
nsCOMArray<nsIStyleSheet> mCatalogSheets;
|
||||
|
||||
// Basically always has at least 1 entry
|
||||
nsDocumentObserverList mObservers;
|
||||
// Array of observers
|
||||
nsTObserverArray<nsIDocumentObserver> mObservers;
|
||||
|
||||
// The document's script global object, the object from which the
|
||||
// document can get its script context and scope. This is the
|
||||
|
69
content/base/src/nsTObserverArray.cpp
Executable file
69
content/base/src/nsTObserverArray.cpp
Executable file
@ -0,0 +1,69 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Mozilla Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2006
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Jonas Sicking <jonas@sicking.cc> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsTObserverArray.h"
|
||||
|
||||
void
|
||||
nsTObserverArray_base::AdjustIterators(PRInt32 aModPos,
|
||||
PRInt32 aAdjustment)
|
||||
{
|
||||
NS_PRECONDITION(aAdjustment == -1 || aAdjustment == 1,
|
||||
"invalid adjustment");
|
||||
Iterator_base* iter = mIterators;
|
||||
while (iter) {
|
||||
NS_ASSERTION(&(iter->mArray) == this, "wrong array");
|
||||
|
||||
if (iter->mPosition > aModPos) {
|
||||
iter->mPosition += aAdjustment;
|
||||
}
|
||||
iter = iter->mNext;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
nsTObserverArray_base::Clear()
|
||||
{
|
||||
mObservers.Clear();
|
||||
|
||||
Iterator_base* iter = mIterators;
|
||||
while (iter) {
|
||||
NS_ASSERTION(&(iter->mArray) == this, "wrong array");
|
||||
|
||||
iter->mPosition = 0;
|
||||
iter = iter->mNext;
|
||||
}
|
||||
}
|
208
content/base/src/nsTObserverArray.h
Executable file
208
content/base/src/nsTObserverArray.h
Executable file
@ -0,0 +1,208 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Mozilla Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2006
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Jonas Sicking <jonas@sicking.cc> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsTArray.h"
|
||||
#include "nsVoidArray.h"
|
||||
|
||||
class nsTObserverArray_base {
|
||||
public:
|
||||
class Iterator_base;
|
||||
friend class Iterator_base;
|
||||
|
||||
class Iterator_base {
|
||||
protected:
|
||||
friend nsTObserverArray_base;
|
||||
|
||||
Iterator_base(PRInt32 aPosition, nsTObserverArray_base& aArray)
|
||||
: mPosition(aPosition),
|
||||
mNext(aArray.mIterators),
|
||||
mArray(aArray) {
|
||||
aArray.mIterators = this;
|
||||
}
|
||||
|
||||
~Iterator_base() {
|
||||
NS_ASSERTION(mArray.mIterators == this,
|
||||
"Iterators must currently be destroyed in opposite order "
|
||||
"from the construction order. It is suggested that you "
|
||||
"simply put them on the stack");
|
||||
mArray.mIterators = mNext;
|
||||
}
|
||||
|
||||
// This function exists solely to avoid having to make the subclasses
|
||||
// into friends of nsTObserverArray_base
|
||||
void* GetSafeElementAt(PRInt32 aIndex) {
|
||||
return mArray.mObservers.SafeElementAt(aIndex);
|
||||
}
|
||||
|
||||
// The current position of the iterator. It's exact meaning differs
|
||||
// depending on if the array is iterated forwards or backwards. See
|
||||
// nsTObserverArray<T>::ForwardIterator and
|
||||
// nsTObserverArray<T>::ReverseIterator
|
||||
PRInt32 mPosition;
|
||||
|
||||
// The next iterator currently iterating the same array
|
||||
Iterator_base* mNext;
|
||||
|
||||
// The array we're iterating
|
||||
nsTObserverArray_base& mArray;
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes all observers and collapses all iterators to the beginning of
|
||||
* the array. The result is that forward iterators will see all elements
|
||||
* in the array, and backward iterators will not see any more elements.
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
protected:
|
||||
nsTObserverArray_base()
|
||||
: mIterators(nsnull) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts iterators after an element has been inserted or removed
|
||||
* from the array.
|
||||
* @param modPos Position where elements were added or removed.
|
||||
* @param adjustment -1 if an element was removed, 1 if an element was
|
||||
* added.
|
||||
*/
|
||||
void AdjustIterators(PRInt32 aModPos, PRInt32 aAdjustment);
|
||||
|
||||
Iterator_base* mIterators;
|
||||
nsVoidArray mObservers;
|
||||
};
|
||||
|
||||
/**
|
||||
* An array of observers. Like a normal array, but supports iterators that are
|
||||
* stable even if the array is modified during iteration.
|
||||
* The template parameter is the type of observer the array will hold pointers
|
||||
* to.
|
||||
*/
|
||||
|
||||
template<class T>
|
||||
class nsTObserverArray : public nsTObserverArray_base {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Adds an observer to the beginning of the array
|
||||
* @param aObserver Observer to add
|
||||
*/
|
||||
PRBool PrependObserver(T* aObserver) {
|
||||
PRBool res = mObservers.InsertElementAt(aObserver, 0);
|
||||
if (res) {
|
||||
AdjustIterators(0, 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an observer to the end of the array
|
||||
* @param aObserver Observer to add
|
||||
* @return True on success, false otherwise
|
||||
*/
|
||||
PRBool AppendObserver(T* aObserver) {
|
||||
return mObservers.AppendElement(aObserver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an observer from the array
|
||||
* @param aObserver Observer to remove
|
||||
* @return True if observer was found and removed, false otherwise
|
||||
*/
|
||||
PRBool RemoveObserver(T* aObserver) {
|
||||
PRInt32 index = mObservers.IndexOf(aObserver);
|
||||
if (index < 0) {
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
mObservers.RemoveElementAt(index);
|
||||
AdjustIterators(index, -1);
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
PRBool Contains(T* aObserver) const {
|
||||
return mObservers.IndexOf(aObserver) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterators
|
||||
*/
|
||||
|
||||
// Iterates the array forward from beginning to end.
|
||||
// mPosition points to the element that will be returned on next call
|
||||
// to GetNext
|
||||
class ForwardIterator : public nsTObserverArray_base::Iterator_base {
|
||||
public:
|
||||
ForwardIterator(nsTObserverArray<T>& aArray)
|
||||
: Iterator_base(0, aArray) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next element and steps one step.
|
||||
* Returns null if there are no more observers. Once null is returned
|
||||
* the iterator becomes invalid and GetNext must not be called any more.
|
||||
* @return The next observer.
|
||||
*/
|
||||
T* GetNext() {
|
||||
return NS_STATIC_CAST(T*, GetSafeElementAt(mPosition++));
|
||||
}
|
||||
};
|
||||
|
||||
class ReverseIterator;
|
||||
friend class ReverseIterator;
|
||||
|
||||
// Iterates the array backwards from end to beginning
|
||||
// mPosition points to the element that was returned from last call to
|
||||
// GetNext
|
||||
class ReverseIterator : public nsTObserverArray_base::Iterator_base {
|
||||
public:
|
||||
ReverseIterator(nsTObserverArray<T>& aArray)
|
||||
: Iterator_base(aArray.mObservers.Count(), aArray) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next element and steps one step.
|
||||
* Returns null if there are no more observers. Once null is returned
|
||||
* the iterator becomes invalid and GetNext must not be called any more.
|
||||
* @return The next observer.
|
||||
*/
|
||||
T* GetNext() {
|
||||
return NS_STATIC_CAST(T*, GetSafeElementAt(--mPosition));
|
||||
}
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user