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); |
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 { |
17 // Load default preferences and set up properties for them | 17 // Load default preferences and set up properties for them |
18 let defaultBranch = Services.prefs.getDefaultBranch(branchName); | 18 let defaultBranch = Services.prefs.getDefaultBranch(branchName); |
19 let scope = | 19 |
| 20 let request = new XMLHttpRequest(); |
| 21 request.open("GET", addonRoot + "defaults/prefs.json", false); |
| 22 request.responseType = "json"; |
| 23 request.send(); |
| 24 |
| 25 let defaults = request.response.defaults; |
| 26 for (let pref in defaults) |
20 { | 27 { |
21 pref: function(pref, value, preconfigurable) | 28 let value = defaults[pref]; |
| 29 let [getter, setter] = typeMap[typeof value]; |
| 30 if (request.response.preconfigurable.indexOf(pref) != -1) |
22 { | 31 { |
23 if (pref.substr(0, branchName.length) != branchName) | 32 try |
24 { | 33 { |
25 Cu.reportError(new Error("Ignoring default preference " + pref + ", wron
g branch.")); | 34 value = getter(preconfiguredBranch, pref); |
26 return; | |
27 } | 35 } |
28 pref = pref.substr(branchName.length); | 36 catch (e) {} |
29 | |
30 let [getter, setter] = typeMap[typeof value]; | |
31 if (preconfigurable) | |
32 { | |
33 try | |
34 { | |
35 value = getter(preconfiguredBranch, pref); | |
36 } | |
37 catch (e) {} | |
38 } | |
39 setter(defaultBranch, pref, value); | |
40 defineProperty(pref, false, getter, setter); | |
41 } | 37 } |
42 }; | 38 setter(defaultBranch, pref, value); |
43 Services.scriptloader.loadSubScript(addonRoot + "defaults/prefs.js", scope); | 39 defineProperty(pref, false, getter, setter); |
| 40 } |
44 | 41 |
45 // Add preference change observer | 42 // Add preference change observer |
46 try | 43 try |
47 { | 44 { |
48 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); | 45 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); |
49 onShutdown.add(function() branch.removeObserver("", Prefs)); | 46 onShutdown.add(function() branch.removeObserver("", Prefs)); |
50 } | 47 } |
51 catch (e) | 48 catch (e) |
52 { | 49 { |
53 Cu.reportError(e); | 50 Cu.reportError(e); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 { | 188 { |
192 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); | 189 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt
ring); |
193 str.data = newValue; | 190 str.data = newValue; |
194 branch.setComplexValue(pref, Ci.nsISupportsString, str); | 191 branch.setComplexValue(pref, Ci.nsISupportsString, str); |
195 } | 192 } |
196 | 193 |
197 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) | 194 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) |
198 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri
ngify(newValue)) | 195 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri
ngify(newValue)) |
199 | 196 |
200 init(); | 197 init(); |
OLD | NEW |