Bug 788076 - Settings API: Need to notify content processes about the settings changes when calling SettingsService.set() from chrome process. r=gwagner
authorGene Lian <clian@mozilla.com>
Tue, 18 Sep 2012 22:29:26 +0800
changeset 111048 a04dc1a6fe29474adf15829b98b15afdaf1f36fe
parent 111047 8aaac684782a0a0d70aeec06001a9029705da8a8
child 111049 64c78f239c471565e60c6e29502e15e6c3eda382
push id2248
push userakeybl@mozilla.com
push dateMon, 08 Oct 2012 19:23:44 +0000
treeherdermozilla-aurora@118a3b748323 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgwagner
bugs788076
milestone18.0a1
Bug 788076 - Settings API: Need to notify content processes about the settings changes when calling SettingsService.set() from chrome process. r=gwagner
dom/settings/SettingsChangeNotifier.jsm
--- a/dom/settings/SettingsChangeNotifier.jsm
+++ b/dom/settings/SettingsChangeNotifier.jsm
@@ -3,54 +3,81 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict"
 
 function debug(s) {
 //  dump("-*- SettingsChangeNotifier: " + s + "\n");
 }
 
-const Cu = Components.utils; 
+const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 let EXPORTED_SYMBOLS = ["SettingsChangeNotifier"];
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
+const kXpcomShutdownObserverTopic      = "xpcom-shutdown";
+const kMozSettingsChangedObserverTopic = "mozsettings-changed";
+const kFromSettingsChangeNotifier      = "fromSettingsChangeNotifier";
+
 XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
                                    "@mozilla.org/parentprocessmessagemanager;1",
                                    "nsIMessageBroadcaster");
 
-
 let SettingsChangeNotifier = {
   init: function() {
     debug("init");
     ppmm.addMessageListener("Settings:Changed", this);
-    Services.obs.addObserver(this, "xpcom-shutdown", false);
+    Services.obs.addObserver(this, kXpcomShutdownObserverTopic, false);
+    Services.obs.addObserver(this, kMozSettingsChangedObserverTopic, false);
   },
 
   observe: function(aSubject, aTopic, aData) {
     debug("observe");
-    ppmm.removeMessageListener("Settings:Changed", this);
-    Services.obs.removeObserver(this, "xpcom-shutdown");
-    ppmm = null;
+    switch (aTopic) {
+      case kXpcomShutdownObserverTopic:
+        ppmm.removeMessageListener("Settings:Changed", this);
+        Services.obs.removeObserver(this, kXpcomShutdownObserverTopic);
+        Services.obs.removeObserver(this, kMozSettingsChangedObserverTopic);
+        ppmm = null;
+        break;
+      case kMozSettingsChangedObserverTopic:
+      {
+        let setting = JSON.parse(aData);
+        // To avoid redundantly broadcasting settings-changed events that are
+        // just requested from content processes themselves, skip the observer
+        // messages that are notified from the internal SettingsChangeNotifier.
+        if (setting.message && setting.message === kFromSettingsChangeNotifier)
+          return;
+        ppmm.broadcastAsyncMessage("Settings:Change:Return:OK",
+          { key: setting.key, value: setting.value });
+        break;
+      }
+      default:
+        debug("Wrong observer topic: " + aTopic);
+        break;
+    }
   },
 
   receiveMessage: function(aMessage) {
     debug("receiveMessage");
     let msg = aMessage.json;
     switch (aMessage.name) {
       case "Settings:Changed":
-        ppmm.broadcastAsyncMessage("Settings:Change:Return:OK", { key: msg.key, value: msg.value });
-        Services.obs.notifyObservers(this, "mozsettings-changed", JSON.stringify({
-          key: msg.key,
-          value: msg.value
-        }));
+        ppmm.broadcastAsyncMessage("Settings:Change:Return:OK",
+          { key: msg.key, value: msg.value });
+        Services.obs.notifyObservers(this, kMozSettingsChangedObserverTopic,
+          JSON.stringify({
+            key: msg.key,
+            value: msg.value,
+            message: kFromSettingsChangeNotifier
+          }));
         break;
-      default: 
+      default:
         debug("Wrong message: " + aMessage.name);
     }
   }
 }
 
 SettingsChangeNotifier.init();