| 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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 * @static | 181 * @static |
| 182 */ | 182 */ |
| 183 let Prefs = exports.Prefs = { | 183 let Prefs = exports.Prefs = { |
| 184 /** | 184 /** |
| 185 * Adds a callback that is called when the | 185 * Adds a callback that is called when the |
| 186 * value of a specified preference changed. | 186 * value of a specified preference changed. |
| 187 * | 187 * |
| 188 * @param {string} preference | 188 * @param {string} preference |
| 189 * @param {function} callback | 189 * @param {function} callback |
| 190 */ | 190 */ |
| 191 on: function(preference, callback) | 191 on: (preference, callback) => |
| 192 { | 192 { |
| 193 eventEmitter.on(preference, callback); | 193 eventEmitter.on(preference, callback); |
| 194 }, | 194 }, |
| 195 | 195 |
| 196 /** | 196 /** |
| 197 * Removes a callback for the specified preference. | 197 * Removes a callback for the specified preference. |
| 198 * | 198 * |
| 199 * @param {string} preference | 199 * @param {string} preference |
| 200 * @param {function} callback | 200 * @param {function} callback |
| 201 */ | 201 */ |
| 202 off: function(preference, callback) | 202 off: (preference, callback) => |
| 203 { | 203 { |
| 204 eventEmitter.off(preference, callback); | 204 eventEmitter.off(preference, callback); |
| 205 }, | 205 }, |
| 206 | 206 |
| 207 /** | 207 /** |
| 208 * A promise that is fullfilled when all preferences have been loaded. | 208 * A promise that is fullfilled when all preferences have been loaded. |
| 209 * Wait for this promise to be fulfilled before using preferences during | 209 * Wait for this promise to be fulfilled before using preferences during |
| 210 * extension initialization. | 210 * extension initialization. |
| 211 * | 211 * |
| 212 * @type {Promise} | 212 * @type {Promise} |
| (...skipping 10 matching lines...) Expand all Loading... |
| 223 } | 223 } |
| 224 | 224 |
| 225 function prefToKey(pref) | 225 function prefToKey(pref) |
| 226 { | 226 { |
| 227 return keyPrefix + pref; | 227 return keyPrefix + pref; |
| 228 } | 228 } |
| 229 | 229 |
| 230 function addPreference(pref) | 230 function addPreference(pref) |
| 231 { | 231 { |
| 232 Object.defineProperty(Prefs, pref, { | 232 Object.defineProperty(Prefs, pref, { |
| 233 get: function() | 233 get: () => (pref in overrides ? overrides : defaults)[pref], |
| 234 { | 234 set: value => |
| 235 return (pref in overrides ? overrides : defaults)[pref]; | |
| 236 }, | |
| 237 set: function(value) | |
| 238 { | 235 { |
| 239 let defaultValue = defaults[pref]; | 236 let defaultValue = defaults[pref]; |
| 240 | 237 |
| 241 if (typeof value != typeof defaultValue) | 238 if (typeof value != typeof defaultValue) |
| 242 throw new Error("Attempt to change preference type"); | 239 throw new Error("Attempt to change preference type"); |
| 243 | 240 |
| 244 if (value == defaultValue) | 241 if (value == defaultValue) |
| 245 { | 242 { |
| 246 delete overrides[pref]; | 243 delete overrides[pref]; |
| 247 ext.storage.remove(prefToKey(pref)); | 244 ext.storage.remove(prefToKey(pref)); |
| 248 } | 245 } |
| 249 else | 246 else |
| 250 { | 247 { |
| 251 overrides[pref] = value; | 248 overrides[pref] = value; |
| 252 ext.storage.set(prefToKey(pref), value); | 249 ext.storage.set(prefToKey(pref), value); |
| 253 } | 250 } |
| 254 }, | 251 }, |
| 255 enumerable: true | 252 enumerable: true |
| 256 }); | 253 }); |
| 257 } | 254 } |
| 258 | 255 |
| 259 function init() | 256 function init() |
| 260 { | 257 { |
| 261 let prefs = Object.keys(defaults); | 258 let prefs = Object.keys(defaults); |
| 262 prefs.forEach(addPreference); | 259 prefs.forEach(addPreference); |
| 263 | 260 |
| 264 let localLoaded = new Promise(resolve => { | 261 let localLoaded = new Promise(resolve => { |
| 265 ext.storage.get(prefs.map(prefToKey), function(items) | 262 ext.storage.get(prefs.map(prefToKey), items => |
| 266 { | 263 { |
| 267 for (let key in items) | 264 for (let key in items) |
| 268 overrides[keyToPref(key)] = items[key]; | 265 overrides[keyToPref(key)] = items[key]; |
| 269 | 266 |
| 270 resolve(); | 267 resolve(); |
| 271 }); | 268 }); |
| 272 }); | 269 }); |
| 273 | 270 |
| 274 let managedLoaded = new Promise(resolve => { | 271 let managedLoaded = new Promise(resolve => { |
| 275 if (require("info").platform == "chromium" && "managed" in chrome.storage) | 272 if (require("info").platform == "chromium" && "managed" in chrome.storage) |
| 276 { | 273 { |
| 277 chrome.storage.managed.get(null, function(items) | 274 chrome.storage.managed.get(null, items => |
| 278 { | 275 { |
| 279 // Opera doesn't support chrome.storage.managed, but instead simply | 276 // Opera doesn't support chrome.storage.managed, but instead simply |
| 280 // removing the API, Opera sets chrome.runtime.lastError when using it. | 277 // removing the API, Opera sets chrome.runtime.lastError when using it. |
| 281 // So we have to retrieve that error, to prevent it from showing up | 278 // So we have to retrieve that error, to prevent it from showing up |
| 282 // in the console. | 279 // in the console. |
| 283 chrome.runtime.lastError; | 280 chrome.runtime.lastError; |
| 284 | 281 |
| 285 for (let key in items) | 282 for (let key in items) |
| 286 defaults[key] = items[key]; | 283 defaults[key] = items[key]; |
| 287 | 284 |
| 288 resolve(); | 285 resolve(); |
| 289 }); | 286 }); |
| 290 } | 287 } |
| 291 else | 288 else |
| 292 { | 289 { |
| 293 resolve(); | 290 resolve(); |
| 294 } | 291 } |
| 295 }); | 292 }); |
| 296 | 293 |
| 297 function onLoaded() | 294 function onLoaded() |
| 298 { | 295 { |
| 299 ext.storage.onChanged.addListener(function(changes) | 296 ext.storage.onChanged.addListener(changes => |
| 300 { | 297 { |
| 301 for (let key in changes) | 298 for (let key in changes) |
| 302 { | 299 { |
| 303 let pref = keyToPref(key); | 300 let pref = keyToPref(key); |
| 304 if (pref && pref in defaults) | 301 if (pref && pref in defaults) |
| 305 { | 302 { |
| 306 let change = changes[key]; | 303 let change = changes[key]; |
| 307 if ("newValue" in change && change.newValue != defaults[pref]) | 304 if ("newValue" in change && change.newValue != defaults[pref]) |
| 308 overrides[pref] = change.newValue; | 305 overrides[pref] = change.newValue; |
| 309 else | 306 else |
| 310 delete overrides[pref]; | 307 delete overrides[pref]; |
| 311 | 308 |
| 312 eventEmitter.emit(pref); | 309 eventEmitter.emit(pref); |
| 313 } | 310 } |
| 314 } | 311 } |
| 315 }); | 312 }); |
| 316 } | 313 } |
| 317 | 314 |
| 318 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); | 315 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); |
| 319 } | 316 } |
| 320 | 317 |
| 321 init(); | 318 init(); |
| OLD | NEW |