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_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "chromeos/cert_loader.h"
19 #include "chromeos/chromeos_export.h"
20 #include "chromeos/dbus/dbus_method_call_status.h"
21 #include "chromeos/login/login_state.h"
22 #include "chromeos/network/network_handler.h"
23 #include "chromeos/network/network_handler_callbacks.h"
24 #include "chromeos/network/network_state_handler_observer.h"
30 // The NetworkConnectionHandler class is used to manage network connection
31 // requests. This is the only class that should make Shill Connect calls.
32 // It handles the following steps:
33 // 1. Determine whether or not sufficient information (e.g. passphrase) is
34 // known to be available to connect to the network.
35 // 2. Request additional information (e.g. user data which contains certificate
36 // information) and determine whether sufficient information is available.
37 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id).
38 // 4. Send the connect request.
39 // 5. Wait for the network state to change to a non connecting state.
40 // 6. Invoke the appropriate callback (always) on success or failure.
42 // NetworkConnectionHandler depends on NetworkStateHandler for immediately
43 // available State information, and NetworkConfigurationHandler for any
44 // configuration calls.
46 class CHROMEOS_EXPORT NetworkConnectionHandler
47 : public LoginState::Observer
,
48 public CertLoader::Observer
,
49 public NetworkStateHandlerObserver
,
50 public base::SupportsWeakPtr
<NetworkConnectionHandler
> {
54 // Called if a connection to network |service_path| was requested, by
55 // calling ConnectToNetwork.
56 virtual void ConnectToNetworkRequested(const std::string
& service_path
) = 0;
59 virtual ~Observer() {}
62 DISALLOW_ASSIGN(Observer
);
65 // Constants for |error_name| from |error_callback| for Connect.
67 // No network matching |service_path| is found (hidden networks must be
68 // configured before connecting).
69 static const char kErrorNotFound
[];
71 // Already connected to the network.
72 static const char kErrorConnected
[];
74 // Already connecting to the network.
75 static const char kErrorConnecting
[];
77 // The passphrase is missing or invalid.
78 static const char kErrorPassphraseRequired
[];
80 static const char kErrorActivationRequired
[];
82 // The network requires a cert and none exists.
83 static const char kErrorCertificateRequired
[];
85 // The network had an authentication error, indicating that additional or
86 // different authentication information is required.
87 static const char kErrorAuthenticationRequired
[];
89 // Additional configuration is required.
90 static const char kErrorConfigurationRequired
[];
92 // Configuration failed during the configure stage of the connect flow.
93 static const char kErrorConfigureFailed
[];
95 // For Disconnect or Activate, an unexpected DBus or Shill error occurred.
96 static const char kErrorShillError
[];
98 // A new network connect request canceled this one.
99 static const char kErrorConnectCanceled
[];
101 // Constants for |error_name| from |error_callback| for Disconnect.
102 static const char kErrorNotConnected
[];
104 // Certificate load timed out.
105 static const char kErrorCertLoadTimeout
[];
107 virtual ~NetworkConnectionHandler();
109 void AddObserver(Observer
* observer
);
110 void RemoveObserver(Observer
* observer
);
112 // ConnectToNetwork() will start an asynchronous connection attempt.
113 // On success, |success_callback| will be called.
114 // On failure, |error_callback| will be called with |error_name| one of the
115 // constants defined above, or shill::kErrorConnectFailed or
116 // shill::kErrorBadPassphrase if the Shill Error property (from a
117 // previous connect attempt) was set to one of those.
118 // |error_message| will contain an additional error string for debugging.
119 // If |check_error_state| is true, the current state of the network is
120 // checked for errors, otherwise current state is ignored (e.g. for recently
121 // configured networks or repeat attempts).
122 void ConnectToNetwork(const std::string
& service_path
,
123 const base::Closure
& success_callback
,
124 const network_handler::ErrorCallback
& error_callback
,
125 bool check_error_state
);
127 // DisconnectNetwork() will send a Disconnect request to Shill.
128 // On success, |success_callback| will be called.
129 // On failure, |error_callback| will be called with |error_name| one of:
130 // kErrorNotFound if no network matching |service_path| is found.
131 // kErrorNotConnected if not connected to the network.
132 // kErrorShillError if a DBus or Shill error occurred.
133 // |error_message| will contain and additional error string for debugging.
134 void DisconnectNetwork(const std::string
& service_path
,
135 const base::Closure
& success_callback
,
136 const network_handler::ErrorCallback
& error_callback
);
138 // Returns true if ConnectToNetwork has been called with |service_path| and
139 // has not completed (i.e. success or error callback has been called).
140 bool HasConnectingNetwork(const std::string
& service_path
);
142 // Returns true if there are any pending connect requests.
143 bool HasPendingConnectRequest();
145 // NetworkStateHandlerObserver
146 virtual void NetworkListChanged() override
;
147 virtual void NetworkPropertiesUpdated(const NetworkState
* network
) override
;
149 // LoginState::Observer
150 virtual void LoggedInStateChanged() override
;
152 // CertLoader::Observer
153 virtual void OnCertificatesLoaded(const net::CertificateList
& cert_list
,
154 bool initial_load
) override
;
157 friend class NetworkHandler
;
158 friend class NetworkConnectionHandlerTest
;
160 struct ConnectRequest
;
162 NetworkConnectionHandler();
164 void Init(NetworkStateHandler
* network_state_handler
,
165 NetworkConfigurationHandler
* network_configuration_handler
,
166 ManagedNetworkConfigurationHandler
*
167 managed_network_configuration_handler
);
169 ConnectRequest
* GetPendingRequest(const std::string
& service_path
);
171 // Callback from Shill.Service.GetProperties. Parses |properties| to verify
172 // whether or not the network appears to be configured. If configured,
173 // attempts a connection, otherwise invokes error_callback from
174 // pending_requests_[service_path]. |check_error_state| is passed from
175 // ConnectToNetwork(), see comment for info.
176 void VerifyConfiguredAndConnect(bool check_error_state
,
177 const std::string
& service_path
,
178 const base::DictionaryValue
& properties
);
180 // Queues a connect request until certificates have loaded.
181 void QueueConnectRequest(const std::string
& service_path
);
183 // Checks to see if certificates have loaded and if not, cancels any queued
184 // connect request and notifies the user.
185 void CheckCertificatesLoaded();
187 // Handles connecting to a queued network after certificates are loaded or
188 // handle cert load timeout.
189 void ConnectToQueuedNetwork();
191 // Calls Shill.Manager.Connect asynchronously.
192 void CallShillConnect(const std::string
& service_path
);
194 // Handles failure from ConfigurationHandler calls.
195 void HandleConfigurationFailure(
196 const std::string
& service_path
,
197 const std::string
& error_name
,
198 scoped_ptr
<base::DictionaryValue
> error_data
);
200 // Handles success or failure from Shill.Service.Connect.
201 void HandleShillConnectSuccess(const std::string
& service_path
);
202 void HandleShillConnectFailure(const std::string
& service_path
,
203 const std::string
& error_name
,
204 const std::string
& error_message
);
206 void CheckPendingRequest(const std::string service_path
);
207 void CheckAllPendingRequests();
209 void ErrorCallbackForPendingRequest(const std::string
& service_path
,
210 const std::string
& error_name
);
212 // Calls Shill.Manager.Disconnect asynchronously.
213 void CallShillDisconnect(
214 const std::string
& service_path
,
215 const base::Closure
& success_callback
,
216 const network_handler::ErrorCallback
& error_callback
);
218 // Handle success from Shill.Service.Disconnect.
219 void HandleShillDisconnectSuccess(const std::string
& service_path
,
220 const base::Closure
& success_callback
);
222 ObserverList
<Observer
> observers_
;
224 // Local references to the associated handler instances.
225 CertLoader
* cert_loader_
;
226 NetworkStateHandler
* network_state_handler_
;
227 NetworkConfigurationHandler
* configuration_handler_
;
228 ManagedNetworkConfigurationHandler
* managed_configuration_handler_
;
230 // Map of pending connect requests, used to prevent repeated attempts while
231 // waiting for Shill and to trigger callbacks on eventual success or failure.
232 std::map
<std::string
, ConnectRequest
> pending_requests_
;
233 scoped_ptr
<ConnectRequest
> queued_connect_
;
235 // Track certificate loading state.
237 bool certificates_loaded_
;
238 base::TimeTicks logged_in_time_
;
240 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler
);
243 } // namespace chromeos
245 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_