From b6ede2c8fa93a148843100268c1ee7a550093899 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 18 Jan 2013 21:38:47 +0000 Subject: [PATCH] Merge revision 1041 changes: - Mac: Add default implementation for JavaScript confirm/prompt dialogs (issue #795). git-svn-id: https://chromiumembedded.googlecode.com/svn/branches/1364@1042 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- cef1/cef_paths2.gypi | 1 + cef1/libcef/browser_webview_delegate_mac.mm | 65 +++++++++++++++++++-- cef1/tests/cefclient/cefclient_mac.mm | 9 +++ cef1/tests/cefclient/client_handler_mac.mm | 5 ++ 4 files changed, 74 insertions(+), 6 deletions(-) diff --git a/cef1/cef_paths2.gypi b/cef1/cef_paths2.gypi index 83e8750cb..d30760972 100644 --- a/cef1/cef_paths2.gypi +++ b/cef1/cef_paths2.gypi @@ -152,6 +152,7 @@ 'tests/cefclient/mac/English.lproj/InfoPlist.strings', 'tests/cefclient/mac/English.lproj/MainMenu.xib', 'tests/cefclient/mac/Info.plist', + '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_mac.mm b/cef1/libcef/browser_webview_delegate_mac.mm index eb47cf433..200fee800 100644 --- a/cef1/libcef/browser_webview_delegate_mac.mm +++ b/cef1/libcef/browser_webview_delegate_mac.mm @@ -13,11 +13,14 @@ #include "base/file_util.h" #include "base/mac/mac_util.h" #include "base/sys_string_conversions.h" +#include "base/utf_string_conversions.h" #include "base/threading/thread_restrictions.h" #include "skia/ext/skia_utils_mac.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" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h" @@ -80,6 +83,17 @@ void AddMenuSeparator(NSMenu* menu) { [menu addItem:item]; } +NSString* GetDialogLabel(WebKit::WebFrame* webframe, const std::string& label) { + const GURL& url = webframe->document().url(); + std::string urlStr; + if (!url.is_empty()) + urlStr = url.host(); + string16 labelStr = ASCIIToUTF16(label); + if (!urlStr.empty()) + labelStr += ASCIIToUTF16(" - " + urlStr); + return base::SysUTF16ToNSString(labelStr); +} + } // namespace @interface BrowserMenuDelegate : NSObject { @@ -470,9 +484,11 @@ void BrowserWebViewDelegate::DidMovePlugin( void BrowserWebViewDelegate::ShowJavaScriptAlert( WebKit::WebFrame* webframe, const CefString& message) { + NSString* label = GetDialogLabel(webframe, "JavaScript Alert"); std::string messageStr(message); - NSString *text = [NSString stringWithUTF8String:messageStr.c_str()]; - NSAlert *alert = [NSAlert alertWithMessageText:@"JavaScript Alert" + NSString* text = [NSString stringWithUTF8String:messageStr.c_str()]; + + NSAlert* alert = [NSAlert alertWithMessageText:label defaultButton:@"OK" alternateButton:nil otherButton:nil @@ -482,15 +498,52 @@ void BrowserWebViewDelegate::ShowJavaScriptAlert( bool BrowserWebViewDelegate::ShowJavaScriptConfirm( WebKit::WebFrame* webframe, const CefString& message) { - NOTIMPLEMENTED(); - return false; + NSString* label = GetDialogLabel(webframe, "JavaScript Confirm"); + std::string messageStr(message); + NSString* text = [NSString stringWithUTF8String:messageStr.c_str()]; + + NSAlert *alert = [NSAlert alertWithMessageText:label + defaultButton:@"OK" + alternateButton:@"Cancel" + otherButton:nil + informativeTextWithFormat:text]; + + NSInteger r = [alert runModal]; + + return (r == NSAlertDefaultReturn); } bool BrowserWebViewDelegate::ShowJavaScriptPrompt( WebKit::WebFrame* webframe, const CefString& message, const CefString& default_value, CefString* result) { - NOTIMPLEMENTED(); - return false; + NSString* label = GetDialogLabel(webframe, "JavaScript Prompt"); + + NSAlert *alert = + [NSAlert alertWithMessageText:label + defaultButton:@"OK" + alternateButton:@"Cancel" + otherButton:nil + informativeTextWithFormat:@""]; + + NSTextField *input = + [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]; + [[input cell] setLineBreakMode:NSLineBreakByTruncatingTail]; + std::string default_valueStr(default_value); + [input setStringValue: + [NSString stringWithUTF8String:default_valueStr.c_str()]]; + [alert setAccessoryView:input]; + [input release]; + + std::string messageStr(message); + [alert setInformativeText:[NSString stringWithUTF8String:messageStr.c_str()]]; + + NSInteger r = [alert runModal]; + if (r == NSAlertDefaultReturn) { + [input validateEditing]; + *result = base::SysNSStringToUTF8([input stringValue]); + } + + return (r == NSAlertDefaultReturn); } // Called to show the file chooser dialog. diff --git a/cef1/tests/cefclient/cefclient_mac.mm b/cef1/tests/cefclient/cefclient_mac.mm index 2dcb3214b..5ca9a7c89 100644 --- a/cef1/tests/cefclient/cefclient_mac.mm +++ b/cef1/tests/cefclient/cefclient_mac.mm @@ -200,6 +200,7 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { - (IBAction)testJSExecute:(id)sender; - (IBAction)testJSInvoke:(id)sender; - (IBAction)testPerformance:(id)sender; +- (IBAction)testDialogs:(id)sender; - (IBAction)testRequest:(id)sender; - (IBAction)testLocalStorage:(id)sender; - (IBAction)testXMLHttpRequest:(id)sender; @@ -259,6 +260,9 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { [testMenu addItemWithTitle:@"Performance Tests" action:@selector(testPerformance:) keyEquivalent:@""]; + [testMenu addItemWithTitle:@"Dialogs" + action:@selector(testDialogs:) + keyEquivalent:@""]; [testMenu addItemWithTitle:@"Popup Window" action:@selector(testPopupWindow:) keyEquivalent:@""]; @@ -445,6 +449,11 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { performance_test::RunTest(g_handler->GetBrowser()); } +- (IBAction)testDialogs:(id)sender { + if (g_handler.get() && g_handler->GetBrowserHwnd()) + RunDialogsTest(g_handler->GetBrowser()); +} + - (IBAction)testRequest:(id)sender { if (g_handler.get() && g_handler->GetBrowserHwnd()) RunRequestTest(g_handler->GetBrowser()); diff --git a/cef1/tests/cefclient/client_handler_mac.mm b/cef1/tests/cefclient/client_handler_mac.mm index 669f605cc..3ab886b5e 100644 --- a/cef1/tests/cefclient/client_handler_mac.mm +++ b/cef1/tests/cefclient/client_handler_mac.mm @@ -80,6 +80,11 @@ bool ClientHandler::OnBeforeResourceLoad(CefRefPtr browser, resourceStream = GetBinaryResourceReader("performance.html"); response->SetMimeType("text/html"); response->SetStatus(200); + } else if (url == "http://tests/dialogs") { + // Show the dialogs HTML contents + resourceStream = GetBinaryResourceReader("dialogs.html"); + response->SetMimeType("text/html"); + response->SetStatus(200); } return false;