| Index: lib/prefs.js |
| =================================================================== |
| --- a/lib/prefs.js |
| +++ b/lib/prefs.js |
| @@ -1,14 +1,14 @@ |
| /* This Source Code Form is subject to the terms of the Mozilla Public |
| * License, v. 2.0. If a copy of the MPL was not distributed with this |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| -Cu.import("resource://gre/modules/Services.jsm"); |
| -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
| +let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| +let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
| let {addonRoot, addonName} = require("info"); |
| let branchName = "extensions." + addonName + "."; |
| let branch = Services.prefs.getBranch(branchName); |
| let preconfiguredBranch = |
| Services.prefs.getBranch(branchName + "preconfigured."); |
| let ignorePrefChanges = false; |
| @@ -39,65 +39,68 @@ function init() |
| setter(defaultBranch, pref, value); |
| defineProperty(pref, false, getter, setter); |
| } |
| // Add preference change observer |
| try |
| { |
| branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); |
| - onShutdown.add(function() branch.removeObserver("", Prefs)); |
| + onShutdown.add(() => branch.removeObserver("", Prefs)); |
| } |
| catch (e) |
| { |
| Cu.reportError(e); |
| } |
| } |
| /** |
| * Sets up getter/setter on Prefs object for preference. |
| */ |
| function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc) |
| { |
| let value = defaultValue; |
| - Prefs["_update_" + name] = function() |
| + Prefs["_update_" + name] = () => |
| { |
| try |
| { |
| value = readFunc(branch, name); |
| triggerListeners(name); |
| } |
| catch(e) |
| { |
| Cu.reportError(e); |
| } |
| }; |
| - Prefs.__defineGetter__(name, function() value); |
| - Prefs.__defineSetter__(name, function(newValue) |
| - { |
| - if (value == newValue) |
| + Object.defineProperty(Prefs, name, { |
| + enumerable: true, |
| + get: () => value, |
| + set: (newValue) => |
| + { |
| + if (value == newValue) |
| + return value; |
| + |
| + try |
| + { |
| + ignorePrefChanges = true; |
| + writeFunc(branch, name, newValue); |
| + value = newValue; |
| + Services.prefs.savePrefFile(null); |
| + triggerListeners(name); |
| + } |
| + catch(e) |
| + { |
| + Cu.reportError(e); |
| + } |
| + finally |
| + { |
| + ignorePrefChanges = false; |
| + } |
| return value; |
| - |
| - try |
| - { |
| - ignorePrefChanges = true; |
| - writeFunc(branch, name, newValue); |
| - value = newValue; |
| - Services.prefs.savePrefFile(null); |
| - triggerListeners(name); |
| } |
| - catch(e) |
| - { |
| - Cu.reportError(e); |
| - } |
| - finally |
| - { |
| - ignorePrefChanges = false; |
| - } |
| - return value; |
| }); |
| Prefs["_update_" + name](); |
| } |
| let listeners = []; |
| function triggerListeners(/**String*/ name) |
| { |
| for (let i = 0; i < listeners.length; i++) |
| @@ -164,35 +167,35 @@ let Prefs = exports.Prefs = |
| if ("_update_" + data in this) |
| this["_update_" + data](); |
| }, |
| QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObserver]) |
| }; |
| +let getIntPref = (branch, pref) => branch.getIntPref(pref); |
| +let setIntPref = (branch, pref, newValue) => branch.setIntPref(pref, newValue); |
| + |
| +let getBoolPref = (branch, pref) => branch.getBoolPref(pref); |
| +let setBoolPref = (branch, pref, newValue) => branch.setBoolPref(pref, newValue); |
| + |
| +let getCharPref = (branch, pref) => branch.getComplexValue(pref, Ci.nsISupportsString).data; |
| +let setCharPref = (branch, pref, newValue) => |
| +{ |
| + let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); |
| + str.data = newValue; |
| + branch.setComplexValue(pref, Ci.nsISupportsString, str); |
| +}; |
| + |
| +let getJSONPref = (branch, pref) => JSON.parse(getCharPref(branch, pref)); |
| +let setJSONPref = (branch, pref, newValue) => setCharPref(branch, pref, JSON.stringify(newValue)); |
| + |
| // Getter/setter functions for difference preference types |
| let typeMap = |
| { |
| boolean: [getBoolPref, setBoolPref], |
| number: [getIntPref, setIntPref], |
| string: [getCharPref, setCharPref], |
| object: [getJSONPref, setJSONPref] |
| }; |
| -function getIntPref(branch, pref) branch.getIntPref(pref) |
| -function setIntPref(branch, pref, newValue) branch.setIntPref(pref, newValue) |
| - |
| -function getBoolPref(branch, pref) branch.getBoolPref(pref) |
| -function setBoolPref(branch, pref, newValue) branch.setBoolPref(pref, newValue) |
| - |
| -function getCharPref(branch, pref) branch.getComplexValue(pref, Ci.nsISupportsString).data |
| -function setCharPref(branch, pref, newValue) |
| -{ |
| - let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); |
| - str.data = newValue; |
| - branch.setComplexValue(pref, Ci.nsISupportsString, str); |
| -} |
| - |
| -function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) |
| -function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stringify(newValue)) |
| - |
| init(); |