Commit 4dfb9b15 authored by Nathan Bruer's avatar Nathan Bruer Committed by Commit Bot

[Devtools] Added file path return to save response

Adds the ability for devtools to know where a file was saved on disk
when a save dialog was saved. This will allow us to serve files from
disk for interception.

R=pfeldman,dgozman
BUG=760316

Change-Id: Ic4477032219c66d00439bc7adecdb4783512a418
Reviewed-on: https://chromium-review.googlesource.com/690625Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Reviewed-by: default avatarPavel Feldman <pfeldman@chromium.org>
Commit-Queue: Blaise Bruer <allada@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505825}
parent cf6030fc
......@@ -224,7 +224,7 @@ void DevToolsFileHelper::Save(const std::string& url,
const std::string& content,
bool save_as,
const SaveCallback& saveCallback,
const SaveCallback& cancelCallback) {
const CancelCallback& cancelCallback) {
PathsMap::iterator it = saved_files_.find(url);
if (it != saved_files_.end() && !save_as) {
SaveAsFileSelected(url, content, saveCallback, it->second);
......@@ -292,7 +292,8 @@ void DevToolsFileHelper::SaveAsFileSelected(const std::string& url,
base::DictionaryValue* files_map = update.Get();
files_map->SetWithoutPathExpansion(base::MD5String(url),
base::CreateFilePathValue(path));
callback.Run();
std::string file_system_path = path.AsUTF8Unsafe();
callback.Run(file_system_path);
file_task_runner_->PostTask(FROM_HERE, BindOnce(&WriteToFile, path, content));
}
......
......@@ -58,7 +58,8 @@ class DevToolsFileHelper {
Delegate* delegate);
~DevToolsFileHelper();
typedef base::Callback<void(void)> SaveCallback;
typedef base::Callback<void(const std::string&)> SaveCallback;
typedef base::Callback<void()> CancelCallback;
typedef base::Callback<void(void)> AppendCallback;
typedef base::Callback<void(const base::string16&,
const base::Callback<void(bool)>&)>
......@@ -71,7 +72,7 @@ class DevToolsFileHelper {
const std::string& content,
bool save_as,
const SaveCallback& saveCallback,
const SaveCallback& cancelCallback);
const CancelCallback& cancelCallback);
// Append |content| to the file that has been associated with given |url|.
// The |url| can be associated with a file via calling Save method.
......
......@@ -1148,9 +1148,12 @@ void DevToolsUIBindings::DevicesUpdated(
NULL);
}
void DevToolsUIBindings::FileSavedAs(const std::string& url) {
void DevToolsUIBindings::FileSavedAs(const std::string& url,
const std::string& file_system_path) {
base::Value url_value(url);
CallClientFunction("DevToolsAPI.savedURL", &url_value, NULL, NULL);
base::Value file_system_path_value(file_system_path);
CallClientFunction("DevToolsAPI.savedURL", &url_value,
&file_system_path_value, NULL);
}
void DevToolsUIBindings::CanceledFileSaveAs(const std::string& url) {
......
......@@ -197,7 +197,7 @@ class DevToolsUIBindings : public DevToolsEmbedderMessageDispatcher::Delegate,
const std::vector<std::string>& removed_paths) override;
// DevToolsFileHelper callbacks.
void FileSavedAs(const std::string& url);
void FileSavedAs(const std::string& url, const std::string& file_system_path);
void CanceledFileSaveAs(const std::string& url);
void AppendedTo(const std::string& url);
void IndexingTotalWorkCalculated(int request_id,
......
......@@ -44,7 +44,7 @@ function test() {
var savedSnapshotData;
function saveMock(url, data) {
savedSnapshotData = data;
setTimeout(() => Workspace.fileManager._savedURL(true, {data: url}), 0);
setTimeout(() => Workspace.fileManager._savedURL({data: {url: url}}), 0);
}
TestRunner.override(InspectorFrontendHost, 'save', saveMock);
......
......@@ -194,10 +194,10 @@ Bindings.FileOutputStream = class {
/** @type {!Array<function()>} */
this._writeCallbacks = [];
this._fileName = fileName;
var success = await Workspace.fileManager.save(this._fileName, '', true);
if (success)
var saveResponse = await Workspace.fileManager.save(this._fileName, '', true);
if (saveResponse)
Workspace.fileManager.addEventListener(Workspace.FileManager.Events.AppendedToURL, this._onAppendDone, this);
return success;
return !!saveResponse;
}
/**
......
......@@ -250,9 +250,10 @@
/**
* @param {string} url
* @param {string=} fileSystemPath
*/
savedURL(url) {
this._dispatchOnInspectorFrontendAPI('savedURL', [url]);
savedURL(url, fileSystemPath) {
this._dispatchOnInspectorFrontendAPI('savedURL', [url, fileSystemPath]);
}
/**
......
......@@ -89,7 +89,7 @@ InspectorFrontendHostAPI.EventDescriptors = [
[InspectorFrontendHostAPI.Events.KeyEventUnhandled, 'keyEventUnhandled', ['event']],
[InspectorFrontendHostAPI.Events.ReloadInspectedPage, 'reloadInspectedPage', ['hard']],
[InspectorFrontendHostAPI.Events.RevealSourceLine, 'revealSourceLine', ['url', 'lineNumber', 'columnNumber']],
[InspectorFrontendHostAPI.Events.SavedURL, 'savedURL', ['url']],
[InspectorFrontendHostAPI.Events.SavedURL, 'savedURL', ['url', 'fileSystemPath']],
[InspectorFrontendHostAPI.Events.SearchCompleted, 'searchCompleted', ['requestId', 'fileSystemPath', 'files']],
[InspectorFrontendHostAPI.Events.SetInspectedTabId, 'setInspectedTabId', ['tabId']],
[InspectorFrontendHostAPI.Events.SetUseSoftMenu, 'setUseSoftMenu', ['useSoftMenu']],
......
......@@ -35,12 +35,11 @@ Workspace.FileManager = class extends Common.Object {
constructor() {
super();
this._savedURLsSetting = Common.settings.createLocalSetting('savedURLs', {});
/** @type {!Map<string, function(boolean)>} */
/** @type {!Map<string, function(?{fileSystemPath: (string|undefined)})>} */
this._saveCallbacks = new Map();
InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.SavedURL, this._savedURL, this);
InspectorFrontendHost.events.addEventListener(
InspectorFrontendHostAPI.Events.SavedURL, this._savedURL.bind(this, true), this);
InspectorFrontendHost.events.addEventListener(
InspectorFrontendHostAPI.Events.CanceledSaveURL, this._savedURL.bind(this, false), this);
InspectorFrontendHostAPI.Events.CanceledSaveURL, this._canceledSavedURL, this);
InspectorFrontendHost.events.addEventListener(
InspectorFrontendHostAPI.Events.AppendedToURL, this._appendedToURL, this);
}
......@@ -49,7 +48,7 @@ Workspace.FileManager = class extends Common.Object {
* @param {string} url
* @param {string} content
* @param {boolean} forceSaveAs
* @return {!Promise<boolean>}
* @return {!Promise<?{fileSystemPath: (string|undefined)}>}
*/
save(url, content, forceSaveAs) {
// Remove this url from the saved URLs while it is being saved.
......@@ -61,21 +60,28 @@ Workspace.FileManager = class extends Common.Object {
}
/**
* @param {boolean} success
* @param {!Common.Event} event
*/
_savedURL(success, event) {
var url = /** @type {string} */ (event.data);
_savedURL(event) {
var url = /** @type {string} */ (event.data.url);
var callback = this._saveCallbacks.get(url);
this._saveCallbacks.delete(url);
if (callback)
callback(success);
if (!success)
return;
callback({fileSystemPath: /** @type {string} */ (event.data.fileSystemPath)});
var savedURLs = this._savedURLsSetting.get();
savedURLs[url] = true;
this._savedURLsSetting.set(savedURLs);
this.dispatchEventToListeners(Workspace.FileManager.Events.SavedURL, url);
}
/**
* @param {!Common.Event} event
*/
_canceledSavedURL(event) {
var url = /** @type {string} */ (event.data);
var callback = this._saveCallbacks.get(url);
this._saveCallbacks.delete(url);
if (callback)
callback(null);
}
/**
......@@ -113,7 +119,6 @@ Workspace.FileManager = class extends Common.Object {
/** @enum {symbol} */
Workspace.FileManager.Events = {
SavedURL: Symbol('SavedURL'),
AppendedToURL: Symbol('AppendedToURL')
};
......
......@@ -345,9 +345,10 @@ Workspace.UISourceCode = class extends Common.Object {
}
saveAs() {
Workspace.fileManager.save(this._url, this.workingCopy(), true).then(accepted => {
if (accepted)
this._contentCommitted(this.workingCopy(), true);
Workspace.fileManager.save(this._url, this.workingCopy(), true).then(saveResponse => {
if (!saveResponse)
return;
this._contentCommitted(this.workingCopy(), true);
});
Workspace.fileManager.close(this._url);
}
......
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