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

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: Externalized back the constant Created 4 years, 4 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.
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()) {
stevenjb 2016/08/11 18:12:02 nit: I am pretty sure that you can just use !callb
igorcov 2016/09/06 13:22:02 I can't because the callback_ object is always the
stevenjb 2016/09/08 16:29:31 https://cs.chromium.org/chromium/src/base/callback
igorcov 2016/09/09 15:53:42 I'm not sure what do you mean here. In my code cal
stevenjb 2016/09/12 21:16:39 TL;DR: is_null() seems to be much more common so n
igorcov 2016/09/13 14:19:32 Acknowledged.
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);
stevenjb 2016/08/11 18:12:02 The dialog can still be closed with SetAcceptInput
igorcov 2016/09/09 15:53:42 Done.
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 // Not locked but the |textfield_| is not enabled. It's just a
108 // notification to the user and [OK] button can be used to close the
109 // dialog.
110 if (!textfield_->enabled()) {
111 return true;
112 }
113 return textfield_->text().size() > 0;
114 case ui::DialogButton::DIALOG_BUTTON_NONE:
115 return true;
116 }
117 }
118
119 bool RequestPinView::IsLocked() {
120 return locked_;
121 }
122
123 void RequestPinView::SetCallback(const RequestPinCallback& callback) {
124 // It is acceptable to drop the old |callback_| if the dialog is locked,
125 // meaning the callback is a temporary one pointing to
126 // CertificateProviderService::OnFlowInterrupted. It is suposed to be used
127 // to notify the service if the dialog is closed while it is locked.
128 DCHECK(locked_ || callback_.is_null());
stevenjb 2016/08/11 18:12:02 This is confusing and seems fragile. While it may
igorcov 2016/09/07 09:12:29 Done.
129 callback_ = callback;
130 }
131
132 void RequestPinView::SetDialogParameters(RequestPinCodeType code_type,
133 RequestPinErrorType error_type,
134 std::unique_ptr<int> attempts_left,
135 const bool accept_input) {
136 locked_ = false;
137
138 SetErrorMessage(error_type, std::move(attempts_left));
139 SetAcceptInput(accept_input);
140
141 switch (code_type) {
142 case RequestPinCodeType::PIN:
143 code_type_ = base::ASCIIToUTF16("PIN");
stevenjb 2016/08/11 18:12:02 These need to be localized strings.
igorcov 2016/09/07 09:12:29 Done.
144 break;
145 case RequestPinCodeType::PUK:
146 code_type_ = base::ASCIIToUTF16("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 // Infomation label.
176 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
177 views::GridLayout::USE_PREF, 0, 0);
178 layout->StartRow(0, column_view_set_id);
179
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 layout->StartRow(0, column_view_set_id);
194 textfield_ = new PassphraseTextfield();
195 textfield_->set_controller(this);
196 textfield_->SetEnabled(true);
197 layout->AddView(textfield_);
198
199 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
200
201 column_view_set_id++;
202 column_set = layout->AddColumnSet(column_view_set_id);
203 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
204 views::GridLayout::USE_PREF, 0, 0);
205
206 // Create an error label.
207 layout->StartRow(0, column_view_set_id);
208 error_label_ = new views::Label();
209 error_label_->SetVisible(false);
210 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
211 layout->AddView(error_label_);
212 }
213
214 void RequestPinView::SetAcceptInput(bool accept_input) {
215 if (accept_input) {
216 textfield_->SetEnabled(true);
217 textfield_->SetBackgroundColor(SK_ColorWHITE);
218 textfield_->RequestFocus();
219 } else {
220 textfield_->SetEnabled(false);
221 textfield_->SetBackgroundColor(SK_ColorGRAY);
222 }
223 }
224
225 void RequestPinView::SetErrorMessage(RequestPinErrorType error_type,
226 std::unique_ptr<int> attempts_left) {
227 base::string16 error_message;
228 switch (error_type) {
229 case RequestPinErrorType::INVALID_PIN:
230 error_message =
231 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PIN_ERROR);
232 break;
233 case RequestPinErrorType::INVALID_PUK:
234 error_message =
235 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PUK_ERROR);
236 break;
237 case RequestPinErrorType::MAX_ATTEMPTS_EXCEEDED:
238 error_message = l10n_util::GetStringUTF16(
239 IDS_REQUEST_PIN_DIALOG_MAX_ATTEMPTS_EXCEEDED_ERROR);
240 break;
241 case RequestPinErrorType::UNKNOWN_ERROR:
242 error_message =
243 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_UNKNOWN_ERROR);
244 break;
245 case RequestPinErrorType::NONE:
246 error_label_->SetVisible(false);
247 return;
248 }
249
250 if (attempts_left) {
251 error_message.append(l10n_util::GetStringFUTF16(
252 IDS_REQUEST_PIN_DIALOG_ATTEMPTS_LEFT,
253 base::ASCIIToUTF16(std::to_string(*(attempts_left.get())))));
254 }
255
256 error_label_->SetVisible(true);
257 error_label_->SetText(error_message);
258 error_label_->SetTooltipText(error_message);
259 error_label_->SetEnabledColor(SK_ColorRED);
260 error_label_->SizeToPreferredSize();
261 }
262
263 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698