Bug 1294190 - allow observation of a preference in shim prefs; r?gregtatum draft
authorTom Tromey <tom@tromey.com>
Wed, 10 Aug 2016 15:28:34 -0600
changeset 399325 321d806c40a1cc55abf085718b07094ecc35d6e3
parent 399324 6b018844478c9c186409053be1fabdcd48dc686b
child 399326 d4d17148a19aad9818caa8d7fe66a38357fdc62b
push id25800
push userbmo:ttromey@mozilla.com
push dateWed, 10 Aug 2016 21:38:19 +0000
reviewersgregtatum
bugs1294190
milestone51.0a1
Bug 1294190 - allow observation of a preference in shim prefs; r?gregtatum MozReview-Commit-ID: 1Rzs8F0yJtt
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
@@ -244,19 +244,16 @@ PrefBranch.prototype = {
   /** @see nsIPrefBranch.prefHasUserValue */
   prefHasUserValue: function (prefName) {
     let thePref = this._findPref(prefName);
     return thePref.hasUserValue;
   },
 
   /** @see nsIPrefBranch.addObserver */
   addObserver: function (domain, observer, holdWeak) {
-    if (domain !== "" && !domain.endsWith(".")) {
-      throw new Error("invalid domain to addObserver: " + domain);
-    }
     if (holdWeak) {
       throw new Error("shim prefs only supports strong observers");
     }
 
     if (!(domain in this._observers)) {
       this._observers[domain] = [];
     }
     this._observers[domain].push(observer);
@@ -320,17 +317,18 @@ PrefBranch.prototype = {
    * changed.  This will also notify the parent branch for further
    * reporting.
    *
    * @param {String} relativeName the name of the updated pref,
    *        relative to this branch
    */
   _notify: function (relativeName) {
     for (let domain in this._observers) {
-      if (relativeName.startsWith(domain)) {
+      if (relativeName === domain || domain === "" ||
+          (domain.endsWith(".") && relativeName.startsWith(domain))) {
         // Allow mutation while walking.
         let localList = this._observers[domain].slice();
         for (let observer of localList) {
           try {
             observer.observe(this, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
                              relativeName);
           } catch (e) {
             console.error(e);
--- a/devtools/client/shared/shim/test/test_service_prefs.html
+++ b/devtools/client/shared/shim/test/test_service_prefs.html
@@ -175,24 +175,17 @@ function do_tests() {
   let notifications = {};
   let clearNotificationList = () => { notifications = {}; }
   let observer = {
     observe: function (subject, topic, data) {
       notifications[data] = true;
     }
   };
 
-  try {
-    threw = false;
-    branch0.addObserver("devtools.branch1", null, null);
-  } catch (e) {
-    threw = true;
-  }
-  ok(threw, "invalid branch name to addObserver");
-
+  branch0.addObserver("devtools.branch1", null, null);
   branch0.addObserver("devtools.branch1.", observer, false);
   branch1.addObserver("", observer, false);
 
   Services.prefs.setCharPref("devtools.branch1.somestring", "elf owl");
   isDeeply(notifications, {
     "devtools.branch1.somestring": true,
     "somestring": true
   }, "notifications sent to two listeners");
@@ -203,16 +196,25 @@ function do_tests() {
 
   clearNotificationList();
   branch0.removeObserver("devtools.branch1.", observer);
   Services.prefs.setCharPref("devtools.branch1.somestring", "tapir");
   isDeeply(notifications, {
     "somestring": true
   }, "removeObserver worked");
 
+  clearNotificationList();
+  branch0.addObserver("devtools.branch1.somestring", observer, false);
+  Services.prefs.setCharPref("devtools.branch1.somestring", "northern shoveler");
+  isDeeply(notifications, {
+    "devtools.branch1.somestring": true,
+    "somestring": true
+  }, "notifications sent to two listeners");
+  branch0.removeObserver("devtools.branch1.somestring", observer);
+
   // Make sure we update if the pref change comes from somewhere else.
   clearNotificationList();
   pref("devtools.branch1.someotherstring", "lazuli bunting");
   isDeeply(notifications, {
     "someotherstring": true
   }, "pref worked");