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. 4 5 #include "content/shell/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_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" 19 20 namespace content { 21 22 void ShellLoginDialog::PlatformCreateDialog(const string16& message) { 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 24 25 int render_process_id; 26 int render_view_id; 27 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderView( 28 &render_process_id, &render_view_id)) { 29 NOTREACHED(); 30 } 31 32 WebContents* web_contents = NULL; 33 RenderViewHost* render_view_host = 34 RenderViewHost::FromID(render_process_id, render_view_id); 35 if (render_view_host) 36 web_contents = WebContents::FromRenderViewHost(render_view_host); 37 DCHECK(web_contents); 38 39 gfx::NativeWindow parent_window = 40 web_contents->GetView()->GetTopLevelNativeWindow(); 41 42 root_ = gtk_message_dialog_new(parent_window, 43 GTK_DIALOG_MODAL, 44 GTK_MESSAGE_INFO, 45 GTK_BUTTONS_OK_CANCEL, 46 "Please log in."); 47 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); 52 53 username_entry_ = gtk_entry_new(); 54 gtk_entry_set_activates_default(GTK_ENTRY(username_entry_), TRUE); 55 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); 59 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); 63 64 GtkWidget* username_label = gtk_label_new("Username:"); 65 gtk_misc_set_alignment(GTK_MISC(username_label), 0, 0.5); 66 67 gtk_table_attach(GTK_TABLE(table), username_label, 0, 1, 0, 1, GTK_FILL, 68 GTK_FILL, 0, 0); 69 gtk_table_attach_defaults(GTK_TABLE(table), username_entry_, 1, 2, 0, 1); 70 71 GtkWidget* password_label = gtk_label_new("Password:"); 72 gtk_misc_set_alignment(GTK_MISC(password_label), 0, 0.5); 73 74 gtk_table_attach(GTK_TABLE(table), password_label, 0, 1, 1, 2, GTK_FILL, 75 GTK_FILL, 0, 0); 76 gtk_table_attach_defaults(GTK_TABLE(table), password_entry_, 1, 2, 1, 2); 77 78 gtk_box_pack_start(GTK_BOX(content_area), table, FALSE, FALSE, 0); 79 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_)); 83 } 84 85 void ShellLoginDialog::PlatformCleanUp() { 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 87 } 88 89 void ShellLoginDialog::PlatformRequestCancelled() { 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 91 } 92 93 void ShellLoginDialog::OnResponse(GtkWidget* sender, int response_id) { 94 switch (response_id) { 95 case GTK_RESPONSE_OK: 96 UserAcceptedAuth( 97 UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(username_entry_))), 98 UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(password_entry_)))); 99 break; 100 case GTK_RESPONSE_CANCEL: 101 case GTK_RESPONSE_DELETE_EVENT: 102 UserCancelledAuth(); 103 break; 104 default: 105 NOTREACHED(); 106 } 107 108 gtk_widget_destroy(root_); 109 } 110 111 } // namespace content 112