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: rebase Created March 29, 2017, 11:14 a.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: "" 49 allowed_connection_type: null
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"
50 }; 56 };
51 57
52 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates", 58 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates",
53 "first_run_subscription_auto_select", "allowed_connection_type"]; 59 "first_run_subscription_auto_select", "allowed_connection_type"];
54 60
55 let values; 61 let values;
56 let path = _fileSystem.resolve("prefs.json"); 62 let path = _fileSystem.resolve("prefs.json");
57 let listeners = []; 63 let listeners = [];
58 let isDirty = false; 64 let isDirty = false;
59 let isSaving = false; 65 let isSaving = false;
60 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
61 function defineProperty(key) 75 function defineProperty(key)
62 { 76 {
63 Object.defineProperty(Prefs, key, 77 Object.defineProperty(Prefs, key,
64 { 78 {
65 get: () => values[key], 79 get: () => values[key],
66 set: function(value) 80 set: function(value)
67 { 81 {
68 if (typeof value != typeof defaults[key]) 82 if (!isValueTypeCorrect(key, value))
69 throw new Error("Attempt to change preference type"); 83 throw new Error("Attempt to change preference type");
70 84
71 if (value == defaults[key]) 85 if (value == defaults[key])
72 delete values[key]; 86 delete values[key];
73 else 87 else
74 values[key] = value; 88 values[key] = value;
75 save(); 89 save();
76 90
77 for (let listener of listeners) 91 for (let listener of listeners)
78 listener(key); 92 listener(key);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 { 149 {
136 let index = listeners.indexOf(listener); 150 let index = listeners.indexOf(listener);
137 if (index >= 0) 151 if (index >= 0)
138 listeners.splice(index, 1); 152 listeners.splice(index, 1);
139 }, 153 },
140 }; 154 };
141 155
142 // Update the default prefs with what was preconfigured 156 // Update the default prefs with what was preconfigured
143 for (let key in _preconfiguredPrefs) 157 for (let key in _preconfiguredPrefs)
144 if (preconfigurable.indexOf(key) != -1) 158 if (preconfigurable.indexOf(key) != -1)
145 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 }
146 166
147 // Define defaults 167 // Define defaults
148 for (let key in defaults) 168 for (let key in defaults)
149 defineProperty(key); 169 defineProperty(key);
150 170
151 // Set values of prefs based on defaults 171 // Set values of prefs based on defaults
152 values = Object.create(defaults); 172 values = Object.create(defaults);
153 173
154 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