| 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 |
| 29 ext.storage.get([key], function(items) | 29 // Make sure we do not have subscriptions in localStorage from older |
| 30 // versions first |
| 31 let entry = localStorage.getItem(key); |
| 32 if (typeof entry == "string") |
| 30 { | 33 { |
| 31 let entry = items[key]; | 34 try |
| 32 | 35 { |
| 33 if (entry) | 36 entry = JSON.parse(entry); |
| 34 successCallback(entry); | 37 } |
| 38 catch(err) |
| 39 { |
| 40 setTimeout(errorCallback(new Error("File is corrupted"))); |
| 41 return; |
| 42 } |
| 43 setTimeout(successCallback(entry)); |
| 44 return; |
| 45 } |
| 46 // Now try to read from IndexedDB |
| 47 localforage.getItem(key, function(err, value) |
| 48 { |
| 49 if (err || !value) |
| 50 errorCallback(new Error("File doesn't exist")); |
| 35 else | 51 else |
| 36 errorCallback(new Error("File doesn't exist")); | 52 successCallback(value); |
| 37 }); | 53 }); |
| 38 } | 54 } |
| 39 | 55 |
| 40 function saveFile(file, data, callback) | 56 function saveFile(file, data, callback) |
| 41 { | 57 { |
| 42 ext.storage.set( | 58 var key = fileToKey(file); |
| 43 fileToKey(file), | 59 var entry = { |
| 44 { | 60 content: Array.from(data), |
| 45 content: Array.from(data), | 61 lastModified: Date.now() |
| 46 lastModified: Date.now() | 62 }; |
| 47 }, | 63 |
| 48 callback | 64 localStorage.removeItem(key); |
| 49 ); | 65 localforage.setItem(key, entry, callback); |
| 50 } | 66 } |
| 51 | 67 |
| 52 exports.IO = | 68 exports.IO = |
| 53 { | 69 { |
| 54 resolveFilePath: function(path) | 70 resolveFilePath: function(path) |
| 55 { | 71 { |
| 56 return new FakeFile(path); | 72 return new FakeFile(path); |
| 57 }, | 73 }, |
| 58 | 74 |
| 59 readFromFile: function(file, listener, callback) | 75 readFromFile: function(file, listener, callback) |
| 60 { | 76 { |
| 61 function onLoaded(entry) | 77 function onLoaded(entry) |
| 62 { | 78 { |
| 63 for (let line of entry.content) | 79 if ("content" in entry) |
| 64 listener.process(line); | 80 { |
| 65 | 81 for (let line of entry.content) |
| 82 listener.process(line); |
| 83 } |
| 66 listener.process(null); | 84 listener.process(null); |
| 67 callback(null); | 85 callback(null); |
| 68 } | 86 } |
| 69 | 87 |
| 70 loadFile(file, onLoaded, callback); | 88 loadFile(file, onLoaded, callback); |
| 71 }, | 89 }, |
| 72 | 90 |
| 73 writeToFile: function(file, data, callback) | 91 writeToFile: function(file, data, callback) |
| 74 { | 92 { |
| 75 saveFile(file, data, callback); | 93 saveFile(file, data, callback); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 { | 127 { |
| 110 callback(null, { | 128 callback(null, { |
| 111 exists: true, | 129 exists: true, |
| 112 lastModified: entry.lastModified | 130 lastModified: entry.lastModified |
| 113 }); | 131 }); |
| 114 } | 132 } |
| 115 | 133 |
| 116 loadFile(file, onLoaded, callback); | 134 loadFile(file, onLoaded, callback); |
| 117 } | 135 } |
| 118 }; | 136 }; |
| OLD | NEW |