2012-06-04 16:12:24 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict"
|
|
|
|
|
|
|
|
function debug(s) {
|
|
|
|
// dump("-*- SettingsChangeNotifier: " + s + "\n");
|
|
|
|
}
|
|
|
|
|
2012-09-18 07:29:26 -07:00
|
|
|
const Cu = Components.utils;
|
2012-06-04 16:12:24 -07:00
|
|
|
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");
|
|
|
|
|
2012-09-18 07:29:26 -07:00
|
|
|
const kXpcomShutdownObserverTopic = "xpcom-shutdown";
|
|
|
|
const kMozSettingsChangedObserverTopic = "mozsettings-changed";
|
|
|
|
const kFromSettingsChangeNotifier = "fromSettingsChangeNotifier";
|
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageBroadcaster");
|
2012-06-04 16:12:24 -07:00
|
|
|
|
|
|
|
let SettingsChangeNotifier = {
|
|
|
|
init: function() {
|
|
|
|
debug("init");
|
2012-09-26 14:10:32 -07:00
|
|
|
this.children = [];
|
|
|
|
this._messages = ["Settings:Changed", "Settings:RegisterForMessages", "Settings:UnregisterForMessages"];
|
|
|
|
this._messages.forEach((function(msgName) {
|
|
|
|
ppmm.addMessageListener(msgName, this);
|
|
|
|
}).bind(this));
|
|
|
|
|
2012-09-18 07:29:26 -07:00
|
|
|
Services.obs.addObserver(this, kXpcomShutdownObserverTopic, false);
|
|
|
|
Services.obs.addObserver(this, kMozSettingsChangedObserverTopic, false);
|
2012-06-04 16:12:24 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
debug("observe");
|
2012-09-18 07:29:26 -07:00
|
|
|
switch (aTopic) {
|
|
|
|
case kXpcomShutdownObserverTopic:
|
2012-09-26 14:10:32 -07:00
|
|
|
this._messages.forEach((function(msgName) {
|
|
|
|
ppmm.removeMessageListener(msgName, this);
|
|
|
|
}).bind(this));
|
2012-09-18 07:29:26 -07:00
|
|
|
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;
|
2012-09-26 14:10:32 -07:00
|
|
|
this.broadcastMessage("Settings:Change:Return:OK",
|
2012-09-18 07:29:26 -07:00
|
|
|
{ key: setting.key, value: setting.value });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
debug("Wrong observer topic: " + aTopic);
|
|
|
|
break;
|
|
|
|
}
|
2012-06-04 16:12:24 -07:00
|
|
|
},
|
|
|
|
|
2012-09-26 14:10:32 -07:00
|
|
|
broadcastMessage: function broadcastMessage(aMsgName, aContent) {
|
|
|
|
let i;
|
|
|
|
for (i = this.children.length - 1; i >= 0; i -= 1) {
|
|
|
|
let msgMgr = this.children[i];
|
|
|
|
try {
|
|
|
|
msgMgr.sendAsyncMessage(aMsgName, aContent);
|
|
|
|
} catch (e) {
|
|
|
|
let index;
|
|
|
|
if ((index = this.children.indexOf(msgMgr)) != -1) {
|
|
|
|
this.children.splice(index, 1);
|
|
|
|
dump("Remove dead MessageManager!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-06-04 16:12:24 -07:00
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
debug("receiveMessage");
|
|
|
|
let msg = aMessage.json;
|
2012-09-26 14:10:32 -07:00
|
|
|
let mm = aMessage.target;
|
2012-06-04 16:12:24 -07:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Settings:Changed":
|
2012-09-26 14:10:32 -07:00
|
|
|
this.broadcastMessage("Settings:Change:Return:OK",
|
2012-09-18 07:29:26 -07:00
|
|
|
{ key: msg.key, value: msg.value });
|
|
|
|
Services.obs.notifyObservers(this, kMozSettingsChangedObserverTopic,
|
|
|
|
JSON.stringify({
|
|
|
|
key: msg.key,
|
|
|
|
value: msg.value,
|
|
|
|
message: kFromSettingsChangeNotifier
|
|
|
|
}));
|
2012-06-04 16:12:24 -07:00
|
|
|
break;
|
2012-09-26 14:10:32 -07:00
|
|
|
case "Settings:RegisterForMessages":
|
|
|
|
debug("Register!");
|
|
|
|
if (this.children.indexOf(mm) == -1) {
|
|
|
|
this.children.push(mm);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Settings:UnregisterForMessages":
|
|
|
|
debug("Unregister");
|
|
|
|
let index;
|
|
|
|
if ((index = this.children.indexOf(mm)) != -1) {
|
|
|
|
this.children.splice(index, 1);
|
|
|
|
}
|
|
|
|
break;
|
2012-09-18 07:29:26 -07:00
|
|
|
default:
|
2012-06-04 16:12:24 -07:00
|
|
|
debug("Wrong message: " + aMessage.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsChangeNotifier.init();
|