1999-04-06 20:54:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
1999-04-06 20:54:09 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* 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.
|
1999-04-06 20:54:09 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-04-06 20:54:09 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:43:54 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-04-06 20:54:09 +00:00
|
|
|
*/
|
|
|
|
|
1999-12-21 00:12:25 +00:00
|
|
|
#define NS_WEAK_OBSERVERS
|
1999-12-19 17:14:47 +00:00
|
|
|
|
1999-04-06 20:54:09 +00:00
|
|
|
#define NS_IMPL_IDS
|
|
|
|
#include "pratom.h"
|
|
|
|
#include "nsIObserverList.h"
|
|
|
|
#include "nsObserverList.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAutoLock.h"
|
1999-12-08 05:24:14 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIWeakReference.h"
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define NS_AUTOLOCK(__monitor) nsAutoLock __lock(__monitor)
|
|
|
|
|
1999-08-23 10:14:16 +00:00
|
|
|
static NS_DEFINE_CID(kObserverListCID, NS_OBSERVERLIST_CID);
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
|
1999-12-08 05:24:14 +00:00
|
|
|
class nsObserverListEnumerator : public nsIBidirectionalEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsObserverListEnumerator( nsISupportsArray* );
|
1999-12-24 03:42:54 +00:00
|
|
|
virtual ~nsObserverListEnumerator() {};
|
1999-12-08 05:24:14 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIENUMERATOR
|
|
|
|
NS_DECL_NSIBIDIRECTIONALENUMERATOR
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRUint32 GetTargetArraySize() const;
|
|
|
|
nsresult MoveToIndex( PRUint32 );
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsISupportsArray> mTargetArray;
|
|
|
|
PRUint32 mCurrentItemIndex;
|
|
|
|
};
|
|
|
|
|
2000-03-05 21:26:01 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsObserverListEnumerator,
|
|
|
|
nsIBidirectionalEnumerator,
|
|
|
|
nsIEnumerator)
|
1999-12-08 05:24:14 +00:00
|
|
|
|
|
|
|
nsObserverListEnumerator::nsObserverListEnumerator( nsISupportsArray* anArray )
|
|
|
|
: mRefCnt(0),
|
|
|
|
mTargetArray(anArray),
|
|
|
|
mCurrentItemIndex(0)
|
|
|
|
{
|
2000-03-05 21:26:01 +00:00
|
|
|
NS_INIT_REFCNT();
|
1999-12-08 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
nsObserverListEnumerator::GetTargetArraySize() const
|
|
|
|
{
|
|
|
|
PRUint32 array_size = 0;
|
|
|
|
mTargetArray->Count(&array_size);
|
|
|
|
return array_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsObserverListEnumerator::MoveToIndex( PRUint32 aNewIndex )
|
|
|
|
{
|
1999-12-19 17:14:47 +00:00
|
|
|
mCurrentItemIndex = aNewIndex;
|
|
|
|
return (aNewIndex < GetTargetArraySize()) ? NS_OK : NS_ERROR_FAILURE;
|
1999-12-08 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverListEnumerator::First()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTargetArray, "must enumerate over an non-null list");
|
|
|
|
|
|
|
|
return MoveToIndex(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverListEnumerator::Last()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTargetArray, "must enumerate over an non-null list");
|
|
|
|
|
|
|
|
return MoveToIndex(GetTargetArraySize()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverListEnumerator::Prev()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTargetArray, "must enumerate over an non-null list");
|
|
|
|
|
|
|
|
return MoveToIndex(mCurrentItemIndex-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverListEnumerator::Next()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTargetArray, "must enumerate over an non-null list");
|
|
|
|
|
|
|
|
return MoveToIndex(mCurrentItemIndex+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverListEnumerator::CurrentItem( nsISupports** aItemPtr )
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTargetArray, "must enumerate over an non-null list");
|
|
|
|
NS_ASSERTION(aItemPtr, "must supply a place to put the result");
|
|
|
|
|
1999-12-19 17:14:47 +00:00
|
|
|
if ( mCurrentItemIndex >= GetTargetArraySize() )
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> elementPtr = getter_AddRefs(mTargetArray->ElementAt(mCurrentItemIndex));
|
1999-12-08 05:24:14 +00:00
|
|
|
|
1999-12-19 17:14:47 +00:00
|
|
|
if ( !aItemPtr || !elementPtr )
|
1999-12-08 05:24:14 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-12-19 17:14:47 +00:00
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(elementPtr);
|
|
|
|
|
|
|
|
nsresult status;
|
|
|
|
if ( weakRef )
|
|
|
|
status = weakRef->QueryReferent(NS_GET_IID(nsIObserver), NS_REINTERPRET_CAST(void**, aItemPtr));
|
|
|
|
else
|
|
|
|
status = elementPtr->QueryInterface(NS_GET_IID(nsIObserver), NS_REINTERPRET_CAST(void**, aItemPtr));
|
|
|
|
|
|
|
|
return status;
|
1999-12-08 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverListEnumerator::IsDone()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTargetArray, "must enumerate over an non-null list");
|
|
|
|
|
1999-12-19 17:14:47 +00:00
|
|
|
return (mCurrentItemIndex >= GetTargetArraySize()) ? NS_OK : NS_ENUMERATOR_FALSE;
|
1999-12-08 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-04-06 20:54:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverList Implementation
|
|
|
|
|
|
|
|
|
1999-08-23 10:14:16 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsObserverList, nsIObserverList)
|
1999-04-06 20:54:09 +00:00
|
|
|
|
1999-05-26 01:38:36 +00:00
|
|
|
NS_COM nsresult NS_NewObserverList(nsIObserverList** anObserverList)
|
1999-04-06 20:54:09 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (anObserverList == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
nsObserverList* it = new nsObserverList();
|
|
|
|
|
|
|
|
if (it == 0) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
1999-08-23 10:14:16 +00:00
|
|
|
return it->QueryInterface(NS_GET_IID(nsIObserverList), (void **) anObserverList);
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverList::nsObserverList()
|
1999-09-12 12:02:03 +00:00
|
|
|
: mLock(nsnull),
|
|
|
|
mObserverList(NULL)
|
1999-04-06 20:54:09 +00:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverList::~nsObserverList(void)
|
|
|
|
{
|
|
|
|
PR_DestroyLock(mLock);
|
1999-07-30 07:58:55 +00:00
|
|
|
NS_IF_RELEASE(mObserverList);
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult nsObserverList::AddObserver(nsIObserver** anObserver)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-04-20 14:50:47 +00:00
|
|
|
PRBool inserted;
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
NS_AUTOLOCK(mLock);
|
|
|
|
|
|
|
|
if (anObserver == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!mObserverList) {
|
|
|
|
rv = NS_NewISupportsArray(&mObserverList);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
1999-12-19 17:14:47 +00:00
|
|
|
#ifdef NS_WEAK_OBSERVERS
|
|
|
|
nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(*anObserver);
|
|
|
|
nsCOMPtr<nsISupports> observerRef;
|
|
|
|
if ( weakRefFactory )
|
|
|
|
observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
|
|
|
|
else
|
|
|
|
observerRef = *anObserver;
|
|
|
|
|
|
|
|
if(observerRef) {
|
|
|
|
inserted = mObserverList->AppendElement(observerRef);
|
1999-12-08 05:24:14 +00:00
|
|
|
#else
|
1999-04-06 20:54:09 +00:00
|
|
|
if(*anObserver) {
|
1999-12-08 05:24:14 +00:00
|
|
|
inserted = mObserverList->AppendElement(*anObserver);
|
|
|
|
#endif
|
1999-04-20 14:50:47 +00:00
|
|
|
return inserted ? NS_OK : NS_ERROR_FAILURE;
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
1999-04-20 14:50:47 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsObserverList::RemoveObserver(nsIObserver** anObserver)
|
|
|
|
{
|
1999-04-20 14:50:47 +00:00
|
|
|
PRBool removed;
|
|
|
|
|
1999-04-06 20:54:09 +00:00
|
|
|
NS_AUTOLOCK(mLock);
|
|
|
|
|
|
|
|
if (anObserver == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
1999-04-21 19:36:41 +00:00
|
|
|
if(!mObserverList) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-12-19 17:14:47 +00:00
|
|
|
#ifdef NS_WEAK_OBSERVERS
|
|
|
|
nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(*anObserver);
|
|
|
|
nsCOMPtr<nsISupports> observerRef;
|
|
|
|
if ( weakRefFactory )
|
|
|
|
observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
|
|
|
|
else
|
|
|
|
observerRef = *anObserver;
|
|
|
|
|
|
|
|
if(observerRef) {
|
|
|
|
removed = mObserverList->RemoveElement(observerRef);
|
1999-12-08 05:24:14 +00:00
|
|
|
#else
|
1999-04-06 20:54:09 +00:00
|
|
|
if(*anObserver) {
|
1999-12-08 05:24:14 +00:00
|
|
|
removed = mObserverList->RemoveElement(*anObserver);
|
|
|
|
#endif
|
1999-04-20 14:50:47 +00:00
|
|
|
return removed ? NS_OK : NS_ERROR_FAILURE;
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
1999-04-20 14:50:47 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsObserverList::EnumerateObserverList(nsIEnumerator** anEnumerator)
|
|
|
|
{
|
|
|
|
NS_AUTOLOCK(mLock);
|
|
|
|
|
|
|
|
if (anEnumerator == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1999-04-21 19:36:41 +00:00
|
|
|
|
|
|
|
if(!mObserverList) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-12-08 05:24:14 +00:00
|
|
|
|
1999-12-19 17:14:47 +00:00
|
|
|
#ifdef NS_WEAK_OBSERVERS
|
1999-12-08 05:24:14 +00:00
|
|
|
nsCOMPtr<nsIBidirectionalEnumerator> enumerator = new nsObserverListEnumerator(mObserverList);
|
|
|
|
if ( !enumerator )
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return CallQueryInterface(enumerator, anEnumerator);
|
|
|
|
#else
|
|
|
|
return mObserverList->Enumerate(anEnumerator);
|
|
|
|
#endif
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|