Bug 1454441 - 1. Add GeckoViewUtils.addLazyPrefObserver; r=esawin
☠☠ backed out by aac9f21986a7 ☠ ☠
authorJim Chen <nchen@mozilla.com>
Mon, 23 Apr 2018 12:07:34 -0400
changeset 468565 fad6be573021af3043f170a612f39f00ac03185f
parent 468564 a8e18873bce8624ef51c9ecd42d0da82c3e1cf68
child 468566 deb0e12bfbd756cee97ed50ab89ad9e29b499118
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: Gh0zFC8FZih
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) {