michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const {Cc, Ci, Cu, CC} = require("chrome"); michael@0: const protocol = require("devtools/server/protocol"); michael@0: const {Arg, method, RetVal} = protocol; michael@0: const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: exports.register = function(handle) { michael@0: handle.addGlobalActor(PreferenceActor, "preferenceActor"); michael@0: }; michael@0: michael@0: exports.unregister = function(handle) { michael@0: }; michael@0: michael@0: let PreferenceActor = protocol.ActorClass({ michael@0: typeName: "preference", michael@0: michael@0: getBoolPref: method(function(name) { michael@0: return Services.prefs.getBoolPref(name); michael@0: }, { michael@0: request: { value: Arg(0) }, michael@0: response: { value: RetVal("boolean") } michael@0: }), michael@0: michael@0: getCharPref: method(function(name) { michael@0: return Services.prefs.getCharPref(name); michael@0: }, { michael@0: request: { value: Arg(0) }, michael@0: response: { value: RetVal("string") } michael@0: }), michael@0: michael@0: getIntPref: method(function(name) { michael@0: return Services.prefs.getIntPref(name); michael@0: }, { michael@0: request: { value: Arg(0) }, michael@0: response: { value: RetVal("number") } michael@0: }), michael@0: michael@0: getAllPrefs: method(function() { michael@0: let prefs = {}; michael@0: Services.prefs.getChildList("").forEach(function(name, index) { michael@0: // append all key/value pairs into a huge json object. michael@0: try { michael@0: let value; michael@0: switch (Services.prefs.getPrefType(name)) { michael@0: case Ci.nsIPrefBranch.PREF_STRING: michael@0: value = Services.prefs.getCharPref(name); michael@0: break; michael@0: case Ci.nsIPrefBranch.PREF_INT: michael@0: value = Services.prefs.getIntPref(name); michael@0: break; michael@0: case Ci.nsIPrefBranch.PREF_BOOL: michael@0: value = Services.prefs.getBoolPref(name); michael@0: break; michael@0: default: michael@0: } michael@0: prefs[name] = { michael@0: value: value, michael@0: hasUserValue: Services.prefs.prefHasUserValue(name) michael@0: }; michael@0: } catch (e) { michael@0: // pref exists but has no user or default value michael@0: } michael@0: }); michael@0: return prefs; michael@0: }, { michael@0: request: {}, michael@0: response: { value: RetVal("json") } michael@0: }), michael@0: michael@0: setBoolPref: method(function(name, value) { michael@0: Services.prefs.setBoolPref(name, value); michael@0: Services.prefs.savePrefFile(null); michael@0: }, { michael@0: request: { name: Arg(0), value: Arg(1) }, michael@0: response: {} michael@0: }), michael@0: michael@0: setCharPref: method(function(name, value) { michael@0: Services.prefs.setCharPref(name, value); michael@0: Services.prefs.savePrefFile(null); michael@0: }, { michael@0: request: { name: Arg(0), value: Arg(1) }, michael@0: response: {} michael@0: }), michael@0: michael@0: setIntPref: method(function(name, value) { michael@0: Services.prefs.setIntPref(name, value); michael@0: Services.prefs.savePrefFile(null); michael@0: }, { michael@0: request: { name: Arg(0), value: Arg(1) }, michael@0: response: {} michael@0: }), michael@0: michael@0: clearUserPref: method(function(name) { michael@0: Services.prefs.clearUserPref(name); michael@0: Services.prefs.savePrefFile(null); michael@0: }, { michael@0: request: { name: Arg(0) }, michael@0: response: {} michael@0: }), michael@0: }); michael@0: michael@0: let PreferenceFront = protocol.FrontClass(PreferenceActor, { michael@0: initialize: function(client, form) { michael@0: protocol.Front.prototype.initialize.call(this, client); michael@0: this.actorID = form.preferenceActor; michael@0: client.addActorPool(this); michael@0: this.manage(this); michael@0: }, michael@0: }); michael@0: michael@0: const _knownPreferenceFronts = new WeakMap(); michael@0: michael@0: exports.getPreferenceFront = function(client, form) { michael@0: if (_knownPreferenceFronts.has(client)) michael@0: return _knownPreferenceFronts.get(client); michael@0: michael@0: let front = new PreferenceFront(client, form); michael@0: _knownPreferenceFronts.set(client, front); michael@0: return front; michael@0: };