Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsObserverList_h___ |
michael@0 | 7 | #define nsObserverList_h___ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsISupports.h" |
michael@0 | 10 | #include "nsTArray.h" |
michael@0 | 11 | #include "nsCOMPtr.h" |
michael@0 | 12 | #include "nsCOMArray.h" |
michael@0 | 13 | #include "nsIObserver.h" |
michael@0 | 14 | #include "nsIWeakReference.h" |
michael@0 | 15 | #include "nsHashKeys.h" |
michael@0 | 16 | #include "nsISimpleEnumerator.h" |
michael@0 | 17 | #include "mozilla/Attributes.h" |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | class ObserverServiceReporter; |
michael@0 | 21 | } // namespace mozilla |
michael@0 | 22 | |
michael@0 | 23 | struct ObserverRef |
michael@0 | 24 | { |
michael@0 | 25 | ObserverRef(const ObserverRef& o) : |
michael@0 | 26 | isWeakRef(o.isWeakRef), ref(o.ref) { } |
michael@0 | 27 | |
michael@0 | 28 | ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) { } |
michael@0 | 29 | ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) { } |
michael@0 | 30 | |
michael@0 | 31 | bool isWeakRef; |
michael@0 | 32 | nsCOMPtr<nsISupports> ref; |
michael@0 | 33 | |
michael@0 | 34 | nsIObserver* asObserver() { |
michael@0 | 35 | NS_ASSERTION(!isWeakRef, "Isn't a strong ref."); |
michael@0 | 36 | return static_cast<nsIObserver*>((nsISupports*) ref); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | nsIWeakReference* asWeak() { |
michael@0 | 40 | NS_ASSERTION(isWeakRef, "Isn't a weak ref."); |
michael@0 | 41 | return static_cast<nsIWeakReference*>((nsISupports*) ref); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | bool operator==(nsISupports* b) const { return ref == b; } |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | class nsObserverList : public nsCharPtrHashKey |
michael@0 | 48 | { |
michael@0 | 49 | friend class nsObserverService; |
michael@0 | 50 | |
michael@0 | 51 | public: |
michael@0 | 52 | nsObserverList(const char *key) : nsCharPtrHashKey(key) |
michael@0 | 53 | { MOZ_COUNT_CTOR(nsObserverList); } |
michael@0 | 54 | |
michael@0 | 55 | ~nsObserverList() { MOZ_COUNT_DTOR(nsObserverList); } |
michael@0 | 56 | |
michael@0 | 57 | nsresult AddObserver(nsIObserver* anObserver, bool ownsWeak); |
michael@0 | 58 | nsresult RemoveObserver(nsIObserver* anObserver); |
michael@0 | 59 | |
michael@0 | 60 | void NotifyObservers(nsISupports *aSubject, |
michael@0 | 61 | const char *aTopic, |
michael@0 | 62 | const char16_t *someData); |
michael@0 | 63 | nsresult GetObserverList(nsISimpleEnumerator** anEnumerator); |
michael@0 | 64 | |
michael@0 | 65 | // Fill an array with the observers of this category. |
michael@0 | 66 | // The array is filled in last-added-first order. |
michael@0 | 67 | void FillObserverArray(nsCOMArray<nsIObserver> &aArray); |
michael@0 | 68 | |
michael@0 | 69 | // Unmark any strongly held observers implemented in JS so the cycle |
michael@0 | 70 | // collector will not traverse them. |
michael@0 | 71 | void UnmarkGrayStrongObservers(); |
michael@0 | 72 | |
michael@0 | 73 | private: |
michael@0 | 74 | nsTArray<ObserverRef> mObservers; |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | class nsObserverEnumerator MOZ_FINAL : public nsISimpleEnumerator |
michael@0 | 78 | { |
michael@0 | 79 | public: |
michael@0 | 80 | NS_DECL_ISUPPORTS |
michael@0 | 81 | NS_DECL_NSISIMPLEENUMERATOR |
michael@0 | 82 | |
michael@0 | 83 | nsObserverEnumerator(nsObserverList* aObserverList); |
michael@0 | 84 | |
michael@0 | 85 | private: |
michael@0 | 86 | ~nsObserverEnumerator() { } |
michael@0 | 87 | |
michael@0 | 88 | int32_t mIndex; // Counts up from 0 |
michael@0 | 89 | nsCOMArray<nsIObserver> mObservers; |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | #endif /* nsObserverList_h___ */ |