Update broken references to image assets
[chromium-blink-merge.git] / chromeos / network / network_state_handler.h
blobd7d1acd14a7d5067d738f9d91e4b042df5ab1780
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
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"
25 namespace base {
26 class DictionaryValue;
27 class ListValue;
28 class Value;
31 namespace tracked_objects {
32 class Location;
35 namespace chromeos {
37 class DeviceState;
38 class NetworkState;
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
51 // changes occur.
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
60 // across sessions.
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 {
67 public:
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,
74 TECHNOLOGY_AVAILABLE,
75 TECHNOLOGY_UNINITIALIZED,
76 TECHNOLOGY_ENABLING,
77 TECHNOLOGY_ENABLED
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,
103 bool enabled,
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
125 // differ.
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,
165 bool visible_only,
166 int limit,
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
210 // only set it.
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 // Sets the |last_error_| property of the matching NetworkState for tests.
228 void SetLastErrorForTest(const std::string& service_path,
229 const std::string& error);
231 // Constructs and initializes an instance for testing.
232 static NetworkStateHandler* InitializeForTest();
234 // Default set of comma separated interfaces on which to enable
235 // portal checking.
236 static const char kDefaultCheckPortalList[];
238 protected:
239 friend class NetworkHandler;
240 NetworkStateHandler();
242 // ShillPropertyHandler::Listener overrides.
244 // This adds new entries to |network_list_| or |device_list_| and deletes any
245 // entries that are no longer in the list.
246 void UpdateManagedList(ManagedState::ManagedType type,
247 const base::ListValue& entries) override;
249 // The list of profiles changed (i.e. a user has logged in). Re-request
250 // properties for all services since they may have changed.
251 void ProfileListChanged() override;
253 // Parses the properties for the network service or device. Mostly calls
254 // managed->PropertyChanged(key, value) for each dictionary entry.
255 void UpdateManagedStateProperties(
256 ManagedState::ManagedType type,
257 const std::string& path,
258 const base::DictionaryValue& properties) override;
260 // Called by ShillPropertyHandler when a watched service property changes.
261 void UpdateNetworkServiceProperty(const std::string& service_path,
262 const std::string& key,
263 const base::Value& value) override;
265 // Called by ShillPropertyHandler when a watched device property changes.
266 void UpdateDeviceProperty(const std::string& device_path,
267 const std::string& key,
268 const base::Value& value) override;
270 // Called by ShillPropertyHandler when a watched network or device
271 // IPConfig property changes.
272 void UpdateIPConfigProperties(
273 ManagedState::ManagedType type,
274 const std::string& path,
275 const std::string& ip_config_path,
276 const base::DictionaryValue& properties) override;
278 // Called by ShillPropertyHandler when the portal check list manager property
279 // changes.
280 void CheckPortalListChanged(const std::string& check_portal_list) override;
282 // Called by ShillPropertyHandler when a technology list changes.
283 void TechnologyListChanged() override;
285 // Called by |shill_property_handler_| when the service or device list has
286 // changed and all entries have been updated. This updates the list and
287 // notifies observers.
288 void ManagedStateListChanged(ManagedState::ManagedType type) override;
290 // Called when the default network service changes. Sets default_network_path_
291 // and notifies listeners.
292 void DefaultNetworkServiceChanged(const std::string& service_path) override;
294 // Called after construction. Called explicitly by tests after adding
295 // test observers.
296 void InitShillPropertyHandler();
298 private:
299 typedef std::map<std::string, std::string> SpecifierGuidMap;
300 friend class NetworkStateHandlerTest;
301 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub);
303 // Sorts the network list. Called when all network updates have been received,
304 // or when the network list is requested but the list is in an unsorted state.
305 // Networks are sorted as follows, maintaining the existing relative ordering:
306 // * Connected or connecting networks (should be listed first by Shill)
307 // * Visible non-wifi networks
308 // * Visible wifi networks
309 // * Hidden (wifi) networks
310 void SortNetworkList();
312 // Updates UMA stats. Called once after all requested networks are updated.
313 void UpdateNetworkStats();
315 // NetworkState specific method for UpdateManagedStateProperties which
316 // notifies observers.
317 void UpdateNetworkStateProperties(NetworkState* network,
318 const base::DictionaryValue& properties);
320 // Ensure a valid GUID for NetworkState.
321 void UpdateGuid(NetworkState* network);
323 // Sends DeviceListChanged() to observers and logs an event.
324 void NotifyDeviceListChanged();
326 // Non-const getters for managed entries. These are const so that they can
327 // be called by Get[Network|Device]State, even though they return non-const
328 // pointers.
329 DeviceState* GetModifiableDeviceState(const std::string& device_path) const;
330 NetworkState* GetModifiableNetworkState(
331 const std::string& service_path) const;
332 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list,
333 const std::string& path) const;
335 // Gets the list specified by |type|.
336 ManagedStateList* GetManagedList(ManagedState::ManagedType type);
338 // Helper function to notify observers. Calls CheckDefaultNetworkChanged().
339 void OnNetworkConnectionStateChanged(NetworkState* network);
341 // Notifies observers when the default network or its properties change.
342 void NotifyDefaultNetworkChanged(const NetworkState* default_network);
344 // Notifies observers about changes to |network|, including IPConfg.
345 void NotifyNetworkPropertiesUpdated(const NetworkState* network);
347 // Notifies observers about changes to |device|, including IPConfigs.
348 void NotifyDevicePropertiesUpdated(const DeviceState* device);
350 // Called whenever Device.Scanning state transitions to false.
351 void NotifyScanCompleted(const DeviceState* device);
353 // Returns one technology type for |type|. This technology will be the
354 // highest priority technology in the type pattern.
355 std::string GetTechnologyForType(const NetworkTypePattern& type) const;
357 // Returns all the technology types for |type|.
358 ScopedVector<std::string> GetTechnologiesForType(
359 const NetworkTypePattern& type) const;
361 // Shill property handler instance, owned by this class.
362 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
364 // Observer list
365 base::ObserverList<NetworkStateHandlerObserver> observers_;
367 // List of managed network states
368 ManagedStateList network_list_;
370 // Set to true when the network list is sorted, cleared when network updates
371 // arrive. Used to trigger sorting when needed.
372 bool network_list_sorted_;
374 // List of managed device states
375 ManagedStateList device_list_;
377 // Keeps track of the default network for notifying observers when it changes.
378 std::string default_network_path_;
380 // List of interfaces on which portal check is enabled.
381 std::string check_portal_list_;
383 // Map of network specifiers to guids. Contains an entry for each
384 // NetworkState that is not saved in a profile.
385 SpecifierGuidMap specifier_guid_map_;
387 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
390 } // namespace chromeos
392 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_