wifi_config_view.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/wifi_config_view.h"
6
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/profiles/profile_manager.h"
16#include "grit/chromium_strings.h"
17#include "grit/generated_resources.h"
18#include "grit/locale_settings.h"
19#include "grit/theme_resources.h"
20#include "ui/base/events/event.h"
21#include "ui/base/l10n/l10n_util.h"
22#include "ui/base/resource/resource_bundle.h"
23#include "ui/views/controls/button/checkbox.h"
24#include "ui/views/controls/button/image_button.h"
25#include "ui/views/controls/combobox/combobox.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"
32
33namespace chromeos {
34
35namespace {
36
37// Returns true if network is known to require 802.1x.
38bool Is8021x(const WifiNetwork* wifi) {
39  return wifi && wifi->encrypted() && wifi->encryption() == SECURITY_8021X;
40}
41
42// Combobox that supports a preferred width.  Used by Server CA combobox
43// because the strings inside it are too wide.
44class ComboboxWithWidth : public views::Combobox {
45 public:
46  ComboboxWithWidth(ui::ComboboxModel* model, int width)
47      : Combobox(model),
48        width_(width) {
49  }
50  virtual ~ComboboxWithWidth() {}
51  virtual gfx::Size GetPreferredSize() OVERRIDE {
52    gfx::Size size = Combobox::GetPreferredSize();
53    size.set_width(width_);
54    return size;
55  }
56 private:
57  int width_;
58  DISALLOW_COPY_AND_ASSIGN(ComboboxWithWidth);
59};
60
61enum SecurityComboboxIndex {
62  SECURITY_INDEX_NONE  = 0,
63  SECURITY_INDEX_WEP   = 1,
64  SECURITY_INDEX_PSK   = 2,
65  SECURITY_INDEX_COUNT = 3
66};
67
68// Methods in alphabetical order.
69enum EAPMethodComboboxIndex {
70  EAP_METHOD_INDEX_NONE  = 0,
71  EAP_METHOD_INDEX_LEAP  = 1,
72  EAP_METHOD_INDEX_PEAP  = 2,
73  EAP_METHOD_INDEX_TLS   = 3,
74  EAP_METHOD_INDEX_TTLS  = 4,
75  EAP_METHOD_INDEX_COUNT = 5
76};
77
78enum Phase2AuthComboboxIndex {
79  PHASE_2_AUTH_INDEX_AUTO     = 0,  // LEAP, EAP-TLS have only this auth.
80  PHASE_2_AUTH_INDEX_MD5      = 1,
81  PHASE_2_AUTH_INDEX_MSCHAPV2 = 2,  // PEAP has up to this auth.
82  PHASE_2_AUTH_INDEX_MSCHAP   = 3,
83  PHASE_2_AUTH_INDEX_PAP      = 4,
84  PHASE_2_AUTH_INDEX_CHAP     = 5,  // EAP-TTLS has up to this auth.
85  PHASE_2_AUTH_INDEX_COUNT    = 6
86};
87
88}  // namespace
89
90namespace internal {
91
92class SecurityComboboxModel : public ui::ComboboxModel {
93 public:
94  SecurityComboboxModel();
95  virtual ~SecurityComboboxModel();
96
97  // Overridden from ui::ComboboxModel:
98  virtual int GetItemCount() const OVERRIDE;
99  virtual string16 GetItemAt(int index) OVERRIDE;
100
101 private:
102  DISALLOW_COPY_AND_ASSIGN(SecurityComboboxModel);
103};
104
105class EAPMethodComboboxModel : public ui::ComboboxModel {
106 public:
107  EAPMethodComboboxModel();
108  virtual ~EAPMethodComboboxModel();
109
110  // Overridden from ui::ComboboxModel:
111  virtual int GetItemCount() const OVERRIDE;
112  virtual string16 GetItemAt(int index) OVERRIDE;
113
114 private:
115  DISALLOW_COPY_AND_ASSIGN(EAPMethodComboboxModel);
116};
117
118class Phase2AuthComboboxModel : public ui::ComboboxModel {
119 public:
120  explicit Phase2AuthComboboxModel(views::Combobox* eap_method_combobox);
121  virtual ~Phase2AuthComboboxModel();
122
123  // Overridden from ui::ComboboxModel:
124  virtual int GetItemCount() const OVERRIDE;
125  virtual string16 GetItemAt(int index) OVERRIDE;
126
127 private:
128  views::Combobox* eap_method_combobox_;
129
130  DISALLOW_COPY_AND_ASSIGN(Phase2AuthComboboxModel);
131};
132
133class ServerCACertComboboxModel : public ui::ComboboxModel {
134 public:
135  explicit ServerCACertComboboxModel(CertLibrary* cert_library);
136  virtual ~ServerCACertComboboxModel();
137
138  // Overridden from ui::ComboboxModel:
139  virtual int GetItemCount() const OVERRIDE;
140  virtual string16 GetItemAt(int index) OVERRIDE;
141
142 private:
143  CertLibrary* cert_library_;
144  DISALLOW_COPY_AND_ASSIGN(ServerCACertComboboxModel);
145};
146
147class UserCertComboboxModel : public ui::ComboboxModel {
148 public:
149  explicit UserCertComboboxModel(CertLibrary* cert_library);
150  virtual ~UserCertComboboxModel();
151
152  // Overridden from ui::ComboboxModel:
153  virtual int GetItemCount() const OVERRIDE;
154  virtual string16 GetItemAt(int index) OVERRIDE;
155
156 private:
157  CertLibrary* cert_library_;
158
159  DISALLOW_COPY_AND_ASSIGN(UserCertComboboxModel);
160};
161
162// SecurityComboboxModel -------------------------------------------------------
163
164SecurityComboboxModel::SecurityComboboxModel() {
165}
166
167SecurityComboboxModel::~SecurityComboboxModel() {
168}
169
170int SecurityComboboxModel::GetItemCount() const {
171    return SECURITY_INDEX_COUNT;
172  }
173string16 SecurityComboboxModel::GetItemAt(int index) {
174  if (index == SECURITY_INDEX_NONE)
175    return l10n_util::GetStringUTF16(
176        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_NONE);
177  else if (index == SECURITY_INDEX_WEP)
178    return l10n_util::GetStringUTF16(
179        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_WEP);
180  else if (index == SECURITY_INDEX_PSK)
181    return l10n_util::GetStringUTF16(
182        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_PSK);
183  NOTREACHED();
184  return string16();
185}
186
187// EAPMethodComboboxModel ------------------------------------------------------
188
189EAPMethodComboboxModel::EAPMethodComboboxModel() {
190}
191
192EAPMethodComboboxModel::~EAPMethodComboboxModel() {
193}
194
195int EAPMethodComboboxModel::GetItemCount() const {
196  return EAP_METHOD_INDEX_COUNT;
197}
198string16 EAPMethodComboboxModel::GetItemAt(int index) {
199  if (index == EAP_METHOD_INDEX_NONE)
200    return l10n_util::GetStringUTF16(
201        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_EAP_METHOD_NONE);
202  else if (index == EAP_METHOD_INDEX_LEAP)
203    return l10n_util::GetStringUTF16(
204        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_EAP_METHOD_LEAP);
205  else if (index == EAP_METHOD_INDEX_PEAP)
206    return l10n_util::GetStringUTF16(
207        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_EAP_METHOD_PEAP);
208  else if (index == EAP_METHOD_INDEX_TLS)
209    return l10n_util::GetStringUTF16(
210        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_EAP_METHOD_TLS);
211  else if (index == EAP_METHOD_INDEX_TTLS)
212    return l10n_util::GetStringUTF16(
213        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_EAP_METHOD_TTLS);
214  NOTREACHED();
215  return string16();
216}
217
218// Phase2AuthComboboxModel -----------------------------------------------------
219
220Phase2AuthComboboxModel::Phase2AuthComboboxModel(
221    views::Combobox* eap_method_combobox)
222    : eap_method_combobox_(eap_method_combobox) {
223}
224
225Phase2AuthComboboxModel::~Phase2AuthComboboxModel() {
226}
227
228int Phase2AuthComboboxModel::GetItemCount() const {
229  switch (eap_method_combobox_->selected_index()) {
230    case EAP_METHOD_INDEX_NONE:
231    case EAP_METHOD_INDEX_TLS:
232    case EAP_METHOD_INDEX_LEAP:
233      return PHASE_2_AUTH_INDEX_AUTO + 1;
234    case EAP_METHOD_INDEX_PEAP:
235      return PHASE_2_AUTH_INDEX_MSCHAPV2 + 1;
236    case EAP_METHOD_INDEX_TTLS:
237      return PHASE_2_AUTH_INDEX_CHAP + 1;
238  }
239  NOTREACHED();
240  return 0;
241}
242
243string16 Phase2AuthComboboxModel::GetItemAt(int index) {
244  if (index == PHASE_2_AUTH_INDEX_AUTO)
245    return l10n_util::GetStringUTF16(
246        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PHASE_2_AUTH_AUTO);
247  else if (index == PHASE_2_AUTH_INDEX_MD5)
248    return l10n_util::GetStringUTF16(
249        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PHASE_2_AUTH_MD5);
250  else if (index == PHASE_2_AUTH_INDEX_MSCHAPV2)
251    return l10n_util::GetStringUTF16(
252        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PHASE_2_AUTH_MSCHAPV2);
253  else if (index == PHASE_2_AUTH_INDEX_MSCHAP)
254    return l10n_util::GetStringUTF16(
255        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PHASE_2_AUTH_MSCHAP);
256  else if (index == PHASE_2_AUTH_INDEX_PAP)
257    return l10n_util::GetStringUTF16(
258        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PHASE_2_AUTH_PAP);
259  else if (index == PHASE_2_AUTH_INDEX_CHAP)
260    return l10n_util::GetStringUTF16(
261        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PHASE_2_AUTH_CHAP);
262  NOTREACHED();
263  return string16();
264}
265
266// ServerCACertComboboxModel ---------------------------------------------------
267
268ServerCACertComboboxModel::ServerCACertComboboxModel(CertLibrary* cert_library)
269    : cert_library_(cert_library) {
270  DCHECK(cert_library);
271}
272
273ServerCACertComboboxModel::~ServerCACertComboboxModel() {
274}
275
276int ServerCACertComboboxModel::GetItemCount() const {
277  if (cert_library_->CertificatesLoading())
278    return 1;  // "Loading"
279  // First "Default", then the certs, then "Do not check".
280  return cert_library_->GetCACertificates().Size() + 2;
281}
282
283string16 ServerCACertComboboxModel::GetItemAt(int index) {
284  if (cert_library_->CertificatesLoading())
285    return l10n_util::GetStringUTF16(
286        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_LOADING);
287  if (index == 0)
288    return l10n_util::GetStringUTF16(
289        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_SERVER_CA_DEFAULT);
290  if (index == GetItemCount() - 1)
291    return l10n_util::GetStringUTF16(
292        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_SERVER_CA_DO_NOT_CHECK);
293  int cert_index = index - 1;
294  return cert_library_->GetCACertificates().GetDisplayStringAt(cert_index);
295}
296
297// UserCertComboboxModel -------------------------------------------------------
298
299UserCertComboboxModel::UserCertComboboxModel(CertLibrary* cert_library)
300    : cert_library_(cert_library) {
301  DCHECK(cert_library);
302}
303
304UserCertComboboxModel::~UserCertComboboxModel() {
305}
306
307int UserCertComboboxModel::GetItemCount() const {
308  if (cert_library_->CertificatesLoading())
309    return 1;  // "Loading"
310  int num_certs = cert_library_->GetUserCertificates().Size();
311  if (num_certs == 0)
312    return 1;  // "None installed"
313  return num_certs;
314}
315
316string16 UserCertComboboxModel::GetItemAt(int index) {
317  if (cert_library_->CertificatesLoading())
318    return l10n_util::GetStringUTF16(
319        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_LOADING);
320  if (cert_library_->GetUserCertificates().Size() == 0)
321    return l10n_util::GetStringUTF16(
322        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_USER_CERT_NONE_INSTALLED);
323  return cert_library_->GetUserCertificates().GetDisplayStringAt(index);
324}
325
326}  // namespace internal
327
328WifiConfigView::WifiConfigView(NetworkConfigView* parent, WifiNetwork* wifi)
329    : ChildNetworkConfigView(parent, wifi),
330      cert_library_(NULL),
331      ssid_textfield_(NULL),
332      eap_method_combobox_(NULL),
333      phase_2_auth_label_(NULL),
334      phase_2_auth_combobox_(NULL),
335      user_cert_label_(NULL),
336      user_cert_combobox_(NULL),
337      server_ca_cert_label_(NULL),
338      server_ca_cert_combobox_(NULL),
339      identity_label_(NULL),
340      identity_textfield_(NULL),
341      identity_anonymous_label_(NULL),
342      identity_anonymous_textfield_(NULL),
343      save_credentials_checkbox_(NULL),
344      share_network_checkbox_(NULL),
345      shared_network_label_(NULL),
346      security_combobox_(NULL),
347      passphrase_label_(NULL),
348      passphrase_textfield_(NULL),
349      passphrase_visible_button_(NULL),
350      error_label_(NULL) {
351  Init(wifi, Is8021x(wifi));
352}
353
354WifiConfigView::WifiConfigView(NetworkConfigView* parent, bool show_8021x)
355    : ChildNetworkConfigView(parent),
356      cert_library_(NULL),
357      ssid_textfield_(NULL),
358      eap_method_combobox_(NULL),
359      phase_2_auth_label_(NULL),
360      phase_2_auth_combobox_(NULL),
361      user_cert_label_(NULL),
362      user_cert_combobox_(NULL),
363      server_ca_cert_label_(NULL),
364      server_ca_cert_combobox_(NULL),
365      identity_label_(NULL),
366      identity_textfield_(NULL),
367      identity_anonymous_label_(NULL),
368      identity_anonymous_textfield_(NULL),
369      save_credentials_checkbox_(NULL),
370      share_network_checkbox_(NULL),
371      shared_network_label_(NULL),
372      security_combobox_(NULL),
373      passphrase_label_(NULL),
374      passphrase_textfield_(NULL),
375      passphrase_visible_button_(NULL),
376      error_label_(NULL) {
377  Init(NULL, show_8021x);
378}
379
380WifiConfigView::~WifiConfigView() {
381  if (cert_library_)
382    cert_library_->RemoveObserver(this);
383}
384
385views::View* WifiConfigView::GetInitiallyFocusedView() {
386  // Return a reasonable widget for initial focus,
387  // depending on what we're showing.
388  if (ssid_textfield_)
389    return ssid_textfield_;
390  else if (eap_method_combobox_)
391    return eap_method_combobox_;
392  else if (passphrase_textfield_ && passphrase_textfield_->enabled())
393    return passphrase_textfield_;
394  else
395    return NULL;
396}
397
398bool WifiConfigView::CanLogin() {
399  static const size_t kMinWirelessPasswordLen = 5;
400
401  // We either have an existing wifi network or the user entered an SSID.
402  if (service_path_.empty() && GetSsid().empty())
403    return false;
404
405  // If the network requires a passphrase, make sure it is the right length.
406  if (passphrase_textfield_ != NULL
407      && passphrase_textfield_->enabled()
408      && passphrase_textfield_->text().length() < kMinWirelessPasswordLen)
409    return false;
410
411  // If we're using EAP, we must have a method.
412  if (eap_method_combobox_ &&
413      eap_method_combobox_->selected_index() == EAP_METHOD_INDEX_NONE)
414    return false;
415
416  // Block login if certs are required but user has none.
417  if (UserCertRequired() && (!HaveUserCerts() || !IsUserCertValid()))
418      return false;
419
420  return true;
421}
422
423bool WifiConfigView::UserCertRequired() const {
424  if (!cert_library_)
425    return false;  // return false until cert_library_ is initialized.
426  return UserCertActive();
427}
428
429bool WifiConfigView::HaveUserCerts() const {
430  return cert_library_->GetUserCertificates().Size() > 0;
431}
432
433bool WifiConfigView::IsUserCertValid() const {
434  if (!UserCertActive())
435    return false;
436  int index = user_cert_combobox_->selected_index();
437  if (index < 0)
438    return false;
439  // Currently only hardware-backed user certificates are valid.
440  if (cert_library_->IsHardwareBacked() &&
441      !cert_library_->GetUserCertificates().IsHardwareBackedAt(index))
442    return false;
443  return true;
444}
445
446bool WifiConfigView::Phase2AuthActive() const {
447  if (phase_2_auth_combobox_)
448    return phase_2_auth_combobox_->model()->GetItemCount() > 1;
449  return false;
450}
451
452bool WifiConfigView::PassphraseActive() const {
453  if (eap_method_combobox_) {
454    // No password for EAP-TLS.
455    int index = eap_method_combobox_->selected_index();
456    return index != EAP_METHOD_INDEX_NONE && index != EAP_METHOD_INDEX_TLS;
457  } else if (security_combobox_) {
458    return security_combobox_->selected_index() != SECURITY_INDEX_NONE;
459  }
460  return false;
461}
462
463bool WifiConfigView::UserCertActive() const {
464  // User certs only for EAP-TLS.
465  if (eap_method_combobox_)
466    return eap_method_combobox_->selected_index() == EAP_METHOD_INDEX_TLS;
467
468  return false;
469}
470
471bool WifiConfigView::CaCertActive() const {
472  // No server CA certs for LEAP.
473  if (eap_method_combobox_) {
474    int index = eap_method_combobox_->selected_index();
475    return index != EAP_METHOD_INDEX_NONE && index != EAP_METHOD_INDEX_LEAP;
476  }
477  return false;
478}
479
480void WifiConfigView::UpdateDialogButtons() {
481  parent_->GetDialogClientView()->UpdateDialogButtons();
482}
483
484void WifiConfigView::RefreshEapFields() {
485  DCHECK(cert_library_);
486
487  // If EAP method changes, the phase 2 auth choices may have changed also.
488  phase_2_auth_combobox_->ModelChanged();
489  phase_2_auth_combobox_->SetSelectedIndex(0);
490  bool phase_2_auth_enabled = Phase2AuthActive();
491  phase_2_auth_combobox_->SetEnabled(phase_2_auth_enabled &&
492                                     phase_2_auth_ui_data_.editable());
493  phase_2_auth_label_->SetEnabled(phase_2_auth_enabled);
494
495  // Passphrase.
496  bool passphrase_enabled = PassphraseActive();
497  passphrase_textfield_->SetEnabled(passphrase_enabled &&
498                                    passphrase_ui_data_.editable());
499  passphrase_label_->SetEnabled(passphrase_enabled);
500  if (!passphrase_enabled)
501    passphrase_textfield_->SetText(string16());
502
503  // User cert.
504  bool certs_loading = cert_library_->CertificatesLoading();
505  bool user_cert_enabled = UserCertActive();
506  user_cert_label_->SetEnabled(user_cert_enabled);
507  bool have_user_certs = !certs_loading && HaveUserCerts();
508  user_cert_combobox_->SetEnabled(user_cert_enabled &&
509                                  have_user_certs &&
510                                  user_cert_ui_data_.editable());
511  user_cert_combobox_->ModelChanged();
512  user_cert_combobox_->SetSelectedIndex(0);
513
514  // Server CA.
515  bool ca_cert_enabled = CaCertActive();
516  server_ca_cert_label_->SetEnabled(ca_cert_enabled);
517  server_ca_cert_combobox_->SetEnabled(ca_cert_enabled &&
518                                       !certs_loading &&
519                                       server_ca_cert_ui_data_.editable());
520  server_ca_cert_combobox_->ModelChanged();
521  server_ca_cert_combobox_->SetSelectedIndex(0);
522
523  // No anonymous identity if no phase 2 auth.
524  bool identity_anonymous_enabled = phase_2_auth_enabled;
525  identity_anonymous_textfield_->SetEnabled(
526      identity_anonymous_enabled && identity_anonymous_ui_data_.editable());
527  identity_anonymous_label_->SetEnabled(identity_anonymous_enabled);
528  if (!identity_anonymous_enabled)
529    identity_anonymous_textfield_->SetText(string16());
530
531  RefreshShareCheckbox();
532}
533
534void WifiConfigView::RefreshShareCheckbox() {
535  if (!share_network_checkbox_)
536    return;
537
538  if (security_combobox_ &&
539      security_combobox_->selected_index() == SECURITY_INDEX_NONE) {
540    share_network_checkbox_->SetEnabled(false);
541    share_network_checkbox_->SetChecked(true);
542  } else if (eap_method_combobox_ &&
543             (eap_method_combobox_->selected_index() == EAP_METHOD_INDEX_TLS ||
544              user_cert_combobox_->selected_index() != 0)) {
545    // Can not share TLS network (requires certificate), or any network where
546    // user certificates are enabled.
547    share_network_checkbox_->SetEnabled(false);
548    share_network_checkbox_->SetChecked(false);
549  } else if (!UserManager::Get()->IsUserLoggedIn()) {
550    // If not logged in, networks must be shared.
551    share_network_checkbox_->SetEnabled(false);
552    share_network_checkbox_->SetChecked(true);
553  } else {
554    share_network_checkbox_->SetEnabled(true);
555    share_network_checkbox_->SetChecked(false);  // Default to unshared.
556  }
557}
558
559void WifiConfigView::UpdateErrorLabel() {
560  std::string error_msg;
561  if (UserCertRequired() && cert_library_->CertificatesLoaded()) {
562    if (!HaveUserCerts()) {
563      if (!UserManager::Get()->IsUserLoggedIn()) {
564        error_msg = l10n_util::GetStringUTF8(
565            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_LOGIN_FOR_USER_CERT);
566      } else {
567        error_msg = l10n_util::GetStringUTF8(
568            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PLEASE_INSTALL_USER_CERT);
569      }
570    } else if (!IsUserCertValid()) {
571      error_msg = l10n_util::GetStringUTF8(
572          IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_REQUIRE_HARDWARE_BACKED);
573    }
574  }
575  if (error_msg.empty() && !service_path_.empty()) {
576    NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
577    const WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path_);
578    if (wifi && wifi->failed()) {
579      bool passphrase_empty = wifi->GetPassphrase().empty();
580      switch (wifi->error()) {
581        case ERROR_BAD_PASSPHRASE:
582          if (!passphrase_empty) {
583            error_msg = l10n_util::GetStringUTF8(
584                IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_BAD_PASSPHRASE);
585          }
586          break;
587        case ERROR_BAD_WEPKEY:
588          if (!passphrase_empty) {
589            error_msg = l10n_util::GetStringUTF8(
590                IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_BAD_WEPKEY);
591          }
592          break;
593        default:
594          error_msg = wifi->GetErrorString();
595          break;
596      }
597    }
598  }
599  if (!error_msg.empty()) {
600    error_label_->SetText(UTF8ToUTF16(error_msg));
601    error_label_->SetVisible(true);
602  } else {
603    error_label_->SetVisible(false);
604  }
605}
606
607void WifiConfigView::ContentsChanged(views::Textfield* sender,
608                                     const string16& new_contents) {
609  UpdateDialogButtons();
610}
611
612bool WifiConfigView::HandleKeyEvent(views::Textfield* sender,
613                                    const ui::KeyEvent& key_event) {
614  if (sender == passphrase_textfield_ &&
615      key_event.key_code() == ui::VKEY_RETURN) {
616    parent_->GetDialogClientView()->AcceptWindow();
617  }
618  return false;
619}
620
621void WifiConfigView::ButtonPressed(views::Button* sender,
622                                   const ui::Event& event) {
623  if (sender == passphrase_visible_button_) {
624    if (passphrase_textfield_) {
625      passphrase_textfield_->SetObscured(!passphrase_textfield_->IsObscured());
626      passphrase_visible_button_->SetToggled(
627          !passphrase_textfield_->IsObscured());
628    }
629  } else {
630    NOTREACHED();
631  }
632}
633
634void WifiConfigView::OnSelectedIndexChanged(views::Combobox* combobox) {
635  if (combobox == security_combobox_) {
636    bool passphrase_enabled = PassphraseActive();
637    passphrase_label_->SetEnabled(passphrase_enabled);
638    passphrase_textfield_->SetEnabled(passphrase_enabled &&
639                                      passphrase_ui_data_.editable());
640    if (!passphrase_enabled)
641      passphrase_textfield_->SetText(string16());
642    RefreshShareCheckbox();
643  } else if (combobox == user_cert_combobox_) {
644    RefreshShareCheckbox();
645  } else if (combobox == eap_method_combobox_) {
646    RefreshEapFields();
647  }
648  UpdateDialogButtons();
649  UpdateErrorLabel();
650}
651
652void WifiConfigView::OnCertificatesLoaded(bool initial_load) {
653  RefreshEapFields();
654  UpdateDialogButtons();
655  UpdateErrorLabel();
656}
657
658bool WifiConfigView::Login() {
659  NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
660  if (service_path_.empty()) {
661    const bool share_default = true;  // share networks by default
662    if (!eap_method_combobox_) {
663      // Hidden ordinary Wi-Fi connection.
664      ConnectionSecurity security = SECURITY_UNKNOWN;
665      switch (security_combobox_->selected_index()) {
666        case SECURITY_INDEX_NONE:
667          security = SECURITY_NONE;
668          break;
669        case SECURITY_INDEX_WEP:
670          security = SECURITY_WEP;
671          break;
672        case SECURITY_INDEX_PSK:
673          security = SECURITY_PSK;
674          break;
675      }
676      cros->ConnectToUnconfiguredWifiNetwork(
677          GetSsid(),
678          security,
679          GetPassphrase(),
680          NULL,
681          GetSaveCredentials(),
682          GetShareNetwork(share_default));
683    } else {
684      // Hidden 802.1X EAP Wi-Fi connection.
685      chromeos::NetworkLibrary::EAPConfigData config_data;
686      config_data.method = GetEapMethod();
687      config_data.auth = GetEapPhase2Auth();
688      config_data.server_ca_cert_nss_nickname = GetEapServerCaCertNssNickname();
689      config_data.use_system_cas = GetEapUseSystemCas();
690      config_data.client_cert_pkcs11_id = GetEapClientCertPkcs11Id();
691      config_data.identity = GetEapIdentity();
692      config_data.anonymous_identity = GetEapAnonymousIdentity();
693      cros->ConnectToUnconfiguredWifiNetwork(
694          GetSsid(),
695          SECURITY_8021X,
696          GetPassphrase(),
697          &config_data,
698          GetSaveCredentials(),
699          GetShareNetwork(share_default));
700    }
701  } else {
702    WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path_);
703    if (!wifi) {
704      // Shill no longer knows about this wifi network (edge case).
705      // TODO(stevenjb): Add a notification (chromium-os13225).
706      LOG(WARNING) << "Wifi network: " << service_path_ << " no longer exists.";
707      return true;
708    }
709    if (eap_method_combobox_) {
710      // Visible 802.1X EAP Wi-Fi connection.
711      EAPMethod method = GetEapMethod();
712      DCHECK(method != EAP_METHOD_UNKNOWN);
713      wifi->SetEAPMethod(method);
714      wifi->SetEAPPhase2Auth(GetEapPhase2Auth());
715      wifi->SetEAPServerCaCertNssNickname(GetEapServerCaCertNssNickname());
716      wifi->SetEAPUseSystemCAs(GetEapUseSystemCas());
717      wifi->SetEAPClientCertPkcs11Id(GetEapClientCertPkcs11Id());
718      wifi->SetEAPIdentity(GetEapIdentity());
719      wifi->SetEAPAnonymousIdentity(GetEapAnonymousIdentity());
720      wifi->SetEAPPassphrase(GetPassphrase());
721      wifi->SetSaveCredentials(GetSaveCredentials());
722    } else {
723      // Visible ordinary Wi-Fi connection.
724      const std::string passphrase = GetPassphrase();
725      if (passphrase != wifi->passphrase())
726        wifi->SetPassphrase(passphrase);
727    }
728    bool share_default = (wifi->profile_type() != PROFILE_USER);
729    wifi->SetEnrollmentDelegate(
730        CreateEnrollmentDelegate(GetWidget()->GetNativeWindow(),
731                                 wifi->name(),
732                                 ProfileManager::GetLastUsedProfile()));
733    cros->ConnectToWifiNetwork(wifi, GetShareNetwork(share_default));
734    // Connection failures are responsible for updating the UI, including
735    // reopening dialogs.
736  }
737  return true;  // dialog will be closed
738}
739
740std::string WifiConfigView::GetSsid() const {
741  std::string result;
742  if (ssid_textfield_ != NULL) {
743    std::string untrimmed = UTF16ToUTF8(ssid_textfield_->text());
744    TrimWhitespaceASCII(untrimmed, TRIM_ALL, &result);
745  }
746  return result;
747}
748
749std::string WifiConfigView::GetPassphrase() const {
750  std::string result;
751  if (passphrase_textfield_ != NULL)
752    result = UTF16ToUTF8(passphrase_textfield_->text());
753  return result;
754}
755
756bool WifiConfigView::GetSaveCredentials() const {
757  if (!save_credentials_checkbox_)
758    return true;  // share networks by default (e.g. non 8021x).
759  return save_credentials_checkbox_->checked();
760}
761
762bool WifiConfigView::GetShareNetwork(bool share_default) const {
763  if (!share_network_checkbox_)
764    return share_default;
765  return share_network_checkbox_->checked();
766}
767
768EAPMethod WifiConfigView::GetEapMethod() const {
769  DCHECK(eap_method_combobox_);
770  switch (eap_method_combobox_->selected_index()) {
771    case EAP_METHOD_INDEX_NONE:
772      return EAP_METHOD_UNKNOWN;
773    case EAP_METHOD_INDEX_PEAP:
774      return EAP_METHOD_PEAP;
775    case EAP_METHOD_INDEX_TLS:
776      return EAP_METHOD_TLS;
777    case EAP_METHOD_INDEX_TTLS:
778      return EAP_METHOD_TTLS;
779    case EAP_METHOD_INDEX_LEAP:
780      return EAP_METHOD_LEAP;
781    default:
782      return EAP_METHOD_UNKNOWN;
783  }
784}
785
786EAPPhase2Auth WifiConfigView::GetEapPhase2Auth() const {
787  DCHECK(phase_2_auth_combobox_);
788  switch (phase_2_auth_combobox_->selected_index()) {
789    case PHASE_2_AUTH_INDEX_AUTO:
790      return EAP_PHASE_2_AUTH_AUTO;
791    case PHASE_2_AUTH_INDEX_MD5:
792      return EAP_PHASE_2_AUTH_MD5;
793    case PHASE_2_AUTH_INDEX_MSCHAPV2:
794      return EAP_PHASE_2_AUTH_MSCHAPV2;
795    case PHASE_2_AUTH_INDEX_MSCHAP:
796      return EAP_PHASE_2_AUTH_MSCHAP;
797    case PHASE_2_AUTH_INDEX_PAP:
798      return EAP_PHASE_2_AUTH_PAP;
799    case PHASE_2_AUTH_INDEX_CHAP:
800      return EAP_PHASE_2_AUTH_CHAP;
801    default:
802      return EAP_PHASE_2_AUTH_AUTO;
803  }
804}
805
806std::string WifiConfigView::GetEapServerCaCertNssNickname() const {
807  DCHECK(server_ca_cert_combobox_);
808  DCHECK(cert_library_);
809  int index = server_ca_cert_combobox_->selected_index();
810  if (index == 0) {
811    // First item is "Default".
812    return std::string();
813  } else if (index == server_ca_cert_combobox_->model()->GetItemCount() - 1) {
814    // Last item is "Do not check".
815    return std::string();
816  } else {
817    DCHECK(cert_library_);
818    int cert_index = index - 1;
819    return cert_library_->GetCACertificates().GetNicknameAt(cert_index);
820  }
821}
822
823bool WifiConfigView::GetEapUseSystemCas() const {
824  DCHECK(server_ca_cert_combobox_);
825  // Only use system CAs if the first item ("Default") is selected.
826  return server_ca_cert_combobox_->selected_index() == 0;
827}
828
829std::string WifiConfigView::GetEapClientCertPkcs11Id() const {
830  DCHECK(user_cert_combobox_);
831  DCHECK(cert_library_);
832  if (!HaveUserCerts()) {
833    return std::string();  // "None installed"
834  } else {
835    // Certificates are listed in the order they appear in the model.
836    int index = user_cert_combobox_->selected_index();
837    return cert_library_->GetUserCertificates().GetPkcs11IdAt(index);
838  }
839}
840
841std::string WifiConfigView::GetEapIdentity() const {
842  DCHECK(identity_textfield_);
843  return UTF16ToUTF8(identity_textfield_->text());
844}
845
846std::string WifiConfigView::GetEapAnonymousIdentity() const {
847  DCHECK(identity_anonymous_textfield_);
848  return UTF16ToUTF8(identity_anonymous_textfield_->text());
849}
850
851void WifiConfigView::Cancel() {
852}
853
854// This will initialize the view depending on if we have a wifi network or not.
855// And if we are doing simple password encryption or the more complicated
856// 802.1x encryption.
857// If we are creating the "Join other network..." dialog, we will allow user
858// to enter the data. And if they select the 802.1x encryption, we will show
859// the 802.1x fields.
860void WifiConfigView::Init(WifiNetwork* wifi, bool show_8021x) {
861  if (wifi) {
862    ParseWiFiEAPUIProperty(&eap_method_ui_data_, wifi, onc::eap::kOuter);
863    ParseWiFiEAPUIProperty(&phase_2_auth_ui_data_, wifi, onc::eap::kInner);
864    ParseWiFiEAPUIProperty(&user_cert_ui_data_, wifi, onc::eap::kClientCertRef);
865    ParseWiFiEAPUIProperty(&server_ca_cert_ui_data_, wifi,
866                           onc::eap::kServerCARef);
867    if (server_ca_cert_ui_data_.managed()) {
868      ParseWiFiEAPUIProperty(&server_ca_cert_ui_data_, wifi,
869                             onc::eap::kUseSystemCAs);
870    }
871    ParseWiFiEAPUIProperty(&identity_ui_data_, wifi, onc::eap::kIdentity);
872    ParseWiFiEAPUIProperty(&identity_anonymous_ui_data_, wifi,
873                           onc::eap::kAnonymousIdentity);
874    ParseWiFiEAPUIProperty(&save_credentials_ui_data_, wifi,
875                           onc::eap::kSaveCredentials);
876    if (show_8021x)
877      ParseWiFiEAPUIProperty(&passphrase_ui_data_, wifi, onc::eap::kPassword);
878    else
879      ParseWiFiUIProperty(&passphrase_ui_data_, wifi, onc::wifi::kPassphrase);
880  }
881
882  views::GridLayout* layout = views::GridLayout::CreatePanel(this);
883  SetLayoutManager(layout);
884
885  const int column_view_set_id = 0;
886  views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
887  const int kPasswordVisibleWidth = 20;
888  // Label
889  column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
890                        views::GridLayout::USE_PREF, 0, 0);
891  column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
892  // Textfield, combobox.
893  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
894                        views::GridLayout::USE_PREF, 0,
895                        ChildNetworkConfigView::kInputFieldMinWidth);
896  column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
897  // Password visible button / policy indicator.
898  column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1,
899                        views::GridLayout::USE_PREF, 0, kPasswordVisibleWidth);
900
901  // Title
902  layout->StartRow(0, column_view_set_id);
903  views::Label* title = new views::Label(l10n_util::GetStringUTF16(
904      IDS_OPTIONS_SETTINGS_JOIN_WIFI_NETWORKS));
905  title->SetFont(title->font().DeriveFont(1, gfx::Font::BOLD));
906  layout->AddView(title, 5, 1);
907  layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
908
909  // SSID input
910  layout->StartRow(0, column_view_set_id);
911  layout->AddView(new views::Label(l10n_util::GetStringUTF16(
912      IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_NETWORK_ID)));
913  if (!wifi) {
914    ssid_textfield_ = new views::Textfield(views::Textfield::STYLE_DEFAULT);
915    ssid_textfield_->SetController(this);
916    ssid_textfield_->SetAccessibleName(l10n_util::GetStringUTF16(
917        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_NETWORK_ID));
918    layout->AddView(ssid_textfield_);
919  } else {
920    views::Label* label = new views::Label(UTF8ToUTF16(wifi->name()));
921    label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
922    layout->AddView(label);
923  }
924  layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
925
926  // Security select
927  if (!wifi && !show_8021x) {
928    layout->StartRow(0, column_view_set_id);
929    layout->AddView(new views::Label(l10n_util::GetStringUTF16(
930          IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY)));
931    security_combobox_model_.reset(new internal::SecurityComboboxModel);
932    security_combobox_ = new views::Combobox(security_combobox_model_.get());
933    security_combobox_->set_listener(this);
934    layout->AddView(security_combobox_);
935    layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
936  }
937
938  // Only enumerate certificates in the data model for 802.1X networks.
939  if (show_8021x) {
940    // Initialize cert_library_ for 802.1X netoworks.
941    cert_library_ = chromeos::CrosLibrary::Get()->GetCertLibrary();
942    // Setup a callback if certificates are yet to be loaded,
943    if (!cert_library_->CertificatesLoaded())
944      cert_library_->AddObserver(this);
945
946    // EAP method
947    layout->StartRow(0, column_view_set_id);
948    layout->AddView(new views::Label(l10n_util::GetStringUTF16(
949        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_EAP_METHOD)));
950    eap_method_combobox_model_.reset(new internal::EAPMethodComboboxModel);
951    eap_method_combobox_ = new views::Combobox(
952        eap_method_combobox_model_.get());
953    eap_method_combobox_->set_listener(this);
954    eap_method_combobox_->SetEnabled(eap_method_ui_data_.editable());
955    layout->AddView(eap_method_combobox_);
956    layout->AddView(new ControlledSettingIndicatorView(eap_method_ui_data_));
957    layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
958
959    // Phase 2 authentication
960    layout->StartRow(0, column_view_set_id);
961    phase_2_auth_label_ = new views::Label(l10n_util::GetStringUTF16(
962        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PHASE_2_AUTH));
963    layout->AddView(phase_2_auth_label_);
964    phase_2_auth_combobox_model_.reset(
965        new internal::Phase2AuthComboboxModel(eap_method_combobox_));
966    phase_2_auth_combobox_ = new views::Combobox(
967        phase_2_auth_combobox_model_.get());
968    phase_2_auth_label_->SetEnabled(false);
969    phase_2_auth_combobox_->SetEnabled(false);
970    phase_2_auth_combobox_->set_listener(this);
971    layout->AddView(phase_2_auth_combobox_);
972    layout->AddView(new ControlledSettingIndicatorView(phase_2_auth_ui_data_));
973    layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
974
975    // Server CA certificate
976    layout->StartRow(0, column_view_set_id);
977    server_ca_cert_label_ = new views::Label(l10n_util::GetStringUTF16(
978        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_SERVER_CA));
979    layout->AddView(server_ca_cert_label_);
980    server_ca_cert_combobox_model_.reset(
981        new internal::ServerCACertComboboxModel(cert_library_));
982    server_ca_cert_combobox_ = new ComboboxWithWidth(
983        server_ca_cert_combobox_model_.get(),
984        ChildNetworkConfigView::kInputFieldMinWidth);
985    server_ca_cert_label_->SetEnabled(false);
986    server_ca_cert_combobox_->SetEnabled(false);
987    server_ca_cert_combobox_->set_listener(this);
988    layout->AddView(server_ca_cert_combobox_);
989    layout->AddView(
990        new ControlledSettingIndicatorView(server_ca_cert_ui_data_));
991    layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
992
993    // User certificate
994    layout->StartRow(0, column_view_set_id);
995    user_cert_label_ = new views::Label(l10n_util::GetStringUTF16(
996        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT));
997    layout->AddView(user_cert_label_);
998    user_cert_combobox_model_.reset(
999        new internal::UserCertComboboxModel(cert_library_));
1000    user_cert_combobox_ = new views::Combobox(user_cert_combobox_model_.get());
1001    user_cert_label_->SetEnabled(false);
1002    user_cert_combobox_->SetEnabled(false);
1003    user_cert_combobox_->set_listener(this);
1004    layout->AddView(user_cert_combobox_);
1005    layout->AddView(new ControlledSettingIndicatorView(user_cert_ui_data_));
1006    layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
1007
1008    // Identity
1009    layout->StartRow(0, column_view_set_id);
1010    identity_label_ = new views::Label(l10n_util::GetStringUTF16(
1011        IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_IDENTITY));
1012    layout->AddView(identity_label_);
1013    identity_textfield_ = new views::Textfield(
1014        views::Textfield::STYLE_DEFAULT);
1015    identity_textfield_->SetController(this);
1016    if (wifi && !wifi->identity().empty())
1017      identity_textfield_->SetText(UTF8ToUTF16(wifi->identity()));
1018    identity_textfield_->SetEnabled(identity_ui_data_.editable());
1019    layout->AddView(identity_textfield_);
1020    layout->AddView(new ControlledSettingIndicatorView(identity_ui_data_));
1021    layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
1022  }
1023
1024  // Passphrase input
1025  layout->StartRow(0, column_view_set_id);
1026  int label_text_id = IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE;
1027  passphrase_label_ = new views::Label(
1028      l10n_util::GetStringUTF16(label_text_id));
1029  layout->AddView(passphrase_label_);
1030  passphrase_textfield_ = new views::Textfield(
1031      views::Textfield::STYLE_OBSCURED);
1032  passphrase_textfield_->SetController(this);
1033  if (wifi && !wifi->GetPassphrase().empty())
1034    passphrase_textfield_->SetText(UTF8ToUTF16(wifi->GetPassphrase()));
1035  // Disable passphrase input initially for other network.
1036  passphrase_label_->SetEnabled(wifi != NULL);
1037  passphrase_textfield_->SetEnabled(wifi && passphrase_ui_data_.editable());
1038  passphrase_textfield_->SetAccessibleName(l10n_util::GetStringUTF16(
1039      label_text_id));
1040  layout->AddView(passphrase_textfield_);
1041
1042  if (passphrase_ui_data_.managed()) {
1043    layout->AddView(new ControlledSettingIndicatorView(passphrase_ui_data_));
1044  } else {
1045    // Password visible button.
1046    passphrase_visible_button_ = new views::ToggleImageButton(this);
1047    passphrase_visible_button_->SetTooltipText(
1048        l10n_util::GetStringUTF16(
1049            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_SHOW));
1050    passphrase_visible_button_->SetToggledTooltipText(
1051        l10n_util::GetStringUTF16(
1052            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_HIDE));
1053    passphrase_visible_button_->SetImage(
1054        views::ImageButton::BS_NORMAL,
1055        ResourceBundle::GetSharedInstance().
1056        GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD));
1057    passphrase_visible_button_->SetImage(
1058        views::ImageButton::BS_HOT,
1059        ResourceBundle::GetSharedInstance().
1060        GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD_HOVER));
1061    passphrase_visible_button_->SetToggledImage(
1062        views::ImageButton::BS_NORMAL,
1063        ResourceBundle::GetSharedInstance().
1064        GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD));
1065    passphrase_visible_button_->SetToggledImage(
1066        views::ImageButton::BS_HOT,
1067        ResourceBundle::GetSharedInstance().
1068        GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD_HOVER));
1069    passphrase_visible_button_->SetImageAlignment(
1070        views::ImageButton::ALIGN_CENTER, views::ImageButton::ALIGN_MIDDLE);
1071    layout->AddView(passphrase_visible_button_);
1072  }
1073
1074  layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
1075
1076  if (show_8021x) {
1077    // Anonymous identity
1078    layout->StartRow(0, column_view_set_id);
1079    identity_anonymous_label_ =
1080        new views::Label(l10n_util::GetStringUTF16(
1081            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_IDENTITY_ANONYMOUS));
1082    layout->AddView(identity_anonymous_label_);
1083    identity_anonymous_textfield_ = new views::Textfield(
1084        views::Textfield::STYLE_DEFAULT);
1085    identity_anonymous_label_->SetEnabled(false);
1086    identity_anonymous_textfield_->SetEnabled(false);
1087    identity_anonymous_textfield_->SetController(this);
1088    layout->AddView(identity_anonymous_textfield_);
1089    layout->AddView(
1090        new ControlledSettingIndicatorView(identity_anonymous_ui_data_));
1091    layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
1092  }
1093
1094  // Checkboxes.
1095
1096  // Save credentials
1097  if (show_8021x) {
1098    layout->StartRow(0, column_view_set_id);
1099    save_credentials_checkbox_ = new views::Checkbox(
1100        l10n_util::GetStringUTF16(
1101            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SAVE_CREDENTIALS));
1102    save_credentials_checkbox_->SetEnabled(
1103        save_credentials_ui_data_.editable());
1104    layout->SkipColumns(1);
1105    layout->AddView(save_credentials_checkbox_);
1106    layout->AddView(
1107        new ControlledSettingIndicatorView(save_credentials_ui_data_));
1108  }
1109
1110  // Share network
1111  if (!wifi ||
1112      (wifi->profile_type() == PROFILE_NONE &&
1113       wifi->IsPassphraseRequired() &&
1114       !wifi->RequiresUserProfile())) {
1115    layout->StartRow(0, column_view_set_id);
1116    share_network_checkbox_ = new views::Checkbox(
1117        l10n_util::GetStringUTF16(
1118            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SHARE_NETWORK));
1119    layout->SkipColumns(1);
1120    layout->AddView(share_network_checkbox_);
1121  }
1122  layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
1123
1124  // Create an error label.
1125  layout->StartRow(0, column_view_set_id);
1126  layout->SkipColumns(1);
1127  error_label_ = new views::Label();
1128  error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1129  error_label_->SetEnabledColor(SK_ColorRED);
1130  layout->AddView(error_label_);
1131
1132  // Initialize the field and checkbox values.
1133
1134  // After creating the fields, we set the values. Fields need to be created
1135  // first because RefreshEapFields() will enable/disable them as appropriate.
1136  if (show_8021x) {
1137    EAPMethod eap_method = (wifi ? wifi->eap_method() : EAP_METHOD_UNKNOWN);
1138    switch (eap_method) {
1139      case EAP_METHOD_PEAP:
1140        eap_method_combobox_->SetSelectedIndex(EAP_METHOD_INDEX_PEAP);
1141        break;
1142      case EAP_METHOD_TTLS:
1143        eap_method_combobox_->SetSelectedIndex(EAP_METHOD_INDEX_TTLS);
1144        break;
1145      case EAP_METHOD_TLS:
1146        eap_method_combobox_->SetSelectedIndex(EAP_METHOD_INDEX_TLS);
1147        break;
1148      case EAP_METHOD_LEAP:
1149        eap_method_combobox_->SetSelectedIndex(EAP_METHOD_INDEX_LEAP);
1150        break;
1151      default:
1152        break;
1153    }
1154    RefreshEapFields();
1155
1156    // Phase 2 authentication and anonymous identity.
1157    if (Phase2AuthActive()) {
1158      EAPPhase2Auth eap_phase_2_auth =
1159          (wifi ? wifi->eap_phase_2_auth() : EAP_PHASE_2_AUTH_AUTO);
1160      switch (eap_phase_2_auth) {
1161        case EAP_PHASE_2_AUTH_MD5:
1162          phase_2_auth_combobox_->SetSelectedIndex(PHASE_2_AUTH_INDEX_MD5);
1163          break;
1164        case EAP_PHASE_2_AUTH_MSCHAPV2:
1165          phase_2_auth_combobox_->SetSelectedIndex(PHASE_2_AUTH_INDEX_MSCHAPV2);
1166          break;
1167        case EAP_PHASE_2_AUTH_MSCHAP:
1168          phase_2_auth_combobox_->SetSelectedIndex(PHASE_2_AUTH_INDEX_MSCHAP);
1169          break;
1170        case EAP_PHASE_2_AUTH_PAP:
1171          phase_2_auth_combobox_->SetSelectedIndex(PHASE_2_AUTH_INDEX_PAP);
1172          break;
1173        case EAP_PHASE_2_AUTH_CHAP:
1174          phase_2_auth_combobox_->SetSelectedIndex(PHASE_2_AUTH_INDEX_CHAP);
1175          break;
1176        default:
1177          break;
1178      }
1179
1180      const std::string& eap_anonymous_identity =
1181          (wifi ? wifi->eap_anonymous_identity() : std::string());
1182      identity_anonymous_textfield_->SetText(
1183          UTF8ToUTF16(eap_anonymous_identity));
1184    }
1185
1186    // Server CA certificate.
1187    if (CaCertActive()) {
1188      const std::string& nss_nickname =
1189          (wifi ? wifi->eap_server_ca_cert_nss_nickname() : std::string());
1190      if (nss_nickname.empty()) {
1191        if (wifi->eap_use_system_cas()) {
1192          // "Default".
1193          server_ca_cert_combobox_->SetSelectedIndex(0);
1194        } else {
1195          // "Do not check".
1196          server_ca_cert_combobox_->SetSelectedIndex(
1197              server_ca_cert_combobox_->model()->GetItemCount() - 1);
1198        }
1199      } else {
1200        // Select the certificate if available.
1201        int cert_index =
1202            cert_library_->GetCACertificates().FindCertByNickname(nss_nickname);
1203        if (cert_index >= 0) {
1204          // Skip item for "Default".
1205          server_ca_cert_combobox_->SetSelectedIndex(1 + cert_index);
1206        }
1207      }
1208    }
1209
1210    // User certificate.
1211    if (UserCertActive()) {
1212      const std::string& pkcs11_id =
1213          (wifi ? wifi->eap_client_cert_pkcs11_id() : std::string());
1214      if (!pkcs11_id.empty()) {
1215        int cert_index =
1216            cert_library_->GetUserCertificates().FindCertByPkcs11Id(pkcs11_id);
1217        if (cert_index >= 0) {
1218          user_cert_combobox_->SetSelectedIndex(cert_index);
1219        }
1220      }
1221    }
1222
1223    // Identity is always active.
1224    const std::string& eap_identity =
1225        (wifi ? wifi->eap_identity() : std::string());
1226    identity_textfield_->SetText(UTF8ToUTF16(eap_identity));
1227
1228    // Passphrase
1229    if (PassphraseActive()) {
1230      const std::string& eap_passphrase =
1231          (wifi ? wifi->eap_passphrase() : std::string());
1232      passphrase_textfield_->SetText(UTF8ToUTF16(eap_passphrase));
1233    }
1234
1235    // Save credentials
1236    bool save_credentials = (wifi ? wifi->save_credentials() : false);
1237    save_credentials_checkbox_->SetChecked(save_credentials);
1238  }
1239
1240  RefreshShareCheckbox();
1241  UpdateErrorLabel();
1242}
1243
1244void WifiConfigView::InitFocus() {
1245  views::View* view_to_focus = GetInitiallyFocusedView();
1246  if (view_to_focus)
1247    view_to_focus->RequestFocus();
1248}
1249
1250// static
1251void WifiConfigView::ParseWiFiUIProperty(
1252    NetworkPropertyUIData* property_ui_data,
1253    Network* network,
1254    const std::string& key) {
1255  NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
1256  property_ui_data->ParseOncProperty(
1257      network->ui_data(),
1258      network_library->FindOncForNetwork(network->unique_id()),
1259      base::StringPrintf("%s.%s", onc::kWiFi, key.c_str()));
1260}
1261
1262// static
1263void WifiConfigView::ParseWiFiEAPUIProperty(
1264    NetworkPropertyUIData* property_ui_data,
1265    Network* network,
1266    const std::string& key) {
1267  ParseWiFiUIProperty(
1268      property_ui_data, network,
1269      base::StringPrintf("%s.%s", onc::wifi::kEAP, key.c_str()));
1270}
1271
1272}  // namespace chromeos
1273