| 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 18 matching lines...) Expand all Loading... |
| 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(() => branch.removeObserver("", Prefs)); |
| 40 } | 40 } |
| 41 catch (e) | 41 catch (e) |
| 42 { | 42 { |
| 43 Cu.reportError(e); | 43 Cu.reportError(e); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * Sets up getter/setter on Prefs object for preference. | 48 * Sets up getter/setter on Prefs object for preference. |
| 49 */ | 49 */ |
| 50 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc,
/**Function*/ writeFunc) | 50 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc,
/**Function*/ writeFunc) |
| 51 { | 51 { |
| 52 let value = defaultValue; | 52 let value = defaultValue; |
| 53 Prefs["_update_" + name] = function() | 53 Prefs["_update_" + name] = function() |
| 54 { | 54 { |
| 55 try | 55 try |
| 56 { | 56 { |
| 57 value = readFunc(branch, name); | 57 value = readFunc(branch, name); |
| 58 triggerListeners(name); | 58 triggerListeners(name); |
| 59 } | 59 } |
| 60 catch(e) | 60 catch(e) |
| 61 { | 61 { |
| 62 Cu.reportError(e); | 62 Cu.reportError(e); |
| 63 } | 63 } |
| 64 }; | 64 }; |
| 65 Prefs.__defineGetter__(name, function() value); | 65 Object.defineProperty(Prefs, name, { |
| 66 Prefs.__defineSetter__(name, function(newValue) | 66 get: () => value, |
| 67 { | 67 set: function(newValue) |
| 68 if (value == newValue) | 68 { |
| 69 if (value == newValue) |
| 70 return value; |
| 71 |
| 72 try |
| 73 { |
| 74 ignorePrefChanges = true; |
| 75 writeFunc(branch, name, newValue); |
| 76 value = newValue; |
| 77 Services.prefs.savePrefFile(null); |
| 78 triggerListeners(name); |
| 79 } |
| 80 catch(e) |
| 81 { |
| 82 Cu.reportError(e); |
| 83 } |
| 84 finally |
| 85 { |
| 86 ignorePrefChanges = false; |
| 87 } |
| 69 return value; | 88 return value; |
| 70 | |
| 71 try | |
| 72 { | |
| 73 ignorePrefChanges = true; | |
| 74 writeFunc(branch, name, newValue); | |
| 75 value = newValue; | |
| 76 Services.prefs.savePrefFile(null); | |
| 77 triggerListeners(name); | |
| 78 } | 89 } |
| 79 catch(e) | |
| 80 { | |
| 81 Cu.reportError(e); | |
| 82 } | |
| 83 finally | |
| 84 { | |
| 85 ignorePrefChanges = false; | |
| 86 } | |
| 87 return value; | |
| 88 }); | 90 }); |
| 89 Prefs["_update_" + name](); | 91 Prefs["_update_" + name](); |
| 90 } | 92 } |
| 91 | 93 |
| 92 let listeners = []; | 94 let listeners = []; |
| 93 function triggerListeners(/**String*/ name) | 95 function triggerListeners(/**String*/ name) |
| 94 { | 96 { |
| 95 for (let i = 0; i < listeners.length; i++) | 97 for (let i = 0; i < listeners.length; i++) |
| 96 { | 98 { |
| 97 try | 99 try |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 { | 183 { |
| 182 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); | 184 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); |
| 183 str.data = newValue; | 185 str.data = newValue; |
| 184 branch.setComplexValue(pref, Ci.nsISupportsString, str); | 186 branch.setComplexValue(pref, Ci.nsISupportsString, str); |
| 185 } | 187 } |
| 186 | 188 |
| 187 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) | 189 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) |
| 188 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri
ngify(newValue)) | 190 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri
ngify(newValue)) |
| 189 | 191 |
| 190 init(); | 192 init(); |
| OLD | NEW |