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

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: Fixed 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 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 }
116
117 bool RequestPinView::IsLocked() {
118 return callback_.is_null();
119 }
120
121 void RequestPinView::SetCallback(const RequestPinCallback& callback) {
122 DCHECK(callback_.is_null());
123 callback_ = callback;
124 }
125
126 void RequestPinView::SetDialogParameters(
127 RequestPinView::RequestPinCodeType code_type,
128 RequestPinView::RequestPinErrorType error_type,
129 int attempts_left,
130 bool accept_input) {
131 SetErrorMessage(error_type, attempts_left);
132 SetAcceptInput(accept_input);
133
134 switch (code_type) {
135 case RequestPinCodeType::PIN:
136 code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PIN);
137 break;
138 case RequestPinCodeType::PUK:
139 code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PUK);
140 break;
141 case RequestPinCodeType::UNCHANGED:
142 break;
143 }
144
145 UpdateHeaderText();
146 }
147
148 void RequestPinView::SetExtensionName(const std::string& extension_name) {
149 window_title_ = base::ASCIIToUTF16(extension_name);
150 UpdateHeaderText();
151 }
152
153 void RequestPinView::UpdateHeaderText() {
154 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER;
155 base::string16 label_text =
156 l10n_util::GetStringFUTF16(label_text_id, window_title_, code_type_);
157 header_label_->SetText(label_text);
158 header_label_->SizeToPreferredSize();
159 }
160
161 void RequestPinView::Init() {
162 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
163 SetLayoutManager(layout);
164
165 int column_view_set_id = 0;
166 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
167
168 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
169 views::GridLayout::USE_PREF, 0, 0);
170 layout->StartRow(0, column_view_set_id);
171
172 // Infomation label.
173 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER;
174 base::string16 label_text = l10n_util::GetStringUTF16(label_text_id);
175 header_label_ = new views::Label(label_text);
176 header_label_->SetEnabled(true);
177 layout->AddView(header_label_);
178
179 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
180
181 column_view_set_id++;
182 column_set = layout->AddColumnSet(column_view_set_id);
183 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 100,
184 views::GridLayout::USE_PREF, 0, 0);
185
186 // Textfield to enter the PIN/PUK.
187 layout->StartRow(0, column_view_set_id);
188 textfield_ = new PassphraseTextfield();
189 textfield_->set_controller(this);
190 textfield_->SetEnabled(true);
191 layout->AddView(textfield_);
192
193 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
194
195 column_view_set_id++;
196 column_set = layout->AddColumnSet(column_view_set_id);
197 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
198 views::GridLayout::USE_PREF, 0, 0);
199
200 // Error label.
201 layout->StartRow(0, column_view_set_id);
202 error_label_ = new views::Label();
203 error_label_->SetVisible(false);
204 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
205 layout->AddView(error_label_);
206 }
207
208 void RequestPinView::SetAcceptInput(bool accept_input) {
209 if (accept_input) {
210 textfield_->SetEnabled(true);
211 textfield_->SetBackgroundColor(SK_ColorWHITE);
212 textfield_->RequestFocus();
213 } else {
214 textfield_->SetEnabled(false);
215 textfield_->SetBackgroundColor(SK_ColorGRAY);
216 }
217 }
218
219 void RequestPinView::SetErrorMessage(RequestPinErrorType error_type,
220 int attempts_left) {
221 base::string16 error_message;
222 switch (error_type) {
223 case RequestPinErrorType::INVALID_PIN:
224 error_message =
225 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PIN_ERROR);
226 break;
227 case RequestPinErrorType::INVALID_PUK:
228 error_message =
229 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PUK_ERROR);
230 break;
231 case RequestPinErrorType::MAX_ATTEMPTS_EXCEEDED:
232 error_message = l10n_util::GetStringUTF16(
233 IDS_REQUEST_PIN_DIALOG_MAX_ATTEMPTS_EXCEEDED_ERROR);
234 break;
235 case RequestPinErrorType::UNKNOWN_ERROR:
236 error_message =
237 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_UNKNOWN_ERROR);
238 break;
239 case RequestPinErrorType::NONE:
240 if (attempts_left < 0) {
241 error_label_->SetVisible(false);
242 return;
243 }
244 break;
245 }
246
247 if (attempts_left >= 0) {
248 error_message.append(l10n_util::GetStringFUTF16(
249 IDS_REQUEST_PIN_DIALOG_ATTEMPTS_LEFT,
250 base::ASCIIToUTF16(std::to_string(attempts_left))));
251 }
252
253 error_label_->SetVisible(true);
254 error_label_->SetText(error_message);
255 error_label_->SetTooltipText(error_message);
256 error_label_->SetEnabledColor(SK_ColorRED);
257 error_label_->SizeToPreferredSize();
258 }
259
260 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698