Linux: Add default implementation for JavaScript alert/confirm/prompt/filechooser dialogs.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1029 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2013-01-18 20:00:45 +00:00
parent 3998917e10
commit df3537658b
7 changed files with 216 additions and 8 deletions

View File

@ -167,6 +167,7 @@
'tests/cefclient/resource_util_linux.cpp', 'tests/cefclient/resource_util_linux.cpp',
], ],
'cefclient_bundle_resources_linux': [ 'cefclient_bundle_resources_linux': [
'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

@ -94,6 +94,112 @@ void SelectionClipboardGetContents(GtkClipboard* clipboard,
} }
} }
void ShowJSAlertDialog(GtkWidget* window,
const gchar* title,
const gchar* message) {
// Create the widgets.
GtkWidget* dialog = gtk_dialog_new_with_buttons(
title,
GTK_WINDOW(window),
GtkDialogFlags(GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL),
GTK_STOCK_OK,
GTK_RESPONSE_NONE,
NULL);
GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
GtkWidget* label = gtk_label_new(message);
// Add the label and show everything we've added to the dialog.
gtk_container_add(GTK_CONTAINER(content_area), label);
gtk_widget_show_all(dialog);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
bool ShowJSConfirmDialog(GtkWidget* window,
const gchar* title,
const gchar* message) {
// Create the widgets.
GtkWidget* dialog = gtk_dialog_new_with_buttons(
title,
GTK_WINDOW(window),
GtkDialogFlags(GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL),
GTK_STOCK_NO,
GTK_RESPONSE_NO,
GTK_STOCK_YES,
GTK_RESPONSE_YES,
NULL);
GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
GtkWidget* label = gtk_label_new(message);
// Add the label and show everything we've added to the dialog.
gtk_container_add(GTK_CONTAINER(content_area), label);
gtk_widget_show_all(dialog);
gint result = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return (result == GTK_RESPONSE_YES);
}
bool ShowJSPromptDialog(GtkWidget* window,
const gchar* title,
const gchar* message,
const gchar* default_val,
std::string* return_val) {
// Create the widgets.
GtkWidget* dialog = gtk_dialog_new_with_buttons(
title,
GTK_WINDOW(window),
GtkDialogFlags(GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL),
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
NULL);
GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
GtkWidget* label = gtk_label_new(message);
GtkWidget* entry = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry), default_val);
// Add the label and entry and show everything we've added to the dialog.
gtk_container_add(GTK_CONTAINER(content_area), label);
gtk_container_add(GTK_CONTAINER(content_area), entry);
gtk_widget_show_all(dialog);
gint result = gtk_dialog_run(GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK)
*return_val = std::string(gtk_entry_get_text(GTK_ENTRY(entry)));
gtk_widget_destroy(dialog);
return (result == GTK_RESPONSE_OK);
}
bool ShowJSFileChooser(GtkWidget* window, FilePath* path) {
// Create the widgets.
GtkWidget* dialog = gtk_file_chooser_dialog_new(
"Open File",
GTK_WINDOW(window),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
gtk_widget_show_all(dialog);
int result = gtk_dialog_run(GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_ACCEPT)
*path = FilePath(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
gtk_widget_destroy(dialog);
return (result == GTK_RESPONSE_ACCEPT);
}
} // namespace } // namespace
// WebViewClient --------------------------------------------------------------- // WebViewClient ---------------------------------------------------------------
@ -382,29 +488,67 @@ void BrowserWebViewDelegate::RegisterDragDrop() {
void BrowserWebViewDelegate::ShowJavaScriptAlert( void BrowserWebViewDelegate::ShowJavaScriptAlert(
WebKit::WebFrame* webframe, const CefString& message) { WebKit::WebFrame* webframe, const CefString& message) {
NOTIMPLEMENTED(); std::string messageStr(message);
std::string urlStr(browser_->GetMainFrame()->GetURL());
gfx::NativeView view = browser_->UIT_GetMainWndHandle();
GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(view));
ShowJSAlertDialog(window,
static_cast<const gchar*>(urlStr.c_str()),
static_cast<const gchar*>(messageStr.c_str()));
} }
bool BrowserWebViewDelegate::ShowJavaScriptConfirm( bool BrowserWebViewDelegate::ShowJavaScriptConfirm(
WebKit::WebFrame* webframe, const CefString& message) { WebKit::WebFrame* webframe, const CefString& message) {
NOTIMPLEMENTED(); std::string messageStr(message);
return false; std::string urlStr(browser_->GetMainFrame()->GetURL());
gfx::NativeView view = browser_->UIT_GetMainWndHandle();
GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(view));
return ShowJSConfirmDialog(
window,
static_cast<const gchar*>(urlStr.c_str()),
static_cast<const gchar*>(messageStr.c_str()));
} }
bool BrowserWebViewDelegate::ShowJavaScriptPrompt( bool BrowserWebViewDelegate::ShowJavaScriptPrompt(
WebKit::WebFrame* webframe, const CefString& message, WebKit::WebFrame* webframe, const CefString& message,
const CefString& default_value, CefString* result) { const CefString& default_value, CefString* result) {
NOTIMPLEMENTED(); std::string messageStr(message);
return false; std::string defaultStr(default_value);
std::string urlStr(browser_->GetMainFrame()->GetURL());
gfx::NativeView view = browser_->UIT_GetMainWndHandle();
GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(view));
std::string return_val;
bool resp = ShowJSPromptDialog(
window,
static_cast<const gchar*>(urlStr.c_str()),
static_cast<const gchar*>(messageStr.c_str()),
static_cast<const gchar*>(defaultStr.c_str()),
&return_val);
if (resp)
*result = return_val;
return resp;
} }
// Called to show the file chooser dialog.
bool BrowserWebViewDelegate::ShowFileChooser( bool BrowserWebViewDelegate::ShowFileChooser(
std::vector<FilePath>& file_names, std::vector<FilePath>& file_names,
bool multi_select, bool multi_select,
const WebKit::WebString& title, const WebKit::WebString& title,
const FilePath& default_file, const FilePath& default_file,
const std::vector<std::string>& accept_mime_types) { const std::vector<std::string>& accept_mime_types) {
NOTIMPLEMENTED(); gfx::NativeView view = browser_->UIT_GetMainWndHandle();
return false; GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(view));
FilePath file_name;
bool resp = ShowJSFileChooser(window, &file_name);
if (resp)
file_names.push_back(file_name);
return resp;
} }

View File

@ -660,3 +660,7 @@ void RunPluginInfoTest(CefRefPtr<CefBrowser> browser) {
void RunGeolocationTest(CefRefPtr<CefBrowser> browser) { void RunGeolocationTest(CefRefPtr<CefBrowser> browser) {
browser->GetMainFrame()->LoadURL("http://html5demos.com/geo"); browser->GetMainFrame()->LoadURL("http://html5demos.com/geo");
} }
void RunDialogsTest(CefRefPtr<CefBrowser> browser) {
browser->GetMainFrame()->LoadURL("http://tests/dialogs");
}

View File

@ -52,6 +52,7 @@ void RunDragDropTest(CefRefPtr<CefBrowser> browser);
void RunModalDialogTest(CefRefPtr<CefBrowser> browser); void RunModalDialogTest(CefRefPtr<CefBrowser> browser);
void RunPluginInfoTest(CefRefPtr<CefBrowser> browser); void RunPluginInfoTest(CefRefPtr<CefBrowser> browser);
void RunGeolocationTest(CefRefPtr<CefBrowser> browser); void RunGeolocationTest(CefRefPtr<CefBrowser> browser);
void RunDialogsTest(CefRefPtr<CefBrowser> browser);
#if defined(OS_WIN) #if defined(OS_WIN)
void RunTransparentPopupTest(CefRefPtr<CefBrowser> browser); void RunTransparentPopupTest(CefRefPtr<CefBrowser> browser);

View File

@ -79,6 +79,14 @@ gboolean PerformanceActivated(GtkWidget* widget) {
return FALSE; // Don't stop this message. return FALSE; // Don't stop this message.
} }
// Callback for Debug > Dialogs... menu item.
gboolean DialogsActivated(GtkWidget* widget) {
if (g_handler.get() && g_handler->GetBrowserHwnd())
RunDialogsTest(g_handler->GetBrowser());
return FALSE; // Don't stop this message.
}
// Callback for Debug > Request... menu item. // Callback for Debug > Request... menu item.
gboolean RequestActivated(GtkWidget* widget) { gboolean RequestActivated(GtkWidget* widget) {
if (g_handler.get() && g_handler->GetBrowserHwnd()) if (g_handler.get() && g_handler->GetBrowserHwnd())
@ -291,6 +299,8 @@ GtkWidget* CreateMenuBar() {
G_CALLBACK(JSExecuteActivated)); G_CALLBACK(JSExecuteActivated));
AddMenuEntry(debug_menu, "Performance Tests", AddMenuEntry(debug_menu, "Performance Tests",
G_CALLBACK(PerformanceActivated)); G_CALLBACK(PerformanceActivated));
AddMenuEntry(debug_menu, "Dialogs",
G_CALLBACK(DialogsActivated));
AddMenuEntry(debug_menu, "Request", AddMenuEntry(debug_menu, "Request",
G_CALLBACK(RequestActivated)); G_CALLBACK(RequestActivated));
AddMenuEntry(debug_menu, "Local Storage", AddMenuEntry(debug_menu, "Local Storage",

View File

@ -66,6 +66,11 @@ bool ClientHandler::OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
resourceStream = GetBinaryResourceReader("performance.html"); resourceStream = GetBinaryResourceReader("performance.html");
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("dialogs.html");
response->SetMimeType("text/html");
response->SetStatus(200);
} }
return false; return false;

View File

@ -0,0 +1,43 @@
<html>
<head>
<title>Dialog Test</title>
<script>
function show_alert() {
alert("I am an alert box!");
}
function show_confirm() {
var r = confirm("Press a button");
var msg = r ? "You pressed OK!" : "You pressed Cancel!";
document.getElementById('cm').innerText = msg;
}
function show_prompt() {
var name = prompt("Please enter your name" ,"Harry Potter");
if (name != null && name != "")
document.getElementById('pm').innerText = "Hello " + name + "!";
}
function update_time() {
document.getElementById('time').innerText = new Date().toLocaleString();
}
function setup() {
update_time();
setInterval(update_time, 1000);
}
window.addEventListener('load', setup, false);
</script>
</head>
<body>
<form>
Click a button to show the associated dialog type.
<br/><input type="button" onclick="show_alert();" value="Show Alert">
<br/><input type="button" onclick="show_confirm();" value="Show Confirm"> <span id="cm"></span>
<br/><input type="button" onclick="show_prompt();" value="Show Prompt"> <span id="pm"></span>
<br/>input type="file": <input type="file" name="pic" accept="text/*,.js,.css,image/*">
<p id="time"></p>
</form>
</body>
</html>