Bug 1473631: Part 7 - Replace pref observers with callbacks in WatchdogManager. r?njn draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 05 Jul 2018 14:30:58 -0700
changeset 818479 f4516506bd2996b092fd8cd13fcf94901b17a51b
parent 818478 6544e3b3c869ad1f0454fb40bcb6d383cd2ec26e
child 818480 cf171797dc1d388443e64d28a3495911496b0ae3
push id116269
push usermaglione.k@gmail.com
push dateSat, 14 Jul 2018 02:18:47 +0000
reviewersnjn
bugs1473631
milestone63.0a1
Bug 1473631: Part 7 - Replace pref observers with callbacks in WatchdogManager. r?njn MozReview-Commit-ID: 7Ctch5PYQzh
js/xpconnect/src/XPCJSContext.cpp
js/xpconnect/src/xpcprivate.h
--- a/js/xpconnect/src/XPCJSContext.cpp
+++ b/js/xpconnect/src/XPCJSContext.cpp
@@ -229,58 +229,56 @@ class Watchdog
     bool mShuttingDown;
     mozilla::Atomic<int32_t> mMinScriptRunTimeSeconds;
 };
 
 #define PREF_MAX_SCRIPT_RUN_TIME_CONTENT "dom.max_script_run_time"
 #define PREF_MAX_SCRIPT_RUN_TIME_CHROME "dom.max_chrome_script_run_time"
 #define PREF_MAX_SCRIPT_RUN_TIME_EXT_CONTENT "dom.max_ext_content_script_run_time"
 
-class WatchdogManager : public nsIObserver
+static const char* gCallbackPrefs[] = {
+    "dom.use_watchdog",
+    PREF_MAX_SCRIPT_RUN_TIME_CONTENT,
+    PREF_MAX_SCRIPT_RUN_TIME_CHROME,
+    PREF_MAX_SCRIPT_RUN_TIME_EXT_CONTENT,
+    nullptr,
+};
+
+class WatchdogManager
 {
   public:
-
-    NS_DECL_ISUPPORTS
     explicit WatchdogManager()
     {
         // All the timestamps start at zero.
         PodArrayZero(mTimestamps);
 
         // Register ourselves as an observer to get updates on the pref.
-        mozilla::Preferences::AddStrongObserver(this, "dom.use_watchdog");
-        mozilla::Preferences::AddStrongObserver(this, PREF_MAX_SCRIPT_RUN_TIME_CONTENT);
-        mozilla::Preferences::AddStrongObserver(this, PREF_MAX_SCRIPT_RUN_TIME_CHROME);
-        mozilla::Preferences::AddStrongObserver(this, PREF_MAX_SCRIPT_RUN_TIME_EXT_CONTENT);
+        Preferences::RegisterCallbacks(PrefsChanged, gCallbackPrefs, this);
     }
 
-  protected:
-
     virtual ~WatchdogManager()
     {
         // Shutting down the watchdog requires context-switching to the watchdog
         // thread, which isn't great to do in a destructor. So we require
         // consumers to shut it down manually before releasing it.
         MOZ_ASSERT(!mWatchdog);
     }
 
+  private:
+
+    static void PrefsChanged(const char* aPref, WatchdogManager* aSelf)
+    {
+        aSelf->RefreshWatchdog();
+    }
+
   public:
 
     void Shutdown()
     {
-        mozilla::Preferences::RemoveObserver(this, "dom.use_watchdog");
-        mozilla::Preferences::RemoveObserver(this, PREF_MAX_SCRIPT_RUN_TIME_CONTENT);
-        mozilla::Preferences::RemoveObserver(this, PREF_MAX_SCRIPT_RUN_TIME_CHROME);
-        mozilla::Preferences::RemoveObserver(this, PREF_MAX_SCRIPT_RUN_TIME_EXT_CONTENT);
-    }
-
-    NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
-                       const char16_t* aData) override
-    {
-        RefreshWatchdog();
-        return NS_OK;
+        Preferences::UnregisterCallbacks(PrefsChanged, gCallbackPrefs, this);
     }
 
     void
     RegisterContext(XPCJSContext* aContext)
     {
         MOZ_ASSERT(NS_IsMainThread());
         AutoLockWatchdog lock(mWatchdog);
 
@@ -449,18 +447,16 @@ class WatchdogManager : public nsIObserv
     LinkedList<XPCJSContext> mActiveContexts;
     LinkedList<XPCJSContext> mInactiveContexts;
     nsAutoPtr<Watchdog> mWatchdog;
 
     // We store ContextStateChange on the contexts themselves.
     PRTime mTimestamps[kWatchdogTimestampCategoryCount - 1];
 };
 
-NS_IMPL_ISUPPORTS(WatchdogManager, nsIObserver)
-
 AutoLockWatchdog::AutoLockWatchdog(Watchdog* aWatchdog MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
   : mWatchdog(aWatchdog)
 {
     MOZ_GUARD_OBJECT_NOTIFIER_INIT;
     if (mWatchdog) {
         PR_Lock(mWatchdog->GetLock());
     }
 }
@@ -1141,17 +1137,17 @@ XPCJSContext::Initialize(XPCJSContext* a
     return NS_OK;
 }
 
 // static
 uint32_t
 XPCJSContext::sInstanceCount;
 
 // static
-StaticRefPtr<WatchdogManager>
+StaticAutoPtr<WatchdogManager>
 XPCJSContext::sWatchdogInstance;
 
 // static
 WatchdogManager*
 XPCJSContext::GetWatchdogManager()
 {
     if (sWatchdogInstance) {
         return sWatchdogInstance;
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -431,17 +431,17 @@ private:
     XPCCallContext*          mCallContext;
     AutoMarkingPtr*          mAutoRoots;
     jsid                     mResolveName;
     XPCWrappedNative*        mResolvingWrapper;
     WatchdogManager*         mWatchdogManager;
 
     // Number of XPCJSContexts currently alive.
     static uint32_t         sInstanceCount;
-    static mozilla::StaticRefPtr<WatchdogManager> sWatchdogInstance;
+    static mozilla::StaticAutoPtr<WatchdogManager> sWatchdogInstance;
     static WatchdogManager* GetWatchdogManager();
 
     // If we spend too much time running JS code in an event handler, then we
     // want to show the slow script UI. The timeout T is controlled by prefs. We
     // invoke the interrupt callback once after T/2 seconds and set
     // mSlowScriptSecondHalf to true. After another T/2 seconds, we invoke the
     // interrupt callback again. Since mSlowScriptSecondHalf is now true, it
     // shows the slow script UI. The reason we invoke the callback twice is to