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_login_dialog.h"
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"
22 void ShellLoginDialog::PlatformCreateDialog(const base::string16& message) {
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
25 int render_process_id;
27 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame(
28 &render_process_id, &render_frame_id)) {
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);
38 gfx::NativeWindow parent_window =
39 web_contents->GetView()->GetTopLevelNativeWindow();
41 root_ = gtk_message_dialog_new(parent_window,
44 GTK_BUTTONS_OK_CANCEL,
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);
52 username_entry_ = gtk_entry_new();
53 gtk_entry_set_activates_default(GTK_ENTRY(username_entry_), TRUE);
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);
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);
63 GtkWidget* username_label = gtk_label_new("Username:");
64 gtk_misc_set_alignment(GTK_MISC(username_label), 0, 0.5);
66 gtk_table_attach(GTK_TABLE(table), username_label, 0, 1, 0, 1, GTK_FILL,
68 gtk_table_attach_defaults(GTK_TABLE(table), username_entry_, 1, 2, 0, 1);
70 GtkWidget* password_label = gtk_label_new("Password:");
71 gtk_misc_set_alignment(GTK_MISC(password_label), 0, 0.5);
73 gtk_table_attach(GTK_TABLE(table), password_label, 0, 1, 1, 2, GTK_FILL,
75 gtk_table_attach_defaults(GTK_TABLE(table), password_entry_, 1, 2, 1, 2);
77 gtk_box_pack_start(GTK_BOX(content_area), table, FALSE, FALSE, 0);
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_));
84 void ShellLoginDialog::PlatformCleanUp() {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
88 void ShellLoginDialog::PlatformRequestCancelled() {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 void ShellLoginDialog::OnResponse(GtkWidget* sender, int response_id) {
93 switch (response_id) {
96 base::UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(username_entry_))),
97 base::UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(password_entry_))));
99 case GTK_RESPONSE_CANCEL:
100 case GTK_RESPONSE_DELETE_EVENT:
107 gtk_widget_destroy(root_);
110 } // namespace content