tor-browser

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

CallbackDebuggerNotification.h (4529B)


      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_CallbackDebuggerNotification_h
      8 #define mozilla_dom_CallbackDebuggerNotification_h
      9 
     10 #include "DebuggerNotification.h"
     11 #include "DebuggerNotificationManager.h"
     12 #include "mozilla/CycleCollectedJSContext.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 class CallbackDebuggerNotification : public DebuggerNotification {
     17 public:
     18  NS_DECL_ISUPPORTS_INHERITED
     19  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CallbackDebuggerNotification,
     20                                           DebuggerNotification)
     21 
     22  CallbackDebuggerNotification(nsIGlobalObject* aDebuggeeGlobal,
     23                               DebuggerNotificationType aType,
     24                               CallbackDebuggerNotificationPhase aPhase,
     25                               nsIGlobalObject* aOwnerGlobal = nullptr)
     26      : DebuggerNotification(aDebuggeeGlobal, aType, aOwnerGlobal),
     27        mPhase(aPhase) {}
     28 
     29  // nsWrapperCache
     30  virtual JSObject* WrapObject(JSContext* aCx,
     31                               JS::Handle<JSObject*> aGivenProto) override;
     32 
     33  already_AddRefed<DebuggerNotification> CloneInto(
     34      nsIGlobalObject* aNewOwner) const override;
     35 
     36  CallbackDebuggerNotificationPhase Phase() const { return mPhase; }
     37 
     38 protected:
     39  ~CallbackDebuggerNotification() = default;
     40 
     41  CallbackDebuggerNotificationPhase mPhase;
     42 };
     43 
     44 class MOZ_RAII CallbackDebuggerNotificationGuard final {
     45 public:
     46  MOZ_CAN_RUN_SCRIPT CallbackDebuggerNotificationGuard(
     47      nsIGlobalObject* aDebuggeeGlobal, DebuggerNotificationType aType)
     48      : mDebuggeeGlobal(aDebuggeeGlobal), mType(aType) {
     49    Dispatch(CallbackDebuggerNotificationPhase::Pre);
     50  }
     51  CallbackDebuggerNotificationGuard(const CallbackDebuggerNotificationGuard&) =
     52      delete;
     53  CallbackDebuggerNotificationGuard(CallbackDebuggerNotificationGuard&&) =
     54      delete;
     55  CallbackDebuggerNotificationGuard& operator=(
     56      const CallbackDebuggerNotificationGuard&) = delete;
     57  CallbackDebuggerNotificationGuard& operator=(
     58      CallbackDebuggerNotificationGuard&&) = delete;
     59 
     60  MOZ_CAN_RUN_SCRIPT ~CallbackDebuggerNotificationGuard() {
     61    Dispatch(CallbackDebuggerNotificationPhase::Post);
     62  }
     63 
     64 private:
     65  MOZ_CAN_RUN_SCRIPT void Dispatch(CallbackDebuggerNotificationPhase aPhase) {
     66 #ifdef MOZ_EXECUTION_TRACING
     67    if (MOZ_UNLIKELY(profiler_is_active())) {
     68      CycleCollectedJSContext* ccjcx = CycleCollectedJSContext::Get();
     69      if (ccjcx) {
     70        const char* typeStr = "";
     71        switch (mType) {
     72          case DebuggerNotificationType::SetTimeout:
     73            typeStr = "setTimeout";
     74            break;
     75          case DebuggerNotificationType::ClearTimeout:
     76            typeStr = "clearTimeout";
     77            break;
     78          case DebuggerNotificationType::SetInterval:
     79            typeStr = "setInterval";
     80            break;
     81          case DebuggerNotificationType::ClearInterval:
     82            typeStr = "clearInterval";
     83            break;
     84          case DebuggerNotificationType::RequestAnimationFrame:
     85            typeStr = "requestAnimationFrame";
     86            break;
     87          case DebuggerNotificationType::CancelAnimationFrame:
     88            typeStr = "cancelAnimationFrame";
     89            break;
     90          case DebuggerNotificationType::SetTimeoutCallback:
     91            typeStr = "setTimeout";
     92            break;
     93          case DebuggerNotificationType::SetIntervalCallback:
     94            typeStr = "setInterval";
     95            break;
     96          case DebuggerNotificationType::RequestAnimationFrameCallback:
     97            typeStr = "requestAnimationFrame";
     98            break;
     99          case DebuggerNotificationType::DomEvent:
    100            MOZ_CRASH("Unreachable");
    101            break;
    102        }
    103        if (aPhase == CallbackDebuggerNotificationPhase::Pre) {
    104          JS_TracerEnterLabelLatin1(ccjcx->Context(), typeStr);
    105        } else {
    106          JS_TracerLeaveLabelLatin1(ccjcx->Context(), typeStr);
    107        }
    108      }
    109    }
    110 #endif
    111 
    112    auto manager = DebuggerNotificationManager::ForDispatch(mDebuggeeGlobal);
    113    if (MOZ_UNLIKELY(manager)) {
    114      manager->Dispatch<CallbackDebuggerNotification>(mType, aPhase);
    115    }
    116  }
    117 
    118  nsIGlobalObject* mDebuggeeGlobal;
    119  DebuggerNotificationType mType;
    120 };
    121 
    122 }  // namespace mozilla::dom
    123 
    124 #endif  // mozilla_dom_CallbackDebuggerNotification_h