| Index: lib/io.js |
| =================================================================== |
| --- a/lib/io.js |
| +++ b/lib/io.js |
| @@ -10,110 +10,175 @@ |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| -/* global FakeFile */ |
| - |
| "use strict"; |
| const keyPrefix = "file:"; |
| -function fileToKey(file) |
| +function fileToKey(fileName) |
| { |
| - return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
| + return keyPrefix + fileName; |
| } |
| -function loadFile(file, successCallback, errorCallback) |
| +function loadFile(fileName) |
| { |
| - let key = fileToKey(file); |
| - |
| - ext.storage.get([key], items => |
| + return new Promise((resolve, reject) => |
| { |
| - let entry = items[key]; |
| + let key = fileToKey(fileName); |
| + |
| + ext.storage.get(key, items => |
| + { |
| + let entry = items[key]; |
| - if (entry) |
| - successCallback(entry); |
| - else |
| - errorCallback(new Error("File doesn't exist")); |
| + if (entry) |
| + resolve(entry); |
| + else |
| + reject({type: "NoSuchFile"}); |
| + }); |
| }); |
| } |
| -function saveFile(file, data, callback) |
| +function saveFile(fileName, data) |
| { |
| - ext.storage.set( |
| - fileToKey(file), |
| + return new Promise((resolve, reject) => |
| + { |
| + ext.storage.set( |
| + fileToKey(fileName), |
| + { |
| + content: Array.from(data), |
| + lastModified: Date.now() |
| + }, |
| + resolve |
| + ); |
| + }); |
| +} |
| + |
| +function removeFile(fileName) |
| +{ |
| + return new Promise((resolve, reject) => |
| + { |
| + ext.storage.remove(fileToKey(fileName), () => |
| { |
| - content: Array.from(data), |
| - lastModified: Date.now() |
| - }, |
| - callback |
| - ); |
| + if (chrome.runtime.lastError) |
| + reject(chrome.runtime.lastError.message); |
| + else |
| + resolve(); |
| + }); |
| + }); |
| } |
| exports.IO = |
| { |
| - resolveFilePath(path) { return new FakeFile(path); }, |
| - |
| - readFromFile(file, listener, callback) |
| + /** |
| + * Reads text lines from a file. |
| + * @param {string} fileName |
| + * Name of the file to be read |
| + * @param {TextSink} listener |
| + * Function that will be called for each line in the file |
| + * @return {Promise} |
| + * Promise to be resolved or rejected once the operation is completed |
| + */ |
| + readFromFile(fileName, listener) |
| { |
| - function onLoaded(entry) |
| + return loadFile(fileName).then(entry => |
| { |
| for (let line of entry.content) |
| - listener.process(line); |
| - |
| - listener.process(null); |
| - callback(null); |
| - } |
| - |
| - loadFile(file, onLoaded, callback); |
| + listener(line); |
| + }); |
| }, |
| - writeToFile(file, data, callback) |
| + /** |
| + * Writes text lines to a file. |
| + * @param {string} fileName |
| + * Name of the file to be written |
| + * @param {Iterable.<string>} data |
| + * An array-like or iterable object containing the lines (without line |
| + * endings) |
| + * @return {Promise} |
| + * Promise to be resolved or rejected once the operation is completed |
| + */ |
| + writeToFile(fileName, data) |
| { |
| - saveFile(file, data, callback); |
| + return saveFile(fileName, data); |
| }, |
| - copyFile(fromFile, toFile, callback) |
| + /** |
| + * Copies a file. |
| + * @param {string} fromFile |
| + * Name of the file to be copied |
| + * @param {string} toFile |
| + * Name of the file to be written, will be overwritten if exists |
| + * @return {Promise} |
| + * Promise to be resolved or rejected once the operation is completed |
| + */ |
| + copyFile(fromFile, toFile) |
| { |
| - function onLoaded(entry) |
| - { |
| - saveFile(toFile, entry.content, callback); |
| - } |
| - |
| - loadFile(fromFile, onLoaded, callback); |
| + return loadFile(fromFile).then(entry => saveFile(toFile, entry.content)); |
| }, |
| - renameFile(fromFile, newName, callback) |
| + /** |
| + * Renames a file. |
| + * @param {string} fromFile |
| + * Name of the file to be renamed |
| + * @param {string} newName |
| + * New file name, will be overwritten if exists |
| + * @return {Promise} |
| + * Promise to be resolved or rejected once the operation is completed |
| + */ |
| + renameFile(fromFile, newName) |
| { |
| - function onLoaded(entry) |
| + return loadFile(fromFile).then(entry => |
| { |
| - ext.storage.remove(fileToKey(fromFile), () => |
| + return new Promise((resolve, reject) => |
| { |
| - ext.storage.set(keyPrefix + newName, entry, callback); |
| + ext.storage.set(fileToKey(newName), entry, () => |
| + { |
| + if (chrome.runtime.lastError) |
| + reject(chrome.runtime.lastError.message); |
| + else |
| + resolve(); |
| + }); |
| }); |
| - } |
| - |
| - loadFile(fromFile, onLoaded, callback); |
| + }).then(() => removeFile(fromFile)); |
| }, |
| - removeFile(file, callback) |
| + /** |
| + * Removes a file. |
| + * @param {string} fileName |
| + * Name of the file to be removed |
| + * @return {Promise} |
| + * Promise to be resolved or rejected once the operation is completed |
| + */ |
| + removeFile(fileName) |
| { |
| - ext.storage.remove(fileToKey(file), callback); |
| + return removeFile(fileName); |
| }, |
| - statFile(file, callback) |
| + /** |
| + * Retrieves file metadata. |
| + * @param {string} fileName |
| + * Name of the file to be looked up |
| + * @return {Promise.<StatData>} |
| + * Promise to be resolved with file metadata once the operation is |
| + * completed |
| + */ |
| + statFile(fileName) |
| { |
| - function onLoaded(entry) |
| + return loadFile(fileName).then(entry => |
| { |
| - callback(null, { |
| + return { |
| exists: true, |
| lastModified: entry.lastModified |
| - }); |
| - } |
| - |
| - loadFile(file, onLoaded, callback); |
| + }; |
| + }).catch(error => |
| + { |
| + if (error.type == "NoSuchFile") |
| + return {exists: false}; |
| + throw error; |
| + }); |
| } |
| }; |