OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/shell/browser/shell_login_dialog.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/strings/string16.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/render_frame_host.h" | |
14 #include "content/public/browser/resource_dispatcher_host.h" | |
15 #include "content/public/browser/resource_request_info.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "content/public/browser/web_contents_view.h" | |
18 #include "ui/base/gtk/gtk_hig_constants.h" | |
19 | |
20 namespace content { | |
21 | |
22 void ShellLoginDialog::PlatformCreateDialog(const base::string16& message) { | |
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
24 | |
25 int render_process_id; | |
26 int render_frame_id; | |
27 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame( | |
28 &render_process_id, &render_frame_id)) { | |
29 NOTREACHED(); | |
30 } | |
31 | |
32 WebContents* web_contents = NULL; | |
33 RenderFrameHost* render_frame_host = | |
34 RenderFrameHost::FromID(render_process_id, render_frame_id); | |
35 web_contents = WebContents::FromRenderFrameHost(render_frame_host); | |
36 DCHECK(web_contents); | |
37 | |
38 gfx::NativeWindow parent_window = | |
39 web_contents->GetView()->GetTopLevelNativeWindow(); | |
40 | |
41 root_ = gtk_message_dialog_new(parent_window, | |
42 GTK_DIALOG_MODAL, | |
43 GTK_MESSAGE_INFO, | |
44 GTK_BUTTONS_OK_CANCEL, | |
45 "Please log in."); | |
46 | |
47 GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(root_)); | |
48 GtkWidget* label = gtk_label_new(base::UTF16ToUTF8(message).c_str()); | |
49 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
50 gtk_box_pack_start(GTK_BOX(content_area), label, FALSE, FALSE, 0); | |
51 | |
52 username_entry_ = gtk_entry_new(); | |
53 gtk_entry_set_activates_default(GTK_ENTRY(username_entry_), TRUE); | |
54 | |
55 password_entry_ = gtk_entry_new(); | |
56 gtk_entry_set_activates_default(GTK_ENTRY(password_entry_), TRUE); | |
57 gtk_entry_set_visibility(GTK_ENTRY(password_entry_), FALSE); | |
58 | |
59 GtkWidget* table = gtk_table_new(2, 2, FALSE); | |
60 gtk_table_set_col_spacing(GTK_TABLE(table), 0, ui::kLabelSpacing); | |
61 gtk_table_set_row_spacings(GTK_TABLE(table), ui::kControlSpacing); | |
62 | |
63 GtkWidget* username_label = gtk_label_new("Username:"); | |
64 gtk_misc_set_alignment(GTK_MISC(username_label), 0, 0.5); | |
65 | |
66 gtk_table_attach(GTK_TABLE(table), username_label, 0, 1, 0, 1, GTK_FILL, | |
67 GTK_FILL, 0, 0); | |
68 gtk_table_attach_defaults(GTK_TABLE(table), username_entry_, 1, 2, 0, 1); | |
69 | |
70 GtkWidget* password_label = gtk_label_new("Password:"); | |
71 gtk_misc_set_alignment(GTK_MISC(password_label), 0, 0.5); | |
72 | |
73 gtk_table_attach(GTK_TABLE(table), password_label, 0, 1, 1, 2, GTK_FILL, | |
74 GTK_FILL, 0, 0); | |
75 gtk_table_attach_defaults(GTK_TABLE(table), password_entry_, 1, 2, 1, 2); | |
76 | |
77 gtk_box_pack_start(GTK_BOX(content_area), table, FALSE, FALSE, 0); | |
78 | |
79 g_signal_connect(root_, "response", G_CALLBACK(OnResponseThunk), this); | |
80 gtk_widget_grab_focus(username_entry_); | |
81 gtk_widget_show_all(GTK_WIDGET(root_)); | |
82 } | |
83 | |
84 void ShellLoginDialog::PlatformCleanUp() { | |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
86 } | |
87 | |
88 void ShellLoginDialog::PlatformRequestCancelled() { | |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
90 } | |
91 | |
92 void ShellLoginDialog::OnResponse(GtkWidget* sender, int response_id) { | |
93 switch (response_id) { | |
94 case GTK_RESPONSE_OK: | |
95 UserAcceptedAuth( | |
96 base::UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(username_entry_))), | |
97 base::UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(password_entry_)))); | |
98 break; | |
99 case GTK_RESPONSE_CANCEL: | |
100 case GTK_RESPONSE_DELETE_EVENT: | |
101 UserCancelledAuth(); | |
102 break; | |
103 default: | |
104 NOTREACHED(); | |
105 } | |
106 | |
107 gtk_widget_destroy(root_); | |
108 } | |
109 | |
110 } // namespace content | |
OLD | NEW |