1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/settings/SettingsManager.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,422 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const DEBUG = false; 1.11 +function debug(s) { 1.12 + if (DEBUG) dump("-*- SettingsManager: " + s + "\n"); 1.13 +} 1.14 + 1.15 +const Cc = Components.classes; 1.16 +const Ci = Components.interfaces; 1.17 +const Cu = Components.utils; 1.18 + 1.19 +Cu.import("resource://gre/modules/SettingsQueue.jsm"); 1.20 +Cu.import("resource://gre/modules/SettingsDB.jsm"); 1.21 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.22 +Cu.import("resource://gre/modules/Services.jsm"); 1.23 +Cu.import("resource://gre/modules/ObjectWrapper.jsm"); 1.24 + 1.25 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm", 1.26 + "@mozilla.org/childprocessmessagemanager;1", 1.27 + "nsIMessageSender"); 1.28 +XPCOMUtils.defineLazyServiceGetter(this, "mrm", 1.29 + "@mozilla.org/memory-reporter-manager;1", 1.30 + "nsIMemoryReporterManager"); 1.31 + 1.32 +function SettingsLock(aSettingsManager) { 1.33 + this._open = true; 1.34 + this._isBusy = false; 1.35 + this._requests = new Queue(); 1.36 + this._settingsManager = aSettingsManager; 1.37 + this._transaction = null; 1.38 +} 1.39 + 1.40 +SettingsLock.prototype = { 1.41 + get closed() { 1.42 + return !this._open; 1.43 + }, 1.44 + 1.45 + _wrap: function _wrap(obj) { 1.46 + return Cu.cloneInto(obj, this._settingsManager._window); 1.47 + }, 1.48 + 1.49 + process: function process() { 1.50 + let lock = this; 1.51 + let store = lock._transaction.objectStore(SETTINGSSTORE_NAME); 1.52 + 1.53 + while (!lock._requests.isEmpty()) { 1.54 + let info = lock._requests.dequeue(); 1.55 + if (DEBUG) debug("info: " + info.intent); 1.56 + let request = info.request; 1.57 + switch (info.intent) { 1.58 + case "clear": 1.59 + let clearReq = store.clear(); 1.60 + clearReq.onsuccess = function() { 1.61 + this._open = true; 1.62 + Services.DOMRequest.fireSuccess(request, 0); 1.63 + this._open = false; 1.64 + }.bind(lock); 1.65 + clearReq.onerror = function() { 1.66 + Services.DOMRequest.fireError(request, 0) 1.67 + }; 1.68 + break; 1.69 + case "set": 1.70 + let keys = Object.getOwnPropertyNames(info.settings); 1.71 + for (let i = 0; i < keys.length; i++) { 1.72 + let key = keys[i]; 1.73 + let last = i === keys.length - 1; 1.74 + if (DEBUG) debug("key: " + key + ", val: " + JSON.stringify(info.settings[key]) + ", type: " + typeof(info.settings[key])); 1.75 + lock._isBusy = true; 1.76 + let checkKeyRequest = store.get(key); 1.77 + 1.78 + checkKeyRequest.onsuccess = function (event) { 1.79 + let defaultValue; 1.80 + let userValue = info.settings[key]; 1.81 + if (event.target.result) { 1.82 + defaultValue = event.target.result.defaultValue; 1.83 + } else { 1.84 + defaultValue = null; 1.85 + if (DEBUG) debug("MOZSETTINGS-SET-WARNING: " + key + " is not in the database.\n"); 1.86 + } 1.87 + 1.88 + let obj = {settingName: key, defaultValue: defaultValue, userValue: userValue}; 1.89 + if (DEBUG) debug("store1: " + JSON.stringify(obj)); 1.90 + let setReq = store.put(obj); 1.91 + 1.92 + setReq.onsuccess = function() { 1.93 + lock._isBusy = false; 1.94 + cpmm.sendAsyncMessage("Settings:Changed", { key: key, value: userValue }); 1.95 + if (last && !request.error) { 1.96 + lock._open = true; 1.97 + Services.DOMRequest.fireSuccess(request, 0); 1.98 + lock._open = false; 1.99 + if (!lock._requests.isEmpty()) { 1.100 + lock.process(); 1.101 + } 1.102 + } 1.103 + }; 1.104 + 1.105 + setReq.onerror = function() { 1.106 + if (!request.error) { 1.107 + Services.DOMRequest.fireError(request, setReq.error.name) 1.108 + } 1.109 + }; 1.110 + }; 1.111 + checkKeyRequest.onerror = function(event) { 1.112 + if (!request.error) { 1.113 + Services.DOMRequest.fireError(request, checkKeyRequest.error.name) 1.114 + } 1.115 + }; 1.116 + } 1.117 + break; 1.118 + case "get": 1.119 + let getReq = (info.name === "*") ? store.mozGetAll() 1.120 + : store.mozGetAll(info.name); 1.121 + 1.122 + getReq.onsuccess = function(event) { 1.123 + if (DEBUG) debug("Request for '" + info.name + "' successful. " + 1.124 + "Record count: " + event.target.result.length); 1.125 + 1.126 + if (event.target.result.length == 0) { 1.127 + if (DEBUG) debug("MOZSETTINGS-GET-WARNING: " + info.name + " is not in the database.\n"); 1.128 + } 1.129 + 1.130 + let results = {}; 1.131 + 1.132 + for (var i in event.target.result) { 1.133 + let result = event.target.result[i]; 1.134 + var name = result.settingName; 1.135 + if (DEBUG) debug("VAL: " + result.userValue +", " + result.defaultValue + "\n"); 1.136 + var value = result.userValue !== undefined ? result.userValue : result.defaultValue; 1.137 + results[name] = this._wrap(value); 1.138 + } 1.139 + 1.140 + this._open = true; 1.141 + Services.DOMRequest.fireSuccess(request, this._wrap(results)); 1.142 + this._open = false; 1.143 + }.bind(lock); 1.144 + 1.145 + getReq.onerror = function() { 1.146 + Services.DOMRequest.fireError(request, 0) 1.147 + }; 1.148 + break; 1.149 + } 1.150 + } 1.151 + }, 1.152 + 1.153 + createTransactionAndProcess: function() { 1.154 + if (this._settingsManager._settingsDB._db) { 1.155 + var lock; 1.156 + while (lock = this._settingsManager._locks.dequeue()) { 1.157 + if (!lock._transaction) { 1.158 + let transactionType = this._settingsManager.hasWritePrivileges ? "readwrite" : "readonly"; 1.159 + lock._transaction = lock._settingsManager._settingsDB._db.transaction(SETTINGSSTORE_NAME, transactionType); 1.160 + } 1.161 + if (!lock._isBusy) { 1.162 + lock.process(); 1.163 + } else { 1.164 + this._settingsManager._locks.enqueue(lock); 1.165 + } 1.166 + } 1.167 + if (!this._requests.isEmpty() && !this._isBusy) { 1.168 + this.process(); 1.169 + } 1.170 + } 1.171 + }, 1.172 + 1.173 + get: function get(aName) { 1.174 + if (!this._open) { 1.175 + dump("Settings lock not open!\n"); 1.176 + throw Components.results.NS_ERROR_ABORT; 1.177 + } 1.178 + 1.179 + if (this._settingsManager.hasReadPrivileges) { 1.180 + let req = Services.DOMRequest.createRequest(this._settingsManager._window); 1.181 + this._requests.enqueue({ request: req, intent:"get", name: aName }); 1.182 + this.createTransactionAndProcess(); 1.183 + return req; 1.184 + } else { 1.185 + if (DEBUG) debug("get not allowed"); 1.186 + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; 1.187 + } 1.188 + }, 1.189 + 1.190 + _serializePreservingBinaries: function _serializePreservingBinaries(aObject) { 1.191 + // We need to serialize settings objects, otherwise they can change between 1.192 + // the set() call and the enqueued request being processed. We can't simply 1.193 + // parse(stringify(obj)) because that breaks things like Blobs, Files and 1.194 + // Dates, so we use stringify's replacer and parse's reviver parameters to 1.195 + // preserve binaries. 1.196 + let manager = this._settingsManager; 1.197 + let binaries = Object.create(null); 1.198 + let stringified = JSON.stringify(aObject, function(key, value) { 1.199 + value = manager._settingsDB.prepareValue(value); 1.200 + let kind = ObjectWrapper.getObjectKind(value); 1.201 + if (kind == "file" || kind == "blob" || kind == "date") { 1.202 + let uuid = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator) 1.203 + .generateUUID().toString(); 1.204 + binaries[uuid] = value; 1.205 + return uuid; 1.206 + } 1.207 + return value; 1.208 + }); 1.209 + return JSON.parse(stringified, function(key, value) { 1.210 + if (value in binaries) { 1.211 + return binaries[value]; 1.212 + } 1.213 + return value; 1.214 + }); 1.215 + }, 1.216 + 1.217 + set: function set(aSettings) { 1.218 + if (!this._open) { 1.219 + throw "Settings lock not open"; 1.220 + } 1.221 + 1.222 + if (this._settingsManager.hasWritePrivileges) { 1.223 + let req = Services.DOMRequest.createRequest(this._settingsManager._window); 1.224 + if (DEBUG) debug("send: " + JSON.stringify(aSettings)); 1.225 + let settings = this._serializePreservingBinaries(aSettings); 1.226 + this._requests.enqueue({request: req, intent: "set", settings: settings}); 1.227 + this.createTransactionAndProcess(); 1.228 + return req; 1.229 + } else { 1.230 + if (DEBUG) debug("set not allowed"); 1.231 + throw "No permission to call set"; 1.232 + } 1.233 + }, 1.234 + 1.235 + clear: function clear() { 1.236 + if (!this._open) { 1.237 + throw "Settings lock not open"; 1.238 + } 1.239 + 1.240 + if (this._settingsManager.hasWritePrivileges) { 1.241 + let req = Services.DOMRequest.createRequest(this._settingsManager._window); 1.242 + this._requests.enqueue({ request: req, intent: "clear"}); 1.243 + this.createTransactionAndProcess(); 1.244 + return req; 1.245 + } else { 1.246 + if (DEBUG) debug("clear not allowed"); 1.247 + throw "No permission to call clear"; 1.248 + } 1.249 + }, 1.250 + 1.251 + classID: Components.ID("{60c9357c-3ae0-4222-8f55-da01428470d5}"), 1.252 + contractID: "@mozilla.org/settingsLock;1", 1.253 + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), 1.254 +}; 1.255 + 1.256 +function SettingsManager() { 1.257 + this._locks = new Queue(); 1.258 + this._settingsDB = new SettingsDB(); 1.259 + this._settingsDB.init(); 1.260 +} 1.261 + 1.262 +SettingsManager.prototype = { 1.263 + _callbacks: null, 1.264 + 1.265 + _wrap: function _wrap(obj) { 1.266 + return Cu.cloneInto(obj, this._window); 1.267 + }, 1.268 + 1.269 + nextTick: function nextTick(aCallback, thisObj) { 1.270 + if (thisObj) 1.271 + aCallback = aCallback.bind(thisObj); 1.272 + 1.273 + Services.tm.currentThread.dispatch(aCallback, Ci.nsIThread.DISPATCH_NORMAL); 1.274 + }, 1.275 + 1.276 + set onsettingchange(aHandler) { 1.277 + this.__DOM_IMPL__.setEventHandler("onsettingchange", aHandler); 1.278 + }, 1.279 + 1.280 + get onsettingchange() { 1.281 + return this.__DOM_IMPL__.getEventHandler("onsettingchange"); 1.282 + }, 1.283 + 1.284 + createLock: function() { 1.285 + if (DEBUG) debug("get lock!"); 1.286 + var lock = new SettingsLock(this); 1.287 + this._locks.enqueue(lock); 1.288 + this._settingsDB.ensureDB( 1.289 + function() { lock.createTransactionAndProcess(); }, 1.290 + function() { dump("Cannot open Settings DB. Trying to open an old version?\n"); } 1.291 + ); 1.292 + this.nextTick(function() { this._open = false; }, lock); 1.293 + return lock; 1.294 + }, 1.295 + 1.296 + receiveMessage: function(aMessage) { 1.297 + if (DEBUG) debug("Settings::receiveMessage: " + aMessage.name); 1.298 + let msg = aMessage.json; 1.299 + 1.300 + switch (aMessage.name) { 1.301 + case "Settings:Change:Return:OK": 1.302 + if (DEBUG) debug('data:' + msg.key + ':' + msg.value + '\n'); 1.303 + 1.304 + let event = new this._window.MozSettingsEvent("settingchange", this._wrap({ 1.305 + settingName: msg.key, 1.306 + settingValue: msg.value 1.307 + })); 1.308 + this.__DOM_IMPL__.dispatchEvent(event); 1.309 + 1.310 + if (this._callbacks && this._callbacks[msg.key]) { 1.311 + if (DEBUG) debug("observe callback called! " + msg.key + " " + this._callbacks[msg.key].length); 1.312 + this._callbacks[msg.key].forEach(function(cb) { 1.313 + cb(this._wrap({settingName: msg.key, settingValue: msg.value})); 1.314 + }.bind(this)); 1.315 + } else { 1.316 + if (DEBUG) debug("no observers stored!"); 1.317 + } 1.318 + break; 1.319 + default: 1.320 + if (DEBUG) debug("Wrong message: " + aMessage.name); 1.321 + } 1.322 + }, 1.323 + 1.324 + addObserver: function addObserver(aName, aCallback) { 1.325 + if (DEBUG) debug("addObserver " + aName); 1.326 + if (!this._callbacks) { 1.327 + cpmm.sendAsyncMessage("Settings:RegisterForMessages"); 1.328 + this._callbacks = {}; 1.329 + } 1.330 + if (!this._callbacks[aName]) { 1.331 + this._callbacks[aName] = [aCallback]; 1.332 + } else { 1.333 + this._callbacks[aName].push(aCallback); 1.334 + } 1.335 + }, 1.336 + 1.337 + removeObserver: function removeObserver(aName, aCallback) { 1.338 + if (DEBUG) debug("deleteObserver " + aName); 1.339 + if (this._callbacks && this._callbacks[aName]) { 1.340 + let index = this._callbacks[aName].indexOf(aCallback) 1.341 + if (index != -1) { 1.342 + this._callbacks[aName].splice(index, 1) 1.343 + } else { 1.344 + if (DEBUG) debug("Callback not found for: " + aName); 1.345 + } 1.346 + } else { 1.347 + if (DEBUG) debug("No observers stored for " + aName); 1.348 + } 1.349 + }, 1.350 + 1.351 + init: function(aWindow) { 1.352 + mrm.registerStrongReporter(this); 1.353 + cpmm.addMessageListener("Settings:Change:Return:OK", this); 1.354 + this._window = aWindow; 1.355 + Services.obs.addObserver(this, "inner-window-destroyed", false); 1.356 + let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); 1.357 + this.innerWindowID = util.currentInnerWindowID; 1.358 + 1.359 + let readPerm = Services.perms.testExactPermissionFromPrincipal(aWindow.document.nodePrincipal, "settings-read"); 1.360 + let writePerm = Services.perms.testExactPermissionFromPrincipal(aWindow.document.nodePrincipal, "settings-write"); 1.361 + this.hasReadPrivileges = readPerm == Ci.nsIPermissionManager.ALLOW_ACTION; 1.362 + this.hasWritePrivileges = writePerm == Ci.nsIPermissionManager.ALLOW_ACTION; 1.363 + 1.364 + if (this.hasReadPrivileges) { 1.365 + cpmm.sendAsyncMessage("Settings:RegisterForMessages"); 1.366 + } 1.367 + 1.368 + if (!this.hasReadPrivileges && !this.hasWritePrivileges) { 1.369 + dump("No settings permission for: " + aWindow.document.nodePrincipal.origin + "\n"); 1.370 + Cu.reportError("No settings permission for: " + aWindow.document.nodePrincipal.origin); 1.371 + } 1.372 + }, 1.373 + 1.374 + observe: function(aSubject, aTopic, aData) { 1.375 + if (DEBUG) debug("Topic: " + aTopic); 1.376 + if (aTopic == "inner-window-destroyed") { 1.377 + let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data; 1.378 + if (wId == this.innerWindowID) { 1.379 + this.cleanup(); 1.380 + } 1.381 + } 1.382 + }, 1.383 + 1.384 + collectReports: function(aCallback, aData) { 1.385 + for (var topic in this._callbacks) { 1.386 + let length = this._callbacks[topic].length; 1.387 + if (length == 0) { 1.388 + continue; 1.389 + } 1.390 + 1.391 + let path; 1.392 + if (length < 20) { 1.393 + path = "settings-observers"; 1.394 + } else { 1.395 + path = "settings-observers-suspect/referent(topic=" + topic + ")"; 1.396 + } 1.397 + 1.398 + aCallback.callback("", path, 1.399 + Ci.nsIMemoryReporter.KIND_OTHER, 1.400 + Ci.nsIMemoryReporter.UNITS_COUNT, 1.401 + this._callbacks[topic].length, 1.402 + "The number of settings observers for this topic.", 1.403 + aData); 1.404 + } 1.405 + }, 1.406 + 1.407 + cleanup: function() { 1.408 + Services.obs.removeObserver(this, "inner-window-destroyed"); 1.409 + cpmm.removeMessageListener("Settings:Change:Return:OK", this); 1.410 + mrm.unregisterStrongReporter(this); 1.411 + this._requests = null; 1.412 + this._window = null; 1.413 + this._innerWindowID = null; 1.414 + this._settingsDB.close(); 1.415 + }, 1.416 + 1.417 + classID: Components.ID("{c40b1c70-00fb-11e2-a21f-0800200c9a66}"), 1.418 + contractID: "@mozilla.org/settingsManager;1", 1.419 + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, 1.420 + Ci.nsIDOMGlobalPropertyInitializer, 1.421 + Ci.nsIObserver, 1.422 + Ci.nsIMemoryReporter]), 1.423 +}; 1.424 + 1.425 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SettingsManager, SettingsLock])