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