Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/prefs.js

Issue 29621569: Issue 6050 - Use localStorage for blocked_total preference on Gecko (Closed)
Patch Set: Created Nov. 27, 2017, 6:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 } 252 }
253 253
254 function savePref(pref) 254 function savePref(pref)
255 { 255 {
256 ext.storage.set(prefToKey(pref), overrides[pref]); 256 ext.storage.set(prefToKey(pref), overrides[pref]);
257 } 257 }
258 258
259 let customSave = new Map(); 259 let customSave = new Map();
260 if (require("info").platform == "gecko") 260 if (require("info").platform == "gecko")
261 { 261 {
262 // Saving one storage value causes all others to be saved as well on Gecko. 262 // Saving one browser.storage value causes all others to be saved as well on
263 // Make sure that updating ad counter doesn't cause the filters data to be 263 // Gecko. Until that is rectified[1] we need to avoid saving the ad counter
264 // saved frequently as a side-effect. 264 // using browser.storage too often, since the filters data is saved as a
265 const MIN_UPDATE_INTERVAL = 60 * 1000; 265 // side-effect. We do this by using localStorage instead, only updating the
266 let lastUpdate = -MIN_UPDATE_INTERVAL; 266 // value saved in browser.storage when the extension first initializes.
267 let updateScheduled = false; 267 // Note: This workaround is sub-optimal since if the user clears browser data
268 // localStorage is cleared.
269 // [1] - https://bugzilla.mozilla.org/show_bug.cgi?id=1277612
268 customSave.set("blocked_total", pref => 270 customSave.set("blocked_total", pref =>
269 { 271 {
270 if (updateScheduled) 272 window.localStorage.setItem("blocked_total", overrides.blocked_total);
271 return;
272
273 let callback = () =>
274 {
275 lastUpdate = performance.now();
276 updateScheduled = false;
277 savePref(pref);
278 };
279
280 let timeElapsed = performance.now() - lastUpdate;
281 if (timeElapsed < MIN_UPDATE_INTERVAL)
282 {
283 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed);
284 updateScheduled = true;
285 }
286 else
287 callback();
288 }); 273 });
289 } 274 }
290 275
291 function addPreference(pref) 276 function addPreference(pref)
292 { 277 {
293 Object.defineProperty(Prefs, pref, { 278 Object.defineProperty(Prefs, pref, {
294 get() { return (pref in overrides ? overrides : defaults)[pref]; }, 279 get() { return (pref in overrides ? overrides : defaults)[pref]; },
295 set(value) 280 set(value)
296 { 281 {
297 let defaultValue = defaults[pref]; 282 let defaultValue = defaults[pref];
(...skipping 21 matching lines...) Expand all
319 let prefs = Object.keys(defaults); 304 let prefs = Object.keys(defaults);
320 prefs.forEach(addPreference); 305 prefs.forEach(addPreference);
321 306
322 let localLoaded = new Promise(resolve => 307 let localLoaded = new Promise(resolve =>
323 { 308 {
324 ext.storage.get(prefs.map(prefToKey), items => 309 ext.storage.get(prefs.map(prefToKey), items =>
325 { 310 {
326 for (let key in items) 311 for (let key in items)
327 overrides[keyToPref(key)] = items[key]; 312 overrides[keyToPref(key)] = items[key];
328 313
314 if (require("info").platform == "gecko")
315 {
316 let blockedTotal = window.localStorage.getItem("blocked_total");
317 if (typeof blockedTotal == "string")
318 {
319 blockedTotal = JSON.parse(blockedTotal);
320 if (blockedTotal > overrides.blocked_total)
321 {
322 overrides.blocked_total = blockedTotal;
323 savePref("blocked_total");
324 }
325 }
326 }
327
329 resolve(); 328 resolve();
330 }); 329 });
331 }); 330 });
332 331
333 let managedLoaded = new Promise(resolve => 332 let managedLoaded = new Promise(resolve =>
334 { 333 {
335 if (require("info").platform == "chromium" && "managed" in browser.storage) 334 if (require("info").platform == "chromium" && "managed" in browser.storage)
336 { 335 {
337 browser.storage.managed.get(null, items => 336 browser.storage.managed.get(null, items =>
338 { 337 {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 eventEmitter.emit(pref); 371 eventEmitter.emit(pref);
373 } 372 }
374 } 373 }
375 }); 374 });
376 } 375 }
377 376
378 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); 377 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded);
379 } 378 }
380 379
381 init(); 380 init();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld