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.
5 #include "chrome/browser/ui/crypto_module_password_dialog.h"
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"
20 // TODO(mattm): change into a constrained dialog.
21 class CryptoModulePasswordDialog
{
23 CryptoModulePasswordDialog(
24 const std::string
& slot_name
,
26 chrome::CryptoModulePasswordReason reason
,
27 const std::string
& hostname
,
28 gfx::NativeWindow parent
,
29 const chrome::CryptoModulePasswordCallback
& callback
);
31 ~CryptoModulePasswordDialog() {}
36 CHROMEGTK_CALLBACK_1(CryptoModulePasswordDialog
, void, OnResponse
, int);
37 CHROMEGTK_CALLBACK_0(CryptoModulePasswordDialog
, void, OnWindowDestroy
);
39 chrome::CryptoModulePasswordCallback callback_
;
42 GtkWidget
* password_entry_
;
44 DISALLOW_COPY_AND_ASSIGN(CryptoModulePasswordDialog
);
47 CryptoModulePasswordDialog::CryptoModulePasswordDialog(
48 const std::string
& slot_name
,
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(),
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(
64 l10n_util::GetStringUTF8(
65 IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL
).c_str(),
68 gtk_widget_set_can_default(ok_button
, TRUE
);
69 gtk_dialog_set_default_response(GTK_DIALOG(dialog_
), GTK_RESPONSE_ACCEPT
);
71 // Select an appropriate text for the reason.
73 const base::string16
& hostname16
= base::UTF8ToUTF16(hostname
);
74 const base::string16
& slot16
= base::UTF8ToUTF16(slot_name
);
76 case chrome::kCryptoModulePasswordKeygen
:
77 text
= l10n_util::GetStringFUTF8(
78 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_KEYGEN
, slot16
, hostname16
);
80 case chrome::kCryptoModulePasswordCertEnrollment
:
81 text
= l10n_util::GetStringFUTF8(
82 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_ENROLLMENT
,
86 case chrome::kCryptoModulePasswordClientAuth
:
87 text
= l10n_util::GetStringFUTF8(
88 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CLIENT_AUTH
, slot16
, hostname16
);
90 case chrome::kCryptoModulePasswordListCerts
:
91 text
= l10n_util::GetStringFUTF8(
92 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_LIST_CERTS
, slot16
);
94 case chrome::kCryptoModulePasswordCertImport
:
95 text
= l10n_util::GetStringFUTF8(
96 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_IMPORT
, slot16
);
98 case chrome::kCryptoModulePasswordCertExport
:
99 text
= l10n_util::GetStringFUTF8(
100 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_EXPORT
, slot16
);
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
,
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
);
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()),
120 gtk_box_pack_start(GTK_BOX(password_box
), password_entry_
,
123 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_
)->vbox
), password_box
,
126 g_signal_connect(dialog_
, "response",
127 G_CALLBACK(OnResponseThunk
), this);
128 g_signal_connect(dialog_
, "destroy",
129 G_CALLBACK(OnWindowDestroyThunk
), this);
132 void CryptoModulePasswordDialog::Show() {
133 gtk_util::ShowDialog(dialog_
);
136 void CryptoModulePasswordDialog::OnResponse(GtkWidget
* dialog
,
138 if (response_id
== GTK_RESPONSE_ACCEPT
)
139 callback_
.Run(gtk_entry_get_text(GTK_ENTRY(password_entry_
)));
141 callback_
.Run(std::string());
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_
);
150 void CryptoModulePasswordDialog::OnWindowDestroy(GtkWidget
* widget
) {
158 void ShowCryptoModulePasswordDialog(
159 const std::string
& slot_name
,
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();
169 } // namespace chrome