Chromium Code Reviews| 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/ui/pk11_password_dialog.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | |
| 9 #include "app/gtk_signal.h" | |
| 10 #include "app/l10n_util.h" | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/crypto/pk11_blocking_password_delegate.h" | |
| 13 #include "base/task.h" | |
| 14 #include "base/utf_string_conversions.h" | |
| 15 #include "base/waitable_event.h" | |
| 16 #include "chrome/browser/browser_thread.h" | |
| 17 #include "chrome/browser/gtk/gtk_util.h" | |
| 18 #include "googleurl/src/gurl.h" | |
| 19 #include "grit/generated_resources.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class PK11BlockingDialogDelegate : public base::PK11BlockingPasswordDelegate { | |
| 24 public: | |
| 25 PK11BlockingDialogDelegate(browser::PK11PasswordReason reason, | |
| 26 const std::string& host) | |
| 27 : event_(false, false), | |
| 28 reason_(reason), | |
| 29 host_(host), | |
| 30 password_(), | |
| 31 cancelled_(false) { | |
| 32 } | |
| 33 | |
| 34 // base::PK11BlockingDialogDelegate implementation. | |
| 35 virtual std::string RequestPassword(const std::string& slot_name, bool retry, | |
| 36 bool* cancelled) { | |
| 37 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 38 DCHECK(!event_.IsSignaled()); | |
| 39 event_.Reset(); | |
| 40 if (BrowserThread::PostTask( | |
| 41 BrowserThread::UI, FROM_HERE, | |
| 42 NewRunnableMethod(this, &PK11BlockingDialogDelegate::ShowDialog, | |
| 43 slot_name, retry))) { | |
| 44 event_.Wait(); | |
| 45 } | |
| 46 *cancelled = cancelled_; | |
| 47 return password_; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 void ShowDialog(const std::string& slot_name, bool retry) { | |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 53 ShowPK11PasswordDialog( | |
| 54 slot_name, retry, reason_, host_, | |
| 55 NewCallback(this, &PK11BlockingDialogDelegate::GotPassword)); | |
| 56 } | |
| 57 void GotPassword(const char* password) { | |
| 58 if (password) | |
| 59 password_ = password; | |
| 60 else | |
| 61 cancelled_ = true; | |
| 62 event_.Signal(); | |
| 63 } | |
| 64 base::WaitableEvent event_; | |
| 65 browser::PK11PasswordReason reason_; | |
| 66 std::string host_; | |
| 67 std::string password_; | |
| 68 bool cancelled_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(PK11BlockingDialogDelegate); | |
| 71 }; | |
| 72 | |
| 73 // TODO(mattm): change into a constrained dialog. | |
| 74 class PK11PasswordDialog { | |
|
Evan Stade
2010/12/10 04:17:04
it's confusing that this class is defined here whe
mattm
2011/01/12 01:22:07
I thought this was a somewhat common pattern to ha
| |
| 75 public: | |
| 76 PK11PasswordDialog(const std::string& slot_name, | |
| 77 bool retry, | |
| 78 browser::PK11PasswordReason reason, | |
| 79 const std::string& host, | |
| 80 browser::PK11PasswordCallback* callback); | |
| 81 | |
| 82 void Show(); | |
| 83 | |
| 84 private: | |
| 85 CHROMEGTK_CALLBACK_1(PK11PasswordDialog, void, OnResponse, int); | |
| 86 CHROMEGTK_CALLBACK_0(PK11PasswordDialog, void, OnWindowDestroy); | |
| 87 | |
| 88 scoped_ptr<browser::PK11PasswordCallback> callback_; | |
| 89 | |
| 90 GtkWidget* dialog_; | |
| 91 GtkWidget* password_entry_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(PK11PasswordDialog); | |
| 94 }; | |
| 95 | |
| 96 PK11PasswordDialog::PK11PasswordDialog(const std::string& slot_name, | |
| 97 bool retry, | |
| 98 browser::PK11PasswordReason reason, | |
| 99 const std::string& host, | |
| 100 browser::PK11PasswordCallback* callback) | |
| 101 : callback_(callback) { | |
| 102 dialog_ = gtk_dialog_new_with_buttons( | |
| 103 l10n_util::GetStringUTF8(IDS_PK11_AUTH_DIALOG_TITLE).c_str(), | |
| 104 NULL, | |
| 105 GTK_DIALOG_NO_SEPARATOR, | |
| 106 NULL); // Populate the buttons later, for control over the OK button. | |
| 107 gtk_dialog_add_button(GTK_DIALOG(dialog_), | |
| 108 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT); | |
| 109 GtkWidget* ok_button = gtk_util::AddButtonToDialog( | |
| 110 dialog_, | |
| 111 l10n_util::GetStringUTF8(IDS_PK11_AUTH_DIALOG_OK_BUTTON_LABEL).c_str(), | |
| 112 GTK_STOCK_OK, | |
| 113 GTK_RESPONSE_ACCEPT); | |
| 114 GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT); | |
| 115 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); | |
| 116 | |
| 117 // Select an appropriate text for the reason. | |
| 118 std::string text; | |
| 119 const string16& host16 = UTF8ToUTF16(host); | |
| 120 const string16& slot16 = UTF8ToUTF16(slot_name); | |
| 121 switch (reason) { | |
| 122 case browser::kPK11PasswordKeygen: | |
| 123 text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_KEYGEN, | |
| 124 slot16, host16); | |
| 125 break; | |
| 126 case browser::kPK11PasswordCertEnrollment: | |
| 127 text = l10n_util::GetStringFUTF8( | |
| 128 IDS_PK11_AUTH_DIALOG_TEXT_CERT_ENROLLMENT, slot16, host16); | |
| 129 break; | |
| 130 case browser::kPK11PasswordClientAuth: | |
| 131 text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_CLIENT_AUTH, | |
| 132 slot16, host16); | |
| 133 break; | |
| 134 case browser::kPK11PasswordCertImport: | |
| 135 text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_CERT_IMPORT, | |
| 136 slot16); | |
| 137 break; | |
| 138 case browser::kPK11PasswordCertExport: | |
| 139 text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_CERT_EXPORT, | |
| 140 slot16); | |
| 141 break; | |
| 142 default: | |
| 143 NOTREACHED(); | |
| 144 } | |
| 145 GtkWidget* label = gtk_label_new(text.c_str()); | |
| 146 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
| 147 gtk_util::LeftAlignMisc(label); | |
| 148 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), label, | |
| 149 FALSE, FALSE, 0); | |
| 150 | |
| 151 password_entry_ = gtk_entry_new(); | |
| 152 gtk_entry_set_activates_default(GTK_ENTRY(password_entry_), TRUE); | |
| 153 gtk_entry_set_visibility(GTK_ENTRY(password_entry_), FALSE); | |
| 154 | |
| 155 GtkWidget* password_box = gtk_hbox_new(FALSE, gtk_util::kLabelSpacing); | |
| 156 gtk_box_pack_start(GTK_BOX(password_box), | |
| 157 gtk_label_new(l10n_util::GetStringUTF8( | |
| 158 IDS_PK11_AUTH_DIALOG_PASSWORD_FIELD).c_str()), | |
| 159 FALSE, FALSE, 0); | |
| 160 gtk_box_pack_start(GTK_BOX(password_box), password_entry_, | |
| 161 TRUE, TRUE, 0); | |
| 162 | |
| 163 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), password_box, | |
| 164 FALSE, FALSE, 0); | |
| 165 | |
| 166 g_signal_connect(dialog_, "response", | |
| 167 G_CALLBACK(OnResponseThunk), this); | |
| 168 g_signal_connect(dialog_, "destroy", | |
| 169 G_CALLBACK(OnWindowDestroyThunk), this); | |
| 170 } | |
| 171 | |
| 172 void PK11PasswordDialog::Show() { | |
| 173 gtk_util::ShowDialog(dialog_); | |
| 174 } | |
| 175 | |
| 176 void PK11PasswordDialog::OnResponse(GtkWidget* dialog, int response_id) { | |
| 177 if (response_id == GTK_RESPONSE_ACCEPT) { | |
|
Evan Stade
2010/12/10 04:17:04
no {}
mattm
2011/01/12 01:22:07
Done.
| |
| 178 callback_->Run(gtk_entry_get_text(GTK_ENTRY(password_entry_))); | |
| 179 } else { | |
| 180 callback_->Run(static_cast<const char*>(NULL)); | |
| 181 } | |
| 182 gtk_widget_destroy(GTK_WIDGET(dialog_)); | |
| 183 } | |
| 184 | |
| 185 void PK11PasswordDialog::OnWindowDestroy(GtkWidget* widget) { | |
| 186 delete this; | |
| 187 } | |
| 188 | |
| 189 } // namespace | |
| 190 | |
| 191 // Every post-task we do blocks, so there's no need to ref-count. | |
| 192 DISABLE_RUNNABLE_METHOD_REFCOUNT(PK11BlockingDialogDelegate); | |
| 193 | |
| 194 namespace browser { | |
| 195 | |
| 196 void ShowPK11PasswordDialog(const std::string& slot_name, | |
| 197 bool retry, | |
| 198 PK11PasswordReason reason, | |
| 199 const std::string& host, | |
| 200 PK11PasswordCallback* callback) { | |
| 201 (new PK11PasswordDialog(slot_name, retry, reason, host, callback))->Show(); | |
| 202 } | |
| 203 | |
| 204 base::PK11BlockingPasswordDelegate* NewPK11BlockingDialogDelegate( | |
| 205 PK11PasswordReason reason, | |
| 206 const std::string& host) { | |
| 207 return new PK11BlockingDialogDelegate(reason, host); | |
| 208 } | |
| 209 | |
| 210 } // namespace browser | |
| OLD | NEW |