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

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

Powered by Google App Engine
This is Rietveld 408576698