From 0ab1178b9910369bdb4f7c4e99e39a5eff2c8f5c Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 18 Jan 2013 23:19:40 +0000 Subject: [PATCH] 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 --- cef1/cef_paths2.gypi | 1 + cef1/libcef/browser_webview_delegate_win.cc | 111 ++++++++++++++++++-- cef1/libcef_dll/libcef_dll.rc | 15 +++ cef1/libcef_dll/resource.h | 4 + cef1/tests/cefclient/cefclient.rc | 16 +-- cef1/tests/cefclient/cefclient_win.cpp | 4 + cef1/tests/cefclient/client_handler_win.cpp | 5 + cef1/tests/cefclient/resource.h | 24 +++-- 8 files changed, 155 insertions(+), 25 deletions(-) diff --git a/cef1/cef_paths2.gypi b/cef1/cef_paths2.gypi index d30760972..4a3638447 100644 --- a/cef1/cef_paths2.gypi +++ b/cef1/cef_paths2.gypi @@ -97,6 +97,7 @@ 'tests/cefclient/performance_test.h', 'tests/cefclient/performance_test_setup.h', 'tests/cefclient/performance_test_tests.cpp', + 'tests/cefclient/res/dialogs.html', 'tests/cefclient/res/domaccess.html', 'tests/cefclient/res/localstorage.html', 'tests/cefclient/res/logo.png', diff --git a/cef1/libcef/browser_webview_delegate_win.cc b/cef1/libcef/browser_webview_delegate_win.cc index f5b98eed5..7ef48298b 100644 --- a/cef1/libcef/browser_webview_delegate_win.cc +++ b/cef1/libcef/browser_webview_delegate_win.cc @@ -19,9 +19,11 @@ #include "libcef/cef_context.h" #include "libcef/drag_data_impl.h" #include "libcef/web_drop_target_win.h" +#include "libcef_dll/resource.h" #include "base/i18n/case_conversion.h" #include "base/message_loop.h" +#include "base/path_service.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/win/registry.h" @@ -29,6 +31,7 @@ #include "net/base/net_errors.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/WebDocument.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/platform/WebImage.h" @@ -277,6 +280,73 @@ void AddMenuSeparator(HMENU menu) { 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(lparam)); + DialogConfig* config = reinterpret_cast(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( + GetWindowLongPtr(dialog, DWL_USER)); + if (config) + EndDialog(dialog, IDCANCEL); + break; + } + case WM_COMMAND: { + DialogConfig* config = reinterpret_cast( + 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 // WebViewClient -------------------------------------------------------------- @@ -714,18 +784,16 @@ void BrowserWebViewDelegate::EndDragging() { void BrowserWebViewDelegate::ShowJavaScriptAlert(WebFrame* webframe, const CefString& message) { - // TODO(cef): Think about what we should be showing as the prompt caption std::wstring messageStr = message; - std::wstring titleStr = browser_->UIT_GetTitle(); + std::wstring titleStr = GetDialogLabel(webframe, "JavaScript Alert"); MessageBox(browser_->UIT_GetMainWndHandle(), messageStr.c_str(), titleStr.c_str(), MB_OK | MB_ICONWARNING); } bool BrowserWebViewDelegate::ShowJavaScriptConfirm(WebFrame* webframe, const CefString& message) { - // TODO(cef): Think about what we should be showing as the prompt caption 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(), titleStr.c_str(), MB_YESNO | MB_ICONQUESTION); return (rv == IDYES); @@ -733,10 +801,39 @@ bool BrowserWebViewDelegate::ShowJavaScriptConfirm(WebFrame* webframe, bool BrowserWebViewDelegate::ShowJavaScriptPrompt(WebFrame* webframe, const CefString& message, - const CefString& default_value, + const CefString& default_value, CefString* result) { - // TODO(cef): Implement a default prompt dialog - return false; + 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); + + 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(&config)); + if (rv == IDOK) + *result = config.result; + + // Return focus to the parent window. + if (IsWindow(parent)) + SetFocus(parent); + + return (rv == IDOK); } namespace { diff --git a/cef1/libcef_dll/libcef_dll.rc b/cef1/libcef_dll/libcef_dll.rc index 7bb739152..07c25c174 100644 --- a/cef1/libcef_dll/libcef_dll.rc +++ b/cef1/libcef_dll/libcef_dll.rc @@ -52,6 +52,21 @@ END #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 ///////////////////////////////////////////////////////////////////////////// // diff --git a/cef1/libcef_dll/resource.h b/cef1/libcef_dll/resource.h index 9553e1997..bbdac5be4 100644 --- a/cef1/libcef_dll/resource.h +++ b/cef1/libcef_dll/resource.h @@ -6,6 +6,10 @@ // Avoid files associated with MacOS #define _X86_ +#define IDD_PROMPT 132 +#define IDC_PROMPTEDIT 1000 +#define IDC_DIALOGTEXT 1001 + // Next default values for new objects // #ifdef APSTUDIO_INVOKED diff --git a/cef1/tests/cefclient/cefclient.rc b/cef1/tests/cefclient/cefclient.rc index 45b82e33e..540946d52 100644 --- a/cef1/tests/cefclient/cefclient.rc +++ b/cef1/tests/cefclient/cefclient.rc @@ -28,17 +28,18 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US // Binary // -IDS_LOGO BINARY "res\\logo.png" -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_DIALOGS BINARY "res\\dialogs.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_MODALMAIN BINARY "res\\modalmain.html" +IDS_OSRPLUGIN BINARY "res\\osrplugin.html" IDS_PERFORMANCE BINARY "res\\performance.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 Invoke", ID_TESTS_JAVASCRIPT_INVOKE MENUITEM "Performance Tests", ID_TESTS_PERFORMANCE + MENUITEM "Dialogs", ID_TESTS_DIALOGS MENUITEM "Plugin", ID_TESTS_PLUGIN MENUITEM "Plugin Info", ID_TESTS_PLUGIN_INFO MENUITEM "Popup Window", ID_TESTS_POPUP diff --git a/cef1/tests/cefclient/cefclient_win.cpp b/cef1/tests/cefclient/cefclient_win.cpp index 88b0b3219..541bf32c3 100644 --- a/cef1/tests/cefclient/cefclient_win.cpp +++ b/cef1/tests/cefclient/cefclient_win.cpp @@ -461,6 +461,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, if (browser.get()) performance_test::RunTest(browser); return 0; + case ID_TESTS_DIALOGS: // Run dialogs tests + if (browser.get()) + RunDialogsTest(browser); + return 0; case ID_TESTS_PLUGIN: // Test the custom plugin if (browser.get()) RunPluginTest(browser); diff --git a/cef1/tests/cefclient/client_handler_win.cpp b/cef1/tests/cefclient/client_handler_win.cpp index ea5f68a81..2a3795c51 100644 --- a/cef1/tests/cefclient/client_handler_win.cpp +++ b/cef1/tests/cefclient/client_handler_win.cpp @@ -120,6 +120,11 @@ bool ClientHandler::OnBeforeResourceLoad(CefRefPtr browser, resourceStream = GetBinaryResourceReader(IDS_PERFORMANCE); response->SetMimeType("text/html"); 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; diff --git a/cef1/tests/cefclient/resource.h b/cef1/tests/cefclient/resource.h index c6aad2a67..c3e63235c 100644 --- a/cef1/tests/cefclient/resource.h +++ b/cef1/tests/cefclient/resource.h @@ -58,18 +58,20 @@ #define ID_TESTS_GETIMAGE 32799 #define ID_TESTS_PLUGIN_INFO 32800 #define ID_TESTS_GEOLOCATION 32801 +#define ID_TESTS_DIALOGS 32803 #define IDC_STATIC -1 -#define IDS_LOGO 1000 -#define IDS_UIPLUGIN 1001 -#define IDS_LOGOBALL 1002 -#define IDS_LOCALSTORAGE 1003 -#define IDS_XMLHTTPREQUEST 1004 -#define IDS_DOMACCESS 1005 -#define IDS_OSRPLUGIN 1006 -#define IDS_MODALMAIN 1007 -#define IDS_MODALDIALOG 1008 -#define IDS_PERFORMANCE 1009 -#define IDS_TRANSPARENCY 1010 +#define IDS_DIALOGS 1000 +#define IDS_DOMACCESS 1001 +#define IDS_LOCALSTORAGE 1002 +#define IDS_LOGO 1003 +#define IDS_LOGOBALL 1004 +#define IDS_MODALDIALOG 1005 +#define IDS_MODALMAIN 1006 +#define IDS_OSRPLUGIN 1007 +#define IDS_PERFORMANCE 1008 +#define IDS_TRANSPARENCY 1009 +#define IDS_UIPLUGIN 1010 +#define IDS_XMLHTTPREQUEST 1011 // Avoid files associated with MacOS #define _X86_