xpcom/ds/nsObserverList.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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"
    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"
    19 namespace mozilla {
    20 class ObserverServiceReporter;
    21 } // namespace mozilla
    23 struct ObserverRef
    24 {
    25   ObserverRef(const ObserverRef& o) :
    26     isWeakRef(o.isWeakRef), ref(o.ref) { }
    28   ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) { }
    29   ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) { }
    31   bool isWeakRef;
    32   nsCOMPtr<nsISupports> ref;
    34   nsIObserver* asObserver() {
    35     NS_ASSERTION(!isWeakRef, "Isn't a strong ref.");
    36     return static_cast<nsIObserver*>((nsISupports*) ref);
    37   }
    39   nsIWeakReference* asWeak() {
    40     NS_ASSERTION(isWeakRef, "Isn't a weak ref.");
    41     return static_cast<nsIWeakReference*>((nsISupports*) ref);
    42   }
    44   bool operator==(nsISupports* b) const { return ref == b; }
    45 };
    47 class nsObserverList : public nsCharPtrHashKey
    48 {
    49   friend class nsObserverService;
    51 public:
    52   nsObserverList(const char *key) : nsCharPtrHashKey(key)
    53   { MOZ_COUNT_CTOR(nsObserverList); }
    55   ~nsObserverList() { MOZ_COUNT_DTOR(nsObserverList); }
    57   nsresult AddObserver(nsIObserver* anObserver, bool ownsWeak);
    58   nsresult RemoveObserver(nsIObserver* anObserver);
    60   void NotifyObservers(nsISupports *aSubject,
    61                        const char *aTopic,
    62                        const char16_t *someData);
    63   nsresult GetObserverList(nsISimpleEnumerator** anEnumerator);
    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);
    69   // Unmark any strongly held observers implemented in JS so the cycle
    70   // collector will not traverse them.
    71   void UnmarkGrayStrongObservers();
    73 private:
    74   nsTArray<ObserverRef> mObservers;
    75 };
    77 class nsObserverEnumerator MOZ_FINAL : public nsISimpleEnumerator
    78 {
    79 public:
    80     NS_DECL_ISUPPORTS
    81     NS_DECL_NSISIMPLEENUMERATOR
    83     nsObserverEnumerator(nsObserverList* aObserverList);
    85 private:
    86     ~nsObserverEnumerator() { }
    88     int32_t mIndex; // Counts up from 0
    89     nsCOMArray<nsIObserver> mObservers;
    90 };
    92 #endif /* nsObserverList_h___ */

mercurial