1999-04-07 00:54:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-09-29 00:14:13 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-04-07 00:54:09 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* 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/
|
2001-09-29 00:14:13 +04: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.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of 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"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-12-21 03:12:25 +03:00
|
|
|
#define NS_WEAK_OBSERVERS
|
1999-12-19 20:14:47 +03:00
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "pratom.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAutoLock.h"
|
1999-12-08 08:24:14 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIWeakReference.h"
|
2001-10-20 00:52:59 +04:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2001-03-03 04:24:36 +03:00
|
|
|
#include "nsObserverList.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
nsObserverList::nsObserverList()
|
|
|
|
{
|
2001-11-28 02:52:05 +03:00
|
|
|
MOZ_COUNT_CTOR(nsObserverList);
|
1999-04-07 00:54:09 +04:00
|
|
|
mLock = PR_NewLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverList::~nsObserverList(void)
|
|
|
|
{
|
2001-11-28 02:52:05 +03:00
|
|
|
MOZ_COUNT_DTOR(nsObserverList);
|
1999-04-07 00:54:09 +04:00
|
|
|
PR_DestroyLock(mLock);
|
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
nsresult
|
|
|
|
nsObserverList::AddObserver(nsIObserver* anObserver, PRBool ownsWeak)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
2002-09-11 06:35:26 +04:00
|
|
|
nsresult rv;
|
|
|
|
PRBool inserted;
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
NS_ENSURE_ARG(anObserver);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2002-09-11 06:35:26 +04:00
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mObserverList) {
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = NS_NewISupportsArray(getter_AddRefs(mObserverList));
|
2002-09-11 06:35:26 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
1999-12-19 20:14:47 +03:00
|
|
|
#ifdef NS_WEAK_OBSERVERS
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCOMPtr<nsISupports> observerRef;
|
|
|
|
if (ownsWeak) {
|
|
|
|
nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
|
|
|
|
NS_ASSERTION(weakRefFactory, "AddObserver: trying weak object that doesnt support nsIWeakReference");
|
|
|
|
if ( weakRefFactory )
|
|
|
|
observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
|
|
|
|
} else {
|
2002-03-19 02:40:36 +03:00
|
|
|
#if DEBUG_dougt_xxx
|
2001-10-20 00:52:59 +04:00
|
|
|
// if you are hitting this assertion, contact dougt@netscape.com. There may be a ownership problem caused by his checkin to freeze nsIObserver
|
|
|
|
nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
|
|
|
|
NS_ASSERTION(!weakRefFactory, "Your object supports weak references, but is being added with a strong reference");
|
1999-12-08 08:24:14 +03:00
|
|
|
#endif
|
2001-10-20 00:52:59 +04:00
|
|
|
observerRef = anObserver;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
2001-10-20 00:52:59 +04:00
|
|
|
if (!observerRef)
|
1999-04-21 23:36:41 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
inserted = mObserverList->AppendElement(observerRef);
|
1999-12-08 08:24:14 +03:00
|
|
|
#else
|
2002-09-11 06:35:26 +04:00
|
|
|
if (*anObserver)
|
|
|
|
inserted = mObserverList->AppendElement(*anObserver);
|
1999-12-08 08:24:14 +03:00
|
|
|
#endif
|
2001-10-20 00:52:59 +04:00
|
|
|
return inserted ? NS_OK : NS_ERROR_FAILURE;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
nsresult
|
|
|
|
nsObserverList::RemoveObserver(nsIObserver* anObserver)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
2002-02-03 10:05:10 +03:00
|
|
|
PRBool removed = PR_FALSE;
|
2001-10-20 00:52:59 +04:00
|
|
|
|
|
|
|
NS_ENSURE_ARG(anObserver);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2002-09-11 06:35:26 +04:00
|
|
|
nsAutoLock lock(mLock);
|
1999-04-21 23:36:41 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
if (!mObserverList)
|
2002-09-11 06:35:26 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2001-10-20 00:52:59 +04:00
|
|
|
|
1999-12-19 20:14:47 +03:00
|
|
|
#ifdef NS_WEAK_OBSERVERS
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
|
|
|
|
nsCOMPtr<nsISupports> observerRef;
|
2002-09-11 06:35:26 +04:00
|
|
|
if (weakRefFactory) {
|
2001-10-20 00:52:59 +04:00
|
|
|
observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
|
2002-09-11 06:35:26 +04:00
|
|
|
if (observerRef)
|
|
|
|
removed = mObserverList->RemoveElement(observerRef);
|
|
|
|
if (!removed)
|
|
|
|
observerRef = anObserver;
|
|
|
|
} else
|
2001-10-20 00:52:59 +04:00
|
|
|
observerRef = anObserver;
|
2002-09-11 06:35:26 +04:00
|
|
|
|
|
|
|
if (!removed && observerRef)
|
|
|
|
removed = mObserverList->RemoveElement(observerRef);
|
1999-12-08 08:24:14 +03:00
|
|
|
#else
|
2002-09-11 06:35:26 +04:00
|
|
|
if (*anObserver)
|
2001-10-20 00:52:59 +04:00
|
|
|
removed = mObserverList->RemoveElement(*anObserver);
|
1999-12-08 08:24:14 +03:00
|
|
|
#endif
|
2001-10-20 00:52:59 +04:00
|
|
|
return removed ? NS_OK : NS_ERROR_FAILURE;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
nsresult
|
|
|
|
nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
|
2001-03-03 04:24:36 +03:00
|
|
|
{
|
2001-10-20 00:52:59 +04:00
|
|
|
nsAutoLock lock(mLock);
|
2001-03-03 04:24:36 +03:00
|
|
|
|
2001-10-24 22:02:46 +04:00
|
|
|
ObserverListEnumerator * enumerator= new ObserverListEnumerator(mObserverList);
|
|
|
|
*anEnumerator = enumerator;
|
2004-07-30 06:43:13 +04:00
|
|
|
if (!enumerator)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(enumerator);
|
2001-10-24 22:02:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObserverListEnumerator::ObserverListEnumerator(nsISupportsArray* aValueArray)
|
2001-11-28 05:48:10 +03:00
|
|
|
: mValueArray(aValueArray), mIndex(0)
|
2001-10-24 22:02:46 +04:00
|
|
|
{
|
2001-11-28 05:48:10 +03:00
|
|
|
if (mValueArray) {
|
2004-07-30 06:43:13 +04:00
|
|
|
NS_ADDREF(mValueArray);
|
2001-11-28 05:48:10 +03:00
|
|
|
PRUint32 total;
|
|
|
|
mValueArray->Count(&total);
|
|
|
|
mIndex = PRInt32(total);
|
|
|
|
}
|
2001-10-24 22:02:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ObserverListEnumerator::~ObserverListEnumerator(void)
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mValueArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(ObserverListEnumerator, nsISimpleEnumerator)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ObserverListEnumerator::HasMoreElements(PRBool* aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (!mValueArray) {
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-11-28 05:48:10 +03:00
|
|
|
*aResult = (mIndex > 0);
|
2001-10-24 22:02:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ObserverListEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (!mValueArray) {
|
|
|
|
*aResult = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-11-28 05:48:10 +03:00
|
|
|
if (mIndex <= 0 )
|
2001-10-24 22:02:46 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2001-11-28 05:48:10 +03:00
|
|
|
mValueArray->GetElementAt(--mIndex, aResult);
|
2001-10-24 22:02:46 +04:00
|
|
|
if (*aResult) {
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRefFactory = do_QueryInterface(*aResult);
|
|
|
|
if ( weakRefFactory ) {
|
|
|
|
nsCOMPtr<nsISupports> weakref = do_QueryReferent(weakRefFactory);
|
|
|
|
NS_RELEASE(*aResult);
|
2001-10-27 07:06:09 +04:00
|
|
|
NS_IF_ADDREF(*aResult = weakref);
|
2001-10-24 22:02:46 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-03-03 04:24:36 +03:00
|
|
|
}
|