network_state_handler.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/shill_property_handler.h"
19
20namespace base {
21class DictionaryValue;
22class ListValue;
23class Value;
24}
25
26namespace chromeos {
27
28class DeviceState;
29class NetworkState;
30class NetworkStateHandlerObserver;
31
32// Class for tracking the list of visible networks and their state.
33//
34// This class maps essential state from the connection manager (Shill) for
35// each visible network. It is not used to change the state of services or
36// devices, only global (manager) state.
37//
38// All getters return the currently cached state. This class is expected to
39// keep states up to date by managing the appropriate Shill observers.
40// It will invoke its own more specific observer methods when the specified
41// changes occur.
42class CHROMEOS_EXPORT NetworkStateHandler
43    : public internal::ShillPropertyHandler::Listener {
44 public:
45  typedef std::vector<ManagedState*> ManagedStateList;
46  typedef std::vector<const NetworkState*> NetworkStateList;
47
48  NetworkStateHandler();
49  virtual ~NetworkStateHandler();
50
51  // Initialize ShillPropertyHandler.
52  void Init();
53
54  // Add/remove observers.
55  void AddObserver(NetworkStateHandlerObserver* observer);
56  void RemoveObserver(NetworkStateHandlerObserver* observer);
57
58  // Returns true if |technology| is enabled / available.
59  bool TechnologyAvailable(const std::string& technology) const;
60  bool TechnologyEnabled(const std::string& technology) const;
61
62  // Asynchronously sets the enabled state for |technology|.
63  // Note: Modifes Manager state. TODO(stevenjb): Add a completion callback.
64  void SetTechnologyEnabled(const std::string& technology, bool enabled);
65
66  // Finds and returns a device state by |device_path| or NULL if not found.
67  const DeviceState* GetDeviceState(const std::string& device_path) const;
68
69  // Finds and returns a device state by |type|. Returns NULL if not found.
70  const DeviceState* GetDeviceStateByType(const std::string& type) const;
71
72  // Finds and returns a network state by |service_path| or NULL if not found.
73  // Note: NetworkState is frequently updated asynchronously, i.e. properties
74  // are not always updated all at once. This will contain the most recent
75  // value for each state. To receive notifications when the state changes,
76  // observer this class and implement NetworkServiceChanged().
77  const NetworkState* GetNetworkState(const std::string& service_path) const;
78
79  // Returns the "active" network (first network in the list if connected),
80  // NULL if none.
81  const NetworkState* ActiveNetwork() const;
82
83  // Returns the first connected network of type |type|, otherwise NULL.
84  const NetworkState* ConnectedNetworkByType(const std::string& type) const;
85
86  // Returns the first connecting network of type |type|, otherwise NULL.
87  // An empty type will return any connecting non-ethernet network.
88  const NetworkState* ConnectingNetworkByType(const std::string& type) const;
89
90  // Returns the hardware (MAC) address for the first connected network
91  // matching |type|, or an empty string if none.
92  std::string HardwareAddressForType(const std::string& type) const;
93  // Same as above but in aa:bb format.
94  std::string FormattedHardwareAddressForType(const std::string& type) const;
95
96  // Sets |list| to contain the list of networks.  The returned list contains
97  // a copy of NetworkState pointers which should not be stored or used beyond
98  // the scope of the calling function (i.e. they may later become invalid, but
99  // only on the UI thread). This also sends a scan request to Shill which may
100  // trigger updates to the networks (which will trigger the appropriate
101  // observer calls).
102  void GetNetworkList(NetworkStateList* list) const;
103
104  // ShillPropertyHandler::Listener overrides.
105
106  // This adds new entries to the managed list specified by |type| and deletes
107  // any entries that are no longer in the list.
108  virtual void UpdateManagedList(ManagedState::ManagedType type,
109                                 const base::ListValue& entries) OVERRIDE;
110
111  // Sets |available_technologies_| to contain only entries in |technologies|.
112  virtual void UpdateAvailableTechnologies(
113      const base::ListValue& technologies) OVERRIDE;
114
115  // Sets |enabled_technologies_| to contain only entries in |technologies|.
116  virtual void UpdateEnabledTechnologies(
117      const base::ListValue& technologies) OVERRIDE;
118
119  // Parses the properties for the network service or device. Mostly calls
120  // managed->PropertyChanged(key, value) for each dictionary entry.
121  virtual void UpdateManagedStateProperties(
122      ManagedState::ManagedType type,
123      const std::string& path,
124      const base::DictionaryValue& properties) OVERRIDE;
125
126  // Called by ShillPropertyHandler when a watched service property changes.
127  // Calls ParseNetworkServiceProperty() and signals observers.
128  virtual void UpdateNetworkServiceProperty(
129      const std::string& service_path,
130      const std::string& key,
131      const base::Value& value) OVERRIDE;
132
133  // Sets the IP Address for the network associated with |service_path|.
134  virtual void UpdateNetworkServiceIPAddress(
135      const std::string& service_path,
136      const std::string& ip_address) OVERRIDE;
137
138  // Sends NetworkManagerChanged() to observers.
139  virtual void ManagerPropertyChanged() OVERRIDE;
140
141  // Called by |shill_property_handler_| when the service or device list has
142  // changed and all entries have been updated. If |type| == TYPE_NETWORK,
143  // this notifies observers that the network list has changed, and if the
144  // active network has changed sends that notification also.
145  virtual void ManagedStateListChanged(
146      ManagedState::ManagedType type) OVERRIDE;
147
148private:
149  FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub);
150
151  // Non-const getters for managed entries. These are const so that they can
152  // be called by Get[Network|Device]State, even though they return non-const
153  // pointers.
154  DeviceState* GetModifiableDeviceState(const std::string& device_path) const;
155  NetworkState* GetModifiableNetworkState(
156      const std::string& service_path) const;
157  ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list,
158                                          const std::string& path) const;
159
160  // Gets the list specified by |type|.
161  ManagedStateList* GetManagedList(ManagedState::ManagedType type);
162
163  // Helper function called to parse |network| properties.
164  bool ParseNetworkServiceProperty(NetworkState* network,
165                                   const std::string& key,
166                                   const base::Value& value);
167
168  // Shill property handler instance, owned by this class.
169  scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
170
171  // Observer list
172  ObserverList<NetworkStateHandlerObserver> observers_;
173
174  // Lists of managed states
175  ManagedStateList network_list_;
176  ManagedStateList device_list_;
177
178  // Lists of available / enabled technologies
179  std::set<std::string> available_technologies_;
180  std::set<std::string> enabled_technologies_;
181
182  // Keeps track of the active network for notifying observers when it changes.
183  std::string active_network_path_;
184
185  DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
186};
187
188}  // namespace chromeos
189
190#endif  // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
191