Left: | ||
Right: |
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-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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 * @type {number} | 207 * @type {number} |
208 */ | 208 */ |
209 defaults.last_updates_page_displayed = 0; | 209 defaults.last_updates_page_displayed = 0; |
210 | 210 |
211 /** | 211 /** |
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. | |
218 * | |
219 * @param {string} preference | |
220 * @param {any} value | |
221 * @return {Promise} A promise that resolves when the underlying | |
222 browser.storage.local.set() operation completes | |
223 */ | |
224 set(preference, value) | |
225 { | |
226 let defaultValue = defaults[preference]; | |
227 | |
228 if (typeof value != typeof defaultValue) | |
229 throw new Error("Attempt to change preference type"); | |
230 | |
231 if (value == defaultValue) | |
232 { | |
233 delete overrides[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 } | |
236 | |
237 overrides[preference] = value; | |
238 return (customSave.get(preference) || savePref)(preference); | |
239 }, | |
240 | |
241 /** | |
217 * Adds a callback that is called when the | 242 * Adds a callback that is called when the |
218 * value of a specified preference changed. | 243 * value of a specified preference changed. |
219 * | 244 * |
220 * @param {string} preference | 245 * @param {string} preference |
221 * @param {function} callback | 246 * @param {function} callback |
222 */ | 247 */ |
223 on(preference, callback) | 248 on(preference, callback) |
224 { | 249 { |
225 eventEmitter.on(preference, callback); | 250 eventEmitter.on(preference, callback); |
226 }, | 251 }, |
(...skipping 27 matching lines...) Expand all Loading... | |
254 return key.substr(keyPrefix.length); | 279 return key.substr(keyPrefix.length); |
255 } | 280 } |
256 | 281 |
257 function prefToKey(pref) | 282 function prefToKey(pref) |
258 { | 283 { |
259 return keyPrefix + pref; | 284 return keyPrefix + pref; |
260 } | 285 } |
261 | 286 |
262 function savePref(pref) | 287 function savePref(pref) |
263 { | 288 { |
264 ext.storage.set(prefToKey(pref), overrides[pref]); | 289 return browser.storage.local.set(prefToKey(pref), overrides[pref]); |
kzar
2018/04/24 16:18:46
As discussed in IRC let's replace ext.storage with
Sebastian Noack
2018/04/24 17:49:25
There we go: https://codereview.adblockplus.org/29
| |
265 } | 290 } |
266 | 291 |
267 let customSave = new Map(); | 292 let customSave = new Map(); |
268 if (require("../buildtools/info").platform == "gecko") | 293 if (require("../buildtools/info").platform == "gecko") |
269 { | 294 { |
270 // 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. |
271 // 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 |
272 // saved frequently as a side-effect. | 297 // saved frequently as a side-effect. |
273 const MIN_UPDATE_INTERVAL = 60 * 1000; | 298 const MIN_UPDATE_INTERVAL = 60 * 1000; |
274 let lastUpdate = -MIN_UPDATE_INTERVAL; | 299 let lastUpdate = -MIN_UPDATE_INTERVAL; |
275 let updateScheduled = false; | 300 let updateScheduled = false; |
276 customSave.set("blocked_total", pref => | 301 customSave.set("blocked_total", pref => |
277 { | 302 { |
278 if (updateScheduled) | 303 if (!updateScheduled) |
279 return; | 304 { |
305 let callback = () => | |
306 { | |
307 lastUpdate = performance.now(); | |
308 updateScheduled = false; | |
309 savePref(pref); | |
310 }; | |
280 | 311 |
281 let callback = () => | 312 let timeElapsed = performance.now() - lastUpdate; |
282 { | 313 if (timeElapsed < MIN_UPDATE_INTERVAL) |
283 lastUpdate = performance.now(); | 314 { |
284 updateScheduled = false; | 315 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed); |
285 savePref(pref); | 316 updateScheduled = true; |
286 }; | 317 } |
287 | 318 else |
288 let timeElapsed = performance.now() - lastUpdate; | 319 callback(); |
289 if (timeElapsed < MIN_UPDATE_INTERVAL) | |
290 { | |
291 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed); | |
292 updateScheduled = true; | |
293 } | 320 } |
294 else | 321 return Promise.resolve(); |
295 callback(); | |
296 }); | 322 }); |
297 } | 323 } |
298 | 324 |
299 function addPreference(pref) | 325 function addPreference(pref) |
300 { | 326 { |
301 Object.defineProperty(Prefs, pref, { | 327 Object.defineProperty(Prefs, pref, { |
302 get() { return (pref in overrides ? overrides : defaults)[pref]; }, | 328 get() { return (pref in overrides ? overrides : defaults)[pref]; }, |
303 set(value) | 329 set(value) |
304 { | 330 { |
305 let defaultValue = defaults[pref]; | 331 Prefs.set(pref, value); |
306 | |
307 if (typeof value != typeof defaultValue) | |
308 throw new Error("Attempt to change preference type"); | |
309 | |
310 if (value == defaultValue) | |
311 { | |
312 delete overrides[pref]; | |
313 ext.storage.remove(prefToKey(pref)); | |
314 } | |
315 else | |
316 { | |
317 overrides[pref] = value; | |
318 (customSave.get(pref) || savePref)(pref); | |
319 } | |
320 }, | 332 }, |
321 enumerable: true | 333 enumerable: true |
322 }); | 334 }); |
323 } | 335 } |
324 | 336 |
325 function init() | 337 function init() |
326 { | 338 { |
327 let prefs = Object.keys(defaults); | 339 let prefs = Object.keys(defaults); |
328 prefs.forEach(addPreference); | 340 prefs.forEach(addPreference); |
329 | 341 |
330 let localLoaded = new Promise(resolve => | 342 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then(items = > |
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.
| |
331 { | 343 { |
332 ext.storage.get(prefs.map(prefToKey), items => | 344 for (let key in items) |
333 { | 345 overrides[keyToPref(key)] = items[key]; |
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
| |
334 for (let key in items) | |
335 overrides[keyToPref(key)] = items[key]; | |
336 | |
337 resolve(); | |
338 }); | |
339 }); | 346 }); |
340 | 347 |
341 let managedLoaded = new Promise(resolve => | 348 let managedLoaded = new Promise(resolve => |
342 { | 349 { |
343 if ("managed" in browser.storage) | 350 if ("managed" in browser.storage) |
344 { | 351 { |
345 browser.storage.managed.get(null, items => | 352 browser.storage.managed.get(null, items => |
346 { | 353 { |
347 // Opera doesn't support browser.storage.managed, but instead simply | 354 // Opera doesn't support browser.storage.managed, but instead simply |
348 // removing the API, Opera sets browser.runtime.lastError when using it. | 355 // removing the API, Opera sets browser.runtime.lastError when using it. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 eventEmitter.emit(pref); | 387 eventEmitter.emit(pref); |
381 } | 388 } |
382 } | 389 } |
383 }); | 390 }); |
384 } | 391 } |
385 | 392 |
386 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); | 393 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); |
387 } | 394 } |
388 | 395 |
389 init(); | 396 init(); |
OLD | NEW |