network_connection_handler.h revision c2db58bd994c04d98e4ee2cd7565b71548655fe3
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 static const char kErrorNotFound[]; 52 static const char kErrorConnected[]; 53 static const char kErrorConnecting[]; 54 static const char kErrorPassphraseRequired[]; 55 static const char kErrorActivationRequired[]; 56 static const char kErrorCertificateRequired[]; 57 static const char kErrorAuthenticationRequired[]; 58 static const char kErrorConfigurationRequired[]; 59 static const char kErrorShillError[]; 60 static const char kErrorConnectFailed[]; 61 static const char kErrorConfigureFailed[]; 62 static const char kErrorActivateFailed[]; 63 static const char kErrorMissingProvider[]; 64 static const char kErrorConnectCanceled[]; 65 static const char kErrorUnknown[]; 66 67 // Constants for |error_name| from |error_callback| for Disconnect. 68 static const char kErrorNotConnected[]; 69 70 virtual ~NetworkConnectionHandler(); 71 72 // ConnectToNetwork() will start an asynchronous connection attempt. 73 // On success, |success_callback| will be called. 74 // On failure, |error_callback| will be called with |error_name| one of: 75 // kErrorNotFound if no network matching |service_path| is found 76 // (hidden networks must be configured before connecting). 77 // kErrorConnected if already connected to the network. 78 // kErrorConnecting if already connecting to the network. 79 // kErrorCertificateRequired if the network requires a cert and none exists. 80 // kErrorPassphraseRequired if passphrase only is missing or incorrect. 81 // kErrorAuthenticationRequired if other authentication is required. 82 // kErrorConfigurationRequired if additional configuration is required. 83 // kErrorShillError if a DBus or Shill error occurred. 84 // |error_message| will contain an additional error string for debugging. 85 // If |check_error_state| is true, the current state of the network is 86 // checked for errors, otherwise current state is ignored (e.g. for recently 87 // configured networks or repeat attempts). 88 void ConnectToNetwork(const std::string& service_path, 89 const base::Closure& success_callback, 90 const network_handler::ErrorCallback& error_callback, 91 bool check_error_state); 92 93 // DisconnectNetwork() will send a Disconnect request to Shill. 94 // On success, |success_callback| will be called. 95 // On failure, |error_callback| will be called with |error_name| one of: 96 // kErrorNotFound if no network matching |service_path| is found. 97 // kErrorNotConnected if not connected to the network. 98 // kErrorShillError if a DBus or Shill error occurred. 99 // |error_message| will contain and additional error string for debugging. 100 void DisconnectNetwork(const std::string& service_path, 101 const base::Closure& success_callback, 102 const network_handler::ErrorCallback& error_callback); 103 104 // ActivateNetwork() will start an asynchronous activation attempt. 105 // |carrier| may be empty or may specify a carrier to activate. 106 // On success, |success_callback| will be called. 107 // On failure, |error_callback| will be called with |error_name| one of: 108 // kErrorNotFound if no network matching |service_path| is found. 109 // kErrorShillError if a DBus or Shill error occurred. 110 // TODO(stevenjb/armansito): Move this to a separate NetworkActivationHandler. 111 void ActivateNetwork(const std::string& service_path, 112 const std::string& carrier, 113 const base::Closure& success_callback, 114 const network_handler::ErrorCallback& error_callback); 115 116 // Returns true if ConnectToNetwork has been called with |service_path| and 117 // has not completed (i.e. success or error callback has been called). 118 bool HasConnectingNetwork(const std::string& service_path); 119 120 // NetworkStateHandlerObserver 121 virtual void NetworkListChanged() OVERRIDE; 122 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE; 123 124 // LoginState::Observer 125 virtual void LoggedInStateChanged(LoginState::LoggedInState state) OVERRIDE; 126 127 // CertLoader::Observer 128 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, 129 bool initial_load) OVERRIDE; 130 131 private: 132 friend class NetworkHandler; 133 friend class NetworkConnectionHandlerTest; 134 135 struct ConnectRequest; 136 137 NetworkConnectionHandler(); 138 139 void Init(NetworkStateHandler* network_state_handler, 140 NetworkConfigurationHandler* network_configuration_handler); 141 142 ConnectRequest* GetPendingRequest(const std::string& service_path); 143 144 // Callback from Shill.Service.GetProperties. Parses |properties| to verify 145 // whether or not the network appears to be configured. If configured, 146 // attempts a connection, otherwise invokes error_callback from 147 // pending_requests_[service_path]. |check_error_state| is passed from 148 // ConnectToNetwork(), see comment for info. 149 void VerifyConfiguredAndConnect(bool check_error_state, 150 const std::string& service_path, 151 const base::DictionaryValue& properties); 152 153 // Calls Shill.Manager.Connect asynchronously. 154 void CallShillConnect(const std::string& service_path); 155 156 // Handle failure from ConfigurationHandler calls. 157 void HandleConfigurationFailure( 158 const std::string& service_path, 159 const std::string& error_name, 160 scoped_ptr<base::DictionaryValue> error_data); 161 162 // Handle success or failure from Shill.Service.Connect. 163 void HandleShillConnectSuccess(const std::string& service_path); 164 void HandleShillConnectFailure(const std::string& service_path, 165 const std::string& error_name, 166 const std::string& error_message); 167 168 void CheckPendingRequest(const std::string service_path); 169 void CheckAllPendingRequests(); 170 171 // Returns the PKCS#11 ID of a cert matching the certificate pattern in 172 // |ui_data|. Returns empty string otherwise. 173 std::string CertificateIsConfigured(NetworkUIData* ui_data); 174 void ErrorCallbackForPendingRequest(const std::string& service_path, 175 const std::string& error_name); 176 177 // Calls Shill.Manager.Disconnect asynchronously. 178 void CallShillDisconnect( 179 const std::string& service_path, 180 const base::Closure& success_callback, 181 const network_handler::ErrorCallback& error_callback); 182 183 // Handle success from Shill.Service.Disconnect. 184 void HandleShillDisconnectSuccess(const std::string& service_path, 185 const base::Closure& success_callback); 186 187 // Calls Shill.Manager.Activate asynchronously. 188 void CallShillActivate( 189 const std::string& service_path, 190 const std::string& carrier, 191 const base::Closure& success_callback, 192 const network_handler::ErrorCallback& error_callback); 193 194 // Handle success from Shill.Service.ActivateCellularModem. 195 void HandleShillActivateSuccess(const std::string& service_path, 196 const base::Closure& success_callback); 197 198 // Local references to the associated handler instances. 199 CertLoader* cert_loader_; 200 NetworkStateHandler* network_state_handler_; 201 NetworkConfigurationHandler* network_configuration_handler_; 202 203 // Map of pending connect requests, used to prevent repeated attempts while 204 // waiting for Shill and to trigger callbacks on eventual success or failure. 205 std::map<std::string, ConnectRequest> pending_requests_; 206 scoped_ptr<ConnectRequest> queued_connect_; 207 208 // Track certificate loading state. 209 bool logged_in_; 210 bool certificates_loaded_; 211 212 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); 213}; 214 215} // namespace chromeos 216 217#endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 218