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 23 namespace base { 24 class DictionaryValue; 25 class ListValue; 26 class Value; 27 } 28 29 namespace tracked_objects { 30 class Location; 31 } 32 33 namespace chromeos { 34 35 class DeviceState; 36 class NetworkState; 37 class NetworkStateHandlerObserver; 38 class NetworkStateHandlerTest; 39 class 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 61 class 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 connected network (which includes VPNs) or NULL. 125 // This is equivalent to ConnectedNetworkByType(kMatchTypeDefault). 126 const NetworkState* DefaultNetwork() const; 127 128 // Returns the FavoriteState associated to DefaultNetwork. Returns NULL if, 129 // and only if, DefaultNetwork returns NULL. 130 const FavoriteState* DefaultFavoriteNetwork() const; 131 132 // Returns the primary connected network of matching |type|, otherwise NULL. 133 const NetworkState* ConnectedNetworkByType( 134 const NetworkTypePattern& type) const; 135 136 // Like ConnectedNetworkByType() but returns a connecting network or NULL. 137 const NetworkState* ConnectingNetworkByType( 138 const NetworkTypePattern& type) const; 139 140 // Like ConnectedNetworkByType() but returns any matching network or NULL. 141 // Mostly useful for mobile networks where there is generally only one 142 // network. Note: O(N). 143 const NetworkState* FirstNetworkByType(const NetworkTypePattern& type) const; 144 145 // Returns the aa:bb formatted hardware (MAC) address for the first connected 146 // network matching |type|, or an empty string if none is connected. 147 std::string FormattedHardwareAddressForType( 148 const NetworkTypePattern& type) const; 149 150 // Sets |list| to contain the list of networks. The returned list contains 151 // a copy of NetworkState pointers which should not be stored or used beyond 152 // the scope of the calling function (i.e. they may later become invalid, but 153 // only on the UI thread). 154 void GetNetworkList(NetworkStateList* list) const; 155 156 // Like GetNetworkList() but only returns networks with matching |type|. 157 void GetNetworkListByType(const NetworkTypePattern& type, 158 NetworkStateList* list) const; 159 160 // Sets |list| to contain the list of devices. The returned list contains 161 // a copy of DeviceState pointers which should not be stored or used beyond 162 // the scope of the calling function (i.e. they may later become invalid, but 163 // only on the UI thread). 164 void GetDeviceList(DeviceStateList* list) const; 165 166 // Like GetDeviceList() but only returns networks with matching |type|. 167 void GetDeviceListByType(const NetworkTypePattern& type, 168 DeviceStateList* list) const; 169 170 // Sets |list| to contain the list of favorite (aka "preferred") networks. 171 // See GetNetworkList() for usage, and notes for |favorite_list_|. 172 // Favorites that are visible have the same path() as the entries in 173 // GetNetworkList(), so GetNetworkState() can be used to determine if a 174 // favorite is visible and retrieve the complete properties (and vice-versa). 175 void GetFavoriteList(FavoriteStateList* list) const; 176 177 // Like GetFavoriteList() but only returns favorites with matching |type|. 178 void GetFavoriteListByType(const NetworkTypePattern& type, 179 FavoriteStateList* list) const; 180 181 // Finds and returns a favorite state by |service_path| or NULL if not found. 182 const FavoriteState* GetFavoriteState(const std::string& service_path) const; 183 184 // Requests a network scan. This may trigger updates to the network 185 // list, which will trigger the appropriate observer calls. 186 void RequestScan() const; 187 188 // Request a scan if not scanning and run |callback| when the Scanning state 189 // for any Device of network type |type| completes. 190 void WaitForScan(const std::string& type, const base::Closure& callback); 191 192 // Request a network scan then signal Shill to connect to the best available 193 // networks when completed. 194 void ConnectToBestWifiNetwork(); 195 196 // Request an update for an existing NetworkState, e.g. after configuring 197 // a network. This is a no-op if an update request is already pending. To 198 // ensure that a change is picked up, this must be called after Shill 199 // acknowledged it (e.g. in the callback of a SetProperties). 200 // When the properties are received, NetworkPropertiesUpdated will be 201 // signaled for each member of |observers_|, regardless of whether any 202 // properties actually changed. 203 void RequestUpdateForNetwork(const std::string& service_path); 204 205 // Request an update for all existing NetworkState entries, e.g. after 206 // loading an ONC configuration file that may have updated one or more 207 // existing networks. 208 void RequestUpdateForAllNetworks(); 209 210 // Set the list of devices on which portal check is enabled. 211 void SetCheckPortalList(const std::string& check_portal_list); 212 213 const std::string& GetCheckPortalListForTest() const { 214 return check_portal_list_; 215 } 216 217 // Returns the FavoriteState of the EthernetEAP service, which contains the 218 // EAP parameters used by the ethernet with |service_path|. If |service_path| 219 // doesn't refer to an ethernet service or if the ethernet service is not 220 // connected using EAP, returns NULL. 221 const FavoriteState* GetEAPForEthernet(const std::string& service_path) const; 222 223 // Generates a DictionaryValue of all NetworkState properties. Currently 224 // provided for debugging purposes only. 225 void GetNetworkStatePropertiesForTest( 226 base::DictionaryValue* dictionary) const; 227 228 // Construct and initialize an instance for testing. 229 static NetworkStateHandler* InitializeForTest(); 230 231 // Default set of comma separated interfaces on which to enable 232 // portal checking. 233 static const char kDefaultCheckPortalList[]; 234 235 protected: 236 friend class NetworkHandler; 237 NetworkStateHandler(); 238 239 // ShillPropertyHandler::Listener overrides. 240 241 // This adds new entries to the managed list specified by |type| and deletes 242 // any entries that are no longer in the list. 243 virtual void UpdateManagedList(ManagedState::ManagedType type, 244 const base::ListValue& entries) OVERRIDE; 245 246 // The list of profiles changed (i.e. a user has logged in). Re-request 247 // properties for all services since they may have changed. 248 virtual void ProfileListChanged() OVERRIDE; 249 250 // Parses the properties for the network service or device. Mostly calls 251 // managed->PropertyChanged(key, value) for each dictionary entry. 252 virtual void UpdateManagedStateProperties( 253 ManagedState::ManagedType type, 254 const std::string& path, 255 const base::DictionaryValue& properties) OVERRIDE; 256 257 // Called by ShillPropertyHandler when a watched service property changes. 258 virtual void UpdateNetworkServiceProperty( 259 const std::string& service_path, 260 const std::string& key, 261 const base::Value& value) OVERRIDE; 262 263 // Called by ShillPropertyHandler when a watched device property changes. 264 virtual void UpdateDeviceProperty( 265 const std::string& device_path, 266 const std::string& key, 267 const base::Value& value) OVERRIDE; 268 269 // Called by ShillPropertyHandler when the portal check list manager property 270 // changes. 271 virtual void CheckPortalListChanged( 272 const std::string& check_portal_list) OVERRIDE; 273 274 // Called by ShillPropertyHandler when a technology list changes. 275 virtual void TechnologyListChanged() OVERRIDE; 276 277 // Called by |shill_property_handler_| when the service or device list has 278 // changed and all entries have been updated. This updates the list and 279 // notifies observers. If |type| == TYPE_NETWORK this also calls 280 // CheckDefaultNetworkChanged(). 281 virtual void ManagedStateListChanged( 282 ManagedState::ManagedType type) OVERRIDE; 283 284 // Called after construction. Called explicitly by tests after adding 285 // test observers. 286 void InitShillPropertyHandler(); 287 288 private: 289 typedef std::list<base::Closure> ScanCallbackList; 290 typedef std::map<std::string, ScanCallbackList> ScanCompleteCallbackMap; 291 friend class NetworkStateHandlerTest; 292 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); 293 294 // NetworkState specific method for UpdateManagedStateProperties which 295 // notifies observers. 296 void UpdateNetworkStateProperties(NetworkState* network, 297 const base::DictionaryValue& properties); 298 299 // Sends DeviceListChanged() to observers and logs an event. 300 void NotifyDeviceListChanged(); 301 302 // Non-const getters for managed entries. These are const so that they can 303 // be called by Get[Network|Device]State, even though they return non-const 304 // pointers. 305 DeviceState* GetModifiableDeviceState(const std::string& device_path) const; 306 NetworkState* GetModifiableNetworkState( 307 const std::string& service_path) const; 308 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list, 309 const std::string& path) const; 310 311 // Gets the list specified by |type|. 312 ManagedStateList* GetManagedList(ManagedState::ManagedType type); 313 314 // Helper function to notify observers. Calls CheckDefaultNetworkChanged(). 315 void OnNetworkConnectionStateChanged(NetworkState* network); 316 317 // If the default network changed returns true and sets 318 // |default_network_path_|. 319 bool CheckDefaultNetworkChanged(); 320 321 // Logs an event and notifies observers. 322 void OnDefaultNetworkChanged(); 323 324 // Notifies observers about changes to |network|. 325 void NetworkPropertiesUpdated(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