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

Delta Between Two Patch Sets: lib/prefs.js

Issue 29396582: Issue 5039 - add support of nullable non-object values in settings
Left Patch Set: fix comment and rename local var Created March 28, 2017, 10:24 a.m.
Right Patch Set: rebase Created March 29, 2017, 4:39 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 | « include/AdblockPlus/JsEngine.h ('k') | src/FilterEngine.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 28 matching lines...) Expand all
39 update_last_error: 0, 39 update_last_error: 0,
40 update_soft_expiration: 0, 40 update_soft_expiration: 0,
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 allowed_connection_type: null
49 }; 50 };
50 51
51 let optionalValues_ExpectedType = { 52 // It is for non-objects, e.g. for string, number, etc.
Oleksandr 2017/03/28 12:42:08 Nit: how about optionalValues_ExpectedTypes for be
sergei 2017/03/28 14:31:41 it's a bit difficult naming case, done.
53 // Don't put here preferences used by adblockpluscore.
54 let nullableValues_ExpectedTypes = {
52 __proto__: null, 55 __proto__: null,
53 allowed_connection_type: "string" 56 allowed_connection_type: "string"
54 }; 57 };
55 58
56 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates", 59 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates",
57 "first_run_subscription_auto_select", "allowed_connection_type"]; 60 "first_run_subscription_auto_select", "allowed_connection_type"];
58 61
59 let values; 62 let values;
60 let path = _fileSystem.resolve("prefs.json"); 63 let path = _fileSystem.resolve("prefs.json");
61 let listeners = []; 64 let listeners = [];
62 let isDirty = false; 65 let isDirty = false;
63 let isSaving = false; 66 let isSaving = false;
64 67
65 function isValueTypeCorrect(key, value) 68 function isValueTypeCorrect(key, value)
66 { 69 {
67 // For values of required settings the first line just works. 70 let nullableValue_ExpectedType = nullableValues_ExpectedTypes[key];
68 // For values of optional settings it works when the type of default 71 return !nullableValue_ExpectedType ?
69 // value is the same as the type of value. It happens when 72 typeof value == typeof defaults[key] :
70 // - value is undefined and no default value 73 value === null || typeof value == nullableValue_ExpectedType;
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 isGoodValueType = typeof value == typeof defaults[key];
76 let optionalValue_ExpectedType = optionalValues_ExpectedType[key];
77 if (!isGoodValueType && optionalValue_ExpectedType)
78 {
79 isGoodValueType = value == undefined || typeof value == optionalValue_Expect edType;
80 }
81 return isGoodValueType;
82 } 74 }
83 75
84 function defineProperty(key) 76 function defineProperty(key)
85 { 77 {
86 Object.defineProperty(Prefs, key, 78 Object.defineProperty(Prefs, key,
87 { 79 {
88 get: () => values[key], 80 get: () => values[key],
89 set: function(value) 81 set: function(value)
90 { 82 {
91 if (!isValueTypeCorrect(key, value)) 83 if (!isValueTypeCorrect(key, value))
Oleksandr 2017/03/28 12:42:08 It looks like this is done only for one property a
92 throw new Error("Attempt to change preference type"); 84 throw new Error("Attempt to change preference type");
93 85
94 if (value == defaults[key] || value == undefined) 86 if (value == defaults[key])
95 delete values[key]; 87 delete values[key];
96 else 88 else
97 values[key] = value; 89 values[key] = value;
98 save(); 90 save();
99 91
100 for (let listener of listeners) 92 for (let listener of listeners)
101 listener(key); 93 listener(key);
102 }, 94 },
103 enumerable: true 95 enumerable: true
104 }); 96 });
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 }; 155 };
164 156
165 // Update the default prefs with what was preconfigured 157 // Update the default prefs with what was preconfigured
166 for (let key in _preconfiguredPrefs) 158 for (let key in _preconfiguredPrefs)
167 if (preconfigurable.indexOf(key) != -1) 159 if (preconfigurable.indexOf(key) != -1)
168 { 160 {
169 let value = _preconfiguredPrefs[key]; 161 let value = _preconfiguredPrefs[key];
170 if (!isValueTypeCorrect(key, value)) 162 if (!isValueTypeCorrect(key, value))
171 throw new Error("Unexpected value type in preconfigured preferences"); 163 throw new Error("Unexpected value type in preconfigured preferences");
172 164
173 if (value == undefined) 165 defaults[key] = value;
174 delete defaults[key];
175 else
176 defaults[key] = value;
177 } 166 }
178 167
179 // Define defaults 168 // Define defaults
180 for (let key in defaults) 169 for (let key in defaults)
181 defineProperty(key); 170 defineProperty(key);
182 171
183 for (let key in optionalValues_ExpectedType)
184 // only those which are not defined yet
185 if (!Object.prototype.hasOwnProperty.call(defaults, key))
186 defineProperty(key);
187
188 // Set values of prefs based on defaults 172 // Set values of prefs based on defaults
189 values = Object.create(defaults); 173 values = Object.create(defaults);
190 174
191 load(); 175 load();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld