| 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 <set> | 9 #include <set> |
| 9 #include <string> | 10 #include <string> |
| 10 | 11 |
| 11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" |
| 14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 15 #include "base/values.h" | 17 #include "base/values.h" |
| 16 #include "chromeos/cert_loader.h" | 18 #include "chromeos/cert_loader.h" |
| 17 #include "chromeos/chromeos_export.h" | 19 #include "chromeos/chromeos_export.h" |
| 18 #include "chromeos/dbus/dbus_method_call_status.h" | 20 #include "chromeos/dbus/dbus_method_call_status.h" |
| 19 #include "chromeos/login/login_state.h" | 21 #include "chromeos/login/login_state.h" |
| 20 #include "chromeos/network/network_handler.h" | 22 #include "chromeos/network/network_handler.h" |
| 21 #include "chromeos/network/network_handler_callbacks.h" | 23 #include "chromeos/network/network_handler_callbacks.h" |
| 22 #include "chromeos/network/network_policy_observer.h" | |
| 23 #include "chromeos/network/network_state_handler_observer.h" | 24 #include "chromeos/network/network_state_handler_observer.h" |
| 24 | 25 |
| 25 namespace chromeos { | 26 namespace chromeos { |
| 26 | 27 |
| 27 class NetworkState; | 28 class NetworkState; |
| 28 | 29 |
| 29 // The NetworkConnectionHandler class is used to manage network connection | 30 // The NetworkConnectionHandler class is used to manage network connection |
| 30 // requests. This is the only class that should make Shill Connect calls. | 31 // requests. This is the only class that should make Shill Connect calls. |
| 31 // It handles the following steps: | 32 // It handles the following steps: |
| 32 // 1. Determine whether or not sufficient information (e.g. passphrase) is | 33 // 1. Determine whether or not sufficient information (e.g. passphrase) is |
| 33 // known to be available to connect to the network. | 34 // known to be available to connect to the network. |
| 34 // 2. Request additional information (e.g. user data which contains certificate | 35 // 2. Request additional information (e.g. user data which contains certificate |
| 35 // information) and determine whether sufficient information is available. | 36 // information) and determine whether sufficient information is available. |
| 36 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id). | 37 // 3. Possibly configure the network certificate info (tpm slot and pkcs11 id). |
| 37 // 4. Send the connect request. | 38 // 4. Send the connect request. |
| 38 // 5. Wait for the network state to change to a non connecting state. | 39 // 5. Wait for the network state to change to a non connecting state. |
| 39 // 6. Invoke the appropriate callback (always) on success or failure. | 40 // 6. Invoke the appropriate callback (always) on success or failure. |
| 40 // | 41 // |
| 41 // NetworkConnectionHandler depends on NetworkStateHandler for immediately | 42 // NetworkConnectionHandler depends on NetworkStateHandler for immediately |
| 42 // available State information, and NetworkConfigurationHandler for any | 43 // available State information, and NetworkConfigurationHandler for any |
| 43 // configuration calls. | 44 // configuration calls. |
| 44 | 45 |
| 45 class CHROMEOS_EXPORT NetworkConnectionHandler | 46 class CHROMEOS_EXPORT NetworkConnectionHandler |
| 46 : public LoginState::Observer, | 47 : public LoginState::Observer, |
| 47 public CertLoader::Observer, | 48 public CertLoader::Observer, |
| 48 public NetworkStateHandlerObserver, | 49 public NetworkStateHandlerObserver, |
| 49 public NetworkPolicyObserver, | |
| 50 public base::SupportsWeakPtr<NetworkConnectionHandler> { | 50 public base::SupportsWeakPtr<NetworkConnectionHandler> { |
| 51 public: | 51 public: |
| 52 class Observer { |
| 53 public: |
| 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; |
| 57 |
| 58 protected: |
| 59 virtual ~Observer() {} |
| 60 |
| 61 private: |
| 62 DISALLOW_ASSIGN(Observer); |
| 63 }; |
| 64 |
| 52 // Constants for |error_name| from |error_callback| for Connect. | 65 // Constants for |error_name| from |error_callback| for Connect. |
| 53 | 66 |
| 54 // No network matching |service_path| is found (hidden networks must be | 67 // No network matching |service_path| is found (hidden networks must be |
| 55 // configured before connecting). | 68 // configured before connecting). |
| 56 static const char kErrorNotFound[]; | 69 static const char kErrorNotFound[]; |
| 57 | 70 |
| 58 // Already connected to the network. | 71 // Already connected to the network. |
| 59 static const char kErrorConnected[]; | 72 static const char kErrorConnected[]; |
| 60 | 73 |
| 61 // Already connecting to the network. | 74 // Already connecting to the network. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 86 static const char kErrorConnectCanceled[]; | 99 static const char kErrorConnectCanceled[]; |
| 87 | 100 |
| 88 // Constants for |error_name| from |error_callback| for Disconnect. | 101 // Constants for |error_name| from |error_callback| for Disconnect. |
| 89 static const char kErrorNotConnected[]; | 102 static const char kErrorNotConnected[]; |
| 90 | 103 |
| 91 // Certificate load timed out. | 104 // Certificate load timed out. |
| 92 static const char kErrorCertLoadTimeout[]; | 105 static const char kErrorCertLoadTimeout[]; |
| 93 | 106 |
| 94 virtual ~NetworkConnectionHandler(); | 107 virtual ~NetworkConnectionHandler(); |
| 95 | 108 |
| 109 void AddObserver(Observer* observer); |
| 110 void RemoveObserver(Observer* observer); |
| 111 |
| 96 // ConnectToNetwork() will start an asynchronous connection attempt. | 112 // ConnectToNetwork() will start an asynchronous connection attempt. |
| 97 // On success, |success_callback| will be called. | 113 // On success, |success_callback| will be called. |
| 98 // On failure, |error_callback| will be called with |error_name| one of the | 114 // On failure, |error_callback| will be called with |error_name| one of the |
| 99 // constants defined above, or shill::kErrorConnectFailed or | 115 // constants defined above, or shill::kErrorConnectFailed or |
| 100 // shill::kErrorBadPassphrase if the Shill Error property (from a | 116 // shill::kErrorBadPassphrase if the Shill Error property (from a |
| 101 // previous connect attempt) was set to one of those. | 117 // previous connect attempt) was set to one of those. |
| 102 // |error_message| will contain an additional error string for debugging. | 118 // |error_message| will contain an additional error string for debugging. |
| 103 // If |check_error_state| is true, the current state of the network is | 119 // If |check_error_state| is true, the current state of the network is |
| 104 // checked for errors, otherwise current state is ignored (e.g. for recently | 120 // checked for errors, otherwise current state is ignored (e.g. for recently |
| 105 // configured networks or repeat attempts). | 121 // configured networks or repeat attempts). |
| (...skipping 24 matching lines...) Expand all Loading... |
| 130 virtual void NetworkListChanged() override; | 146 virtual void NetworkListChanged() override; |
| 131 virtual void NetworkPropertiesUpdated(const NetworkState* network) override; | 147 virtual void NetworkPropertiesUpdated(const NetworkState* network) override; |
| 132 | 148 |
| 133 // LoginState::Observer | 149 // LoginState::Observer |
| 134 virtual void LoggedInStateChanged() override; | 150 virtual void LoggedInStateChanged() override; |
| 135 | 151 |
| 136 // CertLoader::Observer | 152 // CertLoader::Observer |
| 137 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, | 153 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, |
| 138 bool initial_load) override; | 154 bool initial_load) override; |
| 139 | 155 |
| 140 // NetworkPolicyObserver | |
| 141 virtual void PolicyChanged(const std::string& userhash) override; | |
| 142 | |
| 143 private: | 156 private: |
| 144 friend class NetworkHandler; | 157 friend class NetworkHandler; |
| 145 friend class NetworkConnectionHandlerTest; | 158 friend class NetworkConnectionHandlerTest; |
| 146 | 159 |
| 147 struct ConnectRequest; | 160 struct ConnectRequest; |
| 148 | 161 |
| 149 NetworkConnectionHandler(); | 162 NetworkConnectionHandler(); |
| 150 | 163 |
| 151 void Init(NetworkStateHandler* network_state_handler, | 164 void Init(NetworkStateHandler* network_state_handler, |
| 152 NetworkConfigurationHandler* network_configuration_handler, | 165 NetworkConfigurationHandler* network_configuration_handler, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 // Calls Shill.Manager.Disconnect asynchronously. | 212 // Calls Shill.Manager.Disconnect asynchronously. |
| 200 void CallShillDisconnect( | 213 void CallShillDisconnect( |
| 201 const std::string& service_path, | 214 const std::string& service_path, |
| 202 const base::Closure& success_callback, | 215 const base::Closure& success_callback, |
| 203 const network_handler::ErrorCallback& error_callback); | 216 const network_handler::ErrorCallback& error_callback); |
| 204 | 217 |
| 205 // Handle success from Shill.Service.Disconnect. | 218 // Handle success from Shill.Service.Disconnect. |
| 206 void HandleShillDisconnectSuccess(const std::string& service_path, | 219 void HandleShillDisconnectSuccess(const std::string& service_path, |
| 207 const base::Closure& success_callback); | 220 const base::Closure& success_callback); |
| 208 | 221 |
| 209 // If the policy to prevent unmanaged & shared networks to autoconnect is | 222 ObserverList<Observer> observers_; |
| 210 // enabled, then disconnect all such networks except wired networks. Does | |
| 211 // nothing on consecutive calls. | |
| 212 // This is enforced once after a user logs in 1) to allow mananged networks to | |
| 213 // autoconnect and 2) to prevent a previous user from foisting a network on | |
| 214 // the new user. Therefore, this function is called on startup, at login and | |
| 215 // when the device policy is changed. | |
| 216 void DisconnectIfPolicyRequires(); | |
| 217 | |
| 218 // Disconnects from all unmanaged and shared WiFi networks that are currently | |
| 219 // connected or connecting. | |
| 220 void DisconnectFromUnmanagedSharedWiFiNetworks(); | |
| 221 | |
| 222 // Requests a connect to the 'best' available network once after login and | |
| 223 // after any disconnect required by policy is executed (see | |
| 224 // DisconnectIfPolicyRequires()). To include networks with client | |
| 225 // certificates, no request is sent until certificates are loaded. Therefore, | |
| 226 // this function is called on the initial certificate load and by | |
| 227 // DisconnectIfPolicyRequires(). | |
| 228 void ConnectToBestNetworkAfterLogin(); | |
| 229 | 223 |
| 230 // Local references to the associated handler instances. | 224 // Local references to the associated handler instances. |
| 231 CertLoader* cert_loader_; | 225 CertLoader* cert_loader_; |
| 232 NetworkStateHandler* network_state_handler_; | 226 NetworkStateHandler* network_state_handler_; |
| 233 NetworkConfigurationHandler* configuration_handler_; | 227 NetworkConfigurationHandler* configuration_handler_; |
| 234 ManagedNetworkConfigurationHandler* managed_configuration_handler_; | 228 ManagedNetworkConfigurationHandler* managed_configuration_handler_; |
| 235 | 229 |
| 236 // Map of pending connect requests, used to prevent repeated attempts while | 230 // Map of pending connect requests, used to prevent repeated attempts while |
| 237 // waiting for Shill and to trigger callbacks on eventual success or failure. | 231 // waiting for Shill and to trigger callbacks on eventual success or failure. |
| 238 std::map<std::string, ConnectRequest> pending_requests_; | 232 std::map<std::string, ConnectRequest> pending_requests_; |
| 239 scoped_ptr<ConnectRequest> queued_connect_; | 233 scoped_ptr<ConnectRequest> queued_connect_; |
| 240 | 234 |
| 241 // Track certificate loading state. | 235 // Track certificate loading state. |
| 242 bool logged_in_; | 236 bool logged_in_; |
| 243 bool certificates_loaded_; | 237 bool certificates_loaded_; |
| 244 base::TimeTicks logged_in_time_; | 238 base::TimeTicks logged_in_time_; |
| 245 | 239 |
| 246 // Whether the autoconnect policy was applied already, see | |
| 247 // DisconnectIfPolicyRequires(). | |
| 248 bool applied_autoconnect_policy_; | |
| 249 | |
| 250 // Whether the handler already requested a 'ConnectToBestNetwork' after login, | |
| 251 // see ConnectToBestNetworkAfterLogin(). | |
| 252 bool requested_connect_to_best_network_; | |
| 253 | |
| 254 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); | 240 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); |
| 255 }; | 241 }; |
| 256 | 242 |
| 257 } // namespace chromeos | 243 } // namespace chromeos |
| 258 | 244 |
| 259 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ | 245 #endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ |
| OLD | NEW |