Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1467)

Unified Diff: Source/devtools/front_end/bindings/TempFile.js

Issue 705063004: DevTools: Promisify WebInspector.TempFile (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: teste were fixed Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/devtools/front_end/bindings/TempFile.js
diff --git a/Source/devtools/front_end/bindings/TempFile.js b/Source/devtools/front_end/bindings/TempFile.js
index 87f38b504436099e7ab49a61d9f891ecf91efe92..a8c69ff7bc25c3715a748a7d3638f907ebce09ee 100644
--- a/Source/devtools/front_end/bindings/TempFile.js
+++ b/Source/devtools/front_end/bindings/TempFile.js
@@ -32,90 +32,99 @@ window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS
/**
* @constructor
- * @param {!string} dirPath
- * @param {!string} name
- * @param {!function(?WebInspector.TempFile)} callback
*/
-WebInspector.TempFile = function(dirPath, name, callback)
+WebInspector.TempFile = function()
{
this._fileEntry = null;
this._writer = null;
+}
+
+/**
+ * @param {string} dirPath
+ * @param {string} name
+ * @return {!Promise.<!WebInspector.TempFile>}
+ */
+WebInspector.TempFile.create = function(dirPath, name)
+{
+ var file = new WebInspector.TempFile();
+
+ function requestTempFileSystem()
+ {
+ return new Promise(window.requestFileSystem.bind(window, window.TEMPORARY, 10));
+ }
/**
* @param {!FileSystem} fs
- * @this {WebInspector.TempFile}
*/
- function didInitFs(fs)
+ function getDirectoryEntry(fs)
{
- fs.root.getDirectory(dirPath, { create: true }, didGetDir.bind(this), errorHandler);
+ return new Promise(fs.root.getDirectory.bind(fs.root, dirPath, { create: true }));
}
/**
* @param {!DirectoryEntry} dir
- * @this {WebInspector.TempFile}
*/
- function didGetDir(dir)
+ function getFileEntry(dir)
{
- dir.getFile(name, { create: true }, didCreateFile.bind(this), errorHandler);
+ return new Promise(dir.getFile.bind(dir, name, { create: true }));
}
/**
* @param {!FileEntry} fileEntry
- * @this {WebInspector.TempFile}
*/
- function didCreateFile(fileEntry)
+ function createFileWriter(fileEntry)
{
- this._fileEntry = fileEntry;
- fileEntry.createWriter(didCreateWriter.bind(this), errorHandler);
+ file._fileEntry = fileEntry;
+ return new Promise(fileEntry.createWriter.bind(fileEntry));
}
/**
* @param {!FileWriter} writer
- * @this {WebInspector.TempFile}
*/
- function didCreateWriter(writer)
+ function truncateFile(writer)
{
+ if (!writer.length) {
+ file._writer = writer;
+ return Promise.resolve(file);
+ }
+
/**
- * @this {WebInspector.TempFile}
+ * @param {function(?)} fulfill
+ * @param {function(*)} reject
*/
- function didTruncate(e)
+ function truncate(fulfill, reject)
{
- this._writer = writer;
+ writer.onwriteend = fulfill;
+ writer.onerror = reject;
+ writer.truncate(0);
+ }
+
+ function didTruncate()
+ {
+ file._writer = writer;
writer.onwriteend = null;
writer.onerror = null;
- callback(this);
+ return Promise.resolve(file);
}
function onTruncateError(e)
{
- WebInspector.console.error("Failed to truncate temp file " + e.code + " : " + e.message);
- callback(null);
- }
-
- if (writer.length) {
- writer.onwriteend = didTruncate.bind(this);
- writer.onerror = onTruncateError;
- writer.truncate(0);
- } else {
- this._writer = writer;
- callback(this);
+ writer.onwriteend = null;
+ writer.onerror = null;
+ throw e;
}
- }
- function errorHandler(e)
- {
- WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message);
- callback(null);
+ return new Promise(truncate)
+ .then(didTruncate)
yurys 2014/11/12 10:29:06 .then(didTruncate, onTruncateError);
+ .catch(onTruncateError);
}
- /**
- * @this {WebInspector.TempFile}
- */
- function didClearTempStorage()
- {
- window.requestFileSystem(window.TEMPORARY, 10, didInitFs.bind(this), errorHandler);
- }
- WebInspector.TempFile._ensureTempStorageCleared(didClearTempStorage.bind(this));
+ return WebInspector.TempFile.ensureTempStorageCleared()
+ .then(requestTempFileSystem)
+ .then(getDirectoryEntry)
+ .then(getFileEntry)
+ .then(createFileWriter)
+ .then(truncateFile);
}
WebInspector.TempFile.prototype = {
@@ -226,6 +235,7 @@ WebInspector.TempFile.prototype = {
*/
WebInspector.DeferredTempFile = function(dirPath, name)
{
+ /** @type {?Array.<string>} */
this._chunks = [];
this._tempFile = null;
this._isWriting = false;
@@ -233,7 +243,8 @@ WebInspector.DeferredTempFile = function(dirPath, name)
this._finishedWriting = false;
this._callsPendingOpen = [];
this._pendingReads = [];
- new WebInspector.TempFile(dirPath, name, this._didCreateTempFile.bind(this));
+ WebInspector.TempFile.create(dirPath, name)
+ .then(this._didCreateTempFile.bind(this), this._failedToCreateTempFile.bind(this));
}
WebInspector.DeferredTempFile.prototype = {
@@ -263,6 +274,19 @@ WebInspector.DeferredTempFile.prototype = {
this._notifyFinished();
},
+ /**
+ * @param {*} e
+ */
+ _failedToCreateTempFile: function(e)
+ {
+ WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message);
+ this._chunks = null;
+ this._notifyFinished();
+ },
+
+ /**
+ * @param {!WebInspector.TempFile} tempFile
+ */
_didCreateTempFile: function(tempFile)
{
this._tempFile = tempFile;
@@ -270,11 +294,6 @@ WebInspector.DeferredTempFile.prototype = {
this._callsPendingOpen = null;
for (var i = 0; i < callsPendingOpen.length; ++i)
callsPendingOpen[i]();
- if (!tempFile) {
- this._chunks = null;
- this._notifyFinished();
- return;
- }
if (this._chunks.length)
this._writeNextChunk();
},
@@ -284,7 +303,7 @@ WebInspector.DeferredTempFile.prototype = {
var chunks = this._chunks;
this._chunks = [];
this._isWriting = true;
- this._tempFile.write(chunks, this._didWriteChunk.bind(this));
+ this._tempFile.write(/** @type {!Array.<string>} */(chunks), this._didWriteChunk.bind(this));
},
_didWriteChunk: function(success)
@@ -359,66 +378,54 @@ WebInspector.DeferredTempFile.prototype = {
}
/**
- * @constructor
+ * @param {function(?)} fulfill
+ * @param {function(*)} reject
*/
-WebInspector.TempStorageCleaner = function()
+WebInspector.TempFile._clearTempStorage = function(fulfill, reject)
{
- try {
- this._worker = new WorkerRuntime.Worker("temp_storage_shared_worker", "TempStorageCleaner");
- this._worker.onerror = this._handleError.bind(this);
- this._callbacks = [];
- this._worker.port.onmessage = this._handleMessage.bind(this);
- this._worker.port.onerror = this._handleError.bind(this);
- } catch (e) {
- if (e.name === "URLMismatchError")
- console.log("Shared worker wasn't started due to url difference. " + e);
- else
- throw e;
- }
-}
-
-WebInspector.TempStorageCleaner.prototype = {
/**
- * @param {!function()} callback
+ * @param {!Event} event
*/
- ensureStorageCleared: function(callback)
+ function handleError(event)
{
- if (this._callbacks)
- this._callbacks.push(callback);
- else
- callback();
- },
+ WebInspector.console.error(WebInspector.UIString("Failed to clear temp storage: %s", event.data));
+ reject(event.data);
+ }
- _handleMessage: function(event)
+ /**
+ * @param {!Event} event
+ */
+ function handleMessage(event)
{
if (event.data.type === "tempStorageCleared") {
if (event.data.error)
WebInspector.console.error(event.data.error);
- this._notifyCallbacks();
+ else
+ fulfill(undefined);
+ return;
}
- },
-
- _handleError: function(event)
- {
- WebInspector.console.error(WebInspector.UIString("Failed to clear temp storage: %s", event.data));
- this._notifyCallbacks();
- },
+ reject(event.data);
+ }
- _notifyCallbacks: function()
- {
- var callbacks = this._callbacks;
- this._callbacks = null;
- for (var i = 0; i < callbacks.length; i++)
- callbacks[i]();
+ try {
+ var worker = new WorkerRuntime.Worker("temp_storage_shared_worker", "TempStorageCleaner");
+ worker.onerror = handleError;
+ worker.port.onmessage = handleMessage;
+ worker.port.onerror = handleError;
+ } catch (e) {
+ if (e.name === "URLMismatchError")
+ console.log("Shared worker wasn't started due to url difference. " + e);
+ else
+ throw e;
}
}
/**
- * @param {!function()} callback
+ * @return {!Promise.<undefined>}
*/
-WebInspector.TempFile._ensureTempStorageCleared = function(callback)
+WebInspector.TempFile.ensureTempStorageCleared = function()
{
- if (!WebInspector.TempFile._storageCleaner)
- WebInspector.TempFile._storageCleaner = new WebInspector.TempStorageCleaner();
- WebInspector.TempFile._storageCleaner.ensureStorageCleared(callback);
+ if (!WebInspector.TempFile._storageCleanerPromise)
+ WebInspector.TempFile._storageCleanerPromise = new Promise(WebInspector.TempFile._clearTempStorage);
+ return WebInspector.TempFile._storageCleanerPromise;
}

Powered by Google App Engine
This is Rietveld 408576698