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