Implement proximity_auth::WireMessage::Serialize().
[chromium-blink-merge.git] / chromeos / network / network_connection_handler.h
blobddadf7a19e9df118f1cdb13cc07ba1b9847c1cbe
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_
8 #include <map>
9 #include <set>
10 #include <string>
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_connection_observer.h"
23 #include "chromeos/network/network_handler.h"
24 #include "chromeos/network/network_handler_callbacks.h"
25 #include "chromeos/network/network_state_handler_observer.h"
27 namespace chromeos {
29 class NetworkState;
31 // The NetworkConnectionHandler class is used to manage network connection
32 // requests. This is the only class that should make Shill Connect calls.
33 // It handles the following steps:
34 // 1. Determine whether or not sufficient information (e.g. passphrase) is
35 // known to be available to connect to the network.
36 // 2. Request additional information (e.g. user data which contains certificate
37 // information) and determine whether sufficient information is available.
38 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id).
39 // 4. Send the connect request.
40 // 5. Wait for the network state to change to a non connecting state.
41 // 6. Invoke the appropriate callback (always) on success or failure.
43 // NetworkConnectionHandler depends on NetworkStateHandler for immediately
44 // available State information, and NetworkConfigurationHandler for any
45 // configuration calls.
47 class CHROMEOS_EXPORT NetworkConnectionHandler
48 : public LoginState::Observer,
49 public CertLoader::Observer,
50 public NetworkStateHandlerObserver,
51 public base::SupportsWeakPtr<NetworkConnectionHandler> {
52 public:
53 // Constants for |error_name| from |error_callback| for Connect.
55 // No network matching |service_path| is found (hidden networks must be
56 // configured before connecting).
57 static const char kErrorNotFound[];
59 // Already connected to the network.
60 static const char kErrorConnected[];
62 // Already connecting to the network.
63 static const char kErrorConnecting[];
65 // The passphrase is missing or invalid.
66 static const char kErrorPassphraseRequired[];
68 // The passphrase is incorrect.
69 static const char kErrorBadPassphrase[];
71 // The network requires a cert and none exists.
72 static const char kErrorCertificateRequired[];
74 // The network had an authentication error, indicating that additional or
75 // different authentication information is required.
76 static const char kErrorAuthenticationRequired[];
78 // Additional configuration is required.
79 static const char kErrorConfigurationRequired[];
81 // Configuration failed during the configure stage of the connect flow.
82 static const char kErrorConfigureFailed[];
84 // An unexpected DBus or Shill error occurred while connecting.
85 static const char kErrorConnectFailed[];
87 // An unexpected DBus or Shill error occurred while disconnecting.
88 static const char kErrorDisconnectFailed[];
90 // A new network connect request canceled this one.
91 static const char kErrorConnectCanceled[];
93 // Constants for |error_name| from |error_callback| for Disconnect.
94 static const char kErrorNotConnected[];
96 // Certificate load timed out.
97 static const char kErrorCertLoadTimeout[];
99 ~NetworkConnectionHandler() override;
101 void AddObserver(NetworkConnectionObserver* observer);
102 void RemoveObserver(NetworkConnectionObserver* observer);
104 // ConnectToNetwork() will start an asynchronous connection attempt.
105 // On success, |success_callback| will be called.
106 // On failure, |error_callback| will be called with |error_name| one of the
107 // constants defined above.
108 // |error_message| will contain an additional error string for debugging.
109 // If |check_error_state| is true, the current state of the network is
110 // checked for errors, otherwise current state is ignored (e.g. for recently
111 // configured networks or repeat attempts).
112 void ConnectToNetwork(const std::string& service_path,
113 const base::Closure& success_callback,
114 const network_handler::ErrorCallback& error_callback,
115 bool check_error_state);
117 // DisconnectNetwork() will send a Disconnect request to Shill.
118 // On success, |success_callback| will be called.
119 // On failure, |error_callback| will be called with |error_name| one of:
120 // kErrorNotFound if no network matching |service_path| is found.
121 // kErrorNotConnected if not connected to the network.
122 // kErrorDisconnectFailed if a DBus or Shill error occurred.
123 // |error_message| will contain and additional error string for debugging.
124 void DisconnectNetwork(const std::string& service_path,
125 const base::Closure& success_callback,
126 const network_handler::ErrorCallback& error_callback);
128 // Returns true if ConnectToNetwork has been called with |service_path| and
129 // has not completed (i.e. success or error callback has been called).
130 bool HasConnectingNetwork(const std::string& service_path);
132 // Returns true if there are any pending connect requests.
133 bool HasPendingConnectRequest();
135 // NetworkStateHandlerObserver
136 void NetworkListChanged() override;
137 void NetworkPropertiesUpdated(const NetworkState* network) override;
139 // LoginState::Observer
140 void LoggedInStateChanged() override;
142 // CertLoader::Observer
143 void OnCertificatesLoaded(const net::CertificateList& cert_list,
144 bool initial_load) override;
146 private:
147 friend class NetworkHandler;
148 friend class NetworkConnectionHandlerTest;
150 struct ConnectRequest;
152 NetworkConnectionHandler();
154 void Init(NetworkStateHandler* network_state_handler,
155 NetworkConfigurationHandler* network_configuration_handler,
156 ManagedNetworkConfigurationHandler*
157 managed_network_configuration_handler);
159 ConnectRequest* GetPendingRequest(const std::string& service_path);
161 // Callback from Shill.Service.GetProperties. Parses |properties| to verify
162 // whether or not the network appears to be configured. If configured,
163 // attempts a connection, otherwise invokes error_callback from
164 // pending_requests_[service_path]. |check_error_state| is passed from
165 // ConnectToNetwork(), see comment for info.
166 void VerifyConfiguredAndConnect(bool check_error_state,
167 const std::string& service_path,
168 const base::DictionaryValue& properties);
170 // Queues a connect request until certificates have loaded.
171 void QueueConnectRequest(const std::string& service_path);
173 // Checks to see if certificates have loaded and if not, cancels any queued
174 // connect request and notifies the user.
175 void CheckCertificatesLoaded();
177 // Handles connecting to a queued network after certificates are loaded or
178 // handle cert load timeout.
179 void ConnectToQueuedNetwork();
181 // Calls Shill.Manager.Connect asynchronously.
182 void CallShillConnect(const std::string& service_path);
184 // Handles failure from ConfigurationHandler calls.
185 void HandleConfigurationFailure(
186 const std::string& service_path,
187 const std::string& error_name,
188 scoped_ptr<base::DictionaryValue> error_data);
190 // Handles success or failure from Shill.Service.Connect.
191 void HandleShillConnectSuccess(const std::string& service_path);
192 void HandleShillConnectFailure(const std::string& service_path,
193 const std::string& error_name,
194 const std::string& error_message);
196 void CheckPendingRequest(const std::string service_path);
197 void CheckAllPendingRequests();
199 // Notify caller and observers that the connect request succeeded.
200 void InvokeConnectSuccessCallback(const std::string& service_path,
201 const base::Closure& success_callback);
203 // Look up the ConnectRequest for |service_path| and call
204 // InvokeConnectErrorCallback.
205 void ErrorCallbackForPendingRequest(const std::string& service_path,
206 const std::string& error_name);
208 // Notify caller and observers that the connect request failed.
209 // |error_name| will be one of the kError* messages defined above.
210 void InvokeConnectErrorCallback(
211 const std::string& service_path,
212 const network_handler::ErrorCallback& error_callback,
213 const std::string& error_name);
215 // Calls Shill.Manager.Disconnect asynchronously.
216 void CallShillDisconnect(
217 const std::string& service_path,
218 const base::Closure& success_callback,
219 const network_handler::ErrorCallback& error_callback);
221 // Handle success from Shill.Service.Disconnect.
222 void HandleShillDisconnectSuccess(const std::string& service_path,
223 const base::Closure& success_callback);
225 base::ObserverList<NetworkConnectionObserver> observers_;
227 // Local references to the associated handler instances.
228 CertLoader* cert_loader_;
229 NetworkStateHandler* network_state_handler_;
230 NetworkConfigurationHandler* configuration_handler_;
231 ManagedNetworkConfigurationHandler* managed_configuration_handler_;
233 // Map of pending connect requests, used to prevent repeated attempts while
234 // waiting for Shill and to trigger callbacks on eventual success or failure.
235 std::map<std::string, ConnectRequest> pending_requests_;
236 scoped_ptr<ConnectRequest> queued_connect_;
238 // Track certificate loading state.
239 bool logged_in_;
240 bool certificates_loaded_;
241 base::TimeTicks logged_in_time_;
243 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
246 } // namespace chromeos
248 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_