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