1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsObserverList_h___
7 #define nsObserverList_h___
9 #include "nsISupports.h"
12 #include "nsCOMArray.h"
13 #include "nsIObserver.h"
14 #include "nsIWeakReference.h"
15 #include "nsHashKeys.h"
16 #include "nsISimpleEnumerator.h"
17 #include "mozilla/Attributes.h"
20 class ObserverServiceReporter
;
21 } // namespace mozilla
25 ObserverRef(const ObserverRef
& aO
) : isWeakRef(aO
.isWeakRef
), ref(aO
.ref
) {}
26 explicit ObserverRef(nsIObserver
* aObserver
) : isWeakRef(false), ref(aObserver
) {}
27 explicit ObserverRef(nsIWeakReference
* aWeak
) : isWeakRef(true), ref(aWeak
) {}
30 nsCOMPtr
<nsISupports
> ref
;
32 nsIObserver
* asObserver()
34 NS_ASSERTION(!isWeakRef
, "Isn't a strong ref.");
35 return static_cast<nsIObserver
*>((nsISupports
*)ref
);
38 nsIWeakReference
* asWeak()
40 NS_ASSERTION(isWeakRef
, "Isn't a weak ref.");
41 return static_cast<nsIWeakReference
*>((nsISupports
*)ref
);
44 bool operator==(nsISupports
* aRhs
) const { return ref
== aRhs
; }
47 class nsObserverList
: public nsCharPtrHashKey
49 friend class nsObserverService
;
52 explicit nsObserverList(const char* aKey
) : nsCharPtrHashKey(aKey
)
54 MOZ_COUNT_CTOR(nsObserverList
);
59 MOZ_COUNT_DTOR(nsObserverList
);
62 nsresult
AddObserver(nsIObserver
* aObserver
, bool aOwnsWeak
);
63 nsresult
RemoveObserver(nsIObserver
* aObserver
);
65 void NotifyObservers(nsISupports
* aSubject
,
67 const char16_t
* aSomeData
);
68 nsresult
GetObserverList(nsISimpleEnumerator
** aEnumerator
);
70 // Fill an array with the observers of this category.
71 // The array is filled in last-added-first order.
72 void FillObserverArray(nsCOMArray
<nsIObserver
>& aArray
);
74 // Unmark any strongly held observers implemented in JS so the cycle
75 // collector will not traverse them.
76 void UnmarkGrayStrongObservers();
79 nsTArray
<ObserverRef
> mObservers
;
82 class nsObserverEnumerator MOZ_FINAL
: public nsISimpleEnumerator
86 NS_DECL_NSISIMPLEENUMERATOR
88 explicit nsObserverEnumerator(nsObserverList
* aObserverList
);
91 ~nsObserverEnumerator() {}
93 int32_t mIndex
; // Counts up from 0
94 nsCOMArray
<nsIObserver
> mObservers
;
97 #endif /* nsObserverList_h___ */