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
|
|
|
|
2005-11-30 21:51:27 +03:00
|
|
|
#include "nsObserverList.h"
|
1999-12-19 20:14:47 +03:00
|
|
|
|
2005-12-14 00:12:41 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2006-03-21 17:43:56 +03:00
|
|
|
#include "nsCOMArray.h"
|
2005-12-14 00:12:41 +03:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2006-03-04 17:04:05 +03:00
|
|
|
|
2006-03-09 06:14:32 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsObserverList::AddObserver(nsIObserver* anObserver, bool ownsWeak)
|
2006-03-09 06:14:32 +03:00
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
NS_ASSERTION(anObserver, "Null input");
|
|
|
|
|
|
|
|
if (!ownsWeak) {
|
|
|
|
ObserverRef* o = mObservers.AppendElement(anObserver);
|
|
|
|
if (!o)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
2006-03-21 17:43:56 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(anObserver);
|
|
|
|
if (!weak)
|
|
|
|
return NS_NOINTERFACE;
|
1999-04-21 23:36:41 +04:00
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
ObserverRef *o = mObservers.AppendElement(weak);
|
|
|
|
if (!o)
|
2005-11-30 21:51:27 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
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
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
NS_ASSERTION(anObserver, "Null input");
|
2006-03-09 06:14:32 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
if (mObservers.RemoveElement(static_cast<nsISupports*>(anObserver)))
|
2005-11-30 21:51:27 +03:00
|
|
|
return NS_OK;
|
2005-11-24 00:37:16 +03:00
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
nsCOMPtr<nsIWeakReference> observerRef = do_GetWeakReference(anObserver);
|
2005-11-30 21:51:27 +03:00
|
|
|
if (!observerRef)
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-11-24 00:37:16 +03:00
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
if (!mObservers.RemoveElement(observerRef))
|
2005-11-30 21:51:27 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2005-11-24 00:37:16 +03:00
|
|
|
|
2001-10-24 22:02:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-11-30 21:51:27 +03:00
|
|
|
nsresult
|
|
|
|
nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
|
2001-10-24 22:02:46 +04:00
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
nsRefPtr<nsObserverEnumerator> e(new nsObserverEnumerator(this));
|
2005-12-14 00:12:41 +03:00
|
|
|
if (!e)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*anEnumerator = e);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
void
|
|
|
|
nsObserverList::FillObserverArray(nsCOMArray<nsIObserver> &aArray)
|
2005-12-14 00:12:41 +03:00
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
aArray.SetCapacity(mObservers.Length());
|
|
|
|
|
2009-11-17 04:07:41 +03:00
|
|
|
nsTArray<ObserverRef> observers(mObservers);
|
|
|
|
|
|
|
|
for (PRInt32 i = observers.Length() - 1; i >= 0; --i) {
|
|
|
|
if (observers[i].isWeakRef) {
|
|
|
|
nsCOMPtr<nsIObserver> o(do_QueryReferent(observers[i].asWeak()));
|
2006-03-21 17:43:56 +03:00
|
|
|
if (o) {
|
|
|
|
aArray.AppendObject(o);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// the object has gone away, remove the weakref
|
2009-11-17 04:07:41 +03:00
|
|
|
mObservers.RemoveElement(observers[i].asWeak());
|
2006-03-21 17:43:56 +03:00
|
|
|
}
|
2005-12-14 00:12:41 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-11-17 04:07:41 +03:00
|
|
|
aArray.AppendObject(observers[i].asObserver());
|
2005-12-14 00:12:41 +03:00
|
|
|
}
|
|
|
|
}
|
2006-03-21 17:43:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsObserverList::NotifyObservers(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *someData)
|
|
|
|
{
|
|
|
|
nsCOMArray<nsIObserver> observers;
|
|
|
|
FillObserverArray(observers);
|
2005-12-14 00:12:41 +03:00
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
for (PRInt32 i = 0; i < observers.Count(); ++i) {
|
|
|
|
observers[i]->Observe(aSubject, aTopic, someData);
|
|
|
|
}
|
2005-12-14 00:12:41 +03:00
|
|
|
}
|
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(nsObserverEnumerator, nsISimpleEnumerator)
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
nsObserverEnumerator::nsObserverEnumerator(nsObserverList* aObserverList)
|
|
|
|
: mIndex(0)
|
|
|
|
{
|
|
|
|
aObserverList->FillObserverArray(mObservers);
|
|
|
|
}
|
|
|
|
|
2005-12-14 00:12:41 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsObserverEnumerator::HasMoreElements(bool *aResult)
|
2005-12-14 00:12:41 +03:00
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
*aResult = (mIndex < mObservers.Count());
|
2005-12-14 00:12:41 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverEnumerator::GetNext(nsISupports* *aResult)
|
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
if (mIndex == mObservers.Count()) {
|
2005-12-14 00:12:41 +03:00
|
|
|
NS_ERROR("Enumerating after HasMoreElements returned false.");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*aResult = mObservers[mIndex]);
|
2006-03-21 17:43:56 +03:00
|
|
|
++mIndex;
|
2005-12-14 00:12:41 +03:00
|
|
|
return NS_OK;
|
2001-03-03 04:24:36 +03:00
|
|
|
}
|