| 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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 /* global FakeFile */ | |
| 19 | |
| 20 "use strict"; | 18 "use strict"; |
| 21 | 19 |
| 22 const keyPrefix = "file:"; | 20 const keyPrefix = "file:"; |
| 23 | 21 |
| 24 function fileToKey(file) | 22 function fileToKey(fileName) |
| 25 { | 23 { |
| 26 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 24 return keyPrefix + fileName; |
| 27 } | 25 } |
| 28 | 26 |
| 29 function loadFile(file, successCallback, errorCallback) | 27 function loadFile(fileName) |
| 30 { | 28 { |
| 31 let key = fileToKey(file); | 29 return new Promise((resolve, reject) => |
| 30 { |
| 31 let key = fileToKey(fileName); |
| 32 | 32 |
| 33 ext.storage.get([key], items => | 33 ext.storage.get(key, items => |
| 34 { | 34 { |
| 35 let entry = items[key]; | 35 let entry = items[key]; |
| 36 | 36 |
| 37 if (entry) | 37 if (entry) |
| 38 successCallback(entry); | 38 resolve(entry); |
| 39 else | 39 else |
| 40 errorCallback(new Error("File doesn't exist")); | 40 reject({type: "NoSuchFile"}); |
| 41 }); |
| 41 }); | 42 }); |
| 42 } | 43 } |
| 43 | 44 |
| 44 function saveFile(file, data, callback) | 45 function saveFile(fileName, data) |
| 45 { | 46 { |
| 46 ext.storage.set( | 47 return new Promise((resolve, reject) => |
| 47 fileToKey(file), | 48 { |
| 49 ext.storage.set( |
| 50 fileToKey(fileName), |
| 51 { |
| 52 content: Array.from(data), |
| 53 lastModified: Date.now() |
| 54 }, |
| 55 resolve |
| 56 ); |
| 57 }); |
| 58 } |
| 59 |
| 60 function removeFile(fileName) |
| 61 { |
| 62 return new Promise((resolve, reject) => |
| 63 { |
| 64 ext.storage.remove(fileToKey(fileName), () => |
| 48 { | 65 { |
| 49 content: Array.from(data), | 66 if (chrome.runtime.lastError) |
| 50 lastModified: Date.now() | 67 reject(chrome.runtime.lastError.message); |
| 51 }, | 68 else |
| 52 callback | 69 resolve(); |
| 53 ); | 70 }); |
| 71 }); |
| 54 } | 72 } |
| 55 | 73 |
| 56 exports.IO = | 74 exports.IO = |
| 57 { | 75 { |
| 58 resolveFilePath(path) { return new FakeFile(path); }, | 76 /** |
| 59 | 77 * Reads text lines from a file. |
| 60 readFromFile(file, listener, callback) | 78 * @param {string} fileName |
| 79 * Name of the file to be read |
| 80 * @param {TextSink} listener |
| 81 * Function that will be called for each line in the file |
| 82 * @return {Promise} |
| 83 * Promise to be resolved or rejected once the operation is completed |
| 84 */ |
| 85 readFromFile(fileName, listener) |
| 61 { | 86 { |
| 62 function onLoaded(entry) | 87 return loadFile(fileName).then(entry => |
| 63 { | 88 { |
| 64 for (let line of entry.content) | 89 for (let line of entry.content) |
| 65 listener.process(line); | 90 listener(line); |
| 66 | 91 }); |
| 67 listener.process(null); | |
| 68 callback(null); | |
| 69 } | |
| 70 | |
| 71 loadFile(file, onLoaded, callback); | |
| 72 }, | 92 }, |
| 73 | 93 |
| 74 writeToFile(file, data, callback) | 94 /** |
| 95 * Writes text lines to a file. |
| 96 * @param {string} fileName |
| 97 * Name of the file to be written |
| 98 * @param {Iterable.<string>} data |
| 99 * An array-like or iterable object containing the lines (without line |
| 100 * endings) |
| 101 * @return {Promise} |
| 102 * Promise to be resolved or rejected once the operation is completed |
| 103 */ |
| 104 writeToFile(fileName, data) |
| 75 { | 105 { |
| 76 saveFile(file, data, callback); | 106 return saveFile(fileName, data); |
| 77 }, | 107 }, |
| 78 | 108 |
| 79 copyFile(fromFile, toFile, callback) | 109 /** |
| 110 * Copies a file. |
| 111 * @param {string} fromFile |
| 112 * Name of the file to be copied |
| 113 * @param {string} toFile |
| 114 * Name of the file to be written, will be overwritten if exists |
| 115 * @return {Promise} |
| 116 * Promise to be resolved or rejected once the operation is completed |
| 117 */ |
| 118 copyFile(fromFile, toFile) |
| 80 { | 119 { |
| 81 function onLoaded(entry) | 120 return loadFile(fromFile).then(entry => saveFile(toFile, entry.content)); |
| 82 { | |
| 83 saveFile(toFile, entry.content, callback); | |
| 84 } | |
| 85 | |
| 86 loadFile(fromFile, onLoaded, callback); | |
| 87 }, | 121 }, |
| 88 | 122 |
| 89 renameFile(fromFile, newName, callback) | 123 /** |
| 124 * Renames a file. |
| 125 * @param {string} fromFile |
| 126 * Name of the file to be renamed |
| 127 * @param {string} newName |
| 128 * New file name, will be overwritten if exists |
| 129 * @return {Promise} |
| 130 * Promise to be resolved or rejected once the operation is completed |
| 131 */ |
| 132 renameFile(fromFile, newName) |
| 90 { | 133 { |
| 91 function onLoaded(entry) | 134 return loadFile(fromFile).then(entry => |
| 92 { | 135 { |
| 93 ext.storage.remove(fileToKey(fromFile), () => | 136 return new Promise((resolve, reject) => |
| 94 { | 137 { |
| 95 ext.storage.set(keyPrefix + newName, entry, callback); | 138 ext.storage.set(fileToKey(newName), entry, () => |
| 139 { |
| 140 if (chrome.runtime.lastError) |
| 141 reject(chrome.runtime.lastError.message); |
| 142 else |
| 143 resolve(); |
| 144 }); |
| 96 }); | 145 }); |
| 97 } | 146 }).then(() => removeFile(fromFile)); |
| 98 | |
| 99 loadFile(fromFile, onLoaded, callback); | |
| 100 }, | 147 }, |
| 101 | 148 |
| 102 removeFile(file, callback) | 149 /** |
| 150 * Removes a file. |
| 151 * @param {string} fileName |
| 152 * Name of the file to be removed |
| 153 * @return {Promise} |
| 154 * Promise to be resolved or rejected once the operation is completed |
| 155 */ |
| 156 removeFile(fileName) |
| 103 { | 157 { |
| 104 ext.storage.remove(fileToKey(file), callback); | 158 return removeFile(fileName); |
| 105 }, | 159 }, |
| 106 | 160 |
| 107 statFile(file, callback) | 161 /** |
| 162 * Retrieves file metadata. |
| 163 * @param {string} fileName |
| 164 * Name of the file to be looked up |
| 165 * @return {Promise.<StatData>} |
| 166 * Promise to be resolved with file metadata once the operation is |
| 167 * completed |
| 168 */ |
| 169 statFile(fileName) |
| 108 { | 170 { |
| 109 function onLoaded(entry) | 171 return loadFile(fileName).then(entry => |
| 110 { | 172 { |
| 111 callback(null, { | 173 return { |
| 112 exists: true, | 174 exists: true, |
| 113 lastModified: entry.lastModified | 175 lastModified: entry.lastModified |
| 114 }); | 176 }; |
| 115 } | 177 }).catch(error => |
| 116 | 178 { |
| 117 loadFile(file, onLoaded, callback); | 179 if (error.type == "NoSuchFile") |
| 180 return {exists: false}; |
| 181 throw error; |
| 182 }); |
| 118 } | 183 } |
| 119 }; | 184 }; |
| OLD | NEW |