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
|
|
|
|
{
|
|
|
|
ObserverRef(const ObserverRef& o) :
|
|
|
|
isWeakRef(o.isWeakRef), ref(o.ref) { }
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) { }
|
|
|
|
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;
|
|
|
|
|
|
|
|
nsIObserver* asObserver() {
|
|
|
|
NS_ASSERTION(!isWeakRef, "Isn't a strong ref.");
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<nsIObserver*>((nsISupports*) ref);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIWeakReference* asWeak() {
|
|
|
|
NS_ASSERTION(isWeakRef, "Isn't a weak ref.");
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<nsIWeakReference*>((nsISupports*) ref);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool operator==(nsISupports* b) const { return ref == b; }
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsObserverList : public nsCharPtrHashKey
|
|
|
|
{
|
2013-11-27 17:05:00 -08:00
|
|
|
friend class mozilla::ObserverServiceReporter;
|
2013-08-26 11:56:23 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
|
|
|
nsObserverList(const char *key) : nsCharPtrHashKey(key)
|
|
|
|
{ MOZ_COUNT_CTOR(nsObserverList); }
|
|
|
|
|
|
|
|
~nsObserverList() { MOZ_COUNT_DTOR(nsObserverList); }
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult AddObserver(nsIObserver* anObserver, bool ownsWeak);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult RemoveObserver(nsIObserver* anObserver);
|
|
|
|
|
|
|
|
void NotifyObservers(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *someData);
|
|
|
|
nsresult GetObserverList(nsISimpleEnumerator** anEnumerator);
|
|
|
|
|
|
|
|
// Fill an array with the observers of this category.
|
|
|
|
// The array is filled in last-added-first order.
|
|
|
|
void FillObserverArray(nsCOMArray<nsIObserver> &aArray);
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class nsObserverEnumerator MOZ_FINAL : public nsISimpleEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
|
|
|
|
nsObserverEnumerator(nsObserverList* aObserverList);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsObserverEnumerator() { }
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mIndex; // Counts up from 0
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMArray<nsIObserver> mObservers;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsObserverList_h___ */
|