2012-04-28 03:21:31 +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/. */
|
|
|
|
|
|
|
|
"use strict"
|
|
|
|
|
|
|
|
/* static functions */
|
|
|
|
let DEBUG = 0;
|
2012-10-31 20:13:28 +04:00
|
|
|
let debug;
|
2012-04-28 03:21:31 +04:00
|
|
|
if (DEBUG)
|
|
|
|
debug = function (s) { dump("-*- SettingsService: " + s + "\n"); }
|
|
|
|
else
|
|
|
|
debug = function (s) {}
|
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/SettingsQueue.jsm");
|
|
|
|
Cu.import("resource://gre/modules/SettingsDB.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
const nsIClassInfo = Ci.nsIClassInfo;
|
|
|
|
|
|
|
|
const SETTINGSSERVICELOCK_CONTRACTID = "@mozilla.org/settingsServiceLock;1";
|
2012-08-31 11:37:43 +04:00
|
|
|
const SETTINGSSERVICELOCK_CID = Components.ID("{d7a395a0-e292-11e1-834e-1761d57f5f99}");
|
2012-04-28 03:21:31 +04:00
|
|
|
const nsISettingsServiceLock = Ci.nsISettingsServiceLock;
|
|
|
|
|
|
|
|
function SettingsServiceLock(aSettingsService)
|
|
|
|
{
|
2013-01-08 15:04:25 +04:00
|
|
|
if (DEBUG) debug("settingsServiceLock constr!");
|
2012-04-28 03:21:31 +04:00
|
|
|
this._open = true;
|
2013-01-08 15:04:25 +04:00
|
|
|
this._busy = false;
|
2012-04-28 03:21:31 +04:00
|
|
|
this._requests = new Queue();
|
|
|
|
this._settingsService = aSettingsService;
|
|
|
|
this._transaction = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsServiceLock.prototype = {
|
|
|
|
|
|
|
|
process: function process() {
|
|
|
|
debug("process!");
|
|
|
|
let lock = this;
|
|
|
|
lock._open = false;
|
|
|
|
let store = lock._transaction.objectStore(SETTINGSSTORE_NAME);
|
|
|
|
|
|
|
|
while (!lock._requests.isEmpty()) {
|
2013-01-08 15:04:25 +04:00
|
|
|
if (lock._isBusy) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-28 03:21:31 +04:00
|
|
|
let info = lock._requests.dequeue();
|
2013-01-08 15:04:25 +04:00
|
|
|
if (DEBUG) debug("info:" + info.intent);
|
2012-04-28 03:21:31 +04:00
|
|
|
let callback = info.callback;
|
|
|
|
let name = info.name;
|
|
|
|
switch (info.intent) {
|
|
|
|
case "set":
|
|
|
|
let value = info.value;
|
2012-08-31 11:37:43 +04:00
|
|
|
let message = info.message;
|
2013-01-08 15:04:25 +04:00
|
|
|
if(DEBUG && typeof(value) == 'object') {
|
2012-04-28 03:21:31 +04:00
|
|
|
debug("object name:" + name + ", val: " + JSON.stringify(value));
|
2013-01-08 15:04:25 +04:00
|
|
|
}
|
|
|
|
lock._isBusy = true;
|
2013-01-07 18:03:26 +04:00
|
|
|
let checkKeyRequest = store.get(name);
|
2012-04-28 03:21:31 +04:00
|
|
|
|
2013-01-07 18:03:26 +04:00
|
|
|
checkKeyRequest.onsuccess = function (event) {
|
|
|
|
let defaultValue;
|
|
|
|
if (event.target.result) {
|
|
|
|
defaultValue = event.target.result.defaultValue;
|
|
|
|
} else {
|
|
|
|
defaultValue = null;
|
2013-01-08 15:04:25 +04:00
|
|
|
if (DEBUG) debug("MOZSETTINGS-SET-WARNING: " + name + " is not in the database.\n");
|
2013-01-07 18:03:26 +04:00
|
|
|
}
|
2013-01-08 15:04:25 +04:00
|
|
|
let setReq = store.put({ settingName: name, defaultValue: defaultValue, userValue: value });
|
2013-01-07 18:03:26 +04:00
|
|
|
|
2013-01-08 15:04:25 +04:00
|
|
|
setReq.onsuccess = function() {
|
|
|
|
lock._isBusy = false;
|
2013-01-07 18:03:26 +04:00
|
|
|
lock._open = true;
|
|
|
|
if (callback)
|
|
|
|
callback.handle(name, value);
|
|
|
|
Services.obs.notifyObservers(lock, "mozsettings-changed", JSON.stringify({
|
|
|
|
key: name,
|
|
|
|
value: value,
|
|
|
|
message: message
|
|
|
|
}));
|
|
|
|
lock._open = false;
|
2013-01-08 15:04:25 +04:00
|
|
|
lock.process();
|
2013-01-07 18:03:26 +04:00
|
|
|
};
|
|
|
|
|
2013-01-08 15:04:25 +04:00
|
|
|
setReq.onerror = function(event) {
|
|
|
|
lock._isBusy = false;
|
|
|
|
callback ? callback.handleError(event.target.errorMessage) : null;
|
|
|
|
lock.process();
|
|
|
|
};
|
2013-01-07 18:03:26 +04:00
|
|
|
}
|
2012-04-28 03:21:31 +04:00
|
|
|
|
2013-01-08 15:04:25 +04:00
|
|
|
checkKeyRequest.onerror = function(event) {
|
|
|
|
lock._isBusy = false;
|
|
|
|
callback ? callback.handleError(event.target.errorMessage) : null;
|
|
|
|
lock.process();
|
|
|
|
};
|
2012-04-28 03:21:31 +04:00
|
|
|
break;
|
|
|
|
case "get":
|
2013-01-08 15:04:25 +04:00
|
|
|
let getReq = store.mozGetAll(name);
|
|
|
|
getReq.onsuccess = function(event) {
|
|
|
|
if (DEBUG) {
|
|
|
|
debug("Request successful. Record count:" + event.target.result.length);
|
|
|
|
debug("result: " + JSON.stringify(event.target.result));
|
|
|
|
}
|
2012-04-28 03:21:31 +04:00
|
|
|
this._open = true;
|
|
|
|
if (callback) {
|
|
|
|
if (event.target.result[0]) {
|
|
|
|
if (event.target.result.length > 1) {
|
2013-01-08 15:04:25 +04:00
|
|
|
if (DEBUG) debug("Warning: overloaded setting:" + name);
|
2012-04-28 03:21:31 +04:00
|
|
|
}
|
2013-01-07 18:03:26 +04:00
|
|
|
let result = event.target.result[0];
|
|
|
|
let value = result.userValue !== undefined
|
|
|
|
? result.userValue
|
|
|
|
: result.defaultValue;
|
|
|
|
callback.handle(name, value);
|
2013-01-08 15:04:25 +04:00
|
|
|
} else {
|
2012-04-28 03:21:31 +04:00
|
|
|
callback.handle(name, null);
|
2013-01-08 15:04:25 +04:00
|
|
|
}
|
2012-04-28 03:21:31 +04:00
|
|
|
} else {
|
2013-01-08 15:04:25 +04:00
|
|
|
if (DEBUG) debug("no callback defined!");
|
2012-04-28 03:21:31 +04:00
|
|
|
}
|
|
|
|
this._open = false;
|
|
|
|
}.bind(lock);
|
2013-01-08 15:04:25 +04:00
|
|
|
getReq.onerror = function error(event) { callback ? callback.handleError(event.target.errorMessage) : null; };
|
2012-04-28 03:21:31 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lock._open = true;
|
|
|
|
},
|
|
|
|
|
2013-01-08 15:04:25 +04:00
|
|
|
createTransactionAndProcess: function() {
|
2012-04-28 03:21:31 +04:00
|
|
|
if (this._settingsService._settingsDB._db) {
|
2013-01-08 15:04:25 +04:00
|
|
|
let lock;
|
2012-04-28 03:21:31 +04:00
|
|
|
while (lock = this._settingsService._locks.dequeue()) {
|
|
|
|
if (!lock._transaction) {
|
|
|
|
lock._transaction = lock._settingsService._settingsDB._db.transaction(SETTINGSSTORE_NAME, "readwrite");
|
|
|
|
}
|
2013-01-08 15:04:25 +04:00
|
|
|
if (!lock._isBusy) {
|
|
|
|
lock.process();
|
|
|
|
} else {
|
|
|
|
this._settingsService._locks.enqueue(lock);
|
|
|
|
return;
|
|
|
|
}
|
2012-04-28 03:21:31 +04:00
|
|
|
}
|
2013-01-08 15:04:25 +04:00
|
|
|
if (!this._requests.isEmpty() && !this._isBusy) {
|
2012-04-28 03:21:31 +04:00
|
|
|
this.process();
|
2013-01-08 15:04:25 +04:00
|
|
|
}
|
2012-04-28 03:21:31 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function get(aName, aCallback) {
|
2013-01-08 15:04:25 +04:00
|
|
|
if (DEBUG) debug("get: " + aName + ", " + aCallback);
|
2012-04-28 03:21:31 +04:00
|
|
|
this._requests.enqueue({ callback: aCallback, intent:"get", name: aName });
|
|
|
|
this.createTransactionAndProcess();
|
|
|
|
},
|
|
|
|
|
2012-08-31 11:37:43 +04:00
|
|
|
set: function set(aName, aValue, aCallback, aMessage) {
|
2012-04-28 03:21:31 +04:00
|
|
|
debug("set: " + aName + ": " + JSON.stringify(aValue));
|
2012-08-31 11:37:43 +04:00
|
|
|
if (aMessage === undefined)
|
|
|
|
aMessage = null;
|
|
|
|
this._requests.enqueue({ callback: aCallback,
|
|
|
|
intent: "set",
|
|
|
|
name: aName,
|
2013-08-11 19:41:59 +04:00
|
|
|
value: this._settingsService._settingsDB.prepareValue(aValue),
|
2012-08-31 11:37:43 +04:00
|
|
|
message: aMessage });
|
2012-04-28 03:21:31 +04:00
|
|
|
this.createTransactionAndProcess();
|
|
|
|
},
|
|
|
|
|
|
|
|
classID : SETTINGSSERVICELOCK_CID,
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsISettingsServiceLock]),
|
|
|
|
|
2012-08-31 11:37:43 +04:00
|
|
|
classInfo : XPCOMUtils.generateCI({ classID: SETTINGSSERVICELOCK_CID,
|
|
|
|
contractID: SETTINGSSERVICELOCK_CONTRACTID,
|
|
|
|
classDescription: "SettingsServiceLock",
|
|
|
|
interfaces: [nsISettingsServiceLock],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT })
|
2012-04-28 03:21:31 +04:00
|
|
|
};
|
|
|
|
|
2012-09-06 04:33:03 +04:00
|
|
|
const SETTINGSSERVICE_CID = Components.ID("{f656f0c0-f776-11e1-a21f-0800200c9a66}");
|
2012-04-28 03:21:31 +04:00
|
|
|
|
|
|
|
let myGlobal = this;
|
|
|
|
|
|
|
|
function SettingsService()
|
|
|
|
{
|
|
|
|
debug("settingsService Constructor");
|
|
|
|
this._locks = new Queue();
|
2012-08-30 00:55:36 +04:00
|
|
|
if (!("indexedDB" in myGlobal)) {
|
|
|
|
let idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"].getService(Ci.nsIIndexedDatabaseManager);
|
|
|
|
idbManager.initWindowless(myGlobal);
|
|
|
|
}
|
2012-04-28 03:21:31 +04:00
|
|
|
this._settingsDB = new SettingsDB();
|
|
|
|
this._settingsDB.init(myGlobal);
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsService.prototype = {
|
|
|
|
|
|
|
|
nextTick: function nextTick(aCallback, thisObj) {
|
|
|
|
if (thisObj)
|
|
|
|
aCallback = aCallback.bind(thisObj);
|
|
|
|
|
|
|
|
Services.tm.currentThread.dispatch(aCallback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
2012-09-06 04:33:03 +04:00
|
|
|
createLock: function createLock() {
|
2012-04-28 03:21:31 +04:00
|
|
|
var lock = new SettingsServiceLock(this);
|
|
|
|
this._locks.enqueue(lock);
|
|
|
|
this._settingsDB.ensureDB(
|
|
|
|
function() { lock.createTransactionAndProcess(); },
|
2013-01-08 15:04:25 +04:00
|
|
|
function() { dump("SettingsService failed to open DB!\n"); },
|
2012-04-28 03:21:31 +04:00
|
|
|
myGlobal );
|
|
|
|
this.nextTick(function() { this._open = false; }, lock);
|
|
|
|
return lock;
|
|
|
|
},
|
|
|
|
|
|
|
|
classID : SETTINGSSERVICE_CID,
|
2012-07-18 18:58:02 +04:00
|
|
|
QueryInterface : XPCOMUtils.generateQI([Ci.nsISettingsService]),
|
2013-06-01 02:28:46 +04:00
|
|
|
classInfo: XPCOMUtils.generateCI({
|
|
|
|
classID: SETTINGSSERVICE_CID,
|
|
|
|
contractID: "@mozilla.org/settingsService;1",
|
|
|
|
interfaces: [Ci.nsISettingsService],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT
|
|
|
|
})
|
2012-04-28 03:21:31 +04:00
|
|
|
}
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SettingsService, SettingsServiceLock])
|