1999-04-07 00:54:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
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"
|
2012-04-30 23:01:11 +04:00
|
|
|
#include "xpcpublic.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);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = observers.Length() - 1; i >= 0; --i) {
|
2009-11-17 04:07:41 +03:00
|
|
|
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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < observers.Count(); ++i) {
|
2006-03-21 17:43:56 +03:00
|
|
|
observers[i]->Observe(aSubject, aTopic, someData);
|
|
|
|
}
|
2005-12-14 00:12:41 +03:00
|
|
|
}
|
|
|
|
|
2012-04-30 23:01:11 +04:00
|
|
|
void
|
|
|
|
nsObserverList::UnmarkGrayStrongObservers()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mObservers.Length(); ++i) {
|
2012-04-30 23:01:11 +04:00
|
|
|
if (!mObservers[i].isWeakRef) {
|
|
|
|
xpc_TryUnmarkWrappedGrayObject(mObservers[i].asObserver());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|