2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsObserverList_h___
|
|
|
|
#define nsObserverList_h___
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIWeakReference.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2012-06-05 16:51:58 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-26 11:56:23 -07:00
|
|
|
namespace mozilla {
|
|
|
|
class ObserverServiceReporter;
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
struct ObserverRef
|
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
ObserverRef(const ObserverRef& aO) : isWeakRef(aO.isWeakRef), ref(aO.ref) {}
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) {}
|
|
|
|
explicit ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isWeakRef;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> ref;
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
nsIObserver* asObserver()
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(!isWeakRef, "Isn't a strong ref.");
|
2014-07-09 08:15:21 -07:00
|
|
|
return static_cast<nsIObserver*>((nsISupports*)ref);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
nsIWeakReference* asWeak()
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(isWeakRef, "Isn't a weak ref.");
|
2014-07-09 08:15:21 -07:00
|
|
|
return static_cast<nsIWeakReference*>((nsISupports*)ref);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
bool operator==(nsISupports* aRhs) const { return ref == aRhs; }
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsObserverList : public nsCharPtrHashKey
|
|
|
|
{
|
2013-11-06 21:35:30 -08:00
|
|
|
friend class nsObserverService;
|
2013-08-26 11:56:23 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit nsObserverList(const char* aKey) : nsCharPtrHashKey(aKey)
|
2014-07-09 08:15:21 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsObserverList);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
~nsObserverList()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsObserverList);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
nsresult AddObserver(nsIObserver* aObserver, bool aOwnsWeak);
|
|
|
|
nsresult RemoveObserver(nsIObserver* aObserver);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
void NotifyObservers(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aSomeData);
|
|
|
|
nsresult GetObserverList(nsISimpleEnumerator** aEnumerator);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Fill an array with the observers of this category.
|
|
|
|
// The array is filled in last-added-first order.
|
2014-07-09 08:15:21 -07:00
|
|
|
void FillObserverArray(nsCOMArray<nsIObserver>& aArray);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-30 12:01:11 -07:00
|
|
|
// Unmark any strongly held observers implemented in JS so the cycle
|
|
|
|
// collector will not traverse them.
|
|
|
|
void UnmarkGrayStrongObservers();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
nsTArray<ObserverRef> mObservers;
|
|
|
|
};
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class nsObserverEnumerator final : public nsISimpleEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-07-09 08:15:21 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit nsObserverEnumerator(nsObserverList* aObserverList);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-07-09 08:15:21 -07:00
|
|
|
~nsObserverEnumerator() {}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
int32_t mIndex; // Counts up from 0
|
|
|
|
nsCOMArray<nsIObserver> mObservers;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsObserverList_h___ */
|