Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | 17 #include "chromeos/cert_loader.h" |
| 19 #include "chromeos/chromeos_export.h" | 18 #include "chromeos/chromeos_export.h" |
| 20 #include "chromeos/dbus/dbus_method_call_status.h" | 19 #include "chromeos/dbus/dbus_method_call_status.h" |
| 21 #include "chromeos/login/login_state.h" | 20 #include "chromeos/login/login_state.h" |
| 22 #include "chromeos/network/network_connection_observer.h" | 21 #include "chromeos/network/network_connection_observer.h" |
| 23 #include "chromeos/network/network_handler.h" | 22 #include "chromeos/network/network_handler.h" |
| 24 #include "chromeos/network/network_handler_callbacks.h" | 23 #include "chromeos/network/network_handler_callbacks.h" |
| 25 #include "chromeos/network/network_state_handler_observer.h" | 24 #include "chromeos/network/network_state_handler_observer.h" |
| 26 | 25 |
| 27 namespace chromeos { | 26 namespace chromeos { |
| 28 | 27 |
| 29 class NetworkState; | |
| 30 | |
| 31 // The NetworkConnectionHandler class is used to manage network connection | 28 // The NetworkConnectionHandler class is used to manage network connection |
| 32 // requests. This is the only class that should make Shill Connect calls. | 29 // requests. This is the only class that should make Shill Connect calls. |
| 33 // It handles the following steps: | 30 // It handles the following steps: |
| 34 // 1. Determine whether or not sufficient information (e.g. passphrase) is | 31 // 1. Determine whether or not sufficient information (e.g. passphrase) is |
| 35 // known to be available to connect to the network. | 32 // known to be available to connect to the network. |
| 36 // 2. Request additional information (e.g. user data which contains certificate | 33 // 2. Request additional information (e.g. user data which contains certificate |
| 37 // information) and determine whether sufficient information is available. | 34 // information) and determine whether sufficient information is available. |
| 38 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id). | 35 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id). |
| 39 // 4. Send the connect request. | 36 // 4. Send the connect request. |
| 40 // 5. Wait for the network state to change to a non connecting state. | 37 // 5. Wait for the network state to change to a non connecting state. |
| 41 // 6. Invoke the appropriate callback (always) on success or failure. | 38 // 6. Invoke the appropriate callback (always) on success or failure. |
| 42 // | 39 // |
| 43 // NetworkConnectionHandler depends on NetworkStateHandler for immediately | 40 // NetworkConnectionHandler depends on NetworkStateHandler for immediately |
| 44 // available State information, and NetworkConfigurationHandler for any | 41 // available State information, and NetworkConfigurationHandler for any |
| 45 // configuration calls. | 42 // configuration calls. |
| 46 | 43 |
| 47 class CHROMEOS_EXPORT NetworkConnectionHandler | 44 class CHROMEOS_EXPORT NetworkConnectionHandler |
| 48 : public LoginState::Observer, | 45 : public LoginState::Observer, |
| 49 public CertLoader::Observer, | 46 public CertLoader::Observer, |
| 50 public NetworkStateHandlerObserver, | 47 public NetworkStateHandlerObserver { |
|
stevenjb
2017/05/03 22:05:38
Put these in the Impl
Kyle Horimoto
2017/05/03 22:38:30
Done.
| |
| 51 public base::SupportsWeakPtr<NetworkConnectionHandler> { | |
| 52 public: | 48 public: |
| 53 // Constants for |error_name| from |error_callback| for Connect. | 49 // Constants for |error_name| from |error_callback| for Connect. |
| 54 | 50 |
| 55 // No network matching |service_path| is found (hidden networks must be | 51 // No network matching |service_path| is found (hidden networks must be |
| 56 // configured before connecting). | 52 // configured before connecting). |
| 57 static const char kErrorNotFound[]; | 53 static const char kErrorNotFound[]; |
| 58 | 54 |
| 59 // Already connected to the network. | 55 // Already connected to the network. |
| 60 static const char kErrorConnected[]; | 56 static const char kErrorConnected[]; |
| 61 | 57 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 | 117 |
| 122 protected: | 118 protected: |
| 123 virtual ~TetherDelegate() {} | 119 virtual ~TetherDelegate() {} |
| 124 }; | 120 }; |
| 125 | 121 |
| 126 ~NetworkConnectionHandler() override; | 122 ~NetworkConnectionHandler() override; |
| 127 | 123 |
| 128 void AddObserver(NetworkConnectionObserver* observer); | 124 void AddObserver(NetworkConnectionObserver* observer); |
| 129 void RemoveObserver(NetworkConnectionObserver* observer); | 125 void RemoveObserver(NetworkConnectionObserver* observer); |
| 130 | 126 |
| 127 // Sets the TetherDelegate to handle Tether actions. |tether_delegate| is | |
| 128 // owned by the caller. | |
| 129 void SetTetherDelegate(TetherDelegate* tether_delegate); | |
| 130 | |
| 131 // ConnectToNetwork() will start an asynchronous connection attempt. | 131 // ConnectToNetwork() will start an asynchronous connection attempt. |
| 132 // On success, |success_callback| will be called. | 132 // On success, |success_callback| will be called. |
| 133 // On failure, |error_callback| will be called with |error_name| one of the | 133 // On failure, |error_callback| will be called with |error_name| one of the |
| 134 // constants defined above. | 134 // constants defined above. |
| 135 // |error_message| will contain an additional error string for debugging. | 135 // |error_message| will contain an additional error string for debugging. |
| 136 // If |check_error_state| is true, the current state of the network is | 136 // 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 | 137 // checked for errors, otherwise current state is ignored (e.g. for recently |
| 138 // configured networks or repeat attempts). | 138 // configured networks or repeat attempts). |
| 139 void ConnectToNetwork(const std::string& service_path, | 139 virtual void ConnectToNetwork( |
| 140 const base::Closure& success_callback, | 140 const std::string& service_path, |
| 141 const network_handler::ErrorCallback& error_callback, | 141 const base::Closure& success_callback, |
| 142 bool check_error_state); | 142 const network_handler::ErrorCallback& error_callback, |
| 143 bool check_error_state) = 0; | |
| 143 | 144 |
| 144 // DisconnectNetwork() will send a Disconnect request to Shill. | 145 // DisconnectNetwork() will send a Disconnect request to Shill. |
| 145 // On success, |success_callback| will be called. | 146 // On success, |success_callback| will be called. |
| 146 // On failure, |error_callback| will be called with |error_name| one of: | 147 // On failure, |error_callback| will be called with |error_name| one of: |
| 147 // kErrorNotFound if no network matching |service_path| is found. | 148 // kErrorNotFound if no network matching |service_path| is found. |
| 148 // kErrorNotConnected if not connected to the network. | 149 // kErrorNotConnected if not connected to the network. |
| 149 // kErrorDisconnectFailed if a DBus or Shill error occurred. | 150 // kErrorDisconnectFailed if a DBus or Shill error occurred. |
| 150 // |error_message| will contain and additional error string for debugging. | 151 // |error_message| will contain and additional error string for debugging. |
| 151 void DisconnectNetwork(const std::string& service_path, | 152 virtual void DisconnectNetwork( |
| 152 const base::Closure& success_callback, | 153 const std::string& service_path, |
| 153 const network_handler::ErrorCallback& error_callback); | 154 const base::Closure& success_callback, |
| 155 const network_handler::ErrorCallback& error_callback) = 0; | |
| 154 | 156 |
| 155 // Returns true if ConnectToNetwork has been called with |service_path| and | 157 // Returns true if ConnectToNetwork has been called with |service_path| and |
| 156 // has not completed (i.e. success or error callback has been called). | 158 // has not completed (i.e. success or error callback has been called). |
| 157 bool HasConnectingNetwork(const std::string& service_path); | 159 virtual bool HasConnectingNetwork(const std::string& service_path) = 0; |
| 158 | 160 |
| 159 // Returns true if there are any pending connect requests. | 161 // Returns true if there are any pending connect requests. |
| 160 bool HasPendingConnectRequest(); | 162 virtual bool HasPendingConnectRequest() = 0; |
| 161 | |
| 162 // Sets the TetherDelegate to handle Tether actions. |tether_delegate| is | |
| 163 // owned by the caller. | |
| 164 void SetTetherDelegate(TetherDelegate* tether_delegate); | |
| 165 | |
| 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 | 163 |
| 177 protected: | 164 protected: |
| 178 NetworkConnectionHandler(); | 165 NetworkConnectionHandler(); |
| 179 | 166 |
| 180 void InitiateTetherNetworkConnection( | 167 virtual void Init(NetworkStateHandler* network_state_handler, |
| 181 const std::string& tether_network_guid, | 168 NetworkConfigurationHandler* network_configuration_handler, |
| 182 const base::Closure& success_callback, | 169 ManagedNetworkConfigurationHandler* |
| 183 const network_handler::ErrorCallback& error_callback); | 170 managed_network_configuration_handler) = 0; |
| 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 | 171 |
| 244 // Notify caller and observers that the connect request succeeded. | 172 // Notify caller and observers that the connect request succeeded. |
| 245 void InvokeConnectSuccessCallback(const std::string& service_path, | 173 void InvokeConnectSuccessCallback(const std::string& service_path, |
| 246 const base::Closure& success_callback); | 174 const base::Closure& success_callback); |
| 247 | 175 |
| 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. | 176 // Notify caller and observers that the connect request failed. |
| 254 // |error_name| will be one of the kError* messages defined above. | 177 // |error_name| will be one of the kError* messages defined above. |
| 255 void InvokeConnectErrorCallback( | 178 void InvokeConnectErrorCallback( |
| 256 const std::string& service_path, | 179 const std::string& service_path, |
| 257 const network_handler::ErrorCallback& error_callback, | 180 const network_handler::ErrorCallback& error_callback, |
| 258 const std::string& error_name); | 181 const std::string& error_name); |
| 259 | 182 |
| 260 // Calls Shill.Manager.Disconnect asynchronously. | 183 // Initiates a connection to a Tether network. |
| 261 void CallShillDisconnect( | 184 void InitiateTetherNetworkConnection( |
| 262 const std::string& service_path, | 185 const std::string& tether_network_guid, |
| 263 const base::Closure& success_callback, | 186 const base::Closure& success_callback, |
| 264 const network_handler::ErrorCallback& error_callback); | 187 const network_handler::ErrorCallback& error_callback); |
|
stevenjb
2017/05/03 22:05:38
These should also be in the Impl
Kyle Horimoto
2017/05/03 22:38:30
I'd like to keep these three functions in the base
| |
| 265 | 188 |
| 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_; | 189 base::ObserverList<NetworkConnectionObserver, true> observers_; |
| 271 | 190 |
| 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. | 191 // Delegate used to start a connection to a tether network. |
| 289 TetherDelegate* tether_delegate_; | 192 TetherDelegate* tether_delegate_; |
| 290 | 193 |
| 194 private: | |
| 195 friend class NetworkHandler; | |
|
stevenjb
2017/05/03 22:05:38
Don't do this, see below.
Kyle Horimoto
2017/05/03 22:38:30
Done.
| |
| 196 friend class NetworkConnectionHandlerTest; | |
|
stevenjb
2017/05/03 22:05:38
Do we need this?
Kyle Horimoto
2017/05/03 22:38:30
Done.
| |
| 197 | |
| 198 // Should only be called once by NetworkHandler during initialization. | |
| 199 static std::unique_ptr<NetworkConnectionHandler> Create(); | |
|
stevenjb
2017/05/03 22:05:38
Just make this public, or better yet, just have Ne
Kyle Horimoto
2017/05/03 22:38:30
Done.
| |
| 200 | |
| 201 // Only to be used by NetworkConnectionHandler implementation (and not by | |
| 202 // derived classes). | |
| 203 base::WeakPtrFactory<NetworkConnectionHandler> weak_ptr_factory_; | |
| 204 | |
| 291 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); | 205 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); |
| 292 }; | 206 }; |
| 293 | 207 |
| 294 } // namespace chromeos | 208 } // namespace chromeos |
| 295 | 209 |
| 296 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ | 210 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ |
| OLD | NEW |