Bug 1309384 - merge default and user prefs in Services shim; r?jdescottes draft
authorTom Tromey <tom@tromey.com>
Fri, 14 Oct 2016 09:27:44 -0600
changeset 425411 f69db8099a1feaf1d50583af104daa45ca3c477c
parent 424933 62ccb498db3ec05bef1b6427d5b1b4b0e501cad5
child 425412 ab38145a99449f66b50b8d525f2fff5e829d66f1
push id32425
push userbmo:ttromey@mozilla.com
push dateFri, 14 Oct 2016 19:20:10 +0000
reviewersjdescottes
bugs1309384
milestone52.0a1
Bug 1309384 - merge default and user prefs in Services shim; r?jdescottes MozReview-Commit-ID: 19rvPSGOsOZ
devtools/client/shared/shim/Services.js
devtools/client/shared/shim/test/mochitest.ini
devtools/client/shared/shim/test/test_service_prefs_defaults.html
--- a/devtools/client/shared/shim/Services.js
+++ b/devtools/client/shared/shim/Services.js
@@ -351,22 +351,25 @@ PrefBranch.prototype = {
 
   /**
    * Create a new preference.  The new preference is assumed to be in
    * local storage already, and the new value is taken from there.
    *
    * @param {String} keyName the full-qualified name of the preference.
    *        This is also the name of the key in local storage.
    * @param {Any} userValue the user value to use if the pref does not exist
+   * @param {Boolean} hasUserValue if a new pref is created, whether
+   *        the default value is also a user value
    * @param {Any} defaultValue the default value to use if the pref
    *        does not exist
-   * @param {Boolean} hasUserValue if a new pref is created, whether
-   *        the default value is also a user value
+   * @param {Boolean} init if true, then this call is initialization
+   *        from local storage and should override the default prefs
    */
-  _findOrCreatePref: function (keyName, userValue, hasUserValue, defaultValue) {
+  _findOrCreatePref: function (keyName, userValue, hasUserValue, defaultValue,
+                               init = false) {
     let branch = this._createBranch(keyName.split("."));
 
     if (hasUserValue && typeof (userValue) !== typeof (defaultValue)) {
       throw new Error("inconsistent values when creating " + keyName);
     }
 
     let type;
     switch (typeof (defaultValue)) {
@@ -378,17 +381,17 @@ PrefBranch.prototype = {
         break;
       case "string":
         type = PREF_STRING;
         break;
       default:
         throw new Error("unhandled argument type: " + typeof (defaultValue));
     }
 
-    if (branch._type === PREF_INVALID) {
+    if (init || branch._type === PREF_INVALID) {
       branch._storageUpdated(type, userValue, hasUserValue, defaultValue);
     } else if (branch._type !== type) {
       throw new Error("attempt to change type of pref " + keyName);
     }
 
     return branch;
   },
 
@@ -417,34 +420,34 @@ PrefBranch.prototype = {
       thePref._storageUpdated(type, userValue, hasUserValue, defaultValue);
     }
   },
 
   /**
    * Helper function to initialize the root PrefBranch.
    */
   _initializeRoot: function () {
-    if (localStorage.length === 0 && Services._defaultPrefsEnabled) {
+    if (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} =
             JSON.parse(localStorage.getItem(keyName));
         this._findOrCreatePref(keyName.slice(PREFIX.length), userValue,
-                               hasUserValue, defaultValue);
+                               hasUserValue, defaultValue, true);
       }
     }
 
     this._onStorageChange = this._onStorageChange.bind(this);
     window.addEventListener("storage", this._onStorageChange);
   },
 };
 
--- a/devtools/client/shared/shim/test/mochitest.ini
+++ b/devtools/client/shared/shim/test/mochitest.ini
@@ -1,7 +1,8 @@
 [DEFAULT]
 support-files =
   prefs-wrapper.js
 
 [test_service_appinfo.html]
 [test_service_focus.html]
 [test_service_prefs.html]
+[test_service_prefs_defaults.html]
new file mode 100644
--- /dev/null
+++ b/devtools/client/shared/shim/test/test_service_prefs_defaults.html
@@ -0,0 +1,63 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1309384
+-->
+<head>
+  <title>Test for Bug 1309384 - Services.prefs replacement defaults handling</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css"
+        href="chrome://mochikit/content/tests/SimpleTest/test.css">
+
+<script type="application/javascript;version=1.8">
+"use strict";
+var exports = {}
+var module = {exports};
+
+// Allow one require("raw!prefs...") to return some defaults, with the
+// others being ignored.
+var firstTime = true;
+function require(something) {
+  if (!something.startsWith("raw!prefs!")) {
+    throw new Error("whoops");
+  }
+  if (!firstTime) {
+    return "";
+  }
+  firstTime = false;
+  return "pref('pref1', 'pref1default');\n" +
+    "pref('pref2', 'pref2default');\n" +
+    "pref('pref3', 'pref3default');\n";
+}
+
+// Pretend that one of the prefs was modifed by the user in an earlier session.
+localStorage.setItem("Services.prefs:pref3", JSON.stringify({
+  // string
+  type: 32,
+  defaultValue: "pref3default",
+  hasUserValue: true,
+  userValue: "glass winged butterfly"
+}));
+
+</script>
+
+  <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";
+
+is(Services.prefs.getCharPref("pref1"), "pref1default", "pref1 value");
+is(Services.prefs.getCharPref("pref2"), "pref2default", "pref2 value");
+is(Services.prefs.getCharPref("pref3"), "glass winged butterfly", "pref3 value");
+
+// Only pref3 should be in local storage at this point.
+is(localStorage.length, 1, "local storage is correct");
+
+// Clean up.
+localStorage.clear();
+
+</script>
+</body>