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: Make sure we always return asynchronously from loadFile. Add try-catch block. Created Oct. 6, 2016, 11:26 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 try
365 ext.storage.get([key], function(items)
366 { 367 {
367 var entry = items[key]; 368 setTimeout(successCallback(JSON.parse(entry)), 0);
Sebastian Noack 2016/10/06 23:38:35 Not too important but you can call setTimout() wit
Oleksandr 2016/10/07 16:29:17 Done. But I think it wouldn't hurt catching an err
kzar 2016/10/07 17:00:00 Wouldn't this also catch an exception thrown by th
Oleksandr 2016/10/07 17:21:06 Yes, but I think that's expected, no?
368 if (!entry) 369 }
369 { 370 catch(err)
370 try 371 {
371 { 372 setTimeout(errorCallback(new Error("File is corrupted")), 0);
372 entry = JSON.parse(window.localStorage.getItem(key)); 373 }
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 } 374 }
401 else 375 else
402 { 376 {
403 var processedData = LZString.compressToUTF16(JSON.stringify(data)); 377 localforage.getItem(key, function(err, value)
404 ext.storage.remove(key); 378 {
405 entry[key] = { 379 if (err || !value)
406 lastModified: Date.now(), 380 errorCallback(new Error("File doesn't exist"));
407 content: processedData, 381 else
408 compressed: true 382 successCallback(value);
409 }; 383 });
410 window.localStorage.setItem(key, JSON.stringify(entry[key]));
411 setTimeout(callback, 0);
412 } 384 }
413 callback(); 385 }
386
387 function saveFile(file, data, callback)
388 {
389 var key = fileToKey(file);
390 var entry = {
391 lastModified: Date.now(),
392 content: data
393 };
394
395 localStorage.removeItem(key);
396 localforage.setItem(key, entry, callback);
414 } 397 }
415 exports.IO = { 398 exports.IO = {
416 resolveFilePath: function(path) 399 resolveFilePath: function(path)
417 { 400 {
418 return new FakeFile(path); 401 return new FakeFile(path);
419 }, 402 },
420 readFromFile: function(file, listener, callback) 403 readFromFile: function(file, listener, callback)
421 { 404 {
422 function onLoaded(entry) 405 function onLoaded(entry)
423 { 406 {
424 if ("content" in entry) 407 if ("content" in entry)
425 { 408 {
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) 409 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15)
431 { 410 {
432 var line = entry.content[_loopIndex15]; 411 var line = entry.content[_loopIndex15];
433 listener.process(line); 412 listener.process(line);
434 } 413 }
435 } 414 }
436 callback(null); 415 callback(null);
437 } 416 }
438 loadFile(file).then(onLoaded, callback); 417 loadFile(file, onLoaded, callback);
439 }, 418 },
440 writeToFile: function(file, data, callback) 419 writeToFile: function(file, data, callback)
441 { 420 {
442 saveFile(file, data, callback); 421 saveFile(file, data, callback);
443 }, 422 },
444 copyFile: function(fromFile, toFile, callback) 423 copyFile: function(fromFile, toFile, callback)
445 { 424 {
446 function onLoaded(entry) 425 function onLoaded(entry)
447 { 426 {
448 saveFile(toFile, entry.content, callback); 427 saveFile(toFile, entry.content, callback);
449 } 428 }
450 loadFile(file).then(onLoaded, callback); 429 loadFile(file, onLoaded, callback);
451 }, 430 },
452 renameFile: function(fromFile, newName, callback) 431 renameFile: function(fromFile, newName, callback)
453 { 432 {
454 function onLoaded() 433 function onLoaded()
455 { 434 {
456 ext.storage.remove(fileToKey(fromFile), function() 435 ext.storage.remove(fileToKey(fromFile), function()
457 { 436 {
458 ext.storage.set(keyPrefix + newName, entry, callback); 437 ext.storage.set(keyPrefix + newName, entry, callback);
459 }); 438 });
460 } 439 }
461 loadFile(file).then(onLoaded, callback); 440 loadFile(file, onLoaded, callback);
462 }, 441 },
463 removeFile: function(file, callback) 442 removeFile: function(file, callback)
464 { 443 {
465 ext.storage.remove(fileToKey(file), callback); 444 ext.storage.remove(fileToKey(file), callback);
466 }, 445 },
467 statFile: function(file, callback) 446 statFile: function(file, callback)
468 { 447 {
469 function onLoaded(entry) 448 function onLoaded(entry)
470 { 449 {
471 callback(null, 450 callback(null,
472 { 451 {
473 exists: true, 452 exists: true,
474 lastModified: entry.lastModified 453 lastModified: entry.lastModified
475 }); 454 });
476 } 455 }
477 loadFile(file).then(onLoaded, callback); 456 loadFile(file, onLoaded, callback);
478 } 457 }
479 }; 458 };
480 return exports; 459 return exports;
481 })(); 460 })();
482 require.scopes["downloader"] = (function() 461 require.scopes["downloader"] = (function()
483 { 462 {
484 var exports = {}; 463 var exports = {};
485 var Utils = require("utils").Utils; 464 var Utils = require("utils").Utils;
486 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; 465 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000;
487 var MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; 466 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)); 6555 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount));
6577 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&")); 6556 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&"));
6578 } 6557 }
6579 if ("setUninstallURL" in chrome.runtime) 6558 if ("setUninstallURL" in chrome.runtime)
6580 { 6559 {
6581 Prefs.untilLoaded.then(setUninstallURL); 6560 Prefs.untilLoaded.then(setUninstallURL);
6582 Prefs.on("notificationdata", setUninstallURL); 6561 Prefs.on("notificationdata", setUninstallURL);
6583 } 6562 }
6584 return exports; 6563 return exports;
6585 })(); 6564 })();
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