Chromium Code Reviews| 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 a9674a87f2ae9005f86aae2d7f3ea849e5940e1c..5342db0312e6c8658ba9ad3fb03b319239e17692 100644 |
| --- a/Source/devtools/front_end/bindings/TempFile.js |
| +++ b/Source/devtools/front_end/bindings/TempFile.js |
| @@ -32,90 +32,96 @@ 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.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) |
| { |
| - /** |
| - * @this {WebInspector.TempFile} |
| - */ |
| + if (!writer.length) { |
| + file._writer = writer; |
| + return Promise.resolve(file); |
| + } |
| + |
| + function truncate(fulfill, reject) |
| + { |
| + writer.onwriteend = fulfill; |
| + writer.onerror = reject; |
| + writer.truncate(0); |
| + } |
| + |
| function didTruncate(e) |
| { |
| - this._writer = writer; |
| + file._writer = writer; |
| writer.onwriteend = null; |
| writer.onerror = null; |
| - callback(this); |
| + return Promise.resolve(file); |
| } |
| function onTruncateError(e) |
| { |
| + writer.onwriteend = null; |
| + writer.onerror = null; |
| WebInspector.console.error("Failed to truncate temp file " + e.code + " : " + e.message); |
| - callback(null); |
| + return Promise.reject(e); |
| } |
| - if (writer.length) { |
| - writer.onwriteend = didTruncate.bind(this); |
| - writer.onerror = onTruncateError; |
| - writer.truncate(0); |
| - } else { |
| - this._writer = writer; |
| - callback(this); |
| - } |
| + return new Promise(truncate) |
| + .then(didTruncate) |
| + .catch(onTruncateError); |
| } |
| - function errorHandler(e) |
| - { |
| - WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); |
| - callback(null); |
| - } |
| - |
| - /** |
| - * @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 = { |
| @@ -233,7 +239,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._failToCreateTempFile.bind(this)); |
| } |
| WebInspector.DeferredTempFile.prototype = { |
| @@ -263,6 +270,13 @@ WebInspector.DeferredTempFile.prototype = { |
| this._notifyFinished(); |
| }, |
| + _failToCreateTempFile: function(e) |
| + { |
| + WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); |
| + this._chunks = null; |
| + this._notifyFinished(); |
| + }, |
| + |
| _didCreateTempFile: function(tempFile) |
| { |
| this._tempFile = tempFile; |
| @@ -270,11 +284,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(); |
| }, |
| @@ -358,60 +367,39 @@ WebInspector.DeferredTempFile.prototype = { |
| } |
| } |
| -/** |
| - * @constructor |
| - */ |
| -WebInspector.TempStorageCleaner = function() |
| +WebInspector.TempFile._clearTempStorage = function(fulfill, reject) |
| { |
| - this._worker = Runtime.startSharedWorker("temp_storage_shared_worker", "TempStorage"); |
| - 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); |
| -} |
| + var worker = Runtime.startSharedWorker("temp_storage_shared_worker", "TempStorage"); |
|
apavlov
2014/11/07 11:06:03
I'm about to promisify the worker startup (see htt
|
| -WebInspector.TempStorageCleaner.prototype = { |
| - /** |
| - * @param {!function()} callback |
| - */ |
| - ensureStorageCleared: function(callback) |
| + function handleError(event) |
|
apavlov
2014/11/07 11:06:03
Please annotate things while you are here
loislo
2014/11/10 08:56:30
Done.
|
| { |
| - 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) |
| + function handleMessage(event) |
| { |
| if (event.data.type === "tempStorageCleared") { |
| if (event.data.error) |
| WebInspector.console.error(event.data.error); |
| - this._notifyCallbacks(); |
| + else |
| + fulfill(); |
| + return; |
| } |
| - }, |
| - |
| - _handleError: function(event) |
| - { |
| - WebInspector.console.error(WebInspector.UIString("Failed to clear temp storage: %s", event.data)); |
| - this._notifyCallbacks(); |
| - }, |
| - |
| - _notifyCallbacks: function() |
| - { |
| - var callbacks = this._callbacks; |
| - this._callbacks = null; |
| - for (var i = 0; i < callbacks.length; i++) |
| - callbacks[i](); |
| + reject(event.data); |
| } |
| + |
| + worker.onerror = handleError; |
| + worker.port.onmessage = handleMessage; |
| + worker.port.onerror = handleError; |
| } |
| /** |
| - * @param {!function()} callback |
| + * @return {!Promise} |
| */ |
| -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; |
| } |