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

Side by Side Diff: lib/adblockplus.js

Issue 29355962: Issue 4023 - Use localforage (IndexedDB) instead of localStorage (Closed)
Patch Set: Patch set 1 - Address the comments Created Oct. 5, 2016, 1:28 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 | « background.html ('k') | qunit/index.html » ('j') | 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-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 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 } 358 }
359 359
360 function loadFile(file) 360 function loadFile(file)
361 { 361 {
362 return new Promise(function(resolve, reject) 362 return new Promise(function(resolve, reject)
363 { 363 {
364 var key = fileToKey(file); 364 var key = fileToKey(file);
365 ext.storage.get([key], function(items) 365 ext.storage.get([key], function(items)
366 { 366 {
367 var entry = items[key]; 367 var entry = items[key];
368 if (!entry)
369 {
370 try
371 {
372 entry = JSON.parse(window.localStorage.getItem(key));
373 }
374 catch (err)
375 {}
376 }
377 if (entry) 368 if (entry)
378 { 369 {
379 resolve(entry); 370 resolve(entry);
380 } 371 }
381 else 372 else
382 { 373 {
383 reject(new Error("File doesn't exist")); 374 try
375 {
376 entry = localforage.getItem(key, function(err, value)
377 {
378 if (err || !value)
379 reject(new Error("File doesn't exist"));
380 else
381 resolve(value);
382 });
383 }
384 catch (err)
385 {}
384 } 386 }
385 }); 387 });
386 }.bind(this)); 388 }.bind(this));
387 } 389 }
388 function saveFile(file, data, callback) 390 function saveFile(file, data, callback)
389 { 391 {
390 var entry = {}; 392 var entry = {
391 var key = fileToKey(file); 393 lastModified: Date.now(),
394 content: data
395 };
392 396
393 if (typeof browser == "undefined") 397 ext.storage.remove(fileToKey(file));
394 { 398 localforage.setItem(key, entry, callback);
kzar 2016/10/05 13:46:53 Did you test this? Isn't `key` undefined now?
kzar 2016/10/05 13:46:53 Why not just pass in `{lastModified: Date.now(), c
Oleksandr 2016/10/05 21:07:40 I am not sure this is prettier, but I don't have a
Sebastian Noack 2016/10/05 21:27:31 Also considering that we'd have to wrap this line
Sebastian Noack 2016/10/05 21:28:38 ...as it was, I meant.
Oleksandr 2016/10/05 21:49:12 Done.
395 entry[key] = {
396 lastModified: Date.now(),
397 content: data
398 };
399 ext.storage.set(entry, callback);
400 }
401 else
402 {
403 var processedData = LZString.compressToUTF16(JSON.stringify(data));
404 ext.storage.remove(key);
405 entry[key] = {
406 lastModified: Date.now(),
407 content: processedData,
408 compressed: true
409 };
410 window.localStorage.setItem(key, JSON.stringify(entry[key]));
411 setTimeout(callback, 0);
412 }
413 callback();
414 } 399 }
415 exports.IO = { 400 exports.IO = {
416 resolveFilePath: function(path) 401 resolveFilePath: function(path)
417 { 402 {
418 return new FakeFile(path); 403 return new FakeFile(path);
419 }, 404 },
420 readFromFile: function(file, listener, callback) 405 readFromFile: function(file, listener, callback)
421 { 406 {
422 function onLoaded(entry) 407 function onLoaded(entry)
423 { 408 {
424 if ("content" in entry) 409 if ("content" in entry)
425 { 410 {
426 if (entry["compressed"])
427 {
428 entry.content = JSON.parse(LZString.decompressFromUTF16(entry.conten t));
429 }
430 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15) 411 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15)
431 { 412 {
432 var line = entry.content[_loopIndex15]; 413 var line = entry.content[_loopIndex15];
433 listener.process(line); 414 listener.process(line);
434 } 415 }
435 } 416 }
436 callback(null); 417 callback(null);
437 } 418 }
438 loadFile(file).then(onLoaded, callback); 419 loadFile(file).then(onLoaded, callback);
439 }, 420 },
(...skipping 6136 matching lines...) Expand 10 before | Expand all | Expand 10 after
6576 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount)); 6557 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount));
6577 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&")); 6558 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&"));
6578 } 6559 }
6579 if ("setUninstallURL" in chrome.runtime) 6560 if ("setUninstallURL" in chrome.runtime)
6580 { 6561 {
6581 Prefs.untilLoaded.then(setUninstallURL); 6562 Prefs.untilLoaded.then(setUninstallURL);
6582 Prefs.on("notificationdata", setUninstallURL); 6563 Prefs.on("notificationdata", setUninstallURL);
6583 } 6564 }
6584 return exports; 6565 return exports;
6585 })(); 6566 })();
OLDNEW
« no previous file with comments | « background.html ('k') | qunit/index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld