Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/prefs.js

Issue 5695444839563264: Issue 1434 - Remove some non-standard JS usage from buildtools. (Closed)
Patch Set: Created Sept. 24, 2014, 1:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« lib/keySelector.js ('K') | « lib/keySelector.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 18 matching lines...) Expand all
29 setter(defaultBranch, pref, value); 29 setter(defaultBranch, pref, value);
30 defineProperty(pref, false, getter, setter); 30 defineProperty(pref, false, getter, setter);
31 } 31 }
32 }; 32 };
33 Services.scriptloader.loadSubScript(addonRoot + "defaults/prefs.js", scope); 33 Services.scriptloader.loadSubScript(addonRoot + "defaults/prefs.js", scope);
34 34
35 // Add preference change observer 35 // Add preference change observer
36 try 36 try
37 { 37 {
38 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); 38 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true);
39 onShutdown.add(function() branch.removeObserver("", Prefs)); 39 onShutdown.add(function()
40 {
41 branch.removeObserver("", Prefs);
42 });
40 } 43 }
41 catch (e) 44 catch (e)
42 { 45 {
43 Cu.reportError(e); 46 Cu.reportError(e);
44 } 47 }
45 } 48 }
46 49
47 /** 50 /**
48 * Sets up getter/setter on Prefs object for preference. 51 * Sets up getter/setter on Prefs object for preference.
49 */ 52 */
50 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc) 53 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc)
51 { 54 {
52 let value = defaultValue; 55 let value = defaultValue;
53 Prefs["_update_" + name] = function() 56 Prefs["_update_" + name] = function()
54 { 57 {
55 try 58 try
56 { 59 {
57 value = readFunc(branch, name); 60 value = readFunc(branch, name);
58 triggerListeners(name); 61 triggerListeners(name);
59 } 62 }
60 catch(e) 63 catch(e)
61 { 64 {
62 Cu.reportError(e); 65 Cu.reportError(e);
63 } 66 }
64 }; 67 };
65 Prefs.__defineGetter__(name, function() value); 68
66 Prefs.__defineSetter__(name, function(newValue) 69 Object.defineProperty(Prefs, name,
67 { 70 {
68 if (value == newValue) 71 enumerable: true,
72 get: function ()
73 {
69 return value; 74 return value;
75 },
76 set: function (newValue)
77 {
78 if (value == newValue)
79 return value;
70 80
71 try 81 try
72 { 82 {
73 ignorePrefChanges = true; 83 ignorePrefChanges = true;
74 writeFunc(branch, name, newValue); 84 writeFunc(branch, name, newValue);
75 value = newValue; 85 value = newValue;
76 Services.prefs.savePrefFile(null); 86 Services.prefs.savePrefFile(null);
77 triggerListeners(name); 87 triggerListeners(name);
88 }
89 catch(e)
90 {
91 Cu.reportError(e);
92 }
93 finally
94 {
95 ignorePrefChanges = false;
96 }
97 return value;
78 } 98 }
79 catch(e)
80 {
81 Cu.reportError(e);
82 }
83 finally
84 {
85 ignorePrefChanges = false;
86 }
87 return value;
88 }); 99 });
100
89 Prefs["_update_" + name](); 101 Prefs["_update_" + name]();
90 } 102 }
91 103
92 let listeners = []; 104 let listeners = [];
93 function triggerListeners(/**String*/ name) 105 function triggerListeners(/**String*/ name)
94 { 106 {
95 for (let i = 0; i < listeners.length; i++) 107 for (let i = 0; i < listeners.length; i++)
96 { 108 {
97 try 109 try
98 { 110 {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 175
164 // Getter/setter functions for difference preference types 176 // Getter/setter functions for difference preference types
165 let typeMap = 177 let typeMap =
166 { 178 {
167 boolean: [getBoolPref, setBoolPref], 179 boolean: [getBoolPref, setBoolPref],
168 number: [getIntPref, setIntPref], 180 number: [getIntPref, setIntPref],
169 string: [getCharPref, setCharPref], 181 string: [getCharPref, setCharPref],
170 object: [getJSONPref, setJSONPref] 182 object: [getJSONPref, setJSONPref]
171 }; 183 };
172 184
173 function getIntPref(branch, pref) branch.getIntPref(pref) 185 function getIntPref(branch, pref)
174 function setIntPref(branch, pref, newValue) branch.setIntPref(pref, newValue) 186 {
187 return branch.getIntPref(pref);
188 }
189 function setIntPref(branch, pref, newValue)
190 {
191 branch.setIntPref(pref, newValue);
192 }
175 193
176 function getBoolPref(branch, pref) branch.getBoolPref(pref) 194 function getBoolPref(branch, pref)
177 function setBoolPref(branch, pref, newValue) branch.setBoolPref(pref, newValue) 195 {
196 return branch.getBoolPref(pref);
197 }
198 function setBoolPref(branch, pref, newValue)
199 {
200 branch.setBoolPref(pref, newValue);
201 }
178 202
179 function getCharPref(branch, pref) branch.getComplexValue(pref, Ci.nsISupportsSt ring).data 203 function getCharPref(branch, pref)
204 {
205 return branch.getComplexValue(pref, Ci.nsISupportsString).data;
206 }
180 function setCharPref(branch, pref, newValue) 207 function setCharPref(branch, pref, newValue)
181 { 208 {
182 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring); 209 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring);
183 str.data = newValue; 210 str.data = newValue;
184 branch.setComplexValue(pref, Ci.nsISupportsString, str); 211 branch.setComplexValue(pref, Ci.nsISupportsString, str);
185 } 212 }
186 213
187 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) 214 function getJSONPref(branch, pref)
188 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri ngify(newValue)) 215 {
216 return JSON.parse(getCharPref(branch, pref));
217 }
218 function setJSONPref(branch, pref, newValue)
219 {
220 setCharPref(branch, pref, JSON.stringify(newValue))
221 }
189 222
190 init(); 223 init();
OLDNEW
« lib/keySelector.js ('K') | « lib/keySelector.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld