network_state_handler.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/gtest_prod_util.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/observer_list.h"
16#include "chromeos/chromeos_export.h"
17#include "chromeos/network/managed_state.h"
18#include "chromeos/network/network_handler_callbacks.h"
19#include "chromeos/network/shill_property_handler.h"
20
21namespace base {
22class DictionaryValue;
23class ListValue;
24class Value;
25}
26
27namespace chromeos {
28
29class DeviceState;
30class NetworkState;
31class NetworkStateHandlerObserver;
32class NetworkStateHandlerTest;
33
34// Class for tracking the list of visible networks and their properties.
35//
36// This class maps essential properties from the connection manager (Shill) for
37// each visible network. It is not used to change the properties of services or
38// devices, only global (manager) properties.
39//
40// All getters return the currently cached properties. This class is expected to
41// keep properties up to date by managing the appropriate Shill observers.
42// It will invoke its own more specific observer methods when the specified
43// changes occur.
44//
45// Most *ByType or *ForType methods will accept any of the following  for
46// |type|. See individual methods for specific notes.
47// * Any type defined in service_constants.h (e.g. flimflam::kTypeWifi)
48// * kMatchTypeDefault returns the default (active) network
49// * kMatchTypeNonVirtual returns the primary non virtual network
50// * kMatchTypeWireless returns the primary wireless network
51// * kMatchTypeMobile returns the primary cellular or wimax network
52
53class CHROMEOS_EXPORT NetworkStateHandler
54    : public internal::ShillPropertyHandler::Listener {
55 public:
56  typedef std::vector<ManagedState*> ManagedStateList;
57  typedef std::vector<const NetworkState*> NetworkStateList;
58
59  virtual ~NetworkStateHandler();
60
61  // Sets the global instance. Must be called before any calls to Get().
62  static void Initialize();
63
64  // Returns true if the global instance has been initialized.
65  static bool IsInitialized();
66
67  // Destroys the global instance.
68  static void Shutdown();
69
70  // Gets the global instance. Initialize() must be called first.
71  static NetworkStateHandler* Get();
72
73  // Add/remove observers.
74  void AddObserver(NetworkStateHandlerObserver* observer);
75  void RemoveObserver(NetworkStateHandlerObserver* observer);
76
77  // Returns true if technology for |type| is available/ enabled/uninitialized.
78  // kMatchTypeMobile (only) is also supported.
79  bool TechnologyAvailable(const std::string& type) const;
80  bool TechnologyEnabled(const std::string& type) const;
81  bool TechnologyUninitialized(const std::string& type) const;
82
83  // Asynchronously sets the technology enabled property for |type|.
84  // kMatchTypeMobile (only) is also supported.
85  // Note: Modifies Manager state. Calls |error_callback| on failure.
86  void SetTechnologyEnabled(
87      const std::string& type,
88      bool enabled,
89      const network_handler::ErrorCallback& error_callback);
90
91  // Finds and returns a device state by |device_path| or NULL if not found.
92  const DeviceState* GetDeviceState(const std::string& device_path) const;
93
94  // Finds and returns a device state by |type|. Returns NULL if not found.
95  // See note above for valid types.
96  const DeviceState* GetDeviceStateByType(const std::string& type) const;
97
98  // Returns true if any device of |type| is scanning.
99  // See note above for valid types.
100  bool GetScanningByType(const std::string& type) const;
101
102  // Finds and returns a network state by |service_path| or NULL if not found.
103  // Note: NetworkState is frequently updated asynchronously, i.e. properties
104  // are not always updated all at once. This will contain the most recent
105  // value for each property. To receive notifications when a property changes,
106  // observe this class and implement NetworkPropertyChanged().
107  const NetworkState* GetNetworkState(const std::string& service_path) const;
108
109  // Returns the default connected network (which includes VPNs) or NULL.
110  // This is equivalent to ConnectedNetworkByType(kMatchTypeDefault).
111  const NetworkState* DefaultNetwork() const;
112
113  // Returns the primary connected network of matching |type|, otherwise NULL.
114  // See note above for valid types.
115  const NetworkState* ConnectedNetworkByType(const std::string& type) const;
116
117  // Like ConnectedNetworkByType() but returns a connecting network or NULL.
118  const NetworkState* ConnectingNetworkByType(const std::string& type) const;
119
120  // Like ConnectedNetworkByType() but returns any matching network or NULL.
121  // Mostly useful for mobile networks where there is generally only one
122  // network. Note: O(N).
123  const NetworkState* FirstNetworkByType(const std::string& type) const;
124
125  // Returns the hardware (MAC) address for the first connected network
126  // matching |type|, or an empty string if none.
127  // See note above for valid types.
128  std::string HardwareAddressForType(const std::string& type) const;
129  // Same as above but in aa:bb format.
130  std::string FormattedHardwareAddressForType(const std::string& type) const;
131
132  // Sets |list| to contain the list of networks.  The returned list contains
133  // a copy of NetworkState pointers which should not be stored or used beyond
134  // the scope of the calling function (i.e. they may later become invalid, but
135  // only on the UI thread).
136  void GetNetworkList(NetworkStateList* list) const;
137
138  // Requests a network scan. This may trigger updates to the network
139  // list, which will trigger the appropriate observer calls.
140  void RequestScan() const;
141
142  static const char kMatchTypeDefault[];
143  static const char kMatchTypeWireless[];
144  static const char kMatchTypeMobile[];
145  static const char kMatchTypeNonVirtual[];
146
147  const std::string& connecting_network() const { return connecting_network_; }
148  void set_connecting_network(const std::string& service_path) {
149    connecting_network_ = service_path;
150  }
151
152 protected:
153  NetworkStateHandler();
154
155  // ShillPropertyHandler::Listener overrides.
156
157  // This adds new entries to the managed list specified by |type| and deletes
158  // any entries that are no longer in the list.
159  virtual void UpdateManagedList(ManagedState::ManagedType type,
160                                 const base::ListValue& entries) OVERRIDE;
161
162  // Parses the properties for the network service or device. Mostly calls
163  // managed->PropertyChanged(key, value) for each dictionary entry.
164  virtual void UpdateManagedStateProperties(
165      ManagedState::ManagedType type,
166      const std::string& path,
167      const base::DictionaryValue& properties) OVERRIDE;
168
169  // Called by ShillPropertyHandler when a watched service property changes.
170  virtual void UpdateNetworkServiceProperty(
171      const std::string& service_path,
172      const std::string& key,
173      const base::Value& value) OVERRIDE;
174
175  // Sets the IP Address for the network associated with |service_path|.
176  virtual void UpdateNetworkServiceIPAddress(
177      const std::string& service_path,
178      const std::string& ip_address) OVERRIDE;
179
180  // Called by ShillPropertyHandler when a watched device property changes.
181  virtual void UpdateDeviceProperty(
182      const std::string& device_path,
183      const std::string& key,
184      const base::Value& value) OVERRIDE;
185
186  // Sends NetworkManagerChanged() to observers.
187  virtual void ManagerPropertyChanged() OVERRIDE;
188
189  // Called by |shill_property_handler_| when the service or device list has
190  // changed and all entries have been updated. This updates the list and
191  // notifies observers. If |type| == TYPE_NETWORK this also calls
192  // CheckDefaultNetworkChanged().
193  virtual void ManagedStateListChanged(
194      ManagedState::ManagedType type) OVERRIDE;
195
196  // Called in Initialize(). Called explicitly by tests after adding
197  // test observers.
198  void InitShillPropertyHandler();
199
200 private:
201  friend class NetworkStateHandlerTest;
202  FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub);
203
204  // Non-const getters for managed entries. These are const so that they can
205  // be called by Get[Network|Device]State, even though they return non-const
206  // pointers.
207  DeviceState* GetModifiableDeviceState(const std::string& device_path) const;
208  NetworkState* GetModifiableNetworkState(
209      const std::string& service_path) const;
210  ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list,
211                                          const std::string& path) const;
212
213  // Gets the list specified by |type|.
214  ManagedStateList* GetManagedList(ManagedState::ManagedType type);
215
216  // Helper function to notify observers. Calls CheckDefaultNetworkChanged().
217  void OnNetworkConnectionStateChanged(NetworkState* network);
218
219  // If the default network changed returns true and sets
220  // |default_network_path_|.
221  bool CheckDefaultNetworkChanged();
222
223  // Logs an event and notifies observers.
224  void OnDefaultNetworkChanged();
225
226  // Notifies observers and updates connecting_network_.
227  void NetworkPropertiesUpdated(const NetworkState* network);
228
229  // Shill property handler instance, owned by this class.
230  scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
231
232  // Observer list
233  ObserverList<NetworkStateHandlerObserver> observers_;
234
235  // Lists of managed states
236  ManagedStateList network_list_;
237  ManagedStateList device_list_;
238
239  // Keeps track of the default network for notifying observers when it changes.
240  std::string default_network_path_;
241
242  // Convenience member to track the user initiated connecting network. Set
243  // externally when a connection is requested and cleared here when the state
244  // changes to something other than Connecting (after observers are notified).
245  // TODO(stevenjb): Move this to NetworkConfigurationHandler.
246  std::string connecting_network_;
247
248  DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
249};
250
251}  // namespace chromeos
252
253#endif  // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
254