tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DebuggerNotificationManager.h (2073B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_DebuggerNotificationManager_h
      8 #define mozilla_dom_DebuggerNotificationManager_h
      9 
     10 #include "DebuggerNotificationObserver.h"
     11 #include "nsCycleCollectionParticipant.h"
     12 #include "nsIGlobalObject.h"
     13 #include "nsISupports.h"
     14 #include "nsTObserverArray.h"
     15 
     16 namespace mozilla::dom {
     17 
     18 class DebuggerNotification;
     19 class DebuggerNotificationObserver;
     20 
     21 class DebuggerNotificationManager final : public nsISupports {
     22 public:
     23  NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL
     24  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DebuggerNotificationManager)
     25 
     26  static RefPtr<DebuggerNotificationManager> ForDispatch(
     27      nsIGlobalObject* aDebuggeeGlobal) {
     28    if (MOZ_UNLIKELY(!aDebuggeeGlobal)) {
     29      return nullptr;
     30    }
     31    auto managerPtr = aDebuggeeGlobal->GetExistingDebuggerNotificationManager();
     32    if (MOZ_LIKELY(!managerPtr) || !managerPtr->HasListeners()) {
     33      return nullptr;
     34    }
     35 
     36    return managerPtr;
     37  }
     38 
     39  explicit DebuggerNotificationManager(nsIGlobalObject* aDebuggeeGlobal)
     40      : mDebuggeeGlobal(aDebuggeeGlobal) {}
     41 
     42  bool Attach(DebuggerNotificationObserver* aObserver);
     43  bool Detach(DebuggerNotificationObserver* aObserver);
     44 
     45  bool HasListeners();
     46 
     47  template <typename T, typename... Args>
     48  MOZ_CAN_RUN_SCRIPT void Dispatch(Args... aArgs) {
     49    RefPtr<DebuggerNotification> notification(new T(mDebuggeeGlobal, aArgs...));
     50    NotifyListeners(notification);
     51  }
     52 
     53 private:
     54  ~DebuggerNotificationManager() = default;
     55 
     56  MOZ_CAN_RUN_SCRIPT void NotifyListeners(DebuggerNotification* aNotification);
     57 
     58  nsCOMPtr<nsIGlobalObject> mDebuggeeGlobal;
     59  nsTObserverArray<RefPtr<DebuggerNotificationObserver>> mNotificationObservers;
     60 };
     61 
     62 }  // namespace mozilla::dom
     63 
     64 #endif  // mozilla_dom_DebuggerNotificationManager_h