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

Side by Side Diff: lib/prefs.js

Issue 6647895159734272: Issue 1489 - Support preconfigured defaults (Closed)
Patch Set: Make the defaults to leave alone configurable Created March 21, 2015, 7:37 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
« no previous file with comments | « no previous file | 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);
11 let ignorePrefChanges = false; 11 let ignorePrefChanges = false;
12 12
13 function init() 13 function init()
14 { 14 {
15 // Load default preferences and set up properties for them 15 // Load default preferences and set up properties for them
16 let defaultBranch = Services.prefs.getDefaultBranch(branchName); 16 let defaultBranch = Services.prefs.getDefaultBranch(branchName);
17 let scope = 17 let scope =
18 { 18 {
19 pref: function(pref, value) 19 pref: function(pref, value)
20 { 20 {
21 if (pref.substr(0, branchName.length) != branchName) 21 if (pref.substr(0, branchName.length) != branchName)
22 { 22 {
23 Cu.reportError(new Error("Ignoring default preference " + pref + ", wron g branch.")); 23 Cu.reportError(new Error("Ignoring default preference " + pref + ", wron g branch."));
24 return; 24 return;
25 } 25 }
26 pref = pref.substr(branchName.length); 26 pref = pref.substr(branchName.length);
27 27
28 let [getter, setter] = typeMap[typeof value]; 28 let [getter, setter] = typeMap[typeof value];
29 setter(defaultBranch, pref, value); 29 if (!hasPreconfiguredDefault(branch, pref))
30 setter(defaultBranch, pref, value);
31
30 defineProperty(pref, false, getter, setter); 32 defineProperty(pref, false, getter, setter);
31 } 33 }
32 }; 34 };
33 Services.scriptloader.loadSubScript(addonRoot + "defaults/prefs.js", scope); 35 Services.scriptloader.loadSubScript(addonRoot + "defaults/prefs.js", scope);
34 36
35 // Add preference change observer 37 // Add preference change observer
36 try 38 try
37 { 39 {
38 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); 40 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true);
39 onShutdown.add(function() branch.removeObserver("", Prefs)); 41 onShutdown.add(function() branch.removeObserver("", Prefs));
40 } 42 }
41 catch (e) 43 catch (e)
42 { 44 {
43 Cu.reportError(e); 45 Cu.reportError(e);
44 } 46 }
45 } 47 }
46 48
47 /** 49 /**
50 * Checks whether the supplied pref has a preconfigured default value.
51 */
52 function hasPreconfiguredDefault(branch, pref)
53 {
54 try
55 {
56 let defaults = getJSONPref(branch, "preconfigured_defaults");
57 return defaults.indexOf(pref) != -1;
58 }
59 catch (e)
60 {
61 return false;
62 }
63 }
64
65 /**
48 * Sets up getter/setter on Prefs object for preference. 66 * Sets up getter/setter on Prefs object for preference.
49 */ 67 */
50 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc) 68 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc)
51 { 69 {
52 let value = defaultValue; 70 let value = defaultValue;
53 Prefs["_update_" + name] = function() 71 Prefs["_update_" + name] = function()
54 { 72 {
55 try 73 try
56 { 74 {
57 value = readFunc(branch, name); 75 value = readFunc(branch, name);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 { 199 {
182 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring); 200 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring);
183 str.data = newValue; 201 str.data = newValue;
184 branch.setComplexValue(pref, Ci.nsISupportsString, str); 202 branch.setComplexValue(pref, Ci.nsISupportsString, str);
185 } 203 }
186 204
187 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) 205 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref))
188 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri ngify(newValue)) 206 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri ngify(newValue))
189 207
190 init(); 208 init();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld