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

Unified Diff: lib/adblockplus.js

Issue 29351586: Issue 4023 - Move storage of subscription lists to localStorage - adblockplusedgems (Closed)
Patch Set: Rebase to version 0.9.6 Created Sept. 8, 2016, 11:54 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « background.html ('k') | lib/lz-string.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/adblockplus.js
===================================================================
--- a/lib/adblockplus.js
+++ b/lib/adblockplus.js
@@ -357,45 +357,58 @@
return keyPrefix + (file instanceof FakeFile ? file.path : file.spec);
}
- function loadFile(file, successCallback, errorCallback)
- {
- var key = fileToKey(file);
- var entry = localStorage.getItem(key);
- if (entry)
- {
- successCallback(JSON.parse(entry));
- }
- else
- {
- // Also check the chrome.storage.local
- // We may have a init data there
+ function loadFile(file)
+ {
+ return new Promise(function(resolve, reject)
+ {
+ var key = fileToKey(file);
ext.storage.get([key], function(items)
{
var entry = items[key];
+ if (!entry)
+ {
+ try
+ {
+ entry = JSON.parse(window.localStorage.getItem(key));
+ }
+ catch (err)
+ {}
+ }
if (entry)
- successCallback(entry);
+ {
+ resolve(entry);
+ }
else
- errorCallback(new Error("File doesn't exist"));
+ {
+ reject(new Error("File doesn't exist"));
+ }
});
- }
+ }.bind(this));
}
function saveFile(file, data, callback)
{
- try
- {
- localStorage.setItem(fileToKey(file), JSON.stringify({
- content: data,
- lastModified: Date.now()
- }));
- }
- catch(error)
- {
- // QuotaExceededError can happen. Notify the user and ignore
- var errorMessage = "Subscription storage is full. " +
- "Please remove some subscriptions and try again.";
- alert(errorMessage);
Oleksandr 2016/09/10 00:35:38 The alert was here
- callback(new Error(errorMessage));
- return;
+ var entry = {};
+ var key = fileToKey(file);
+
+ if (typeof browser == "undefined")
+ {
+ entry[key] = {
+ lastModified: Date.now(),
+ content: data
+ };
+ ext.storage.set(entry, callback);
+ }
+ else
+ {
+ var processedData = LZString.compressToUTF16(JSON.stringify(data));
+ ext.storage.remove(key);
+ entry[key] = {
+ lastModified: Date.now(),
+ content: processedData,
+ compressed: true
+ };
+ window.localStorage.setItem(key, JSON.stringify(entry[key]));
+ setTimeout(callback, 0);
}
callback();
}
@@ -408,15 +421,21 @@
{
function onLoaded(entry)
{
- for (var _loopIndex1 = 0; _loopIndex1 < entry.content.length; ++_loopIndex1)
- {
- var line = entry.content[_loopIndex1];
- listener.process(line);
- }
- listener.process(null);
+ if ("content" in entry)
+ {
+ if (entry["compressed"])
+ {
+ entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content));
+ }
+ for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loopIndex15)
+ {
+ var line = entry.content[_loopIndex15];
+ listener.process(line);
+ }
+ }
callback(null);
}
- loadFile(file, onLoaded, callback);
+ loadFile(file).then(onLoaded, callback);
},
writeToFile: function(file, data, callback)
{
@@ -428,7 +447,7 @@
{
saveFile(toFile, entry.content, callback);
}
- loadFile(fromFile, onLoaded, callback);
+ loadFile(file).then(onLoaded, callback);
},
renameFile: function(fromFile, newName, callback)
{
@@ -439,7 +458,7 @@
ext.storage.set(keyPrefix + newName, entry, callback);
});
}
- loadFile(fromFile, onLoaded, callback);
+ loadFile(file).then(onLoaded, callback);
},
removeFile: function(file, callback)
{
@@ -455,7 +474,7 @@
lastModified: entry.lastModified
});
}
- loadFile(file, onLoaded, callback);
+ loadFile(file).then(onLoaded, callback);
}
};
return exports;
« no previous file with comments | « background.html ('k') | lib/lz-string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld