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