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: Don't call both successCallback and errorCallback Created Oct. 11, 2016, 1:50 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 // Make sure we do not have subscriptions in localStorage from older version s first
364 var entry = localStorage.getItem(key);
365 if (typeof entry == "string")
363 { 366 {
364 var key = fileToKey(file); 367 try
365 ext.storage.get([key], function(items)
366 { 368 {
367 var entry = items[key]; 369 entry = JSON.parse(entry);
368 if (!entry) 370 }
369 { 371 catch(err)
370 try 372 {
371 { 373 setTimeout(errorCallback(new Error("File is corrupted")));
372 entry = JSON.parse(window.localStorage.getItem(key)); 374 return;
373 } 375 }
374 catch (err) 376 setTimeout(successCallback(entry));
375 {} 377 return;
376 } 378 }
377 if (entry) 379 // Now try to read from IndexedDB
378 { 380 localforage.getItem(key, function(err, value)
379 resolve(entry); 381 {
380 } 382 if (err || !value)
381 else 383 errorCallback(new Error("File doesn't exist"));
382 { 384 else
383 reject(new Error("File doesn't exist")); 385 successCallback(value);
384 } 386 });
385 });
386 }.bind(this));
387 } 387 }
388
388 function saveFile(file, data, callback) 389 function saveFile(file, data, callback)
389 { 390 {
390 var entry = {};
391 var key = fileToKey(file); 391 var key = fileToKey(file);
392 var entry = {
393 lastModified: Date.now(),
394 content: data
395 };
392 396
393 if (typeof browser == "undefined") 397 localStorage.removeItem(key);
394 { 398 localforage.setItem(key, entry, callback);
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, onLoaded, callback);
439 }, 420 },
440 writeToFile: function(file, data, callback) 421 writeToFile: function(file, data, callback)
441 { 422 {
442 saveFile(file, data, callback); 423 saveFile(file, data, callback);
443 }, 424 },
444 copyFile: function(fromFile, toFile, callback) 425 copyFile: function(fromFile, toFile, callback)
445 { 426 {
446 function onLoaded(entry) 427 function onLoaded(entry)
447 { 428 {
448 saveFile(toFile, entry.content, callback); 429 saveFile(toFile, entry.content, callback);
449 } 430 }
450 loadFile(file).then(onLoaded, callback); 431 loadFile(file, onLoaded, callback);
451 }, 432 },
452 renameFile: function(fromFile, newName, callback) 433 renameFile: function(fromFile, newName, callback)
453 { 434 {
454 function onLoaded() 435 function onLoaded()
455 { 436 {
456 ext.storage.remove(fileToKey(fromFile), function() 437 ext.storage.remove(fileToKey(fromFile), function()
457 { 438 {
458 ext.storage.set(keyPrefix + newName, entry, callback); 439 ext.storage.set(keyPrefix + newName, entry, callback);
459 }); 440 });
460 } 441 }
461 loadFile(file).then(onLoaded, callback); 442 loadFile(file, onLoaded, callback);
462 }, 443 },
463 removeFile: function(file, callback) 444 removeFile: function(file, callback)
464 { 445 {
465 ext.storage.remove(fileToKey(file), callback); 446 ext.storage.remove(fileToKey(file), callback);
466 }, 447 },
467 statFile: function(file, callback) 448 statFile: function(file, callback)
468 { 449 {
469 function onLoaded(entry) 450 function onLoaded(entry)
470 { 451 {
471 callback(null, 452 callback(null,
472 { 453 {
473 exists: true, 454 exists: true,
474 lastModified: entry.lastModified 455 lastModified: entry.lastModified
475 }); 456 });
476 } 457 }
477 loadFile(file).then(onLoaded, callback); 458 loadFile(file, onLoaded, callback);
478 } 459 }
479 }; 460 };
480 return exports; 461 return exports;
481 })(); 462 })();
482 require.scopes["downloader"] = (function() 463 require.scopes["downloader"] = (function()
483 { 464 {
484 var exports = {}; 465 var exports = {};
485 var Utils = require("utils").Utils; 466 var Utils = require("utils").Utils;
486 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; 467 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000;
487 var MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; 468 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)); 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