Bug 753862 - Settings API: electrolysis support. r=fabrice

This commit is contained in:
Christoph Kerschbaumer 2012-06-04 16:12:24 -07:00
parent cec2715e6c
commit b06853949d
4 changed files with 92 additions and 25 deletions

View File

@ -12,6 +12,7 @@ const Cr = Components.results;
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/ContactService.jsm');
Cu.import('resource://gre/modules/SettingsChangeNotifier.jsm');
Cu.import('resource://gre/modules/Webapps.jsm');
XPCOMUtils.defineLazyServiceGetter(Services, 'env',

View File

@ -25,6 +25,7 @@ EXTRA_COMPONENTS = \
EXTRA_JS_MODULES = \
SettingsQueue.jsm \
SettingsDB.jsm \
SettingsChangeNotifier.jsm \
$(NULL)
ifdef ENABLE_TESTS

View File

@ -0,0 +1,56 @@
/* 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");
}
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");
XPCOMUtils.defineLazyGetter(this, "ppmm", function() {
return Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIFrameMessageManager);
});
let SettingsChangeNotifier = {
init: function() {
debug("init");
ppmm.addMessageListener("Settings:Changed", this);
Services.obs.addObserver(this, "xpcom-shutdown", false);
},
observe: function(aSubject, aTopic, aData) {
debug("observe");
ppmm.removeMessageListener("Settings:Changed", this);
Services.obs.removeObserver(this, "xpcom-shutdown");
ppmm = null;
},
receiveMessage: function(aMessage) {
debug("receiveMessage");
let msg = aMessage.json;
switch (aMessage.name) {
case "Settings:Changed":
ppmm.sendAsyncMessage("Settings:Change:Return:OK", { key: msg.key, value: msg.value });
Services.obs.notifyObservers(this, "mozsettings-changed", JSON.stringify({
key: msg.key,
value: msg.value
}));
break;
default:
debug("Wrong message: " + aMessage.name);
}
}
}
SettingsChangeNotifier.init();

View File

@ -4,12 +4,9 @@
"use strict";
/* static functions */
let DEBUG = false;
if (DEBUG)
debug = function (s) { dump("-*- SettingsManager: " + s + "\n"); };
else
debug = function (s) {};
function debug(s) {
// dump("-*- SettingsManager: " + s + "\n");
}
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -20,6 +17,10 @@ Cu.import("resource://gre/modules/SettingsDB.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
return Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsIFrameMessageManager);
});
const nsIClassInfo = Ci.nsIClassInfo;
const SETTINGSLOCK_CONTRACTID = "@mozilla.org/settingsLock;1";
const SETTINGSLOCK_CID = Components.ID("{ef95ddd0-6308-11e1-b86c-0800200c9a66}");
@ -67,10 +68,7 @@ SettingsLock.prototype = {
req.onsuccess = function() {
lock._open = true;
Services.DOMRequest.fireSuccess(request, 0);
Services.obs.notifyObservers(lock, "mozsettings-changed", JSON.stringify({
key: key,
value: info.settings[key]
}));
cpmm.sendAsyncMessage("Settings:Changed", { key: key, value: info.settings[key] });
lock._open = false;
};
@ -227,14 +225,37 @@ SettingsManager.prototype = {
return lock;
},
receiveMessage: function(aMessage) {
debug("Settings::receiveMessage: " + aMessage.name);
let msg = aMessage.json;
switch (aMessage.name) {
case "Settings:Change:Return:OK":
debug("Settings:Change:Return:OK");
if (!this._onsettingchange)
return;
debug('key:' + msg.key + ', value:' + msg.value + '\n');
let event = new this._window.MozSettingsEvent("settingchanged", {
settingName: msg.key,
settingValue: msg.value
});
this._onsettingchange.handleEvent(event);
break;
default:
debug("Wrong message: " + aMessage.name);
}
},
init: function(aWindow) {
// Set navigator.mozSettings to null.
if (!Services.prefs.getBoolPref("dom.mozSettings.enabled"))
return null;
cpmm.addMessageListener("Settings:Change:Return:OK", this);
this._window = aWindow;
Services.obs.addObserver(this, "inner-window-destroyed", false);
Services.obs.addObserver(this, "mozsettings-changed", false);
let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
this.innerWindowID = util.currentInnerWindowID;
@ -253,26 +274,14 @@ SettingsManager.prototype = {
let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
if (wId == this.innerWindowID) {
Services.obs.removeObserver(this, "inner-window-destroyed");
Services.obs.removeObserver(this, "mozsettings-changed");
cpmm.removeMessageListener("Settings:Change:Return:OK", this);
this._requests = null;
this._window = null;
this._innerWindowID = null;
this._onsettingchange = null;
this._settingsDB.close();
cpmm = null;
}
} else if (aTopic == "mozsettings-changed") {
if (!this._onsettingchange)
return;
let data = JSON.parse(aData);
debug('data:' + data.key + ':' + data.value + '\n');
let event = new this._window.MozSettingsEvent("settingchanged", {
settingName: data.key,
settingValue: data.value
});
this._onsettingchange.handleEvent(event);
}
},