Windows: Add default implementation for JavaScript prompt dialog (issue #861).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1049 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2013-01-18 23:19:40 +00:00
parent 52c203d4cf
commit 0ab1178b99
8 changed files with 155 additions and 25 deletions

View File

@ -97,6 +97,7 @@
'tests/cefclient/performance_test.h', 'tests/cefclient/performance_test.h',
'tests/cefclient/performance_test_setup.h', 'tests/cefclient/performance_test_setup.h',
'tests/cefclient/performance_test_tests.cpp', 'tests/cefclient/performance_test_tests.cpp',
'tests/cefclient/res/dialogs.html',
'tests/cefclient/res/domaccess.html', 'tests/cefclient/res/domaccess.html',
'tests/cefclient/res/localstorage.html', 'tests/cefclient/res/localstorage.html',
'tests/cefclient/res/logo.png', 'tests/cefclient/res/logo.png',

View File

@ -19,9 +19,11 @@
#include "libcef/cef_context.h" #include "libcef/cef_context.h"
#include "libcef/drag_data_impl.h" #include "libcef/drag_data_impl.h"
#include "libcef/web_drop_target_win.h" #include "libcef/web_drop_target_win.h"
#include "libcef_dll/resource.h"
#include "base/i18n/case_conversion.h" #include "base/i18n/case_conversion.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h" #include "base/string_util.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "base/win/registry.h" #include "base/win/registry.h"
@ -29,6 +31,7 @@
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebDragData.h" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebDragData.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebImage.h" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebImage.h"
@ -277,6 +280,73 @@ void AddMenuSeparator(HMENU menu) {
InsertMenuItem(menu, -1, TRUE, &mii); InsertMenuItem(menu, -1, TRUE, &mii);
} }
std::wstring GetDialogLabel(WebKit::WebFrame* webframe,
const std::string& label) {
const GURL& url = webframe->document().url();
std::string urlStr;
if (!url.is_empty())
urlStr = url.host();
std::string labelStr = label;
if (!urlStr.empty())
labelStr += " - " + urlStr;
return UTF8ToWide(labelStr);
}
struct DialogConfig {
std::wstring label;
std::wstring message;
std::wstring default_value;
std::wstring result;
};
INT_PTR CALLBACK DialogProc(HWND dialog,
UINT message,
WPARAM wparam,
LPARAM lparam) {
switch (message) {
case WM_INITDIALOG: {
SetWindowLongPtr(dialog, DWL_USER, static_cast<LONG_PTR>(lparam));
DialogConfig* config = reinterpret_cast<DialogConfig*>(lparam);
SetWindowText(dialog, config->label.c_str());
SetDlgItemText(dialog, IDC_DIALOGTEXT, config->message.c_str());
SetDlgItemText(dialog, IDC_PROMPTEDIT, config->default_value.c_str());
break;
}
case WM_CLOSE: {
DialogConfig* config = reinterpret_cast<DialogConfig*>(
GetWindowLongPtr(dialog, DWL_USER));
if (config)
EndDialog(dialog, IDCANCEL);
break;
}
case WM_COMMAND: {
DialogConfig* config = reinterpret_cast<DialogConfig*>(
GetWindowLongPtr(dialog, DWL_USER));
bool finish = false;
switch (LOWORD(wparam)) {
case IDOK: {
finish = true;
size_t length =
GetWindowTextLength(GetDlgItem(dialog, IDC_PROMPTEDIT)) + 1;
if (length > 1) {
GetDlgItemText(dialog, IDC_PROMPTEDIT,
WriteInto(&config->result, length), length);
}
break;
}
case IDCANCEL:
finish = true;
break;
}
if (finish)
EndDialog(dialog, LOWORD(wparam));
break;
}
}
return DefWindowProc(dialog, message, wparam, lparam);
}
} // namespace } // namespace
// WebViewClient -------------------------------------------------------------- // WebViewClient --------------------------------------------------------------
@ -714,18 +784,16 @@ void BrowserWebViewDelegate::EndDragging() {
void BrowserWebViewDelegate::ShowJavaScriptAlert(WebFrame* webframe, void BrowserWebViewDelegate::ShowJavaScriptAlert(WebFrame* webframe,
const CefString& message) { const CefString& message) {
// TODO(cef): Think about what we should be showing as the prompt caption
std::wstring messageStr = message; std::wstring messageStr = message;
std::wstring titleStr = browser_->UIT_GetTitle(); std::wstring titleStr = GetDialogLabel(webframe, "JavaScript Alert");
MessageBox(browser_->UIT_GetMainWndHandle(), messageStr.c_str(), MessageBox(browser_->UIT_GetMainWndHandle(), messageStr.c_str(),
titleStr.c_str(), MB_OK | MB_ICONWARNING); titleStr.c_str(), MB_OK | MB_ICONWARNING);
} }
bool BrowserWebViewDelegate::ShowJavaScriptConfirm(WebFrame* webframe, bool BrowserWebViewDelegate::ShowJavaScriptConfirm(WebFrame* webframe,
const CefString& message) { const CefString& message) {
// TODO(cef): Think about what we should be showing as the prompt caption
std::wstring messageStr = message; std::wstring messageStr = message;
std::wstring titleStr = browser_->UIT_GetTitle(); std::wstring titleStr = GetDialogLabel(webframe, "JavaScript Confirm");
int rv = MessageBox(browser_->UIT_GetMainWndHandle(), messageStr.c_str(), int rv = MessageBox(browser_->UIT_GetMainWndHandle(), messageStr.c_str(),
titleStr.c_str(), MB_YESNO | MB_ICONQUESTION); titleStr.c_str(), MB_YESNO | MB_ICONQUESTION);
return (rv == IDYES); return (rv == IDYES);
@ -735,8 +803,37 @@ bool BrowserWebViewDelegate::ShowJavaScriptPrompt(WebFrame* webframe,
const CefString& message, const CefString& message,
const CefString& default_value, const CefString& default_value,
CefString* result) { CefString* result) {
// TODO(cef): Implement a default prompt dialog FilePath file_path;
return false; 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);
DialogConfig config;
config.label = GetDialogLabel(webframe, "JavaScript Prompt");
config.message = message;
config.default_value = default_value;
HWND parent = GetAncestor(browser_->GetWindowHandle(), GA_ROOT);
// Show the modal dialog.
int rv = DialogBoxParam(hModule,
MAKEINTRESOURCE(IDD_PROMPT),
parent,
DialogProc,
reinterpret_cast<LPARAM>(&config));
if (rv == IDOK)
*result = config.result;
// Return focus to the parent window.
if (IsWindow(parent))
SetFocus(parent);
return (rv == IDOK);
} }
namespace { namespace {

View File

@ -52,6 +52,21 @@ END
#endif // APSTUDIO_INVOKED #endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_PROMPT DIALOGEX 0, 0, 241, 76
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "JavaScript Prompt"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,131,55,50,14
LTEXT "",IDC_DIALOGTEXT,16,17,210,18
PUSHBUTTON "Cancel",IDCANCEL,184,55,50,14
EDITTEXT IDC_PROMPTEDIT,15,33,210,14,ES_AUTOHSCROLL
END
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// //

View File

@ -6,6 +6,10 @@
// Avoid files associated with MacOS // Avoid files associated with MacOS
#define _X86_ #define _X86_
#define IDD_PROMPT 132
#define IDC_PROMPTEDIT 1000
#define IDC_DIALOGTEXT 1001
// Next default values for new objects // Next default values for new objects
// //
#ifdef APSTUDIO_INVOKED #ifdef APSTUDIO_INVOKED

View File

@ -28,17 +28,18 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
// Binary // Binary
// //
IDS_LOGO BINARY "res\\logo.png" IDS_DIALOGS BINARY "res\\dialogs.html"
IDS_UIPLUGIN BINARY "res\\uiplugin.html"
IDS_OSRPLUGIN BINARY "res\\osrplugin.html"
IDS_LOGOBALL BINARY "res\\logoball.png"
IDS_LOCALSTORAGE BINARY "res\\localstorage.html"
IDS_XMLHTTPREQUEST BINARY "res\\xmlhttprequest.html"
IDS_DOMACCESS BINARY "res\\domaccess.html" IDS_DOMACCESS BINARY "res\\domaccess.html"
IDS_MODALMAIN BINARY "res\\modalmain.html" IDS_LOCALSTORAGE BINARY "res\\localstorage.html"
IDS_LOGO BINARY "res\\logo.png"
IDS_LOGOBALL BINARY "res\\logoball.png"
IDS_MODALDIALOG BINARY "res\\modaldialog.html" IDS_MODALDIALOG BINARY "res\\modaldialog.html"
IDS_MODALMAIN BINARY "res\\modalmain.html"
IDS_OSRPLUGIN BINARY "res\\osrplugin.html"
IDS_PERFORMANCE BINARY "res\\performance.html" IDS_PERFORMANCE BINARY "res\\performance.html"
IDS_TRANSPARENCY BINARY "res\\transparency.html" IDS_TRANSPARENCY BINARY "res\\transparency.html"
IDS_UIPLUGIN BINARY "res\\uiplugin.html"
IDS_XMLHTTPREQUEST BINARY "res\\xmlhttprequest.html"
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// //
@ -78,6 +79,7 @@ BEGIN
MENUITEM "JavaScript Execute", ID_TESTS_JAVASCRIPT_EXECUTE MENUITEM "JavaScript Execute", ID_TESTS_JAVASCRIPT_EXECUTE
MENUITEM "JavaScript Invoke", ID_TESTS_JAVASCRIPT_INVOKE MENUITEM "JavaScript Invoke", ID_TESTS_JAVASCRIPT_INVOKE
MENUITEM "Performance Tests", ID_TESTS_PERFORMANCE MENUITEM "Performance Tests", ID_TESTS_PERFORMANCE
MENUITEM "Dialogs", ID_TESTS_DIALOGS
MENUITEM "Plugin", ID_TESTS_PLUGIN MENUITEM "Plugin", ID_TESTS_PLUGIN
MENUITEM "Plugin Info", ID_TESTS_PLUGIN_INFO MENUITEM "Plugin Info", ID_TESTS_PLUGIN_INFO
MENUITEM "Popup Window", ID_TESTS_POPUP MENUITEM "Popup Window", ID_TESTS_POPUP

View File

@ -461,6 +461,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
if (browser.get()) if (browser.get())
performance_test::RunTest(browser); performance_test::RunTest(browser);
return 0; return 0;
case ID_TESTS_DIALOGS: // Run dialogs tests
if (browser.get())
RunDialogsTest(browser);
return 0;
case ID_TESTS_PLUGIN: // Test the custom plugin case ID_TESTS_PLUGIN: // Test the custom plugin
if (browser.get()) if (browser.get())
RunPluginTest(browser); RunPluginTest(browser);

View File

@ -120,6 +120,11 @@ bool ClientHandler::OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
resourceStream = GetBinaryResourceReader(IDS_PERFORMANCE); resourceStream = GetBinaryResourceReader(IDS_PERFORMANCE);
response->SetMimeType("text/html"); response->SetMimeType("text/html");
response->SetStatus(200); response->SetStatus(200);
} else if (url == "http://tests/dialogs") {
// Show the dialogs HTML contents
resourceStream = GetBinaryResourceReader(IDS_DIALOGS);
response->SetMimeType("text/html");
response->SetStatus(200);
} }
return false; return false;

View File

@ -58,18 +58,20 @@
#define ID_TESTS_GETIMAGE 32799 #define ID_TESTS_GETIMAGE 32799
#define ID_TESTS_PLUGIN_INFO 32800 #define ID_TESTS_PLUGIN_INFO 32800
#define ID_TESTS_GEOLOCATION 32801 #define ID_TESTS_GEOLOCATION 32801
#define ID_TESTS_DIALOGS 32803
#define IDC_STATIC -1 #define IDC_STATIC -1
#define IDS_LOGO 1000 #define IDS_DIALOGS 1000
#define IDS_UIPLUGIN 1001 #define IDS_DOMACCESS 1001
#define IDS_LOGOBALL 1002 #define IDS_LOCALSTORAGE 1002
#define IDS_LOCALSTORAGE 1003 #define IDS_LOGO 1003
#define IDS_XMLHTTPREQUEST 1004 #define IDS_LOGOBALL 1004
#define IDS_DOMACCESS 1005 #define IDS_MODALDIALOG 1005
#define IDS_OSRPLUGIN 1006 #define IDS_MODALMAIN 1006
#define IDS_MODALMAIN 1007 #define IDS_OSRPLUGIN 1007
#define IDS_MODALDIALOG 1008 #define IDS_PERFORMANCE 1008
#define IDS_PERFORMANCE 1009 #define IDS_TRANSPARENCY 1009
#define IDS_TRANSPARENCY 1010 #define IDS_UIPLUGIN 1010
#define IDS_XMLHTTPREQUEST 1011
// Avoid files associated with MacOS // Avoid files associated with MacOS
#define _X86_ #define _X86_