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

Delta Between Two Patch Sets: lib/prefs.js

Issue 6647895159734272: Issue 1489 - Support preconfigured defaults (Closed)
Left Patch Set: Make the defaults to leave alone configurable Created March 21, 2015, 7:37 p.m.
Right Patch Set: Added braces around the if body Created April 28, 2015, 9:28 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 =
12 Services.prefs.getBranch(branchName + "preconfigured.");
11 let ignorePrefChanges = false; 13 let ignorePrefChanges = false;
12 14
13 function init() 15 function init()
14 { 16 {
15 // Load default preferences and set up properties for them 17 // Load default preferences and set up properties for them
16 let defaultBranch = Services.prefs.getDefaultBranch(branchName); 18 let defaultBranch = Services.prefs.getDefaultBranch(branchName);
17 let scope = 19 let scope =
18 { 20 {
19 pref: function(pref, value) 21 pref: function(pref, value, preconfigurable)
20 { 22 {
21 if (pref.substr(0, branchName.length) != branchName) 23 if (pref.substr(0, branchName.length) != branchName)
22 { 24 {
23 Cu.reportError(new Error("Ignoring default preference " + pref + ", wron g branch.")); 25 Cu.reportError(new Error("Ignoring default preference " + pref + ", wron g branch."));
24 return; 26 return;
25 } 27 }
26 pref = pref.substr(branchName.length); 28 pref = pref.substr(branchName.length);
27 29
28 let [getter, setter] = typeMap[typeof value]; 30 let [getter, setter] = typeMap[typeof value];
29 if (!hasPreconfiguredDefault(branch, pref)) 31 if (preconfigurable)
30 setter(defaultBranch, pref, value); 32 {
31 33 try
34 {
35 value = getter(preconfiguredBranch, pref);
36 }
37 catch (e) {}
38 }
39 setter(defaultBranch, pref, value);
32 defineProperty(pref, false, getter, setter); 40 defineProperty(pref, false, getter, setter);
33 } 41 }
34 }; 42 };
35 Services.scriptloader.loadSubScript(addonRoot + "defaults/prefs.js", scope); 43 Services.scriptloader.loadSubScript(addonRoot + "defaults/prefs.js", scope);
36 44
37 // Add preference change observer 45 // Add preference change observer
38 try 46 try
39 { 47 {
40 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); 48 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true);
41 onShutdown.add(function() branch.removeObserver("", Prefs)); 49 onShutdown.add(function() branch.removeObserver("", Prefs));
42 } 50 }
43 catch (e) 51 catch (e)
44 { 52 {
45 Cu.reportError(e); 53 Cu.reportError(e);
46 }
47 }
48
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 } 54 }
63 } 55 }
64 56
65 /** 57 /**
66 * Sets up getter/setter on Prefs object for preference. 58 * Sets up getter/setter on Prefs object for preference.
67 */ 59 */
68 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc) 60 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc)
69 { 61 {
70 let value = defaultValue; 62 let value = defaultValue;
71 Prefs["_update_" + name] = function() 63 Prefs["_update_" + name] = function()
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 { 191 {
200 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring); 192 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring);
201 str.data = newValue; 193 str.data = newValue;
202 branch.setComplexValue(pref, Ci.nsISupportsString, str); 194 branch.setComplexValue(pref, Ci.nsISupportsString, str);
203 } 195 }
204 196
205 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) 197 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref))
206 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri ngify(newValue)) 198 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri ngify(newValue))
207 199
208 init(); 200 init();
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld