Bug 1294190 - prefix local storage keys used by prefs; r?gregtatum draft
authorTom Tromey <tom@tromey.com>
Wed, 10 Aug 2016 15:36:26 -0600
changeset 399326 d4d17148a19aad9818caa8d7fe66a38357fdc62b
parent 399325 321d806c40a1cc55abf085718b07094ecc35d6e3
child 527902 81c2dfc0c058f134209a70b8c36dec892b315444
push id25800
push userbmo:ttromey@mozilla.com
push dateWed, 10 Aug 2016 21:38:19 +0000
reviewersgregtatum
bugs1294190
milestone51.0a1
Bug 1294190 - prefix local storage keys used by prefs; r?gregtatum MozReview-Commit-ID: 33oX6bHSfyi
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
@@ -10,16 +10,19 @@
 
 // Some constants from nsIPrefBranch.idl.
 const PREF_INVALID = 0;
 const PREF_STRING = 32;
 const PREF_INT = 64;
 const PREF_BOOL = 128;
 const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
 
+// We prefix all our local storage items with this.
+const PREFIX = "Services.prefs:";
+
 /**
  * Create a new preference object.
  *
  * @param {PrefBranch} branch the branch holding this preference
  * @param {String} name the base name of this preference
  * @param {String} fullName the fully-qualified name of this preference
  */
 function Preference(branch, name, fullName) {
@@ -96,17 +99,17 @@ Preference.prototype = {
   saveAndNotify: function () {
     let store = {
       type: this.type,
       defaultValue: this.defaultValue,
       hasUserValue: this.hasUserValue,
       userValue: this.userValue,
     };
 
-    localStorage.setItem(this.fullName, JSON.stringify(store));
+    localStorage.setItem(PREFIX + this.fullName, JSON.stringify(store));
     this.branch._notify(this.name);
   },
 
   /**
    * Change this preference's value without writing it back to local
    * storage.  This is used to handle changes to local storage that
    * were made externally.
    *
@@ -441,19 +444,22 @@ PrefBranch.prototype = {
       // FIXME - this is where we'll load devtools.js to install the
       // default prefs.
     }
 
     // Read the prefs from local storage and create the local
     // representations.
     for (let i = 0; i < localStorage.length; ++i) {
       let keyName = localStorage.key(i);
-      let {userValue, hasUserValue, defaultValue} =
-          JSON.parse(localStorage.getItem(keyName));
-      this._findOrCreatePref(keyName, userValue, hasUserValue, defaultValue);
+      if (keyName.startsWith(PREFIX)) {
+        let {userValue, hasUserValue, defaultValue} =
+            JSON.parse(localStorage.getItem(keyName));
+        this._findOrCreatePref(keyName.slice(PREFIX.length), userValue,
+                               hasUserValue, defaultValue);
+      }
     }
 
     this._onStorageChange = this._onStorageChange.bind(this);
     window.addEventListener("storage", this._onStorageChange);
   },
 };
 
 const Services = {
--- a/devtools/client/shared/shim/test/test_service_prefs.html
+++ b/devtools/client/shared/shim/test/test_service_prefs.html
@@ -10,33 +10,33 @@ https://bugzilla.mozilla.org/show_bug.cg
   <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 = {};
 
  // Add some starter prefs.
-localStorage.setItem("devtools.branch1.somebool", JSON.stringify({
+localStorage.setItem("Services.prefs:devtools.branch1.somebool", JSON.stringify({
   // bool
   type: 128,
   defaultValue: false,
   hasUserValue: false,
   userValue: false
 }));
 
-localStorage.setItem("devtools.branch1.somestring", JSON.stringify({
+localStorage.setItem("Services.prefs:devtools.branch1.somestring", JSON.stringify({
   // string
   type: 32,
   defaultValue: "dinosaurs",
   hasUserValue: true,
   userValue: "elephants"
 }));
 
-localStorage.setItem("devtools.branch2.someint", JSON.stringify({
+localStorage.setItem("Services.prefs:devtools.branch2.someint", JSON.stringify({
   // string
   type: 64,
   defaultValue: -16,
   hasUserValue: false,
   userValue: null
 }));
 
 </script>