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/views/crypto_module_password_dialog_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/grit/generated_resources.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/events/event.h"
11 #include "ui/views/controls/label.h"
12 #include "ui/views/controls/textfield/textfield.h"
13 #include "ui/views/layout/grid_layout.h"
14 #include "ui/views/layout/layout_constants.h"
15 #include "ui/views/widget/widget.h"
19 ////////////////////////////////////////////////////////////////////////////////
20 // CryptoModulePasswordDialogView, public:
22 CryptoModulePasswordDialogView::CryptoModulePasswordDialogView(
23 const std::string
& slot_name
,
24 CryptoModulePasswordReason reason
,
25 const std::string
& hostname
,
26 const CryptoModulePasswordCallback
& callback
)
27 : callback_(callback
) {
28 Init(hostname
, slot_name
, reason
);
31 CryptoModulePasswordDialogView::~CryptoModulePasswordDialogView() {
34 ////////////////////////////////////////////////////////////////////////////////
35 // CryptoModulePasswordDialogView, private:
37 views::View
* CryptoModulePasswordDialogView::GetInitiallyFocusedView() {
38 return password_entry_
;
41 ui::ModalType
CryptoModulePasswordDialogView::GetModalType() const {
42 return ui::MODAL_TYPE_WINDOW
;
45 base::string16
CryptoModulePasswordDialogView::GetWindowTitle() const {
46 return l10n_util::GetStringUTF16(IDS_CRYPTO_MODULE_AUTH_DIALOG_TITLE
);
49 base::string16
CryptoModulePasswordDialogView::GetDialogButtonLabel(
50 ui::DialogButton button
) const {
51 return l10n_util::GetStringUTF16(button
== ui::DIALOG_BUTTON_OK
?
52 IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL
: IDS_CANCEL
);
55 bool CryptoModulePasswordDialogView::Cancel() {
56 callback_
.Run(std::string());
57 const base::string16 empty
;
58 password_entry_
->SetText(empty
);
62 bool CryptoModulePasswordDialogView::Accept() {
63 callback_
.Run(base::UTF16ToUTF8(password_entry_
->text()));
64 const base::string16 empty
;
65 password_entry_
->SetText(empty
);
69 void CryptoModulePasswordDialogView::ContentsChanged(
70 views::Textfield
* sender
,
71 const base::string16
& new_contents
) {
74 bool CryptoModulePasswordDialogView::HandleKeyEvent(
75 views::Textfield
* sender
,
76 const ui::KeyEvent
& keystroke
) {
80 void CryptoModulePasswordDialogView::Init(const std::string
& hostname
,
81 const std::string
& slot_name
,
82 CryptoModulePasswordReason reason
) {
83 // Select an appropriate text for the reason.
85 const base::string16
& hostname16
= base::UTF8ToUTF16(hostname
);
86 const base::string16
& slot16
= base::UTF8ToUTF16(slot_name
);
88 case chrome::kCryptoModulePasswordKeygen
:
89 text
= l10n_util::GetStringFUTF8(
90 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_KEYGEN
, slot16
, hostname16
);
92 case chrome::kCryptoModulePasswordCertEnrollment
:
93 text
= l10n_util::GetStringFUTF8(
94 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_ENROLLMENT
,
98 case chrome::kCryptoModulePasswordClientAuth
:
99 text
= l10n_util::GetStringFUTF8(
100 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CLIENT_AUTH
, slot16
, hostname16
);
102 case chrome::kCryptoModulePasswordListCerts
:
103 text
= l10n_util::GetStringFUTF8(
104 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_LIST_CERTS
, slot16
);
106 case chrome::kCryptoModulePasswordCertImport
:
107 text
= l10n_util::GetStringFUTF8(
108 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_IMPORT
, slot16
);
110 case chrome::kCryptoModulePasswordCertExport
:
111 text
= l10n_util::GetStringFUTF8(
112 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_EXPORT
, slot16
);
117 reason_label_
= new views::Label(base::UTF8ToUTF16(text
));
118 reason_label_
->SetMultiLine(true);
120 password_label_
= new views::Label(l10n_util::GetStringUTF16(
121 IDS_CRYPTO_MODULE_AUTH_DIALOG_PASSWORD_FIELD
));
123 password_entry_
= new views::Textfield();
124 password_entry_
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
125 password_entry_
->set_controller(this);
127 views::GridLayout
* layout
= views::GridLayout::CreatePanel(this);
128 SetLayoutManager(layout
);
130 views::ColumnSet
* reason_column_set
= layout
->AddColumnSet(0);
131 reason_column_set
->AddColumn(
132 views::GridLayout::LEADING
, views::GridLayout::LEADING
, 1,
133 views::GridLayout::USE_PREF
, 0, 0);
135 views::ColumnSet
* column_set
= layout
->AddColumnSet(1);
136 column_set
->AddColumn(views::GridLayout::LEADING
,
137 views::GridLayout::LEADING
, 0,
138 views::GridLayout::USE_PREF
, 0, 0);
139 column_set
->AddPaddingColumn(
140 0, views::kUnrelatedControlLargeHorizontalSpacing
);
141 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::FILL
, 1,
142 views::GridLayout::USE_PREF
, 0, 0);
144 layout
->StartRow(0, 0);
145 layout
->AddView(reason_label_
);
146 layout
->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing
);
148 layout
->StartRow(0, 1);
149 layout
->AddView(password_label_
);
150 layout
->AddView(password_entry_
);
153 void ShowCryptoModulePasswordDialog(
154 const std::string
& slot_name
,
156 CryptoModulePasswordReason reason
,
157 const std::string
& hostname
,
158 gfx::NativeWindow parent
,
159 const CryptoModulePasswordCallback
& callback
) {
160 CryptoModulePasswordDialogView
* dialog
=
161 new CryptoModulePasswordDialogView(slot_name
, reason
, hostname
, callback
);
162 views::DialogDelegate::CreateDialogWidget(dialog
, NULL
, parent
)->Show();
165 } // namespace chrome