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