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#ifndef CHROME_BROWSER_CHROMEOS_OPTIONS_VPN_CONFIG_VIEW_H_
6#define CHROME_BROWSER_CHROMEOS_OPTIONS_VPN_CONFIG_VIEW_H_
7
8#include <string>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/strings/string16.h"
12#include "chrome/browser/chromeos/options/cert_library.h"
13#include "chrome/browser/chromeos/options/network_config_view.h"
14#include "chrome/browser/chromeos/options/network_property_ui_data.h"
15#include "chrome/browser/chromeos/options/passphrase_textfield.h"
16#include "ui/views/controls/button/button.h"
17#include "ui/views/controls/combobox/combobox_listener.h"
18#include "ui/views/controls/textfield/textfield_controller.h"
19#include "ui/views/view.h"
20
21namespace base {
22class DictionaryValue;
23}
24
25namespace views {
26class Checkbox;
27class GridLayout;
28class Label;
29}
30
31namespace chromeos {
32
33class NetworkState;
34
35namespace internal {
36class ProviderTypeComboboxModel;
37class VpnServerCACertComboboxModel;
38class VpnUserCertComboboxModel;
39}
40
41// A dialog box to allow configuration of VPN connection.
42class VPNConfigView : public ChildNetworkConfigView,
43                      public views::TextfieldController,
44                      public views::ButtonListener,
45                      public views::ComboboxListener,
46                      public CertLibrary::Observer {
47 public:
48  VPNConfigView(NetworkConfigView* parent, const std::string& service_path);
49  virtual ~VPNConfigView();
50
51  // views::TextfieldController:
52  virtual void ContentsChanged(views::Textfield* sender,
53                               const base::string16& new_contents) OVERRIDE;
54  virtual bool HandleKeyEvent(views::Textfield* sender,
55                              const ui::KeyEvent& key_event) OVERRIDE;
56
57  // views::ButtonListener:
58  virtual void ButtonPressed(views::Button* sender,
59                             const ui::Event& event) OVERRIDE;
60
61  // views::ComboboxListener:
62  virtual void OnPerformAction(views::Combobox* combobox) OVERRIDE;
63
64  // CertLibrary::Observer:
65  virtual void OnCertificatesLoaded(bool initial_load) OVERRIDE;
66
67  // ChildNetworkConfigView:
68  virtual base::string16 GetTitle() const OVERRIDE;
69  virtual views::View* GetInitiallyFocusedView() OVERRIDE;
70  virtual bool CanLogin() OVERRIDE;
71  virtual bool Login() OVERRIDE;
72  virtual void Cancel() OVERRIDE;
73  virtual void InitFocus() OVERRIDE;
74
75 private:
76  // Initializes data members and create UI controls.
77  void Init();
78
79  // Callback to initialize fields from uncached network properties.
80  void InitFromProperties(const std::string& service_path,
81                          const base::DictionaryValue& dictionary);
82  void ParseUIProperties(const NetworkState* vpn);
83  void GetPropertiesError(const std::string& error_name,
84                          scoped_ptr<base::DictionaryValue> error_data);
85
86  // Helper function to set configurable properties.
87  void SetConfigProperties(base::DictionaryValue* properties);
88
89  // Set and update all control values.
90  void Refresh();
91
92  // Update various controls.
93  void UpdateControlsToEnable();
94  void UpdateControls();
95  void UpdateErrorLabel();
96
97  // Update state of the Login button.
98  void UpdateCanLogin();
99
100  // Returns true if there is at least one user certificate installed.
101  bool HaveUserCerts() const;
102
103  // Returns true if there is a selected user certificate and it is valid.
104  bool IsUserCertValid() const;
105
106  // Get text from input field.
107  const std::string GetTextFromField(views::Textfield* textfield,
108                                     bool trim_whitespace) const;
109
110  // Get passphrase from input field.
111  const std::string GetPassphraseFromField(
112      PassphraseTextfield* textfield) const;
113
114  // Convenience methods to get text from input field or cached VirtualNetwork.
115  const std::string GetService() const;
116  const std::string GetServer() const;
117  const std::string GetPSKPassphrase() const;
118  const std::string GetUsername() const;
119  const std::string GetUserPassphrase() const;
120  const std::string GetOTP() const;
121  const std::string GetGroupName() const;
122  const std::string GetServerCACertPEM() const;
123  const std::string GetUserCertID() const;
124  bool GetSaveCredentials() const;
125  int GetProviderTypeIndex() const;
126  std::string GetProviderTypeString() const;
127
128  // Parses a VPN UI |property| from the given |network|. |key| is the property
129  // name within the type-specific VPN subdictionary named |dict_key|.
130  void ParseVPNUIProperty(const NetworkState* network,
131                          const std::string& dict_key,
132                          const std::string& key,
133                          NetworkPropertyUIData* property_ui_data);
134
135  base::string16 service_name_from_server_;
136  bool service_text_modified_;
137
138  // Initialized in Init():
139
140  bool enable_psk_passphrase_;
141  bool enable_user_cert_;
142  bool enable_server_ca_cert_;
143  bool enable_otp_;
144  bool enable_group_name_;
145
146  NetworkPropertyUIData ca_cert_ui_data_;
147  NetworkPropertyUIData psk_passphrase_ui_data_;
148  NetworkPropertyUIData user_cert_ui_data_;
149  NetworkPropertyUIData username_ui_data_;
150  NetworkPropertyUIData user_passphrase_ui_data_;
151  NetworkPropertyUIData group_name_ui_data_;
152  NetworkPropertyUIData save_credentials_ui_data_;
153
154  int title_;
155
156  views::GridLayout* layout_;
157  views::Textfield* server_textfield_;
158  views::Label* service_text_;
159  views::Textfield* service_textfield_;
160  scoped_ptr<internal::ProviderTypeComboboxModel> provider_type_combobox_model_;
161  views::Combobox* provider_type_combobox_;
162  views::Label* provider_type_text_label_;
163  views::Label* psk_passphrase_label_;
164  PassphraseTextfield* psk_passphrase_textfield_;
165  views::Label* user_cert_label_;
166  scoped_ptr<internal::VpnUserCertComboboxModel> user_cert_combobox_model_;
167  views::Combobox* user_cert_combobox_;
168  views::Label* server_ca_cert_label_;
169  scoped_ptr<internal::VpnServerCACertComboboxModel>
170      server_ca_cert_combobox_model_;
171  views::Combobox* server_ca_cert_combobox_;
172  views::Textfield* username_textfield_;
173  PassphraseTextfield* user_passphrase_textfield_;
174  views::Label* otp_label_;
175  views::Textfield* otp_textfield_;
176  views::Label* group_name_label_;
177  views::Textfield* group_name_textfield_;
178  views::Checkbox* save_credentials_checkbox_;
179  views::Label* error_label_;
180
181  // Cached VPN properties, only set when configuring an existing network.
182  int provider_type_index_;
183  std::string ca_cert_pem_;
184  std::string client_cert_id_;
185
186  base::WeakPtrFactory<VPNConfigView> weak_ptr_factory_;
187
188  DISALLOW_COPY_AND_ASSIGN(VPNConfigView);
189};
190
191}  // namespace chromeos
192
193#endif  // CHROME_BROWSER_CHROMEOS_OPTIONS_VPN_CONFIG_VIEW_H_
194