2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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
|
|
|
|
|
|
|
#ifndef nsObserverList_h___
|
|
|
|
#define nsObserverList_h___
|
|
|
|
|
2005-11-30 21:51:27 +03:00
|
|
|
#include "nsISupports.h"
|
2006-03-21 17:43:56 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2005-11-30 21:51:27 +03:00
|
|
|
#include "nsCOMArray.h"
|
2006-03-21 17:43:56 +03:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIWeakReference.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2001-10-24 22:02:46 +04:00
|
|
|
|
2013-08-26 22:56:23 +04:00
|
|
|
namespace mozilla {
|
|
|
|
class ObserverServiceReporter;
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
struct ObserverRef
|
|
|
|
{
|
|
|
|
ObserverRef(const ObserverRef& o) :
|
|
|
|
isWeakRef(o.isWeakRef), ref(o.ref) { }
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) { }
|
|
|
|
ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) { }
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isWeakRef;
|
2006-03-21 17:43:56 +03:00
|
|
|
nsCOMPtr<nsISupports> ref;
|
|
|
|
|
|
|
|
nsIObserver* asObserver() {
|
|
|
|
NS_ASSERTION(!isWeakRef, "Isn't a strong ref.");
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<nsIObserver*>((nsISupports*) ref);
|
2006-03-21 17:43:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIWeakReference* asWeak() {
|
|
|
|
NS_ASSERTION(isWeakRef, "Isn't a weak ref.");
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<nsIWeakReference*>((nsISupports*) ref);
|
2006-03-21 17:43:56 +03:00
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool operator==(nsISupports* b) const { return ref == b; }
|
2006-03-21 17:43:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsObserverList : public nsCharPtrHashKey
|
2001-03-03 04:24:36 +03:00
|
|
|
{
|
2013-11-07 09:35:30 +04:00
|
|
|
friend class nsObserverService;
|
2013-08-26 22:56:23 +04:00
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
public:
|
2006-03-21 17:43:56 +03:00
|
|
|
nsObserverList(const char *key) : nsCharPtrHashKey(key)
|
|
|
|
{ MOZ_COUNT_CTOR(nsObserverList); }
|
|
|
|
|
|
|
|
~nsObserverList() { MOZ_COUNT_DTOR(nsObserverList); }
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult AddObserver(nsIObserver* anObserver, bool ownsWeak);
|
2001-10-20 00:52:59 +04:00
|
|
|
nsresult RemoveObserver(nsIObserver* anObserver);
|
2006-03-21 17:43:56 +03:00
|
|
|
|
|
|
|
void NotifyObservers(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *someData);
|
2001-10-20 00:52:59 +04:00
|
|
|
nsresult GetObserverList(nsISimpleEnumerator** anEnumerator);
|
2006-03-21 17:43:56 +03:00
|
|
|
|
|
|
|
// 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 23:01:11 +04:00
|
|
|
// Unmark any strongly held observers implemented in JS so the cycle
|
|
|
|
// collector will not traverse them.
|
|
|
|
void UnmarkGrayStrongObservers();
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
private:
|
|
|
|
nsTArray<ObserverRef> mObservers;
|
1999-04-07 00:54:09 +04:00
|
|
|
};
|
|
|
|
|
2012-06-06 03:51:58 +04:00
|
|
|
class nsObserverEnumerator MOZ_FINAL : public nsISimpleEnumerator
|
2006-03-21 17:43:56 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
|
|
|
|
nsObserverEnumerator(nsObserverList* aObserverList);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsObserverEnumerator() { }
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mIndex; // Counts up from 0
|
2006-03-21 17:43:56 +03:00
|
|
|
nsCOMArray<nsIObserver> mObservers;
|
|
|
|
};
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
#endif /* nsObserverList_h___ */
|