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