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: Patch set 1 - Address the comments Created Oct. 5, 2016, 1:28 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 {
391 var key = fileToKey(file);
392 var entry = { 392 var entry = {
393 lastModified: Date.now(), 393 lastModified: Date.now(),
394 content: data 394 content: data
395 }; 395 };
396 396
397 ext.storage.remove(fileToKey(file)); 397 localStorage.removeItem(key);
398 localforage.setItem(key, entry, callback); 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.
399 } 399 }
400 exports.IO = { 400 exports.IO = {
401 resolveFilePath: function(path) 401 resolveFilePath: function(path)
402 { 402 {
403 return new FakeFile(path); 403 return new FakeFile(path);
404 }, 404 },
405 readFromFile: function(file, listener, callback) 405 readFromFile: function(file, listener, callback)
406 { 406 {
407 function onLoaded(entry) 407 function onLoaded(entry)
408 { 408 {
409 if ("content" in entry) 409 if ("content" in entry)
410 { 410 {
411 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15) 411 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loo pIndex15)
412 { 412 {
413 var line = entry.content[_loopIndex15]; 413 var line = entry.content[_loopIndex15];
414 listener.process(line); 414 listener.process(line);
415 } 415 }
416 } 416 }
417 callback(null); 417 callback(null);
418 } 418 }
419 loadFile(file).then(onLoaded, callback); 419 loadFile(file, onLoaded, callback);
420 }, 420 },
421 writeToFile: function(file, data, callback) 421 writeToFile: function(file, data, callback)
422 { 422 {
423 saveFile(file, data, callback); 423 saveFile(file, data, callback);
424 }, 424 },
425 copyFile: function(fromFile, toFile, callback) 425 copyFile: function(fromFile, toFile, callback)
426 { 426 {
427 function onLoaded(entry) 427 function onLoaded(entry)
428 { 428 {
429 saveFile(toFile, entry.content, callback); 429 saveFile(toFile, entry.content, callback);
430 } 430 }
431 loadFile(file).then(onLoaded, callback); 431 loadFile(file, onLoaded, callback);
432 }, 432 },
433 renameFile: function(fromFile, newName, callback) 433 renameFile: function(fromFile, newName, callback)
434 { 434 {
435 function onLoaded() 435 function onLoaded()
436 { 436 {
437 ext.storage.remove(fileToKey(fromFile), function() 437 ext.storage.remove(fileToKey(fromFile), function()
438 { 438 {
439 ext.storage.set(keyPrefix + newName, entry, callback); 439 ext.storage.set(keyPrefix + newName, entry, callback);
440 }); 440 });
441 } 441 }
442 loadFile(file).then(onLoaded, callback); 442 loadFile(file, onLoaded, callback);
443 }, 443 },
444 removeFile: function(file, callback) 444 removeFile: function(file, callback)
445 { 445 {
446 ext.storage.remove(fileToKey(file), callback); 446 ext.storage.remove(fileToKey(file), callback);
447 }, 447 },
448 statFile: function(file, callback) 448 statFile: function(file, callback)
449 { 449 {
450 function onLoaded(entry) 450 function onLoaded(entry)
451 { 451 {
452 callback(null, 452 callback(null,
453 { 453 {
454 exists: true, 454 exists: true,
455 lastModified: entry.lastModified 455 lastModified: entry.lastModified
456 }); 456 });
457 } 457 }
458 loadFile(file).then(onLoaded, callback); 458 loadFile(file, onLoaded, callback);
459 } 459 }
460 }; 460 };
461 return exports; 461 return exports;
462 })(); 462 })();
463 require.scopes["downloader"] = (function() 463 require.scopes["downloader"] = (function()
464 { 464 {
465 var exports = {}; 465 var exports = {};
466 var Utils = require("utils").Utils; 466 var Utils = require("utils").Utils;
467 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; 467 var MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000;
468 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
6557 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount)); 6557 search.push("notificationDownloadCount=" + encodeURIComponent(downlCount));
6558 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&")); 6558 chrome.runtime.setUninstallURL(Utils.getDocLink("uninstalled") + "&" + searc h.join("&"));
6559 } 6559 }
6560 if ("setUninstallURL" in chrome.runtime) 6560 if ("setUninstallURL" in chrome.runtime)
6561 { 6561 {
6562 Prefs.untilLoaded.then(setUninstallURL); 6562 Prefs.untilLoaded.then(setUninstallURL);
6563 Prefs.on("notificationdata", setUninstallURL); 6563 Prefs.on("notificationdata", setUninstallURL);
6564 } 6564 }
6565 return exports; 6565 return exports;
6566 })(); 6566 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld