Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / options / wimax_config_view.cc
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.
4
5 #include "chrome/browser/chromeos/options/wimax_config_view.h"
6
7 #include "base/bind.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/enrollment_dialog_view.h"
12 #include "chrome/browser/chromeos/login/startup_utils.h"
13 #include "chrome/browser/chromeos/net/onc_utils.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "chrome/grit/theme_resources.h"
17 #include "chromeos/login/login_state.h"
18 #include "chromeos/network/network_configuration_handler.h"
19 #include "chromeos/network/network_event_log.h"
20 #include "chromeos/network/network_profile.h"
21 #include "chromeos/network/network_profile_handler.h"
22 #include "chromeos/network/network_state.h"
23 #include "chromeos/network/network_state_handler.h"
24 #include "components/onc/onc_constants.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/chromeos/network/network_connect.h"
29 #include "ui/events/event.h"
30 #include "ui/views/controls/button/checkbox.h"
31 #include "ui/views/controls/button/image_button.h"
32 #include "ui/views/controls/label.h"
33 #include "ui/views/controls/textfield/textfield.h"
34 #include "ui/views/layout/grid_layout.h"
35 #include "ui/views/layout/layout_constants.h"
36 #include "ui/views/widget/widget.h"
37 #include "ui/views/window/dialog_client_view.h"
38
39 namespace chromeos {
40
41 namespace {
42
43 void ShillError(const std::string& function,
44                 const std::string& error_name,
45                 scoped_ptr<base::DictionaryValue> error_data) {
46   NET_LOG_ERROR("Shill Error from WimaxConfigView: " + error_name, function);
47 }
48
49 }  // namespace
50
51 WimaxConfigView::WimaxConfigView(NetworkConfigView* parent,
52                                  const std::string& service_path)
53     : ChildNetworkConfigView(parent, service_path),
54       identity_label_(NULL),
55       identity_textfield_(NULL),
56       save_credentials_checkbox_(NULL),
57       share_network_checkbox_(NULL),
58       shared_network_label_(NULL),
59       passphrase_label_(NULL),
60       passphrase_textfield_(NULL),
61       passphrase_visible_button_(NULL),
62       error_label_(NULL),
63       weak_ptr_factory_(this) {
64   Init();
65 }
66
67 WimaxConfigView::~WimaxConfigView() {
68   RemoveAllChildViews(true);  // Destroy children before models
69 }
70
71 base::string16 WimaxConfigView::GetTitle() const {
72   return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_JOIN_WIMAX_NETWORKS);
73 }
74
75 views::View* WimaxConfigView::GetInitiallyFocusedView() {
76   if (identity_textfield_ && identity_textfield_->enabled())
77     return identity_textfield_;
78   if (passphrase_textfield_ && passphrase_textfield_->enabled())
79     return passphrase_textfield_;
80   return NULL;
81 }
82
83 bool WimaxConfigView::CanLogin() {
84   // In OOBE it may be valid to log in with no credentials (crbug.com/137776).
85   if (!chromeos::StartupUtils::IsOobeCompleted())
86     return true;
87
88   // TODO(benchan): Update this with the correct minimum length (don't just
89   // check if empty).
90   // If the network requires a passphrase, make sure it is the right length.
91   return passphrase_textfield_ && !passphrase_textfield_->text().empty();
92 }
93
94 void WimaxConfigView::UpdateDialogButtons() {
95   parent_->GetDialogClientView()->UpdateDialogButtons();
96 }
97
98 void WimaxConfigView::UpdateErrorLabel() {
99   base::string16 error_msg;
100   if (!service_path_.empty()) {
101     const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
102         GetNetworkState(service_path_);
103     if (wimax && wimax->connection_state() == shill::kStateFailure)
104       error_msg = ui::NetworkConnect::Get()->GetErrorString(
105           wimax->last_error(), wimax->path());
106   }
107   if (!error_msg.empty()) {
108     error_label_->SetText(error_msg);
109     error_label_->SetVisible(true);
110   } else {
111     error_label_->SetVisible(false);
112   }
113 }
114
115 void WimaxConfigView::ContentsChanged(views::Textfield* sender,
116                                       const base::string16& new_contents) {
117   UpdateDialogButtons();
118 }
119
120 bool WimaxConfigView::HandleKeyEvent(views::Textfield* sender,
121                                      const ui::KeyEvent& key_event) {
122   if (sender == passphrase_textfield_ &&
123       key_event.key_code() == ui::VKEY_RETURN) {
124     parent_->GetDialogClientView()->AcceptWindow();
125   }
126   return false;
127 }
128
129 void WimaxConfigView::ButtonPressed(views::Button* sender,
130                                    const ui::Event& event) {
131   if (sender == passphrase_visible_button_ && passphrase_textfield_) {
132     if (passphrase_textfield_->GetTextInputType() == ui::TEXT_INPUT_TYPE_TEXT) {
133       passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
134       passphrase_visible_button_->SetToggled(false);
135     } else {
136       passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
137       passphrase_visible_button_->SetToggled(true);
138     }
139   } else {
140     NOTREACHED();
141   }
142 }
143
144 bool WimaxConfigView::Login() {
145   const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
146       GetNetworkState(service_path_);
147   if (!wimax) {
148     // Shill no longer knows about this network (edge case).
149     // TODO(stevenjb): Add notification for this.
150     NET_LOG_ERROR("Network not found", service_path_);
151     return true;  // Close dialog
152   }
153   base::DictionaryValue properties;
154   properties.SetStringWithoutPathExpansion(
155       shill::kEapIdentityProperty, GetEapIdentity());
156   properties.SetStringWithoutPathExpansion(
157       shill::kEapPasswordProperty, GetEapPassphrase());
158   properties.SetBooleanWithoutPathExpansion(
159       shill::kSaveCredentialsProperty, GetSaveCredentials());
160
161   const bool share_default = true;
162   bool share_network = GetShareNetwork(share_default);
163
164   bool only_policy_autoconnect =
165       onc::PolicyAllowsOnlyPolicyNetworksToAutoconnect(!share_network);
166   if (only_policy_autoconnect) {
167     properties.SetBooleanWithoutPathExpansion(shill::kAutoConnectProperty,
168                                               false);
169   }
170
171   ui::NetworkConnect::Get()->ConfigureNetworkAndConnect(
172       service_path_, properties, share_network);
173   return true;  // dialog will be closed
174 }
175
176 std::string WimaxConfigView::GetEapIdentity() const {
177   DCHECK(identity_textfield_);
178   return base::UTF16ToUTF8(identity_textfield_->text());
179 }
180
181 std::string WimaxConfigView::GetEapPassphrase() const {
182   return passphrase_textfield_ ? base::UTF16ToUTF8(
183                                      passphrase_textfield_->text()) :
184                                  std::string();
185 }
186
187 bool WimaxConfigView::GetSaveCredentials() const {
188   return save_credentials_checkbox_ ? save_credentials_checkbox_->checked() :
189                                       false;
190 }
191
192 bool WimaxConfigView::GetShareNetwork(bool share_default) const {
193   return share_network_checkbox_ ? share_network_checkbox_->checked() :
194                                    share_default;
195 }
196
197 void WimaxConfigView::Cancel() {
198 }
199
200 void WimaxConfigView::Init() {
201   const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
202       GetNetworkState(service_path_);
203   DCHECK(wimax && wimax->type() == shill::kTypeWimax);
204
205   WifiConfigView::ParseEAPUIProperty(
206       &save_credentials_ui_data_, wimax, ::onc::eap::kSaveCredentials);
207   WifiConfigView::ParseEAPUIProperty(
208       &identity_ui_data_, wimax, ::onc::eap::kIdentity);
209   WifiConfigView::ParseUIProperty(
210       &passphrase_ui_data_, wimax, ::onc::wifi::kPassphrase);
211
212   views::GridLayout* layout = views::GridLayout::CreatePanel(this);
213   SetLayoutManager(layout);
214
215   const int column_view_set_id = 0;
216   views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
217   const int kPasswordVisibleWidth = 20;
218   // Label
219   column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
220                         views::GridLayout::USE_PREF, 0, 0);
221   column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
222   // Textfield, combobox.
223   column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
224                         views::GridLayout::USE_PREF, 0,
225                         ChildNetworkConfigView::kInputFieldMinWidth);
226   column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
227   // Password visible button / policy indicator.
228   column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1,
229                         views::GridLayout::USE_PREF, 0, kPasswordVisibleWidth);
230
231   // Network name
232   layout->StartRow(0, column_view_set_id);
233   layout->AddView(new views::Label(l10n_util::GetStringUTF16(
234       IDS_OPTIONS_SETTINGS_INTERNET_TAB_NETWORK)));
235   views::Label* label = new views::Label(base::UTF8ToUTF16(wimax->name()));
236   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
237   layout->AddView(label);
238   layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
239
240   // Identity
241   layout->StartRow(0, column_view_set_id);
242   base::string16 identity_label_text = l10n_util::GetStringUTF16(
243       IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_IDENTITY);
244   identity_label_ = new views::Label(identity_label_text);
245   layout->AddView(identity_label_);
246   identity_textfield_ = new views::Textfield();
247   identity_textfield_->SetAccessibleName(identity_label_text);
248   identity_textfield_->set_controller(this);
249   identity_textfield_->SetEnabled(identity_ui_data_.IsEditable());
250   layout->AddView(identity_textfield_);
251   layout->AddView(new ControlledSettingIndicatorView(identity_ui_data_));
252   layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
253
254   // Passphrase input
255   layout->StartRow(0, column_view_set_id);
256   base::string16 passphrase_label_text = l10n_util::GetStringUTF16(
257       IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE);
258   passphrase_label_ = new views::Label(passphrase_label_text);
259   layout->AddView(passphrase_label_);
260   passphrase_textfield_ = new views::Textfield();
261   passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
262   passphrase_textfield_->set_controller(this);
263   passphrase_label_->SetEnabled(true);
264   passphrase_textfield_->SetEnabled(passphrase_ui_data_.IsEditable());
265   passphrase_textfield_->SetAccessibleName(passphrase_label_text);
266   layout->AddView(passphrase_textfield_);
267
268   if (passphrase_ui_data_.IsManaged()) {
269     layout->AddView(new ControlledSettingIndicatorView(passphrase_ui_data_));
270   } else {
271     // Password visible button.
272     passphrase_visible_button_ = new views::ToggleImageButton(this);
273     passphrase_visible_button_->SetFocusable(true);
274     passphrase_visible_button_->SetTooltipText(
275         l10n_util::GetStringUTF16(
276             IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_SHOW));
277     passphrase_visible_button_->SetToggledTooltipText(
278         l10n_util::GetStringUTF16(
279             IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_HIDE));
280     passphrase_visible_button_->SetImage(
281         views::ImageButton::STATE_NORMAL,
282         ResourceBundle::GetSharedInstance().
283         GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD));
284     passphrase_visible_button_->SetImage(
285         views::ImageButton::STATE_HOVERED,
286         ResourceBundle::GetSharedInstance().
287         GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD_HOVER));
288     passphrase_visible_button_->SetToggledImage(
289         views::ImageButton::STATE_NORMAL,
290         ResourceBundle::GetSharedInstance().
291         GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD));
292     passphrase_visible_button_->SetToggledImage(
293         views::ImageButton::STATE_HOVERED,
294         ResourceBundle::GetSharedInstance().
295         GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD_HOVER));
296     passphrase_visible_button_->SetImageAlignment(
297         views::ImageButton::ALIGN_CENTER, views::ImageButton::ALIGN_MIDDLE);
298     layout->AddView(passphrase_visible_button_);
299   }
300
301   layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
302
303   // Checkboxes.
304
305   if (LoginState::Get()->IsUserAuthenticated()) {
306     // Save credentials
307     layout->StartRow(0, column_view_set_id);
308     save_credentials_checkbox_ = new views::Checkbox(
309         l10n_util::GetStringUTF16(
310             IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SAVE_CREDENTIALS));
311     save_credentials_checkbox_->SetEnabled(
312         save_credentials_ui_data_.IsEditable());
313     layout->SkipColumns(1);
314     layout->AddView(save_credentials_checkbox_);
315     layout->AddView(
316         new ControlledSettingIndicatorView(save_credentials_ui_data_));
317   }
318
319   // Share network
320   if (wimax->profile_path().empty()) {
321     layout->StartRow(0, column_view_set_id);
322     share_network_checkbox_ = new views::Checkbox(
323         l10n_util::GetStringUTF16(
324             IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SHARE_NETWORK));
325
326     bool share_network_checkbox_value = false;
327     bool share_network_checkbox_enabled = false;
328     ChildNetworkConfigView::GetShareStateForLoginState(
329         &share_network_checkbox_value,
330         &share_network_checkbox_enabled);
331
332     share_network_checkbox_->SetChecked(share_network_checkbox_value);
333     share_network_checkbox_->SetEnabled(share_network_checkbox_enabled);
334
335     layout->SkipColumns(1);
336     layout->AddView(share_network_checkbox_);
337   }
338   layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
339
340   // Create an error label.
341   layout->StartRow(0, column_view_set_id);
342   layout->SkipColumns(1);
343   error_label_ = new views::Label();
344   error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
345   error_label_->SetEnabledColor(SK_ColorRED);
346   layout->AddView(error_label_);
347
348   UpdateErrorLabel();
349
350   if (wimax) {
351     NetworkHandler::Get()->network_configuration_handler()->GetProperties(
352         service_path_,
353         base::Bind(&WimaxConfigView::InitFromProperties,
354                    weak_ptr_factory_.GetWeakPtr()),
355         base::Bind(&ShillError, "GetProperties"));
356   }
357 }
358
359 void WimaxConfigView::InitFromProperties(
360     const std::string& service_path,
361     const base::DictionaryValue& properties) {
362   // EapIdentity
363   std::string eap_identity;
364   properties.GetStringWithoutPathExpansion(
365       shill::kEapIdentityProperty, &eap_identity);
366   identity_textfield_->SetText(base::UTF8ToUTF16(eap_identity));
367
368   // Save credentials
369   if (save_credentials_checkbox_) {
370     bool save_credentials = false;
371     properties.GetBooleanWithoutPathExpansion(
372         shill::kSaveCredentialsProperty, &save_credentials);
373     save_credentials_checkbox_->SetChecked(save_credentials);
374   }
375 }
376
377 void WimaxConfigView::InitFocus() {
378   views::View* view_to_focus = GetInitiallyFocusedView();
379   if (view_to_focus)
380     view_to_focus->RequestFocus();
381 }
382
383 }  // namespace chromeos