xpcom/ds/nsObserverList.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/nsObserverList.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,92 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsObserverList_h___
    1.10 +#define nsObserverList_h___
    1.11 +
    1.12 +#include "nsISupports.h"
    1.13 +#include "nsTArray.h"
    1.14 +#include "nsCOMPtr.h"
    1.15 +#include "nsCOMArray.h"
    1.16 +#include "nsIObserver.h"
    1.17 +#include "nsIWeakReference.h"
    1.18 +#include "nsHashKeys.h"
    1.19 +#include "nsISimpleEnumerator.h"
    1.20 +#include "mozilla/Attributes.h"
    1.21 +
    1.22 +namespace mozilla {
    1.23 +class ObserverServiceReporter;
    1.24 +} // namespace mozilla
    1.25 +
    1.26 +struct ObserverRef
    1.27 +{
    1.28 +  ObserverRef(const ObserverRef& o) :
    1.29 +    isWeakRef(o.isWeakRef), ref(o.ref) { }
    1.30 +  
    1.31 +  ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) { }
    1.32 +  ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) { }
    1.33 +
    1.34 +  bool isWeakRef;
    1.35 +  nsCOMPtr<nsISupports> ref;
    1.36 +
    1.37 +  nsIObserver* asObserver() {
    1.38 +    NS_ASSERTION(!isWeakRef, "Isn't a strong ref.");
    1.39 +    return static_cast<nsIObserver*>((nsISupports*) ref);
    1.40 +  }
    1.41 +
    1.42 +  nsIWeakReference* asWeak() {
    1.43 +    NS_ASSERTION(isWeakRef, "Isn't a weak ref.");
    1.44 +    return static_cast<nsIWeakReference*>((nsISupports*) ref);
    1.45 +  }
    1.46 +
    1.47 +  bool operator==(nsISupports* b) const { return ref == b; }
    1.48 +};
    1.49 +
    1.50 +class nsObserverList : public nsCharPtrHashKey
    1.51 +{
    1.52 +  friend class nsObserverService;
    1.53 +
    1.54 +public:
    1.55 +  nsObserverList(const char *key) : nsCharPtrHashKey(key)
    1.56 +  { MOZ_COUNT_CTOR(nsObserverList); }
    1.57 +
    1.58 +  ~nsObserverList() { MOZ_COUNT_DTOR(nsObserverList); }
    1.59 +
    1.60 +  nsresult AddObserver(nsIObserver* anObserver, bool ownsWeak);
    1.61 +  nsresult RemoveObserver(nsIObserver* anObserver);
    1.62 +
    1.63 +  void NotifyObservers(nsISupports *aSubject,
    1.64 +                       const char *aTopic,
    1.65 +                       const char16_t *someData);
    1.66 +  nsresult GetObserverList(nsISimpleEnumerator** anEnumerator);
    1.67 +
    1.68 +  // Fill an array with the observers of this category.
    1.69 +  // The array is filled in last-added-first order.
    1.70 +  void FillObserverArray(nsCOMArray<nsIObserver> &aArray);
    1.71 +
    1.72 +  // Unmark any strongly held observers implemented in JS so the cycle
    1.73 +  // collector will not traverse them.
    1.74 +  void UnmarkGrayStrongObservers();
    1.75 +
    1.76 +private:
    1.77 +  nsTArray<ObserverRef> mObservers;
    1.78 +};
    1.79 +
    1.80 +class nsObserverEnumerator MOZ_FINAL : public nsISimpleEnumerator
    1.81 +{
    1.82 +public:
    1.83 +    NS_DECL_ISUPPORTS
    1.84 +    NS_DECL_NSISIMPLEENUMERATOR
    1.85 +
    1.86 +    nsObserverEnumerator(nsObserverList* aObserverList);
    1.87 +
    1.88 +private:
    1.89 +    ~nsObserverEnumerator() { }
    1.90 +
    1.91 +    int32_t mIndex; // Counts up from 0
    1.92 +    nsCOMArray<nsIObserver> mObservers;
    1.93 +};
    1.94 +
    1.95 +#endif /* nsObserverList_h___ */

mercurial