network_connection_handler.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
6#define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
7
8#include <set>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/memory/weak_ptr.h"
14#include "base/values.h"
15#include "chromeos/chromeos_export.h"
16#include "chromeos/dbus/dbus_method_call_status.h"
17#include "chromeos/login/login_state.h"
18#include "chromeos/network/cert_loader.h"
19#include "chromeos/network/network_handler.h"
20#include "chromeos/network/network_handler_callbacks.h"
21#include "chromeos/network/network_state_handler_observer.h"
22
23namespace chromeos {
24
25class NetworkState;
26class NetworkUIData;
27
28// The NetworkConnectionHandler class is used to manage network connection
29// requests. This is the only class that should make Shill Connect calls.
30// It handles the following steps:
31// 1. Determine whether or not sufficient information (e.g. passphrase) is
32//    known to be available to connect to the network.
33// 2. Request additional information (e.g. user data which contains certificate
34//    information) and determine whether sufficient information is available.
35// 3. Possibly configure the network certificate info (tpm slot and pkcs11 id).
36// 4. Send the connect request.
37// 5. Wait for the network state to change to a non connecting state.
38// 6. Invoke the appropriate callback (always) on success or failure.
39//
40// NetworkConnectionHandler depends on NetworkStateHandler for immediately
41// available State information, and NetworkConfigurationHandler for any
42// configuration calls.
43
44class CHROMEOS_EXPORT NetworkConnectionHandler
45    : public LoginState::Observer,
46      public CertLoader::Observer,
47      public NetworkStateHandlerObserver,
48      public base::SupportsWeakPtr<NetworkConnectionHandler> {
49 public:
50  // Constants for |error_name| from |error_callback| for Connect.
51  static const char kErrorNotFound[];
52  static const char kErrorConnected[];
53  static const char kErrorConnecting[];
54  static const char kErrorPassphraseRequired[];
55  static const char kErrorActivationRequired[];
56  static const char kErrorCertificateRequired[];
57  static const char kErrorConfigurationRequired[];
58  static const char kErrorShillError[];
59  static const char kErrorConnectFailed[];
60  static const char kErrorUnknown[];
61
62  // Constants for |error_name| from |error_callback| for Disconnect.
63  static const char kErrorNotConnected[];
64
65  virtual ~NetworkConnectionHandler();
66
67  // ConnectToNetwork() will start an asynchronous connection attempt.
68  // On success, |success_callback| will be called.
69  // On failure, |error_callback| will be called with |error_name| one of:
70  //  kErrorNotFound if no network matching |service_path| is found
71  //    (hidden networks must be configured before connecting).
72  //  kErrorConnected if already connected to the network.
73  //  kErrorConnecting if already connecting to the network.
74  //  kErrorCertificateRequired if the network requires a cert and none exists.
75  //  kErrorPassphraseRequired if passphrase only is required.
76  //  kErrorConfigurationRequired if additional configuration is required.
77  //  kErrorShillError if a DBus or Shill error occurred.
78  // |error_message| will contain an additional error string for debugging.
79  // If |ignore_error_state| is true, error state for the network is ignored
80  //  (e.g. for repeat attempts).
81  void ConnectToNetwork(const std::string& service_path,
82                        const base::Closure& success_callback,
83                        const network_handler::ErrorCallback& error_callback,
84                        bool ignore_error_state);
85
86  // DisconnectToNetwork() will send a Disconnect request to Shill.
87  // On success, |success_callback| will be called.
88  // On failure, |error_callback| will be called with |error_name| one of:
89  //  kErrorNotFound if no network matching |service_path| is found.
90  //  kErrorNotConnected if not connected to the network.
91  //  kErrorShillError if a DBus or Shill error occurred.
92  // |error_message| will contain and additional error string for debugging.
93  void DisconnectNetwork(const std::string& service_path,
94                         const base::Closure& success_callback,
95                         const network_handler::ErrorCallback& error_callback);
96
97  // Returns true if ConnectToNetwork has been called with |service_path| and
98  // has not completed (i.e. success or error callback has been called).
99  bool HasConnectingNetwork(const std::string& service_path);
100
101  // NetworkStateHandlerObserver
102  virtual void NetworkListChanged() OVERRIDE;
103  virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE;
104
105  // LoginState::Observer
106  virtual void LoggedInStateChanged(LoginState::LoggedInState state) OVERRIDE;
107
108  // CertLoader::Observer
109  virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
110                                    bool initial_load) OVERRIDE;
111
112 private:
113  friend class NetworkHandler;
114  friend class NetworkConnectionHandlerTest;
115
116  struct ConnectRequest;
117
118  NetworkConnectionHandler();
119
120  void Init(CertLoader* cert_loader,
121            NetworkStateHandler* network_state_handler,
122            NetworkConfigurationHandler* network_configuration_handler);
123
124  ConnectRequest* pending_request(const std::string& service_path);
125
126  // Callback from Shill.Service.GetProperties. Parses |properties| to verify
127  // whether or not the network appears to be configured. If configured,
128  // attempts a connection, otherwise invokes error_callback from
129  // pending_requests_[service_path].
130  void VerifyConfiguredAndConnect(const std::string& service_path,
131                                  const base::DictionaryValue& properties);
132
133  // Calls Shill.Manager.Connect asynchronously.
134  void CallShillConnect(const std::string& service_path);
135
136  // Handle failure from ConfigurationHandler calls.
137  void HandleConfigurationFailure(
138      const std::string& service_path,
139      const std::string& error_name,
140      scoped_ptr<base::DictionaryValue> error_data);
141
142  // Handle success or failure from Shill.Service.Connect.
143  void HandleShillConnectSuccess(const std::string& service_path);
144  void HandleShillConnectFailure(const std::string& service_path,
145                                 const std::string& error_name,
146                                 const std::string& error_message);
147
148  void CheckPendingRequest(const std::string service_path);
149  void CheckAllPendingRequests();
150  bool CertificateIsConfigured(NetworkUIData* ui_data, std::string* pkcs11_id);
151  void ErrorCallbackForPendingRequest(const std::string& service_path,
152                                      const std::string& error_name);
153
154  // Calls Shill.Manager.Disconnect asynchronously.
155  void CallShillDisconnect(
156      const std::string& service_path,
157      const base::Closure& success_callback,
158      const network_handler::ErrorCallback& error_callback);
159
160  // Handle success or failure from Shill.Service.Disconnect.
161  void HandleShillDisconnectSuccess(const std::string& service_path,
162                                    const base::Closure& success_callback);
163  void HandleShillDisconnectFailure(
164      const std::string& service_path,
165      const network_handler::ErrorCallback& error_callback,
166      const std::string& error_name,
167      const std::string& error_message);
168
169  // Local references to the associated handler instances.
170  CertLoader* cert_loader_;
171  NetworkStateHandler* network_state_handler_;
172  NetworkProfileHandler* network_profile_handler_;
173  NetworkConfigurationHandler* network_configuration_handler_;
174
175  // Map of pending connect requests, used to prevent repeated attempts while
176  // waiting for Shill and to trigger callbacks on eventual success or failure.
177  std::map<std::string, ConnectRequest> pending_requests_;
178  scoped_ptr<ConnectRequest> queued_connect_;
179
180  // Track certificate loading state.
181  bool logged_in_;
182  bool certificates_loaded_;
183
184  DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
185};
186
187}  // namespace chromeos
188
189#endif  // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
190