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/options/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 const std::string& dialog_type, | |
34 const base::string16& error_message, | |
35 const bool accept_input, | |
36 const UserInputCallback& callback) | |
37 : callback_(callback), weak_ptr_factory_(this) { | |
emaxx
2016/07/08 02:00:14
It's a bit unsafe to keep all data members uniniti
| |
38 Init(); | |
39 SetExtensionName(extension_name); | |
40 SetDialogType(dialog_type); | |
41 SetErrorMessage(error_message); | |
42 SetAcceptInput(accept_input); | |
43 } | |
44 | |
45 RequestPinView::~RequestPinView() {} | |
emaxx
2016/07/08 02:00:14
Looks like this can be omitted.
igorcov
2016/07/08 11:40:53
Compiler says it can't.
| |
46 | |
47 void RequestPinView::ContentsChanged(views::Textfield* sender, | |
48 const base::string16& new_contents) { | |
49 GetDialogClientView()->UpdateDialogButtons(); | |
50 } | |
51 | |
52 bool RequestPinView::Cancel() { | |
53 waiting_for_request_ = false; | |
54 // This is null when the dialog is closed by the service with window->Close() | |
55 // function, or when the user input has been sent to extension and while | |
56 // waiting for validation the user closes the dialog. | |
57 if (!callback_.is_null()) { | |
58 callback_.Run(base::string16()); | |
59 callback_.Reset(); | |
60 } | |
61 | |
62 return true; | |
63 } | |
64 | |
65 bool RequestPinView::Accept() { | |
66 if (callback_.is_null()) { | |
emaxx
2016/07/08 02:00:14
Shouldn't this be a DCHECK?
igorcov
2016/07/08 11:40:53
Done.
| |
67 LOG(ERROR) << "Something broke the flow, the dialog will be closed"; | |
emaxx
2016/07/08 02:00:14
The message looks quite informal.
| |
68 return true; | |
69 } | |
70 | |
71 if (!accept_input_) { | |
72 callback_.Run(base::string16()); | |
73 callback_.Reset(); | |
74 waiting_for_request_ = false; | |
75 return true; | |
76 } | |
77 | |
78 error_label_->SetVisible(true); | |
79 error_label_->SetText( | |
80 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PROCESSING)); | |
81 error_label_->SetEnabledColor(SK_ColorGRAY); | |
82 error_label_->SizeToPreferredSize(); | |
83 passphrase_textfield_->SetEnabled(false); | |
84 passphrase_textfield_->SetBackgroundColor(SK_ColorGRAY); | |
85 GetDialogClientView()->UpdateDialogButtons(); | |
86 | |
87 // Set the flag specifying that Chrome is waiting for a notification request | |
88 // from extension now. | |
89 waiting_for_request_ = true; | |
90 callback_.Run(passphrase_textfield_->text()); | |
91 return false; | |
92 } | |
93 | |
94 base::string16 RequestPinView::GetWindowTitle() const { | |
95 return window_title_; | |
96 } | |
97 | |
98 ui::ModalType RequestPinView::GetModalType() const { | |
99 return ui::MODAL_TYPE_SYSTEM; | |
100 } | |
101 | |
102 views::View* RequestPinView::GetInitiallyFocusedView() { | |
103 return passphrase_textfield_; | |
104 } | |
105 | |
106 bool RequestPinView::IsDialogButtonEnabled(ui::DialogButton button) const { | |
107 switch (button) { | |
108 case ui::DialogButton::DIALOG_BUTTON_CANCEL: | |
109 return true; | |
110 case ui::DialogButton::DIALOG_BUTTON_OK: | |
111 // No more input accepted, user is just notified. | |
112 if (!accept_input_) { | |
113 return true; | |
114 } | |
115 | |
116 // The text field is disabled, the button should be disabled too. | |
117 if (!passphrase_textfield_->enabled()) { | |
118 return false; | |
119 } | |
120 | |
121 return passphrase_textfield_->text().size() > 0; | |
122 case ui::DialogButton::DIALOG_BUTTON_NONE: | |
123 return true; | |
124 } | |
125 } | |
126 | |
127 void RequestPinView::SetExtensionName(const std::string& extension_name) { | |
128 window_title_ = base::ASCIIToUTF16(extension_name); | |
129 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; | |
130 base::string16 label_text = l10n_util::GetStringFUTF16( | |
131 label_text_id, base::ASCIIToUTF16(extension_name)); | |
132 header_label_->SetText(label_text); | |
133 header_label_->SizeToPreferredSize(); | |
134 } | |
135 | |
136 void RequestPinView::SetDialogType(const std::string& dialog_type) { | |
137 dialog_type_label_->SetText(base::ASCIIToUTF16(dialog_type)); | |
138 dialog_type_label_->SizeToPreferredSize(); | |
139 } | |
140 | |
141 void RequestPinView::SetErrorMessage(const base::string16& error_message) { | |
142 if (error_message.empty()) { | |
143 error_label_->SetVisible(false); | |
144 } else { | |
145 error_label_->SetVisible(true); | |
146 error_label_->SetText(error_message); | |
147 error_label_->SetEnabledColor(SK_ColorRED); | |
148 error_label_->SizeToPreferredSize(); | |
149 } | |
150 } | |
151 | |
152 void RequestPinView::SetAcceptInput(bool accept_input) { | |
153 accept_input_ = accept_input; | |
154 if (accept_input_) { | |
155 passphrase_textfield_->SetEnabled(true); | |
156 passphrase_textfield_->SetBackgroundColor(SK_ColorWHITE); | |
157 passphrase_textfield_->RequestFocus(); | |
158 } else { | |
159 passphrase_textfield_->SetEnabled(false); | |
160 passphrase_textfield_->SetBackgroundColor(SK_ColorGRAY); | |
161 } | |
162 } | |
163 | |
164 void RequestPinView::SetCallback(const UserInputCallback& callback) { | |
165 if (waiting_for_request_) { | |
166 callback_.Reset(); | |
emaxx
2016/07/08 02:00:14
In which scenario is it fine to just drop the call
igorcov
2016/07/08 11:40:53
It's the case when user submitted some input and t
| |
167 } else { | |
168 // Make sure the old callback was used already before setting the new one. | |
169 DCHECK(callback_.is_null()); | |
170 } | |
171 | |
172 callback_ = callback; | |
173 } | |
174 | |
175 void RequestPinView::Init() { | |
176 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
177 SetLayoutManager(layout); | |
178 | |
179 const int kTextfieldWidth = 25; | |
180 int column_view_set_id = 0; | |
181 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id); | |
182 | |
183 // Infomation label. | |
184 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
185 views::GridLayout::USE_PREF, 0, 0); | |
186 layout->StartRow(0, column_view_set_id); | |
187 | |
188 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; | |
189 base::string16 label_text = l10n_util::GetStringUTF16(label_text_id); | |
190 header_label_ = new views::Label(label_text); | |
191 header_label_->SetEnabled(true); | |
192 layout->AddView(header_label_); | |
193 | |
194 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
195 | |
196 column_view_set_id++; | |
197 column_set = layout->AddColumnSet(column_view_set_id); | |
198 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
199 views::GridLayout::USE_PREF, 0, 0); | |
200 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
201 views::GridLayout::USE_PREF, 0, 0); | |
202 | |
203 layout->StartRow(0, column_view_set_id); | |
204 dialog_type_label_ = new views::Label(); | |
205 dialog_type_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
206 dialog_type_label_->SetEnabled(true); | |
207 layout->AddView(dialog_type_label_); | |
208 | |
209 passphrase_textfield_ = new PassphraseTextfield(); | |
210 passphrase_textfield_->set_controller(this); | |
211 passphrase_textfield_->SetEnabled(true); | |
212 passphrase_textfield_->SetAccessibleName(dialog_type_label_->text()); | |
213 passphrase_textfield_->set_default_width_in_chars(kTextfieldWidth); | |
214 layout->AddView(passphrase_textfield_); | |
215 | |
216 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
217 | |
218 column_view_set_id++; | |
219 column_set = layout->AddColumnSet(column_view_set_id); | |
220 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
221 views::GridLayout::USE_PREF, 0, 0); | |
222 | |
223 // Create an error label. | |
224 layout->StartRow(0, column_view_set_id); | |
225 error_label_ = new views::Label(); | |
226 error_label_->SetVisible(false); | |
227 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
228 layout->AddView(error_label_); | |
229 } | |
230 | |
231 } // namespace chromeos | |
OLD | NEW |