| 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 "use strict"; |
| 19 |
| 18 const keyPrefix = "file:"; | 20 const keyPrefix = "file:"; |
| 19 | 21 |
| 20 function fileToKey(file) | 22 function fileToKey(file) |
| 21 { | 23 { |
| 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 24 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
| 23 } | 25 } |
| 24 | 26 |
| 25 function loadFile(file, successCallback, errorCallback) | 27 function loadFile(file, successCallback, errorCallback) |
| 26 { | 28 { |
| 27 let key = fileToKey(file); | 29 let key = fileToKey(file); |
| 28 | 30 |
| 29 ext.storage.get([key], function(items) | 31 ext.storage.get([key], items => |
| 30 { | 32 { |
| 31 let entry = items[key]; | 33 let entry = items[key]; |
| 32 | 34 |
| 33 if (entry) | 35 if (entry) |
| 34 successCallback(entry); | 36 successCallback(entry); |
| 35 else | 37 else |
| 36 errorCallback(new Error("File doesn't exist")); | 38 errorCallback(new Error("File doesn't exist")); |
| 37 }); | 39 }); |
| 38 } | 40 } |
| 39 | 41 |
| 40 function saveFile(file, data, callback) | 42 function saveFile(file, data, callback) |
| 41 { | 43 { |
| 42 ext.storage.set( | 44 ext.storage.set( |
| 43 fileToKey(file), | 45 fileToKey(file), |
| 44 { | 46 { |
| 45 content: Array.from(data), | 47 content: Array.from(data), |
| 46 lastModified: Date.now() | 48 lastModified: Date.now() |
| 47 }, | 49 }, |
| 48 callback | 50 callback |
| 49 ); | 51 ); |
| 50 } | 52 } |
| 51 | 53 |
| 52 exports.IO = | 54 exports.IO = |
| 53 { | 55 { |
| 54 resolveFilePath: function(path) | 56 resolveFilePath(path) { return new FakeFile(path); }, |
| 55 { | |
| 56 return new FakeFile(path); | |
| 57 }, | |
| 58 | 57 |
| 59 readFromFile: function(file, listener, callback) | 58 readFromFile(file, listener, callback) |
| 60 { | 59 { |
| 61 function onLoaded(entry) | 60 function onLoaded(entry) |
| 62 { | 61 { |
| 63 for (let line of entry.content) | 62 for (let line of entry.content) |
| 64 listener.process(line); | 63 listener.process(line); |
| 65 | 64 |
| 66 listener.process(null); | 65 listener.process(null); |
| 67 callback(null); | 66 callback(null); |
| 68 } | 67 } |
| 69 | 68 |
| 70 loadFile(file, onLoaded, callback); | 69 loadFile(file, onLoaded, callback); |
| 71 }, | 70 }, |
| 72 | 71 |
| 73 writeToFile: function(file, data, callback) | 72 writeToFile(file, data, callback) |
| 74 { | 73 { |
| 75 saveFile(file, data, callback); | 74 saveFile(file, data, callback); |
| 76 }, | 75 }, |
| 77 | 76 |
| 78 copyFile: function(fromFile, toFile, callback) | 77 copyFile(fromFile, toFile, callback) |
| 79 { | 78 { |
| 80 function onLoaded(entry) | 79 function onLoaded(entry) |
| 81 { | 80 { |
| 82 saveFile(toFile, entry.content, callback); | 81 saveFile(toFile, entry.content, callback); |
| 83 } | 82 } |
| 84 | 83 |
| 85 loadFile(fromFile, onLoaded, callback); | 84 loadFile(fromFile, onLoaded, callback); |
| 86 }, | 85 }, |
| 87 | 86 |
| 88 renameFile: function(fromFile, newName, callback) | 87 renameFile(fromFile, newName, callback) |
| 89 { | 88 { |
| 90 function onLoaded(entry) | 89 function onLoaded(entry) |
| 91 { | 90 { |
| 92 ext.storage.remove(fileToKey(fromFile), function() | 91 ext.storage.remove(fileToKey(fromFile), () => |
| 93 { | 92 { |
| 94 ext.storage.set(keyPrefix + newName, entry, callback); | 93 ext.storage.set(keyPrefix + newName, entry, callback); |
| 95 }); | 94 }); |
| 96 } | 95 } |
| 97 | 96 |
| 98 loadFile(fromFile, onLoaded, callback); | 97 loadFile(fromFile, onLoaded, callback); |
| 99 }, | 98 }, |
| 100 | 99 |
| 101 removeFile: function(file, callback) | 100 removeFile(file, callback) |
| 102 { | 101 { |
| 103 ext.storage.remove(fileToKey(file), callback); | 102 ext.storage.remove(fileToKey(file), callback); |
| 104 }, | 103 }, |
| 105 | 104 |
| 106 statFile: function(file, callback) | 105 statFile(file, callback) |
| 107 { | 106 { |
| 108 function onLoaded(entry) | 107 function onLoaded(entry) |
| 109 { | 108 { |
| 110 callback(null, { | 109 callback(null, { |
| 111 exists: true, | 110 exists: true, |
| 112 lastModified: entry.lastModified | 111 lastModified: entry.lastModified |
| 113 }); | 112 }); |
| 114 } | 113 } |
| 115 | 114 |
| 116 loadFile(file, onLoaded, callback); | 115 loadFile(file, onLoaded, callback); |
| 117 } | 116 } |
| 118 }; | 117 }; |
| OLD | NEW |