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. 4 5#ifndef CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 6#define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 7 8#include <set> 9#include <string> 10 11#include "base/basictypes.h" 12#include "base/callback.h" 13#include "base/memory/weak_ptr.h" 14#include "base/values.h" 15#include "chromeos/cert_loader.h" 16#include "chromeos/chromeos_export.h" 17#include "chromeos/dbus/dbus_method_call_status.h" 18#include "chromeos/login/login_state.h" 19#include "chromeos/network/network_handler.h" 20#include "chromeos/network/network_handler_callbacks.h" 21#include "chromeos/network/network_state_handler_observer.h" 22 23namespace chromeos { 24 25class NetworkState; 26class NetworkUIData; 27 28// The NetworkConnectionHandler class is used to manage network connection 29// requests. This is the only class that should make Shill Connect calls. 30// It handles the following steps: 31// 1. Determine whether or not sufficient information (e.g. passphrase) is 32// known to be available to connect to the network. 33// 2. Request additional information (e.g. user data which contains certificate 34// information) and determine whether sufficient information is available. 35// 3. Possibly configure the network certificate info (tpm slot and pkcs11 id). 36// 4. Send the connect request. 37// 5. Wait for the network state to change to a non connecting state. 38// 6. Invoke the appropriate callback (always) on success or failure. 39// 40// NetworkConnectionHandler depends on NetworkStateHandler for immediately 41// available State information, and NetworkConfigurationHandler for any 42// configuration calls. 43 44class CHROMEOS_EXPORT NetworkConnectionHandler 45 : public LoginState::Observer, 46 public CertLoader::Observer, 47 public NetworkStateHandlerObserver, 48 public base::SupportsWeakPtr<NetworkConnectionHandler> { 49 public: 50 // Constants for |error_name| from |error_callback| for Connect. 51 52 // No network matching |service_path| is found (hidden networks must be 53 // configured before connecting). 54 static const char kErrorNotFound[]; 55 56 // Already connected to the network. 57 static const char kErrorConnected[]; 58 59 // Already connecting to the network. 60 static const char kErrorConnecting[]; 61 62 // The passphrase is missing or invalid. 63 static const char kErrorPassphraseRequired[]; 64 65 static const char kErrorActivationRequired[]; 66 67 // The network requires a cert and none exists. 68 static const char kErrorCertificateRequired[]; 69 70 // The network had an authentication error, indicating that additional or 71 // different authentication information is required. 72 static const char kErrorAuthenticationRequired[]; 73 74 // Additional configuration is required. 75 static const char kErrorConfigurationRequired[]; 76 77 // Configuration failed during the configure stage of the connect flow. 78 static const char kErrorConfigureFailed[]; 79 80 // For Disconnect or Activate, an unexpected DBus or Shill error occurred. 81 static const char kErrorShillError[]; 82 83 // A new network connect request canceled this one. 84 static const char kErrorConnectCanceled[]; 85 86 // Constants for |error_name| from |error_callback| for Disconnect. 87 static const char kErrorNotConnected[]; 88 89 virtual ~NetworkConnectionHandler(); 90 91 // ConnectToNetwork() will start an asynchronous connection attempt. 92 // On success, |success_callback| will be called. 93 // On failure, |error_callback| will be called with |error_name| one of the 94 // constants defined above, or flimflam::kErrorConnectFailed or 95 // flimflam::kErrorBadPassphrase if the Shill Error property (from a 96 // previous connect attempt) was set to one of those. 97 // |error_message| will contain an additional error string for debugging. 98 // If |check_error_state| is true, the current state of the network is 99 // checked for errors, otherwise current state is ignored (e.g. for recently 100 // configured networks or repeat attempts). 101 void ConnectToNetwork(const std::string& service_path, 102 const base::Closure& success_callback, 103 const network_handler::ErrorCallback& error_callback, 104 bool check_error_state); 105 106 // DisconnectNetwork() will send a Disconnect request to Shill. 107 // On success, |success_callback| will be called. 108 // On failure, |error_callback| will be called with |error_name| one of: 109 // kErrorNotFound if no network matching |service_path| is found. 110 // kErrorNotConnected if not connected to the network. 111 // kErrorShillError if a DBus or Shill error occurred. 112 // |error_message| will contain and additional error string for debugging. 113 void DisconnectNetwork(const std::string& service_path, 114 const base::Closure& success_callback, 115 const network_handler::ErrorCallback& error_callback); 116 117 // ActivateNetwork() will start an asynchronous activation attempt. 118 // |carrier| may be empty or may specify a carrier to activate. 119 // On success, |success_callback| will be called. 120 // On failure, |error_callback| will be called with |error_name| one of: 121 // kErrorNotFound if no network matching |service_path| is found. 122 // kErrorShillError if a DBus or Shill error occurred. 123 // TODO(stevenjb/armansito): Move this to a separate NetworkActivationHandler. 124 void ActivateNetwork(const std::string& service_path, 125 const std::string& carrier, 126 const base::Closure& success_callback, 127 const network_handler::ErrorCallback& error_callback); 128 129 // Returns true if ConnectToNetwork has been called with |service_path| and 130 // has not completed (i.e. success or error callback has been called). 131 bool HasConnectingNetwork(const std::string& service_path); 132 133 // NetworkStateHandlerObserver 134 virtual void NetworkListChanged() OVERRIDE; 135 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE; 136 137 // LoginState::Observer 138 virtual void LoggedInStateChanged(LoginState::LoggedInState state) OVERRIDE; 139 140 // CertLoader::Observer 141 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, 142 bool initial_load) OVERRIDE; 143 144 private: 145 friend class NetworkHandler; 146 friend class NetworkConnectionHandlerTest; 147 148 struct ConnectRequest; 149 150 NetworkConnectionHandler(); 151 152 void Init(NetworkStateHandler* network_state_handler, 153 NetworkConfigurationHandler* network_configuration_handler); 154 155 ConnectRequest* GetPendingRequest(const std::string& service_path); 156 157 // Callback from Shill.Service.GetProperties. Parses |properties| to verify 158 // whether or not the network appears to be configured. If configured, 159 // attempts a connection, otherwise invokes error_callback from 160 // pending_requests_[service_path]. |check_error_state| is passed from 161 // ConnectToNetwork(), see comment for info. 162 void VerifyConfiguredAndConnect(bool check_error_state, 163 const std::string& service_path, 164 const base::DictionaryValue& properties); 165 166 // Calls Shill.Manager.Connect asynchronously. 167 void CallShillConnect(const std::string& service_path); 168 169 // Handle failure from ConfigurationHandler calls. 170 void HandleConfigurationFailure( 171 const std::string& service_path, 172 const std::string& error_name, 173 scoped_ptr<base::DictionaryValue> error_data); 174 175 // Handle success or failure from Shill.Service.Connect. 176 void HandleShillConnectSuccess(const std::string& service_path); 177 void HandleShillConnectFailure(const std::string& service_path, 178 const std::string& error_name, 179 const std::string& error_message); 180 181 void CheckPendingRequest(const std::string service_path); 182 void CheckAllPendingRequests(); 183 184 // Returns the PKCS#11 ID of a cert matching the certificate pattern in 185 // |ui_data|. Returns empty string otherwise. 186 std::string CertificateIsConfigured(NetworkUIData* ui_data); 187 void ErrorCallbackForPendingRequest(const std::string& service_path, 188 const std::string& error_name); 189 190 // Calls Shill.Manager.Disconnect asynchronously. 191 void CallShillDisconnect( 192 const std::string& service_path, 193 const base::Closure& success_callback, 194 const network_handler::ErrorCallback& error_callback); 195 196 // Handle success from Shill.Service.Disconnect. 197 void HandleShillDisconnectSuccess(const std::string& service_path, 198 const base::Closure& success_callback); 199 200 // Calls Shill.Manager.Activate asynchronously. 201 void CallShillActivate( 202 const std::string& service_path, 203 const std::string& carrier, 204 const base::Closure& success_callback, 205 const network_handler::ErrorCallback& error_callback); 206 207 // Handle success from Shill.Service.ActivateCellularModem. 208 void HandleShillActivateSuccess(const std::string& service_path, 209 const base::Closure& success_callback); 210 211 // Local references to the associated handler instances. 212 CertLoader* cert_loader_; 213 NetworkStateHandler* network_state_handler_; 214 NetworkConfigurationHandler* network_configuration_handler_; 215 216 // Map of pending connect requests, used to prevent repeated attempts while 217 // waiting for Shill and to trigger callbacks on eventual success or failure. 218 std::map<std::string, ConnectRequest> pending_requests_; 219 scoped_ptr<ConnectRequest> queued_connect_; 220 221 // Track certificate loading state. 222 bool logged_in_; 223 bool certificates_loaded_; 224 225 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); 226}; 227 228} // namespace chromeos 229 230#endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 231