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