Commit 2996ad64 authored by Nathan Bruer's avatar Nathan Bruer Committed by Commit Bot

[Devtools] Migrated isolatedFileSystem to use blobs and promises

This patch adds the ability to request a file as a blob and splits the
other read functions to use the blob reader.

R=lushnikov
BUG=None

Change-Id: I227b21fe5da19277ddbceedf2c69c33e1f4ef705
Reviewed-on: https://chromium-review.googlesource.com/667958
Commit-Queue: Blaise Bruer <allada@chromium.org>
Reviewed-by: default avatarAndrey Lushnikov <lushnikov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#502154}
parent 20f081de
......@@ -261,69 +261,67 @@ Persistence.IsolatedFileSystem = class {
/**
* @param {string} path
* @return {!Promise<?string>}
* @return {!Promise<?Blob>}
*/
requestFileContentPromise(path) {
var fulfill;
var promise = new Promise(x => fulfill = x);
this.requestFileContent(path, fulfill);
return promise;
requestFileBlob(path) {
return new Promise(resolve => {
this._domFileSystem.root.getFile(path, undefined, entry => {
entry.file(resolve, errorHandler.bind(this));
}, errorHandler.bind(this));
/**
* @this {Persistence.IsolatedFileSystem}
*/
function errorHandler(error) {
if (error.name === 'NotFoundError') {
resolve(null);
return;
}
var errorMessage = Persistence.IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when getting content for file \'' + (this._path + '/' + path) + '\'');
resolve(null);
}
});
}
/**
* @param {string} path
* @param {function(?string)} callback
* @return {!Promise<?string>}
*/
requestFileContent(path, callback) {
this._domFileSystem.root.getFile(path, undefined, fileEntryLoaded.bind(this), errorHandler.bind(this));
/**
* @param {!FileEntry} entry
* @this {Persistence.IsolatedFileSystem}
*/
function fileEntryLoaded(entry) {
entry.file(fileLoaded, errorHandler.bind(this));
async requestFileContentPromise(path) {
var blob = await this.requestFileBlob(path);
if (!blob)
return null;
var reader = new FileReader();
var fileContentsLoadedPromise = new Promise(resolve => reader.onloadend = resolve);
if (Persistence.IsolatedFileSystem.ImageExtensions.has(Common.ParsedURL.extractExtension(path)))
reader.readAsDataURL(blob);
else
reader.readAsText(blob);
await fileContentsLoadedPromise;
if (reader.error) {
console.error('Can\'t read file: ' + path + ': ' + reader.error);
return null;
}
/**
* @param {!Blob} file
*/
function fileLoaded(file) {
var reader = new FileReader();
reader.onloadend = readerLoadEnd;
if (Persistence.IsolatedFileSystem.ImageExtensions.has(Common.ParsedURL.extractExtension(path)))
reader.readAsDataURL(file);
else
reader.readAsText(file);
try {
var result = reader.result;
} catch (e) {
result = null;
console.error('Can\'t read file: ' + path + ': ' + e);
}
if (result === undefined)
return null;
return result;
}
/**
* @this {!FileReader}
*/
function readerLoadEnd() {
/** @type {?string} */
var string = null;
try {
string = /** @type {string} */ (this.result);
} catch (e) {
console.error('Can\'t read file: ' + path + ': ' + e);
}
callback(string);
}
/**
* @this {Persistence.IsolatedFileSystem}
*/
function errorHandler(error) {
if (error.name === 'NotFoundError') {
callback(null);
return;
}
var errorMessage = Persistence.IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when getting content for file \'' + (this._path + '/' + path) + '\'');
callback(null);
}
/**
* @param {string} path
* @param {function(?string)} callback
*/
requestFileContent(path, callback) {
this.requestFileContentPromise(path).then(callback);
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment