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: Get back to async loadFile. Don't return a promise. Check localStorage. Created Oct. 5, 2016, 10:55 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 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 require.scopes["io"] = (function() 350 require.scopes["io"] = (function()
351 { 351 {
352 var exports = {}; 352 var exports = {};
353 var keyPrefix = "file:"; 353 var keyPrefix = "file:";
354 354
355 function fileToKey(file) 355 function fileToKey(file)
356 { 356 {
357 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); 357 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec);
358 } 358 }
359 359
360 function loadFile(file) 360 function loadFile(file, successCallback, errorCallback)
361 { 361 {
362 return new Promise(function(resolve, reject) 362 var key = fileToKey(file);
363 var entry = localStorage.getItem(key);
364 if (entry)
363 { 365 {
364 var key = fileToKey(file); 366 successCallback(JSON.parse(entry));
Sebastian Noack 2016/10/06 12:07:36 Don't we have to respond asynchronously here (i.e.
Oleksandr 2016/10/06 23:28:13 Indeed, we should. Great catch! For reference, thi
Sebastian Noack 2016/10/06 23:38:35 Yes, that is what I remember, and why I brought it
365 ext.storage.get([key], function(items)
366 {
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)
378 {
379 resolve(entry);
380 }
381 else
382 {
383 reject(new Error("File doesn't exist"));
384 }
385 });
386 }.bind(this));
387 }
388 function saveFile(file, data, callback)
389 {
390 var entry = {};
391 var key = fileToKey(file);
392
393 if (typeof browser == "undefined")
394 {
395 entry[key] = {
396 lastModified: Date.now(),
397 content: data
398 };
399 ext.storage.set(entry, callback);
400 } 367 }
401 else 368 else
402 { 369 {
403 var processedData = LZString.compressToUTF16(JSON.stringify(data)); 370 entry = localforage.getItem(key, function(err, value)
Sebastian Noack 2016/10/06 12:07:36 It seems unnecessary to assign to "entry" here. It
Oleksandr 2016/10/06 23:28:13 Done.
404 ext.storage.remove(key); 371 {
405 entry[key] = { 372 if (err || !value)
406 lastModified: Date.now(), 373 errorCallback(new Error("File doesn't exist"));
407 content: processedData, 374 else
408 compressed: true 375 successCallback(value);
409 }; 376 });
410 window.localStorage.setItem(key, JSON.stringify(entry[key]));
411 setTimeout(callback, 0);
412 } 377 }
413 callback(); 378 }
379
380 function saveFile(file, data, callback)
381 {
382 var key = fileToKey(file);
383 var entry = {
384 lastModified: Date.now(),
385 content: data
386 };
387
388 localStorage.removeItem(key);
389 localforage.setItem(key, entry, callback);
414 } 390 }
415 exports.IO = { 391 exports.IO = {
416 resolveFilePath: function(path) 392 resolveFilePath: function(path)
417 { 393 {
418 return new FakeFile(path); 394 return new FakeFile(path);
419 }, 395 },
420 readFromFile: function(file, listener, callback) 396 readFromFile: function(file, listener, callback)
421 { 397 {
422 function onLoaded(entry) 398 function onLoaded(entry)
423 { 399 {
424 if ("content" in entry) 400 if ("content" in entry)
425 { 401 {
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) 402 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15)
431 { 403 {
432 var line = entry.content[_loopIndex15]; 404 var line = entry.content[_loopIndex15];
433 listener.process(line); 405 listener.process(line);
434 } 406 }
435 } 407 }
436 callback(null); 408 callback(null);
437 } 409 }
438 loadFile(file).then(onLoaded, callback); 410 loadFile(file, onLoaded, callback);
439 }, 411 },
440 writeToFile: function(file, data, callback) 412 writeToFile: function(file, data, callback)
441 { 413 {
442 saveFile(file, data, callback); 414 saveFile(file, data, callback);
443 }, 415 },
444 copyFile: function(fromFile, toFile, callback) 416 copyFile: function(fromFile, toFile, callback)
445 { 417 {
446 function onLoaded(entry) 418 function onLoaded(entry)
447 { 419 {
448 saveFile(toFile, entry.content, callback); 420 saveFile(toFile, entry.content, callback);
449 } 421 }
450 loadFile(file).then(onLoaded, callback); 422 loadFile(file, onLoaded, callback);
451 }, 423 },
452 renameFile: function(fromFile, newName, callback) 424 renameFile: function(fromFile, newName, callback)
453 { 425 {
454 function onLoaded() 426 function onLoaded()
455 { 427 {
456 ext.storage.remove(fileToKey(fromFile), function() 428 ext.storage.remove(fileToKey(fromFile), function()
457 { 429 {
458 ext.storage.set(keyPrefix + newName, entry, callback); 430 ext.storage.set(keyPrefix + newName, entry, callback);
459 }); 431 });
460 } 432 }
461 loadFile(file).then(onLoaded, callback); 433 loadFile(file, onLoaded, callback);
462 }, 434 },
463 removeFile: function(file, callback) 435 removeFile: function(file, callback)
464 { 436 {
465 ext.storage.remove(fileToKey(file), callback); 437 ext.storage.remove(fileToKey(file), callback);
466 }, 438 },
467 statFile: function(file, callback) 439 statFile: function(file, callback)
468 { 440 {
469 function onLoaded(entry) 441 function onLoaded(entry)
470 { 442 {
471 callback(null, 443 callback(null,
472 { 444 {
473 exists: true, 445 exists: true,
474 lastModified: entry.lastModified 446 lastModified: entry.lastModified
475 }); 447 });
476 } 448 }
477 loadFile(file).then(onLoaded, callback); 449 loadFile(file, onLoaded, callback);
478 } 450 }
479 }; 451 };
480 return exports; 452 return exports;
481 })(); 453 })();
482 require.scopes["downloader"] = (function() 454 require.scopes["downloader"] = (function()
483 { 455 {
484 var exports = {}; 456 var exports = {};
485 var Utils = require("utils").Utils; 457 var Utils = require("utils").Utils;
486 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; 458 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000;
487 var MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; 459 var MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND;
(...skipping 6088 matching lines...) Expand 10 before | Expand all | Expand 10 after
6576 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount)); 6548 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount));
6577 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&")); 6549 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&"));
6578 } 6550 }
6579 if ("setUninstallURL" in chrome.runtime) 6551 if ("setUninstallURL" in chrome.runtime)
6580 { 6552 {
6581 Prefs.untilLoaded.then(setUninstallURL); 6553 Prefs.untilLoaded.then(setUninstallURL);
6582 Prefs.on("notificationdata", setUninstallURL); 6554 Prefs.on("notificationdata", setUninstallURL);
6583 } 6555 }
6584 return exports; 6556 return exports;
6585 })(); 6557 })();
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