2015-01-23 21:03:47 +01:00
|
|
|
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/dialog_handler_gtk.h"
|
2015-01-30 19:55:55 +01:00
|
|
|
|
2015-03-10 19:54:05 +01:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2024-05-29 22:38:00 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
2015-01-23 21:03:47 +01:00
|
|
|
#include "include/cef_browser.h"
|
2015-04-09 16:59:34 +02:00
|
|
|
#include "include/cef_parser.h"
|
2015-01-23 21:03:47 +01:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/root_window.h"
|
2018-09-20 15:00:14 +02:00
|
|
|
#include "tests/cefclient/browser/util_gtk.h"
|
2024-05-29 22:38:00 +02:00
|
|
|
#include "tests/shared/common/string_util.h"
|
2015-01-23 21:03:47 +01:00
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kPromptTextId[] = "cef_prompt_text";
|
|
|
|
|
|
|
|
// If there's a text entry in the dialog, get the text from the first one and
|
|
|
|
// return it.
|
|
|
|
std::string GetPromptText(GtkDialog* dialog) {
|
|
|
|
GtkWidget* widget = static_cast<GtkWidget*>(
|
|
|
|
g_object_get_data(G_OBJECT(dialog), kPromptTextId));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (widget) {
|
2015-01-23 21:03:47 +01:00
|
|
|
return gtk_entry_get_text(GTK_ENTRY(widget));
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetDescriptionFromMimeType(const std::string& mime_type) {
|
|
|
|
// Check for wild card mime types and return an appropriate description.
|
|
|
|
static const struct {
|
|
|
|
const char* mime_type;
|
|
|
|
const char* label;
|
|
|
|
} kWildCardMimeTypes[] = {
|
2017-05-17 11:29:28 +02:00
|
|
|
{"audio", "Audio Files"},
|
|
|
|
{"image", "Image Files"},
|
|
|
|
{"text", "Text Files"},
|
|
|
|
{"video", "Video Files"},
|
2015-01-23 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0;
|
|
|
|
i < sizeof(kWildCardMimeTypes) / sizeof(kWildCardMimeTypes[0]); ++i) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (mime_type == std::string(kWildCardMimeTypes[i].mime_type) + "/*") {
|
2015-01-23 21:03:47 +01:00
|
|
|
return std::string(kWildCardMimeTypes[i].label);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
2024-05-29 22:38:00 +02:00
|
|
|
LOG(WARNING) << "Unrecognized mime type: " << mime_type;
|
2015-01-23 21:03:47 +01:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddFilters(GtkFileChooser* chooser,
|
|
|
|
const std::vector<CefString>& accept_filters,
|
2024-05-29 22:38:00 +02:00
|
|
|
const std::vector<CefString>& accept_extensions,
|
|
|
|
const std::vector<CefString>& accept_descriptions,
|
2015-01-23 21:03:47 +01:00
|
|
|
bool include_all_files,
|
|
|
|
std::vector<GtkFileFilter*>* filters) {
|
|
|
|
bool has_filter = false;
|
|
|
|
|
2021-12-16 23:35:54 +01:00
|
|
|
for (size_t j = 0; j < accept_filters.size(); ++j) {
|
|
|
|
const std::string& filter = accept_filters[j];
|
2023-01-02 23:59:03 +01:00
|
|
|
if (filter.empty()) {
|
2015-01-23 21:03:47 +01:00
|
|
|
continue;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2024-05-29 22:38:00 +02:00
|
|
|
// Extensions and descriptions may be provided.
|
|
|
|
std::vector<std::string> extensions =
|
|
|
|
AsciiStrSplit(accept_extensions[j], ';');
|
|
|
|
std::string description = accept_descriptions[j];
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2024-05-29 22:38:00 +02:00
|
|
|
if (extensions.empty()) {
|
|
|
|
// Try to convert mime type to one or more extensions.
|
2015-01-23 21:03:47 +01:00
|
|
|
std::vector<CefString> ext;
|
|
|
|
CefGetExtensionsForMimeType(filter, ext);
|
2023-01-02 23:59:03 +01:00
|
|
|
for (size_t x = 0; x < ext.size(); ++x) {
|
2015-01-23 21:03:47 +01:00
|
|
|
extensions.push_back("." + ext[x].ToString());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (extensions.empty()) {
|
2015-01-23 21:03:47 +01:00
|
|
|
continue;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2024-05-29 22:38:00 +02:00
|
|
|
if (description.empty() && filter[0] != '.') {
|
|
|
|
description = GetDescriptionFromMimeType(filter);
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:03:47 +01:00
|
|
|
GtkFileFilter* gtk_filter = gtk_file_filter_new();
|
|
|
|
|
|
|
|
std::string ext_str;
|
|
|
|
for (size_t x = 0; x < extensions.size(); ++x) {
|
|
|
|
const std::string& pattern = "*" + extensions[x];
|
2023-01-02 23:59:03 +01:00
|
|
|
if (x != 0) {
|
2015-01-23 21:03:47 +01:00
|
|
|
ext_str += ";";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
ext_str += pattern;
|
|
|
|
gtk_file_filter_add_pattern(gtk_filter, pattern.c_str());
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (description.empty()) {
|
2015-01-23 21:03:47 +01:00
|
|
|
description = ext_str;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2015-01-23 21:03:47 +01:00
|
|
|
description += " (" + ext_str + ")";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
|
|
|
|
gtk_file_filter_set_name(gtk_filter, description.c_str());
|
|
|
|
gtk_file_chooser_add_filter(chooser, gtk_filter);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!has_filter) {
|
2015-01-23 21:03:47 +01:00
|
|
|
has_filter = true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
|
|
|
|
filters->push_back(gtk_filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the *.* filter, but only if we have added other filters (otherwise it
|
|
|
|
// is implied).
|
|
|
|
if (include_all_files && has_filter) {
|
|
|
|
GtkFileFilter* filter = gtk_file_filter_new();
|
|
|
|
gtk_file_filter_add_pattern(filter, "*");
|
|
|
|
gtk_file_filter_set_name(filter, "All Files (*)");
|
|
|
|
gtk_file_chooser_add_filter(chooser, filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
GtkWindow* GetWindow(CefRefPtr<CefBrowser> browser) {
|
2018-09-20 15:00:14 +02:00
|
|
|
REQUIRE_MAIN_THREAD();
|
2015-01-30 19:55:55 +01:00
|
|
|
scoped_refptr<RootWindow> root_window =
|
|
|
|
RootWindow::GetForBrowser(browser->GetIdentifier());
|
2016-01-19 21:09:01 +01:00
|
|
|
if (root_window) {
|
2021-03-01 20:41:27 +01:00
|
|
|
GtkWidget* window = root_window->GetWindowHandle();
|
2022-04-15 21:55:23 +02:00
|
|
|
DCHECK(window);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!window) {
|
2016-01-19 21:09:01 +01:00
|
|
|
LOG(ERROR) << "No GtkWindow for browser";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-03-01 20:41:27 +01:00
|
|
|
return GTK_WINDOW(window);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
2020-01-15 15:28:12 +01:00
|
|
|
return nullptr;
|
2015-01-30 19:55:55 +01:00
|
|
|
}
|
|
|
|
|
2024-05-29 22:38:00 +02:00
|
|
|
// Returns true if |accept_filters| contains a MIME type value without matching
|
|
|
|
// extensions in |accept_extensions|.
|
|
|
|
bool MissingMimeTypeData(const std::vector<CefString>& accept_filters,
|
|
|
|
const std::vector<CefString>& accept_extensions) {
|
|
|
|
if (accept_filters.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < accept_filters.size(); ++i) {
|
|
|
|
const std::string& filter = accept_filters[i];
|
|
|
|
if (filter[0] != '.' && accept_extensions[i].empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:03:47 +01:00
|
|
|
} // namespace
|
|
|
|
|
2020-01-15 15:28:12 +01:00
|
|
|
ClientDialogHandlerGtk::ClientDialogHandlerGtk() : gtk_dialog_(nullptr) {}
|
2015-01-23 21:03:47 +01:00
|
|
|
|
|
|
|
bool ClientDialogHandlerGtk::OnFileDialog(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
FileDialogMode mode,
|
|
|
|
const CefString& title,
|
|
|
|
const CefString& default_file_path,
|
|
|
|
const std::vector<CefString>& accept_filters,
|
2024-05-29 22:38:00 +02:00
|
|
|
const std::vector<CefString>& accept_extensions,
|
|
|
|
const std::vector<CefString>& accept_descriptions,
|
2015-01-23 21:03:47 +01:00
|
|
|
CefRefPtr<CefFileDialogCallback> callback) {
|
2018-09-20 15:00:14 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
2024-06-04 17:19:50 +02:00
|
|
|
DCHECK((accept_filters.size() == accept_extensions.size()) ==
|
2024-05-29 22:38:00 +02:00
|
|
|
accept_descriptions.size());
|
|
|
|
|
|
|
|
if (MissingMimeTypeData(accept_filters, accept_extensions)) {
|
|
|
|
// Wait for the 2nd call that provides MIME type data.
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-20 15:00:14 +02:00
|
|
|
|
|
|
|
OnFileDialogParams params;
|
|
|
|
params.browser = browser;
|
|
|
|
params.mode = mode;
|
|
|
|
params.title = title;
|
|
|
|
params.default_file_path = default_file_path;
|
|
|
|
params.accept_filters = accept_filters;
|
2024-05-29 22:38:00 +02:00
|
|
|
params.accept_extensions = accept_extensions;
|
|
|
|
params.accept_descriptions = accept_descriptions;
|
2018-09-20 15:00:14 +02:00
|
|
|
params.callback = callback;
|
|
|
|
|
|
|
|
GetWindowAndContinue(
|
2021-06-19 21:54:45 +02:00
|
|
|
browser, base::BindOnce(&ClientDialogHandlerGtk::OnFileDialogContinue,
|
|
|
|
this, params));
|
2018-09-20 15:00:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientDialogHandlerGtk::OnJSDialog(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& origin_url,
|
|
|
|
JSDialogType dialog_type,
|
|
|
|
const CefString& message_text,
|
|
|
|
const CefString& default_prompt_text,
|
|
|
|
CefRefPtr<CefJSDialogCallback> callback,
|
|
|
|
bool& suppress_message) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
OnJSDialogParams params;
|
|
|
|
params.browser = browser;
|
|
|
|
params.origin_url = origin_url;
|
|
|
|
params.dialog_type = dialog_type;
|
|
|
|
params.message_text = message_text;
|
|
|
|
params.default_prompt_text = default_prompt_text;
|
|
|
|
params.callback = callback;
|
|
|
|
|
|
|
|
GetWindowAndContinue(
|
2021-06-19 21:54:45 +02:00
|
|
|
browser, base::BindOnce(&ClientDialogHandlerGtk::OnJSDialogContinue, this,
|
|
|
|
params));
|
2018-09-20 15:00:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientDialogHandlerGtk::OnBeforeUnloadDialog(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& message_text,
|
|
|
|
bool is_reload,
|
|
|
|
CefRefPtr<CefJSDialogCallback> callback) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
const std::string& new_message_text =
|
|
|
|
message_text.ToString() + "\n\nIs it OK to leave/reload this page?";
|
|
|
|
bool suppress_message = false;
|
|
|
|
|
|
|
|
return OnJSDialog(browser, CefString(), JSDIALOGTYPE_CONFIRM,
|
|
|
|
new_message_text, CefString(), callback, suppress_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDialogHandlerGtk::OnResetDialogState(CefRefPtr<CefBrowser> browser) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!gtk_dialog_) {
|
2018-09-20 15:00:14 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-09-20 15:00:14 +02:00
|
|
|
|
|
|
|
gtk_widget_destroy(gtk_dialog_);
|
2020-01-15 15:28:12 +01:00
|
|
|
gtk_dialog_ = nullptr;
|
|
|
|
js_dialog_callback_ = nullptr;
|
2018-09-20 15:00:14 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
void ClientDialogHandlerGtk::OnFileDialogContinue(
|
|
|
|
const OnFileDialogParams& params,
|
|
|
|
GtkWindow* window) {
|
2021-02-12 20:46:38 +01:00
|
|
|
REQUIRE_MAIN_THREAD();
|
2018-09-20 15:00:14 +02:00
|
|
|
|
|
|
|
ScopedGdkThreadsEnter scoped_gdk_threads;
|
|
|
|
|
2015-01-23 21:03:47 +01:00
|
|
|
std::vector<CefString> files;
|
|
|
|
|
|
|
|
GtkFileChooserAction action;
|
|
|
|
const gchar* accept_button;
|
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
if (params.mode == FILE_DIALOG_OPEN ||
|
|
|
|
params.mode == FILE_DIALOG_OPEN_MULTIPLE) {
|
2015-01-23 21:03:47 +01:00
|
|
|
action = GTK_FILE_CHOOSER_ACTION_OPEN;
|
2021-03-01 20:41:27 +01:00
|
|
|
accept_button = "_Open";
|
2022-04-15 21:55:23 +02:00
|
|
|
} else if (params.mode == FILE_DIALOG_OPEN_FOLDER) {
|
2015-01-23 21:03:47 +01:00
|
|
|
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
|
2021-03-01 20:41:27 +01:00
|
|
|
accept_button = "_Open";
|
2022-04-15 21:55:23 +02:00
|
|
|
} else if (params.mode == FILE_DIALOG_SAVE) {
|
2015-01-23 21:03:47 +01:00
|
|
|
action = GTK_FILE_CHOOSER_ACTION_SAVE;
|
2021-03-01 20:41:27 +01:00
|
|
|
accept_button = "_Save";
|
2015-01-23 21:03:47 +01:00
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
2018-09-20 15:00:14 +02:00
|
|
|
params.callback->Cancel();
|
2018-09-25 13:23:40 +02:00
|
|
|
return;
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string title_str;
|
2018-09-20 15:00:14 +02:00
|
|
|
if (!params.title.empty()) {
|
|
|
|
title_str = params.title;
|
2015-01-23 21:03:47 +01:00
|
|
|
} else {
|
2022-04-15 21:55:23 +02:00
|
|
|
switch (params.mode) {
|
2015-01-23 21:03:47 +01:00
|
|
|
case FILE_DIALOG_OPEN:
|
|
|
|
title_str = "Open File";
|
|
|
|
break;
|
|
|
|
case FILE_DIALOG_OPEN_MULTIPLE:
|
|
|
|
title_str = "Open Files";
|
|
|
|
break;
|
|
|
|
case FILE_DIALOG_OPEN_FOLDER:
|
|
|
|
title_str = "Open Folder";
|
|
|
|
break;
|
|
|
|
case FILE_DIALOG_SAVE:
|
|
|
|
title_str = "Save File";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget* dialog = gtk_file_chooser_dialog_new(
|
2021-03-01 20:41:27 +01:00
|
|
|
title_str.c_str(), GTK_WINDOW(window), action, "_Cancel",
|
2021-06-17 22:08:01 +02:00
|
|
|
GTK_RESPONSE_CANCEL, accept_button, GTK_RESPONSE_ACCEPT, nullptr);
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (params.mode == FILE_DIALOG_OPEN_MULTIPLE) {
|
2015-01-23 21:03:47 +01:00
|
|
|
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
if (!params.default_file_path.empty() && params.mode == FILE_DIALOG_SAVE) {
|
2018-09-20 15:00:14 +02:00
|
|
|
const std::string& file_path = params.default_file_path;
|
2015-03-10 19:54:05 +01:00
|
|
|
bool exists = false;
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
struct stat sb;
|
|
|
|
if (stat(file_path.c_str(), &sb) == 0 && S_ISREG(sb.st_mode)) {
|
|
|
|
// Use the directory and name of the existing file.
|
2017-05-17 11:29:28 +02:00
|
|
|
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), file_path.data());
|
2015-11-17 19:20:13 +01:00
|
|
|
exists = true;
|
2015-03-10 19:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
// Set the current file name but let the user choose the directory.
|
|
|
|
std::string file_name_str = file_path;
|
|
|
|
const char* file_name = basename(const_cast<char*>(file_name_str.data()));
|
|
|
|
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), file_name);
|
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<GtkFileFilter*> filters;
|
2024-05-29 22:38:00 +02:00
|
|
|
AddFilters(GTK_FILE_CHOOSER(dialog), params.accept_filters,
|
|
|
|
params.accept_extensions, params.accept_descriptions, true,
|
|
|
|
&filters);
|
2015-01-23 21:03:47 +01:00
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
|
2022-04-15 21:55:23 +02:00
|
|
|
if (params.mode == FILE_DIALOG_OPEN ||
|
|
|
|
params.mode == FILE_DIALOG_OPEN_FOLDER ||
|
|
|
|
params.mode == FILE_DIALOG_SAVE) {
|
2015-01-23 21:03:47 +01:00
|
|
|
char* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
|
|
|
|
files.push_back(std::string(filename));
|
|
|
|
success = true;
|
2022-04-15 21:55:23 +02:00
|
|
|
} else if (params.mode == FILE_DIALOG_OPEN_MULTIPLE) {
|
2015-01-23 21:03:47 +01:00
|
|
|
GSList* filenames =
|
|
|
|
gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
|
|
|
|
if (filenames) {
|
2020-01-15 15:28:12 +01:00
|
|
|
for (GSList* iter = filenames; iter != nullptr;
|
2015-01-23 21:03:47 +01:00
|
|
|
iter = g_slist_next(iter)) {
|
|
|
|
std::string path(static_cast<char*>(iter->data));
|
|
|
|
g_free(iter->data);
|
|
|
|
files.push_back(path);
|
|
|
|
}
|
|
|
|
g_slist_free(filenames);
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (success) {
|
2022-04-15 21:55:23 +02:00
|
|
|
params.callback->Continue(files);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2018-09-20 15:00:14 +02:00
|
|
|
params.callback->Cancel();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
void ClientDialogHandlerGtk::OnJSDialogContinue(const OnJSDialogParams& params,
|
2018-09-20 15:00:14 +02:00
|
|
|
GtkWindow* window) {
|
2021-02-12 20:46:38 +01:00
|
|
|
REQUIRE_MAIN_THREAD();
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
ScopedGdkThreadsEnter scoped_gdk_threads;
|
|
|
|
|
2015-01-23 21:03:47 +01:00
|
|
|
GtkButtonsType buttons = GTK_BUTTONS_NONE;
|
|
|
|
GtkMessageType gtk_message_type = GTK_MESSAGE_OTHER;
|
|
|
|
std::string title;
|
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
switch (params.dialog_type) {
|
2015-01-23 21:03:47 +01:00
|
|
|
case JSDIALOGTYPE_ALERT:
|
|
|
|
buttons = GTK_BUTTONS_NONE;
|
|
|
|
gtk_message_type = GTK_MESSAGE_WARNING;
|
|
|
|
title = "JavaScript Alert";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSDIALOGTYPE_CONFIRM:
|
|
|
|
buttons = GTK_BUTTONS_CANCEL;
|
|
|
|
gtk_message_type = GTK_MESSAGE_QUESTION;
|
|
|
|
title = "JavaScript Confirm";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSDIALOGTYPE_PROMPT:
|
|
|
|
buttons = GTK_BUTTONS_CANCEL;
|
|
|
|
gtk_message_type = GTK_MESSAGE_QUESTION;
|
|
|
|
title = "JavaScript Prompt";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
js_dialog_callback_ = params.callback;
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
if (!params.origin_url.empty()) {
|
2015-01-23 21:03:47 +01:00
|
|
|
title += " - ";
|
2018-09-20 15:00:14 +02:00
|
|
|
title += CefFormatUrlForSecurityDisplay(params.origin_url).ToString();
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
gtk_dialog_ = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL,
|
|
|
|
gtk_message_type, buttons, "%s",
|
2018-09-20 15:00:14 +02:00
|
|
|
params.message_text.ToString().c_str());
|
2017-05-17 11:29:28 +02:00
|
|
|
g_signal_connect(gtk_dialog_, "delete-event",
|
2021-06-17 22:08:01 +02:00
|
|
|
G_CALLBACK(gtk_widget_hide_on_delete), nullptr);
|
2015-01-23 21:03:47 +01:00
|
|
|
|
|
|
|
gtk_window_set_title(GTK_WINDOW(gtk_dialog_), title.c_str());
|
|
|
|
|
2021-03-01 20:41:27 +01:00
|
|
|
GtkWidget* ok_button =
|
|
|
|
gtk_dialog_add_button(GTK_DIALOG(gtk_dialog_), "_OK", GTK_RESPONSE_OK);
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (params.dialog_type != JSDIALOGTYPE_PROMPT) {
|
2015-01-23 21:03:47 +01:00
|
|
|
gtk_widget_grab_focus(ok_button);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
if (params.dialog_type == JSDIALOGTYPE_PROMPT) {
|
2015-01-23 21:03:47 +01:00
|
|
|
GtkWidget* content_area =
|
|
|
|
gtk_dialog_get_content_area(GTK_DIALOG(gtk_dialog_));
|
|
|
|
GtkWidget* text_box = gtk_entry_new();
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(text_box),
|
2018-09-20 15:00:14 +02:00
|
|
|
params.default_prompt_text.ToString().c_str());
|
2015-01-23 21:03:47 +01:00
|
|
|
gtk_box_pack_start(GTK_BOX(content_area), text_box, TRUE, TRUE, 0);
|
|
|
|
g_object_set_data(G_OBJECT(gtk_dialog_), kPromptTextId, text_box);
|
|
|
|
gtk_entry_set_activates_default(GTK_ENTRY(text_box), TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_dialog_set_default_response(GTK_DIALOG(gtk_dialog_), GTK_RESPONSE_OK);
|
|
|
|
g_signal_connect(gtk_dialog_, "response", G_CALLBACK(OnDialogResponse), this);
|
|
|
|
gtk_widget_show_all(GTK_WIDGET(gtk_dialog_));
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
void ClientDialogHandlerGtk::GetWindowAndContinue(
|
2015-01-23 21:03:47 +01:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceCallback<void(GtkWindow*)> callback) {
|
2018-09-20 15:00:14 +02:00
|
|
|
if (!CURRENTLY_ON_MAIN_THREAD()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
MAIN_POST_CLOSURE(
|
|
|
|
base::BindOnce(&ClientDialogHandlerGtk::GetWindowAndContinue, this,
|
|
|
|
browser, std::move(callback)));
|
2015-01-23 21:03:47 +01:00
|
|
|
return;
|
2018-09-20 15:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWindow* window = GetWindow(browser);
|
|
|
|
if (window) {
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(callback).Run(window);
|
2018-09-20 15:00:14 +02:00
|
|
|
}
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ClientDialogHandlerGtk::OnDialogResponse(GtkDialog* dialog,
|
|
|
|
gint response_id,
|
|
|
|
ClientDialogHandlerGtk* handler) {
|
2021-02-12 20:46:38 +01:00
|
|
|
REQUIRE_MAIN_THREAD();
|
2015-01-23 21:03:47 +01:00
|
|
|
|
|
|
|
DCHECK_EQ(dialog, GTK_DIALOG(handler->gtk_dialog_));
|
|
|
|
switch (response_id) {
|
|
|
|
case GTK_RESPONSE_OK:
|
|
|
|
handler->js_dialog_callback_->Continue(true, GetPromptText(dialog));
|
|
|
|
break;
|
|
|
|
case GTK_RESPONSE_CANCEL:
|
|
|
|
case GTK_RESPONSE_DELETE_EVENT:
|
|
|
|
handler->js_dialog_callback_->Continue(false, CefString());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::BindOnce(&ClientDialogHandlerGtk::OnResetDialogState,
|
|
|
|
handler, nullptr));
|
2015-01-23 21:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace client
|