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 Cu.import("resource://gre/modules/Services.jsm"); |
6 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | 6 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); |
(...skipping 26 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] = function() |
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 get: () => value, |
75 { | 75 set: function(newValue) |
76 if (value == newValue) | 76 { |
| 77 if (value == newValue) |
| 78 return value; |
| 79 |
| 80 try |
| 81 { |
| 82 ignorePrefChanges = true; |
| 83 writeFunc(branch, name, newValue); |
| 84 value = newValue; |
| 85 Services.prefs.savePrefFile(null); |
| 86 triggerListeners(name); |
| 87 } |
| 88 catch(e) |
| 89 { |
| 90 Cu.reportError(e); |
| 91 } |
| 92 finally |
| 93 { |
| 94 ignorePrefChanges = false; |
| 95 } |
77 return value; | 96 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 } | 97 } |
87 catch(e) | |
88 { | |
89 Cu.reportError(e); | |
90 } | |
91 finally | |
92 { | |
93 ignorePrefChanges = false; | |
94 } | |
95 return value; | |
96 }); | 98 }); |
97 Prefs["_update_" + name](); | 99 Prefs["_update_" + name](); |
98 } | 100 } |
99 | 101 |
100 let listeners = []; | 102 let listeners = []; |
101 function triggerListeners(/**String*/ name) | 103 function triggerListeners(/**String*/ name) |
102 { | 104 { |
103 for (let i = 0; i < listeners.length; i++) | 105 for (let i = 0; i < listeners.length; i++) |
104 { | 106 { |
105 try | 107 try |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 { | 191 { |
190 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); | 192 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); |
191 str.data = newValue; | 193 str.data = newValue; |
192 branch.setComplexValue(pref, Ci.nsISupportsString, str); | 194 branch.setComplexValue(pref, Ci.nsISupportsString, str); |
193 } | 195 } |
194 | 196 |
195 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) | 197 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) |
196 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri
ngify(newValue)) | 198 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri
ngify(newValue)) |
197 | 199 |
198 init(); | 200 init(); |
OLD | NEW |