network_connection_handler.h revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 kErrorDisconnectFailed[];
61  static const char kErrorUnknown[];
62
63  // Constants for |error_name| from |error_callback| for Disconnect.
64  static const char kErrorNotConnected[];
65
66  virtual ~NetworkConnectionHandler();
67
68  // ConnectToNetwork() will start an asynchronous connection attempt.
69  // On success, |success_callback| will be called.
70  // On failure, |error_callback| will be called with |error_name| one of:
71  //  kErrorNotFound if no network matching |service_path| is found
72  //    (hidden networks must be configured before connecting).
73  //  kErrorConnected if already connected to the network.
74  //  kErrorConnecting if already connecting to the network.
75  //  kErrorCertificateRequired if the network requires a cert and none exists.
76  //  kErrorPassphraseRequired if passphrase only is required.
77  //  kErrorConfigurationRequired if additional configuration is required.
78  //  kErrorShillError if a DBus or Shill error occurred.
79  // |error_message| will contain an additional error string for debugging.
80  // If |ignore_error_state| is true, error state for the network is ignored
81  //  (e.g. for repeat attempts).
82  void ConnectToNetwork(const std::string& service_path,
83                        const base::Closure& success_callback,
84                        const network_handler::ErrorCallback& error_callback,
85                        bool ignore_error_state);
86
87  // DisconnectNetwork() will send a Disconnect request to Shill.
88  // On success, |success_callback| will be called.
89  // On failure, |error_callback| will be called with |error_name| one of:
90  //  kErrorNotFound if no network matching |service_path| is found.
91  //  kErrorNotConnected if not connected to the network.
92  //  kErrorShillError if a DBus or Shill error occurred.
93  // |error_message| will contain and additional error string for debugging.
94  void DisconnectNetwork(const std::string& service_path,
95                         const base::Closure& success_callback,
96                         const network_handler::ErrorCallback& error_callback);
97
98  // Returns true if ConnectToNetwork has been called with |service_path| and
99  // has not completed (i.e. success or error callback has been called).
100  bool HasConnectingNetwork(const std::string& service_path);
101
102  // NetworkStateHandlerObserver
103  virtual void NetworkListChanged() OVERRIDE;
104  virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE;
105
106  // LoginState::Observer
107  virtual void LoggedInStateChanged(LoginState::LoggedInState state) OVERRIDE;
108
109  // CertLoader::Observer
110  virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
111                                    bool initial_load) OVERRIDE;
112
113 private:
114  friend class NetworkHandler;
115  friend class NetworkConnectionHandlerTest;
116
117  struct ConnectRequest;
118
119  NetworkConnectionHandler();
120
121  void Init(CertLoader* cert_loader,
122            NetworkStateHandler* network_state_handler,
123            NetworkConfigurationHandler* network_configuration_handler);
124
125  ConnectRequest* pending_request(const std::string& service_path);
126
127  // Callback from Shill.Service.GetProperties. Parses |properties| to verify
128  // whether or not the network appears to be configured. If configured,
129  // attempts a connection, otherwise invokes error_callback from
130  // pending_requests_[service_path].
131  void VerifyConfiguredAndConnect(const std::string& service_path,
132                                  const base::DictionaryValue& properties);
133
134  // Calls Shill.Manager.Connect asynchronously.
135  void CallShillConnect(const std::string& service_path);
136
137  // Handle failure from ConfigurationHandler calls.
138  void HandleConfigurationFailure(
139      const std::string& service_path,
140      const std::string& error_name,
141      scoped_ptr<base::DictionaryValue> error_data);
142
143  // Handle success or failure from Shill.Service.Connect.
144  void HandleShillConnectSuccess(const std::string& service_path);
145  void HandleShillConnectFailure(const std::string& service_path,
146                                 const std::string& error_name,
147                                 const std::string& error_message);
148
149  void CheckPendingRequest(const std::string service_path);
150  void CheckAllPendingRequests();
151  bool CertificateIsConfigured(NetworkUIData* ui_data, std::string* pkcs11_id);
152  void ErrorCallbackForPendingRequest(const std::string& service_path,
153                                      const std::string& error_name);
154
155  // Calls Shill.Manager.Disconnect asynchronously.
156  void CallShillDisconnect(
157      const std::string& service_path,
158      const base::Closure& success_callback,
159      const network_handler::ErrorCallback& error_callback);
160
161  // Handle success or failure from Shill.Service.Disconnect.
162  void HandleShillDisconnectSuccess(const std::string& service_path,
163                                    const base::Closure& success_callback);
164
165  // Local references to the associated handler instances.
166  CertLoader* cert_loader_;
167  NetworkStateHandler* network_state_handler_;
168  NetworkConfigurationHandler* network_configuration_handler_;
169
170  // Map of pending connect requests, used to prevent repeated attempts while
171  // waiting for Shill and to trigger callbacks on eventual success or failure.
172  std::map<std::string, ConnectRequest> pending_requests_;
173  scoped_ptr<ConnectRequest> queued_connect_;
174
175  // Track certificate loading state.
176  bool logged_in_;
177  bool certificates_loaded_;
178
179  DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
180};
181
182}  // namespace chromeos
183
184#endif  // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
185