OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 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 | 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/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 Cu.import("resource://gre/modules/Services.jsm"); | 5 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
6 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | 6 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
7 | 7 |
8 let {addonRoot, addonName} = require("info"); | 8 let {addonRoot, addonName} = require("info"); |
9 let branchName = "extensions." + addonName + "."; | 9 let branchName = "extensions." + addonName + "."; |
10 let branch = Services.prefs.getBranch(branchName); | 10 let branch = Services.prefs.getBranch(branchName); |
11 let preconfiguredBranch = | 11 let preconfiguredBranch = |
12 Services.prefs.getBranch(branchName + "preconfigured."); | 12 Services.prefs.getBranch(branchName + "preconfigured."); |
13 let ignorePrefChanges = false; | 13 let ignorePrefChanges = false; |
14 | 14 |
15 function init() | 15 function init() |
16 { | 16 { |
(...skipping 20 matching lines...) Expand all Loading... |
37 catch (e) {} | 37 catch (e) {} |
38 } | 38 } |
39 setter(defaultBranch, pref, value); | 39 setter(defaultBranch, pref, value); |
40 defineProperty(pref, false, getter, setter); | 40 defineProperty(pref, false, getter, setter); |
41 } | 41 } |
42 | 42 |
43 // Add preference change observer | 43 // Add preference change observer |
44 try | 44 try |
45 { | 45 { |
46 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); | 46 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); |
47 onShutdown.add(function() branch.removeObserver("", Prefs)); | 47 onShutdown.add(() => branch.removeObserver("", Prefs)); |
48 } | 48 } |
49 catch (e) | 49 catch (e) |
50 { | 50 { |
51 Cu.reportError(e); | 51 Cu.reportError(e); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 /** | 55 /** |
56 * Sets up getter/setter on Prefs object for preference. | 56 * Sets up getter/setter on Prefs object for preference. |
57 */ | 57 */ |
58 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc,
/**Function*/ writeFunc) | 58 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc,
/**Function*/ writeFunc) |
59 { | 59 { |
60 let value = defaultValue; | 60 let value = defaultValue; |
61 Prefs["_update_" + name] = function() | 61 Prefs["_update_" + name] = () => |
62 { | 62 { |
63 try | 63 try |
64 { | 64 { |
65 value = readFunc(branch, name); | 65 value = readFunc(branch, name); |
66 triggerListeners(name); | 66 triggerListeners(name); |
67 } | 67 } |
68 catch(e) | 68 catch(e) |
69 { | 69 { |
70 Cu.reportError(e); | 70 Cu.reportError(e); |
71 } | 71 } |
72 }; | 72 }; |
73 Prefs.__defineGetter__(name, function() value); | 73 Object.defineProperty(Prefs, name, { |
74 Prefs.__defineSetter__(name, function(newValue) | 74 enumerable: true, |
75 { | 75 get: () => value, |
76 if (value == newValue) | 76 set: (newValue) => |
| 77 { |
| 78 if (value == newValue) |
| 79 return value; |
| 80 |
| 81 try |
| 82 { |
| 83 ignorePrefChanges = true; |
| 84 writeFunc(branch, name, newValue); |
| 85 value = newValue; |
| 86 Services.prefs.savePrefFile(null); |
| 87 triggerListeners(name); |
| 88 } |
| 89 catch(e) |
| 90 { |
| 91 Cu.reportError(e); |
| 92 } |
| 93 finally |
| 94 { |
| 95 ignorePrefChanges = false; |
| 96 } |
77 return value; | 97 return value; |
78 | |
79 try | |
80 { | |
81 ignorePrefChanges = true; | |
82 writeFunc(branch, name, newValue); | |
83 value = newValue; | |
84 Services.prefs.savePrefFile(null); | |
85 triggerListeners(name); | |
86 } | 98 } |
87 catch(e) | |
88 { | |
89 Cu.reportError(e); | |
90 } | |
91 finally | |
92 { | |
93 ignorePrefChanges = false; | |
94 } | |
95 return value; | |
96 }); | 99 }); |
97 Prefs["_update_" + name](); | 100 Prefs["_update_" + name](); |
98 } | 101 } |
99 | 102 |
100 let listeners = []; | 103 let listeners = []; |
101 function triggerListeners(/**String*/ name) | 104 function triggerListeners(/**String*/ name) |
102 { | 105 { |
103 for (let i = 0; i < listeners.length; i++) | 106 for (let i = 0; i < listeners.length; i++) |
104 { | 107 { |
105 try | 108 try |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 if (ignorePrefChanges || topic != "nsPref:changed") | 165 if (ignorePrefChanges || topic != "nsPref:changed") |
163 return; | 166 return; |
164 | 167 |
165 if ("_update_" + data in this) | 168 if ("_update_" + data in this) |
166 this["_update_" + data](); | 169 this["_update_" + data](); |
167 }, | 170 }, |
168 | 171 |
169 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse
rver]) | 172 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse
rver]) |
170 }; | 173 }; |
171 | 174 |
| 175 let getIntPref = (branch, pref) => branch.getIntPref(pref); |
| 176 let setIntPref = (branch, pref, newValue) => branch.setIntPref(pref, newValue); |
| 177 |
| 178 let getBoolPref = (branch, pref) => branch.getBoolPref(pref); |
| 179 let setBoolPref = (branch, pref, newValue) => branch.setBoolPref(pref, newValue)
; |
| 180 |
| 181 let getCharPref = (branch, pref) => branch.getComplexValue(pref, Ci.nsISupportsS
tring).data; |
| 182 let setCharPref = (branch, pref, newValue) => |
| 183 { |
| 184 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); |
| 185 str.data = newValue; |
| 186 branch.setComplexValue(pref, Ci.nsISupportsString, str); |
| 187 }; |
| 188 |
| 189 let getJSONPref = (branch, pref) => JSON.parse(getCharPref(branch, pref)); |
| 190 let setJSONPref = (branch, pref, newValue) => setCharPref(branch, pref, JSON.str
ingify(newValue)); |
| 191 |
172 // Getter/setter functions for difference preference types | 192 // Getter/setter functions for difference preference types |
173 let typeMap = | 193 let typeMap = |
174 { | 194 { |
175 boolean: [getBoolPref, setBoolPref], | 195 boolean: [getBoolPref, setBoolPref], |
176 number: [getIntPref, setIntPref], | 196 number: [getIntPref, setIntPref], |
177 string: [getCharPref, setCharPref], | 197 string: [getCharPref, setCharPref], |
178 object: [getJSONPref, setJSONPref] | 198 object: [getJSONPref, setJSONPref] |
179 }; | 199 }; |
180 | 200 |
181 function getIntPref(branch, pref) branch.getIntPref(pref) | |
182 function setIntPref(branch, pref, newValue) branch.setIntPref(pref, newValue) | |
183 | |
184 function getBoolPref(branch, pref) branch.getBoolPref(pref) | |
185 function setBoolPref(branch, pref, newValue) branch.setBoolPref(pref, newValue) | |
186 | |
187 function getCharPref(branch, pref) branch.getComplexValue(pref, Ci.nsISupportsSt
ring).data | |
188 function setCharPref(branch, pref, newValue) | |
189 { | |
190 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); | |
191 str.data = newValue; | |
192 branch.setComplexValue(pref, Ci.nsISupportsString, str); | |
193 } | |
194 | |
195 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) | |
196 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri
ngify(newValue)) | |
197 | |
198 init(); | 201 init(); |
OLD | NEW |