2012-04-16 23:15:27 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/javascript_dialog_manager.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
|
|
|
#include "base/functional/callback_helpers.h"
|
2012-04-16 23:15:27 +02:00
|
|
|
#include "base/logging.h"
|
2024-07-02 19:50:48 +02:00
|
|
|
#include "cef/libcef/browser/browser_guest_util.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/browser_host_base.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
2022-06-02 11:49:50 +02:00
|
|
|
#include "components/javascript_dialogs/tab_modal_dialog_manager.h"
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefJSDialogCallbackImpl : public CefJSDialogCallback {
|
|
|
|
public:
|
2020-03-04 01:29:39 +01:00
|
|
|
using CallbackType = content::JavaScriptDialogManager::DialogClosedCallback;
|
2017-12-21 22:47:20 +01:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
explicit CefJSDialogCallbackImpl(CallbackType callback)
|
2020-03-04 01:29:39 +01:00
|
|
|
: callback_(std::move(callback)) {}
|
2014-11-12 20:25:15 +01:00
|
|
|
~CefJSDialogCallbackImpl() override {
|
2012-04-16 23:15:27 +02:00
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// The callback is still pending. Cancel it now.
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
2017-10-20 19:45:20 +02:00
|
|
|
CancelNow(std::move(callback_));
|
2012-04-16 23:15:27 +02:00
|
|
|
} else {
|
2020-03-04 01:29:39 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::BindOnce(&CefJSDialogCallbackImpl::CancelNow,
|
|
|
|
std::move(callback_)));
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Continue(bool success, const CefString& user_input) override {
|
2012-04-16 23:15:27 +02:00
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
if (!callback_.is_null()) {
|
2017-10-20 19:45:20 +02:00
|
|
|
std::move(callback_).Run(success, user_input);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-04 01:29:39 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(&CefJSDialogCallbackImpl::Continue,
|
|
|
|
this, success, user_input));
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 23:23:40 +01:00
|
|
|
[[nodiscard]] CallbackType Disconnect() { return std::move(callback_); }
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
private:
|
2020-03-04 01:29:39 +01:00
|
|
|
static void CancelNow(CallbackType callback) {
|
2012-04-16 23:15:27 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2021-04-21 00:52:34 +02:00
|
|
|
std::move(callback).Run(false, std::u16string());
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
CallbackType callback_;
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefJSDialogCallbackImpl);
|
|
|
|
};
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
javascript_dialogs::TabModalDialogManager* GetTabModalDialogManager(
|
|
|
|
content::WebContents* web_contents) {
|
2024-02-15 21:35:11 +01:00
|
|
|
if (auto* manager =
|
|
|
|
javascript_dialogs::TabModalDialogManager::FromWebContents(
|
|
|
|
web_contents)) {
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
// Try the owner WebContents if the dialog originates from an excluded view
|
|
|
|
// such as the PDF viewer or Print Preview. This is safe to call even if Alloy
|
|
|
|
// extensions are disabled.
|
2024-07-02 19:50:48 +02:00
|
|
|
if (auto* owner_contents = GetOwnerForGuestContents(web_contents)) {
|
2024-04-17 18:01:26 +02:00
|
|
|
return javascript_dialogs::TabModalDialogManager::FromWebContents(
|
|
|
|
owner_contents);
|
2024-02-15 21:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2022-06-02 11:49:50 +02:00
|
|
|
}
|
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
} // namespace
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
CefJavaScriptDialogManager::CefJavaScriptDialogManager(
|
2022-06-02 11:49:50 +02:00
|
|
|
CefBrowserHostBase* browser)
|
|
|
|
: browser_(browser), weak_ptr_factory_(this) {}
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
CefJavaScriptDialogManager::~CefJavaScriptDialogManager() = default;
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefJavaScriptDialogManager::Destroy() {
|
2022-06-02 11:49:50 +02:00
|
|
|
if (handler_) {
|
|
|
|
CancelDialogs(nullptr, false);
|
|
|
|
}
|
|
|
|
if (runner_) {
|
|
|
|
runner_.reset();
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
void CefJavaScriptDialogManager::RunJavaScriptDialog(
|
2012-04-16 23:15:27 +02:00
|
|
|
content::WebContents* web_contents,
|
2018-03-20 21:15:08 +01:00
|
|
|
content::RenderFrameHost* render_frame_host,
|
2017-03-03 23:37:23 +01:00
|
|
|
content::JavaScriptDialogType message_type,
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& message_text,
|
|
|
|
const std::u16string& default_prompt_text,
|
2017-10-20 19:45:20 +02:00
|
|
|
DialogClosedCallback callback,
|
2012-04-16 23:15:27 +02:00
|
|
|
bool* did_suppress_message) {
|
2022-06-02 11:49:50 +02:00
|
|
|
*did_suppress_message = false;
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
const GURL& origin_url = render_frame_host->GetLastCommittedURL();
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
// Always call DialogClosed().
|
|
|
|
callback =
|
|
|
|
base::BindOnce(&CefJavaScriptDialogManager::DialogClosed,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
|
|
|
|
|
|
|
|
if (auto client = browser_->GetClient()) {
|
|
|
|
if (auto handler = client->GetJSDialogHandler()) {
|
|
|
|
// If the dialog is handled this will be cleared in DialogClosed().
|
|
|
|
handler_ = handler;
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefJSDialogCallbackImpl> callbackPtr(
|
2017-10-20 19:45:20 +02:00
|
|
|
new CefJSDialogCallbackImpl(std::move(callback)));
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
// Execute the user callback.
|
2017-05-17 11:29:28 +02:00
|
|
|
bool handled = handler->OnJSDialog(
|
2024-05-11 17:48:38 +02:00
|
|
|
browser_.get(), origin_url.spec(),
|
2017-05-17 11:29:28 +02:00
|
|
|
static_cast<cef_jsdialog_type_t>(message_type), message_text,
|
|
|
|
default_prompt_text, callbackPtr.get(), *did_suppress_message);
|
2014-03-12 16:43:05 +01:00
|
|
|
if (handled) {
|
|
|
|
// Invalid combination of values. Crash sooner rather than later.
|
|
|
|
CHECK(!*did_suppress_message);
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
2014-03-12 16:43:05 +01:00
|
|
|
}
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2017-12-21 22:47:20 +01:00
|
|
|
// |callback| may be null if the user executed it despite returning false.
|
|
|
|
callback = callbackPtr->Disconnect();
|
2022-06-02 11:49:50 +02:00
|
|
|
if (callback.is_null()) {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "OnJSDialog should return true when executing the callback";
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
2022-06-02 11:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*did_suppress_message) {
|
|
|
|
// Call OnResetDialogState but don't execute |callback|.
|
|
|
|
CancelDialogs(web_contents, /*reset_state=*/true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler_ = nullptr;
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
DCHECK(!handler_);
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
if (InitializeRunner()) {
|
|
|
|
runner_->Run(browser_, message_type, origin_url, message_text,
|
|
|
|
default_prompt_text, std::move(callback));
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
if (!CanUseChromeDialogs()) {
|
|
|
|
// Dismiss the dialog.
|
|
|
|
std::move(callback).Run(false, std::u16string());
|
|
|
|
return;
|
|
|
|
}
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
auto manager = GetTabModalDialogManager(web_contents);
|
2024-02-15 21:35:11 +01:00
|
|
|
if (!manager) {
|
|
|
|
// Dismiss the dialog.
|
|
|
|
std::move(callback).Run(false, std::u16string());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
manager->RunJavaScriptDialog(web_contents, render_frame_host, message_type,
|
|
|
|
message_text, default_prompt_text,
|
|
|
|
std::move(callback), did_suppress_message);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
void CefJavaScriptDialogManager::RunBeforeUnloadDialog(
|
2012-04-16 23:15:27 +02:00
|
|
|
content::WebContents* web_contents,
|
2017-12-07 22:44:24 +01:00
|
|
|
content::RenderFrameHost* render_frame_host,
|
2012-04-16 23:15:27 +02:00
|
|
|
bool is_reload,
|
2017-10-20 19:45:20 +02:00
|
|
|
DialogClosedCallback callback) {
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& message_text = u"Is it OK to leave/reload this page?";
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
// Always call DialogClosed().
|
|
|
|
callback =
|
|
|
|
base::BindOnce(&CefJavaScriptDialogManager::DialogClosed,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
|
|
|
|
|
|
|
|
if (auto client = browser_->GetClient()) {
|
|
|
|
if (auto handler = client->GetJSDialogHandler()) {
|
|
|
|
// If the dialog is handled this will be cleared in DialogClosed().
|
|
|
|
handler_ = handler;
|
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
CefRefPtr<CefJSDialogCallbackImpl> callbackPtr(
|
2017-10-20 19:45:20 +02:00
|
|
|
new CefJSDialogCallbackImpl(std::move(callback)));
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
// Execute the user callback.
|
2017-05-17 11:29:28 +02:00
|
|
|
bool handled = handler->OnBeforeUnloadDialog(
|
2024-05-11 17:48:38 +02:00
|
|
|
browser_.get(), message_text, is_reload, callbackPtr.get());
|
2022-06-02 11:49:50 +02:00
|
|
|
if (handled) {
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
2022-06-02 11:49:50 +02:00
|
|
|
}
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2017-12-21 22:47:20 +01:00
|
|
|
// |callback| may be null if the user executed it despite returning false.
|
|
|
|
callback = callbackPtr->Disconnect();
|
2022-06-02 11:49:50 +02:00
|
|
|
if (callback.is_null()) {
|
|
|
|
LOG(WARNING) << "OnBeforeUnloadDialog should return true when "
|
|
|
|
"executing the callback";
|
2017-12-21 22:47:20 +01:00
|
|
|
return;
|
2022-06-02 11:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handler_ = nullptr;
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
DCHECK(!handler_);
|
|
|
|
|
|
|
|
if (InitializeRunner()) {
|
|
|
|
runner_->Run(browser_, content::JAVASCRIPT_DIALOG_TYPE_CONFIRM,
|
|
|
|
/*origin_url=*/GURL(), message_text,
|
|
|
|
/*default_prompt_text=*/std::u16string(), std::move(callback));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CanUseChromeDialogs()) {
|
|
|
|
// Accept the unload without showing the prompt.
|
2021-04-21 00:52:34 +02:00
|
|
|
std::move(callback).Run(true, std::u16string());
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
auto manager = GetTabModalDialogManager(web_contents);
|
2024-02-15 21:35:11 +01:00
|
|
|
if (!manager) {
|
|
|
|
// Accept the unload without showing the prompt.
|
|
|
|
std::move(callback).Run(true, std::u16string());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
manager->RunBeforeUnloadDialog(web_contents, render_frame_host, is_reload,
|
|
|
|
std::move(callback));
|
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
bool CefJavaScriptDialogManager::HandleJavaScriptDialog(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
bool accept,
|
|
|
|
const std::u16string* prompt_override) {
|
|
|
|
if (handler_) {
|
|
|
|
DialogClosed(base::NullCallback(), accept,
|
|
|
|
prompt_override ? *prompt_override : std::u16string());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runner_) {
|
|
|
|
runner_->Handle(accept, prompt_override);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!CanUseChromeDialogs()) {
|
2022-06-02 11:49:50 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-06-02 11:49:50 +02:00
|
|
|
|
|
|
|
auto manager = GetTabModalDialogManager(web_contents);
|
2024-02-15 21:35:11 +01:00
|
|
|
if (!manager) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
return manager->HandleJavaScriptDialog(web_contents, accept, prompt_override);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
void CefJavaScriptDialogManager::CancelDialogs(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
bool reset_state) {
|
2022-06-02 11:49:50 +02:00
|
|
|
if (handler_) {
|
|
|
|
if (reset_state) {
|
2024-05-11 17:48:38 +02:00
|
|
|
handler_->OnResetDialogState(browser_.get());
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
2022-06-02 11:49:50 +02:00
|
|
|
handler_ = nullptr;
|
|
|
|
return;
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
if (runner_) {
|
2015-11-17 19:20:13 +01:00
|
|
|
runner_->Cancel();
|
2022-06-02 11:49:50 +02:00
|
|
|
return;
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
2022-06-02 11:49:50 +02:00
|
|
|
|
|
|
|
// Null when called from DialogClosed() or Destroy().
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!web_contents) {
|
2022-06-02 11:49:50 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-06-02 11:49:50 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!CanUseChromeDialogs()) {
|
2022-06-02 11:49:50 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-06-02 11:49:50 +02:00
|
|
|
|
|
|
|
auto manager = GetTabModalDialogManager(web_contents);
|
2024-02-15 21:35:11 +01:00
|
|
|
if (!manager) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
manager->CancelDialogs(web_contents, reset_state);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefJavaScriptDialogManager::DialogClosed(
|
2017-10-20 19:45:20 +02:00
|
|
|
DialogClosedCallback callback,
|
2015-11-17 19:20:13 +01:00
|
|
|
bool success,
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& user_input) {
|
2022-06-02 11:49:50 +02:00
|
|
|
if (handler_) {
|
2024-05-11 17:48:38 +02:00
|
|
|
handler_->OnDialogClosed(browser_.get());
|
2022-06-02 11:49:50 +02:00
|
|
|
// Call OnResetDialogState.
|
|
|
|
CancelDialogs(/*web_contents=*/nullptr, /*reset_state=*/true);
|
2013-05-31 15:45:21 +02:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
// Null when called from HandleJavaScriptDialog().
|
|
|
|
if (!callback.is_null()) {
|
|
|
|
std::move(callback).Run(success, user_input);
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
bool CefJavaScriptDialogManager::InitializeRunner() {
|
|
|
|
if (!runner_initialized_) {
|
|
|
|
runner_ = browser_->platform_delegate()->CreateJavaScriptDialogRunner();
|
|
|
|
runner_initialized_ = true;
|
|
|
|
}
|
|
|
|
return !!runner_.get();
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
2022-06-02 11:49:50 +02:00
|
|
|
|
|
|
|
bool CefJavaScriptDialogManager::CanUseChromeDialogs() const {
|
|
|
|
if (browser_->IsWindowless() &&
|
|
|
|
browser_->GetWindowHandle() == kNullWindowHandle) {
|
|
|
|
LOG(ERROR) << "Default dialog implementation requires a parent window "
|
|
|
|
"handle; canceling the JS dialog";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2024-01-20 23:48:57 +01:00
|
|
|
}
|