2015-04-09 20:25:05 +03: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 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
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
struct ObserverRef
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
ObserverRef(const ObserverRef& aO) : isWeakRef(aO.isWeakRef), ref(aO.ref) {}
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) {}
|
|
|
|
explicit 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;
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIObserver* asObserver()
|
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
NS_ASSERTION(!isWeakRef, "Isn't a strong ref.");
|
2014-07-09 19:15:21 +04:00
|
|
|
return static_cast<nsIObserver*>((nsISupports*)ref);
|
2006-03-21 17:43:56 +03:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIWeakReference* asWeak()
|
|
|
|
{
|
2006-03-21 17:43:56 +03:00
|
|
|
NS_ASSERTION(isWeakRef, "Isn't a weak ref.");
|
2014-07-09 19:15:21 +04:00
|
|
|
return static_cast<nsIWeakReference*>((nsISupports*)ref);
|
2006-03-21 17:43:56 +03:00
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
bool operator==(nsISupports* aRhs) const { return ref == aRhs; }
|
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:
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsObserverList(const char* aKey) : nsCharPtrHashKey(aKey)
|
2014-07-09 19:15:21 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsObserverList);
|
|
|
|
}
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
~nsObserverList()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsObserverList);
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2016-04-28 10:11:17 +03:00
|
|
|
MOZ_MUST_USE nsresult AddObserver(nsIObserver* aObserver, bool aOwnsWeak);
|
|
|
|
MOZ_MUST_USE nsresult RemoveObserver(nsIObserver* aObserver);
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
void NotifyObservers(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aSomeData);
|
2016-04-28 10:11:17 +03:00
|
|
|
void GetObserverList(nsISimpleEnumerator** aEnumerator);
|
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.
|
2014-07-09 19:15:21 +04:00
|
|
|
void FillObserverArray(nsCOMArray<nsIObserver>& aArray);
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2015-04-09 03:16:30 +03:00
|
|
|
// Like FillObserverArray(), but only for strongly held observers.
|
|
|
|
void AppendStrongObservers(nsCOMArray<nsIObserver>& aArray);
|
2012-04-30 23:01:11 +04:00
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
private:
|
|
|
|
nsTArray<ObserverRef> mObservers;
|
1999-04-07 00:54:09 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsObserverEnumerator final : public nsISimpleEnumerator
|
2006-03-21 17:43:56 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsObserverEnumerator(nsObserverList* aObserverList);
|
2006-03-21 17:43:56 +03:00
|
|
|
|
|
|
|
private:
|
2014-07-09 19:15:21 +04:00
|
|
|
~nsObserverEnumerator() {}
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
int32_t mIndex; // Counts up from 0
|
|
|
|
nsCOMArray<nsIObserver> mObservers;
|
2006-03-21 17:43:56 +03:00
|
|
|
};
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
#endif /* nsObserverList_h___ */
|