1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/browser/shell_javascript_dialog.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/shell/app/resource.h"
13 #include "content/shell/browser/shell.h"
14 #include "content/shell/browser/shell_javascript_dialog_manager.h"
18 const char kPromptTextId
[] = "content_shell_prompt_text";
20 // If there's a text entry in the dialog, get the text from the first one and
22 string16
GetPromptText(GtkDialog
* dialog
) {
23 GtkWidget
* widget
= static_cast<GtkWidget
*>(
24 g_object_get_data(G_OBJECT(dialog
), kPromptTextId
));
26 return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(widget
)));
35 ShellJavaScriptDialog::ShellJavaScriptDialog(
36 ShellJavaScriptDialogManager
* manager
,
37 gfx::NativeWindow parent_window
,
38 JavaScriptMessageType message_type
,
39 const string16
& message_text
,
40 const string16
& default_prompt_text
,
41 const JavaScriptDialogManager::DialogClosedCallback
& callback
)
44 parent_window_(parent_window
) {
45 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
46 GtkMessageType gtk_message_type
= GTK_MESSAGE_OTHER
;
48 switch (message_type
) {
49 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT
:
50 buttons
= GTK_BUTTONS_NONE
;
51 gtk_message_type
= GTK_MESSAGE_WARNING
;
54 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM
:
55 buttons
= GTK_BUTTONS_CANCEL
;
56 gtk_message_type
= GTK_MESSAGE_QUESTION
;
59 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT
:
60 buttons
= GTK_BUTTONS_CANCEL
;
61 gtk_message_type
= GTK_MESSAGE_QUESTION
;
68 gtk_dialog_
= gtk_message_dialog_new(parent_window_
,
73 UTF16ToUTF8(message_text
).c_str());
74 g_signal_connect(gtk_dialog_
,
76 G_CALLBACK(gtk_widget_hide_on_delete
),
78 gtk_window_set_title(GTK_WINDOW(gtk_dialog_
), "JavaScript");
80 GtkWidget
* ok_button
= gtk_dialog_add_button(GTK_DIALOG(gtk_dialog_
),
84 if (message_type
!= content::JAVASCRIPT_MESSAGE_TYPE_PROMPT
)
85 gtk_widget_grab_focus(ok_button
);
87 if (message_type
== content::JAVASCRIPT_MESSAGE_TYPE_PROMPT
) {
88 GtkWidget
* content_area
=
89 gtk_dialog_get_content_area(GTK_DIALOG(gtk_dialog_
));
90 GtkWidget
* text_box
= gtk_entry_new();
91 gtk_entry_set_text(GTK_ENTRY(text_box
),
92 UTF16ToUTF8(default_prompt_text
).c_str());
93 gtk_box_pack_start(GTK_BOX(content_area
), text_box
, TRUE
, TRUE
, 0);
94 g_object_set_data(G_OBJECT(gtk_dialog_
), kPromptTextId
, text_box
);
95 gtk_entry_set_activates_default(GTK_ENTRY(text_box
), TRUE
);
98 gtk_dialog_set_default_response(GTK_DIALOG(gtk_dialog_
), GTK_RESPONSE_OK
);
99 g_signal_connect(gtk_dialog_
, "response", G_CALLBACK(OnResponseThunk
), this);
100 gtk_widget_show_all(GTK_WIDGET(gtk_dialog_
));
103 ShellJavaScriptDialog::~ShellJavaScriptDialog() {
106 void ShellJavaScriptDialog::Cancel() {
109 void ShellJavaScriptDialog::OnResponse(GtkWidget
* dialog
, int response_id
) {
110 switch (response_id
) {
111 case GTK_RESPONSE_OK
:
112 callback_
.Run(true, GetPromptText(GTK_DIALOG(dialog
)));
114 case GTK_RESPONSE_CANCEL
:
115 case GTK_RESPONSE_DELETE_EVENT
:
116 callback_
.Run(false, string16());
122 gtk_widget_destroy(dialog
);
124 manager_
->DialogClosed(this);
127 } // namespace content