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