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