Bug 1272098 - load default prefs into Services shim; r?bgrins draft
authorTom Tromey <tom@tromey.com>
Fri, 19 Aug 2016 14:36:27 -0600
changeset 404052 df653a7c999125d4489414961004e89e7fbb0882
parent 403834 662270014cb72f82af415385f2f0bef5b135ec04
child 529073 551e5af4fa0233bbd59402eb2ddbd2a53c176221
push id27088
push userbmo:ttromey@mozilla.com
push dateMon, 22 Aug 2016 18:45:27 +0000
reviewersbgrins
bugs1272098
milestone51.0a1
Bug 1272098 - load default prefs into Services shim; r?bgrins MozReview-Commit-ID: InZRrl47YWS
devtools/client/shared/shim/Services.js
devtools/client/shared/shim/test/test_service_prefs.html
--- a/devtools/client/shared/shim/Services.js
+++ b/devtools/client/shared/shim/Services.js
@@ -435,19 +435,23 @@ PrefBranch.prototype = {
       thePref.storageUpdated(type, userValue, hasUserValue, defaultValue);
     }
   },
 
   /**
    * Helper function to initialize the root PrefBranch.
    */
   _initializeRoot: function () {
-    if (localStorage.length === 0) {
-      // FIXME - this is where we'll load devtools.js to install the
-      // default prefs.
+    if (localStorage.length === 0 && Services._defaultPrefsEnabled) {
+      /* eslint-disable no-eval */
+      let devtools = require("raw!prefs!devtools/client/preferences/devtools");
+      eval(devtools);
+      let all = require("raw!prefs!modules/libpref/init/all");
+      eval(all);
+      /* eslint-enable no-eval */
     }
 
     // Read the prefs from local storage and create the local
     // representations.
     for (let i = 0; i < localStorage.length; ++i) {
       let keyName = localStorage.key(i);
       if (keyName.startsWith(PREFIX)) {
         let {userValue, hasUserValue, defaultValue} =
@@ -458,22 +462,35 @@ PrefBranch.prototype = {
     }
 
     this._onStorageChange = this._onStorageChange.bind(this);
     window.addEventListener("storage", this._onStorageChange);
   },
 };
 
 const Services = {
+  _prefs: null,
+
+  // For use by tests.  If set to false before Services.prefs is used,
+  // this will disable the reading of the default prefs.
+  _defaultPrefsEnabled: true,
+
   /**
    * An implementation of nsIPrefService that is based on local
    * storage.  Only the subset of nsIPrefService that is actually used
-   * by devtools is implemented here.
+   * by devtools is implemented here.  This is lazily instantiated so
+   * that the tests have a chance to disable the loading of default
+   * prefs.
    */
-  prefs: new PrefBranch(null, "", ""),
+  get prefs() {
+    if (!this._prefs) {
+      this._prefs = new PrefBranch(null, "", "");
+    }
+    return this._prefs;
+  },
 
   /**
    * An implementation of Services.appinfo that holds just the
    * properties needed by devtools.
    */
   appinfo: {
     get OS() {
       const os = window.navigator.userAgent;
@@ -591,12 +608,10 @@ const Services = {
  * @param {Any} value the default value of the preference
  */
 function pref(name, value) {
   let thePref = Services.prefs._findOrCreatePref(name, value, true, value);
   thePref.setDefault(value);
 }
 
 module.exports = Services;
-// This is exported to silence eslint and, at some point, perhaps to
-// provide it when loading devtools.js in order to install the default
-// preferences.
+// This is exported to silence eslint.
 exports.pref = pref;
--- a/devtools/client/shared/shim/test/test_service_prefs.html
+++ b/devtools/client/shared/shim/test/test_service_prefs.html
@@ -47,16 +47,19 @@ localStorage.setItem("Services.prefs:dev
   <script type="application/javascript;version=1.8"
 	  src="resource://devtools/client/shared/shim/Services.js"></script>
 </head>
 <body>
 <script type="application/javascript;version=1.8">
 "use strict";
 
 function do_tests() {
+  // We can't load the defaults in this context.
+  Services._defaultPrefsEnabled = false;
+
   is(Services.prefs.getBoolPref("devtools.branch1.somebool"), false,
     "bool pref value");
   Services.prefs.setBoolPref("devtools.branch1.somebool", true);
   is(Services.prefs.getBoolPref("devtools.branch1.somebool"), true,
     "bool pref value after setting");
 
   let threw;