Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(730)

Side by Side Diff: chromeos/network/network_connection_handler.h

Issue 2861883002: [CrOS Tether] Transform NetworkConnectionHandler to an interface. (Closed)
Patch Set: stevenjb@ comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 5 #ifndef CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
6 #define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 6 #define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 11
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h" 15 #include "base/observer_list.h"
16 #include "base/time/time.h"
17 #include "base/values.h" 16 #include "base/values.h"
18 #include "chromeos/cert_loader.h"
19 #include "chromeos/chromeos_export.h" 17 #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" 18 #include "chromeos/network/network_connection_observer.h"
23 #include "chromeos/network/network_handler.h" 19 #include "chromeos/network/network_handler.h"
24 #include "chromeos/network/network_handler_callbacks.h" 20 #include "chromeos/network/network_handler_callbacks.h"
25 #include "chromeos/network/network_state_handler_observer.h"
26 21
27 namespace chromeos { 22 namespace chromeos {
28 23
29 class NetworkState;
30
31 // The NetworkConnectionHandler class is used to manage network connection 24 // The NetworkConnectionHandler class is used to manage network connection
32 // requests. This is the only class that should make Shill Connect calls. 25 // requests. This is the only class that should make Shill Connect calls.
33 // It handles the following steps: 26 // It handles the following steps:
34 // 1. Determine whether or not sufficient information (e.g. passphrase) is 27 // 1. Determine whether or not sufficient information (e.g. passphrase) is
35 // known to be available to connect to the network. 28 // known to be available to connect to the network.
36 // 2. Request additional information (e.g. user data which contains certificate 29 // 2. Request additional information (e.g. user data which contains certificate
37 // information) and determine whether sufficient information is available. 30 // information) and determine whether sufficient information is available.
38 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id). 31 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id).
39 // 4. Send the connect request. 32 // 4. Send the connect request.
40 // 5. Wait for the network state to change to a non connecting state. 33 // 5. Wait for the network state to change to a non connecting state.
41 // 6. Invoke the appropriate callback (always) on success or failure. 34 // 6. Invoke the appropriate callback (always) on success or failure.
42 // 35 //
43 // NetworkConnectionHandler depends on NetworkStateHandler for immediately 36 // NetworkConnectionHandler depends on NetworkStateHandler for immediately
44 // available State information, and NetworkConfigurationHandler for any 37 // available State information, and NetworkConfigurationHandler for any
45 // configuration calls. 38 // configuration calls.
46 39
47 class CHROMEOS_EXPORT NetworkConnectionHandler 40 class CHROMEOS_EXPORT NetworkConnectionHandler {
48 : public LoginState::Observer,
49 public CertLoader::Observer,
50 public NetworkStateHandlerObserver,
51 public base::SupportsWeakPtr<NetworkConnectionHandler> {
52 public: 41 public:
53 // Constants for |error_name| from |error_callback| for Connect. 42 // Constants for |error_name| from |error_callback| for Connect.
54 43
55 // No network matching |service_path| is found (hidden networks must be 44 // No network matching |service_path| is found (hidden networks must be
56 // configured before connecting). 45 // configured before connecting).
57 static const char kErrorNotFound[]; 46 static const char kErrorNotFound[];
58 47
59 // Already connected to the network. 48 // Already connected to the network.
60 static const char kErrorConnected[]; 49 static const char kErrorConnected[];
61 50
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 // |error_callback|, passing the relevant error code declared above. 105 // |error_callback|, passing the relevant error code declared above.
117 virtual void ConnectToNetwork( 106 virtual void ConnectToNetwork(
118 const std::string& tether_network_guid, 107 const std::string& tether_network_guid,
119 const base::Closure& success_callback, 108 const base::Closure& success_callback,
120 const network_handler::StringResultCallback& error_callback) = 0; 109 const network_handler::StringResultCallback& error_callback) = 0;
121 110
122 protected: 111 protected:
123 virtual ~TetherDelegate() {} 112 virtual ~TetherDelegate() {}
124 }; 113 };
125 114
126 ~NetworkConnectionHandler() override; 115 virtual ~NetworkConnectionHandler();
127 116
128 void AddObserver(NetworkConnectionObserver* observer); 117 void AddObserver(NetworkConnectionObserver* observer);
129 void RemoveObserver(NetworkConnectionObserver* observer); 118 void RemoveObserver(NetworkConnectionObserver* observer);
130 119
120 // Sets the TetherDelegate to handle Tether actions. |tether_delegate| is
121 // owned by the caller.
122 void SetTetherDelegate(TetherDelegate* tether_delegate);
123
131 // ConnectToNetwork() will start an asynchronous connection attempt. 124 // ConnectToNetwork() will start an asynchronous connection attempt.
132 // On success, |success_callback| will be called. 125 // On success, |success_callback| will be called.
133 // On failure, |error_callback| will be called with |error_name| one of the 126 // On failure, |error_callback| will be called with |error_name| one of the
134 // constants defined above. 127 // constants defined above.
135 // |error_message| will contain an additional error string for debugging. 128 // |error_message| will contain an additional error string for debugging.
136 // If |check_error_state| is true, the current state of the network is 129 // If |check_error_state| is true, the current state of the network is
137 // checked for errors, otherwise current state is ignored (e.g. for recently 130 // checked for errors, otherwise current state is ignored (e.g. for recently
138 // configured networks or repeat attempts). 131 // configured networks or repeat attempts).
139 void ConnectToNetwork(const std::string& service_path, 132 virtual void ConnectToNetwork(
140 const base::Closure& success_callback, 133 const std::string& service_path,
141 const network_handler::ErrorCallback& error_callback, 134 const base::Closure& success_callback,
142 bool check_error_state); 135 const network_handler::ErrorCallback& error_callback,
136 bool check_error_state) = 0;
143 137
144 // DisconnectNetwork() will send a Disconnect request to Shill. 138 // DisconnectNetwork() will send a Disconnect request to Shill.
145 // On success, |success_callback| will be called. 139 // On success, |success_callback| will be called.
146 // On failure, |error_callback| will be called with |error_name| one of: 140 // On failure, |error_callback| will be called with |error_name| one of:
147 // kErrorNotFound if no network matching |service_path| is found. 141 // kErrorNotFound if no network matching |service_path| is found.
148 // kErrorNotConnected if not connected to the network. 142 // kErrorNotConnected if not connected to the network.
149 // kErrorDisconnectFailed if a DBus or Shill error occurred. 143 // kErrorDisconnectFailed if a DBus or Shill error occurred.
150 // |error_message| will contain and additional error string for debugging. 144 // |error_message| will contain and additional error string for debugging.
151 void DisconnectNetwork(const std::string& service_path, 145 virtual void DisconnectNetwork(
152 const base::Closure& success_callback, 146 const std::string& service_path,
153 const network_handler::ErrorCallback& error_callback); 147 const base::Closure& success_callback,
148 const network_handler::ErrorCallback& error_callback) = 0;
154 149
155 // Returns true if ConnectToNetwork has been called with |service_path| and 150 // Returns true if ConnectToNetwork has been called with |service_path| and
156 // has not completed (i.e. success or error callback has been called). 151 // has not completed (i.e. success or error callback has been called).
157 bool HasConnectingNetwork(const std::string& service_path); 152 virtual bool HasConnectingNetwork(const std::string& service_path) = 0;
158 153
159 // Returns true if there are any pending connect requests. 154 // Returns true if there are any pending connect requests.
160 bool HasPendingConnectRequest(); 155 virtual bool HasPendingConnectRequest() = 0;
161 156
162 // Sets the TetherDelegate to handle Tether actions. |tether_delegate| is 157 virtual void Init(NetworkStateHandler* network_state_handler,
163 // owned by the caller. 158 NetworkConfigurationHandler* network_configuration_handler,
164 void SetTetherDelegate(TetherDelegate* tether_delegate); 159 ManagedNetworkConfigurationHandler*
165 160 managed_network_configuration_handler) = 0;
166 // NetworkStateHandlerObserver
167 void NetworkListChanged() override;
168 void NetworkPropertiesUpdated(const NetworkState* network) override;
169
170 // LoginState::Observer
171 void LoggedInStateChanged() override;
172
173 // CertLoader::Observer
174 void OnCertificatesLoaded(const net::CertificateList& cert_list,
175 bool initial_load) override;
176 161
177 protected: 162 protected:
178 NetworkConnectionHandler(); 163 NetworkConnectionHandler();
179 164
180 void InitiateTetherNetworkConnection(
181 const std::string& tether_network_guid,
182 const base::Closure& success_callback,
183 const network_handler::ErrorCallback& error_callback);
184
185 private:
186 friend class NetworkHandler;
187 friend class NetworkConnectionHandlerTest;
188
189 struct ConnectRequest;
190
191 void Init(NetworkStateHandler* network_state_handler,
192 NetworkConfigurationHandler* network_configuration_handler,
193 ManagedNetworkConfigurationHandler*
194 managed_network_configuration_handler);
195
196 ConnectRequest* GetPendingRequest(const std::string& service_path);
197
198 // Callback from Shill.Service.GetProperties. Parses |properties| to verify
199 // whether or not the network appears to be configured. If configured,
200 // attempts a connection, otherwise invokes error_callback from
201 // pending_requests_[service_path]. |check_error_state| is passed from
202 // ConnectToNetwork(), see comment for info.
203 void VerifyConfiguredAndConnect(bool check_error_state,
204 const std::string& service_path,
205 const base::DictionaryValue& properties);
206
207 bool IsNetworkProhibitedByPolicy(const std::string& type,
208 const std::string& guid,
209 const std::string& profile_path);
210
211 // Queues a connect request until certificates have loaded.
212 void QueueConnectRequest(const std::string& service_path);
213
214 // Checks to see if certificates have loaded and if not, cancels any queued
215 // connect request and notifies the user.
216 void CheckCertificatesLoaded();
217
218 // Handles connecting to a queued network after certificates are loaded or
219 // handle cert load timeout.
220 void ConnectToQueuedNetwork();
221
222 // Calls Shill.Manager.Connect asynchronously.
223 void CallShillConnect(const std::string& service_path);
224
225 // Handles failure from ConfigurationHandler calls.
226 void HandleConfigurationFailure(
227 const std::string& service_path,
228 const std::string& error_name,
229 std::unique_ptr<base::DictionaryValue> error_data);
230
231 // Handles success or failure from Shill.Service.Connect.
232 void HandleShillConnectSuccess(const std::string& service_path);
233 void HandleShillConnectFailure(const std::string& service_path,
234 const std::string& error_name,
235 const std::string& error_message);
236
237 // Note: |service_path| is passed by value here, because in some cases
238 // the value may be located in the map and then it can be deleted, producing
239 // a reference to invalid memory.
240 void CheckPendingRequest(const std::string service_path);
241
242 void CheckAllPendingRequests();
243
244 // Notify caller and observers that the connect request succeeded. 165 // Notify caller and observers that the connect request succeeded.
245 void InvokeConnectSuccessCallback(const std::string& service_path, 166 void InvokeConnectSuccessCallback(const std::string& service_path,
246 const base::Closure& success_callback); 167 const base::Closure& success_callback);
247 168
248 // Look up the ConnectRequest for |service_path| and call
249 // InvokeConnectErrorCallback.
250 void ErrorCallbackForPendingRequest(const std::string& service_path,
251 const std::string& error_name);
252
253 // Notify caller and observers that the connect request failed. 169 // Notify caller and observers that the connect request failed.
254 // |error_name| will be one of the kError* messages defined above. 170 // |error_name| will be one of the kError* messages defined above.
255 void InvokeConnectErrorCallback( 171 void InvokeConnectErrorCallback(
256 const std::string& service_path, 172 const std::string& service_path,
257 const network_handler::ErrorCallback& error_callback, 173 const network_handler::ErrorCallback& error_callback,
258 const std::string& error_name); 174 const std::string& error_name);
259 175
260 // Calls Shill.Manager.Disconnect asynchronously. 176 // Initiates a connection to a Tether network.
261 void CallShillDisconnect( 177 void InitiateTetherNetworkConnection(
262 const std::string& service_path, 178 const std::string& tether_network_guid,
263 const base::Closure& success_callback, 179 const base::Closure& success_callback,
264 const network_handler::ErrorCallback& error_callback); 180 const network_handler::ErrorCallback& error_callback);
265 181
266 // Handle success from Shill.Service.Disconnect.
267 void HandleShillDisconnectSuccess(const std::string& service_path,
268 const base::Closure& success_callback);
269
270 base::ObserverList<NetworkConnectionObserver, true> observers_; 182 base::ObserverList<NetworkConnectionObserver, true> observers_;
271 183
272 // Local references to the associated handler instances.
273 CertLoader* cert_loader_;
274 NetworkStateHandler* network_state_handler_;
275 NetworkConfigurationHandler* configuration_handler_;
276 ManagedNetworkConfigurationHandler* managed_configuration_handler_;
277
278 // Map of pending connect requests, used to prevent repeated attempts while
279 // waiting for Shill and to trigger callbacks on eventual success or failure.
280 std::map<std::string, ConnectRequest> pending_requests_;
281 std::unique_ptr<ConnectRequest> queued_connect_;
282
283 // Track certificate loading state.
284 bool logged_in_;
285 bool certificates_loaded_;
286 base::TimeTicks logged_in_time_;
287
288 // Delegate used to start a connection to a tether network. 184 // Delegate used to start a connection to a tether network.
289 TetherDelegate* tether_delegate_; 185 TetherDelegate* tether_delegate_;
290 186
187 private:
188 // Only to be used by NetworkConnectionHandler implementation (and not by
189 // derived classes).
190 base::WeakPtrFactory<NetworkConnectionHandler> weak_ptr_factory_;
191
291 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); 192 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
292 }; 193 };
293 194
294 } // namespace chromeos 195 } // namespace chromeos
295 196
296 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 197 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
OLDNEW
« no previous file with comments | « chromeos/components/tether/tether_connector_unittest.cc ('k') | chromeos/network/network_connection_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698