feat: promisify dialog.showSaveDialog() (#17054)

* feat: promisify dialog.showSaveDialog()

* address some feedback from review

* filename => filePath

* fix last filename => filePath
This commit is contained in:
Shelley Vohr 2019-03-05 13:48:20 -08:00 committed by GitHub
parent 92c9dbc179
commit 6cb7b8d3a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 166 additions and 135 deletions

View file

@ -68,18 +68,21 @@ v8::Local<v8::Promise> ShowOpenDialog(
return handle;
}
void ShowSaveDialog(const file_dialog::DialogSettings& settings,
mate::Arguments* args) {
v8::Local<v8::Value> peek = args->PeekNext();
file_dialog::SaveDialogCallback callback;
if (mate::Converter<file_dialog::SaveDialogCallback>::FromV8(
args->isolate(), peek, &callback)) {
file_dialog::ShowSaveDialog(settings, callback);
} else {
base::FilePath path;
if (file_dialog::ShowSaveDialog(settings, &path))
args->Return(path);
}
void ShowSaveDialogSync(const file_dialog::DialogSettings& settings,
mate::Arguments* args) {
base::FilePath path;
if (file_dialog::ShowSaveDialogSync(settings, &path))
args->Return(path);
}
v8::Local<v8::Promise> ShowSaveDialog(
const file_dialog::DialogSettings& settings,
mate::Arguments* args) {
atom::util::Promise promise(args->isolate());
v8::Local<v8::Promise> handle = promise.GetHandle();
file_dialog::ShowSaveDialog(settings, std::move(promise));
return handle;
}
void Initialize(v8::Local<v8::Object> exports,
@ -91,6 +94,7 @@ void Initialize(v8::Local<v8::Object> exports,
dict.SetMethod("showErrorBox", &atom::ShowErrorBox);
dict.SetMethod("showOpenDialogSync", &ShowOpenDialogSync);
dict.SetMethod("showOpenDialog", &ShowOpenDialog);
dict.SetMethod("showSaveDialogSync", &ShowSaveDialogSync);
dict.SetMethod("showSaveDialog", &ShowSaveDialog);
#if defined(OS_MACOSX) || defined(OS_WIN)
dict.SetMethod("showCertificateTrustDialog",

View file

@ -5,12 +5,14 @@
#include "atom/browser/atom_download_manager_delegate.h"
#include <string>
#include <utility>
#include "atom/browser/api/atom_api_download_item.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/native_window.h"
#include "atom/browser/ui/file_dialog.h"
#include "atom/browser/web_contents_preferences.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/options_switches.h"
#include "base/bind.h"
#include "base/files/file_util.h"
@ -119,10 +121,14 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
!web_preferences || web_preferences->IsEnabled(options::kOffscreen);
settings.force_detached = offscreen;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
atom::util::Promise dialog_promise(isolate);
auto dialog_callback =
base::Bind(&AtomDownloadManagerDelegate::OnDownloadSaveDialogDone,
base::Unretained(this), download_id, callback);
file_dialog::ShowSaveDialog(settings, dialog_callback);
file_dialog::ShowSaveDialog(settings, std::move(dialog_promise));
ignore_result(dialog_promise.Then(dialog_callback));
} else {
callback.Run(path, download::DownloadItem::TARGET_DISPOSITION_PROMPT,
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path,

View file

@ -382,7 +382,7 @@ void CommonWebContentsDelegate::DevToolsSaveToFile(const std::string& url,
settings.force_detached = offscreen_;
settings.title = url;
settings.default_path = base::FilePath::FromUTF8Unsafe(url);
if (!file_dialog::ShowSaveDialog(settings, &path)) {
if (!file_dialog::ShowSaveDialogSync(settings, &path)) {
base::Value url_value(url);
web_contents_->CallClientFunction("DevToolsAPI.canceledSaveURL",
&url_value, nullptr, nullptr);

View file

@ -36,16 +36,6 @@ enum FileDialogProperty {
FILE_DIALOG_TREAT_PACKAGE_APP_AS_DIRECTORY = 1 << 7,
};
#if defined(MAS_BUILD)
typedef base::Callback<void(bool result,
const base::FilePath& path,
const std::string& bookmarkData)>
SaveDialogCallback;
#else
typedef base::Callback<void(bool result, const base::FilePath& path)>
SaveDialogCallback;
#endif
struct DialogSettings {
atom::NativeWindow* parent_window = nullptr;
std::string title;
@ -70,10 +60,10 @@ bool ShowOpenDialogSync(const DialogSettings& settings,
void ShowOpenDialog(const DialogSettings& settings,
atom::util::Promise promise);
bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path);
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path);
void ShowSaveDialog(const DialogSettings& settings,
const SaveDialogCallback& callback);
atom::util::Promise promise);
} // namespace file_dialog

View file

@ -126,8 +126,8 @@ class FileChooserDialog {
gtk_window_present_with_time(GTK_WINDOW(dialog_), time);
}
void RunSaveAsynchronous(const SaveDialogCallback& callback) {
save_callback_ = callback;
void RunSaveAsynchronous(atom::util::Promise promise) {
save_promise_.reset(new atom::util::Promise(std::move(promise)));
RunAsynchronous();
}
@ -173,7 +173,7 @@ class FileChooserDialog {
GtkWidget* preview_;
Filters filters_;
SaveDialogCallback save_callback_;
std::unique_ptr<atom::util::Promise> save_promise_;
std::unique_ptr<atom::util::Promise> open_promise_;
// Callback for when we update the preview for the selection.
@ -184,12 +184,17 @@ class FileChooserDialog {
void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) {
gtk_widget_hide(dialog_);
if (!save_callback_.is_null()) {
if (response == GTK_RESPONSE_ACCEPT)
save_callback_.Run(true, GetFileName());
else
save_callback_.Run(false, base::FilePath());
if (save_promise_) {
mate::Dictionary dict =
mate::Dictionary::CreateEmpty(save_promise_->isolate());
if (response == GTK_RESPONSE_ACCEPT) {
dict.Set("canceled", false);
dict.Set("filePath", GetFileName());
} else {
dict.Set("canceled", true);
dict.Set("filePath", base::FilePath());
}
save_promise_->Resolve(dict.GetHandle());
} else if (open_promise_) {
mate::Dictionary dict =
mate::Dictionary::CreateEmpty(open_promise_->isolate());
@ -285,23 +290,22 @@ void ShowOpenDialog(const DialogSettings& settings,
open_dialog->RunOpenAsynchronous(std::move(promise));
}
bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
FileChooserDialog save_dialog(GTK_FILE_CHOOSER_ACTION_SAVE, settings);
gtk_widget_show_all(save_dialog.dialog());
int response = gtk_dialog_run(GTK_DIALOG(save_dialog.dialog()));
if (response == GTK_RESPONSE_ACCEPT) {
*path = save_dialog.GetFileName();
return true;
} else {
return false;
}
return false;
}
void ShowSaveDialog(const DialogSettings& settings,
const SaveDialogCallback& callback) {
atom::util::Promise promise) {
FileChooserDialog* save_dialog =
new FileChooserDialog(GTK_FILE_CHOOSER_ACTION_SAVE, settings);
save_dialog->RunSaveAsynchronous(callback);
save_dialog->RunSaveAsynchronous(std::move(promise));
}
} // namespace file_dialog

View file

@ -346,7 +346,7 @@ void ShowOpenDialog(const DialogSettings& settings,
}
}
bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
DCHECK(path);
NSSavePanel* dialog = [NSSavePanel savePanel];
@ -363,50 +363,57 @@ bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
void SaveDialogCompletion(int chosen,
NSSavePanel* dialog,
bool security_scoped_bookmarks,
const SaveDialogCallback& callback) {
atom::util::Promise promise) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate());
if (chosen == NSFileHandlingPanelCancelButton) {
dict.Set("canceled", true);
dict.Set("filePath", base::FilePath());
#if defined(MAS_BUILD)
callback.Run(false, base::FilePath(), "");
#else
callback.Run(false, base::FilePath());
dict.Set("bookmark", "");
#endif
} else {
std::string path = base::SysNSStringToUTF8([[dialog URL] path]);
dict.Set("filePath", base::FilePath(path));
dict.Set("canceled", false);
#if defined(MAS_BUILD)
std::string bookmark;
if (security_scoped_bookmarks) {
bookmark = GetBookmarkDataFromNSURL([dialog URL]);
dict.Set("bookmark", bookmark);
}
callback.Run(true, base::FilePath(path), bookmark);
#else
callback.Run(true, base::FilePath(path));
#endif
}
promise.Resolve(dict.GetHandle());
}
void ShowSaveDialog(const DialogSettings& settings,
const SaveDialogCallback& c) {
atom::util::Promise promise) {
NSSavePanel* dialog = [NSSavePanel savePanel];
SetupDialog(dialog, settings);
[dialog setCanSelectHiddenExtension:YES];
__block SaveDialogCallback callback = c;
// Capture the value of the security_scoped_bookmarks settings flag
// and pass it to the completion handler.
bool security_scoped_bookmarks = settings.security_scoped_bookmarks;
__block atom::util::Promise p = std::move(promise);
if (!settings.parent_window || !settings.parent_window->GetNativeWindow() ||
settings.force_detached) {
[dialog beginWithCompletionHandler:^(NSInteger chosen) {
SaveDialogCompletion(chosen, dialog, security_scoped_bookmarks, callback);
SaveDialogCompletion(chosen, dialog, security_scoped_bookmarks,
std::move(p));
}];
} else {
NSWindow* window =
settings.parent_window->GetNativeWindow().GetNativeNSWindow();
[dialog beginSheetModalForWindow:window
completionHandler:^(NSInteger chosen) {
SaveDialogCompletion(chosen, dialog,
security_scoped_bookmarks, callback);
}];
[dialog
beginSheetModalForWindow:window
completionHandler:^(NSInteger chosen) {
SaveDialogCompletion(chosen, dialog, security_scoped_bookmarks,
std::move(p));
}];
}
}

View file

@ -101,13 +101,23 @@ void RunOpenDialogInNewThread(const RunState& run_state,
run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
}
void OnSaveDialogDone(atom::util::Promise promise,
bool canceled,
const base::FilePath path) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate());
dict.Set("canceled", canceled);
dict.Set("filePath", path);
promise.Resolve(dict.GetHandle());
}
void RunSaveDialogInNewThread(const RunState& run_state,
const DialogSettings& settings,
const SaveDialogCallback& callback) {
atom::util::Promise promise) {
base::FilePath path;
bool result = ShowSaveDialog(settings, &path);
run_state.ui_task_runner->PostTask(FROM_HERE,
base::Bind(callback, result, path));
bool result = ShowSaveDialogSync(settings, &path);
run_state.ui_task_runner->PostTask(
FROM_HERE,
base::BindOnce(&OnSaveDialogDone, std::move(promise), result, path));
run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
}
@ -275,7 +285,7 @@ void ShowOpenDialog(const DialogSettings& settings,
}
}
bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
ATL::CComPtr<IFileSaveDialog> file_save_dialog;
HRESULT hr = file_save_dialog.CoCreateInstance(CLSID_FileSaveDialog);
if (FAILED(hr))
@ -306,16 +316,18 @@ bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
}
void ShowSaveDialog(const DialogSettings& settings,
const SaveDialogCallback& callback) {
atom::util::Promise promise) {
RunState run_state;
if (!CreateDialogThread(&run_state)) {
callback.Run(false, base::FilePath());
return;
mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate());
dict.Set("canceled", false);
dict.Set("filePath", base::FilePath());
promise.Resolve(dict.GetHandle());
} else {
run_state.dialog_thread->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&RunSaveDialogInNewThread, run_state,
settings, std::move(promise)));
}
run_state.dialog_thread->task_runner()->PostTask(
FROM_HERE,
base::Bind(&RunSaveDialogInNewThread, run_state, settings, callback));
}
} // namespace file_dialog

View file

@ -56,8 +56,16 @@ class FileSelectHelper : public base::RefCounted<FileSelectHelper>,
}
void ShowSaveDialog(const file_dialog::DialogSettings& settings) {
auto callback = base::Bind(&FileSelectHelper::OnSaveDialogDone, this);
file_dialog::ShowSaveDialog(settings, callback);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
atom::util::Promise promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
file_dialog::ShowSaveDialog(settings, std::move(promise));
ignore_result(handle->Then(
context,
v8::Local<v8::Function>::Cast(mate::ConvertToV8(
isolate, base::Bind(&FileSelectHelper::OnSaveDialogDone, this)))));
}
private: