electron/shell/browser/ui/message_box_gtk.cc
electron-roller[bot] dd03cceda0
chore: bump chromium to 137.0.7128.1 (main) (#46482)
* chore: bump chromium in DEPS to 137.0.7107.0

* chore: bump chromium in DEPS to 137.0.7109.0

* chore: bump chromium in DEPS to 137.0.7111.0

* chore: bump chromium in DEPS to 137.0.7113.0

* 6384240: Remove double-declaration for accessibility on macOS | 6384240

* 6422872: Remove unused includes in isolation_info_mojom_traits.h | 6422872

* chore: update patches

* 6400733: Avoid ipc_message_macros.h usage in some foo_param_traits_macros.h files | 6400733

* chore: update patches

* 6423410: Enable unsafe buffer warnings for chromium, try #3. | 6423410

* chore: iwyu

* refactor: prefer value initialization over memset()

From the looks up upstream commits in base/, it looks like memset()
could trigger `-Wunsafe-buffer-usage` warnings soon?

Value initialization is more C++ish and less error-prone anyway,
due to memset()'s easily swappable parameters.

* refactor: NotifyIcon::InitIconData() returns a NOTIFYICONDATA

This follows F.20 in the C++ Core Guidelines and also removes the need
for memset()

* 6423410: Enable unsafe buffer warnings for chromium, try #3. | 6423410

remove all uses of:

- strcmp()

* fixup!  6423410: Enable unsafe buffer warnings for chromium, try #3. | 6423410

* 6433203: Add a PassKey to RegisterDeleteDelegateCallback(). | 6433203

* chore: bump chromium in DEPS to 137.0.7115.0

* 6387077: [PermissionOptions] Generalize PermissionRequestDescription | 6387077

* chore: update patches

* 6387077: [PermissionOptions] Generalize PermissionRequestDescription | 6387077

* fix: add pragma for MacSDK unsafe buffers | 6423410: Enable unsafe buffer warnings for chromium, try #3. | 6423410

* chore: bump chromium in DEPS to 137.0.7117.0

* chore: update patches

* chore: update filesnames.libcxx.gni

* 6431756: Replace SetOwnedByWidget() bool arg with a PassKey. | 6431756

* 6387077: [PermissionOptions] Generalize PermissionRequestDescription | 6387077

* 6428345: Remove ExtensionService usage from ChromeExtensionRegistrarDelegate | 6428345

* 6384315: Migrate extensions_enabled from ExtensionService to Registrar | 6384315

* 6428749: [extensions] Refactor ExtensionService for AddNewAndUpdateExtension. | 6428749

* chore: bump chromium in DEPS to 137.0.7119.0

* 6440290: corner-shape: support inset shadow | 6440290

* 6429230: FSA: Move blocked paths to the PermissionContext class | 6429230

* chore: update patches

* chore: bump chromium in DEPS to 137.0.7121.0

* chore: update patches

* fix: partially revert 6443473: Remove ItemDelete from the Mac version of AppleKeychain | 6443473

* fix: update filenames.libcxx.gni

* chore: bump chromium in DEPS to 137.0.7123.0

* chore: update patches

* chore: "grandfather in" electron views too

Lock further access to View::set_owned_by_client() | 6448510

* chore: update feat_corner_smoothing_css_rule_and_blink_painting.patch

corner-shape: support inset shadow | 6440290

* refactor: grandfather in AutofillPopupView as a subclass of WidgetDelegateView

Add a PassKey for std::make_unique<WidgetDelegateView>() | 6442265

* Provide dbus appmenu information on Wayland | 6405535

* [extensions] Move OnExtensionInstalled out of ExtensionService. | 6443325

* refactor: grandfather in NativeWindowViews for delete callbacks

6433203: Add a PassKey to RegisterDeleteDelegateCallback(). | 6433203

* chore: merge the four "grandfather" patches into one

* [A11yPerformance] Remove IsAccessibilityAllowed() | 6404386: [A11yPerformance] Remove IsAccessibilityAllowed() | 6404386

NB: the changes here are copied from the upstream changes in
chrome/browser/ui/webui/accessibility/accessibility_ui.cc

* 6420753: [PermissionOptions] Use PermissionDescriptorPtr in PermissionController | 6420753

* 6429573: [accessibility] Move mode change out of AccessibilityNotificationWaiter | 6429573

* chore: e patches all

* 6419936: [win] Change ScreenWin public static methods to virtual | 6419936

* 6423410: Enable unsafe buffer warnings for chromium, try #3. | 6423410

remove all uses of:

- fprintf()
- fputs()
- snprintf()
- vsnprintf()

* fix: size conversion FTBFS on Win

* 6423410: Enable unsafe buffer warnings for chromium, try #3. | 6423410

remove all uses of:

- wcscpy_s()

* 6423410: Enable unsafe buffer warnings for chromium, try #3. | 6423410

remove all uses of:

- wcsncpy_s()

* chore: update mas_avoid_private_macos_api_usage.patch.patch

6394283: Remove double-declaration for accessibility on iOS | 6394283

Lots of context shear in this commit but the only interesting part is:

-+  return nullptr;
++  return {};

Which is needed because the return type is sometimes not a pointer.

* chore: e patches all

* chore: disable -Wmacro-redefined warning in electron_main_win.cc

* chore: bump chromium in DEPS to 137.0.7123.5

* refactor: patch electron PermissionTypes into blink

6387077: [PermissionOptions] Generalize PermissionRequestDescription | 6387077

* chore: e patches all

* chore: remove the box_painter_base.cc part of feat_corner_smoothing_css_rule_and_blink_painting.patch

as per code review @ https://github.com/electron/electron/pull/46482#pullrequestreview-2777338370

* test: enable window-smaller-than-64x64 test on Linux

* chore: bump chromium in DEPS to 137.0.7124.1

* chore: bump chromium in DEPS to 137.0.7125.1

* chore: bump chromium in DEPS to 137.0.7127.3

* 6459201: [Extensions] Remove ExtensionSystem::FinishDelayedInstallationIfReady() | 6459201

* 6454796: [Extensions] Move (most) registrar delayed install logic to //extensions | 6454796

* chore: bump chromium in DEPS to 137.0.7128.1

* chore: e patches all

* chore: node ./script/gen-libc++-filenames.js

* [views] Gate DesktopWindowTreeHostWin::window_enlargement_ behind flag

Refs 6428649

* feat: allow opt-out animated_content_sampler.

Refs 6438681

* Trigger CI

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: Keeley Hammond <vertedinde@electronjs.org>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2025-04-22 15:53:29 -04:00

268 lines
8.8 KiB
C++

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <iostream>
#include <string_view>
#include "shell/browser/ui/message_box.h"
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "electron/electron_gtk_stubs.h"
#include "shell/browser/browser.h"
#include "shell/browser/native_window_observer.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/ui/gtk_util.h"
#include "ui/base/glib/scoped_gsignal.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gtk/gtk_ui.h" // nogncheck
#include "ui/gtk/gtk_util.h" // nogncheck
#if defined(USE_OZONE)
#include "ui/base/ui_base_features.h"
#endif
namespace electron {
MessageBoxSettings::MessageBoxSettings() = default;
MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
MessageBoxSettings::~MessageBoxSettings() = default;
namespace {
// <ID, messageBox> map
base::flat_map<int, GtkWidget*>& GetDialogsMap() {
static base::NoDestructor<base::flat_map<int, GtkWidget*>> dialogs;
return *dialogs;
}
class GtkMessageBox : private NativeWindowObserver {
public:
explicit GtkMessageBox(const MessageBoxSettings& settings)
: id_(settings.id),
cancel_id_(settings.cancel_id),
parent_(static_cast<NativeWindow*>(settings.parent_window)) {
// Create dialog.
dialog_ =
gtk_message_dialog_new(nullptr, // parent
static_cast<GtkDialogFlags>(0), // no flags
GetMessageType(settings.type), // type
GTK_BUTTONS_NONE, // no buttons
"%s", settings.message.c_str());
if (id_)
GetDialogsMap()[*id_] = dialog_;
if (!settings.detail.empty())
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_),
"%s", settings.detail.c_str());
if (!settings.title.empty())
gtk_window_set_title(GTK_WINDOW(dialog_), settings.title.c_str());
if (!settings.icon.isNull()) {
// No easy way to obtain this programmatically, but GTK+'s docs
// define GTK_ICON_SIZE_DIALOG to be 48 pixels
static constexpr int pixel_width = 48;
static constexpr int pixel_height = 48;
GdkPixbuf* pixbuf =
gtk_util::GdkPixbufFromSkBitmap(*settings.icon.bitmap());
GdkPixbuf* scaled_pixbuf = gdk_pixbuf_scale_simple(
pixbuf, pixel_width, pixel_height, GDK_INTERP_BILINEAR);
GtkWidget* w = gtk_image_new_from_pixbuf(scaled_pixbuf);
gtk_message_dialog_set_image(GTK_MESSAGE_DIALOG(dialog_), w);
gtk_widget_show(w);
g_clear_pointer(&scaled_pixbuf, g_object_unref);
g_clear_pointer(&pixbuf, g_object_unref);
}
if (!settings.checkbox_label.empty()) {
GtkWidget* message_area =
gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog_));
GtkWidget* check_button =
gtk_check_button_new_with_label(settings.checkbox_label.c_str());
signals_.emplace_back(
check_button, "toggled",
base::BindRepeating(&GtkMessageBox::OnCheckboxToggled,
base::Unretained(this)));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button),
settings.checkbox_checked);
gtk_container_add(GTK_CONTAINER(message_area), check_button);
gtk_widget_show(check_button);
}
// Add buttons.
GtkDialog* dialog = GTK_DIALOG(dialog_);
if (settings.buttons.size() == 0) {
gtk_dialog_add_button(dialog, TranslateToStock("OK"), 0);
} else {
for (size_t i = 0; i < settings.buttons.size(); ++i) {
gtk_dialog_add_button(dialog, TranslateToStock(settings.buttons[i]), i);
}
}
gtk_dialog_set_default_response(dialog, settings.default_id);
// Parent window.
if (parent_) {
parent_->AddObserver(this);
static_cast<NativeWindowViews*>(parent_)->SetEnabled(false);
gtk::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
}
}
~GtkMessageBox() override {
gtk_widget_destroy(dialog_);
if (parent_) {
parent_->RemoveObserver(this);
static_cast<NativeWindowViews*>(parent_)->SetEnabled(true);
}
}
// disable copy
GtkMessageBox(const GtkMessageBox&) = delete;
GtkMessageBox& operator=(const GtkMessageBox&) = delete;
GtkMessageType GetMessageType(MessageBoxType type) {
switch (type) {
case MessageBoxType::kInformation:
return GTK_MESSAGE_INFO;
case MessageBoxType::kWarning:
return GTK_MESSAGE_WARNING;
case MessageBoxType::kQuestion:
return GTK_MESSAGE_QUESTION;
case MessageBoxType::kError:
return GTK_MESSAGE_ERROR;
default:
return GTK_MESSAGE_OTHER;
}
}
static const char* TranslateToStock(const std::string& text) {
const std::string lower = base::ToLowerASCII(text);
if (lower == "cancel") {
return gtk_util::GetCancelLabel();
}
if (lower == "no") {
return gtk_util::GetNoLabel();
}
if (lower == "ok") {
return gtk_util::GetOkLabel();
}
if (lower == "yes") {
return gtk_util::GetYesLabel();
}
return text.c_str();
}
void Show() {
gtk_widget_show(dialog_);
gtk::GtkUi::GetPlatform()->ShowGtkWindow(GTK_WINDOW(dialog_));
}
int RunSynchronous() {
Show();
int response = gtk_dialog_run(GTK_DIALOG(dialog_));
return (response < 0) ? cancel_id_ : response;
}
void RunAsynchronous(MessageBoxCallback callback) {
callback_ = std::move(callback);
signals_.emplace_back(dialog_, "delete-event",
base::BindRepeating(gtk_widget_hide_on_delete));
signals_.emplace_back(dialog_, "response",
base::BindRepeating(&GtkMessageBox::OnResponseDialog,
base::Unretained(this)));
Show();
}
// NativeWindowObserver
void OnWindowClosed() override {
parent_->RemoveObserver(this);
parent_ = nullptr;
}
void OnResponseDialog(GtkWidget* widget, int response);
void OnCheckboxToggled(GtkWidget* widget);
private:
// The id of the dialog.
std::optional<int> id_;
// The id to return when the dialog is closed without pressing buttons.
int cancel_id_ = 0;
bool checkbox_checked_ = false;
raw_ptr<NativeWindow> parent_;
RAW_PTR_EXCLUSION GtkWidget* dialog_;
MessageBoxCallback callback_;
std::vector<ScopedGSignal> signals_;
};
void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
if (id_)
GetDialogsMap().erase(*id_);
gtk_widget_hide(dialog_);
if (response < 0)
std::move(callback_).Run(cancel_id_, checkbox_checked_);
else
std::move(callback_).Run(response, checkbox_checked_);
delete this;
}
void GtkMessageBox::OnCheckboxToggled(GtkWidget* widget) {
checkbox_checked_ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
}
} // namespace
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
return GtkMessageBox(settings).RunSynchronous();
}
void ShowMessageBox(const MessageBoxSettings& settings,
MessageBoxCallback callback) {
if (settings.id && GetDialogsMap().contains(*settings.id))
CloseMessageBox(*settings.id);
(new GtkMessageBox(settings))->RunAsynchronous(std::move(callback));
}
void CloseMessageBox(int id) {
auto it = GetDialogsMap().find(id);
if (it == GetDialogsMap().end()) {
LOG(ERROR) << "CloseMessageBox called with nonexistent ID";
return;
}
gtk_window_close(GTK_WINDOW(it->second));
}
void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
if (Browser::Get()->is_ready()) {
electron::MessageBoxSettings settings;
settings.type = electron::MessageBoxType::kError;
settings.buttons = {};
settings.title = "Error";
settings.message = base::UTF16ToUTF8(title);
settings.detail = base::UTF16ToUTF8(content);
GtkMessageBox(settings).RunSynchronous();
} else {
static constexpr std::string_view ANSI_FG_RED = "\x1b[31m";
static constexpr std::string_view ANSI_FG_BLACK = "\x1b[30m";
static constexpr std::string_view ANSI_TEXT_BOLD = "\x1b[1m";
static constexpr std::string_view ANSI_BG_GRAY = "\x1b[47m";
static constexpr std::string_view ANSI_RESET = "\x1b[0m";
std::cerr << ANSI_TEXT_BOLD << ANSI_BG_GRAY << ANSI_FG_RED
<< base::UTF16ToUTF8(title) << '\n'
<< ANSI_FG_BLACK << base::UTF16ToUTF8(content) << '\n'
<< ANSI_RESET;
}
}
} // namespace electron