dom/settings/SettingsChangeNotifier.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict"
michael@0 6
michael@0 7 const DEBUG = false;
michael@0 8 function debug(s) {
michael@0 9 if (DEBUG) dump("-*- SettingsChangeNotifier: " + s + "\n");
michael@0 10 }
michael@0 11
michael@0 12 const Cu = Components.utils;
michael@0 13 const Cc = Components.classes;
michael@0 14 const Ci = Components.interfaces;
michael@0 15
michael@0 16 this.EXPORTED_SYMBOLS = ["SettingsChangeNotifier"];
michael@0 17
michael@0 18 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 19 Cu.import("resource://gre/modules/Services.jsm");
michael@0 20
michael@0 21 const kXpcomShutdownObserverTopic = "xpcom-shutdown";
michael@0 22 const kMozSettingsChangedObserverTopic = "mozsettings-changed";
michael@0 23 const kFromSettingsChangeNotifier = "fromSettingsChangeNotifier";
michael@0 24
michael@0 25 XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
michael@0 26 "@mozilla.org/parentprocessmessagemanager;1",
michael@0 27 "nsIMessageBroadcaster");
michael@0 28
michael@0 29 this.SettingsChangeNotifier = {
michael@0 30 init: function() {
michael@0 31 if (DEBUG) debug("init");
michael@0 32 this.children = [];
michael@0 33 this._messages = ["Settings:Changed", "Settings:RegisterForMessages", "child-process-shutdown"];
michael@0 34 this._messages.forEach((function(msgName) {
michael@0 35 ppmm.addMessageListener(msgName, this);
michael@0 36 }).bind(this));
michael@0 37
michael@0 38 Services.obs.addObserver(this, kXpcomShutdownObserverTopic, false);
michael@0 39 Services.obs.addObserver(this, kMozSettingsChangedObserverTopic, false);
michael@0 40 },
michael@0 41
michael@0 42 observe: function(aSubject, aTopic, aData) {
michael@0 43 if (DEBUG) debug("observe");
michael@0 44 switch (aTopic) {
michael@0 45 case kXpcomShutdownObserverTopic:
michael@0 46 this._messages.forEach((function(msgName) {
michael@0 47 ppmm.removeMessageListener(msgName, this);
michael@0 48 }).bind(this));
michael@0 49 Services.obs.removeObserver(this, kXpcomShutdownObserverTopic);
michael@0 50 Services.obs.removeObserver(this, kMozSettingsChangedObserverTopic);
michael@0 51 ppmm = null;
michael@0 52 break;
michael@0 53 case kMozSettingsChangedObserverTopic:
michael@0 54 {
michael@0 55 let setting = JSON.parse(aData);
michael@0 56 // To avoid redundantly broadcasting settings-changed events that are
michael@0 57 // just requested from content processes themselves, skip the observer
michael@0 58 // messages that are notified from the internal SettingsChangeNotifier.
michael@0 59 if (setting.message && setting.message === kFromSettingsChangeNotifier)
michael@0 60 return;
michael@0 61 this.broadcastMessage("Settings:Change:Return:OK",
michael@0 62 { key: setting.key, value: setting.value });
michael@0 63 break;
michael@0 64 }
michael@0 65 default:
michael@0 66 if (DEBUG) debug("Wrong observer topic: " + aTopic);
michael@0 67 break;
michael@0 68 }
michael@0 69 },
michael@0 70
michael@0 71 broadcastMessage: function broadcastMessage(aMsgName, aContent) {
michael@0 72 if (DEBUG) debug("Broadast");
michael@0 73 this.children.forEach(function(msgMgr) {
michael@0 74 msgMgr.sendAsyncMessage(aMsgName, aContent);
michael@0 75 });
michael@0 76 },
michael@0 77
michael@0 78 receiveMessage: function(aMessage) {
michael@0 79 if (DEBUG) debug("receiveMessage");
michael@0 80 let msg = aMessage.data;
michael@0 81 let mm = aMessage.target;
michael@0 82 switch (aMessage.name) {
michael@0 83 case "Settings:Changed":
michael@0 84 if (!aMessage.target.assertPermission("settings-write")) {
michael@0 85 Cu.reportError("Settings message " + msg.name +
michael@0 86 " from a content process with no 'settings-write' privileges.");
michael@0 87 return null;
michael@0 88 }
michael@0 89 this.broadcastMessage("Settings:Change:Return:OK",
michael@0 90 { key: msg.key, value: msg.value });
michael@0 91 Services.obs.notifyObservers(this, kMozSettingsChangedObserverTopic,
michael@0 92 JSON.stringify({
michael@0 93 key: msg.key,
michael@0 94 value: msg.value,
michael@0 95 message: kFromSettingsChangeNotifier
michael@0 96 }));
michael@0 97 break;
michael@0 98 case "Settings:RegisterForMessages":
michael@0 99 if (!aMessage.target.assertPermission("settings-read")) {
michael@0 100 Cu.reportError("Settings message " + msg.name +
michael@0 101 " from a content process with no 'settings-read' privileges.");
michael@0 102 return null;
michael@0 103 }
michael@0 104 if (DEBUG) debug("Register!");
michael@0 105 if (this.children.indexOf(mm) == -1) {
michael@0 106 this.children.push(mm);
michael@0 107 }
michael@0 108 break;
michael@0 109 case "child-process-shutdown":
michael@0 110 if (DEBUG) debug("Unregister");
michael@0 111 let index;
michael@0 112 if ((index = this.children.indexOf(mm)) != -1) {
michael@0 113 if (DEBUG) debug("Unregister index: " + index);
michael@0 114 this.children.splice(index, 1);
michael@0 115 }
michael@0 116 break;
michael@0 117 default:
michael@0 118 if (DEBUG) debug("Wrong message: " + aMessage.name);
michael@0 119 }
michael@0 120 }
michael@0 121 }
michael@0 122
michael@0 123 SettingsChangeNotifier.init();

mercurial