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"
|
|
|
|
#include "base/utf_string_conversions.h"
|
|
|
|
#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) {
|
|
|
|
}
|
|
|
|
~CefJSDialogCallbackImpl() {
|
|
|
|
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_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Continue(bool success,
|
|
|
|
const CefString& user_input) OVERRIDE {
|
|
|
|
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();
|
|
|
|
callback.Run(false, string16());
|
|
|
|
}
|
|
|
|
|
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,
|
2012-04-16 23:15:27 +02:00
|
|
|
const string16& message_text,
|
|
|
|
const string16& default_prompt_text,
|
|
|
|
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);
|
|
|
|
if (handled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
string16 display_url = net::FormatUrl(origin_url, accept_lang);
|
|
|
|
|
|
|
|
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,
|
|
|
|
const string16& message_text,
|
|
|
|
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.
|
|
|
|
callback.Run(true, string16());
|
|
|
|
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!?
|
|
|
|
callback.Run(true, string16());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string16 new_message_text =
|
|
|
|
message_text +
|
|
|
|
ASCIIToUTF16("\n\nIs it OK to leave/reload this page?");
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
dialog_.reset(
|
|
|
|
new CefJavaScriptDialog(this,
|
|
|
|
content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
|
|
|
|
string16(), // display_url
|
|
|
|
new_message_text,
|
|
|
|
string16(), // default_prompt_text
|
|
|
|
callback));
|
2012-04-16 23:15:27 +02:00
|
|
|
#else
|
|
|
|
// TODO(port): implement CefJavaScriptDialog for other platforms.
|
|
|
|
callback.Run(true, string16());
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
void CefJavaScriptDialogManager::ResetJavaScriptState(
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|