| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 const keyPrefix = "file:"; | 18 const keyPrefix = "file:"; |
| 19 | 19 |
| 20 function fileToKey(file) | 20 function fileToKey(file) |
| 21 { | 21 { |
| 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
| 23 } | 23 } |
| 24 | 24 |
| 25 function loadFile(file, successCallback, errorCallback) | 25 function loadFile(file, successCallback, errorCallback) |
| 26 { | 26 { |
| 27 let key = fileToKey(file); | 27 let key = fileToKey(file); |
| 28 | 28 // Make sure we do not have subscriptions in localStorage from older |
| 29 ext.storage.get([key], function(items) | 29 // versions first |
| 30 let entry = localStorage.getItem(key); | |
| 31 if (typeof entry == "string") | |
| 30 { | 32 { |
| 31 let entry = items[key]; | 33 try |
| 32 | 34 { |
| 33 if (entry) | 35 entry = JSON.parse(entry); |
| 34 successCallback(entry); | 36 } |
| 37 catch(err) | |
| 38 { | |
| 39 setTimeout(errorCallback(new Error("File is corrupted"))); | |
| 40 return; | |
| 41 } | |
| 42 setTimeout(successCallback(entry)); | |
| 43 return; | |
| 44 } | |
| 45 // Now try to read from IndexedDB | |
| 46 localforage.getItem(key, function(err, value) | |
| 47 { | |
| 48 if (err || !value) | |
| 49 errorCallback(new Error("File doesn't exist")); | |
| 35 else | 50 else |
| 36 errorCallback(new Error("File doesn't exist")); | 51 successCallback(value); |
| 37 }); | 52 }); |
| 38 } | 53 } |
| 39 | 54 |
| 40 function saveFile(file, data, callback) | 55 function saveFile(file, data, callback) |
| 41 { | 56 { |
| 42 ext.storage.set( | 57 var key = fileToKey(file); |
| 43 fileToKey(file), | 58 var entry = { |
| 44 { | 59 lastModified: Date.now(), |
| 45 content: Array.from(data), | 60 content: Array.from(data) |
| 46 lastModified: Date.now() | 61 }; |
| 47 }, | 62 |
| 48 callback | 63 localStorage.removeItem(key); |
| 49 ); | 64 localforage.setItem(key, entry, callback); |
| 50 } | 65 } |
| 51 | 66 exports.IO = { |
|
kzar
2016/12/19 13:31:04
Nit: looks like you missed this change, please cou
| |
| 52 exports.IO = | |
| 53 { | |
| 54 resolveFilePath: function(path) | 67 resolveFilePath: function(path) |
| 55 { | 68 { |
| 56 return new FakeFile(path); | 69 return new FakeFile(path); |
| 57 }, | 70 }, |
| 58 | 71 |
| 59 readFromFile: function(file, listener, callback) | 72 readFromFile: function(file, listener, callback) |
| 60 { | 73 { |
| 61 function onLoaded(entry) | 74 function onLoaded(entry) |
| 62 { | 75 { |
| 63 for (let line of entry.content) | 76 if ("content" in entry) |
| 64 listener.process(line); | 77 { |
| 65 | 78 for (let line of entry.content) |
| 79 listener.process(line); | |
| 80 } | |
| 66 listener.process(null); | 81 listener.process(null); |
| 67 callback(null); | 82 callback(null); |
| 68 } | 83 } |
| 69 | 84 |
| 70 loadFile(file, onLoaded, callback); | 85 loadFile(file, onLoaded, callback); |
| 71 }, | 86 }, |
| 72 | 87 |
| 73 writeToFile: function(file, data, callback) | 88 writeToFile: function(file, data, callback) |
| 74 { | 89 { |
| 75 saveFile(file, data, callback); | 90 saveFile(file, data, callback); |
| 76 }, | 91 }, |
| 77 | 92 |
| 78 copyFile: function(fromFile, toFile, callback) | 93 copyFile: function(fromFile, toFile, callback) |
| 79 { | 94 { |
| 80 function onLoaded(entry) | 95 function onLoaded(entry) |
| 81 { | 96 { |
| 82 saveFile(toFile, entry.content, callback); | 97 saveFile(toFile, entry.content, callback); |
| 83 } | 98 } |
| 84 | 99 |
| 85 loadFile(fromFile, onLoaded, callback); | 100 loadFile(file, onLoaded, callback); |
|
kzar
2016/12/19 13:31:04
Seems to be a mistake? (Same below.)
Oleksandr
2016/12/20 00:18:28
Yep. We don't use any of these functions anyway, s
| |
| 86 }, | 101 }, |
| 87 | 102 |
| 88 renameFile: function(fromFile, newName, callback) | 103 renameFile: function(fromFile, newName, callback) |
| 89 { | 104 { |
| 90 function onLoaded() | 105 function onLoaded() |
| 91 { | 106 { |
| 92 ext.storage.remove(fileToKey(fromFile), function() | 107 ext.storage.remove(fileToKey(fromFile), function() |
| 93 { | 108 { |
| 94 ext.storage.set(keyPrefix + newName, entry, callback); | 109 ext.storage.set(keyPrefix + newName, entry, callback); |
| 95 }); | 110 }); |
| 96 } | 111 } |
| 97 | 112 |
| 98 loadFile(fromFile, onLoaded, callback); | 113 loadFile(file, onLoaded, callback); |
| 99 }, | 114 }, |
| 100 | 115 |
| 101 removeFile: function(file, callback) | 116 removeFile: function(file, callback) |
| 102 { | 117 { |
| 103 ext.storage.remove(fileToKey(file), callback); | 118 ext.storage.remove(fileToKey(file), callback); |
| 104 }, | 119 }, |
| 105 | 120 |
| 106 statFile: function(file, callback) | 121 statFile: function(file, callback) |
| 107 { | 122 { |
| 108 function onLoaded(entry) | 123 function onLoaded(entry) |
| 109 { | 124 { |
| 110 callback(null, { | 125 callback(null, { |
| 111 exists: true, | 126 exists: true, |
| 112 lastModified: entry.lastModified | 127 lastModified: entry.lastModified |
| 113 }); | 128 }); |
| 114 } | 129 } |
| 115 | 130 |
| 116 loadFile(file, onLoaded, callback); | 131 loadFile(file, onLoaded, callback); |
| 117 } | 132 } |
| 118 }; | 133 }; |
| OLD | NEW |