2012-03-27 01:49:38 +04: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/. */
|
|
|
|
|
2012-05-25 21:37:45 +04:00
|
|
|
"use strict";
|
2012-03-27 01:49:38 +04:00
|
|
|
|
2012-06-05 03:12:24 +04:00
|
|
|
function debug(s) {
|
|
|
|
// dump("-*- SettingsManager: " + s + "\n");
|
|
|
|
}
|
2012-03-27 01:49:38 +04:00
|
|
|
|
2012-04-27 02:10:04 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2012-04-28 03:21:31 +04:00
|
|
|
Cu.import("resource://gre/modules/SettingsQueue.jsm");
|
|
|
|
Cu.import("resource://gre/modules/SettingsDB.jsm");
|
2012-03-27 01:49:38 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2012-08-27 18:13:02 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2012-06-05 03:12:24 +04:00
|
|
|
|
2012-03-27 01:49:38 +04:00
|
|
|
const nsIClassInfo = Ci.nsIClassInfo;
|
|
|
|
const SETTINGSLOCK_CONTRACTID = "@mozilla.org/settingsLock;1";
|
|
|
|
const SETTINGSLOCK_CID = Components.ID("{ef95ddd0-6308-11e1-b86c-0800200c9a66}");
|
|
|
|
const nsIDOMSettingsLock = Ci.nsIDOMSettingsLock;
|
|
|
|
|
|
|
|
function SettingsLock(aSettingsManager)
|
|
|
|
{
|
|
|
|
this._open = true;
|
|
|
|
this._requests = new Queue();
|
|
|
|
this._settingsManager = aSettingsManager;
|
|
|
|
this._transaction = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsLock.prototype = {
|
|
|
|
|
|
|
|
process: function process() {
|
|
|
|
let lock = this;
|
|
|
|
lock._open = false;
|
2012-04-28 03:21:31 +04:00
|
|
|
let store = lock._transaction.objectStore(SETTINGSSTORE_NAME);
|
2012-03-27 01:49:38 +04:00
|
|
|
|
|
|
|
while (!lock._requests.isEmpty()) {
|
|
|
|
let info = lock._requests.dequeue();
|
2012-06-11 03:44:50 +04:00
|
|
|
debug("info: " + info.intent);
|
2012-03-27 01:49:38 +04:00
|
|
|
let request = info.request;
|
|
|
|
switch (info.intent) {
|
|
|
|
case "clear":
|
|
|
|
let req = store.clear();
|
|
|
|
req.onsuccess = function() { this._open = true;
|
|
|
|
Services.DOMRequest.fireSuccess(request, 0);
|
|
|
|
this._open = false; }.bind(lock);
|
|
|
|
req.onerror = function() { Services.DOMRequest.fireError(request, 0) };
|
|
|
|
break;
|
|
|
|
case "set":
|
2012-03-27 23:04:16 +04:00
|
|
|
for (let key in info.settings) {
|
|
|
|
debug("key: " + key + ", val: " + JSON.stringify(info.settings[key]) + ", type: " + typeof(info.settings[key]));
|
2012-03-27 01:49:38 +04:00
|
|
|
|
2012-06-20 23:56:23 +04:00
|
|
|
let checkKeyRequest = store.get(key);
|
|
|
|
checkKeyRequest.onsuccess = function (event) {
|
|
|
|
if (!event.target.result) {
|
2012-07-26 23:15:47 +04:00
|
|
|
debug("MOZSETTINGS-SET-WARNING: " + key + " is not in the database. Please add it to build/settings.js\n");
|
2012-06-20 23:56:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:49:38 +04:00
|
|
|
if(typeof(info.settings[key]) != 'object') {
|
|
|
|
req = store.put({settingName: key, settingValue: info.settings[key]});
|
|
|
|
} else {
|
|
|
|
//Workaround for cloning issues
|
|
|
|
let obj = JSON.parse(JSON.stringify(info.settings[key]));
|
|
|
|
req = store.put({settingName: key, settingValue: obj});
|
|
|
|
}
|
2012-03-27 23:04:16 +04:00
|
|
|
|
|
|
|
req.onsuccess = function() {
|
|
|
|
lock._open = true;
|
|
|
|
Services.DOMRequest.fireSuccess(request, 0);
|
2012-06-05 03:12:24 +04:00
|
|
|
cpmm.sendAsyncMessage("Settings:Changed", { key: key, value: info.settings[key] });
|
2012-03-27 23:04:16 +04:00
|
|
|
lock._open = false;
|
|
|
|
};
|
|
|
|
|
2012-06-20 23:56:23 +04:00
|
|
|
req.onerror = function() {
|
|
|
|
Services.DOMRequest.fireError(request, 0)
|
|
|
|
};
|
2012-03-27 01:49:38 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "get":
|
2012-06-30 00:53:41 +04:00
|
|
|
req = (info.name === "*") ? store.mozGetAll()
|
|
|
|
: store.mozGetAll(info.name);
|
2012-06-06 16:19:33 +04:00
|
|
|
|
2012-03-27 01:49:38 +04:00
|
|
|
req.onsuccess = function(event) {
|
2012-05-25 21:37:45 +04:00
|
|
|
debug("Request for '" + info.name + "' successful. " +
|
|
|
|
"Record count: " + event.target.result.length);
|
2012-03-27 01:49:38 +04:00
|
|
|
debug("result: " + JSON.stringify(event.target.result));
|
2012-06-06 16:19:33 +04:00
|
|
|
|
2012-06-20 23:56:23 +04:00
|
|
|
if (event.target.result.length == 0) {
|
2012-07-26 23:15:47 +04:00
|
|
|
debug("MOZSETTINGS-GET-WARNING: " + info.name + " is not in the database. Please add it to build/settings.js\n");
|
2012-06-20 23:56:23 +04:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:19:33 +04:00
|
|
|
let results = {
|
|
|
|
__exposedProps__: {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var i in event.target.result) {
|
|
|
|
let result = event.target.result[i];
|
2012-08-15 07:54:33 +04:00
|
|
|
var name = result.settingName;
|
|
|
|
var value = result.settingValue;
|
|
|
|
results[name] = value;
|
|
|
|
results.__exposedProps__[name] = "r";
|
|
|
|
// If the value itself is an object, expose the properties.
|
2012-10-25 17:02:15 +04:00
|
|
|
if (typeof value == "object" && value != null) {
|
2012-08-15 07:54:33 +04:00
|
|
|
var exposed = {};
|
|
|
|
Object.keys(value).forEach(function(key) { exposed[key] = 'r'; });
|
|
|
|
results[name].__exposedProps__ = exposed;
|
|
|
|
}
|
2012-06-06 16:19:33 +04:00
|
|
|
}
|
|
|
|
|
2012-03-27 01:49:38 +04:00
|
|
|
this._open = true;
|
2012-06-06 16:19:33 +04:00
|
|
|
Services.DOMRequest.fireSuccess(request, results);
|
2012-03-27 01:49:38 +04:00
|
|
|
this._open = false;
|
|
|
|
}.bind(lock);
|
2012-06-06 16:19:33 +04:00
|
|
|
|
|
|
|
req.onerror = function() {
|
|
|
|
Services.DOMRequest.fireError(request, 0)
|
|
|
|
};
|
2012-03-27 01:49:38 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!lock._requests.isEmpty())
|
|
|
|
throw Components.results.NS_ERROR_ABORT;
|
|
|
|
lock._open = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
createTransactionAndProcess: function() {
|
2012-04-27 02:10:04 +04:00
|
|
|
if (this._settingsManager._settingsDB._db) {
|
2012-03-27 01:49:38 +04:00
|
|
|
var lock;
|
|
|
|
while (lock = this._settingsManager._locks.dequeue()) {
|
|
|
|
if (!lock._transaction) {
|
2012-04-28 03:21:31 +04:00
|
|
|
lock._transaction = lock._settingsManager._settingsDB._db.transaction(SETTINGSSTORE_NAME, "readwrite");
|
2012-03-27 01:49:38 +04:00
|
|
|
}
|
|
|
|
lock.process();
|
|
|
|
}
|
|
|
|
if (!this._requests.isEmpty())
|
|
|
|
this.process();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function get(aName) {
|
2012-05-23 05:18:56 +04:00
|
|
|
if (!this._open) {
|
|
|
|
dump("Settings lock not open!\n");
|
2012-03-27 01:49:38 +04:00
|
|
|
throw Components.results.NS_ERROR_ABORT;
|
2012-05-23 05:18:56 +04:00
|
|
|
}
|
2012-03-27 01:49:38 +04:00
|
|
|
|
2012-08-08 03:22:24 +04:00
|
|
|
if (this._settingsManager.hasPrivileges) {
|
2012-03-27 01:49:38 +04:00
|
|
|
let req = Services.DOMRequest.createRequest(this._settingsManager._window);
|
|
|
|
this._requests.enqueue({ request: req, intent:"get", name: aName });
|
|
|
|
this.createTransactionAndProcess();
|
|
|
|
return req;
|
|
|
|
} else {
|
|
|
|
debug("get not allowed");
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function set(aSettings) {
|
2012-05-23 05:18:56 +04:00
|
|
|
if (!this._open) {
|
|
|
|
dump("Settings lock not open!\n");
|
2012-03-27 01:49:38 +04:00
|
|
|
throw Components.results.NS_ERROR_ABORT;
|
2012-05-23 05:18:56 +04:00
|
|
|
}
|
2012-03-27 01:49:38 +04:00
|
|
|
|
2012-08-08 03:22:24 +04:00
|
|
|
if (this._settingsManager.hasPrivileges) {
|
2012-03-27 01:49:38 +04:00
|
|
|
let req = Services.DOMRequest.createRequest(this._settingsManager._window);
|
|
|
|
debug("send: " + JSON.stringify(aSettings));
|
|
|
|
this._requests.enqueue({request: req, intent: "set", settings: aSettings});
|
|
|
|
this.createTransactionAndProcess();
|
|
|
|
return req;
|
|
|
|
} else {
|
|
|
|
debug("set not allowed");
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function clear() {
|
2012-05-23 05:18:56 +04:00
|
|
|
if (!this._open) {
|
|
|
|
dump("Settings lock not open!\n");
|
2012-03-27 01:49:38 +04:00
|
|
|
throw Components.results.NS_ERROR_ABORT;
|
2012-05-23 05:18:56 +04:00
|
|
|
}
|
2012-03-27 01:49:38 +04:00
|
|
|
|
2012-08-08 03:22:24 +04:00
|
|
|
if (this._settingsManager.hasPrivileges) {
|
2012-03-27 01:49:38 +04:00
|
|
|
let req = Services.DOMRequest.createRequest(this._settingsManager._window);
|
|
|
|
this._requests.enqueue({ request: req, intent: "clear"});
|
|
|
|
this.createTransactionAndProcess();
|
|
|
|
return req;
|
|
|
|
} else {
|
|
|
|
debug("clear not allowed");
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
classID : SETTINGSLOCK_CID,
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMSettingsLock]),
|
|
|
|
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: SETTINGSLOCK_CID,
|
|
|
|
contractID: SETTINGSLOCK_CONTRACTID,
|
|
|
|
classDescription: "SettingsLock",
|
|
|
|
interfaces: [nsIDOMSettingsLock],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT})
|
|
|
|
};
|
|
|
|
|
|
|
|
const SETTINGSMANAGER_CONTRACTID = "@mozilla.org/settingsManager;1";
|
2012-09-19 21:21:30 +04:00
|
|
|
const SETTINGSMANAGER_CID = Components.ID("{c40b1c70-00fb-11e2-a21f-0800200c9a66}");
|
2012-03-27 01:49:38 +04:00
|
|
|
const nsIDOMSettingsManager = Ci.nsIDOMSettingsManager;
|
|
|
|
|
|
|
|
let myGlobal = this;
|
|
|
|
|
|
|
|
function SettingsManager()
|
|
|
|
{
|
|
|
|
this._locks = new Queue();
|
2012-08-29 22:52:48 +04:00
|
|
|
if (!("indexedDB" in myGlobal)) {
|
|
|
|
let idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"].getService(Ci.nsIIndexedDatabaseManager);
|
|
|
|
idbManager.initWindowless(myGlobal);
|
|
|
|
}
|
2012-03-27 01:49:38 +04:00
|
|
|
this._settingsDB = new SettingsDB();
|
2012-04-27 02:10:04 +04:00
|
|
|
this._settingsDB.init(myGlobal);
|
2012-03-27 01:49:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SettingsManager.prototype = {
|
|
|
|
_onsettingchange: null,
|
2012-06-11 03:44:50 +04:00
|
|
|
_callbacks: null,
|
2012-03-27 01:49:38 +04:00
|
|
|
|
|
|
|
nextTick: function nextTick(aCallback, thisObj) {
|
|
|
|
if (thisObj)
|
|
|
|
aCallback = aCallback.bind(thisObj);
|
|
|
|
|
|
|
|
Services.tm.currentThread.dispatch(aCallback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
|
|
|
set onsettingchange(aCallback) {
|
2012-09-27 01:10:32 +04:00
|
|
|
if (this.hasPrivileges) {
|
|
|
|
if (!this._onsettingchange) {
|
|
|
|
cpmm.sendAsyncMessage("Settings:RegisterForMessages");
|
|
|
|
}
|
2012-03-27 01:49:38 +04:00
|
|
|
this._onsettingchange = aCallback;
|
2012-09-27 01:10:32 +04:00
|
|
|
} else {
|
2012-03-27 01:49:38 +04:00
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
2012-09-27 01:10:32 +04:00
|
|
|
}
|
2012-03-27 01:49:38 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get onsettingchange() {
|
|
|
|
return this._onsettingchange;
|
|
|
|
},
|
|
|
|
|
2012-09-06 04:33:03 +04:00
|
|
|
createLock: function() {
|
2012-03-27 01:49:38 +04:00
|
|
|
debug("get lock!");
|
|
|
|
var lock = new SettingsLock(this);
|
|
|
|
this._locks.enqueue(lock);
|
|
|
|
this._settingsDB.ensureDB(
|
|
|
|
function() { lock.createTransactionAndProcess(); },
|
|
|
|
function() { dump("ensureDB error cb!\n"); },
|
|
|
|
myGlobal );
|
|
|
|
this.nextTick(function() { this._open = false; }, lock);
|
|
|
|
return lock;
|
|
|
|
},
|
|
|
|
|
2012-06-05 03:12:24 +04:00
|
|
|
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");
|
2012-06-11 03:44:50 +04:00
|
|
|
if (this._onsettingchange || this._callbacks) {
|
|
|
|
debug('data:' + msg.key + ':' + msg.value + '\n');
|
|
|
|
|
|
|
|
if (this._onsettingchange) {
|
|
|
|
let event = new this._window.MozSettingsEvent("settingchanged", {
|
|
|
|
settingName: msg.key,
|
|
|
|
settingValue: msg.value
|
|
|
|
});
|
|
|
|
this._onsettingchange.handleEvent(event);
|
|
|
|
}
|
|
|
|
if (this._callbacks && this._callbacks[msg.key]) {
|
|
|
|
debug("observe callback called! " + msg.key + " " + this._callbacks[msg.key].length);
|
2012-07-31 00:52:51 +04:00
|
|
|
this._callbacks[msg.key].forEach(function(cb) {
|
2012-08-15 07:54:33 +04:00
|
|
|
cb({settingName: msg.key, settingValue: msg.value,
|
|
|
|
__exposedProps__: {settingName: 'r', settingValue: 'r'}});
|
2012-07-31 00:52:51 +04:00
|
|
|
});
|
2012-06-11 03:44:50 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug("no observers stored!");
|
|
|
|
}
|
2012-06-05 03:12:24 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug("Wrong message: " + aMessage.name);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
addObserver: function addObserver(aName, aCallback) {
|
|
|
|
debug("addObserver " + aName);
|
2012-09-27 01:10:32 +04:00
|
|
|
if (!this._callbacks) {
|
|
|
|
cpmm.sendAsyncMessage("Settings:RegisterForMessages");
|
2012-06-11 03:44:50 +04:00
|
|
|
this._callbacks = {};
|
2012-09-27 01:10:32 +04:00
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
if (!this._callbacks[aName]) {
|
|
|
|
this._callbacks[aName] = [aCallback];
|
|
|
|
} else {
|
|
|
|
this._callbacks[aName].push(aCallback);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removeObserver: function removeObserver(aName, aCallback) {
|
|
|
|
debug("deleteObserver " + aName);
|
|
|
|
if (this._callbacks && this._callbacks[aName]) {
|
|
|
|
let index = this._callbacks[aName].indexOf(aCallback)
|
|
|
|
if (index != -1) {
|
|
|
|
this._callbacks[aName].splice(index, 1)
|
|
|
|
} else {
|
|
|
|
debug("Callback not found for: " + aName);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug("No observers stored for " + aName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-03-27 01:49:38 +04:00
|
|
|
init: function(aWindow) {
|
|
|
|
// Set navigator.mozSettings to null.
|
|
|
|
if (!Services.prefs.getBoolPref("dom.mozSettings.enabled"))
|
|
|
|
return null;
|
|
|
|
|
2012-06-05 03:12:24 +04:00
|
|
|
cpmm.addMessageListener("Settings:Change:Return:OK", this);
|
2012-03-27 01:49:38 +04:00
|
|
|
this._window = aWindow;
|
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
|
|
|
let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
this.innerWindowID = util.currentInnerWindowID;
|
|
|
|
|
2012-08-08 03:22:24 +04:00
|
|
|
let perm = Services.perms.testExactPermissionFromPrincipal(aWindow.document.nodePrincipal, "settings");
|
|
|
|
this.hasPrivileges = perm == Ci.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
debug("has privileges :" + this.hasPrivileges);
|
2012-03-27 01:49:38 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
debug("Topic: " + aTopic);
|
|
|
|
if (aTopic == "inner-window-destroyed") {
|
|
|
|
let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this.innerWindowID) {
|
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
2012-06-05 03:12:24 +04:00
|
|
|
cpmm.removeMessageListener("Settings:Change:Return:OK", this);
|
2012-03-27 01:49:38 +04:00
|
|
|
this._requests = null;
|
|
|
|
this._window = null;
|
|
|
|
this._innerWindowID = null;
|
|
|
|
this._onsettingchange = null;
|
|
|
|
this._settingsDB.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
classID : SETTINGSMANAGER_CID,
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMSettingsManager, Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: SETTINGSMANAGER_CID,
|
|
|
|
contractID: SETTINGSMANAGER_CONTRACTID,
|
|
|
|
classDescription: "SettingsManager",
|
|
|
|
interfaces: [nsIDOMSettingsManager],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT})
|
|
|
|
}
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SettingsManager, SettingsLock])
|