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