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

Delta Between Two Patch Sets: lib/adblockplus.js

Issue 29355962: Issue 4023 - Use localforage (IndexedDB) instead of localStorage (Closed)
Left Patch Set: Simplify the code. Fix the "undefined" bug. Created Oct. 5, 2016, 9:02 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « background.html ('k') | qunit/index.html » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 { 363 // Make sure we do not have subscriptions in localStorage from older version s first
364 var key = fileToKey(file); 364 var entry = localStorage.getItem(key);
365 ext.storage.get([key], function(items) 365 if (typeof entry == "string")
366 { 366 {
367 var entry = items[key]; 367 try
368 if (entry) 368 {
369 { 369 entry = JSON.parse(entry);
370 resolve(entry); 370 }
371 } 371 catch(err)
372 else 372 {
373 { 373 setTimeout(errorCallback(new Error("File is corrupted")));
374 try 374 return;
375 { 375 }
376 entry = localforage.getItem(key, function(err, value) 376 setTimeout(successCallback(entry));
377 { 377 return;
378 if (err || !value) 378 }
379 reject(new Error("File doesn't exist")); 379 // Now try to read from IndexedDB
380 else 380 localforage.getItem(key, function(err, value)
381 resolve(value); 381 {
382 }); 382 if (err || !value)
383 } 383 errorCallback(new Error("File doesn't exist"));
384 catch (err) 384 else
385 {} 385 successCallback(value);
386 } 386 });
387 }); 387 }
388 }.bind(this)); 388
389 }
390 function saveFile(file, data, callback) 389 function saveFile(file, data, callback)
391 { 390 {
392 var key = fileToKey(file); 391 var key = fileToKey(file);
393 ext.storage.remove(key); 392 var entry = {
394 localforage.setItem(key, 393 lastModified: Date.now(),
395 { lastModified: Date.now(), content: data }, 394 content: data
396 callback 395 };
397 ); 396
397 localStorage.removeItem(key);
398 localforage.setItem(key, entry, callback);
398 } 399 }
399 exports.IO = { 400 exports.IO = {
400 resolveFilePath: function(path) 401 resolveFilePath: function(path)
401 { 402 {
402 return new FakeFile(path); 403 return new FakeFile(path);
403 }, 404 },
404 readFromFile: function(file, listener, callback) 405 readFromFile: function(file, listener, callback)
405 { 406 {
406 function onLoaded(entry) 407 function onLoaded(entry)
407 { 408 {
408 if ("content" in entry) 409 if ("content" in entry)
409 { 410 {
410 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15) 411 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15)
411 { 412 {
412 var line = entry.content[_loopIndex15]; 413 var line = entry.content[_loopIndex15];
413 listener.process(line); 414 listener.process(line);
414 } 415 }
415 } 416 }
416 callback(null); 417 callback(null);
417 } 418 }
418 loadFile(file).then(onLoaded, callback); 419 loadFile(file, onLoaded, callback);
419 }, 420 },
420 writeToFile: function(file, data, callback) 421 writeToFile: function(file, data, callback)
421 { 422 {
422 saveFile(file, data, callback); 423 saveFile(file, data, callback);
423 }, 424 },
424 copyFile: function(fromFile, toFile, callback) 425 copyFile: function(fromFile, toFile, callback)
425 { 426 {
426 function onLoaded(entry) 427 function onLoaded(entry)
427 { 428 {
428 saveFile(toFile, entry.content, callback); 429 saveFile(toFile, entry.content, callback);
429 } 430 }
430 loadFile(file).then(onLoaded, callback); 431 loadFile(file, onLoaded, callback);
431 }, 432 },
432 renameFile: function(fromFile, newName, callback) 433 renameFile: function(fromFile, newName, callback)
433 { 434 {
434 function onLoaded() 435 function onLoaded()
435 { 436 {
436 ext.storage.remove(fileToKey(fromFile), function() 437 ext.storage.remove(fileToKey(fromFile), function()
437 { 438 {
438 ext.storage.set(keyPrefix + newName, entry, callback); 439 ext.storage.set(keyPrefix + newName, entry, callback);
439 }); 440 });
440 } 441 }
441 loadFile(file).then(onLoaded, callback); 442 loadFile(file, onLoaded, callback);
442 }, 443 },
443 removeFile: function(file, callback) 444 removeFile: function(file, callback)
444 { 445 {
445 ext.storage.remove(fileToKey(file), callback); 446 ext.storage.remove(fileToKey(file), callback);
446 }, 447 },
447 statFile: function(file, callback) 448 statFile: function(file, callback)
448 { 449 {
449 function onLoaded(entry) 450 function onLoaded(entry)
450 { 451 {
451 callback(null, 452 callback(null,
452 { 453 {
453 exists: true, 454 exists: true,
454 lastModified: entry.lastModified 455 lastModified: entry.lastModified
455 }); 456 });
456 } 457 }
457 loadFile(file).then(onLoaded, callback); 458 loadFile(file, onLoaded, callback);
458 } 459 }
459 }; 460 };
460 return exports; 461 return exports;
461 })(); 462 })();
462 require.scopes["downloader"] = (function() 463 require.scopes["downloader"] = (function()
463 { 464 {
464 var exports = {}; 465 var exports = {};
465 var Utils = require("utils").Utils; 466 var Utils = require("utils").Utils;
466 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; 467 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000;
467 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
6556 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount)); 6557 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount));
6557 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&")); 6558 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&"));
6558 } 6559 }
6559 if ("setUninstallURL" in chrome.runtime) 6560 if ("setUninstallURL" in chrome.runtime)
6560 { 6561 {
6561 Prefs.untilLoaded.then(setUninstallURL); 6562 Prefs.untilLoaded.then(setUninstallURL);
6562 Prefs.on("notificationdata", setUninstallURL); 6563 Prefs.on("notificationdata", setUninstallURL);
6563 } 6564 }
6564 return exports; 6565 return exports;
6565 })(); 6566 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld