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.
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
#include "libcef/browser/javascript_dialog_manager.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/logging.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-10-09 17:23:12 +02:00
|
|
|
#include "components/url_formatter/elide_url.h"
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefJSDialogCallbackImpl : public CefJSDialogCallback {
|
|
|
|
public:
|
2017-12-21 22:47:20 +01:00
|
|
|
using Callback = content::JavaScriptDialogManager::DialogClosedCallback;
|
|
|
|
|
|
|
|
CefJSDialogCallbackImpl(Callback callback) : 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 {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT, base::Bind(&CefJSDialogCallbackImpl::CancelNow,
|
2017-10-20 19:45:20 +02:00
|
|
|
base::Passed(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 {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT, base::Bind(&CefJSDialogCallbackImpl::Continue,
|
|
|
|
this, success, user_input));
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 22:47:20 +01:00
|
|
|
Callback Disconnect() WARN_UNUSED_RESULT { return std::move(callback_); }
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
private:
|
2017-12-21 22:47:20 +01:00
|
|
|
static void CancelNow(Callback callback) {
|
2012-04-16 23:15:27 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2017-10-20 19:45:20 +02:00
|
|
|
std::move(callback).Run(false, base::string16());
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2017-12-21 22:47:20 +01:00
|
|
|
Callback callback_;
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefJSDialogCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
CefJavaScriptDialogManager::CefJavaScriptDialogManager(
|
2015-11-17 19:20:13 +01:00
|
|
|
CefBrowserHostImpl* browser,
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<CefJavaScriptDialogRunner> runner)
|
2015-11-17 19:20:13 +01:00
|
|
|
: browser_(browser),
|
2016-01-06 20:20:54 +01:00
|
|
|
runner_(std::move(runner)),
|
2015-11-17 19:20:13 +01:00
|
|
|
dialog_running_(false),
|
2017-05-17 11:29:28 +02:00
|
|
|
weak_ptr_factory_(this) {}
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefJavaScriptDialogManager::~CefJavaScriptDialogManager() {}
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefJavaScriptDialogManager::Destroy() {
|
|
|
|
if (runner_.get()) {
|
|
|
|
runner_.reset(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2013-12-17 23:04:35 +01:00
|
|
|
const base::string16& message_text,
|
|
|
|
const base::string16& 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) {
|
2018-03-20 21:15:08 +01:00
|
|
|
const GURL& origin_url = render_frame_host->GetLastCommittedURL();
|
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefJSDialogHandler> handler = client->GetJSDialogHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
*did_suppress_message = false;
|
|
|
|
|
|
|
|
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(
|
|
|
|
browser_, origin_url.spec(),
|
|
|
|
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();
|
|
|
|
if (callback.is_null() || *did_suppress_message)
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*did_suppress_message = false;
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
if (!runner_.get() || dialog_running_) {
|
|
|
|
// Suppress the dialog if there is no platform runner or if the dialog is
|
|
|
|
// currently running.
|
|
|
|
if (!runner_.get())
|
|
|
|
LOG(WARNING) << "No javascript dialog runner available for this platform";
|
2012-04-16 23:15:27 +02:00
|
|
|
*did_suppress_message = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
dialog_running_ = true;
|
|
|
|
|
2016-04-27 22:38:52 +02:00
|
|
|
const base::string16& display_url =
|
|
|
|
url_formatter::FormatUrlForSecurityDisplay(origin_url);
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2017-12-21 22:47:20 +01:00
|
|
|
DCHECK(!callback.is_null());
|
2015-11-17 19:20:13 +01:00
|
|
|
runner_->Run(browser_, message_type, display_url, message_text,
|
|
|
|
default_prompt_text,
|
|
|
|
base::Bind(&CefJavaScriptDialogManager::DialogClosed,
|
2017-10-20 19:45:20 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(),
|
|
|
|
base::Passed(std::move(callback))));
|
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) {
|
2013-03-19 23:59:33 +01:00
|
|
|
if (browser_->destruction_state() >=
|
|
|
|
CefBrowserHostImpl::DESTRUCTION_STATE_ACCEPTED) {
|
|
|
|
// Currently destroying the browser. Accept the unload without showing
|
|
|
|
// the prompt.
|
2017-10-20 19:45:20 +02:00
|
|
|
std::move(callback).Run(true, base::string16());
|
2013-03-19 23:59:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
const base::string16& message_text =
|
|
|
|
base::ASCIIToUTF16("Is it OK to leave/reload this page?");
|
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefJSDialogHandler> handler = client->GetJSDialogHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
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(
|
|
|
|
browser_, message_text, is_reload, callbackPtr.get());
|
2012-04-16 23:15:27 +02:00
|
|
|
if (handled)
|
|
|
|
return;
|
|
|
|
|
2017-12-21 22:47:20 +01:00
|
|
|
// |callback| may be null if the user executed it despite returning false.
|
|
|
|
callback = callbackPtr->Disconnect();
|
|
|
|
if (callback.is_null())
|
|
|
|
return;
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
if (!runner_.get() || dialog_running_) {
|
|
|
|
if (!runner_.get())
|
|
|
|
LOG(WARNING) << "No javascript dialog runner available for this platform";
|
|
|
|
// Suppress the dialog if there is no platform runner or if the dialog is
|
|
|
|
// currently running.
|
2017-10-20 19:45:20 +02:00
|
|
|
std::move(callback).Run(true, base::string16());
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
dialog_running_ = true;
|
|
|
|
|
2017-12-21 22:47:20 +01:00
|
|
|
DCHECK(!callback.is_null());
|
2017-05-17 11:29:28 +02:00
|
|
|
runner_->Run(browser_, content::JAVASCRIPT_DIALOG_TYPE_CONFIRM,
|
2015-11-17 19:20:13 +01:00
|
|
|
base::string16(), // display_url
|
2016-03-16 03:55:59 +01:00
|
|
|
message_text,
|
2015-11-17 19:20:13 +01:00
|
|
|
base::string16(), // default_prompt_text
|
|
|
|
base::Bind(&CefJavaScriptDialogManager::DialogClosed,
|
2017-10-20 19:45:20 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(),
|
|
|
|
base::Passed(std::move(callback))));
|
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) {
|
2012-04-16 23:15:27 +02:00
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefJSDialogHandler> handler = client->GetJSDialogHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
// Execute the user callback.
|
|
|
|
handler->OnResetDialogState(browser_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
if (runner_.get() && dialog_running_) {
|
|
|
|
runner_->Cancel();
|
|
|
|
dialog_running_ = false;
|
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,
|
|
|
|
const base::string16& user_input) {
|
2013-05-31 15:45:21 +02:00
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefJSDialogHandler> handler = client->GetJSDialogHandler();
|
|
|
|
if (handler.get())
|
|
|
|
handler->OnDialogClosed(browser_);
|
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
DCHECK(runner_.get());
|
|
|
|
DCHECK(dialog_running_);
|
|
|
|
|
|
|
|
dialog_running_ = false;
|
|
|
|
|
2017-10-20 19:45:20 +02:00
|
|
|
std::move(callback).Run(success, user_input);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|