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 = {
* @param {!string} name
* @param {!function(?WebInspector.TempFile)} callback
*/
InspectorTest.TempFileMock = function(dirPath, name, callback)
InspectorTest.TempFileMock = function(dirPath, name)
{
this._chunks = [];
this._name = name;
setTimeout(callback.bind(this, this), 1);
}
InspectorTest.TempFileMock.prototype = {
......@@ -527,6 +526,12 @@ InspectorTest.TempFileMock.prototype = {
remove: function() { }
}
InspectorTest.TempFileMock.create = function(dirPath, name)
{
var tempFile = new InspectorTest.TempFileMock(dirPath, name);
return Promise.resolve(tempFile);
}
InspectorTest.dumpLoadedModules = function(next)
{
function moduleSorter(left, right)
......@@ -581,8 +586,6 @@ InspectorTest.TimeoutMock.prototype = {
}
}
WebInspector.TempFile = InspectorTest.TempFileMock;
WebInspector.targetManager.observeTargets({
targetAdded: function(target)
{
......
var initialize_Timeline = function() {
InspectorTest.preloadPanel("timeline");
WebInspector.TempFile = InspectorTest.TempFileMock;
// Scrub values when printing out these properties in the record or data field.
InspectorTest.timelinePropertyFormatters = {
......
var initialize_ProfilerTest = function() {
InspectorTest.preloadPanel("profiles");
WebInspector.TempFile = InspectorTest.TempFileMock;
InspectorTest.startProfilerTest = function(callback)
{
......
......@@ -5,13 +5,13 @@
function test()
{
var cleaner = new WebInspector.TempStorageCleaner();
function finish()
{
InspectorTest.addResult("PASSED: we got a call from TempStorageCleaner that it had done the job.");
InspectorTest.completeTest();
}
cleaner.ensureStorageCleared(finish);
WebInspector.TempFile.ensureTempStorageCleared()
.then(function finish() {
InspectorTest.addResult("PASSED: we got a call from TempStorageCleaner that it had done the job.");
}, function(e) {
InspectorTest.addResult("FAILED: we got a rejection: " + e);
})
.then(InspectorTest.completeTest);
}
</script>
......
......@@ -2,6 +2,7 @@ function initialize_TracingTest()
{
InspectorTest.preloadPanel("timeline");
WebInspector.TempFile = InspectorTest.TempFileMock;
InspectorTest.tracingManager = function()
{
......
......@@ -32,90 +32,97 @@ 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, 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 +233,7 @@ WebInspector.TempFile.prototype = {
*/
WebInspector.DeferredTempFile = function(dirPath, name)
{
/** @type {?Array.<string>} */
this._chunks = [];
this._tempFile = null;
this._isWriting = false;
......@@ -233,7 +241,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 +272,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 +292,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 +301,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 +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)
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;
}
......@@ -745,7 +745,8 @@ WebInspector.CPUProfileHeader.prototype = {
{
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