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

Unified Diff: lib/io.js

Issue 29350042: Issue 4023 - Move storage of subscription lists to localStorage (Closed)
Patch Set: Fix formatting Created Aug. 31, 2016, 9:49 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 | « chrome/ext/background.js ('k') | lib/lz-string.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/io.js
===================================================================
--- a/lib/io.js
+++ b/lib/io.js
@@ -15,6 +15,8 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
+"use strict";
+
const keyPrefix = "file:";
function fileToKey(file)
@@ -22,33 +24,31 @@
return keyPrefix + (file instanceof FakeFile ? file.path : file.spec);
}
-function loadFile(file, successCallback, errorCallback)
+function loadFile(file)
{
- let key = fileToKey(file);
-
- ext.storage.get([key], function(items)
+ return new Promise((resolve, reject) =>
{
- let entry = items[key];
-
- if (entry)
- successCallback(entry);
- else
- errorCallback(new Error("File doesn't exist"));
+ let key = fileToKey(file);
+ ext.storage.get([key], items =>
+ {
+ let entry = items[key];
+ if (!entry)
+ {
+ // Check in localStorage
+ try
+ {
+ entry = JSON.parse(window.localStorage.getItem(key));
+ }
+ catch(err) {}
+ }
+ if (entry)
+ resolve(entry);
+ else
+ reject(new Error("File doesn't exist"));
+ });
});
}
-function saveFile(file, data, callback)
-{
- ext.storage.set(
- fileToKey(file),
- {
- content: data,
- lastModified: Date.now()
- },
- callback
- );
-}
-
exports.IO =
{
resolveFilePath: function(path)
@@ -60,47 +60,42 @@
{
function onLoaded(entry)
{
- for (let line of entry.content)
- listener.process(line);
-
- listener.process(null);
+ if ("content" in entry)
+ {
+ if ("compressed" in entry)
+ entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content));
+ for (let line of entry.content)
+ listener.process(line);
+ }
callback(null);
}
- loadFile(file, onLoaded, callback);
+ loadFile(file).then(onLoaded, callback);
},
writeToFile: function(file, data, callback)
{
- saveFile(file, data, callback);
- },
+ let entry = {};
+ let key = fileToKey(file);
- copyFile: function(fromFile, toFile, callback)
- {
- function onLoaded(entry)
+ if (typeof browser == "undefined")
{
- saveFile(toFile, entry.content, callback);
+ entry[key] = {lastModified: Date.now(), content: data};
+ ext.storage.set(entry, callback);
}
-
- loadFile(fromFile, onLoaded, callback);
- },
-
- renameFile: function(fromFile, newName, callback)
- {
- function onLoaded()
+ else
{
- ext.storage.remove(fileToKey(fromFile), function()
- {
- ext.storage.set(keyPrefix + newName, entry, callback);
- });
+ // Edge cannot write files larger than 1Mb (500k string in UTF-16) via
+ // storage API. However it does not always raise an exception when trying
+ // to do so. The solution is to compress and fallback to localStorage
kzar 2016/09/01 12:57:38 Nit: Mind adding a fullstop at the end of this com
+ let 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]));
+ callback();
}
-
- loadFile(fromFile, onLoaded, callback);
- },
-
- removeFile: function(file, callback)
- {
- ext.storage.remove(fileToKey(file), callback);
},
statFile: function(file, callback)
@@ -113,6 +108,6 @@
});
}
- loadFile(file, onLoaded, callback);
+ loadFile(file).then(onLoaded, callback);
}
};
« no previous file with comments | « chrome/ext/background.js ('k') | lib/lz-string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld