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
|
|
|
#include "net/base/net_util.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefJSDialogCallbackImpl : public CefJSDialogCallback {
|
|
|
|
public:
|
|
|
|
CefJSDialogCallbackImpl(
|
2013-02-23 01:43:28 +01:00
|
|
|
const content::JavaScriptDialogManager::DialogClosedCallback& callback)
|
2012-04-16 23:15:27 +02:00
|
|
|
: callback_(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()) {
|
|
|
|
CancelNow(callback_);
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefJSDialogCallbackImpl::CancelNow, callback_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01: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()) {
|
|
|
|
callback_.Run(success, user_input);
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefJSDialogCallbackImpl::Continue, this, success,
|
|
|
|
user_input));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect() {
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void CancelNow(
|
2013-02-23 01:43:28 +01:00
|
|
|
const content::JavaScriptDialogManager::DialogClosedCallback& callback) {
|
2012-04-16 23:15:27 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2013-12-17 23:04:35 +01:00
|
|
|
callback.Run(false, base::string16());
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
content::JavaScriptDialogManager::DialogClosedCallback 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,
|
|
|
|
scoped_ptr<CefJavaScriptDialogRunner> runner)
|
|
|
|
: 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),
|
|
|
|
weak_ptr_factory_(this) {
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 01:43:28 +01: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()) {
|
|
|
|
DCHECK(!dialog_running_);
|
|
|
|
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,
|
|
|
|
const GURL& origin_url,
|
|
|
|
const std::string& accept_lang,
|
2012-05-31 17:19:33 +02:00
|
|
|
content::JavaScriptMessageType message_type,
|
2013-12-17 23:04:35 +01:00
|
|
|
const base::string16& message_text,
|
|
|
|
const base::string16& default_prompt_text,
|
2012-04-16 23:15:27 +02:00
|
|
|
const DialogClosedCallback& callback,
|
|
|
|
bool* did_suppress_message) {
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefJSDialogHandler> handler = client->GetJSDialogHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
*did_suppress_message = false;
|
|
|
|
|
|
|
|
CefRefPtr<CefJSDialogCallbackImpl> callbackPtr(
|
|
|
|
new CefJSDialogCallbackImpl(callback));
|
|
|
|
|
|
|
|
// Execute the user callback.
|
|
|
|
bool handled = handler->OnJSDialog(browser_, origin_url.spec(),
|
|
|
|
accept_lang,
|
2012-05-31 17:19:33 +02:00
|
|
|
static_cast<cef_jsdialog_type_t>(message_type),
|
2012-04-16 23:15:27 +02:00
|
|
|
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
|
|
|
|
|
|
|
callbackPtr->Disconnect();
|
|
|
|
if (*did_suppress_message)
|
|
|
|
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;
|
|
|
|
|
2015-08-14 16:41:08 +02:00
|
|
|
base::string16 display_url =
|
2015-10-09 17:23:12 +02:00
|
|
|
url_formatter::FormatUrlForSecurityDisplay(origin_url, accept_lang);
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
runner_->Run(browser_, message_type, display_url, message_text,
|
|
|
|
default_prompt_text,
|
|
|
|
base::Bind(&CefJavaScriptDialogManager::DialogClosed,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), 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,
|
2013-12-17 23:04:35 +01:00
|
|
|
const base::string16& message_text,
|
2012-04-16 23:15:27 +02:00
|
|
|
bool is_reload,
|
|
|
|
const 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.
|
2013-12-17 23:04:35 +01:00
|
|
|
callback.Run(true, base::string16());
|
2013-03-19 23:59:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
new CefJSDialogCallbackImpl(callback));
|
|
|
|
|
|
|
|
// Execute the user callback.
|
|
|
|
bool handled = handler->OnBeforeUnloadDialog(browser_, message_text,
|
|
|
|
is_reload, callbackPtr.get());
|
|
|
|
if (handled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
callbackPtr->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2013-12-17 23:04:35 +01:00
|
|
|
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;
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
base::string16 new_message_text =
|
2012-04-16 23:15:27 +02:00
|
|
|
message_text +
|
2014-01-02 23:41:11 +01:00
|
|
|
base::ASCIIToUTF16("\n\nIs it OK to leave/reload this page?");
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
runner_->Run(browser_,
|
|
|
|
content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
|
|
|
|
base::string16(), // display_url
|
|
|
|
new_message_text,
|
|
|
|
base::string16(), // default_prompt_text
|
|
|
|
base::Bind(&CefJavaScriptDialogManager::DialogClosed,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), callback));
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2013-08-15 21:38:55 +02:00
|
|
|
void CefJavaScriptDialogManager::CancelActiveAndPendingDialogs(
|
2012-04-16 23:15:27 +02:00
|
|
|
content::WebContents* web_contents) {
|
|
|
|
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-04-23 12:03:42 +02:00
|
|
|
void CefJavaScriptDialogManager::ResetDialogState(
|
2013-08-15 21:38:55 +02:00
|
|
|
content::WebContents* web_contents) {
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefJavaScriptDialogManager::DialogClosed(
|
|
|
|
const DialogClosedCallback& callback,
|
|
|
|
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;
|
|
|
|
|
|
|
|
callback.Run(success, user_input);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|