Commit 1ff14c26 authored by yurys@chromium.org's avatar yurys@chromium.org

Clear DevTools temp storage

Now that DevTools caches CPU and heap profiles in temporary files we need to be careful to remove these files. The files are deleted in case a profile is removed as a result of user action and on DevTools shutdown.

To make sure we don't leave any garbage the temproary storage is also cleared on first attempt to create a new temp file. A SharedWorker is used to synchronize between DevTools front-ends when clearing temp files to avoid removing temp files that might be used by another front-end.

BUG=327298,324769
R=alph@chromium.org, pfeldman@chromium.org

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

git-svn-id: svn://svn.chromium.org/blink/trunk@165135 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 41d91b8e
......@@ -54,6 +54,7 @@
'concatenated_devtools_layers_js',
'concatenated_heap_snapshot_worker_js',
'concatenated_script_formatter_worker_js',
'concatenated_temp_storage_shared_worker_js',
'concatenated_devtools_css'],
}],
],
......@@ -151,6 +152,7 @@
'concatenated_devtools_layers_js',
'concatenated_heap_snapshot_worker_js',
'concatenated_script_formatter_worker_js',
'concatenated_temp_storage_shared_worker_js',
'concatenated_devtools_css'],
'actions': [{
'action_name': 'generate_devtools_grd',
......@@ -169,6 +171,7 @@
'<(PRODUCT_DIR)/resources/inspector/CodeMirrorTextEditor.js',
'<(PRODUCT_DIR)/resources/inspector/HeapSnapshotWorker.js',
'<(PRODUCT_DIR)/resources/inspector/ScriptFormatterWorker.js',
'<(PRODUCT_DIR)/resources/inspector/TempStorageSharedWorker.js',
'<(PRODUCT_DIR)/resources/inspector/inspector.css',
'<(PRODUCT_DIR)/resources/inspector/devtools_extension_api.js',
'<@(devtools_standalone_files)',
......@@ -442,6 +445,22 @@
'action': ['python', '<@(_script_name)', '<@(_input_file)', '<@(_search_path)', '<@(_outputs)'],
}],
},
{
'target_name': 'concatenated_temp_storage_shared_worker_js',
'type': 'none',
'actions': [{
'action_name': 'concatenate_temp_storage_shared_worker_js',
'script_name': 'scripts/inline_js_imports.py',
'input_file': 'front_end/TempStorageSharedWorker.js',
'inputs': [
'<@(_script_name)',
'<@(devtools_temp_storage_shared_worker_js_files)'
],
'search_path': 'front_end',
'outputs': ['<(PRODUCT_DIR)/resources/inspector/TempStorageSharedWorker.js'],
'action': ['python', '<@(_script_name)', '<@(_input_file)', '<@(_search_path)', '<@(_outputs)'],
}],
},
{
'target_name': 'concatenated_devtools_layers_js',
'type': 'none',
......
......@@ -466,5 +466,8 @@
'front_end/ExtensionAPI.js',
'front_end/DevToolsExtensionAPI.js'
],
'devtools_temp_storage_shared_worker_js_files': [
'front_end/TempStorageSharedWorker.js',
],
},
}
......@@ -895,6 +895,14 @@ WebInspector.CPUProfileHeader.prototype = {
close: function() { },
/**
* @override
*/
dispose: function()
{
this.removeTempFile();
},
/**
* @override
* @return {!WebInspector.ProfileSidebarTreeElement}
......
......@@ -1291,6 +1291,7 @@ WebInspector.HeapProfileHeader.prototype = {
this._view = null;
view.dispose();
}
this.removeTempFile();
},
_updateSubtitle: function(value)
......
......@@ -39,6 +39,8 @@ WebInspector.ProfileType = function(id, name)
this.treeElement = null;
/** @type {?WebInspector.ProfileHeader} */
this._profileBeingRecorded = null;
window.addEventListener("unload", this._clearTempStorage.bind(this), false);
}
WebInspector.ProfileType.Events = {
......@@ -206,6 +208,12 @@ WebInspector.ProfileType.prototype = {
}
},
_clearTempStorage: function()
{
for (var i = 0; i < this._profiles.length; ++i)
this._profiles[i].removeTempFile();
},
/**
* @nosideeffects
* @return {?WebInspector.ProfileHeader}
......@@ -297,6 +305,12 @@ WebInspector.ProfileHeader.prototype = {
throw new Error("Not implemented.");
},
removeTempFile: function()
{
if (this._tempFile)
this._tempFile.remove();
},
dispose: function()
{
},
......
......@@ -107,8 +107,16 @@ WebInspector.TempFile = function(dirPath, name, callback)
WebInspector.ConsoleMessage.MessageLevel.Error);
callback(null);
}
var boundErrorHandler = errorHandler.bind(this)
window.requestFileSystem(window.TEMPORARY, 10, didInitFs.bind(this), errorHandler.bind(this));
/**
* @this {WebInspector.TempFile}
*/
function didClearTempStorage()
{
window.requestFileSystem(window.TEMPORARY, 10, didInitFs.bind(this), boundErrorHandler);
}
WebInspector.TempFile._ensureTempStorageCleared(didClearTempStorage.bind(this));
}
WebInspector.TempFile.prototype = {
......@@ -185,6 +193,12 @@ WebInspector.TempFile.prototype = {
callback(null);
}
this._fileEntry.file(callback, didFailToGetFile.bind(this));
},
remove: function()
{
if (this._fileEntry)
this._fileEntry.remove(function() {});
}
}
......@@ -279,3 +293,61 @@ WebInspector.BufferedTempFileWriter.prototype = {
this._finishCallback(this._tempFile);
}
}
/**
* @constructor
*/
WebInspector.TempStorageCleaner = function()
{
this._worker = new SharedWorker("TempStorageSharedWorker.js", "TempStorage");
this._callbacks = [];
this._worker.port.onmessage = this._handleMessage.bind(this);
this._worker.port.onerror = this._handleError.bind(this);
}
WebInspector.TempStorageCleaner.prototype = {
/**
* @param {!function()} callback
*/
ensureStorageCleared: function(callback)
{
if (this._callbacks)
this._callbacks.push(callback);
else
callback();
},
_handleMessage: function(event)
{
if (event.data.type === "tempStorageCleared") {
if (event.data.error)
WebInspector.log(event.data.error, WebInspector.ConsoleMessage.MessageLevel.Error);
this._notifyCallbacks();
}
},
_handleError: function(event)
{
WebInspector.log(WebInspector.UIString("Failed to clear temp storage: %s", event.data),
WebInspector.ConsoleMessage.MessageLevel.Error);
this._notifyCallbacks();
},
_notifyCallbacks: function()
{
var callbacks = this._callbacks;
this._callbacks = null;
for (var i = 0; i < callbacks.length; i++)
callbacks[i]();
}
}
/**
* @param {!function()} callback
*/
WebInspector.TempFile._ensureTempStorageCleared = function(callback)
{
if (!WebInspector.TempFile._storageCleaner)
WebInspector.TempFile._storageCleaner = new WebInspector.TempStorageCleaner();
WebInspector.TempFile._storageCleaner.ensureStorageCleared(callback);
}
/*
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var ports = [];
var isTempStorageCleared = false;
/**
* @type {string}
*/
var tempStorageError;
/**
* @param {!MessageEvent} event
*/
self.onconnect = function(event)
{
var newPort = /** @type {!MessagePort} */ (event.ports[0]);
if (isTempStorageCleared) {
notifyTempStorageCleared(newPort);
return;
}
newPort.onmessage = handleMessage;
newPort.onerror = handleError;
ports.push(newPort);
if (ports.length === 1)
clearTempStorage();
}
function clearTempStorage()
{
function didFail(e)
{
tempStorageError = "Failed to clear temp storage: " + e.message + " " + e.name;
console.error(tempStorageError);
didClearTempStorage();
}
/**
* @param {!FileSystem} fs
*/
function didGetFS(fs)
{
fs.root.createReader().readEntries(didReadEntries, didFail);
}
/**
* @param {!Array.<!Entry>} entries
*/
function didReadEntries(entries)
{
var remainingEntries = entries.length;
if (!remainingEntries) {
didClearTempStorage();
return;
}
function didDeleteEntry()
{
if (!--remainingEntries)
didClearTempStorage();
}
function failedToDeleteEntry(e)
{
tempStorageError = "Failed to delete entry: " + e.message + " " + e.name;
console.error(tempStorageError);
didDeleteEntry();
}
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
if (entry.isFile)
entry.remove(didDeleteEntry, failedToDeleteEntry);
else
entry.removeRecursively(didDeleteEntry, failedToDeleteEntry);
}
}
self.webkitRequestFileSystem(self.TEMPORARY, 10, didGetFS, didFail);
}
function didClearTempStorage()
{
isTempStorageCleared = true;
for (var i = 0; i < ports.length; i++)
notifyTempStorageCleared(ports[i]);
ports = null;
}
/**
* @param {!MessagePort} port
*/
function notifyTempStorageCleared(port)
{
port.postMessage({
type: "tempStorageCleared",
error: tempStorageError
});
}
function handleMessage(event)
{
if (event.data.type === "disconnect")
removePort(event.target);
}
function handleError(event)
{
console.error("Error: " + event.data);
removePort(event.target);
}
function removePort(port)
{
if (!ports)
return;
var index = ports.indexOf(port);
ports.splice(index, 1);
}
......@@ -325,6 +325,13 @@
"TestController.js"
]
},
{
"name": "temp_storage_shared_worker",
"dependencies": [],
"sources": [
"TempStorageSharedWorker.js"
]
},
{
"name": "profiler",
"dependencies": ["components", "workers"],
......
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