2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-07-05 01:59:53 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/message_box.h"
|
2013-07-05 01:59:53 +00:00
|
|
|
|
2016-08-26 22:43:40 +00:00
|
|
|
#include <windows.h> // windows.h must be included first
|
|
|
|
|
2015-07-08 06:12:11 +00:00
|
|
|
#include <commctrl.h>
|
|
|
|
|
2015-07-08 08:23:43 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
#include "base/containers/contains.h"
|
|
|
|
#include "base/no_destructor.h"
|
2015-07-08 07:55:53 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2013-12-17 06:01:40 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2021-07-14 22:59:27 +00:00
|
|
|
#include "base/synchronization/lock.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "base/task/post_task.h"
|
2015-07-08 07:04:34 +00:00
|
|
|
#include "base/win/scoped_gdi_object.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
#include "shell/browser/native_window_views.h"
|
2020-05-12 17:37:51 +00:00
|
|
|
#include "shell/browser/ui/win/dialog_thread.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/unresponsive_suppressor.h"
|
2015-07-08 07:04:34 +00:00
|
|
|
#include "ui/gfx/icon_util.h"
|
2018-04-10 06:23:16 +00:00
|
|
|
#include "ui/gfx/image/image_skia.h"
|
2014-11-05 08:05:11 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-07-05 01:59:53 +00:00
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
MessageBoxSettings::MessageBoxSettings() = default;
|
|
|
|
MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
|
|
|
|
MessageBoxSettings::~MessageBoxSettings() = default;
|
|
|
|
|
2013-07-24 10:34:50 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
struct DialogResult {
|
|
|
|
int button_id;
|
|
|
|
bool verification_flag_checked;
|
|
|
|
};
|
|
|
|
|
|
|
|
// <ID, messageBox> map.
|
|
|
|
//
|
|
|
|
// Note that the HWND is stored in a unique_ptr, because the pointer of HWND
|
|
|
|
// will be passed between threads and we need to ensure the memory of HWND is
|
|
|
|
// not changed while dialogs map is modified.
|
|
|
|
std::map<int, std::unique_ptr<HWND>>& GetDialogsMap() {
|
|
|
|
static base::NoDestructor<std::map<int, std::unique_ptr<HWND>>> dialogs;
|
|
|
|
return *dialogs;
|
|
|
|
}
|
|
|
|
|
2022-02-21 09:27:45 +00:00
|
|
|
// Special HWND used by the dialogs map.
|
2021-07-14 22:59:27 +00:00
|
|
|
//
|
|
|
|
// - ID is used but window has not been created yet.
|
|
|
|
const HWND kHwndReserve = reinterpret_cast<HWND>(-1);
|
|
|
|
// - Notification to cancel message box.
|
|
|
|
const HWND kHwndCancel = reinterpret_cast<HWND>(-2);
|
|
|
|
|
|
|
|
// Lock used for modifying HWND between threads.
|
|
|
|
//
|
|
|
|
// Note that there might be multiple dialogs being opened at the same time, but
|
|
|
|
// we only use one lock for them all, because each dialog is independent from
|
|
|
|
// each other and there is no need to use different lock for each one.
|
|
|
|
// Also note that the |GetDialogsMap| is only used in the main thread, what is
|
|
|
|
// shared between threads is the memory of HWND, so there is no need to use lock
|
|
|
|
// when accessing dialogs map.
|
|
|
|
base::Lock& GetHWNDLock() {
|
|
|
|
static base::NoDestructor<base::Lock> lock;
|
|
|
|
return *lock;
|
|
|
|
}
|
|
|
|
|
2015-07-08 06:35:28 +00:00
|
|
|
// Small command ID values are already taken by Windows, we have to start from
|
|
|
|
// a large number to avoid conflicts with Windows.
|
|
|
|
const int kIDStart = 100;
|
|
|
|
|
2015-07-08 07:55:53 +00:00
|
|
|
// Get the common ID from button's name.
|
|
|
|
struct CommonButtonID {
|
|
|
|
int button;
|
|
|
|
int id;
|
|
|
|
};
|
2021-03-18 19:55:51 +00:00
|
|
|
CommonButtonID GetCommonID(const std::wstring& button) {
|
|
|
|
std::wstring lower = base::ToLowerASCII(button);
|
2015-07-08 07:55:53 +00:00
|
|
|
if (lower == L"ok")
|
2018-04-18 01:55:30 +00:00
|
|
|
return {TDCBF_OK_BUTTON, IDOK};
|
2015-07-08 07:55:53 +00:00
|
|
|
else if (lower == L"yes")
|
2018-04-18 01:55:30 +00:00
|
|
|
return {TDCBF_YES_BUTTON, IDYES};
|
2015-07-08 07:55:53 +00:00
|
|
|
else if (lower == L"no")
|
2018-04-18 01:55:30 +00:00
|
|
|
return {TDCBF_NO_BUTTON, IDNO};
|
2015-07-08 07:55:53 +00:00
|
|
|
else if (lower == L"cancel")
|
2018-04-18 01:55:30 +00:00
|
|
|
return {TDCBF_CANCEL_BUTTON, IDCANCEL};
|
2015-07-08 07:55:53 +00:00
|
|
|
else if (lower == L"retry")
|
2018-04-18 01:55:30 +00:00
|
|
|
return {TDCBF_RETRY_BUTTON, IDRETRY};
|
2015-07-08 07:55:53 +00:00
|
|
|
else if (lower == L"close")
|
2018-04-18 01:55:30 +00:00
|
|
|
return {TDCBF_CLOSE_BUTTON, IDCLOSE};
|
|
|
|
return {-1, -1};
|
2015-07-08 07:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether the buttons are common buttons, if so map common ID
|
|
|
|
// to button ID.
|
2021-03-31 01:06:00 +00:00
|
|
|
void MapToCommonID(const std::vector<std::wstring>& buttons,
|
2015-07-08 07:55:53 +00:00
|
|
|
std::map<int, int>* id_map,
|
|
|
|
TASKDIALOG_COMMON_BUTTON_FLAGS* button_flags,
|
|
|
|
std::vector<TASKDIALOG_BUTTON>* dialog_buttons) {
|
|
|
|
for (size_t i = 0; i < buttons.size(); ++i) {
|
2021-03-31 01:06:00 +00:00
|
|
|
auto common = GetCommonID(buttons[i]);
|
2015-07-08 07:55:53 +00:00
|
|
|
if (common.button != -1) {
|
|
|
|
// It is a common button.
|
|
|
|
(*id_map)[common.id] = i;
|
|
|
|
(*button_flags) |= common.button;
|
|
|
|
} else {
|
|
|
|
// It is a custom button.
|
2021-03-31 01:06:00 +00:00
|
|
|
dialog_buttons->push_back(
|
|
|
|
{static_cast<int>(i + kIDStart), buttons[i].c_str()});
|
2015-07-08 07:55:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
// Callback of the task dialog. The TaskDialogIndirect API does not provide the
|
|
|
|
// HWND of the dialog, and we have to listen to the TDN_CREATED message to get
|
|
|
|
// it.
|
|
|
|
// Note that this callback runs in dialog thread instead of main thread, so it
|
|
|
|
// is possible for CloseMessageBox to be called before or all after the dialog
|
|
|
|
// window is created.
|
|
|
|
HRESULT CALLBACK
|
|
|
|
TaskDialogCallback(HWND hwnd, UINT msg, WPARAM, LPARAM, LONG_PTR data) {
|
|
|
|
if (msg == TDN_CREATED) {
|
|
|
|
HWND* target = reinterpret_cast<HWND*>(data);
|
|
|
|
// Lock since CloseMessageBox might be called.
|
|
|
|
base::AutoLock lock(GetHWNDLock());
|
|
|
|
if (*target == kHwndCancel) {
|
|
|
|
// The dialog is cancelled before it is created, close it directly.
|
|
|
|
::PostMessage(hwnd, WM_CLOSE, 0, 0);
|
|
|
|
} else if (*target == kHwndReserve) {
|
|
|
|
// Otherwise save the hwnd.
|
|
|
|
*target = hwnd;
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-03-31 01:06:00 +00:00
|
|
|
DialogResult ShowTaskDialogWstr(NativeWindow* parent,
|
|
|
|
MessageBoxType type,
|
|
|
|
const std::vector<std::wstring>& buttons,
|
|
|
|
int default_id,
|
|
|
|
int cancel_id,
|
|
|
|
bool no_link,
|
2021-07-12 04:33:26 +00:00
|
|
|
const std::u16string& title,
|
|
|
|
const std::u16string& message,
|
|
|
|
const std::u16string& detail,
|
|
|
|
const std::u16string& checkbox_label,
|
2021-03-31 01:06:00 +00:00
|
|
|
bool checkbox_checked,
|
2021-07-14 22:59:27 +00:00
|
|
|
const gfx::ImageSkia& icon,
|
|
|
|
HWND* hwnd) {
|
2015-08-22 01:20:52 +00:00
|
|
|
TASKDIALOG_FLAGS flags =
|
2018-04-18 01:55:30 +00:00
|
|
|
TDF_SIZE_TO_CONTENT | // Show all content.
|
2015-08-22 01:26:09 +00:00
|
|
|
TDF_ALLOW_DIALOG_CANCELLATION; // Allow canceling the dialog.
|
2015-07-08 06:12:11 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
TASKDIALOGCONFIG config = {0};
|
|
|
|
config.cbSize = sizeof(config);
|
|
|
|
config.hInstance = GetModuleHandle(NULL);
|
|
|
|
config.dwFlags = flags;
|
2016-01-11 13:12:07 +00:00
|
|
|
|
2017-02-06 15:35:36 +00:00
|
|
|
if (parent) {
|
2019-06-19 21:23:04 +00:00
|
|
|
config.hwndParent = static_cast<electron::NativeWindowViews*>(parent)
|
|
|
|
->GetAcceleratedWidget();
|
2017-02-06 15:35:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 13:12:07 +00:00
|
|
|
if (default_id > 0)
|
|
|
|
config.nDefaultButton = kIDStart + default_id;
|
2015-07-08 08:23:43 +00:00
|
|
|
|
2015-08-04 13:56:17 +00:00
|
|
|
// TaskDialogIndirect doesn't allow empty name, if we set empty title it
|
|
|
|
// will show "electron.exe" in title.
|
2022-01-13 21:23:36 +00:00
|
|
|
std::wstring app_name;
|
2021-03-31 01:06:00 +00:00
|
|
|
if (title.empty()) {
|
2022-01-13 21:23:36 +00:00
|
|
|
app_name = base::UTF8ToWide(Browser::Get()->GetName());
|
2015-08-04 13:56:17 +00:00
|
|
|
config.pszWindowTitle = app_name.c_str();
|
2021-03-31 01:06:00 +00:00
|
|
|
} else {
|
2021-07-12 04:33:26 +00:00
|
|
|
config.pszWindowTitle = base::as_wcstr(title);
|
2021-03-31 01:06:00 +00:00
|
|
|
}
|
2015-07-08 06:41:19 +00:00
|
|
|
|
2015-07-08 07:04:34 +00:00
|
|
|
base::win::ScopedHICON hicon;
|
|
|
|
if (!icon.isNull()) {
|
2016-03-09 19:27:16 +00:00
|
|
|
hicon = IconUtil::CreateHICONFromSkBitmap(*icon.bitmap());
|
2015-07-08 07:04:34 +00:00
|
|
|
config.dwFlags |= TDF_USE_HICON_MAIN;
|
2016-03-09 19:27:16 +00:00
|
|
|
config.hMainIcon = hicon.get();
|
2015-07-08 07:04:34 +00:00
|
|
|
} else {
|
|
|
|
// Show icon according to dialog's type.
|
|
|
|
switch (type) {
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kInformation:
|
|
|
|
case MessageBoxType::kQuestion:
|
2015-07-08 07:04:34 +00:00
|
|
|
config.pszMainIcon = TD_INFORMATION_ICON;
|
|
|
|
break;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kWarning:
|
2015-07-08 07:04:34 +00:00
|
|
|
config.pszMainIcon = TD_WARNING_ICON;
|
|
|
|
break;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kError:
|
2015-07-08 07:04:34 +00:00
|
|
|
config.pszMainIcon = TD_ERROR_ICON;
|
|
|
|
break;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kNone:
|
2018-06-25 20:30:00 +00:00
|
|
|
break;
|
2015-07-08 07:04:34 +00:00
|
|
|
}
|
2015-07-08 06:46:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 17:25:21 +00:00
|
|
|
// If "detail" is empty then don't make message highlighted.
|
2015-07-08 06:41:19 +00:00
|
|
|
if (detail.empty()) {
|
2021-07-12 04:33:26 +00:00
|
|
|
config.pszContent = base::as_wcstr(message);
|
2015-07-08 06:41:19 +00:00
|
|
|
} else {
|
2021-07-12 04:33:26 +00:00
|
|
|
config.pszMainInstruction = base::as_wcstr(message);
|
|
|
|
config.pszContent = base::as_wcstr(detail);
|
2015-07-08 06:41:19 +00:00
|
|
|
}
|
2015-07-08 06:12:11 +00:00
|
|
|
|
2017-02-06 15:35:36 +00:00
|
|
|
if (!checkbox_label.empty()) {
|
2021-07-12 04:33:26 +00:00
|
|
|
config.pszVerificationText = base::as_wcstr(checkbox_label);
|
2019-11-27 07:39:07 +00:00
|
|
|
if (checkbox_checked)
|
2017-02-06 15:35:36 +00:00
|
|
|
config.dwFlags |= TDF_VERIFICATION_FLAG_CHECKED;
|
|
|
|
}
|
|
|
|
|
2015-07-08 07:55:53 +00:00
|
|
|
// Iterate through the buttons, put common buttons in dwCommonButtons
|
|
|
|
// and custom buttons in pButtons.
|
|
|
|
std::map<int, int> id_map;
|
|
|
|
std::vector<TASKDIALOG_BUTTON> dialog_buttons;
|
2019-12-05 21:38:57 +00:00
|
|
|
if (no_link) {
|
2015-07-23 09:28:04 +00:00
|
|
|
for (size_t i = 0; i < buttons.size(); ++i)
|
2021-03-31 01:06:00 +00:00
|
|
|
dialog_buttons.push_back(
|
|
|
|
{static_cast<int>(i + kIDStart), buttons[i].c_str()});
|
2015-07-23 09:28:04 +00:00
|
|
|
} else {
|
|
|
|
MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
|
|
|
|
}
|
2020-10-22 20:24:59 +00:00
|
|
|
if (!dialog_buttons.empty()) {
|
2015-07-08 08:23:43 +00:00
|
|
|
config.pButtons = &dialog_buttons.front();
|
|
|
|
config.cButtons = dialog_buttons.size();
|
2019-12-05 21:38:57 +00:00
|
|
|
if (!no_link)
|
2015-07-23 09:20:43 +00:00
|
|
|
config.dwFlags |= TDF_USE_COMMAND_LINKS; // custom buttons as links.
|
2015-07-08 08:23:43 +00:00
|
|
|
}
|
2015-07-08 07:55:53 +00:00
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
// Pass a callback to receive the HWND of the message box.
|
|
|
|
if (hwnd) {
|
|
|
|
config.pfCallback = &TaskDialogCallback;
|
|
|
|
config.lpCallbackData = reinterpret_cast<LONG_PTR>(hwnd);
|
|
|
|
}
|
2019-11-27 07:39:07 +00:00
|
|
|
|
2015-07-08 06:12:11 +00:00
|
|
|
int id = 0;
|
2021-07-14 22:59:27 +00:00
|
|
|
BOOL verification_flag_checked = FALSE;
|
|
|
|
TaskDialogIndirect(&config, &id, nullptr, &verification_flag_checked);
|
2017-02-06 15:35:36 +00:00
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
int button_id;
|
2015-07-08 07:55:53 +00:00
|
|
|
if (id_map.find(id) != id_map.end()) // common button.
|
2019-11-27 07:39:07 +00:00
|
|
|
button_id = id_map[id];
|
2015-07-08 07:55:53 +00:00
|
|
|
else if (id >= kIDStart) // custom button.
|
2019-11-27 07:39:07 +00:00
|
|
|
button_id = id - kIDStart;
|
2015-07-08 07:55:53 +00:00
|
|
|
else
|
2019-11-27 07:39:07 +00:00
|
|
|
button_id = cancel_id;
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
return {button_id, static_cast<bool>(verification_flag_checked)};
|
2015-07-08 06:12:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
DialogResult ShowTaskDialogUTF8(const MessageBoxSettings& settings,
|
|
|
|
HWND* hwnd) {
|
2021-03-31 01:06:00 +00:00
|
|
|
std::vector<std::wstring> buttons;
|
2019-06-14 15:26:25 +00:00
|
|
|
for (const auto& button : settings.buttons)
|
2021-03-31 01:06:00 +00:00
|
|
|
buttons.push_back(base::UTF8ToWide(button));
|
|
|
|
|
2021-07-12 04:33:26 +00:00
|
|
|
const std::u16string title = base::UTF8ToUTF16(settings.title);
|
|
|
|
const std::u16string message = base::UTF8ToUTF16(settings.message);
|
|
|
|
const std::u16string detail = base::UTF8ToUTF16(settings.detail);
|
|
|
|
const std::u16string checkbox_label =
|
|
|
|
base::UTF8ToUTF16(settings.checkbox_label);
|
2021-03-31 01:06:00 +00:00
|
|
|
|
|
|
|
return ShowTaskDialogWstr(
|
|
|
|
settings.parent_window, settings.type, buttons, settings.default_id,
|
|
|
|
settings.cancel_id, settings.no_link, title, message, detail,
|
2021-07-14 22:59:27 +00:00
|
|
|
checkbox_label, settings.checkbox_checked, settings.icon, hwnd);
|
2017-02-06 15:35:36 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 10:34:50 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::UnresponsiveSuppressor suppressor;
|
2021-07-14 22:59:27 +00:00
|
|
|
DialogResult result = ShowTaskDialogUTF8(settings, nullptr);
|
|
|
|
return result.button_id;
|
2013-07-05 01:59:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
void ShowMessageBox(const MessageBoxSettings& settings,
|
2019-03-12 18:06:59 +00:00
|
|
|
MessageBoxCallback callback) {
|
2021-07-14 22:59:27 +00:00
|
|
|
// The dialog is created in a new thread so we don't know its HWND yet, put
|
|
|
|
// kHwndReserve in the dialogs map for now.
|
|
|
|
HWND* hwnd = nullptr;
|
|
|
|
if (settings.id) {
|
|
|
|
if (base::Contains(GetDialogsMap(), *settings.id))
|
|
|
|
CloseMessageBox(*settings.id);
|
|
|
|
auto it = GetDialogsMap().emplace(*settings.id,
|
|
|
|
std::make_unique<HWND>(kHwndReserve));
|
|
|
|
hwnd = it.first->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_thread::Run(
|
|
|
|
base::BindOnce(&ShowTaskDialogUTF8, settings, base::Unretained(hwnd)),
|
|
|
|
base::BindOnce(
|
|
|
|
[](MessageBoxCallback callback, absl::optional<int> id,
|
|
|
|
DialogResult result) {
|
|
|
|
if (id)
|
|
|
|
GetDialogsMap().erase(*id);
|
|
|
|
std::move(callback).Run(result.button_id,
|
|
|
|
result.verification_flag_checked);
|
|
|
|
},
|
|
|
|
std::move(callback), settings.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseMessageBox(int id) {
|
|
|
|
auto it = GetDialogsMap().find(id);
|
|
|
|
if (it == GetDialogsMap().end()) {
|
|
|
|
LOG(ERROR) << "CloseMessageBox called with nonexistent ID";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HWND* hwnd = it->second.get();
|
|
|
|
// Lock since the TaskDialogCallback might be saving the dialog's HWND.
|
|
|
|
base::AutoLock lock(GetHWNDLock());
|
|
|
|
DCHECK(*hwnd != kHwndCancel);
|
|
|
|
if (*hwnd == kHwndReserve) {
|
|
|
|
// If the dialog window has not been created yet, tell it to cancel.
|
|
|
|
*hwnd = kHwndCancel;
|
|
|
|
} else {
|
|
|
|
// Otherwise send a message to close it.
|
|
|
|
::PostMessage(*hwnd, WM_CLOSE, 0, 0);
|
|
|
|
}
|
2013-09-22 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::UnresponsiveSuppressor suppressor;
|
2021-03-31 01:06:00 +00:00
|
|
|
ShowTaskDialogWstr(nullptr, MessageBoxType::kError, {}, -1, 0, false,
|
2021-07-14 22:59:27 +00:00
|
|
|
u"Error", title, content, u"", false, gfx::ImageSkia(),
|
|
|
|
nullptr);
|
2014-11-05 08:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|