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 09f48cff81b4bd5731a39af11804be97f0bb507a..101e85143f7d304ffcf41198b5299645852359bc 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 |
|
apavlov
2014/11/11 10:11:04
Please never use '!' with non-object types, as the
|
| + * @param {!string} name |
|
apavlov
2014/11/11 10:11:04
ditto
|
| + * @return {!Promise} |
|
apavlov
2014/11/11 10:11:04
You need to specify the promise resolution type: {
|
| + */ |
| +WebInspector.TempFile.create = function(dirPath, name) |
| +{ |
| + var file = new WebInspector.TempFile(); |
| + |
| + function requestTempFileSystem() |
|
apavlov
2014/11/11 10:11:04
Please annotate
|
| + { |
| + return new Promise(window.requestFileSystem.bind(window, window.TEMPORARY, 10)); |
|
apavlov
2014/11/11 10:11:05
How are you going to handle requestFileSystem fail
|
| + } |
| /** |
| * @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 })); |
|
yurys
2014/11/10 16:29:30
I'd rather make it more verbose and arguably more
loislo
2014/11/10 21:02:17
it would be too wordy.
|
| } |
| /** |
| * @param {!DirectoryEntry} dir |
| - * @this {WebInspector.TempFile} |
| */ |
| - function didGetDir(dir) |
| + function getFileEntry(dir) |
|
apavlov
2014/11/11 10:11:05
no "get" prefix
|
| { |
| - 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) |
|
apavlov
2014/11/11 10:11:04
Please annotate
|
| { |
| - this._writer = writer; |
| + file._writer = writer; |
| writer.onwriteend = null; |
| writer.onerror = null; |
| - callback(this); |
| + return Promise.resolve(file); |
| } |
| function onTruncateError(e) |
|
apavlov
2014/11/11 10:11:05
Please annotate
|
| { |
| + writer.onwriteend = null; |
| + writer.onerror = null; |
| WebInspector.console.error("Failed to truncate temp file " + e.code + " : " + e.message); |
|
yurys
2014/11/11 09:41:04
Let's move all error handling to the client side.
|
| - 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); |
| - } |
| - } |
| - |
| - function errorHandler(e) |
| - { |
| - WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); |
| - callback(null); |
| + return new Promise(truncate) |
| + .then(didTruncate) |
| + .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); |
|
yurys
2014/11/10 16:29:30
Where is catch? There should still be errorHandler
loislo
2014/11/10 21:02:17
as far as I understand .catch statement eats the r
|
| } |
| 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)); |
|
apavlov
2014/11/11 10:11:05
_failedToCreateTempFile
|
| } |
| WebInspector.DeferredTempFile.prototype = { |
| @@ -263,6 +270,13 @@ WebInspector.DeferredTempFile.prototype = { |
| this._notifyFinished(); |
| }, |
| + _failToCreateTempFile: function(e) |
|
apavlov
2014/11/11 10:11:05
Please annotate
|
| + { |
| + 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,67 +367,51 @@ WebInspector.DeferredTempFile.prototype = { |
| } |
| } |
| -/** |
| - * @constructor |
| - */ |
| -WebInspector.TempStorageCleaner = function() |
| +WebInspector.TempFile._clearTempStorage = function(fulfill, reject) |
|
apavlov
2014/11/11 10:11:05
Please annotate
|
| { |
| - try { |
| - this._worker = Runtime.startSharedWorker("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(); |
| + 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 = Runtime.startSharedWorker("temp_storage_shared_worker", "TempStorageCleaner"); |
|
apavlov
2014/11/11 10:11:05
Please rebase, as our workers now start asynchrono
|
| + 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} |
|
apavlov
2014/11/11 10:11:05
Please specify the Promise resolution type. If the
|
| */ |
| -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; |
| } |