Left: | ||
Right: |
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 { |
233 delete overrides[preference]; | 233 delete overrides[preference]; |
234 return browser.storage.local.remove(prefToKey(preference)); | 234 return browser.storage.local.remove(prefToKey(preference)); |
kzar
2018/04/24 16:18:46
Nit: Since we're just calling browser.storage.loca
Sebastian Noack
2018/04/24 17:49:25
Well, nothing changed in this regard. Except for r
| |
235 } | 235 } |
236 | 236 |
237 overrides[preference] = value; | 237 overrides[preference] = value; |
238 return (customSave.get(preference) || savePref)(preference); | 238 return (customSave.get(preference) || savePref)(preference); |
239 }, | 239 }, |
240 | 240 |
241 /** | 241 /** |
242 * Adds a callback that is called when the | 242 * Adds a callback that is called when the |
243 * value of a specified preference changed. | 243 * value of a specified preference changed. |
244 * | 244 * |
(...skipping 34 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 = browser.storage.local.get(prefs.map(prefToKey)).then(items = > | 339 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then( |
kzar
2018/04/24 16:18:46
Nit: This line is too long in Rietveld, did it pas
Sebastian Noack
2018/04/24 17:49:25
This is no longer the case after the rebase.
kzar
2018/04/25 10:53:51
Acknowledged.
| |
343 { | 340 items => |
344 for (let key in items) | 341 { |
345 overrides[keyToPref(key)] = items[key]; | 342 for (let key in items) |
kzar
2018/04/24 16:18:46
We just had the key above, so is there not a way w
Sebastian Noack
2018/04/24 17:49:25
What do you have in mind?
let keys = prefs.map(
kzar
2018/04/25 10:53:51
Yea, fair enough I can't see a much better way to
kzar
2018/04/25 10:59:07
(I had assumed that browser.storage.local.get woul
| |
346 }); | 343 overrides[keyToPref(key)] = items[key]; |
347 | 344 } |
348 let managedLoaded = new Promise(resolve => | 345 ); |
349 { | 346 |
350 if ("managed" in browser.storage) | 347 let managedLoaded; |
351 { | 348 if ("managed" in browser.storage) |
352 browser.storage.managed.get(null, items => | 349 { |
350 managedLoaded = browser.storage.managed.get(null).then( | |
351 items => | |
353 { | 352 { |
354 // Opera doesn't support browser.storage.managed, but instead simply | |
355 // removing the API, Opera sets browser.runtime.lastError when using it. | |
356 // So we have to retrieve that error, to prevent it from showing up | |
357 // in the console. | |
358 browser.runtime.lastError; | |
359 | |
360 for (let key in items) | 353 for (let key in items) |
361 defaults[key] = items[key]; | 354 defaults[key] = items[key]; |
362 | 355 }, |
363 resolve(); | 356 |
364 }); | 357 // Opera doesn't support browser.storage.managed, but instead of simply |
365 } | 358 // removing the API, it gives an asynchronous error which we ignore here. |
366 else | 359 () => {} |
367 { | 360 ); |
368 resolve(); | 361 } |
369 } | 362 else |
370 }); | 363 { |
364 managedLoaded = Promise.resolve(); | |
365 } | |
371 | 366 |
372 function onLoaded() | 367 function onLoaded() |
373 { | 368 { |
374 ext.storage.onChanged.addListener(changes => | 369 browser.storage.onChanged.addListener(changes => |
375 { | 370 { |
376 for (let key in changes) | 371 for (let key in changes) |
377 { | 372 { |
378 let pref = keyToPref(key); | 373 let pref = keyToPref(key); |
379 if (pref && pref in defaults) | 374 if (pref && pref in defaults) |
380 { | 375 { |
381 let change = changes[key]; | 376 let change = changes[key]; |
382 if ("newValue" in change && change.newValue != defaults[pref]) | 377 if ("newValue" in change && change.newValue != defaults[pref]) |
383 overrides[pref] = change.newValue; | 378 overrides[pref] = change.newValue; |
384 else | 379 else |
385 delete overrides[pref]; | 380 delete overrides[pref]; |
386 | 381 |
387 eventEmitter.emit(pref); | 382 eventEmitter.emit(pref); |
388 } | 383 } |
389 } | 384 } |
390 }); | 385 }); |
391 } | 386 } |
392 | 387 |
393 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); | 388 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); |
394 } | 389 } |
395 | 390 |
396 init(); | 391 init(); |
LEFT | RIGHT |