network_connection_handler.h revision 868fa2fe829687343ffae624259930155e16dbd8
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/chromeos_export.h" 16#include "chromeos/dbus/dbus_method_call_status.h" 17#include "chromeos/network/network_handler.h" 18#include "chromeos/network/network_handler_callbacks.h" 19#include "chromeos/network/network_state_handler_observer.h" 20 21namespace chromeos { 22 23class NetworkState; 24 25// The NetworkConnectionHandler class is used to manage network connection 26// requests. This is the only class that should make Shill Connect calls. 27// It handles the following steps: 28// 1. Determine whether or not sufficient information (e.g. passphrase) is 29// known to be available to connect to the network. 30// 2. Request additional information (e.g. user data which contains certificate 31// information) and determine whether sufficient information is available. 32// 3. Send the connect request. 33// 4. Wait for the network state to change to a non connecting state. 34// 5. Invoke the appropriate callback (always) on success or failure. 35// 36// NetworkConnectionHandler depends on NetworkStateHandler for immediately 37// available State information, and NetworkConfigurationHandler for any 38// configuration calls. 39 40class CHROMEOS_EXPORT NetworkConnectionHandler 41 : public NetworkStateHandlerObserver, 42 public base::SupportsWeakPtr<NetworkConnectionHandler> { 43 public: 44 // Constants for |error_name| from |error_callback| for Connect. 45 static const char kErrorNotFound[]; 46 static const char kErrorConnected[]; 47 static const char kErrorConnecting[]; 48 static const char kErrorPassphraseRequired[]; 49 static const char kErrorActivationRequired[]; 50 static const char kErrorCertificateRequired[]; 51 static const char kErrorConfigurationRequired[]; 52 static const char kErrorShillError[]; 53 static const char kErrorConnectFailed[]; 54 static const char kErrorUnknown[]; 55 56 // Constants for |error_name| from |error_callback| for Disconnect. 57 static const char kErrorNotConnected[]; 58 59 virtual ~NetworkConnectionHandler(); 60 61 // ConnectToNetwork() will start an asynchronous connection attempt. 62 // On success, |success_callback| will be called. 63 // On failure, |error_callback| will be called with |error_name| one of: 64 // kErrorNotFound if no network matching |service_path| is found 65 // (hidden networks must be configured before connecting). 66 // kErrorConnected if already connected to the network. 67 // kErrorConnecting if already connecting to the network. 68 // kErrorCertificateRequired if the network requires a cert and none exists. 69 // kErrorPassphraseRequired if passphrase only is required. 70 // kErrorConfigurationRequired if additional configuration is required. 71 // kErrorShillError if a DBus or Shill error occurred. 72 // |error_message| will contain an additional error string for debugging. 73 // If |ignore_error_state| is true, error state for the network is ignored 74 // (e.g. for repeat attempts). 75 void ConnectToNetwork(const std::string& service_path, 76 const base::Closure& success_callback, 77 const network_handler::ErrorCallback& error_callback, 78 bool ignore_error_state); 79 80 // DisconnectToNetwork() will send a Disconnect request to Shill. 81 // On success, |success_callback| will be called. 82 // On failure, |error_callback| will be called with |error_name| one of: 83 // kErrorNotFound if no network matching |service_path| is found. 84 // kErrorNotConnected if not connected to the network. 85 // kErrorShillError if a DBus or Shill error occurred. 86 // |error_message| will contain and additional error string for debugging. 87 void DisconnectNetwork(const std::string& service_path, 88 const base::Closure& success_callback, 89 const network_handler::ErrorCallback& error_callback); 90 91 // Returns true if ConnectToNetwork has been called with |service_path| and 92 // has not completed (i.e. success or error callback has been called). 93 bool HasConnectingNetwork(const std::string& service_path); 94 95 // NetworkStateHandlerObserver 96 virtual void NetworkListChanged() OVERRIDE; 97 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE; 98 99 private: 100 friend class NetworkHandler; 101 friend class NetworkConnectionHandlerTest; 102 103 struct ConnectRequest; 104 105 NetworkConnectionHandler(); 106 107 void Init(NetworkStateHandler* network_state_handler, 108 NetworkConfigurationHandler* network_configuration_handler); 109 110 ConnectRequest* pending_request(const std::string& service_path); 111 112 // Callback from Shill.Service.GetProperties. Parses |properties| to verify 113 // whether or not the network appears to be configured. If configured, 114 // attempts a connection, otherwise invokes error_callback from 115 // pending_requests_[service_path]. 116 void VerifyConfiguredAndConnect(const std::string& service_path, 117 const base::DictionaryValue& properties); 118 119 // Calls Shill.Manager.Connect asynchronously. 120 void CallShillConnect(const std::string& service_path); 121 122 // Handle failure from ConfigurationHandler calls. 123 void HandleConfigurationFailure( 124 const std::string& service_path, 125 const std::string& error_name, 126 scoped_ptr<base::DictionaryValue> error_data); 127 128 // Handle success or failure from Shill.Service.Connect. 129 void HandleShillConnectSuccess(const std::string& service_path); 130 void HandleShillConnectFailure(const std::string& service_path, 131 const std::string& error_name, 132 const std::string& error_message); 133 134 void CheckPendingRequest(const std::string service_path); 135 void CheckAllPendingRequests(); 136 137 // Calls Shill.Manager.Disconnect asynchronously. 138 void CallShillDisconnect( 139 const std::string& service_path, 140 const base::Closure& success_callback, 141 const network_handler::ErrorCallback& error_callback); 142 143 // Handle success or failure from Shill.Service.Disconnect. 144 void HandleShillDisconnectSuccess(const std::string& service_path, 145 const base::Closure& success_callback); 146 void HandleShillDisconnectFailure( 147 const std::string& service_path, 148 const network_handler::ErrorCallback& error_callback, 149 const std::string& error_name, 150 const std::string& error_message); 151 152 // Local references to the associated handler instances. 153 NetworkStateHandler* network_state_handler_; 154 NetworkProfileHandler* network_profile_handler_; 155 NetworkConfigurationHandler* network_configuration_handler_; 156 157 // Map of pending connect requests, used to prevent repeat attempts while 158 // waiting for Shill and to trigger callbacks on eventual success or failure. 159 std::map<std::string, ConnectRequest> pending_requests_; 160 161 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler); 162}; 163 164} // namespace chromeos 165 166#endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_ 167