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"
|
2012-04-16 23:15:27 +02:00
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
|
|
|
#include "libcef/browser/javascript_dialog.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(
|
2012-04-16 23:15:27 +02:00
|
|
|
CefBrowserHostImpl* browser)
|
|
|
|
: browser_(browser) {
|
|
|
|
}
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
CefJavaScriptDialogManager::~CefJavaScriptDialogManager() {
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
|
2012-04-16 23:15:27 +02:00
|
|
|
*did_suppress_message = false;
|
|
|
|
|
|
|
|
if (dialog_.get()) {
|
|
|
|
// One dialog at a time, please.
|
|
|
|
*did_suppress_message = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
dialog_.reset(new CefJavaScriptDialog(this,
|
2012-05-31 17:19:33 +02:00
|
|
|
message_type,
|
2012-04-16 23:15:27 +02:00
|
|
|
display_url,
|
|
|
|
message_text,
|
|
|
|
default_prompt_text,
|
|
|
|
callback));
|
|
|
|
#else
|
|
|
|
// TODO(port): implement CefJavaScriptDialog for other platforms.
|
|
|
|
*did_suppress_message = true;
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
|
2012-04-16 23:15:27 +02:00
|
|
|
if (dialog_.get()) {
|
|
|
|
// Seriously!?
|
2013-12-17 23:04:35 +01:00
|
|
|
callback.Run(true, base::string16());
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
dialog_.reset(
|
|
|
|
new CefJavaScriptDialog(this,
|
|
|
|
content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
|
2013-12-17 23:04:35 +01:00
|
|
|
base::string16(), // display_url
|
2012-08-04 02:59:58 +02:00
|
|
|
new_message_text,
|
2013-12-17 23:04:35 +01:00
|
|
|
base::string16(), // default_prompt_text
|
2012-08-04 02:59:58 +02:00
|
|
|
callback));
|
2012-04-16 23:15:27 +02:00
|
|
|
#else
|
|
|
|
// TODO(port): implement CefJavaScriptDialog for other platforms.
|
2013-12-17 23:04:35 +01:00
|
|
|
callback.Run(true, base::string16());
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
|
2012-04-16 23:15:27 +02:00
|
|
|
if (dialog_.get()) {
|
|
|
|
dialog_->Cancel();
|
|
|
|
dialog_.reset();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-23 12:03:42 +02:00
|
|
|
void CefJavaScriptDialogManager::ResetDialogState(
|
2013-08-15 21:38:55 +02:00
|
|
|
content::WebContents* web_contents) {
|
|
|
|
}
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
void CefJavaScriptDialogManager::DialogClosed(CefJavaScriptDialog* dialog) {
|
2012-08-04 02:59:58 +02:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
|
2012-04-16 23:15:27 +02:00
|
|
|
DCHECK_EQ(dialog, dialog_.get());
|
|
|
|
dialog_.reset();
|
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_);
|
|
|
|
}
|
2012-04-16 23:15:27 +02:00
|
|
|
#endif
|
|
|
|
}
|