Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: chrome/browser/chromeos/ui/request_pin_view.cc

Issue 2094333002: Implementation for chrome.certificateProvider.requestPin/stopPinRequest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implemented review comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
emaxx 2016/09/06 15:02:11 nit: The current style guide says not to use "(c)"
igorcov 2016/09/07 09:12:29 Done.
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 : 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 != 0);
41 SetDialogParameters(code_type, RequestPinErrorType::NONE, attempts_left,
42 accept_input);
43 }
44
45 RequestPinView::~RequestPinView() {
46 if (!callback_.is_null()) {
47 callback_.Run(base::string16());
48 callback_.Reset();
49 }
50
51 if (!locked_callback_.is_null()) {
52 locked_callback_.Run(true);
53 locked_callback_.Reset();
54 }
55 }
56
57 void RequestPinView::ContentsChanged(views::Textfield* sender,
58 const base::string16& new_contents) {
59 GetDialogClientView()->UpdateDialogButtons();
60 }
61
62 bool RequestPinView::Cancel() {
63 // This is null when the dialog is closed by the service with window->Close()
64 // function.
65 if (!callback_.is_null()) {
66 callback_.Run(base::string16());
67 callback_.Reset();
68 } else {
69 locked_callback_.Run(true);
70 locked_callback_.Reset();
71 }
72
73 return true;
74 }
75
76 bool RequestPinView::Accept() {
77 DCHECK(!callback_.is_null() && locked_callback_.is_null());
78
79 if (!textfield_->enabled()) {
80 callback_.Run(base::string16());
81 callback_.Reset();
82 return true;
83 }
84
85 error_label_->SetVisible(true);
86 error_label_->SetText(
87 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PROCESSING));
88 error_label_->SetTooltipText(error_label_->text());
89 error_label_->SetEnabledColor(SK_ColorGRAY);
90 error_label_->SizeToPreferredSize();
91 SetAcceptInput(false);
92 callback_.Run(textfield_->text());
93 callback_.Reset();
94 GetDialogClientView()->UpdateDialogButtons();
95
96 return false;
97 }
98
99 base::string16 RequestPinView::GetWindowTitle() const {
100 return window_title_;
101 }
102
103 ui::ModalType RequestPinView::GetModalType() const {
104 return ui::MODAL_TYPE_NONE;
105 }
106
107 views::View* RequestPinView::GetInitiallyFocusedView() {
108 return textfield_;
109 }
110
111 bool RequestPinView::IsDialogButtonEnabled(ui::DialogButton button) const {
112 switch (button) {
113 case ui::DialogButton::DIALOG_BUTTON_CANCEL:
114 return true;
115 case ui::DialogButton::DIALOG_BUTTON_OK:
116 if (!locked_callback_.is_null()) {
117 return false;
118 }
119 // Not locked but the |textfield_| is not enabled. It's just a
120 // notification to the user and [OK] button can be used to close the
121 // dialog.
122 if (!textfield_->enabled()) {
123 return true;
124 }
125 return textfield_->text().size() > 0;
126 case ui::DialogButton::DIALOG_BUTTON_NONE:
127 return true;
128 }
129 }
130
131 bool RequestPinView::IsLocked() {
132 return !locked_callback_.is_null();
133 }
134
135 void RequestPinView::SetCallback(const RequestPinCallback& callback) {
136 if (!locked_callback_.is_null()) {
137 locked_callback_.Run(false);
138 locked_callback_.Reset();
139 }
140
141 DCHECK(callback_.is_null());
142 callback_ = callback;
143 }
144
145 void RequestPinView::SetLockedCallback(
146 const CloseLockedViewCallback& locked_callback) {
147 DCHECK(locked_callback_.is_null());
148 locked_callback_ = locked_callback;
149 }
150
151 void RequestPinView::SetDialogParameters(RequestPinCodeType code_type,
152 RequestPinErrorType error_type,
153 const int attempts_left,
154 const bool accept_input) {
155 DCHECK(locked_callback_.is_null());
156 SetErrorMessage(error_type, attempts_left);
157 SetAcceptInput(accept_input);
158
159 switch (code_type) {
160 case RequestPinCodeType::PIN:
161 code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PIN);
162 break;
163 case RequestPinCodeType::PUK:
164 code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PUK);
165 break;
166 case RequestPinCodeType::UNCHANGED:
167 break;
168 }
169
170 UpdateHeaderText();
171 }
172
173 void RequestPinView::SetExtensionName(const std::string& extension_name) {
174 window_title_ = base::ASCIIToUTF16(extension_name);
175 UpdateHeaderText();
176 }
177
178 void RequestPinView::UpdateHeaderText() {
179 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER;
180 base::string16 label_text =
181 l10n_util::GetStringFUTF16(label_text_id, window_title_, code_type_);
182 header_label_->SetText(label_text);
183 header_label_->SizeToPreferredSize();
184 }
185
186 void RequestPinView::Init() {
187 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
188 SetLayoutManager(layout);
189
190 int column_view_set_id = 0;
191 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
192
193 // Infomation label.
emaxx 2016/09/06 15:02:11 nit: If you have this comment, then please also ad
igorcov 2016/09/07 09:12:29 Done.
194 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
195 views::GridLayout::USE_PREF, 0, 0);
196 layout->StartRow(0, column_view_set_id);
197
198 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER;
199 base::string16 label_text = l10n_util::GetStringUTF16(label_text_id);
200 header_label_ = new views::Label(label_text);
201 header_label_->SetEnabled(true);
202 layout->AddView(header_label_);
203
204 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
205
206 column_view_set_id++;
207 column_set = layout->AddColumnSet(column_view_set_id);
208 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 100,
209 views::GridLayout::USE_PREF, 0, 0);
210
211 layout->StartRow(0, column_view_set_id);
212 textfield_ = new PassphraseTextfield();
213 textfield_->set_controller(this);
214 textfield_->SetEnabled(true);
215 layout->AddView(textfield_);
216
217 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
218
219 column_view_set_id++;
220 column_set = layout->AddColumnSet(column_view_set_id);
221 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
222 views::GridLayout::USE_PREF, 0, 0);
223
224 // Create an error label.
225 layout->StartRow(0, column_view_set_id);
226 error_label_ = new views::Label();
227 error_label_->SetVisible(false);
228 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
229 layout->AddView(error_label_);
230 }
231
232 void RequestPinView::SetAcceptInput(bool accept_input) {
233 if (accept_input) {
234 textfield_->SetEnabled(true);
235 textfield_->SetBackgroundColor(SK_ColorWHITE);
236 textfield_->RequestFocus();
237 } else {
238 textfield_->SetEnabled(false);
239 textfield_->SetBackgroundColor(SK_ColorGRAY);
240 }
241 }
242
243 void RequestPinView::SetErrorMessage(RequestPinErrorType error_type,
244 int attempts_left) {
245 base::string16 error_message;
246 switch (error_type) {
247 case RequestPinErrorType::INVALID_PIN:
248 error_message =
249 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PIN_ERROR);
250 break;
251 case RequestPinErrorType::INVALID_PUK:
252 error_message =
253 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PUK_ERROR);
254 break;
255 case RequestPinErrorType::MAX_ATTEMPTS_EXCEEDED:
256 error_message = l10n_util::GetStringUTF16(
257 IDS_REQUEST_PIN_DIALOG_MAX_ATTEMPTS_EXCEEDED_ERROR);
258 break;
259 case RequestPinErrorType::UNKNOWN_ERROR:
260 error_message =
261 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_UNKNOWN_ERROR);
262 break;
263 case RequestPinErrorType::NONE:
264 error_label_->SetVisible(false);
265 return;
266 }
267
268 if (attempts_left >= 0) {
269 error_message.append(l10n_util::GetStringFUTF16(
270 IDS_REQUEST_PIN_DIALOG_ATTEMPTS_LEFT,
271 base::ASCIIToUTF16(std::to_string(attempts_left))));
272 }
273
274 error_label_->SetVisible(true);
275 error_label_->SetText(error_message);
276 error_label_->SetTooltipText(error_message);
277 error_label_->SetEnabledColor(SK_ColorRED);
278 error_label_->SizeToPreferredSize();
279 }
280
281 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698