| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "chrome/browser/login_prompt.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | |
| 9 #include "app/l10n_util.h" | |
| 10 #include "app/gtk_signal.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "chrome/browser/browser_thread.h" | |
| 13 #include "chrome/browser/gtk/constrained_window_gtk.h" | |
| 14 #include "chrome/browser/gtk/gtk_util.h" | |
| 15 #include "chrome/browser/login_model.h" | |
| 16 #include "chrome/browser/password_manager/password_manager.h" | |
| 17 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | |
| 18 #include "chrome/browser/tab_contents/navigation_controller.h" | |
| 19 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 20 #include "chrome/browser/tab_contents/tab_contents_delegate.h" | |
| 21 #include "chrome/browser/tab_contents/tab_contents_view_gtk.h" | |
| 22 #include "chrome/browser/tab_contents/tab_util.h" | |
| 23 #include "chrome/common/notification_service.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "net/url_request/url_request.h" | |
| 26 | |
| 27 using webkit_glue::PasswordForm; | |
| 28 | |
| 29 // ---------------------------------------------------------------------------- | |
| 30 // LoginHandlerGtk | |
| 31 | |
| 32 // This class simply forwards the authentication from the LoginView (on | |
| 33 // the UI thread) to the net::URLRequest (on the I/O thread). | |
| 34 // This class uses ref counting to ensure that it lives until all InvokeLaters | |
| 35 // have been called. | |
| 36 class LoginHandlerGtk : public LoginHandler, | |
| 37 public ConstrainedWindowGtkDelegate { | |
| 38 public: | |
| 39 LoginHandlerGtk(net::AuthChallengeInfo* auth_info, net::URLRequest* request) | |
| 40 : LoginHandler(auth_info, request), | |
| 41 username_entry_(NULL), | |
| 42 password_entry_(NULL), | |
| 43 ok_(NULL) { | |
| 44 } | |
| 45 | |
| 46 virtual ~LoginHandlerGtk() { | |
| 47 root_.Destroy(); | |
| 48 } | |
| 49 | |
| 50 // LoginModelObserver implementation. | |
| 51 virtual void OnAutofillDataAvailable(const std::wstring& username, | |
| 52 const std::wstring& password) { | |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 54 | |
| 55 // NOTE: Would be nice to use gtk_entry_get_text_length, but it is fairly | |
| 56 // new and not always in our GTK version. | |
| 57 if (strlen(gtk_entry_get_text(GTK_ENTRY(username_entry_))) == 0) { | |
| 58 gtk_entry_set_text(GTK_ENTRY(username_entry_), | |
| 59 WideToUTF8(username).c_str()); | |
| 60 gtk_entry_set_text(GTK_ENTRY(password_entry_), | |
| 61 WideToUTF8(password).c_str()); | |
| 62 gtk_editable_select_region(GTK_EDITABLE(username_entry_), 0, -1); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 // LoginHandler: | |
| 67 virtual void BuildViewForPasswordManager(PasswordManager* manager, | |
| 68 std::wstring explanation) { | |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 70 | |
| 71 root_.Own(gtk_vbox_new(FALSE, gtk_util::kContentAreaBorder)); | |
| 72 GtkWidget* label = gtk_label_new(WideToUTF8(explanation).c_str()); | |
| 73 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
| 74 gtk_box_pack_start(GTK_BOX(root_.get()), label, FALSE, FALSE, 0); | |
| 75 | |
| 76 username_entry_ = gtk_entry_new(); | |
| 77 gtk_entry_set_activates_default(GTK_ENTRY(username_entry_), TRUE); | |
| 78 | |
| 79 password_entry_ = gtk_entry_new(); | |
| 80 gtk_entry_set_activates_default(GTK_ENTRY(password_entry_), TRUE); | |
| 81 gtk_entry_set_visibility(GTK_ENTRY(password_entry_), FALSE); | |
| 82 | |
| 83 GtkWidget* table = gtk_util::CreateLabeledControlsGroup(NULL, | |
| 84 l10n_util::GetStringUTF8(IDS_LOGIN_DIALOG_USERNAME_FIELD).c_str(), | |
| 85 username_entry_, | |
| 86 l10n_util::GetStringUTF8(IDS_LOGIN_DIALOG_PASSWORD_FIELD).c_str(), | |
| 87 password_entry_, | |
| 88 NULL); | |
| 89 gtk_box_pack_start(GTK_BOX(root_.get()), table, FALSE, FALSE, 0); | |
| 90 | |
| 91 GtkWidget* hbox = gtk_hbox_new(FALSE, 12); | |
| 92 gtk_box_pack_start(GTK_BOX(root_.get()), hbox, FALSE, FALSE, 0); | |
| 93 | |
| 94 ok_ = gtk_button_new_from_stock(GTK_STOCK_OK); | |
| 95 gtk_button_set_label( | |
| 96 GTK_BUTTON(ok_), | |
| 97 l10n_util::GetStringUTF8(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL).c_str()); | |
| 98 g_signal_connect(ok_, "clicked", G_CALLBACK(OnOKClickedThunk), this); | |
| 99 gtk_box_pack_end(GTK_BOX(hbox), ok_, FALSE, FALSE, 0); | |
| 100 | |
| 101 GtkWidget* cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
| 102 g_signal_connect(cancel, "clicked", G_CALLBACK(OnCancelClickedThunk), this); | |
| 103 gtk_box_pack_end(GTK_BOX(hbox), cancel, FALSE, FALSE, 0); | |
| 104 | |
| 105 g_signal_connect(root_.get(), "hierarchy-changed", | |
| 106 G_CALLBACK(OnPromptHierarchyChangedThunk), this); | |
| 107 | |
| 108 SetModel(manager); | |
| 109 | |
| 110 // Scary thread safety note: This can potentially be called *after* SetAuth | |
| 111 // or CancelAuth (say, if the request was cancelled before the UI thread got | |
| 112 // control). However, that's OK since any UI interaction in those functions | |
| 113 // will occur via an InvokeLater on the UI thread, which is guaranteed | |
| 114 // to happen after this is called (since this was InvokeLater'd first). | |
| 115 SetDialog(GetTabContentsForLogin()->CreateConstrainedDialog(this)); | |
| 116 | |
| 117 NotifyAuthNeeded(); | |
| 118 } | |
| 119 | |
| 120 // Overridden from ConstrainedWindowGtkDelegate: | |
| 121 virtual GtkWidget* GetWidgetRoot() { | |
| 122 return root_.get(); | |
| 123 } | |
| 124 | |
| 125 virtual void DeleteDelegate() { | |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 127 | |
| 128 // The constrained window is going to delete itself; clear our pointer. | |
| 129 SetDialog(NULL); | |
| 130 SetModel(NULL); | |
| 131 | |
| 132 ReleaseSoon(); | |
| 133 } | |
| 134 | |
| 135 private: | |
| 136 friend class LoginPrompt; | |
| 137 | |
| 138 CHROMEGTK_CALLBACK_0(LoginHandlerGtk, void, OnOKClicked); | |
| 139 CHROMEGTK_CALLBACK_0(LoginHandlerGtk, void, OnCancelClicked); | |
| 140 CHROMEGTK_CALLBACK_1(LoginHandlerGtk, void, OnPromptHierarchyChanged, | |
| 141 GtkWidget*); | |
| 142 | |
| 143 // The GtkWidgets that form our visual hierarchy: | |
| 144 // The root container we pass to our parent. | |
| 145 OwnedWidgetGtk root_; | |
| 146 | |
| 147 // GtkEntry widgets that the user types into. | |
| 148 GtkWidget* username_entry_; | |
| 149 GtkWidget* password_entry_; | |
| 150 GtkWidget* ok_; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(LoginHandlerGtk); | |
| 153 }; | |
| 154 | |
| 155 void LoginHandlerGtk::OnOKClicked(GtkWidget* sender) { | |
| 156 SetAuth( | |
| 157 UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(username_entry_))), | |
| 158 UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(password_entry_)))); | |
| 159 } | |
| 160 | |
| 161 void LoginHandlerGtk::OnCancelClicked(GtkWidget* sender) { | |
| 162 CancelAuth(); | |
| 163 } | |
| 164 | |
| 165 void LoginHandlerGtk::OnPromptHierarchyChanged(GtkWidget* sender, | |
| 166 GtkWidget* previous_toplevel) { | |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 168 | |
| 169 if (!GTK_WIDGET_TOPLEVEL(gtk_widget_get_toplevel(ok_))) | |
| 170 return; | |
| 171 | |
| 172 // Now that we have attached ourself to the window, we can make our OK | |
| 173 // button the default action and mess with the focus. | |
| 174 GTK_WIDGET_SET_FLAGS(ok_, GTK_CAN_DEFAULT); | |
| 175 gtk_widget_grab_default(ok_); | |
| 176 | |
| 177 TabContents* contents = GetTabContentsForLogin(); | |
| 178 | |
| 179 // The user may have focused another tab. In this case do not grab focus | |
| 180 // until this tab is refocused. | |
| 181 if ((!contents->delegate() || | |
| 182 contents->delegate()->ShouldFocusConstrainedWindow()) && | |
| 183 gtk_util::IsWidgetAncestryVisible(username_entry_)) { | |
| 184 gtk_widget_grab_focus(username_entry_); | |
| 185 } else { | |
| 186 // TODO(estade): this define should not need to be here because this class | |
| 187 // should not be used on linux/views. | |
| 188 #if defined(TOOLKIT_GTK) | |
| 189 static_cast<TabContentsViewGtk*>(contents->view())-> | |
| 190 SetFocusedWidget(username_entry_); | |
| 191 #endif | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 // static | |
| 196 LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info, | |
| 197 net::URLRequest* request) { | |
| 198 return new LoginHandlerGtk(auth_info, request); | |
| 199 } | |
| OLD | NEW |