toolkit/devtools/server/actors/preference.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:821034ef58dc
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 const {Cc, Ci, Cu, CC} = require("chrome");
6 const protocol = require("devtools/server/protocol");
7 const {Arg, method, RetVal} = protocol;
8 const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
9
10 Cu.import("resource://gre/modules/Services.jsm");
11
12 exports.register = function(handle) {
13 handle.addGlobalActor(PreferenceActor, "preferenceActor");
14 };
15
16 exports.unregister = function(handle) {
17 };
18
19 let PreferenceActor = protocol.ActorClass({
20 typeName: "preference",
21
22 getBoolPref: method(function(name) {
23 return Services.prefs.getBoolPref(name);
24 }, {
25 request: { value: Arg(0) },
26 response: { value: RetVal("boolean") }
27 }),
28
29 getCharPref: method(function(name) {
30 return Services.prefs.getCharPref(name);
31 }, {
32 request: { value: Arg(0) },
33 response: { value: RetVal("string") }
34 }),
35
36 getIntPref: method(function(name) {
37 return Services.prefs.getIntPref(name);
38 }, {
39 request: { value: Arg(0) },
40 response: { value: RetVal("number") }
41 }),
42
43 getAllPrefs: method(function() {
44 let prefs = {};
45 Services.prefs.getChildList("").forEach(function(name, index) {
46 // append all key/value pairs into a huge json object.
47 try {
48 let value;
49 switch (Services.prefs.getPrefType(name)) {
50 case Ci.nsIPrefBranch.PREF_STRING:
51 value = Services.prefs.getCharPref(name);
52 break;
53 case Ci.nsIPrefBranch.PREF_INT:
54 value = Services.prefs.getIntPref(name);
55 break;
56 case Ci.nsIPrefBranch.PREF_BOOL:
57 value = Services.prefs.getBoolPref(name);
58 break;
59 default:
60 }
61 prefs[name] = {
62 value: value,
63 hasUserValue: Services.prefs.prefHasUserValue(name)
64 };
65 } catch (e) {
66 // pref exists but has no user or default value
67 }
68 });
69 return prefs;
70 }, {
71 request: {},
72 response: { value: RetVal("json") }
73 }),
74
75 setBoolPref: method(function(name, value) {
76 Services.prefs.setBoolPref(name, value);
77 Services.prefs.savePrefFile(null);
78 }, {
79 request: { name: Arg(0), value: Arg(1) },
80 response: {}
81 }),
82
83 setCharPref: method(function(name, value) {
84 Services.prefs.setCharPref(name, value);
85 Services.prefs.savePrefFile(null);
86 }, {
87 request: { name: Arg(0), value: Arg(1) },
88 response: {}
89 }),
90
91 setIntPref: method(function(name, value) {
92 Services.prefs.setIntPref(name, value);
93 Services.prefs.savePrefFile(null);
94 }, {
95 request: { name: Arg(0), value: Arg(1) },
96 response: {}
97 }),
98
99 clearUserPref: method(function(name) {
100 Services.prefs.clearUserPref(name);
101 Services.prefs.savePrefFile(null);
102 }, {
103 request: { name: Arg(0) },
104 response: {}
105 }),
106 });
107
108 let PreferenceFront = protocol.FrontClass(PreferenceActor, {
109 initialize: function(client, form) {
110 protocol.Front.prototype.initialize.call(this, client);
111 this.actorID = form.preferenceActor;
112 client.addActorPool(this);
113 this.manage(this);
114 },
115 });
116
117 const _knownPreferenceFronts = new WeakMap();
118
119 exports.getPreferenceFront = function(client, form) {
120 if (_knownPreferenceFronts.has(client))
121 return _knownPreferenceFronts.get(client);
122
123 let front = new PreferenceFront(client, form);
124 _knownPreferenceFronts.set(client, front);
125 return front;
126 };

mercurial