network_state_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_STATE_HANDLER_H_ 6#define CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ 7 8#include <map> 9#include <set> 10#include <string> 11#include <vector> 12 13#include "base/callback_forward.h" 14#include "base/gtest_prod_util.h" 15#include "base/memory/scoped_ptr.h" 16#include "base/observer_list.h" 17#include "chromeos/chromeos_export.h" 18#include "chromeos/network/managed_state.h" 19#include "chromeos/network/network_handler.h" 20#include "chromeos/network/network_handler_callbacks.h" 21#include "chromeos/network/shill_property_handler.h" 22 23namespace base { 24class DictionaryValue; 25class ListValue; 26class Value; 27} 28 29namespace chromeos { 30 31class DeviceState; 32class NetworkState; 33class NetworkStateHandlerObserver; 34class NetworkStateHandlerTest; 35 36// Class for tracking the list of visible networks and their properties. 37// 38// This class maps essential properties from the connection manager (Shill) for 39// each visible network. It is not used to change the properties of services or 40// devices, only global (manager) properties. 41// 42// All getters return the currently cached properties. This class is expected to 43// keep properties up to date by managing the appropriate Shill observers. 44// It will invoke its own more specific observer methods when the specified 45// changes occur. 46// 47// Most *ByType or *ForType methods will accept any of the following for 48// |type|. See individual methods for specific notes. 49// * Any type defined in service_constants.h (e.g. flimflam::kTypeWifi) 50// * kMatchTypeDefault returns the default (active) network 51// * kMatchTypeNonVirtual returns the primary non virtual network 52// * kMatchTypeWireless returns the primary wireless network 53// * kMatchTypeMobile returns the primary cellular or wimax network 54 55class CHROMEOS_EXPORT NetworkStateHandler 56 : public internal::ShillPropertyHandler::Listener { 57 public: 58 typedef std::vector<ManagedState*> ManagedStateList; 59 typedef std::vector<const NetworkState*> NetworkStateList; 60 61 enum TechnologyState { 62 TECHNOLOGY_UNAVAILABLE, 63 TECHNOLOGY_AVAILABLE, 64 TECHNOLOGY_UNINITIALIZED, 65 TECHNOLOGY_ENABLING, 66 TECHNOLOGY_ENABLED 67 }; 68 69 virtual ~NetworkStateHandler(); 70 71 // Add/remove observers. 72 void AddObserver(NetworkStateHandlerObserver* observer); 73 void RemoveObserver(NetworkStateHandlerObserver* observer); 74 75 // Returns the state for technology |type|. kMatchTypeMobile (only) is 76 // also supported. 77 TechnologyState GetTechnologyState(const std::string& type) const; 78 bool IsTechnologyEnabled(const std::string& type) const { 79 return GetTechnologyState(type) == TECHNOLOGY_ENABLED; 80 } 81 82 // Asynchronously sets the technology enabled property for |type|. 83 // kMatchTypeMobile (only) is also supported. 84 // Note: Modifies Manager state. Calls |error_callback| on failure. 85 void SetTechnologyEnabled( 86 const std::string& type, 87 bool enabled, 88 const network_handler::ErrorCallback& error_callback); 89 90 // Finds and returns a device state by |device_path| or NULL if not found. 91 const DeviceState* GetDeviceState(const std::string& device_path) const; 92 93 // Finds and returns a device state by |type|. Returns NULL if not found. 94 // See note above for valid types. 95 const DeviceState* GetDeviceStateByType(const std::string& type) const; 96 97 // Returns true if any device of |type| is scanning. 98 // See note above for valid types. 99 bool GetScanningByType(const std::string& type) const; 100 101 // Finds and returns a network state by |service_path| or NULL if not found. 102 // Note: NetworkState is frequently updated asynchronously, i.e. properties 103 // are not always updated all at once. This will contain the most recent 104 // value for each property. To receive notifications when a property changes, 105 // observe this class and implement NetworkPropertyChanged(). 106 const NetworkState* GetNetworkState(const std::string& service_path) const; 107 108 // Returns the default connected network (which includes VPNs) or NULL. 109 // This is equivalent to ConnectedNetworkByType(kMatchTypeDefault). 110 const NetworkState* DefaultNetwork() const; 111 112 // Returns the primary connected network of matching |type|, otherwise NULL. 113 // See note above for valid types. 114 const NetworkState* ConnectedNetworkByType(const std::string& type) const; 115 116 // Like ConnectedNetworkByType() but returns a connecting network or NULL. 117 const NetworkState* ConnectingNetworkByType(const std::string& type) const; 118 119 // Like ConnectedNetworkByType() but returns any matching network or NULL. 120 // Mostly useful for mobile networks where there is generally only one 121 // network. Note: O(N). 122 const NetworkState* FirstNetworkByType(const std::string& type) const; 123 124 // Returns the hardware (MAC) address for the first connected network 125 // matching |type|, or an empty string if none. 126 // See note above for valid types. 127 std::string HardwareAddressForType(const std::string& type) const; 128 // Same as above but in aa:bb format. 129 std::string FormattedHardwareAddressForType(const std::string& type) const; 130 131 // Sets |list| to contain the list of networks. The returned list contains 132 // a copy of NetworkState pointers which should not be stored or used beyond 133 // the scope of the calling function (i.e. they may later become invalid, but 134 // only on the UI thread). 135 void GetNetworkList(NetworkStateList* list) const; 136 137 // Requests a network scan. This may trigger updates to the network 138 // list, which will trigger the appropriate observer calls. 139 void RequestScan() const; 140 141 // Request a scan if not scanning and run |callback| when the Scanning state 142 // for any Device matching |type| completes. 143 void WaitForScan(const std::string& type, const base::Closure& callback); 144 145 // Request a network scan then signal Shill to connect to the best available 146 // networks when completed. 147 void ConnectToBestWifiNetwork(); 148 149 // Request an update for an existing NetworkState, e.g. after configuring 150 // a network. This is a no-op if an update request is already pending. 151 // Returns true if the network exists and an update is requested or pending. 152 // When the properties are received, NetworkPropertiesUpdated will be 153 // signaled for each member of |observers_|, regardless of whether any 154 // properties actually changed. 155 bool RequestUpdateForNetwork(const std::string& service_path); 156 157 // Request an update for all existing NetworkState entries, e.g. after 158 // loading an ONC configuration file that may have updated one or more 159 // existing networks. 160 void RequestUpdateForAllNetworks(); 161 162 // Set the user initiated connecting network. 163 void SetConnectingNetwork(const std::string& service_path); 164 165 // Set the list of devices on which portal check is enabled. 166 void SetCheckPortalList(const std::string& check_portal_list); 167 168 const std::string& connecting_network() const { return connecting_network_; } 169 const std::string& check_portal_list() const { return check_portal_list_; } 170 171 // Generates a DictionaryValue of all NetworkState properties. Currently 172 // provided for debugging purposes only. 173 void GetNetworkStatePropertiesForTest( 174 base::DictionaryValue* dictionary) const; 175 176 // Construct and initialize an instance for testing. 177 static NetworkStateHandler* InitializeForTest(); 178 179 static const char kMatchTypeDefault[]; 180 static const char kMatchTypeWireless[]; 181 static const char kMatchTypeMobile[]; 182 static const char kMatchTypeNonVirtual[]; 183 184 // Default set of comma separated interfaces on which to enable 185 // portal checking. 186 static const char kDefaultCheckPortalList[]; 187 188 protected: 189 friend class NetworkHandler; 190 NetworkStateHandler(); 191 192 // ShillPropertyHandler::Listener overrides. 193 194 // This adds new entries to the managed list specified by |type| and deletes 195 // any entries that are no longer in the list. 196 virtual void UpdateManagedList(ManagedState::ManagedType type, 197 const base::ListValue& entries) OVERRIDE; 198 199 // The list of profiles changed (i.e. a user has logged in). Re-request 200 // properties for all services since they may have changed. 201 virtual void ProfileListChanged() OVERRIDE; 202 203 // Parses the properties for the network service or device. Mostly calls 204 // managed->PropertyChanged(key, value) for each dictionary entry. 205 virtual void UpdateManagedStateProperties( 206 ManagedState::ManagedType type, 207 const std::string& path, 208 const base::DictionaryValue& properties) OVERRIDE; 209 210 // Called by ShillPropertyHandler when a watched service property changes. 211 virtual void UpdateNetworkServiceProperty( 212 const std::string& service_path, 213 const std::string& key, 214 const base::Value& value) OVERRIDE; 215 216 // Called by ShillPropertyHandler when a watched device property changes. 217 virtual void UpdateDeviceProperty( 218 const std::string& device_path, 219 const std::string& key, 220 const base::Value& value) OVERRIDE; 221 222 // Called by ShillPropertyHandler when the portal check list manager property 223 // changes. 224 virtual void CheckPortalListChanged( 225 const std::string& check_portal_list) OVERRIDE; 226 227 // Sends NetworkManagerChanged() to observers and logs an event. 228 virtual void NotifyManagerPropertyChanged() OVERRIDE; 229 230 // Called by |shill_property_handler_| when the service or device list has 231 // changed and all entries have been updated. This updates the list and 232 // notifies observers. If |type| == TYPE_NETWORK this also calls 233 // CheckDefaultNetworkChanged(). 234 virtual void ManagedStateListChanged( 235 ManagedState::ManagedType type) OVERRIDE; 236 237 // Called after construction. Called explicitly by tests after adding 238 // test observers. 239 void InitShillPropertyHandler(); 240 241 private: 242 typedef std::list<base::Closure> ScanCallbackList; 243 typedef std::map<std::string, ScanCallbackList> ScanCompleteCallbackMap; 244 friend class NetworkStateHandlerTest; 245 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); 246 247 // Non-const getters for managed entries. These are const so that they can 248 // be called by Get[Network|Device]State, even though they return non-const 249 // pointers. 250 DeviceState* GetModifiableDeviceState(const std::string& device_path) const; 251 NetworkState* GetModifiableNetworkState( 252 const std::string& service_path) const; 253 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list, 254 const std::string& path) const; 255 256 // Gets the list specified by |type|. 257 ManagedStateList* GetManagedList(ManagedState::ManagedType type); 258 259 // Helper function to notify observers. Calls CheckDefaultNetworkChanged(). 260 void OnNetworkConnectionStateChanged(NetworkState* network); 261 262 // If the default network changed returns true and sets 263 // |default_network_path_|. 264 bool CheckDefaultNetworkChanged(); 265 266 // Logs an event and notifies observers. 267 void OnDefaultNetworkChanged(); 268 269 // Notifies observers and updates connecting_network_. 270 void NetworkPropertiesUpdated(const NetworkState* network); 271 272 // Called whenever Device.Scanning state transitions to false. 273 void ScanCompleted(const std::string& type); 274 275 // Returns the technology type for |type|. 276 std::string GetTechnologyForType(const std::string& type) const; 277 278 // Shill property handler instance, owned by this class. 279 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_; 280 281 // Observer list 282 ObserverList<NetworkStateHandlerObserver> observers_; 283 284 // Lists of managed states 285 ManagedStateList network_list_; 286 ManagedStateList device_list_; 287 288 // Keeps track of the default network for notifying observers when it changes. 289 std::string default_network_path_; 290 291 // Convenience member to track the user initiated connecting network. Set 292 // externally when a connection is requested and cleared here when the state 293 // changes to something other than Connecting (after observers are notified). 294 // TODO(stevenjb): Move this to NetworkConfigurationHandler. 295 std::string connecting_network_; 296 297 // List of interfaces on which portal check is enabled. 298 std::string check_portal_list_; 299 300 // Callbacks to run when a scan for the technology type completes. 301 ScanCompleteCallbackMap scan_complete_callbacks_; 302 303 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); 304}; 305 306} // namespace chromeos 307 308#endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ 309