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

Side by Side Diff: lib/prefs.js

Issue 29396582: Issue 5039 - add support of nullable non-object values in settings
Patch Set: address comment and rename local var Created March 28, 2017, 1:38 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 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 30 matching lines...) Expand all
41 update_hard_expiration: 0, 41 update_hard_expiration: 0,
42 currentVersion: "0.0", 42 currentVersion: "0.0",
43 notificationdata: {}, 43 notificationdata: {},
44 notificationurl: "https://notification.adblockplus.org/notification.json", 44 notificationurl: "https://notification.adblockplus.org/notification.json",
45 suppress_first_run_page: false, 45 suppress_first_run_page: false,
46 disable_auto_updates: false, 46 disable_auto_updates: false,
47 first_run_subscription_auto_select: true, 47 first_run_subscription_auto_select: true,
48 notifications_ignoredcategories: [], 48 notifications_ignoredcategories: [],
49 }; 49 };
50 50
51 let optionalValues_ExpectedTypes = {
52 __proto__: null,
53 allowed_connection_type: "string"
54 };
55
51 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates", 56 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates",
52 "first_run_subscription_auto_select", "allowed_connection_type"]; 57 "first_run_subscription_auto_select", "allowed_connection_type"];
53 58
54 let values; 59 let values;
55 let path = _fileSystem.resolve("prefs.json"); 60 let path = _fileSystem.resolve("prefs.json");
56 let listeners = []; 61 let listeners = [];
57 let isDirty = false; 62 let isDirty = false;
58 let isSaving = false; 63 let isSaving = false;
59 64
65 function isValueTypeCorrect(key, value)
66 {
67 // For values of required settings the first line just works.
68 // For values of optional settings it works when the type of default
69 // value is the same as the type of value. It happens when
70 // - value is undefined and no default value
71 // - value is not undefined and there is a default value.
72 // However, for optional values types are different when
73 // - value is undefined and there is a default value
74 // - value is not undefined and there is no default value.
75 let isValueTypeGood = typeof value == typeof defaults[key];
76 let optionalValue_ExpectedType = optionalValues_ExpectedTypes[key];
77 if (!isValueTypeGood && optionalValue_ExpectedType)
78 {
79 isValueTypeGood = value == undefined || typeof value == optionalValue_Expect edType;
80 }
81 return isValueTypeGood;
82 }
83
60 function defineProperty(key) 84 function defineProperty(key)
61 { 85 {
62 Object.defineProperty(Prefs, key, 86 Object.defineProperty(Prefs, key,
63 { 87 {
64 get: () => values[key], 88 get: () => values[key],
65 set: function(value) 89 set: function(value)
66 { 90 {
67 if (typeof value != typeof defaults[key]) 91 if (!isValueTypeCorrect(key, value))
68 throw new Error("Attempt to change preference type"); 92 throw new Error("Attempt to change preference type");
69 93
70 if (value == defaults[key]) 94 if (value == defaults[key] || value == undefined)
71 delete values[key]; 95 delete values[key];
72 else 96 else
73 values[key] = value; 97 values[key] = value;
74 save(); 98 save();
75 99
76 for (let listener of listeners) 100 for (let listener of listeners)
77 listener(key); 101 listener(key);
78 }, 102 },
79 enumerable: true 103 enumerable: true
80 }); 104 });
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 { 158 {
135 let index = listeners.indexOf(listener); 159 let index = listeners.indexOf(listener);
136 if (index >= 0) 160 if (index >= 0)
137 listeners.splice(index, 1); 161 listeners.splice(index, 1);
138 }, 162 },
139 }; 163 };
140 164
141 // Update the default prefs with what was preconfigured 165 // Update the default prefs with what was preconfigured
142 for (let key in _preconfiguredPrefs) 166 for (let key in _preconfiguredPrefs)
143 if (preconfigurable.indexOf(key) != -1) 167 if (preconfigurable.indexOf(key) != -1)
144 defaults[key] = _preconfiguredPrefs[key]; 168 {
169 let value = _preconfiguredPrefs[key];
170 if (!isValueTypeCorrect(key, value))
171 throw new Error("Unexpected value type in preconfigured preferences");
172
173 if (value == undefined)
174 delete defaults[key];
175 else
176 defaults[key] = value;
177 }
145 178
146 // Define defaults 179 // Define defaults
147 for (let key in defaults) 180 for (let key in defaults)
148 defineProperty(key); 181 defineProperty(key);
149 182
183 for (let key in optionalValues_ExpectedTypes)
184 // only those which are not defined yet
185 if (!Object.prototype.hasOwnProperty.call(defaults, key))
186 defineProperty(key);
187
150 // Set values of prefs based on defaults 188 // Set values of prefs based on defaults
151 values = Object.create(defaults); 189 values = Object.create(defaults);
152 190
153 load(); 191 load();
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