Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chromeos / network / network_state_handler.h
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/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/shill_property_handler.h"
23
24 namespace base {
25 class DictionaryValue;
26 class ListValue;
27 class Value;
28 }
29
30 namespace tracked_objects {
31 class Location;
32 }
33
34 namespace chromeos {
35
36 class DeviceState;
37 class NetworkState;
38 class NetworkStateHandlerObserver;
39 class NetworkStateHandlerTest;
40 class NetworkTypePattern;
41
42 // Class for tracking the list of visible networks and their properties.
43 //
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.
47 //
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.
52
53 class CHROMEOS_EXPORT NetworkStateHandler
54     : public internal::ShillPropertyHandler::Listener {
55  public:
56   typedef std::vector<ManagedState*> ManagedStateList;
57   typedef std::vector<const NetworkState*> NetworkStateList;
58   typedef std::vector<const DeviceState*> DeviceStateList;
59   typedef std::vector<const FavoriteState*> FavoriteStateList;
60
61   enum TechnologyState {
62     TECHNOLOGY_UNAVAILABLE,
63     TECHNOLOGY_AVAILABLE,
64     TECHNOLOGY_UNINITIALIZED,
65     TECHNOLOGY_ENABLING,
66     TECHNOLOGY_ENABLED
67   };
68
69   virtual ~NetworkStateHandler();
70
71   // Add/remove observers.
72   void AddObserver(NetworkStateHandlerObserver* observer,
73                    const tracked_objects::Location& from_here);
74   void RemoveObserver(NetworkStateHandlerObserver* observer,
75                       const tracked_objects::Location& from_here);
76
77   // Requests all Manager properties, specifically to update the complete
78   // list of services which determines the list of Favorites. This should be
79   // called any time a new service is configured or a Profile is loaded.
80   void UpdateManagerProperties();
81
82   // Returns the state for technology |type|. Only
83   // NetworkTypePattern::Primitive, ::Mobile and ::Ethernet are supported.
84   TechnologyState GetTechnologyState(const NetworkTypePattern& type) const;
85   bool IsTechnologyAvailable(const NetworkTypePattern& type) const {
86     return GetTechnologyState(type) != TECHNOLOGY_UNAVAILABLE;
87   }
88   bool IsTechnologyEnabled(const NetworkTypePattern& type) const {
89     return GetTechnologyState(type) == TECHNOLOGY_ENABLED;
90   }
91
92   // Asynchronously sets the technology enabled property for |type|. Only
93   // NetworkTypePattern::Primitive, ::Mobile and ::Ethernet are supported.
94   // Note: Modifies Manager state. Calls |error_callback| on failure.
95   void SetTechnologyEnabled(
96       const NetworkTypePattern& type,
97       bool enabled,
98       const network_handler::ErrorCallback& error_callback);
99
100   // Finds and returns a device state by |device_path| or NULL if not found.
101   const DeviceState* GetDeviceState(const std::string& device_path) const;
102
103   // Finds and returns a device state by |type|. Returns NULL if not found.
104   const DeviceState* GetDeviceStateByType(const NetworkTypePattern& type) const;
105
106   // Returns true if any device of |type| is scanning.
107   bool GetScanningByType(const NetworkTypePattern& type) const;
108
109   // Finds and returns a network state by |service_path| or NULL if not found.
110   // Note: NetworkState is frequently updated asynchronously, i.e. properties
111   // are not always updated all at once. This will contain the most recent
112   // value for each property. To receive notifications when a property changes,
113   // observe this class and implement NetworkPropertyChanged().
114   const NetworkState* GetNetworkState(const std::string& service_path) const;
115
116   // Returns the default network (which includes VPNs) based on the Shill
117   // Manager.DefaultNetwork property. Normally this is the same as
118   // ConnectedNetworkByType(NetworkTypePattern::Default()), but the timing might
119   // differ.
120   const NetworkState* DefaultNetwork() const;
121
122   // Returns the FavoriteState associated to DefaultNetwork. Returns NULL if,
123   // and only if, DefaultNetwork returns NULL.
124   const FavoriteState* DefaultFavoriteNetwork() const;
125
126   // Returns the primary connected network of matching |type|, otherwise NULL.
127   const NetworkState* ConnectedNetworkByType(
128       const NetworkTypePattern& type) const;
129
130   // Like ConnectedNetworkByType() but returns a connecting network or NULL.
131   const NetworkState* ConnectingNetworkByType(
132       const NetworkTypePattern& type) const;
133
134   // Like ConnectedNetworkByType() but returns any matching network or NULL.
135   // Mostly useful for mobile networks where there is generally only one
136   // network. Note: O(N).
137   const NetworkState* FirstNetworkByType(const NetworkTypePattern& type) const;
138
139   // Returns the aa:bb formatted hardware (MAC) address for the first connected
140   // network matching |type|, or an empty string if none is connected.
141   std::string FormattedHardwareAddressForType(
142       const NetworkTypePattern& type) const;
143
144   // Sets |list| to contain the list of networks.  The returned list contains
145   // a copy of NetworkState pointers which should not be stored or used beyond
146   // the scope of the calling function (i.e. they may later become invalid, but
147   // only on the UI thread).
148   void GetNetworkList(NetworkStateList* list) const;
149
150   // Like GetNetworkList() but only returns networks with matching |type|.
151   void GetNetworkListByType(const NetworkTypePattern& type,
152                             NetworkStateList* list) const;
153
154   // Sets |list| to contain the list of devices.  The returned list contains
155   // a copy of DeviceState pointers which should not be stored or used beyond
156   // the scope of the calling function (i.e. they may later become invalid, but
157   // only on the UI thread).
158   void GetDeviceList(DeviceStateList* list) const;
159
160   // Like GetDeviceList() but only returns networks with matching |type|.
161   void GetDeviceListByType(const NetworkTypePattern& type,
162                            DeviceStateList* list) const;
163
164   // Sets |list| to contain the list of favorite (aka "preferred") networks.
165   // See GetNetworkList() for usage, and notes for |favorite_list_|.
166   // Favorites that are visible have the same path() as the entries in
167   // GetNetworkList(), so GetNetworkState() can be used to determine if a
168   // favorite is visible and retrieve the complete properties (and vice-versa).
169   void GetFavoriteList(FavoriteStateList* list) const;
170
171   // Like GetFavoriteList() but only returns favorites with matching |type|.
172   void GetFavoriteListByType(const NetworkTypePattern& type,
173                              FavoriteStateList* list) const;
174
175   // Finds and returns a favorite state by |service_path| or NULL if not found.
176   const FavoriteState* GetFavoriteState(const std::string& service_path) const;
177
178   // Requests a network scan. This may trigger updates to the network
179   // list, which will trigger the appropriate observer calls.
180   void RequestScan() const;
181
182   // Request a scan if not scanning and run |callback| when the Scanning state
183   // for any Device of network type |type| completes.
184   void WaitForScan(const std::string& type, const base::Closure& callback);
185
186   // Request a network scan then signal Shill to connect to the best available
187   // networks when completed.
188   void ConnectToBestWifiNetwork();
189
190   // Request an update for an existing NetworkState, e.g. after configuring
191   // a network. This is a no-op if an update request is already pending. To
192   // ensure that a change is picked up, this must be called after Shill
193   // acknowledged it (e.g. in the callback of a SetProperties).
194   // When the properties are received, NetworkPropertiesUpdated will be
195   // signaled for each member of |observers_|, regardless of whether any
196   // properties actually changed.
197   void RequestUpdateForNetwork(const std::string& service_path);
198
199   // Clear the last_error value for the NetworkState for |service_path|.
200   void ClearLastErrorForNetwork(const std::string& service_path);
201
202   // Set the list of devices on which portal check is enabled.
203   void SetCheckPortalList(const std::string& check_portal_list);
204
205   const std::string& GetCheckPortalListForTest() const {
206     return check_portal_list_;
207   }
208
209   // Returns the FavoriteState of the EthernetEAP service, which contains the
210   // EAP parameters used by the ethernet with |service_path|. If |service_path|
211   // doesn't refer to an ethernet service or if the ethernet service is not
212   // connected using EAP, returns NULL.
213   const FavoriteState* GetEAPForEthernet(const std::string& service_path) const;
214
215   // Construct and initialize an instance for testing.
216   static NetworkStateHandler* InitializeForTest();
217
218   // Default set of comma separated interfaces on which to enable
219   // portal checking.
220   static const char kDefaultCheckPortalList[];
221
222  protected:
223   friend class NetworkHandler;
224   NetworkStateHandler();
225
226   // ShillPropertyHandler::Listener overrides.
227
228   // This adds new entries to the managed list specified by |type| and deletes
229   // any entries that are no longer in the list.
230   virtual void UpdateManagedList(ManagedState::ManagedType type,
231                                  const base::ListValue& entries) OVERRIDE;
232
233   // The list of profiles changed (i.e. a user has logged in). Re-request
234   // properties for all services since they may have changed.
235   virtual void ProfileListChanged() OVERRIDE;
236
237   // Parses the properties for the network service or device. Mostly calls
238   // managed->PropertyChanged(key, value) for each dictionary entry.
239   virtual void UpdateManagedStateProperties(
240       ManagedState::ManagedType type,
241       const std::string& path,
242       const base::DictionaryValue& properties) OVERRIDE;
243
244   // Called by ShillPropertyHandler when a watched service property changes.
245   virtual void UpdateNetworkServiceProperty(
246       const std::string& service_path,
247       const std::string& key,
248       const base::Value& value) OVERRIDE;
249
250   // Called by ShillPropertyHandler when a watched device property changes.
251   virtual void UpdateDeviceProperty(
252       const std::string& device_path,
253       const std::string& key,
254       const base::Value& value) OVERRIDE;
255
256   // Called by ShillPropertyHandler when a watched network or device
257   // IPConfig property changes.
258   virtual void UpdateIPConfigProperties(
259       ManagedState::ManagedType type,
260       const std::string& path,
261       const std::string& ip_config_path,
262       const base::DictionaryValue& properties) OVERRIDE;
263
264   // Called by ShillPropertyHandler when the portal check list manager property
265   // changes.
266   virtual void CheckPortalListChanged(
267       const std::string& check_portal_list) OVERRIDE;
268
269   // Called by ShillPropertyHandler when a technology list changes.
270   virtual void TechnologyListChanged() OVERRIDE;
271
272   // Called by |shill_property_handler_| when the service or device list has
273   // changed and all entries have been updated. This updates the list and
274   // notifies observers. If |type| == TYPE_NETWORK this also calls
275   // CheckDefaultNetworkChanged().
276   virtual void ManagedStateListChanged(
277       ManagedState::ManagedType type) OVERRIDE;
278
279   // Called when the default network service changes. Sets default_network_path_
280   // and notifies listeners.
281   virtual void DefaultNetworkServiceChanged(
282       const std::string& service_path) 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   // Notifies observers when the default network or its properties change.
318   void NotifyDefaultNetworkChanged(const NetworkState* default_network);
319
320   // Notifies observers about changes to |network|.
321   void NotifyNetworkPropertiesUpdated(const NetworkState* network);
322
323   // Called whenever Device.Scanning state transitions to false.
324   void ScanCompleted(const std::string& type);
325
326   // Returns one technology type for |type|. This technology will be the
327   // highest priority technology in the type pattern.
328   std::string GetTechnologyForType(const NetworkTypePattern& type) const;
329
330   // Returns all the technology types for |type|.
331   ScopedVector<std::string> GetTechnologiesForType(
332       const NetworkTypePattern& type) const;
333
334   // Shill property handler instance, owned by this class.
335   scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
336
337   // Observer list
338   ObserverList<NetworkStateHandlerObserver> observers_;
339
340   // List of managed network states
341   ManagedStateList network_list_;
342
343   // List of managed favorite states; this list includes all entries in
344   // Manager.ServiceCompleteList, but only entries with a non-empty Profile
345   // property are returned in GetFavoriteList().
346   ManagedStateList favorite_list_;
347
348   // List of managed device states
349   ManagedStateList device_list_;
350
351   // Keeps track of the default network for notifying observers when it changes.
352   std::string default_network_path_;
353
354   // List of interfaces on which portal check is enabled.
355   std::string check_portal_list_;
356
357   // Callbacks to run when a scan for the technology type completes.
358   ScanCompleteCallbackMap scan_complete_callbacks_;
359
360   DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
361 };
362
363 }  // namespace chromeos
364
365 #endif  // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_