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; |