mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Add support for JavaScript alert, confirm, prompt and onbeforeunload dialogs (issue #507).
- Add CefJSDialogHandler and CefJSDialogCallback interfaces. - Add default dialog implementations for Windows and Mac OS-X. - Add "JavaScript Dialogs" example to cefclient. - Change TestHandler::AddResource to ignore the query component when matching URLs. git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@594 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
@@ -852,10 +852,10 @@ void CefBrowserHostImpl::DidNavigateMainFramePostCommit(
|
||||
}
|
||||
|
||||
content::JavaScriptDialogCreator*
|
||||
CefBrowserHostImpl::GetJavaScriptDialogCreator() {
|
||||
// TODO(cef): Provide a custom JavaScriptDialogCreator implementation to
|
||||
// support JavaScript alerts, prompts, etc.
|
||||
return content::WebContentsDelegate::GetJavaScriptDialogCreator();
|
||||
CefBrowserHostImpl::GetJavaScriptDialogCreator() {
|
||||
if (!dialog_creator_.get())
|
||||
dialog_creator_.reset(new CefJavaScriptDialogCreator(this));
|
||||
return dialog_creator_.get();
|
||||
}
|
||||
|
||||
void CefBrowserHostImpl::RunFileChooser(
|
||||
|
@@ -16,6 +16,7 @@
|
||||
#include "include/cef_client.h"
|
||||
#include "include/cef_frame.h"
|
||||
#include "libcef/browser/frame_host_impl.h"
|
||||
#include "libcef/browser/javascript_dialog_creator.h"
|
||||
#include "libcef/browser/url_request_context_getter_proxy.h"
|
||||
#include "libcef/common/response_manager.h"
|
||||
|
||||
@@ -353,6 +354,9 @@ class CefBrowserHostImpl : public CefBrowserHost,
|
||||
// Used for proxying cookie requests.
|
||||
scoped_refptr<CefURLRequestContextGetterProxy> request_context_proxy_;
|
||||
|
||||
// Used for creating and managing JavaScript dialogs.
|
||||
scoped_ptr<CefJavaScriptDialogCreator> dialog_creator_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefBrowserHostImpl);
|
||||
DISALLOW_EVIL_CONSTRUCTORS(CefBrowserHostImpl);
|
||||
};
|
||||
|
66
libcef/browser/javascript_dialog.h
Normal file
66
libcef/browser/javascript_dialog.h
Normal file
@@ -0,0 +1,66 @@
|
||||
// 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.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_H_
|
||||
#define CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_H_
|
||||
#pragma once
|
||||
|
||||
#include "content/public/browser/javascript_dialogs.h"
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
#if __OBJC__
|
||||
@class CefJavaScriptDialogHelper;
|
||||
#else
|
||||
class CefJavaScriptDialogHelper;
|
||||
#endif // __OBJC__
|
||||
#endif // defined(OS_MACOSX)
|
||||
|
||||
class CefJavaScriptDialogCreator;
|
||||
|
||||
class CefJavaScriptDialog {
|
||||
public:
|
||||
CefJavaScriptDialog(
|
||||
CefJavaScriptDialogCreator* creator,
|
||||
ui::JavascriptMessageType javascript_message_type,
|
||||
const string16& display_url,
|
||||
const string16& message_text,
|
||||
const string16& default_prompt_text,
|
||||
const content::JavaScriptDialogCreator::DialogClosedCallback& callback);
|
||||
~CefJavaScriptDialog();
|
||||
|
||||
// Called to cancel a dialog mid-flight.
|
||||
void Cancel();
|
||||
|
||||
// Activate the dialog.
|
||||
void Activate();
|
||||
|
||||
private:
|
||||
CefJavaScriptDialogCreator* creator_;
|
||||
content::JavaScriptDialogCreator::DialogClosedCallback callback_;
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
CefJavaScriptDialogHelper* helper_; // owned
|
||||
#elif defined(OS_WIN)
|
||||
ui::JavascriptMessageType message_type_;
|
||||
HWND dialog_win_;
|
||||
HWND parent_win_;
|
||||
string16 message_text_;
|
||||
string16 default_prompt_text_;
|
||||
static INT_PTR CALLBACK DialogProc(HWND dialog, UINT message, WPARAM wparam,
|
||||
LPARAM lparam);
|
||||
|
||||
// Since the message loop we expect to run in isn't going to be nicely
|
||||
// calling IsDialogMessage(), we need to hook the wnd proc and call it
|
||||
// ourselves. See http://support.microsoft.com/kb/q187988/
|
||||
static bool InstallMessageHook();
|
||||
static bool UninstallMessageHook();
|
||||
static LRESULT CALLBACK GetMsgProc(int code, WPARAM wparam, LPARAM lparam);
|
||||
static HHOOK msg_hook_;
|
||||
#endif
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefJavaScriptDialog);
|
||||
};
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_H_
|
204
libcef/browser/javascript_dialog_creator.cc
Normal file
204
libcef/browser/javascript_dialog_creator.cc
Normal file
@@ -0,0 +1,204 @@
|
||||
// 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.
|
||||
|
||||
#include "libcef/browser/javascript_dialog_creator.h"
|
||||
#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(
|
||||
const content::JavaScriptDialogCreator::DialogClosedCallback& callback)
|
||||
: 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(
|
||||
const content::JavaScriptDialogCreator::DialogClosedCallback& callback) {
|
||||
CEF_REQUIRE_UIT();
|
||||
callback.Run(false, string16());
|
||||
}
|
||||
|
||||
content::JavaScriptDialogCreator::DialogClosedCallback callback_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefJSDialogCallbackImpl);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
CefJavaScriptDialogCreator::CefJavaScriptDialogCreator(
|
||||
CefBrowserHostImpl* browser)
|
||||
: browser_(browser) {
|
||||
}
|
||||
|
||||
CefJavaScriptDialogCreator::~CefJavaScriptDialogCreator() {
|
||||
}
|
||||
|
||||
void CefJavaScriptDialogCreator::RunJavaScriptDialog(
|
||||
content::WebContents* web_contents,
|
||||
const GURL& origin_url,
|
||||
const std::string& accept_lang,
|
||||
ui::JavascriptMessageType javascript_message_type,
|
||||
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,
|
||||
static_cast<cef_jsdialog_type_t>(javascript_message_type),
|
||||
message_text, default_prompt_text, callbackPtr.get(),
|
||||
*did_suppress_message);
|
||||
if (handled)
|
||||
return;
|
||||
|
||||
callbackPtr->Disconnect();
|
||||
if (*did_suppress_message)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(OS_MACOSX) || defined(OS_WIN)
|
||||
*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,
|
||||
javascript_message_type,
|
||||
display_url,
|
||||
message_text,
|
||||
default_prompt_text,
|
||||
callback));
|
||||
#else
|
||||
// TODO(port): implement CefJavaScriptDialog for other platforms.
|
||||
*did_suppress_message = true;
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CefJavaScriptDialogCreator::RunBeforeUnloadDialog(
|
||||
content::WebContents* web_contents,
|
||||
const string16& message_text,
|
||||
bool is_reload,
|
||||
const DialogClosedCallback& callback) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(OS_MACOSX) || defined(OS_WIN)
|
||||
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?");
|
||||
|
||||
dialog_.reset(new CefJavaScriptDialog(this,
|
||||
ui::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
|
||||
string16(), // display_url
|
||||
new_message_text,
|
||||
string16(), // default_prompt_text
|
||||
callback));
|
||||
#else
|
||||
// TODO(port): implement CefJavaScriptDialog for other platforms.
|
||||
callback.Run(true, string16());
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CefJavaScriptDialogCreator::ResetJavaScriptState(
|
||||
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_);
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(OS_MACOSX) || defined(OS_WIN)
|
||||
if (dialog_.get()) {
|
||||
dialog_->Cancel();
|
||||
dialog_.reset();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void CefJavaScriptDialogCreator::DialogClosed(CefJavaScriptDialog* dialog) {
|
||||
#if defined(OS_MACOSX) || defined(OS_WIN)
|
||||
DCHECK_EQ(dialog, dialog_.get());
|
||||
dialog_.reset();
|
||||
#endif
|
||||
}
|
61
libcef/browser/javascript_dialog_creator.h
Normal file
61
libcef/browser/javascript_dialog_creator.h
Normal file
@@ -0,0 +1,61 @@
|
||||
// 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.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_CREATOR_H_
|
||||
#define CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_CREATOR_H_
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "content/public/browser/javascript_dialogs.h"
|
||||
|
||||
class CefBrowserHostImpl;
|
||||
class CefJavaScriptDialog;
|
||||
|
||||
class CefJavaScriptDialogCreator : public content::JavaScriptDialogCreator {
|
||||
public:
|
||||
explicit CefJavaScriptDialogCreator(CefBrowserHostImpl* browser);
|
||||
virtual ~CefJavaScriptDialogCreator();
|
||||
|
||||
// JavaScriptDialogCreator methods.
|
||||
virtual void RunJavaScriptDialog(
|
||||
content::WebContents* web_contents,
|
||||
const GURL& origin_url,
|
||||
const std::string& accept_lang,
|
||||
ui::JavascriptMessageType javascript_message_type,
|
||||
const string16& message_text,
|
||||
const string16& default_prompt_text,
|
||||
const DialogClosedCallback& callback,
|
||||
bool* did_suppress_message) OVERRIDE;
|
||||
|
||||
virtual void RunBeforeUnloadDialog(
|
||||
content::WebContents* web_contents,
|
||||
const string16& message_text,
|
||||
bool is_reload,
|
||||
const DialogClosedCallback& callback) OVERRIDE;
|
||||
|
||||
virtual void ResetJavaScriptState(
|
||||
content::WebContents* web_contents) OVERRIDE;
|
||||
|
||||
// Called by the CefJavaScriptDialog when it closes.
|
||||
void DialogClosed(CefJavaScriptDialog* dialog);
|
||||
|
||||
CefBrowserHostImpl* browser() const { return browser_; }
|
||||
|
||||
private:
|
||||
// This pointer is guaranteed to outlive the CefJavaScriptDialogCreator.
|
||||
CefBrowserHostImpl* browser_;
|
||||
|
||||
#if defined(OS_MACOSX) || defined(OS_WIN)
|
||||
// The dialog being shown. No queueing.
|
||||
scoped_ptr<CefJavaScriptDialog> dialog_;
|
||||
#endif
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefJavaScriptDialogCreator);
|
||||
};
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_CREATOR_H_
|
156
libcef/browser/javascript_dialog_mac.mm
Normal file
156
libcef/browser/javascript_dialog_mac.mm
Normal file
@@ -0,0 +1,156 @@
|
||||
// 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.
|
||||
|
||||
#include "libcef/browser/javascript_dialog.h"
|
||||
#include "libcef/browser/javascript_dialog_creator.h"
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
#import "base/mac/cocoa_protocols.h"
|
||||
#import "base/memory/scoped_nsobject.h"
|
||||
#include "base/sys_string_conversions.h"
|
||||
#include "base/utf_string_conversions.h"
|
||||
|
||||
// Helper object that receives the notification that the dialog/sheet is
|
||||
// going away. Is responsible for cleaning itself up.
|
||||
@interface CefJavaScriptDialogHelper : NSObject<NSAlertDelegate> {
|
||||
@private
|
||||
scoped_nsobject<NSAlert> alert_;
|
||||
NSTextField* textField_; // WEAK; owned by alert_
|
||||
|
||||
// Copies of the fields in CefJavaScriptDialog because they're private.
|
||||
CefJavaScriptDialogCreator* creator_;
|
||||
content::JavaScriptDialogCreator::DialogClosedCallback callback_;
|
||||
}
|
||||
|
||||
- (id)initHelperWithCreator:(CefJavaScriptDialogCreator*)creator
|
||||
andCallback:(content::JavaScriptDialogCreator::DialogClosedCallback)callback;
|
||||
- (NSAlert*)alert;
|
||||
- (NSTextField*)textField;
|
||||
- (void)alertDidEnd:(NSAlert*)alert
|
||||
returnCode:(int)returnCode
|
||||
contextInfo:(void*)contextInfo;
|
||||
- (void)cancel;
|
||||
|
||||
@end
|
||||
|
||||
@implementation CefJavaScriptDialogHelper
|
||||
|
||||
- (id)initHelperWithCreator:(CefJavaScriptDialogCreator*)creator
|
||||
andCallback:(content::JavaScriptDialogCreator::DialogClosedCallback)callback {
|
||||
if (self = [super init]) {
|
||||
creator_ = creator;
|
||||
callback_ = callback;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSAlert*)alert {
|
||||
alert_.reset([[NSAlert alloc] init]);
|
||||
return alert_;
|
||||
}
|
||||
|
||||
- (NSTextField*)textField {
|
||||
textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)];
|
||||
[[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail];
|
||||
[alert_ setAccessoryView:textField_];
|
||||
[textField_ release];
|
||||
|
||||
return textField_;
|
||||
}
|
||||
|
||||
- (void)alertDidEnd:(NSAlert*)alert
|
||||
returnCode:(int)returnCode
|
||||
contextInfo:(void*)contextInfo {
|
||||
if (returnCode == NSRunStoppedResponse)
|
||||
return;
|
||||
|
||||
bool success = returnCode == NSAlertFirstButtonReturn;
|
||||
string16 input;
|
||||
if (textField_)
|
||||
input = base::SysNSStringToUTF16([textField_ stringValue]);
|
||||
|
||||
CefJavaScriptDialog* native_dialog =
|
||||
reinterpret_cast<CefJavaScriptDialog*>(contextInfo);
|
||||
callback_.Run(success, input);
|
||||
creator_->DialogClosed(native_dialog);
|
||||
}
|
||||
|
||||
- (void)cancel {
|
||||
[NSApp endSheet:[alert_ window]];
|
||||
alert_.reset();
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
CefJavaScriptDialog::CefJavaScriptDialog(
|
||||
CefJavaScriptDialogCreator* creator,
|
||||
ui::JavascriptMessageType javascript_message_type,
|
||||
const string16& display_url,
|
||||
const string16& message_text,
|
||||
const string16& default_prompt_text,
|
||||
const content::JavaScriptDialogCreator::DialogClosedCallback& callback)
|
||||
: creator_(creator),
|
||||
callback_(callback) {
|
||||
bool text_field =
|
||||
javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_PROMPT;
|
||||
bool one_button =
|
||||
javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_ALERT;
|
||||
|
||||
helper_ =
|
||||
[[CefJavaScriptDialogHelper alloc] initHelperWithCreator:creator
|
||||
andCallback:callback];
|
||||
|
||||
// Show the modal dialog.
|
||||
NSAlert* alert = [helper_ alert];
|
||||
NSTextField* field = nil;
|
||||
if (text_field) {
|
||||
field = [helper_ textField];
|
||||
[field setStringValue:base::SysUTF16ToNSString(default_prompt_text)];
|
||||
}
|
||||
[alert setDelegate:helper_];
|
||||
[alert setInformativeText:base::SysUTF16ToNSString(message_text)];
|
||||
|
||||
string16 label;
|
||||
switch (javascript_message_type) {
|
||||
case ui::JAVASCRIPT_MESSAGE_TYPE_ALERT:
|
||||
label = ASCIIToUTF16("JavaScript Alert");
|
||||
break;
|
||||
case ui::JAVASCRIPT_MESSAGE_TYPE_PROMPT:
|
||||
label = ASCIIToUTF16("JavaScript Prompt");
|
||||
break;
|
||||
case ui::JAVASCRIPT_MESSAGE_TYPE_CONFIRM:
|
||||
label = ASCIIToUTF16("JavaScript Confirm");
|
||||
break;
|
||||
}
|
||||
if (!display_url.empty())
|
||||
label += ASCIIToUTF16(" - ") + display_url;
|
||||
|
||||
[alert setMessageText:base::SysUTF16ToNSString(label)];
|
||||
|
||||
[alert addButtonWithTitle:@"OK"];
|
||||
if (!one_button) {
|
||||
NSButton* other = [alert addButtonWithTitle:@"Cancel"];
|
||||
[other setKeyEquivalent:@"\e"];
|
||||
}
|
||||
|
||||
[alert
|
||||
beginSheetModalForWindow:nil // nil here makes it app-modal
|
||||
modalDelegate:helper_
|
||||
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
|
||||
contextInfo:this];
|
||||
|
||||
if ([alert accessoryView])
|
||||
[[alert window] makeFirstResponder:[alert accessoryView]];
|
||||
}
|
||||
|
||||
CefJavaScriptDialog::~CefJavaScriptDialog() {
|
||||
[helper_ release];
|
||||
}
|
||||
|
||||
void CefJavaScriptDialog::Cancel() {
|
||||
[helper_ cancel];
|
||||
}
|
213
libcef/browser/javascript_dialog_win.cc
Normal file
213
libcef/browser/javascript_dialog_win.cc
Normal file
@@ -0,0 +1,213 @@
|
||||
// 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.
|
||||
|
||||
#include "libcef/browser/javascript_dialog.h"
|
||||
#include "libcef/browser/javascript_dialog_creator.h"
|
||||
#include "libcef/browser/browser_host_impl.h"
|
||||
#include "libcef_dll/resource.h"
|
||||
|
||||
#include "base/file_path.h"
|
||||
#include "base/path_service.h"
|
||||
#include "base/string_util.h"
|
||||
#include "base/utf_string_conversions.h"
|
||||
|
||||
class CefJavaScriptDialog;
|
||||
|
||||
HHOOK CefJavaScriptDialog::msg_hook_ = NULL;
|
||||
|
||||
INT_PTR CALLBACK CefJavaScriptDialog::DialogProc(HWND dialog, UINT message,
|
||||
WPARAM wparam,
|
||||
LPARAM lparam) {
|
||||
switch (message) {
|
||||
case WM_INITDIALOG: {
|
||||
SetWindowLongPtr(dialog, DWL_USER, static_cast<LONG_PTR>(lparam));
|
||||
CefJavaScriptDialog* owner =
|
||||
reinterpret_cast<CefJavaScriptDialog*>(lparam);
|
||||
owner->dialog_win_ = dialog;
|
||||
SetDlgItemText(dialog, IDC_DIALOGTEXT, owner->message_text_.c_str());
|
||||
if (owner->message_type_ == ui::JAVASCRIPT_MESSAGE_TYPE_PROMPT)
|
||||
SetDlgItemText(dialog, IDC_PROMPTEDIT,
|
||||
owner->default_prompt_text_.c_str());
|
||||
break;
|
||||
}
|
||||
case WM_CLOSE: {
|
||||
CefJavaScriptDialog* owner = reinterpret_cast<CefJavaScriptDialog*>(
|
||||
GetWindowLongPtr(dialog, DWL_USER));
|
||||
if (owner) {
|
||||
owner->Cancel();
|
||||
owner->callback_.Run(false, string16());
|
||||
owner->creator_->DialogClosed(owner);
|
||||
|
||||
// No need for the system to call DestroyWindow() because it will be
|
||||
// called by the Cancel() method.
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WM_COMMAND: {
|
||||
CefJavaScriptDialog* owner = reinterpret_cast<CefJavaScriptDialog*>(
|
||||
GetWindowLongPtr(dialog, DWL_USER));
|
||||
string16 user_input;
|
||||
bool finish = false;
|
||||
bool result;
|
||||
switch (LOWORD(wparam)) {
|
||||
case IDOK:
|
||||
finish = true;
|
||||
result = true;
|
||||
if (owner->message_type_ == ui::JAVASCRIPT_MESSAGE_TYPE_PROMPT) {
|
||||
size_t length =
|
||||
GetWindowTextLength(GetDlgItem(dialog, IDC_PROMPTEDIT)) + 1;
|
||||
GetDlgItemText(dialog, IDC_PROMPTEDIT,
|
||||
WriteInto(&user_input, length), length);
|
||||
}
|
||||
break;
|
||||
case IDCANCEL:
|
||||
finish = true;
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
if (finish) {
|
||||
owner->Cancel();
|
||||
owner->callback_.Run(result, user_input);
|
||||
owner->creator_->DialogClosed(owner);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return DefWindowProc(dialog, message, wparam, lparam);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
CefJavaScriptDialog::CefJavaScriptDialog(
|
||||
CefJavaScriptDialogCreator* creator,
|
||||
ui::JavascriptMessageType javascript_message_type,
|
||||
const string16& display_url,
|
||||
const string16& message_text,
|
||||
const string16& default_prompt_text,
|
||||
const content::JavaScriptDialogCreator::DialogClosedCallback& callback)
|
||||
: creator_(creator),
|
||||
callback_(callback),
|
||||
message_text_(message_text),
|
||||
default_prompt_text_(default_prompt_text),
|
||||
message_type_(javascript_message_type) {
|
||||
InstallMessageHook();
|
||||
|
||||
int dialog_type;
|
||||
if (javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_ALERT)
|
||||
dialog_type = IDD_ALERT;
|
||||
else if (javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_CONFIRM)
|
||||
dialog_type = IDD_CONFIRM;
|
||||
else // JAVASCRIPT_MESSAGE_TYPE_PROMPT
|
||||
dialog_type = IDD_PROMPT;
|
||||
|
||||
FilePath file_path;
|
||||
HMODULE hModule = NULL;
|
||||
|
||||
// Try to load the dialog from the DLL.
|
||||
if (PathService::Get(base::FILE_MODULE, &file_path))
|
||||
hModule = ::GetModuleHandle(file_path.value().c_str());
|
||||
if (!hModule)
|
||||
hModule = ::GetModuleHandle(NULL);
|
||||
DCHECK(hModule);
|
||||
|
||||
parent_win_ = GetAncestor(creator->browser()->GetWindowHandle(), GA_ROOT);
|
||||
dialog_win_ = CreateDialogParam(hModule,
|
||||
MAKEINTRESOURCE(dialog_type),
|
||||
parent_win_,
|
||||
DialogProc,
|
||||
reinterpret_cast<LPARAM>(this));
|
||||
DCHECK(dialog_win_);
|
||||
|
||||
if (!display_url.empty()) {
|
||||
// Add the display URL to the window title.
|
||||
TCHAR text[64];
|
||||
GetWindowText(dialog_win_, text, sizeof(text)/sizeof(TCHAR));
|
||||
|
||||
string16 new_window_text = text + ASCIIToUTF16(" - ") + display_url;
|
||||
SetWindowText(dialog_win_, new_window_text.c_str());
|
||||
}
|
||||
|
||||
// Disable the parent window so the user can't interact with it.
|
||||
if (IsWindowEnabled(parent_win_))
|
||||
EnableWindow(parent_win_, FALSE);
|
||||
|
||||
ShowWindow(dialog_win_, SW_SHOWNORMAL);
|
||||
}
|
||||
|
||||
CefJavaScriptDialog::~CefJavaScriptDialog() {
|
||||
Cancel();
|
||||
UninstallMessageHook();
|
||||
}
|
||||
|
||||
void CefJavaScriptDialog::Cancel() {
|
||||
HWND parent = NULL;
|
||||
|
||||
// Re-enable the parent before closing the popup to avoid focus/activation/
|
||||
// z-order issues.
|
||||
if (parent_win_ && IsWindow(parent_win_) && !IsWindowEnabled(parent_win_)) {
|
||||
parent = parent_win_;
|
||||
EnableWindow(parent_win_, TRUE);
|
||||
parent_win_ = NULL;
|
||||
}
|
||||
|
||||
if (dialog_win_ && IsWindow(dialog_win_)) {
|
||||
SetWindowLongPtr(dialog_win_, DWL_USER, NULL);
|
||||
DestroyWindow(dialog_win_);
|
||||
dialog_win_ = NULL;
|
||||
}
|
||||
|
||||
// Return focus to the parent window.
|
||||
if (parent)
|
||||
SetFocus(parent);
|
||||
}
|
||||
|
||||
// static
|
||||
LRESULT CALLBACK CefJavaScriptDialog::GetMsgProc(int code, WPARAM wparam,
|
||||
LPARAM lparam) {
|
||||
// Mostly borrowed from http://support.microsoft.com/kb/q187988/
|
||||
// and http://www.codeproject.com/KB/atl/cdialogmessagehook.aspx.
|
||||
LPMSG msg = reinterpret_cast<LPMSG>(lparam);
|
||||
if (code >= 0 && wparam == PM_REMOVE &&
|
||||
msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST) {
|
||||
HWND hwnd = GetActiveWindow();
|
||||
if (::IsWindow(hwnd) && ::IsDialogMessage(hwnd, msg)) {
|
||||
// The value returned from this hookproc is ignored, and it cannot
|
||||
// be used to tell Windows the message has been handled. To avoid
|
||||
// further processing, convert the message to WM_NULL before
|
||||
// returning.
|
||||
msg->hwnd = NULL;
|
||||
msg->message = WM_NULL;
|
||||
msg->lParam = 0L;
|
||||
msg->wParam = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Passes the hook information to the next hook procedure in
|
||||
// the current hook chain.
|
||||
return ::CallNextHookEx(msg_hook_, code, wparam, lparam);
|
||||
}
|
||||
|
||||
// static
|
||||
bool CefJavaScriptDialog::InstallMessageHook() {
|
||||
// Make sure we only call this once.
|
||||
DCHECK(msg_hook_ == NULL);
|
||||
msg_hook_ = ::SetWindowsHookEx(WH_GETMESSAGE,
|
||||
&CefJavaScriptDialog::GetMsgProc,
|
||||
NULL,
|
||||
GetCurrentThreadId());
|
||||
DCHECK(msg_hook_ != NULL);
|
||||
return msg_hook_ != NULL;
|
||||
}
|
||||
|
||||
// static
|
||||
bool CefJavaScriptDialog::UninstallMessageHook() {
|
||||
DCHECK(msg_hook_ != NULL);
|
||||
BOOL result = ::UnhookWindowsHookEx(msg_hook_);
|
||||
DCHECK(result);
|
||||
msg_hook_ = NULL;
|
||||
|
||||
return result != FALSE;
|
||||
}
|
@@ -29,7 +29,7 @@ class CefAuthCallbackImpl : public CefAuthCallback {
|
||||
CancelNow(callback_);
|
||||
} else {
|
||||
CEF_POST_TASK(CEF_IOT,
|
||||
base::Bind(&CefAuthCallbackImpl::CancelNow, callback_));
|
||||
base::Bind(&CefAuthCallbackImpl::CancelNow, callback_));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user