OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/chromeos/ui/request_pin_view.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/chromeos/options/passphrase_textfield.h" |
| 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "chrome/grit/theme_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/events/event.h" |
| 18 #include "ui/views/controls/label.h" |
| 19 #include "ui/views/controls/textfield/textfield.h" |
| 20 #include "ui/views/layout/grid_layout.h" |
| 21 #include "ui/views/layout/layout_constants.h" |
| 22 #include "ui/views/widget/widget.h" |
| 23 #include "ui/views/window/dialog_client_view.h" |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 RequestPinView::RequestPinView(const std::string& extension_name, |
| 28 RequestPinView::RequestPinCodeType code_type, |
| 29 int attempts_left, |
| 30 const RequestPinCallback& callback, |
| 31 Delegate* delegate) |
| 32 : callback_(callback), delegate_(delegate), weak_ptr_factory_(this) { |
| 33 DCHECK(code_type != RequestPinCodeType::UNCHANGED); |
| 34 DCHECK(delegate); |
| 35 Init(); |
| 36 SetExtensionName(extension_name); |
| 37 const bool accept_input = (attempts_left != 0); |
| 38 SetDialogParameters(code_type, RequestPinErrorType::NONE, attempts_left, |
| 39 accept_input); |
| 40 } |
| 41 |
| 42 // When the parent window is closed while the dialog is active, this object is |
| 43 // destroyed without triggering Accept or Cancel. If the callback_ wasn't called |
| 44 // it needs to send the response. |
| 45 RequestPinView::~RequestPinView() { |
| 46 if (!callback_.is_null()) { |
| 47 callback_.Run(base::string16()); |
| 48 callback_.Reset(); |
| 49 } |
| 50 |
| 51 delegate_->OnPinDialogClosed(); |
| 52 } |
| 53 |
| 54 void RequestPinView::ContentsChanged(views::Textfield* sender, |
| 55 const base::string16& new_contents) { |
| 56 GetDialogClientView()->UpdateDialogButtons(); |
| 57 } |
| 58 |
| 59 bool RequestPinView::Cancel() { |
| 60 // Destructor will be called after this which notifies the delegate. |
| 61 return true; |
| 62 } |
| 63 |
| 64 bool RequestPinView::Accept() { |
| 65 DCHECK(!callback_.is_null()); |
| 66 |
| 67 if (!textfield_->enabled()) { |
| 68 return true; |
| 69 } |
| 70 |
| 71 error_label_->SetVisible(true); |
| 72 error_label_->SetText( |
| 73 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PROCESSING)); |
| 74 error_label_->SetTooltipText(error_label_->text()); |
| 75 error_label_->SetEnabledColor(SK_ColorGRAY); |
| 76 error_label_->SizeToPreferredSize(); |
| 77 // The |textfield_| and OK button become disabled, but the user still can |
| 78 // close the dialog. |
| 79 SetAcceptInput(false); |
| 80 callback_.Run(textfield_->text()); |
| 81 callback_.Reset(); |
| 82 GetDialogClientView()->UpdateDialogButtons(); |
| 83 |
| 84 return false; |
| 85 } |
| 86 |
| 87 base::string16 RequestPinView::GetWindowTitle() const { |
| 88 return window_title_; |
| 89 } |
| 90 |
| 91 views::View* RequestPinView::GetInitiallyFocusedView() { |
| 92 return textfield_; |
| 93 } |
| 94 |
| 95 bool RequestPinView::IsDialogButtonEnabled(ui::DialogButton button) const { |
| 96 switch (button) { |
| 97 case ui::DialogButton::DIALOG_BUTTON_CANCEL: |
| 98 return true; |
| 99 case ui::DialogButton::DIALOG_BUTTON_OK: |
| 100 if (callback_.is_null()) { |
| 101 return false; |
| 102 } |
| 103 // Not locked but the |textfield_| is not enabled. It's just a |
| 104 // notification to the user and [OK] button can be used to close the |
| 105 // dialog. |
| 106 if (!textfield_->enabled()) { |
| 107 return true; |
| 108 } |
| 109 return textfield_->text().size() > 0; |
| 110 case ui::DialogButton::DIALOG_BUTTON_NONE: |
| 111 return true; |
| 112 } |
| 113 |
| 114 NOTREACHED(); |
| 115 return true; |
| 116 } |
| 117 |
| 118 bool RequestPinView::IsLocked() { |
| 119 return callback_.is_null(); |
| 120 } |
| 121 |
| 122 void RequestPinView::SetCallback(const RequestPinCallback& callback) { |
| 123 DCHECK(callback_.is_null()); |
| 124 callback_ = callback; |
| 125 } |
| 126 |
| 127 void RequestPinView::SetDialogParameters( |
| 128 RequestPinView::RequestPinCodeType code_type, |
| 129 RequestPinView::RequestPinErrorType error_type, |
| 130 int attempts_left, |
| 131 bool accept_input) { |
| 132 SetErrorMessage(error_type, attempts_left); |
| 133 SetAcceptInput(accept_input); |
| 134 |
| 135 switch (code_type) { |
| 136 case RequestPinCodeType::PIN: |
| 137 code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PIN); |
| 138 break; |
| 139 case RequestPinCodeType::PUK: |
| 140 code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PUK); |
| 141 break; |
| 142 case RequestPinCodeType::UNCHANGED: |
| 143 break; |
| 144 } |
| 145 |
| 146 UpdateHeaderText(); |
| 147 } |
| 148 |
| 149 void RequestPinView::SetExtensionName(const std::string& extension_name) { |
| 150 window_title_ = base::ASCIIToUTF16(extension_name); |
| 151 UpdateHeaderText(); |
| 152 } |
| 153 |
| 154 void RequestPinView::UpdateHeaderText() { |
| 155 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; |
| 156 base::string16 label_text = |
| 157 l10n_util::GetStringFUTF16(label_text_id, window_title_, code_type_); |
| 158 header_label_->SetText(label_text); |
| 159 header_label_->SizeToPreferredSize(); |
| 160 } |
| 161 |
| 162 void RequestPinView::Init() { |
| 163 views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
| 164 SetLayoutManager(layout); |
| 165 |
| 166 int column_view_set_id = 0; |
| 167 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id); |
| 168 |
| 169 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
| 170 views::GridLayout::USE_PREF, 0, 0); |
| 171 layout->StartRow(0, column_view_set_id); |
| 172 |
| 173 // Infomation label. |
| 174 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; |
| 175 base::string16 label_text = l10n_util::GetStringUTF16(label_text_id); |
| 176 header_label_ = new views::Label(label_text); |
| 177 header_label_->SetEnabled(true); |
| 178 layout->AddView(header_label_); |
| 179 |
| 180 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 181 |
| 182 column_view_set_id++; |
| 183 column_set = layout->AddColumnSet(column_view_set_id); |
| 184 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 100, |
| 185 views::GridLayout::USE_PREF, 0, 0); |
| 186 |
| 187 // Textfield to enter the PIN/PUK. |
| 188 layout->StartRow(0, column_view_set_id); |
| 189 textfield_ = new PassphraseTextfield(); |
| 190 textfield_->set_controller(this); |
| 191 textfield_->SetEnabled(true); |
| 192 layout->AddView(textfield_); |
| 193 |
| 194 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 195 |
| 196 column_view_set_id++; |
| 197 column_set = layout->AddColumnSet(column_view_set_id); |
| 198 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
| 199 views::GridLayout::USE_PREF, 0, 0); |
| 200 |
| 201 // Error label. |
| 202 layout->StartRow(0, column_view_set_id); |
| 203 error_label_ = new views::Label(); |
| 204 error_label_->SetVisible(false); |
| 205 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 206 layout->AddView(error_label_); |
| 207 } |
| 208 |
| 209 void RequestPinView::SetAcceptInput(bool accept_input) { |
| 210 if (accept_input) { |
| 211 textfield_->SetEnabled(true); |
| 212 textfield_->SetBackgroundColor(SK_ColorWHITE); |
| 213 textfield_->RequestFocus(); |
| 214 } else { |
| 215 textfield_->SetEnabled(false); |
| 216 textfield_->SetBackgroundColor(SK_ColorGRAY); |
| 217 } |
| 218 } |
| 219 |
| 220 void RequestPinView::SetErrorMessage(RequestPinErrorType error_type, |
| 221 int attempts_left) { |
| 222 base::string16 error_message; |
| 223 switch (error_type) { |
| 224 case RequestPinErrorType::INVALID_PIN: |
| 225 error_message = |
| 226 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PIN_ERROR); |
| 227 break; |
| 228 case RequestPinErrorType::INVALID_PUK: |
| 229 error_message = |
| 230 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PUK_ERROR); |
| 231 break; |
| 232 case RequestPinErrorType::MAX_ATTEMPTS_EXCEEDED: |
| 233 error_message = l10n_util::GetStringUTF16( |
| 234 IDS_REQUEST_PIN_DIALOG_MAX_ATTEMPTS_EXCEEDED_ERROR); |
| 235 break; |
| 236 case RequestPinErrorType::UNKNOWN_ERROR: |
| 237 error_message = |
| 238 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_UNKNOWN_ERROR); |
| 239 break; |
| 240 case RequestPinErrorType::NONE: |
| 241 if (attempts_left < 0) { |
| 242 error_label_->SetVisible(false); |
| 243 return; |
| 244 } |
| 245 break; |
| 246 } |
| 247 |
| 248 if (attempts_left >= 0) { |
| 249 error_message.append(l10n_util::GetStringFUTF16( |
| 250 IDS_REQUEST_PIN_DIALOG_ATTEMPTS_LEFT, |
| 251 base::ASCIIToUTF16(std::to_string(attempts_left)))); |
| 252 } |
| 253 |
| 254 error_label_->SetVisible(true); |
| 255 error_label_->SetText(error_message); |
| 256 error_label_->SetTooltipText(error_message); |
| 257 error_label_->SetEnabledColor(SK_ColorRED); |
| 258 error_label_->SizeToPreferredSize(); |
| 259 } |
| 260 |
| 261 } // namespace chromeos |
OLD | NEW |