|
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/. */ |
|
5 |
|
6 #ifndef nsObserverList_h___ |
|
7 #define nsObserverList_h___ |
|
8 |
|
9 #include "nsISupports.h" |
|
10 #include "nsTArray.h" |
|
11 #include "nsCOMPtr.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" |
|
18 |
|
19 namespace mozilla { |
|
20 class ObserverServiceReporter; |
|
21 } // namespace mozilla |
|
22 |
|
23 struct ObserverRef |
|
24 { |
|
25 ObserverRef(const ObserverRef& o) : |
|
26 isWeakRef(o.isWeakRef), ref(o.ref) { } |
|
27 |
|
28 ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) { } |
|
29 ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) { } |
|
30 |
|
31 bool isWeakRef; |
|
32 nsCOMPtr<nsISupports> ref; |
|
33 |
|
34 nsIObserver* asObserver() { |
|
35 NS_ASSERTION(!isWeakRef, "Isn't a strong ref."); |
|
36 return static_cast<nsIObserver*>((nsISupports*) ref); |
|
37 } |
|
38 |
|
39 nsIWeakReference* asWeak() { |
|
40 NS_ASSERTION(isWeakRef, "Isn't a weak ref."); |
|
41 return static_cast<nsIWeakReference*>((nsISupports*) ref); |
|
42 } |
|
43 |
|
44 bool operator==(nsISupports* b) const { return ref == b; } |
|
45 }; |
|
46 |
|
47 class nsObserverList : public nsCharPtrHashKey |
|
48 { |
|
49 friend class nsObserverService; |
|
50 |
|
51 public: |
|
52 nsObserverList(const char *key) : nsCharPtrHashKey(key) |
|
53 { MOZ_COUNT_CTOR(nsObserverList); } |
|
54 |
|
55 ~nsObserverList() { MOZ_COUNT_DTOR(nsObserverList); } |
|
56 |
|
57 nsresult AddObserver(nsIObserver* anObserver, bool ownsWeak); |
|
58 nsresult RemoveObserver(nsIObserver* anObserver); |
|
59 |
|
60 void NotifyObservers(nsISupports *aSubject, |
|
61 const char *aTopic, |
|
62 const char16_t *someData); |
|
63 nsresult GetObserverList(nsISimpleEnumerator** anEnumerator); |
|
64 |
|
65 // Fill an array with the observers of this category. |
|
66 // The array is filled in last-added-first order. |
|
67 void FillObserverArray(nsCOMArray<nsIObserver> &aArray); |
|
68 |
|
69 // Unmark any strongly held observers implemented in JS so the cycle |
|
70 // collector will not traverse them. |
|
71 void UnmarkGrayStrongObservers(); |
|
72 |
|
73 private: |
|
74 nsTArray<ObserverRef> mObservers; |
|
75 }; |
|
76 |
|
77 class nsObserverEnumerator MOZ_FINAL : public nsISimpleEnumerator |
|
78 { |
|
79 public: |
|
80 NS_DECL_ISUPPORTS |
|
81 NS_DECL_NSISIMPLEENUMERATOR |
|
82 |
|
83 nsObserverEnumerator(nsObserverList* aObserverList); |
|
84 |
|
85 private: |
|
86 ~nsObserverEnumerator() { } |
|
87 |
|
88 int32_t mIndex; // Counts up from 0 |
|
89 nsCOMArray<nsIObserver> mObservers; |
|
90 }; |
|
91 |
|
92 #endif /* nsObserverList_h___ */ |