Commit 95055fab authored by loislo@chromium.org's avatar loislo@chromium.org

DevTools: Promisify WebInspector.TempFile

BUG=

Review URL: https://codereview.chromium.org/705063004

git-svn-id: svn://svn.chromium.org/blink/trunk@185201 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent de47d53b
...@@ -445,11 +445,10 @@ InspectorTest.MockSetting.prototype = { ...@@ -445,11 +445,10 @@ InspectorTest.MockSetting.prototype = {
* @param {!string} name * @param {!string} name
* @param {!function(?WebInspector.TempFile)} callback * @param {!function(?WebInspector.TempFile)} callback
*/ */
InspectorTest.TempFileMock = function(dirPath, name, callback) InspectorTest.TempFileMock = function(dirPath, name)
{ {
this._chunks = []; this._chunks = [];
this._name = name; this._name = name;
setTimeout(callback.bind(this, this), 1);
} }
InspectorTest.TempFileMock.prototype = { InspectorTest.TempFileMock.prototype = {
...@@ -527,6 +526,12 @@ InspectorTest.TempFileMock.prototype = { ...@@ -527,6 +526,12 @@ InspectorTest.TempFileMock.prototype = {
remove: function() { } remove: function() { }
} }
InspectorTest.TempFileMock.create = function(dirPath, name)
{
var tempFile = new InspectorTest.TempFileMock(dirPath, name);
return Promise.resolve(tempFile);
}
InspectorTest.dumpLoadedModules = function(next) InspectorTest.dumpLoadedModules = function(next)
{ {
function moduleSorter(left, right) function moduleSorter(left, right)
...@@ -581,8 +586,6 @@ InspectorTest.TimeoutMock.prototype = { ...@@ -581,8 +586,6 @@ InspectorTest.TimeoutMock.prototype = {
} }
} }
WebInspector.TempFile = InspectorTest.TempFileMock;
WebInspector.targetManager.observeTargets({ WebInspector.targetManager.observeTargets({
targetAdded: function(target) targetAdded: function(target)
{ {
......
var initialize_Timeline = function() { var initialize_Timeline = function() {
InspectorTest.preloadPanel("timeline"); InspectorTest.preloadPanel("timeline");
WebInspector.TempFile = InspectorTest.TempFileMock;
// Scrub values when printing out these properties in the record or data field. // Scrub values when printing out these properties in the record or data field.
InspectorTest.timelinePropertyFormatters = { InspectorTest.timelinePropertyFormatters = {
......
var initialize_ProfilerTest = function() { var initialize_ProfilerTest = function() {
InspectorTest.preloadPanel("profiles"); InspectorTest.preloadPanel("profiles");
WebInspector.TempFile = InspectorTest.TempFileMock;
InspectorTest.startProfilerTest = function(callback) InspectorTest.startProfilerTest = function(callback)
{ {
......
...@@ -5,13 +5,13 @@ ...@@ -5,13 +5,13 @@
function test() function test()
{ {
var cleaner = new WebInspector.TempStorageCleaner(); WebInspector.TempFile.ensureTempStorageCleared()
function finish() .then(function finish() {
{ InspectorTest.addResult("PASSED: we got a call from TempStorageCleaner that it had done the job.");
InspectorTest.addResult("PASSED: we got a call from TempStorageCleaner that it had done the job."); }, function(e) {
InspectorTest.completeTest(); InspectorTest.addResult("FAILED: we got a rejection: " + e);
} })
cleaner.ensureStorageCleared(finish); .then(InspectorTest.completeTest);
} }
</script> </script>
......
...@@ -2,6 +2,7 @@ function initialize_TracingTest() ...@@ -2,6 +2,7 @@ function initialize_TracingTest()
{ {
InspectorTest.preloadPanel("timeline"); InspectorTest.preloadPanel("timeline");
WebInspector.TempFile = InspectorTest.TempFileMock;
InspectorTest.tracingManager = function() InspectorTest.tracingManager = function()
{ {
......
...@@ -32,90 +32,97 @@ window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS ...@@ -32,90 +32,97 @@ window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS
/** /**
* @constructor * @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._fileEntry = null;
this._writer = 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 * @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 * @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 * @param {!FileEntry} fileEntry
* @this {WebInspector.TempFile}
*/ */
function didCreateFile(fileEntry) function createFileWriter(fileEntry)
{ {
this._fileEntry = fileEntry; file._fileEntry = fileEntry;
fileEntry.createWriter(didCreateWriter.bind(this), errorHandler); return new Promise(fileEntry.createWriter.bind(fileEntry));
} }
/** /**
* @param {!FileWriter} writer * @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.onwriteend = null;
writer.onerror = null; writer.onerror = null;
callback(this); return Promise.resolve(file);
} }
function onTruncateError(e) function onTruncateError(e)
{ {
WebInspector.console.error("Failed to truncate temp file " + e.code + " : " + e.message); writer.onwriteend = null;
callback(null); writer.onerror = null;
} throw e;
if (writer.length) {
writer.onwriteend = didTruncate.bind(this);
writer.onerror = onTruncateError;
writer.truncate(0);
} else {
this._writer = writer;
callback(this);
} }
}
function errorHandler(e) return new Promise(truncate).then(didTruncate, onTruncateError);
{
WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message);
callback(null);
} }
/** return WebInspector.TempFile.ensureTempStorageCleared()
* @this {WebInspector.TempFile} .then(requestTempFileSystem)
*/ .then(getDirectoryEntry)
function didClearTempStorage() .then(getFileEntry)
{ .then(createFileWriter)
window.requestFileSystem(window.TEMPORARY, 10, didInitFs.bind(this), errorHandler); .then(truncateFile);
}
WebInspector.TempFile._ensureTempStorageCleared(didClearTempStorage.bind(this));
} }
WebInspector.TempFile.prototype = { WebInspector.TempFile.prototype = {
...@@ -226,6 +233,7 @@ WebInspector.TempFile.prototype = { ...@@ -226,6 +233,7 @@ WebInspector.TempFile.prototype = {
*/ */
WebInspector.DeferredTempFile = function(dirPath, name) WebInspector.DeferredTempFile = function(dirPath, name)
{ {
/** @type {?Array.<string>} */
this._chunks = []; this._chunks = [];
this._tempFile = null; this._tempFile = null;
this._isWriting = false; this._isWriting = false;
...@@ -233,7 +241,8 @@ WebInspector.DeferredTempFile = function(dirPath, name) ...@@ -233,7 +241,8 @@ WebInspector.DeferredTempFile = function(dirPath, name)
this._finishedWriting = false; this._finishedWriting = false;
this._callsPendingOpen = []; this._callsPendingOpen = [];
this._pendingReads = []; 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 = { WebInspector.DeferredTempFile.prototype = {
...@@ -263,6 +272,19 @@ WebInspector.DeferredTempFile.prototype = { ...@@ -263,6 +272,19 @@ WebInspector.DeferredTempFile.prototype = {
this._notifyFinished(); 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) _didCreateTempFile: function(tempFile)
{ {
this._tempFile = tempFile; this._tempFile = tempFile;
...@@ -270,11 +292,6 @@ WebInspector.DeferredTempFile.prototype = { ...@@ -270,11 +292,6 @@ WebInspector.DeferredTempFile.prototype = {
this._callsPendingOpen = null; this._callsPendingOpen = null;
for (var i = 0; i < callsPendingOpen.length; ++i) for (var i = 0; i < callsPendingOpen.length; ++i)
callsPendingOpen[i](); callsPendingOpen[i]();
if (!tempFile) {
this._chunks = null;
this._notifyFinished();
return;
}
if (this._chunks.length) if (this._chunks.length)
this._writeNextChunk(); this._writeNextChunk();
}, },
...@@ -284,7 +301,7 @@ WebInspector.DeferredTempFile.prototype = { ...@@ -284,7 +301,7 @@ WebInspector.DeferredTempFile.prototype = {
var chunks = this._chunks; var chunks = this._chunks;
this._chunks = []; this._chunks = [];
this._isWriting = true; 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) _didWriteChunk: function(success)
...@@ -359,66 +376,54 @@ WebInspector.DeferredTempFile.prototype = { ...@@ -359,66 +376,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) WebInspector.console.error(WebInspector.UIString("Failed to clear temp storage: %s", event.data));
this._callbacks.push(callback); reject(event.data);
else }
callback();
},
_handleMessage: function(event) /**
* @param {!Event} event
*/
function handleMessage(event)
{ {
if (event.data.type === "tempStorageCleared") { if (event.data.type === "tempStorageCleared") {
if (event.data.error) if (event.data.error)
WebInspector.console.error(event.data.error); WebInspector.console.error(event.data.error);
this._notifyCallbacks(); else
fulfill(undefined);
return;
} }
}, reject(event.data);
}
_handleError: function(event)
{
WebInspector.console.error(WebInspector.UIString("Failed to clear temp storage: %s", event.data));
this._notifyCallbacks();
},
_notifyCallbacks: function() try {
{ var worker = new WorkerRuntime.Worker("temp_storage_shared_worker", "TempStorageCleaner");
var callbacks = this._callbacks; worker.onerror = handleError;
this._callbacks = null; worker.port.onmessage = handleMessage;
for (var i = 0; i < callbacks.length; i++) worker.port.onerror = handleError;
callbacks[i](); } 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) if (!WebInspector.TempFile._storageCleanerPromise)
WebInspector.TempFile._storageCleaner = new WebInspector.TempStorageCleaner(); WebInspector.TempFile._storageCleanerPromise = new Promise(WebInspector.TempFile._clearTempStorage);
WebInspector.TempFile._storageCleaner.ensureStorageCleared(callback); return WebInspector.TempFile._storageCleanerPromise;
} }
...@@ -745,7 +745,8 @@ WebInspector.CPUProfileHeader.prototype = { ...@@ -745,7 +745,8 @@ WebInspector.CPUProfileHeader.prototype = {
{ {
this._writeToTempFile(tempFile, serializedData); this._writeToTempFile(tempFile, serializedData);
} }
new WebInspector.TempFile("cpu-profiler", String(this.uid), didCreateTempFile.bind(this)); WebInspector.TempFile.create("cpu-profiler", String(this.uid))
.then(didCreateTempFile.bind(this));
}, },
/** /**
......
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