Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / shell / browser / shell_login_dialog_gtk.cc
blobb08b449ad912c56ca09959afdd544f80e23886ab
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"
7 #include <gtk/gtk.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_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"
20 namespace content {
22 void ShellLoginDialog::PlatformCreateDialog(const string16& message) {
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
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();
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);
39 gfx::NativeWindow parent_window =
40 web_contents->GetView()->GetTopLevelNativeWindow();
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.");
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,
68 GTK_FILL, 0, 0);
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,
75 GTK_FILL, 0, 0);
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) {
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();
108 gtk_widget_destroy(root_);
111 } // namespace content