Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / chromeos / options / wimax_config_view.cc
blob2c1c50ba630fe08e081826f3ae509a9de3121eb5
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/options/wimax_config_view.h"
7 #include "base/string_util.h"
8 #include "base/stringprintf.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/chromeos/cros/cros_library.h"
11 #include "chrome/browser/chromeos/cros/network_library.h"
12 #include "chrome/browser/chromeos/cros/onc_constants.h"
13 #include "chrome/browser/chromeos/enrollment_dialog_view.h"
14 #include "chrome/browser/chromeos/login/user_manager.h"
15 #include "chrome/browser/chromeos/login/wizard_controller.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "grit/chromium_strings.h"
18 #include "grit/generated_resources.h"
19 #include "grit/locale_settings.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/events/event.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/views/controls/button/checkbox.h"
25 #include "ui/views/controls/button/image_button.h"
26 #include "ui/views/controls/label.h"
27 #include "ui/views/controls/textfield/textfield.h"
28 #include "ui/views/layout/grid_layout.h"
29 #include "ui/views/layout/layout_constants.h"
30 #include "ui/views/widget/widget.h"
31 #include "ui/views/window/dialog_client_view.h"
33 namespace chromeos {
35 WimaxConfigView::WimaxConfigView(NetworkConfigView* parent, WimaxNetwork* wimax)
36 : ChildNetworkConfigView(parent, wimax),
37 identity_label_(NULL),
38 identity_textfield_(NULL),
39 save_credentials_checkbox_(NULL),
40 share_network_checkbox_(NULL),
41 shared_network_label_(NULL),
42 passphrase_label_(NULL),
43 passphrase_textfield_(NULL),
44 passphrase_visible_button_(NULL),
45 error_label_(NULL) {
46 Init(wimax);
49 WimaxConfigView::~WimaxConfigView() {
52 views::View* WimaxConfigView::GetInitiallyFocusedView() {
53 if (identity_textfield_ && identity_textfield_->enabled())
54 return identity_textfield_;
55 if (passphrase_textfield_ && passphrase_textfield_->enabled())
56 return passphrase_textfield_;
57 return NULL;
60 bool WimaxConfigView::CanLogin() {
61 // In OOBE it may be valid to log in with no credentials (crbug.com/137776).
62 if (!chromeos::WizardController::IsOobeCompleted())
63 return true;
65 // TODO(benchan): Update this with the correct minimum length (don't just
66 // check if empty).
67 // If the network requires a passphrase, make sure it is the right length.
68 return passphrase_textfield_ && !passphrase_textfield_->text().empty();
71 void WimaxConfigView::UpdateDialogButtons() {
72 parent_->GetDialogClientView()->UpdateDialogButtons();
75 void WimaxConfigView::UpdateErrorLabel() {
76 std::string error_msg;
77 if (!service_path_.empty()) {
78 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
79 const WimaxNetwork* wimax = cros->FindWimaxNetworkByPath(service_path_);
80 if (wimax && wimax->failed()) {
81 bool passphrase_empty = wimax->eap_passphrase().empty();
82 switch (wimax->error()) {
83 case ERROR_BAD_PASSPHRASE:
84 if (!passphrase_empty) {
85 error_msg = l10n_util::GetStringUTF8(
86 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_BAD_PASSPHRASE);
88 break;
89 case ERROR_BAD_WEPKEY:
90 if (!passphrase_empty) {
91 error_msg = l10n_util::GetStringUTF8(
92 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_BAD_WEPKEY);
94 break;
95 default:
96 error_msg = wimax->GetErrorString();
97 break;
101 if (!error_msg.empty()) {
102 error_label_->SetText(UTF8ToUTF16(error_msg));
103 error_label_->SetVisible(true);
104 } else {
105 error_label_->SetVisible(false);
109 void WimaxConfigView::ContentsChanged(views::Textfield* sender,
110 const string16& new_contents) {
111 UpdateDialogButtons();
114 bool WimaxConfigView::HandleKeyEvent(views::Textfield* sender,
115 const ui::KeyEvent& key_event) {
116 if (sender == passphrase_textfield_ &&
117 key_event.key_code() == ui::VKEY_RETURN) {
118 parent_->GetDialogClientView()->AcceptWindow();
120 return false;
123 void WimaxConfigView::ButtonPressed(views::Button* sender,
124 const ui::Event& event) {
125 if (sender == passphrase_visible_button_) {
126 if (passphrase_textfield_) {
127 passphrase_textfield_->SetObscured(!passphrase_textfield_->IsObscured());
128 passphrase_visible_button_->SetToggled(
129 !passphrase_textfield_->IsObscured());
131 } else {
132 NOTREACHED();
136 bool WimaxConfigView::Login() {
137 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
138 WimaxNetwork* wimax = cros->FindWimaxNetworkByPath(service_path_);
139 if (!wimax) {
140 // Shill no longer knows about this wimax network (edge case).
141 // TODO(stevenjb): Add a notification (chromium-os13225).
142 LOG(WARNING) << "Wimax network: " << service_path_ << " no longer exists.";
143 return true;
145 wimax->SetEAPIdentity(GetEapIdentity());
146 wimax->SetEAPPassphrase(GetEapPassphrase());
147 wimax->SetSaveCredentials(GetSaveCredentials());
148 bool share_default = (wimax->profile_type() != PROFILE_USER);
149 bool share = GetShareNetwork(share_default);
150 wimax->SetEnrollmentDelegate(
151 CreateEnrollmentDelegate(GetWidget()->GetNativeWindow(),
152 wimax->name(),
153 ProfileManager::GetLastUsedProfile()));
154 cros->ConnectToWimaxNetwork(wimax, share);
155 // Connection failures are responsible for updating the UI, including
156 // reopening dialogs.
157 return true; // dialog will be closed
160 std::string WimaxConfigView::GetEapIdentity() const {
161 DCHECK(identity_textfield_);
162 return UTF16ToUTF8(identity_textfield_->text());
165 std::string WimaxConfigView::GetEapPassphrase() const {
166 return passphrase_textfield_ ? UTF16ToUTF8(passphrase_textfield_->text()) :
167 std::string();
170 bool WimaxConfigView::GetSaveCredentials() const {
171 return save_credentials_checkbox_ ? save_credentials_checkbox_->checked() :
172 false;
175 bool WimaxConfigView::GetShareNetwork(bool share_default) const {
176 return share_network_checkbox_ ? share_network_checkbox_->checked() :
177 share_default;
180 void WimaxConfigView::Cancel() {
183 void WimaxConfigView::Init(WimaxNetwork* wimax) {
184 DCHECK(wimax);
185 WifiConfigView::ParseWiFiEAPUIProperty(
186 &save_credentials_ui_data_, wimax, onc::eap::kSaveCredentials);
187 WifiConfigView::ParseWiFiEAPUIProperty(
188 &identity_ui_data_, wimax, onc::eap::kIdentity);
189 WifiConfigView::ParseWiFiUIProperty(
190 &passphrase_ui_data_, wimax, onc::wifi::kPassphrase);
192 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
193 SetLayoutManager(layout);
195 const int column_view_set_id = 0;
196 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
197 const int kPasswordVisibleWidth = 20;
198 // Label
199 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
200 views::GridLayout::USE_PREF, 0, 0);
201 column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
202 // Textfield, combobox.
203 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
204 views::GridLayout::USE_PREF, 0,
205 ChildNetworkConfigView::kInputFieldMinWidth);
206 column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
207 // Password visible button / policy indicator.
208 column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1,
209 views::GridLayout::USE_PREF, 0, kPasswordVisibleWidth);
211 // Title
212 layout->StartRow(0, column_view_set_id);
213 views::Label* title = new views::Label(l10n_util::GetStringUTF16(
214 IDS_OPTIONS_SETTINGS_JOIN_WIMAX_NETWORKS));
215 title->SetFont(title->font().DeriveFont(1, gfx::Font::BOLD));
216 layout->AddView(title, 5, 1);
217 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
219 // Netowrk name
220 layout->StartRow(0, column_view_set_id);
221 layout->AddView(new views::Label(l10n_util::GetStringUTF16(
222 IDS_OPTIONS_SETTINGS_INTERNET_TAB_NETWORK)));
223 views::Label* label = new views::Label(UTF8ToUTF16(wimax->name()));
224 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
225 layout->AddView(label);
226 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
228 // Identity
229 layout->StartRow(0, column_view_set_id);
230 identity_label_ = new views::Label(l10n_util::GetStringUTF16(
231 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_IDENTITY));
232 layout->AddView(identity_label_);
233 identity_textfield_ = new views::Textfield(
234 views::Textfield::STYLE_DEFAULT);
235 identity_textfield_->SetController(this);
236 const std::string& eap_identity = wimax->eap_identity();
237 identity_textfield_->SetText(UTF8ToUTF16(eap_identity));
238 identity_textfield_->SetEnabled(identity_ui_data_.editable());
239 layout->AddView(identity_textfield_);
240 layout->AddView(new ControlledSettingIndicatorView(identity_ui_data_));
241 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
243 // Passphrase input
244 layout->StartRow(0, column_view_set_id);
245 int label_text_id = IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE;
246 passphrase_label_ = new views::Label(
247 l10n_util::GetStringUTF16(label_text_id));
248 layout->AddView(passphrase_label_);
249 passphrase_textfield_ = new views::Textfield(
250 views::Textfield::STYLE_OBSCURED);
251 passphrase_textfield_->SetController(this);
252 passphrase_label_->SetEnabled(true);
253 passphrase_textfield_->SetEnabled(passphrase_ui_data_.editable());
254 passphrase_textfield_->SetAccessibleName(l10n_util::GetStringUTF16(
255 label_text_id));
256 layout->AddView(passphrase_textfield_);
258 if (passphrase_ui_data_.managed()) {
259 layout->AddView(new ControlledSettingIndicatorView(passphrase_ui_data_));
260 } else {
261 // Password visible button.
262 passphrase_visible_button_ = new views::ToggleImageButton(this);
263 passphrase_visible_button_->SetTooltipText(
264 l10n_util::GetStringUTF16(
265 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_SHOW));
266 passphrase_visible_button_->SetToggledTooltipText(
267 l10n_util::GetStringUTF16(
268 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_HIDE));
269 passphrase_visible_button_->SetImage(
270 views::ImageButton::STATE_NORMAL,
271 ResourceBundle::GetSharedInstance().
272 GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD));
273 passphrase_visible_button_->SetImage(
274 views::ImageButton::STATE_HOVERED,
275 ResourceBundle::GetSharedInstance().
276 GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD_HOVER));
277 passphrase_visible_button_->SetToggledImage(
278 views::ImageButton::STATE_NORMAL,
279 ResourceBundle::GetSharedInstance().
280 GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD));
281 passphrase_visible_button_->SetToggledImage(
282 views::ImageButton::STATE_HOVERED,
283 ResourceBundle::GetSharedInstance().
284 GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD_HOVER));
285 passphrase_visible_button_->SetImageAlignment(
286 views::ImageButton::ALIGN_CENTER, views::ImageButton::ALIGN_MIDDLE);
287 layout->AddView(passphrase_visible_button_);
290 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
292 // Checkboxes.
294 if (UserManager::Get()->IsUserLoggedIn()) {
295 // Save credentials
296 layout->StartRow(0, column_view_set_id);
297 save_credentials_checkbox_ = new views::Checkbox(
298 l10n_util::GetStringUTF16(
299 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SAVE_CREDENTIALS));
300 save_credentials_checkbox_->SetEnabled(
301 save_credentials_ui_data_.editable());
302 save_credentials_checkbox_->SetChecked(wimax->save_credentials());
303 layout->SkipColumns(1);
304 layout->AddView(save_credentials_checkbox_);
305 layout->AddView(
306 new ControlledSettingIndicatorView(save_credentials_ui_data_));
308 // Share network
309 if (wimax->profile_type() == PROFILE_NONE && wimax->passphrase_required()) {
310 layout->StartRow(0, column_view_set_id);
311 share_network_checkbox_ = new views::Checkbox(
312 l10n_util::GetStringUTF16(
313 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SHARE_NETWORK));
314 share_network_checkbox_->SetEnabled(true);
315 share_network_checkbox_->SetChecked(false); // Default to unshared.
316 layout->SkipColumns(1);
317 layout->AddView(share_network_checkbox_);
320 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
322 // Create an error label.
323 layout->StartRow(0, column_view_set_id);
324 layout->SkipColumns(1);
325 error_label_ = new views::Label();
326 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
327 error_label_->SetEnabledColor(SK_ColorRED);
328 layout->AddView(error_label_);
330 UpdateErrorLabel();
333 void WimaxConfigView::InitFocus() {
334 views::View* view_to_focus = GetInitiallyFocusedView();
335 if (view_to_focus)
336 view_to_focus->RequestFocus();
339 } // namespace chromeos