[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / chromeos / network / network_state_handler.h
blob8c2d12573c62d13aa87ee8a15bcb0b3fbaddd134
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/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"
21 namespace base {
22 class DictionaryValue;
23 class ListValue;
24 class Value;
27 namespace chromeos {
29 class DeviceState;
30 class NetworkState;
31 class NetworkStateHandlerObserver;
32 class NetworkStateHandlerTest;
34 // Class for tracking the list of visible networks and their state.
36 // This class maps essential state from the connection manager (Shill) for
37 // each visible network. It is not used to change the state of services or
38 // devices, only global (manager) state.
40 // All getters return the currently cached state. This class is expected to
41 // keep states 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 class CHROMEOS_EXPORT NetworkStateHandler
45 : public internal::ShillPropertyHandler::Listener {
46 public:
47 typedef std::vector<ManagedState*> ManagedStateList;
48 typedef std::vector<const NetworkState*> NetworkStateList;
50 virtual ~NetworkStateHandler();
52 // Sets the global instance. Must be called before any calls to Get().
53 static void Initialize();
55 // Destroys the global instance.
56 static void Shutdown();
58 // Gets the global instance. Initialize() must be called first.
59 static NetworkStateHandler* Get();
61 // Add/remove observers.
62 void AddObserver(NetworkStateHandlerObserver* observer);
63 void RemoveObserver(NetworkStateHandlerObserver* observer);
65 // Returns true if |technology| is enabled / available.
66 bool TechnologyAvailable(const std::string& technology) const;
67 bool TechnologyEnabled(const std::string& technology) const;
69 // Asynchronously sets the enabled state for |technology|.
70 // Note: Modifes Manager state. Calls |error_callback| on failure.
71 void SetTechnologyEnabled(
72 const std::string& technology,
73 bool enabled,
74 const network_handler::ErrorCallback& error_callback);
76 // Finds and returns a device state by |device_path| or NULL if not found.
77 const DeviceState* GetDeviceState(const std::string& device_path) const;
79 // Finds and returns a device state by |type|. Returns NULL if not found.
80 const DeviceState* GetDeviceStateByType(const std::string& type) const;
82 // Finds and returns a network state by |service_path| or NULL if not found.
83 // Note: NetworkState is frequently updated asynchronously, i.e. properties
84 // are not always updated all at once. This will contain the most recent
85 // value for each state. To receive notifications when the state changes,
86 // observer this class and implement NetworkServiceChanged().
87 const NetworkState* GetNetworkState(const std::string& service_path) const;
89 // Returns the "active" network (first network in the list if connected),
90 // NULL if none.
91 const NetworkState* ActiveNetwork() const;
93 // Returns the first connected network of type |type|, otherwise NULL.
94 const NetworkState* ConnectedNetworkByType(const std::string& type) const;
96 // Returns the first connecting network of type |type|, otherwise NULL.
97 // An empty type will return any connecting non-ethernet network.
98 const NetworkState* ConnectingNetworkByType(const std::string& type) const;
100 // Returns the hardware (MAC) address for the first connected network
101 // matching |type|, or an empty string if none.
102 std::string HardwareAddressForType(const std::string& type) const;
103 // Same as above but in aa:bb format.
104 std::string FormattedHardwareAddressForType(const std::string& type) const;
106 // Sets |list| to contain the list of networks. The returned list contains
107 // a copy of NetworkState pointers which should not be stored or used beyond
108 // the scope of the calling function (i.e. they may later become invalid, but
109 // only on the UI thread).
110 void GetNetworkList(NetworkStateList* list) const;
112 // Requests a scan of wifi networks. This may trigger updates to the network
113 // list, which will trigger the appropriate observer calls.
114 // Returns true if a scan was requested.
115 bool RequestWifiScan() const;
117 protected:
118 NetworkStateHandler();
120 // ShillPropertyHandler::Listener overrides.
122 // This adds new entries to the managed list specified by |type| and deletes
123 // any entries that are no longer in the list.
124 virtual void UpdateManagedList(ManagedState::ManagedType type,
125 const base::ListValue& entries) OVERRIDE;
127 // Sets |available_technologies_| to contain only entries in |technologies|.
128 virtual void UpdateAvailableTechnologies(
129 const base::ListValue& technologies) OVERRIDE;
131 // Sets |enabled_technologies_| to contain only entries in |technologies|.
132 virtual void UpdateEnabledTechnologies(
133 const base::ListValue& technologies) OVERRIDE;
135 // Parses the properties for the network service or device. Mostly calls
136 // managed->PropertyChanged(key, value) for each dictionary entry.
137 virtual void UpdateManagedStateProperties(
138 ManagedState::ManagedType type,
139 const std::string& path,
140 const base::DictionaryValue& properties) OVERRIDE;
142 // Called by ShillPropertyHandler when a watched service property changes.
143 // Calls ParseNetworkServiceProperty() and signals observers.
144 virtual void UpdateNetworkServiceProperty(
145 const std::string& service_path,
146 const std::string& key,
147 const base::Value& value) OVERRIDE;
149 // Sets the IP Address for the network associated with |service_path|.
150 virtual void UpdateNetworkServiceIPAddress(
151 const std::string& service_path,
152 const std::string& ip_address) OVERRIDE;
154 // Sends NetworkManagerChanged() to observers.
155 virtual void ManagerPropertyChanged() OVERRIDE;
157 // Called by |shill_property_handler_| when the service or device list has
158 // changed and all entries have been updated. If |type| == TYPE_NETWORK,
159 // this notifies observers that the network list has changed, and if the
160 // active network has changed sends that notification also.
161 virtual void ManagedStateListChanged(
162 ManagedState::ManagedType type) OVERRIDE;
164 // Called in Initialize(). Called explicitly by tests after adding
165 // test observers.
166 void InitShillPropertyHandler();
168 private:
169 friend class NetworkStateHandlerTest;
170 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub);
172 // Non-const getters for managed entries. These are const so that they can
173 // be called by Get[Network|Device]State, even though they return non-const
174 // pointers.
175 DeviceState* GetModifiableDeviceState(const std::string& device_path) const;
176 NetworkState* GetModifiableNetworkState(
177 const std::string& service_path) const;
178 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list,
179 const std::string& path) const;
181 // Gets the list specified by |type|.
182 ManagedStateList* GetManagedList(ManagedState::ManagedType type);
184 // Helper function called to parse |network| properties.
185 bool ParseNetworkServiceProperty(NetworkState* network,
186 const std::string& key,
187 const base::Value& value);
189 // Shill property handler instance, owned by this class.
190 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
192 // Observer list
193 ObserverList<NetworkStateHandlerObserver> observers_;
195 // Lists of managed states
196 ManagedStateList network_list_;
197 ManagedStateList device_list_;
199 // Lists of available / enabled technologies
200 std::set<std::string> available_technologies_;
201 std::set<std::string> enabled_technologies_;
203 // Keeps track of the active network for notifying observers when it changes.
204 std::string active_network_path_;
206 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
209 } // namespace chromeos
211 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_