Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/io.js

Issue 29452181: Noissue - Merge current tip to Edge bookmark (Closed)
Patch Set: Created May 30, 2017, 3:49 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/icon.js ('k') | lib/messaging.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/io.js
===================================================================
--- a/lib/io.js
+++ b/lib/io.js
@@ -1,6 +1,6 @@
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
- * Copyright (C) 2006-2016 Eyeo GmbH
+ * Copyright (C) 2006-2017 eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
@@ -19,117 +19,166 @@
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);
+ return new Promise((resolve, reject) =>
+ {
+ let key = fileToKey(fileName);
- // Make sure we do not have subscriptions in localStorage from older
- // versions first
- let entry = localStorage.getItem(key);
- if (typeof entry == "string")
- {
- try
+ ext.storage.get(key, items =>
{
- entry = JSON.parse(entry);
- }
- catch(err)
- {
- setTimeout(errorCallback(new Error("File is corrupted")));
- return;
- }
- setTimeout(successCallback(entry));
- return;
- }
- // Now try to read from IndexedDB
- localforage.getItem(key, function(err, value)
- {
- if (err || !value)
- errorCallback(new Error("File doesn't exist"));
- else
- successCallback(value);
+ let entry = items[key];
+
+ if (entry)
+ resolve(entry);
+ else
+ reject({type: "NoSuchFile"});
+ });
});
}
-function saveFile(file, data, callback)
+function saveFile(fileName, data)
{
- var key = fileToKey(file);
- var entry = {
- content: Array.from(data),
- lastModified: Date.now()
- };
+ return new Promise((resolve, reject) =>
+ {
+ ext.storage.set(
+ fileToKey(fileName),
+ {
+ content: Array.from(data),
+ lastModified: Date.now()
+ },
+ resolve
+ );
+ });
+}
- localStorage.removeItem(key);
- localforage.setItem(key, entry, callback);
+function removeFile(fileName)
+{
+ return new Promise((resolve, reject) =>
+ {
+ ext.storage.remove(fileToKey(fileName), () =>
+ {
+ 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 =>
{
- if ("content" in entry)
- {
- for (let line of entry.content)
- listener.process(line);
- }
- listener.process(null);
- callback(null);
- }
-
- loadFile(file, onLoaded, callback);
+ for (let line of entry.content)
+ 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;
+ });
}
};
« no previous file with comments | « lib/icon.js ('k') | lib/messaging.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld