1 // Copyright (c) 2012 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/shell_login_dialog.h"
9 #include "base/logging.h"
10 #include "base/string16.h"
11 #include "base/utf_string_conversions.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_view_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"
22 void ShellLoginDialog::PlatformCreateDialog(const string16
& message
) {
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
25 int render_process_id
;
27 if (!ResourceRequestInfo::ForRequest(request_
)->GetAssociatedRenderView(
28 &render_process_id
, &render_view_id
)) {
32 WebContents
* web_contents
= NULL
;
33 RenderViewHost
* render_view_host
=
34 RenderViewHost::FromID(render_process_id
, render_view_id
);
36 web_contents
= WebContents::FromRenderViewHost(render_view_host
);
39 gfx::NativeWindow parent_window
=
40 web_contents
->GetView()->GetTopLevelNativeWindow();
42 root_
= gtk_message_dialog_new(parent_window
,
45 GTK_BUTTONS_OK_CANCEL
,
48 GtkWidget
* content_area
= gtk_dialog_get_content_area(GTK_DIALOG(root_
));
49 GtkWidget
* label
= gtk_label_new(UTF16ToUTF8(message
).c_str());
50 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
51 gtk_box_pack_start(GTK_BOX(content_area
), label
, FALSE
, FALSE
, 0);
53 username_entry_
= gtk_entry_new();
54 gtk_entry_set_activates_default(GTK_ENTRY(username_entry_
), TRUE
);
56 password_entry_
= gtk_entry_new();
57 gtk_entry_set_activates_default(GTK_ENTRY(password_entry_
), TRUE
);
58 gtk_entry_set_visibility(GTK_ENTRY(password_entry_
), FALSE
);
60 GtkWidget
* table
= gtk_table_new(2, 2, FALSE
);
61 gtk_table_set_col_spacing(GTK_TABLE(table
), 0, ui::kLabelSpacing
);
62 gtk_table_set_row_spacings(GTK_TABLE(table
), ui::kControlSpacing
);
64 GtkWidget
* username_label
= gtk_label_new("Username:");
65 gtk_misc_set_alignment(GTK_MISC(username_label
), 0, 0.5);
67 gtk_table_attach(GTK_TABLE(table
), username_label
, 0, 1, 0, 1, GTK_FILL
,
69 gtk_table_attach_defaults(GTK_TABLE(table
), username_entry_
, 1, 2, 0, 1);
71 GtkWidget
* password_label
= gtk_label_new("Password:");
72 gtk_misc_set_alignment(GTK_MISC(password_label
), 0, 0.5);
74 gtk_table_attach(GTK_TABLE(table
), password_label
, 0, 1, 1, 2, GTK_FILL
,
76 gtk_table_attach_defaults(GTK_TABLE(table
), password_entry_
, 1, 2, 1, 2);
78 gtk_box_pack_start(GTK_BOX(content_area
), table
, FALSE
, FALSE
, 0);
80 g_signal_connect(root_
, "response", G_CALLBACK(OnResponseThunk
), this);
81 gtk_widget_grab_focus(username_entry_
);
82 gtk_widget_show_all(GTK_WIDGET(root_
));
85 void ShellLoginDialog::PlatformCleanUp() {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
89 void ShellLoginDialog::PlatformRequestCancelled() {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
93 void ShellLoginDialog::OnResponse(GtkWidget
* sender
, int response_id
) {
94 switch (response_id
) {
97 UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(username_entry_
))),
98 UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(password_entry_
))));
100 case GTK_RESPONSE_CANCEL
:
101 case GTK_RESPONSE_DELETE_EVENT
:
108 gtk_widget_destroy(root_
);
111 } // namespace content