Bug 1454441 - 1. Add GeckoViewUtils.addLazyPrefObserver; r=esawin
☠☠ backed out by 85d1752bed2e ☠ ☠
authorJim Chen <nchen@mozilla.com>
Fri, 20 Apr 2018 15:51:29 -0400
changeset 468323 fbcb2eee69fc2337b8cdb0079f2e20f3a3ef11a9
parent 468322 cba1a84a758fd762a07886960bd0b0c1ac92578c
child 468324 99fa43f0686e1ae486b1b0a49a06531db4d52aad
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersesawin
bugs1454441
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1454441 - 1. Add GeckoViewUtils.addLazyPrefObserver; r=esawin Add a convenience function to add lazy pref observers that only load a given module if the pref value deviates from default or when the pref value changes. MozReview-Commit-ID: G7o3B61W5Bn
mobile/android/modules/geckoview/GeckoViewUtils.jsm
--- a/mobile/android/modules/geckoview/GeckoViewUtils.jsm
+++ b/mobile/android/modules/geckoview/GeckoViewUtils.jsm
@@ -174,16 +174,68 @@ var GeckoViewUtils = {
           dispatcher.unregisterListener(listener, args[0]);
           handlers.forEach(handler =>
             dispatcher.registerListener(handler, args[0]));
         }
         handlers.forEach(handler => handler.onEvent(...args));
       });
   },
 
+  /**
+   * Add lazy pref observers, and only load the actual handler once the pref
+   * value changes from default, and every time the pref value changes
+   * afterwards.
+   *
+   * @param aPrefs  Prefs as an object or array. Each pref object has fields
+   *                "name" and "default", indicating the name and default value
+   *                of the pref, respectively.
+   * @param handler If specified, function that, for a given pref, returns the
+   *                actual event handler as an object or an array of objects.
+   *                If handler is not specified, the actual event handler is
+   *                specified using the scope and name pair.
+   * @param scope   See handler.
+   * @param name    See handler.
+   * @param once    If true, only observe the specified prefs once.
+   */
+  addLazyPrefObserver: function(aPrefs, {handler, scope, name, once}) {
+    this._addLazyListeners(aPrefs, handler, scope, name,
+      (prefs, observer) => {
+        prefs.forEach(pref => Services.prefs.addObserver(pref.name, observer));
+        prefs.forEach(pref => {
+          if (pref.default === undefined) {
+            return;
+          }
+          let value;
+          switch (typeof pref.default) {
+            case "string":
+              value = Services.prefs.getCharPref(pref.name, pref.default);
+              break;
+            case "number":
+              value = Services.prefs.getIntPref(pref.name, pref.default);
+              break;
+            case "boolean":
+              value = Services.prefs.getBoolPref(pref.name, pref.default);
+              break;
+          }
+          if (pref.default !== value) {
+            // Notify observer if value already changed from default.
+            observer(Services.prefs, "nsPref:changed", pref.name);
+          }
+        });
+      },
+      (handlers, observer, args) => {
+        if (!once) {
+          Services.prefs.removeObserver(args[2], observer);
+          handlers.forEach(handler =>
+            Services.prefs.addObserver(args[2], observer));
+        }
+        handlers.forEach(handler => handler.observe(...args));
+      });
+  },
+
   getRootDocShell: function(aWin) {
     if (!aWin) {
       return null;
     }
     let docShell;
     try {
       docShell = aWin.QueryInterface(Ci.nsIDocShell);
     } catch (e) {