2015-04-09 17:25:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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-06 20:54:09 +00:00
|
|
|
|
2005-11-30 18:51:27 +00:00
|
|
|
#include "nsObserverList.h"
|
1999-12-19 17:14:47 +00:00
|
|
|
|
2005-12-13 21:12:41 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2006-03-21 14:43:56 +00:00
|
|
|
#include "nsCOMArray.h"
|
2005-12-13 21:12:41 +00:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2012-04-30 19:01:11 +00:00
|
|
|
#include "xpcpublic.h"
|
2006-03-04 14:04:05 +00:00
|
|
|
|
2006-03-09 03:14:32 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsObserverList::AddObserver(nsIObserver* anObserver, bool ownsWeak)
|
2006-03-09 03:14:32 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
NS_ASSERTION(anObserver, "Null input");
|
2006-03-21 14:43:56 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (!ownsWeak) {
|
|
|
|
ObserverRef* o = mObservers.AppendElement(anObserver);
|
|
|
|
if (!o) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
2005-11-30 18:51:27 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(anObserver);
|
|
|
|
if (!weak) {
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObserverRef* o = mObservers.AppendElement(weak);
|
|
|
|
if (!o) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
nsresult
|
|
|
|
nsObserverList::RemoveObserver(nsIObserver* anObserver)
|
1999-04-06 20:54:09 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
NS_ASSERTION(anObserver, "Null input");
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (mObservers.RemoveElement(static_cast<nsISupports*>(anObserver))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-11-23 21:37:16 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsCOMPtr<nsIWeakReference> observerRef = do_GetWeakReference(anObserver);
|
|
|
|
if (!observerRef) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-11-23 21:37:16 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (!mObservers.RemoveElement(observerRef)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-11-23 21:37:16 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
return NS_OK;
|
2001-10-24 18:02:46 +00:00
|
|
|
}
|
|
|
|
|
2005-11-30 18:51:27 +00:00
|
|
|
nsresult
|
|
|
|
nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
|
2001-10-24 18:02:46 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsObserverEnumerator> e(new nsObserverEnumerator(this));
|
2014-07-09 15:15:21 +00:00
|
|
|
e.forget(anEnumerator);
|
|
|
|
return NS_OK;
|
2005-12-13 21:12:41 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
void
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverList::FillObserverArray(nsCOMArray<nsIObserver>& aArray)
|
2005-12-13 21:12:41 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
aArray.SetCapacity(mObservers.Length());
|
|
|
|
|
|
|
|
nsTArray<ObserverRef> observers(mObservers);
|
|
|
|
|
|
|
|
for (int32_t i = observers.Length() - 1; i >= 0; --i) {
|
|
|
|
if (observers[i].isWeakRef) {
|
|
|
|
nsCOMPtr<nsIObserver> o(do_QueryReferent(observers[i].asWeak()));
|
|
|
|
if (o) {
|
|
|
|
aArray.AppendObject(o);
|
|
|
|
} else {
|
|
|
|
// the object has gone away, remove the weakref
|
|
|
|
mObservers.RemoveElement(observers[i].asWeak());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aArray.AppendObject(observers[i].asObserver());
|
2005-12-13 21:12:41 +00:00
|
|
|
}
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2006-03-21 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 00:16:30 +00:00
|
|
|
void
|
|
|
|
nsObserverList::AppendStrongObservers(nsCOMArray<nsIObserver>& aArray)
|
|
|
|
{
|
|
|
|
aArray.SetCapacity(aArray.Length() + mObservers.Length());
|
|
|
|
|
|
|
|
for (int32_t i = mObservers.Length() - 1; i >= 0; --i) {
|
|
|
|
if (!mObservers[i].isWeakRef) {
|
|
|
|
aArray.AppendObject(mObservers[i].asObserver());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
void
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverList::NotifyObservers(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* someData)
|
2006-03-21 14:43:56 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
nsCOMArray<nsIObserver> observers;
|
|
|
|
FillObserverArray(observers);
|
2005-12-13 21:12:41 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
for (int32_t i = 0; i < observers.Count(); ++i) {
|
|
|
|
observers[i]->Observe(aSubject, aTopic, someData);
|
|
|
|
}
|
2005-12-13 21:12:41 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsObserverEnumerator, nsISimpleEnumerator)
|
2008-02-21 20:39:20 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
nsObserverEnumerator::nsObserverEnumerator(nsObserverList* aObserverList)
|
2014-07-09 15:15:21 +00:00
|
|
|
: mIndex(0)
|
2006-03-21 14:43:56 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
aObserverList->FillObserverArray(mObservers);
|
2006-03-21 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
2005-12-13 21:12:41 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverEnumerator::HasMoreElements(bool* aResult)
|
2005-12-13 21:12:41 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
*aResult = (mIndex < mObservers.Count());
|
|
|
|
return NS_OK;
|
2005-12-13 21:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverEnumerator::GetNext(nsISupports** aResult)
|
2005-12-13 21:12:41 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
if (mIndex == mObservers.Count()) {
|
|
|
|
NS_ERROR("Enumerating after HasMoreElements returned false.");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*aResult = mObservers[mIndex]);
|
|
|
|
++mIndex;
|
|
|
|
return NS_OK;
|
2001-03-03 01:24:36 +00:00
|
|
|
}
|