LEFT | RIGHT |
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 * @namespace | 212 * @namespace |
213 * @static | 213 * @static |
214 */ | 214 */ |
215 let Prefs = exports.Prefs = { | 215 let Prefs = exports.Prefs = { |
216 /** | 216 /** |
217 * Sets the given preference. | 217 * Sets the given preference. |
218 * | 218 * |
219 * @param {string} preference | 219 * @param {string} preference |
220 * @param {any} value | 220 * @param {any} value |
221 * @return {Promise} A promise that resolves when the underlying | 221 * @return {Promise} A promise that resolves when the underlying |
222 browser.storage.local.set() operation completes | 222 browser.storage.local.set/remove() operation completes |
223 */ | 223 */ |
224 set(preference, value) | 224 set(preference, value) |
225 { | 225 { |
226 let defaultValue = defaults[preference]; | 226 let defaultValue = defaults[preference]; |
227 | 227 |
228 if (typeof value != typeof defaultValue) | 228 if (typeof value != typeof defaultValue) |
229 throw new Error("Attempt to change preference type"); | 229 throw new Error("Attempt to change preference type"); |
230 | 230 |
231 if (value == defaultValue) | 231 if (value == defaultValue) |
232 { | 232 { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 return key.substr(keyPrefix.length); | 279 return key.substr(keyPrefix.length); |
280 } | 280 } |
281 | 281 |
282 function prefToKey(pref) | 282 function prefToKey(pref) |
283 { | 283 { |
284 return keyPrefix + pref; | 284 return keyPrefix + pref; |
285 } | 285 } |
286 | 286 |
287 function savePref(pref) | 287 function savePref(pref) |
288 { | 288 { |
289 return browser.storage.local.set(prefToKey(pref), overrides[pref]); | 289 return browser.storage.local.set({[prefToKey(pref)]: overrides[pref]}); |
290 } | 290 } |
291 | 291 |
292 let customSave = new Map(); | 292 let customSave = new Map(); |
293 if (require("../buildtools/info").platform == "gecko") | 293 if (require("../buildtools/info").platform == "gecko") |
294 { | 294 { |
295 // Saving one storage value causes all others to be saved as well on Gecko. | 295 // Saving one storage value causes all others to be saved as well on Gecko. |
296 // Make sure that updating ad counter doesn't cause the filters data to be | 296 // Make sure that updating ad counter doesn't cause the filters data to be |
297 // saved frequently as a side-effect. | 297 // saved frequently as a side-effect. |
298 const MIN_UPDATE_INTERVAL = 60 * 1000; | 298 const MIN_UPDATE_INTERVAL = 60 * 1000; |
299 let lastUpdate = -MIN_UPDATE_INTERVAL; | 299 let lastUpdate = -MIN_UPDATE_INTERVAL; |
300 let updateScheduled = false; | 300 let promise = null; |
301 customSave.set("blocked_total", pref => | 301 customSave.set("blocked_total", pref => |
302 { | 302 { |
303 if (!updateScheduled) | 303 if (!promise) |
304 { | 304 { |
305 let callback = () => | 305 promise = new Promise((resolve, reject) => |
306 { | 306 { |
307 lastUpdate = performance.now(); | 307 setTimeout( |
308 updateScheduled = false; | 308 () => |
309 savePref(pref); | 309 { |
310 }; | 310 lastUpdate = performance.now(); |
311 | 311 promise = null; |
312 let timeElapsed = performance.now() - lastUpdate; | 312 savePref(pref).then(resolve, reject); |
313 if (timeElapsed < MIN_UPDATE_INTERVAL) | 313 }, |
314 { | 314 lastUpdate + MIN_UPDATE_INTERVAL - performance.now() |
315 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed); | 315 ); |
316 updateScheduled = true; | 316 }); |
317 } | |
318 else | |
319 callback(); | |
320 } | 317 } |
321 return Promise.resolve(); | 318 return promise; |
322 }); | 319 }); |
323 } | 320 } |
324 | 321 |
325 function addPreference(pref) | 322 function addPreference(pref) |
326 { | 323 { |
327 Object.defineProperty(Prefs, pref, { | 324 Object.defineProperty(Prefs, pref, { |
328 get() { return (pref in overrides ? overrides : defaults)[pref]; }, | 325 get() { return (pref in overrides ? overrides : defaults)[pref]; }, |
329 set(value) | 326 set(value) |
330 { | 327 { |
331 Prefs.set(pref, value); | 328 Prefs.set(pref, value); |
332 }, | 329 }, |
333 enumerable: true | 330 enumerable: true |
334 }); | 331 }); |
335 } | 332 } |
336 | 333 |
337 function init() | 334 function init() |
338 { | 335 { |
339 let prefs = Object.keys(defaults); | 336 let prefs = Object.keys(defaults); |
340 prefs.forEach(addPreference); | 337 prefs.forEach(addPreference); |
341 | 338 |
342 let localLoaded = new Promise((resolve, reject) => | 339 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then( |
343 { | 340 items => |
344 browser.storage.local.get(prefs.map(prefToKey)).then(items => | |
345 { | 341 { |
346 for (let key in items) | 342 for (let key in items) |
347 overrides[keyToPref(key)] = items[key]; | 343 overrides[keyToPref(key)] = items[key]; |
348 | 344 } |
349 resolve(); | 345 ); |
350 }, reject); | 346 |
351 }); | 347 let managedLoaded; |
352 | 348 if ("managed" in browser.storage) |
353 let managedLoaded = new Promise(resolve => | 349 { |
354 { | 350 managedLoaded = browser.storage.managed.get(null).then( |
355 if ("managed" in browser.storage) | 351 items => |
356 { | |
357 browser.storage.managed.get(null, items => | |
358 { | 352 { |
359 // Opera doesn't support browser.storage.managed, but instead simply | |
360 // removing the API, Opera sets browser.runtime.lastError when using it. | |
361 // So we have to retrieve that error, to prevent it from showing up | |
362 // in the console. | |
363 browser.runtime.lastError; | |
364 | |
365 for (let key in items) | 353 for (let key in items) |
366 defaults[key] = items[key]; | 354 defaults[key] = items[key]; |
367 | 355 }, |
368 resolve(); | 356 |
369 }); | 357 // Opera doesn't support browser.storage.managed, but instead of simply |
370 } | 358 // removing the API, it gives an asynchronous error which we ignore here. |
371 else | 359 () => {} |
372 { | 360 ); |
373 resolve(); | 361 } |
374 } | 362 else |
375 }); | 363 { |
| 364 managedLoaded = Promise.resolve(); |
| 365 } |
376 | 366 |
377 function onLoaded() | 367 function onLoaded() |
378 { | 368 { |
379 ext.storage.onChanged.addListener(changes => | 369 browser.storage.onChanged.addListener(changes => |
380 { | 370 { |
381 for (let key in changes) | 371 for (let key in changes) |
382 { | 372 { |
383 let pref = keyToPref(key); | 373 let pref = keyToPref(key); |
384 if (pref && pref in defaults) | 374 if (pref && pref in defaults) |
385 { | 375 { |
386 let change = changes[key]; | 376 let change = changes[key]; |
387 if ("newValue" in change && change.newValue != defaults[pref]) | 377 if ("newValue" in change && change.newValue != defaults[pref]) |
388 overrides[pref] = change.newValue; | 378 overrides[pref] = change.newValue; |
389 else | 379 else |
390 delete overrides[pref]; | 380 delete overrides[pref]; |
391 | 381 |
392 eventEmitter.emit(pref); | 382 eventEmitter.emit(pref); |
393 } | 383 } |
394 } | 384 } |
395 }); | 385 }); |
396 } | 386 } |
397 | 387 |
398 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); | 388 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); |
399 } | 389 } |
400 | 390 |
401 init(); | 391 init(); |
LEFT | RIGHT |