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: add proper support of nullable setting values Created March 28, 2017, 2:28 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 | « include/AdblockPlus/JsEngine.h ('k') | src/FilterEngine.cpp » ('j') | 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 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
Oleksandr 2017/03/28 14:45:01 I think it would be better to have an empty string
sergei 2017/03/28 14:46:35 That where we started from, an empty string is a m
50 };
51
52 // It is for non-objects, e.g. for string, number, etc.
53 let nullableValues_ExpectedTypes = {
54 __proto__: null,
55 allowed_connection_type: "string"
49 }; 56 };
50 57
51 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates", 58 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates",
52 "first_run_subscription_auto_select", "allowed_connection_type"]; 59 "first_run_subscription_auto_select", "allowed_connection_type"];
53 60
54 let values; 61 let values;
55 let path = _fileSystem.resolve("prefs.json"); 62 let path = _fileSystem.resolve("prefs.json");
56 let listeners = []; 63 let listeners = [];
57 let isDirty = false; 64 let isDirty = false;
58 let isSaving = false; 65 let isSaving = false;
59 66
67 function isValueTypeCorrect(key, value)
68 {
69 let nullableValue_ExpectedType = nullableValues_ExpectedTypes[key];
70 return !nullableValue_ExpectedType ?
71 typeof value == typeof defaults[key] :
72 value === null || typeof value == nullableValue_ExpectedType;
73 }
74
60 function defineProperty(key) 75 function defineProperty(key)
61 { 76 {
62 Object.defineProperty(Prefs, key, 77 Object.defineProperty(Prefs, key,
63 { 78 {
64 get: () => values[key], 79 get: () => values[key],
65 set: function(value) 80 set: function(value)
66 { 81 {
67 if (typeof value != typeof defaults[key]) 82 if (!isValueTypeCorrect(key, value))
68 throw new Error("Attempt to change preference type"); 83 throw new Error("Attempt to change preference type");
69 84
70 if (value == defaults[key]) 85 if (value == defaults[key])
71 delete values[key]; 86 delete values[key];
72 else 87 else
73 values[key] = value; 88 values[key] = value;
74 save(); 89 save();
75 90
76 for (let listener of listeners) 91 for (let listener of listeners)
77 listener(key); 92 listener(key);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 { 149 {
135 let index = listeners.indexOf(listener); 150 let index = listeners.indexOf(listener);
136 if (index >= 0) 151 if (index >= 0)
137 listeners.splice(index, 1); 152 listeners.splice(index, 1);
138 }, 153 },
139 }; 154 };
140 155
141 // Update the default prefs with what was preconfigured 156 // Update the default prefs with what was preconfigured
142 for (let key in _preconfiguredPrefs) 157 for (let key in _preconfiguredPrefs)
143 if (preconfigurable.indexOf(key) != -1) 158 if (preconfigurable.indexOf(key) != -1)
144 defaults[key] = _preconfiguredPrefs[key]; 159 {
160 let value = _preconfiguredPrefs[key];
161 if (!isValueTypeCorrect(key, value))
162 throw new Error("Unexpected value type in preconfigured preferences");
163
164 defaults[key] = value;
165 }
145 166
146 // Define defaults 167 // Define defaults
147 for (let key in defaults) 168 for (let key in defaults)
148 defineProperty(key); 169 defineProperty(key);
149 170
150 // Set values of prefs based on defaults 171 // Set values of prefs based on defaults
151 values = Object.create(defaults); 172 values = Object.create(defaults);
152 173
153 load(); 174 load();
OLDNEW
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | src/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld