| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/ui/crypto_module_password_dialog.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "chrome/browser/ui/gtk/gtk_util.h" | |
| 12 #include "grit/generated_resources.h" | |
| 13 #include "ui/base/gtk/gtk_hig_constants.h" | |
| 14 #include "ui/base/gtk/gtk_signal.h" | |
| 15 #include "ui/base/l10n/l10n_util.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // TODO(mattm): change into a constrained dialog. | |
| 21 class CryptoModulePasswordDialog { | |
| 22 public: | |
| 23 CryptoModulePasswordDialog( | |
| 24 const std::string& slot_name, | |
| 25 bool retry, | |
| 26 chrome::CryptoModulePasswordReason reason, | |
| 27 const std::string& hostname, | |
| 28 gfx::NativeWindow parent, | |
| 29 const chrome::CryptoModulePasswordCallback& callback); | |
| 30 | |
| 31 ~CryptoModulePasswordDialog() {} | |
| 32 | |
| 33 void Show(); | |
| 34 | |
| 35 private: | |
| 36 CHROMEGTK_CALLBACK_1(CryptoModulePasswordDialog, void, OnResponse, int); | |
| 37 CHROMEGTK_CALLBACK_0(CryptoModulePasswordDialog, void, OnWindowDestroy); | |
| 38 | |
| 39 chrome::CryptoModulePasswordCallback callback_; | |
| 40 | |
| 41 GtkWidget* dialog_; | |
| 42 GtkWidget* password_entry_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(CryptoModulePasswordDialog); | |
| 45 }; | |
| 46 | |
| 47 CryptoModulePasswordDialog::CryptoModulePasswordDialog( | |
| 48 const std::string& slot_name, | |
| 49 bool retry, | |
| 50 chrome::CryptoModulePasswordReason reason, | |
| 51 const std::string& hostname, | |
| 52 gfx::NativeWindow parent, | |
| 53 const chrome::CryptoModulePasswordCallback& callback) | |
| 54 : callback_(callback) { | |
| 55 dialog_ = gtk_dialog_new_with_buttons( | |
| 56 l10n_util::GetStringUTF8(IDS_CRYPTO_MODULE_AUTH_DIALOG_TITLE).c_str(), | |
| 57 parent, | |
| 58 GTK_DIALOG_NO_SEPARATOR, | |
| 59 NULL); // Populate the buttons later, for control over the OK button. | |
| 60 gtk_dialog_add_button(GTK_DIALOG(dialog_), | |
| 61 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT); | |
| 62 GtkWidget* ok_button = gtk_util::AddButtonToDialog( | |
| 63 dialog_, | |
| 64 l10n_util::GetStringUTF8( | |
| 65 IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL).c_str(), | |
| 66 GTK_STOCK_OK, | |
| 67 GTK_RESPONSE_ACCEPT); | |
| 68 gtk_widget_set_can_default(ok_button, TRUE); | |
| 69 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); | |
| 70 | |
| 71 // Select an appropriate text for the reason. | |
| 72 std::string text; | |
| 73 const base::string16& hostname16 = base::UTF8ToUTF16(hostname); | |
| 74 const base::string16& slot16 = base::UTF8ToUTF16(slot_name); | |
| 75 switch (reason) { | |
| 76 case chrome::kCryptoModulePasswordKeygen: | |
| 77 text = l10n_util::GetStringFUTF8( | |
| 78 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_KEYGEN, slot16, hostname16); | |
| 79 break; | |
| 80 case chrome::kCryptoModulePasswordCertEnrollment: | |
| 81 text = l10n_util::GetStringFUTF8( | |
| 82 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_ENROLLMENT, | |
| 83 slot16, | |
| 84 hostname16); | |
| 85 break; | |
| 86 case chrome::kCryptoModulePasswordClientAuth: | |
| 87 text = l10n_util::GetStringFUTF8( | |
| 88 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CLIENT_AUTH, slot16, hostname16); | |
| 89 break; | |
| 90 case chrome::kCryptoModulePasswordListCerts: | |
| 91 text = l10n_util::GetStringFUTF8( | |
| 92 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_LIST_CERTS, slot16); | |
| 93 break; | |
| 94 case chrome::kCryptoModulePasswordCertImport: | |
| 95 text = l10n_util::GetStringFUTF8( | |
| 96 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_IMPORT, slot16); | |
| 97 break; | |
| 98 case chrome::kCryptoModulePasswordCertExport: | |
| 99 text = l10n_util::GetStringFUTF8( | |
| 100 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_EXPORT, slot16); | |
| 101 break; | |
| 102 default: | |
| 103 NOTREACHED(); | |
| 104 } | |
| 105 GtkWidget* label = gtk_label_new(text.c_str()); | |
| 106 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
| 107 gtk_util::LeftAlignMisc(label); | |
| 108 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), label, | |
| 109 FALSE, FALSE, 0); | |
| 110 | |
| 111 password_entry_ = gtk_entry_new(); | |
| 112 gtk_entry_set_activates_default(GTK_ENTRY(password_entry_), TRUE); | |
| 113 gtk_entry_set_visibility(GTK_ENTRY(password_entry_), FALSE); | |
| 114 | |
| 115 GtkWidget* password_box = gtk_hbox_new(FALSE, ui::kLabelSpacing); | |
| 116 gtk_box_pack_start(GTK_BOX(password_box), | |
| 117 gtk_label_new(l10n_util::GetStringUTF8( | |
| 118 IDS_CRYPTO_MODULE_AUTH_DIALOG_PASSWORD_FIELD).c_str()), | |
| 119 FALSE, FALSE, 0); | |
| 120 gtk_box_pack_start(GTK_BOX(password_box), password_entry_, | |
| 121 TRUE, TRUE, 0); | |
| 122 | |
| 123 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), password_box, | |
| 124 FALSE, FALSE, 0); | |
| 125 | |
| 126 g_signal_connect(dialog_, "response", | |
| 127 G_CALLBACK(OnResponseThunk), this); | |
| 128 g_signal_connect(dialog_, "destroy", | |
| 129 G_CALLBACK(OnWindowDestroyThunk), this); | |
| 130 } | |
| 131 | |
| 132 void CryptoModulePasswordDialog::Show() { | |
| 133 gtk_util::ShowDialog(dialog_); | |
| 134 } | |
| 135 | |
| 136 void CryptoModulePasswordDialog::OnResponse(GtkWidget* dialog, | |
| 137 int response_id) { | |
| 138 if (response_id == GTK_RESPONSE_ACCEPT) | |
| 139 callback_.Run(gtk_entry_get_text(GTK_ENTRY(password_entry_))); | |
| 140 else | |
| 141 callback_.Run(std::string()); | |
| 142 | |
| 143 // This will cause gtk to zero out the buffer. (see | |
| 144 // gtk_entry_buffer_normal_delete_text: | |
| 145 // http://git.gnome.org/browse/gtk+/tree/gtk/gtkentrybuffer.c#n187) | |
| 146 gtk_editable_delete_text(GTK_EDITABLE(password_entry_), 0, -1); | |
| 147 gtk_widget_destroy(dialog_); | |
| 148 } | |
| 149 | |
| 150 void CryptoModulePasswordDialog::OnWindowDestroy(GtkWidget* widget) { | |
| 151 delete this; | |
| 152 } | |
| 153 | |
| 154 } // namespace | |
| 155 | |
| 156 namespace chrome { | |
| 157 | |
| 158 void ShowCryptoModulePasswordDialog( | |
| 159 const std::string& slot_name, | |
| 160 bool retry, | |
| 161 CryptoModulePasswordReason reason, | |
| 162 const std::string& hostname, | |
| 163 gfx::NativeWindow parent, | |
| 164 const CryptoModulePasswordCallback& callback) { | |
| 165 (new CryptoModulePasswordDialog( | |
| 166 slot_name, retry, reason, hostname, parent, callback))->Show(); | |
| 167 } | |
| 168 | |
| 169 } // namespace chrome | |
| OLD | NEW |