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