1999-04-07 00:54:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
1999-11-06 06:43:54 +03: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-07 00:54:09 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03: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-07 00:54:09 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-04-07 00:54:09 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-04-07 00:54:09 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define NS_IMPL_IDS
|
|
|
|
#include "pratom.h"
|
|
|
|
#include "nsIObserverList.h"
|
|
|
|
#include "nsObserverList.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define NS_AUTOLOCK(__monitor) nsAutoLock __lock(__monitor)
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
static NS_DEFINE_CID(kObserverListCID, NS_OBSERVERLIST_CID);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverList Implementation
|
|
|
|
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsObserverList, nsIObserverList)
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM nsresult NS_NewObserverList(nsIObserverList** anObserverList)
|
1999-04-07 00:54:09 +04: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 14:14:16 +04:00
|
|
|
return it->QueryInterface(NS_GET_IID(nsIObserverList), (void **) anObserverList);
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverList::nsObserverList()
|
1999-09-12 16:02:03 +04:00
|
|
|
: mLock(nsnull),
|
|
|
|
mObserverList(NULL)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverList::~nsObserverList(void)
|
|
|
|
{
|
|
|
|
PR_DestroyLock(mLock);
|
1999-07-30 11:58:55 +04:00
|
|
|
NS_IF_RELEASE(mObserverList);
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult nsObserverList::AddObserver(nsIObserver** anObserver)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-04-20 18:50:47 +04:00
|
|
|
PRBool inserted;
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
NS_AUTOLOCK(mLock);
|
|
|
|
|
|
|
|
if (anObserver == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!mObserverList) {
|
|
|
|
rv = NS_NewISupportsArray(&mObserverList);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*anObserver) {
|
1999-04-20 18:50:47 +04:00
|
|
|
inserted = mObserverList->AppendElement(*anObserver);
|
|
|
|
return inserted ? NS_OK : NS_ERROR_FAILURE;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
1999-04-20 18:50:47 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsObserverList::RemoveObserver(nsIObserver** anObserver)
|
|
|
|
{
|
1999-04-20 18:50:47 +04:00
|
|
|
PRBool removed;
|
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
NS_AUTOLOCK(mLock);
|
|
|
|
|
|
|
|
if (anObserver == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
1999-04-21 23:36:41 +04:00
|
|
|
if(!mObserverList) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
if(*anObserver) {
|
1999-04-20 18:50:47 +04:00
|
|
|
removed = mObserverList->RemoveElement(*anObserver);
|
|
|
|
return removed ? NS_OK : NS_ERROR_FAILURE;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
1999-04-20 18:50:47 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsObserverList::EnumerateObserverList(nsIEnumerator** anEnumerator)
|
|
|
|
{
|
|
|
|
NS_AUTOLOCK(mLock);
|
|
|
|
|
|
|
|
if (anEnumerator == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1999-04-21 23:36:41 +04:00
|
|
|
|
|
|
|
if(!mObserverList) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
return mObserverList->Enumerate(anEnumerator);
|
|
|
|
}
|
|
|
|
|