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.
5 #ifndef CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
6 #define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
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/cert_loader.h"
16 #include "chromeos/chromeos_export.h"
17 #include "chromeos/dbus/dbus_method_call_status.h"
18 #include "chromeos/login/login_state.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"
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.
40 // NetworkConnectionHandler depends on NetworkStateHandler for immediately
41 // available State information, and NetworkConfigurationHandler for any
42 // configuration calls.
44 class CHROMEOS_EXPORT NetworkConnectionHandler
45 : public LoginState::Observer
,
46 public CertLoader::Observer
,
47 public NetworkStateHandlerObserver
,
48 public base::SupportsWeakPtr
<NetworkConnectionHandler
> {
50 // Constants for |error_name| from |error_callback| for Connect.
52 // No network matching |service_path| is found (hidden networks must be
53 // configured before connecting).
54 static const char kErrorNotFound
[];
56 // Already connected to the network.
57 static const char kErrorConnected
[];
59 // Already connecting to the network.
60 static const char kErrorConnecting
[];
62 // The passphrase is missing or invalid.
63 static const char kErrorPassphraseRequired
[];
65 static const char kErrorActivationRequired
[];
67 // The network requires a cert and none exists.
68 static const char kErrorCertificateRequired
[];
70 // The network had an authentication error, indicating that additional or
71 // different authentication information is required.
72 static const char kErrorAuthenticationRequired
[];
74 // Additional configuration is required.
75 static const char kErrorConfigurationRequired
[];
77 // Configuration failed during the configure stage of the connect flow.
78 static const char kErrorConfigureFailed
[];
80 // For Disconnect or Activate, an unexpected DBus or Shill error occurred.
81 static const char kErrorShillError
[];
83 // A new network connect request canceled this one.
84 static const char kErrorConnectCanceled
[];
86 // Constants for |error_name| from |error_callback| for Disconnect.
87 static const char kErrorNotConnected
[];
89 virtual ~NetworkConnectionHandler();
91 // ConnectToNetwork() will start an asynchronous connection attempt.
92 // On success, |success_callback| will be called.
93 // On failure, |error_callback| will be called with |error_name| one of the
94 // constants defined above, or shill::kErrorConnectFailed or
95 // shill::kErrorBadPassphrase if the Shill Error property (from a
96 // previous connect attempt) was set to one of those.
97 // |error_message| will contain an additional error string for debugging.
98 // If |check_error_state| is true, the current state of the network is
99 // checked for errors, otherwise current state is ignored (e.g. for recently
100 // configured networks or repeat attempts).
101 void ConnectToNetwork(const std::string
& service_path
,
102 const base::Closure
& success_callback
,
103 const network_handler::ErrorCallback
& error_callback
,
104 bool check_error_state
);
106 // DisconnectNetwork() will send a Disconnect request to Shill.
107 // On success, |success_callback| will be called.
108 // On failure, |error_callback| will be called with |error_name| one of:
109 // kErrorNotFound if no network matching |service_path| is found.
110 // kErrorNotConnected if not connected to the network.
111 // kErrorShillError if a DBus or Shill error occurred.
112 // |error_message| will contain and additional error string for debugging.
113 void DisconnectNetwork(const std::string
& service_path
,
114 const base::Closure
& success_callback
,
115 const network_handler::ErrorCallback
& error_callback
);
117 // Returns true if ConnectToNetwork has been called with |service_path| and
118 // has not completed (i.e. success or error callback has been called).
119 bool HasConnectingNetwork(const std::string
& service_path
);
121 // Returns true if there are any pending connect requests.
122 bool HasPendingConnectRequest();
124 // NetworkStateHandlerObserver
125 virtual void NetworkListChanged() OVERRIDE
;
126 virtual void NetworkPropertiesUpdated(const NetworkState
* network
) OVERRIDE
;
128 // LoginState::Observer
129 virtual void LoggedInStateChanged() OVERRIDE
;
131 // CertLoader::Observer
132 virtual void OnCertificatesLoaded(const net::CertificateList
& cert_list
,
133 bool initial_load
) OVERRIDE
;
136 friend class NetworkHandler
;
137 friend class NetworkConnectionHandlerTest
;
139 struct ConnectRequest
;
141 NetworkConnectionHandler();
143 void Init(NetworkStateHandler
* network_state_handler
,
144 NetworkConfigurationHandler
* network_configuration_handler
);
146 ConnectRequest
* GetPendingRequest(const std::string
& service_path
);
148 // Callback from Shill.Service.GetProperties. Parses |properties| to verify
149 // whether or not the network appears to be configured. If configured,
150 // attempts a connection, otherwise invokes error_callback from
151 // pending_requests_[service_path]. |check_error_state| is passed from
152 // ConnectToNetwork(), see comment for info.
153 void VerifyConfiguredAndConnect(bool check_error_state
,
154 const std::string
& service_path
,
155 const base::DictionaryValue
& properties
);
157 // Calls Shill.Manager.Connect asynchronously.
158 void CallShillConnect(const std::string
& service_path
);
160 // Handle failure from ConfigurationHandler calls.
161 void HandleConfigurationFailure(
162 const std::string
& service_path
,
163 const std::string
& error_name
,
164 scoped_ptr
<base::DictionaryValue
> error_data
);
166 // Handle success or failure from Shill.Service.Connect.
167 void HandleShillConnectSuccess(const std::string
& service_path
);
168 void HandleShillConnectFailure(const std::string
& service_path
,
169 const std::string
& error_name
,
170 const std::string
& error_message
);
172 void CheckPendingRequest(const std::string service_path
);
173 void CheckAllPendingRequests();
175 // Returns the PKCS#11 ID of a cert matching the certificate pattern in
176 // |ui_data|. Returns empty string otherwise.
177 std::string
CertificateIsConfigured(NetworkUIData
* ui_data
);
178 void ErrorCallbackForPendingRequest(const std::string
& service_path
,
179 const std::string
& error_name
);
181 // Calls Shill.Manager.Disconnect asynchronously.
182 void CallShillDisconnect(
183 const std::string
& service_path
,
184 const base::Closure
& success_callback
,
185 const network_handler::ErrorCallback
& error_callback
);
187 // Handle success from Shill.Service.Disconnect.
188 void HandleShillDisconnectSuccess(const std::string
& service_path
,
189 const base::Closure
& success_callback
);
192 // Local references to the associated handler instances.
193 CertLoader
* cert_loader_
;
194 NetworkStateHandler
* network_state_handler_
;
195 NetworkConfigurationHandler
* network_configuration_handler_
;
197 // Map of pending connect requests, used to prevent repeated attempts while
198 // waiting for Shill and to trigger callbacks on eventual success or failure.
199 std::map
<std::string
, ConnectRequest
> pending_requests_
;
200 scoped_ptr
<ConnectRequest
> queued_connect_
;
202 // Track certificate loading state.
204 bool certificates_loaded_
;
206 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler
);
209 } // namespace chromeos
211 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_