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