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.
5 #ifndef CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
6 #define CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
13 #include "base/callback_forward.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/observer_list.h"
18 #include "chromeos/chromeos_export.h"
19 #include "chromeos/network/managed_state.h"
20 #include "chromeos/network/network_handler.h"
21 #include "chromeos/network/network_handler_callbacks.h"
22 #include "chromeos/network/network_type_pattern.h"
23 #include "chromeos/network/shill_property_handler.h"
26 class DictionaryValue
;
31 namespace tracked_objects
{
39 class NetworkStateHandlerObserver
;
40 class NetworkStateHandlerTest
;
42 // Class for tracking the list of visible networks and their properties.
44 // This class maps essential properties from the connection manager (Shill) for
45 // each visible network. It is not used to change the properties of services or
46 // devices, only global (manager) properties.
48 // All getters return the currently cached properties. This class is expected to
49 // keep properties up to date by managing the appropriate Shill observers.
50 // It will invoke its own more specific observer methods when the specified
53 // Some notes about NetworkState and GUIDs:
54 // * A NetworkState exists for all network services stored in a profile, and
55 // all "visible" networks (physically connected networks like ethernet and
56 // cellular or in-range wifi networks). If the network is stored in a profile,
57 // NetworkState.IsInProfile() will return true.
58 // * "Visible" networks return true for NetworkState.visible().
59 // * All networks saved to a profile will have a saved GUID that is persistent
61 // * Networks that are not saved to a profile will have a GUID assigned when
62 // the initial properties are received. The GUID will be consistent for
63 // the duration of a session, even if the network drops out and returns.
65 class CHROMEOS_EXPORT NetworkStateHandler
66 : public internal::ShillPropertyHandler::Listener
{
68 typedef std::vector
<ManagedState
*> ManagedStateList
;
69 typedef std::vector
<const NetworkState
*> NetworkStateList
;
70 typedef std::vector
<const DeviceState
*> DeviceStateList
;
72 enum TechnologyState
{
73 TECHNOLOGY_UNAVAILABLE
,
75 TECHNOLOGY_UNINITIALIZED
,
80 ~NetworkStateHandler() override
;
82 // Add/remove observers.
83 void AddObserver(NetworkStateHandlerObserver
* observer
,
84 const tracked_objects::Location
& from_here
);
85 void RemoveObserver(NetworkStateHandlerObserver
* observer
,
86 const tracked_objects::Location
& from_here
);
88 // Returns the state for technology |type|. Only
89 // NetworkTypePattern::Primitive, ::Mobile and ::Ethernet are supported.
90 TechnologyState
GetTechnologyState(const NetworkTypePattern
& type
) const;
91 bool IsTechnologyAvailable(const NetworkTypePattern
& type
) const {
92 return GetTechnologyState(type
) != TECHNOLOGY_UNAVAILABLE
;
94 bool IsTechnologyEnabled(const NetworkTypePattern
& type
) const {
95 return GetTechnologyState(type
) == TECHNOLOGY_ENABLED
;
98 // Asynchronously sets the technology enabled property for |type|. Only
99 // NetworkTypePattern::Primitive, ::Mobile and ::Ethernet are supported.
100 // Note: Modifies Manager state. Calls |error_callback| on failure.
101 void SetTechnologyEnabled(
102 const NetworkTypePattern
& type
,
104 const network_handler::ErrorCallback
& error_callback
);
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;
109 // Finds and returns a device state by |type|. Returns NULL if not found.
110 const DeviceState
* GetDeviceStateByType(const NetworkTypePattern
& type
) const;
112 // Returns true if any device of |type| is scanning.
113 bool GetScanningByType(const NetworkTypePattern
& type
) const;
115 // Finds and returns a network state by |service_path| or NULL if not found.
116 // Note: NetworkState is frequently updated asynchronously, i.e. properties
117 // are not always updated all at once. This will contain the most recent
118 // value for each property. To receive notifications when a property changes,
119 // observe this class and implement NetworkPropertyChanged().
120 const NetworkState
* GetNetworkState(const std::string
& service_path
) const;
122 // Returns the default network (which includes VPNs) based on the Shill
123 // Manager.DefaultNetwork property. Normally this is the same as
124 // ConnectedNetworkByType(NetworkTypePattern::Default()), but the timing might
126 const NetworkState
* DefaultNetwork() const;
128 // Returns the primary connected network of matching |type|, otherwise NULL.
129 const NetworkState
* ConnectedNetworkByType(
130 const NetworkTypePattern
& type
) const;
132 // Like ConnectedNetworkByType() but returns a connecting network or NULL.
133 const NetworkState
* ConnectingNetworkByType(
134 const NetworkTypePattern
& type
) const;
136 // Like ConnectedNetworkByType() but returns any matching visible network or
137 // NULL. Mostly useful for mobile networks where there is generally only one
138 // network. Note: O(N).
139 const NetworkState
* FirstNetworkByType(const NetworkTypePattern
& type
);
141 // Returns the aa:bb formatted hardware (MAC) address for the first connected
142 // network matching |type|, or an empty string if none is connected.
143 std::string
FormattedHardwareAddressForType(
144 const NetworkTypePattern
& type
) const;
146 // Convenience method to call GetNetworkListByType(visible=true).
147 void GetVisibleNetworkListByType(const NetworkTypePattern
& type
,
148 NetworkStateList
* list
);
150 // Convenience method for GetVisibleNetworkListByType(Default).
151 void GetVisibleNetworkList(NetworkStateList
* list
);
153 // Sets |list| to contain the list of networks with matching |type| and the
154 // following properties:
155 // |configured_only| - if true only include networks where IsInProfile is true
156 // |visible_only| - if true only include networks in the visible Services list
157 // |limit| - if > 0 limits the number of results.
158 // The returned list contains a copy of NetworkState pointers which should not
159 // be stored or used beyond the scope of the calling function (i.e. they may
160 // later become invalid, but only on the UI thread). SortNetworkList() will be
161 // called if necessary to provide the states in a convenient order (see
162 // SortNetworkList for details).
163 void GetNetworkListByType(const NetworkTypePattern
& type
,
164 bool configured_only
,
167 NetworkStateList
* list
);
169 // Finds and returns the NetworkState associated with |service_path| or NULL
170 // if not found. If |configured_only| is true, only returns saved entries
171 // (IsInProfile is true).
172 const NetworkState
* GetNetworkStateFromServicePath(
173 const std::string
& service_path
,
174 bool configured_only
) const;
176 // Finds and returns the NetworkState associated with |guid| or NULL if not
177 // found. This returns all entries (IsInProfile() may be true or false).
178 const NetworkState
* GetNetworkStateFromGuid(const std::string
& guid
) const;
180 // Sets |list| to contain the list of devices. The returned list contains
181 // a copy of DeviceState pointers which should not be stored or used beyond
182 // the scope of the calling function (i.e. they may later become invalid, but
183 // only on the UI thread).
184 void GetDeviceList(DeviceStateList
* list
) const;
186 // Like GetDeviceList() but only returns networks with matching |type|.
187 void GetDeviceListByType(const NetworkTypePattern
& type
,
188 DeviceStateList
* list
) const;
190 // Requests a network scan. This may trigger updates to the network
191 // list, which will trigger the appropriate observer calls.
192 void RequestScan() const;
194 // Requests an update for an existing NetworkState, e.g. after configuring
195 // a network. This is a no-op if an update request is already pending. To
196 // ensure that a change is picked up, this must be called after Shill
197 // acknowledged it (e.g. in the callback of a SetProperties).
198 // When the properties are received, NetworkPropertiesUpdated will be
199 // signaled for each member of |observers_|, regardless of whether any
200 // properties actually changed.
201 void RequestUpdateForNetwork(const std::string
& service_path
);
203 // Clears the last_error value for the NetworkState for |service_path|.
204 void ClearLastErrorForNetwork(const std::string
& service_path
);
206 // Sets the list of devices on which portal check is enabled.
207 void SetCheckPortalList(const std::string
& check_portal_list
);
209 // Sets the Manager.WakeOnLan property. Note: we do not track this state, we
211 void SetWakeOnLanEnabled(bool enabled
);
213 const std::string
& GetCheckPortalListForTest() const {
214 return check_portal_list_
;
217 // Returns the NetworkState 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 NetworkState
* GetEAPForEthernet(const std::string
& service_path
);
223 const std::string
& default_network_path() const {
224 return default_network_path_
;
227 // Constructs and initializes an instance for testing.
228 static NetworkStateHandler
* InitializeForTest();
230 // Default set of comma separated interfaces on which to enable
232 static const char kDefaultCheckPortalList
[];
235 friend class NetworkHandler
;
236 NetworkStateHandler();
238 // ShillPropertyHandler::Listener overrides.
240 // This adds new entries to |network_list_| or |device_list_| and deletes any
241 // entries that are no longer in the list.
242 void UpdateManagedList(ManagedState::ManagedType type
,
243 const base::ListValue
& entries
) override
;
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 void ProfileListChanged() override
;
249 // Parses the properties for the network service or device. Mostly calls
250 // managed->PropertyChanged(key, value) for each dictionary entry.
251 void UpdateManagedStateProperties(
252 ManagedState::ManagedType type
,
253 const std::string
& path
,
254 const base::DictionaryValue
& properties
) override
;
256 // Called by ShillPropertyHandler when a watched service property changes.
257 void UpdateNetworkServiceProperty(const std::string
& service_path
,
258 const std::string
& key
,
259 const base::Value
& value
) override
;
261 // Called by ShillPropertyHandler when a watched device property changes.
262 void UpdateDeviceProperty(const std::string
& device_path
,
263 const std::string
& key
,
264 const base::Value
& value
) override
;
266 // Called by ShillPropertyHandler when a watched network or device
267 // IPConfig property changes.
268 void UpdateIPConfigProperties(
269 ManagedState::ManagedType type
,
270 const std::string
& path
,
271 const std::string
& ip_config_path
,
272 const base::DictionaryValue
& properties
) override
;
274 // Called by ShillPropertyHandler when the portal check list manager property
276 void CheckPortalListChanged(const std::string
& check_portal_list
) override
;
278 // Called by ShillPropertyHandler when a technology list changes.
279 void TechnologyListChanged() override
;
281 // Called by |shill_property_handler_| when the service or device list has
282 // changed and all entries have been updated. This updates the list and
283 // notifies observers.
284 void ManagedStateListChanged(ManagedState::ManagedType type
) override
;
286 // Called when the default network service changes. Sets default_network_path_
287 // and notifies listeners.
288 void DefaultNetworkServiceChanged(const std::string
& service_path
) override
;
290 // Called after construction. Called explicitly by tests after adding
292 void InitShillPropertyHandler();
295 typedef std::map
<std::string
, std::string
> SpecifierGuidMap
;
296 friend class NetworkStateHandlerTest
;
297 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest
, NetworkStateHandlerStub
);
299 // Sorts the network list. Called when all network updates have been received,
300 // or when the network list is requested but the list is in an unsorted state.
301 // Networks are sorted as follows, maintaining the existing relative ordering:
302 // * Connected or connecting networks (should be listed first by Shill)
303 // * Visible non-wifi networks
304 // * Visible wifi networks
305 // * Hidden (wifi) networks
306 void SortNetworkList();
308 // Updates UMA stats. Called once after all requested networks are updated.
309 void UpdateNetworkStats();
311 // NetworkState specific method for UpdateManagedStateProperties which
312 // notifies observers.
313 void UpdateNetworkStateProperties(NetworkState
* network
,
314 const base::DictionaryValue
& properties
);
316 // Ensure a valid GUID for NetworkState.
317 void UpdateGuid(NetworkState
* network
);
319 // Sends DeviceListChanged() to observers and logs an event.
320 void NotifyDeviceListChanged();
322 // Non-const getters for managed entries. These are const so that they can
323 // be called by Get[Network|Device]State, even though they return non-const
325 DeviceState
* GetModifiableDeviceState(const std::string
& device_path
) const;
326 NetworkState
* GetModifiableNetworkState(
327 const std::string
& service_path
) const;
328 ManagedState
* GetModifiableManagedState(const ManagedStateList
* managed_list
,
329 const std::string
& path
) const;
331 // Gets the list specified by |type|.
332 ManagedStateList
* GetManagedList(ManagedState::ManagedType type
);
334 // Helper function to notify observers. Calls CheckDefaultNetworkChanged().
335 void OnNetworkConnectionStateChanged(NetworkState
* network
);
337 // Notifies observers when the default network or its properties change.
338 void NotifyDefaultNetworkChanged(const NetworkState
* default_network
);
340 // Notifies observers about changes to |network|, including IPConfg.
341 void NotifyNetworkPropertiesUpdated(const NetworkState
* network
);
343 // Notifies observers about changes to |device|, including IPConfigs.
344 void NotifyDevicePropertiesUpdated(const DeviceState
* device
);
346 // Called whenever Device.Scanning state transitions to false.
347 void NotifyScanCompleted(const DeviceState
* device
);
349 // Returns one technology type for |type|. This technology will be the
350 // highest priority technology in the type pattern.
351 std::string
GetTechnologyForType(const NetworkTypePattern
& type
) const;
353 // Returns all the technology types for |type|.
354 ScopedVector
<std::string
> GetTechnologiesForType(
355 const NetworkTypePattern
& type
) const;
357 // Shill property handler instance, owned by this class.
358 scoped_ptr
<internal::ShillPropertyHandler
> shill_property_handler_
;
361 ObserverList
<NetworkStateHandlerObserver
> observers_
;
363 // List of managed network states
364 ManagedStateList network_list_
;
366 // Set to true when the network list is sorted, cleared when network updates
367 // arrive. Used to trigger sorting when needed.
368 bool network_list_sorted_
;
370 // List of managed device states
371 ManagedStateList device_list_
;
373 // Keeps track of the default network for notifying observers when it changes.
374 std::string default_network_path_
;
376 // List of interfaces on which portal check is enabled.
377 std::string check_portal_list_
;
379 // Map of network specifiers to guids. Contains an entry for each
380 // NetworkState that is not saved in a profile.
381 SpecifierGuidMap specifier_guid_map_
;
383 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler
);
386 } // namespace chromeos
388 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_