OLD | NEW |
---|---|
(Empty) | |
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 <list> | |
gauravsh
2012/10/25 23:41:05
Is this used anywhere?
stevenjb
2012/10/26 21:36:39
Fixed
| |
9 #include <map> | |
10 #include <set> | |
11 #include <string> | |
12 | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/observer_list.h" | |
16 #include "chromeos/chromeos_export.h" | |
17 #include "chromeos/dbus/dbus_method_call_status.h" | |
18 #include "chromeos/dbus/shill_property_changed_observer.h" | |
19 #include "chromeos/network/managed_state.h" | |
20 | |
21 namespace base { | |
22 class DictionaryValue; | |
23 class ListValue; | |
24 class Value; | |
25 } | |
26 | |
27 namespace chromeos { | |
28 | |
29 class DeviceState; | |
30 class NetworkState; | |
31 class NetworkStateHandlerObserver; | |
32 class ShillServiceObserver; | |
33 class ShillManagerClient; | |
34 | |
35 // Class for tracking the list of visible networks and their state. | |
36 // | |
37 // This class maps essential state from the connection manager (Shill) for | |
38 // each visible network. It is not used to change the state of services or | |
39 // devices, only global (manager) state. | |
40 // | |
41 // All getters return the currently cached state. This class is expected to | |
42 // keep states up to date by managing the appropriate Shill observers. | |
43 // It will send invoke its own more specific observer methods when the | |
gauravsh
2012/10/25 23:41:05
send invoke -> invoke
stevenjb
2012/10/26 21:36:39
Done.
| |
44 // specified changes occur. | |
45 // | |
46 // This class must not outlive the ShillManagerClient instance. | |
47 class CHROMEOS_EXPORT NetworkStateHandler | |
48 : public ShillPropertyChangedObserver { | |
49 public: | |
50 typedef std::vector<ManagedState*> ManagedStateList; | |
51 typedef std::vector<NetworkState*> NetworkStateList; | |
52 typedef std::vector<DeviceState*> DeviceStateList; | |
53 | |
54 NetworkStateHandler(); | |
55 ~NetworkStateHandler(); | |
56 | |
57 // Initialize NetworkStateHandler. Sends an initial property request. | |
58 void Init(); | |
59 | |
60 // Add remove observers. | |
gauravsh
2012/10/25 23:41:05
NIT: Add/remove
stevenjb
2012/10/26 21:36:39
Done.
| |
61 void AddObserver(NetworkStateHandlerObserver* observer); | |
62 void RemoveObserver(NetworkStateHandlerObserver* observer); | |
63 | |
64 // Returns true if |technology| is enabled / available. | |
65 bool TechnologyAvailable(const std::string& technology) const; | |
66 bool TechnologyEnabled(const std::string& technology) const; | |
67 | |
68 // Asynchronously sets the enabled state for |technology|. | |
69 // Note: Modifes Manager state. TODO(stevenjb): Add a completion callback. | |
70 void SetTechnologyEnabled(const std::string& technology, bool enabled); | |
71 | |
72 // Finds and returns a device by |path|. Returns NULL if not found. | |
73 const DeviceState* GetDeviceState(const std::string& path) const; | |
gauravsh
2012/10/25 23:41:05
Suggest calling this device_path to distinguish be
stevenjb
2012/10/26 21:36:39
Done.
| |
74 | |
75 // Finds and returns a device by |type|. Returns NULL if not found. | |
gauravsh
2012/10/25 23:41:05
device -> device state
stevenjb
2012/10/26 21:36:39
Done.
| |
76 const DeviceState* GetDeviceStateByType(const std::string& type) const; | |
77 | |
78 // Finds and returns a network by |path|. Returns NULL if not found. | |
gauravsh
2012/10/25 23:41:05
network state
stevenjb
2012/10/26 21:36:39
Done.
| |
79 // Note: NetworkState is frequently updated asynchronously, i.e. properties | |
80 // are not always updated all at once. This will contain the most recent | |
81 // value for each state. To receive notifications when the state changes, | |
82 // observer this class and implement NetworkServicePropertyChanged(). | |
83 const NetworkState* GetNetworkState(const std::string& path) const; | |
84 | |
85 // Returns the "active" network (first network in the list if connected), | |
86 // NULL if none. | |
87 const NetworkState* ActiveNetwork() const; | |
88 | |
89 // Returns the first connected network of type |type|, otherwise NULL. | |
90 // An empty type will return any connected non-ethernet network. | |
gauravsh
2012/10/25 23:41:05
Why do we need to special-case this to only return
stevenjb
2012/10/26 21:36:39
Oops, not true, comment copied from connecting.
| |
91 const NetworkState* ConnectedNetworkByType(const std::string& type) const; | |
92 | |
93 // Returns the first connecting network of type |type|, otherwise NULL. | |
94 // An empty type will return any connecting non-ethernet network. | |
gauravsh
2012/10/25 23:41:05
See question about special-casing ethernet.
stevenjb
2012/10/26 21:36:39
Ethernet is rarely if ever in a "connecting" state
gauravsh
2012/10/27 00:26:33
But why not handle this damping at the UI layer in
| |
95 const NetworkState* ConnectingNetworkByType(const std::string& type) const; | |
96 | |
97 // Returns the hardware (MAC) address for the first connected network | |
98 // matching |type|, or an empty string if none. | |
99 std::string HardwareAddress(const std::string& type) const; | |
gauravsh
2012/10/26 01:15:01
I don't quite understand why we need this. Isn't t
stevenjb
2012/10/26 21:36:39
I improved the names, but feel that:
a) We will be
| |
100 // Same as above but in aa:bb format. | |
101 std::string HardwareAddressFormatted(const std::string& type) const; | |
gauravsh
2012/10/26 01:15:01
Since everything is a string - please change type-
stevenjb
2012/10/26 21:36:39
I think that would be more confusing. We use 'type
| |
102 | |
103 // Returns the list of networks. This also sends a scan request to Shill. | |
104 const NetworkStateList& GetNetworkList() const; | |
gauravsh
2012/10/25 23:41:05
Is this synchronous? Are the list of networks retu
stevenjb
2012/10/26 21:36:39
Comments here have been expanded.
| |
105 | |
106 // ShillPropertyChangedObserver overrides. | |
107 virtual void OnPropertyChanged(const std::string& key, | |
108 const base::Value& value) OVERRIDE; | |
109 | |
110 private: | |
111 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); | |
112 | |
113 typedef std::map<std::string, ShillServiceObserver*> ShillServiceObserverMap; | |
114 | |
115 // Non-const getters for managed entries. These are const so that they can | |
116 // be called by Get[Network|Device]State, even though they return non-const | |
117 // pointers. | |
118 DeviceState* GetModifiableDeviceState(const std::string& path) const; | |
119 NetworkState* GetModifiableNetworkState(const std::string& path) const; | |
120 | |
121 // Callback for dbus method fetching properties. | |
122 void ManagerPropertiesCallback(DBusMethodCallStatus call_status, | |
123 const base::DictionaryValue& properties); | |
124 // Called form OnPropertyChanged() and ManagerPropertiesCallback(). | |
125 // Returns true if observers should be notified. | |
126 bool ManagerPropertyChanged(const std::string& key, const base::Value& value); | |
127 | |
128 // Casts the list specified by |type| to a list of base ManagedState pointers. | |
129 ManagedStateList* GetManagedList(ManagedState::ManagedType type); | |
130 | |
131 // Adds new entries to the managed list specified by |type| and deletes any | |
132 // entries that are no longer in the list. | |
133 void UpdateManagedList(ManagedState::ManagedType type, | |
134 const base::ListValue& entries); | |
135 | |
136 // Updates the Shill service property observers to observe any connected | |
137 // or connecting networks. | |
138 void UpdateObservedNetworkServices(); | |
139 | |
140 // Sets |available_technologies_| to contain only entries in |technologies|. | |
141 void UpdateAvailableTechnologies(const base::ListValue& technologies); | |
gauravsh
2012/10/25 23:41:05
Should this just be called SetAvailableTechnologie
stevenjb
2012/10/26 21:36:39
It's named update because it's more involved than
| |
142 | |
143 // Sets |enabled_technologies_| to contain only entries in |technologies|. | |
144 void UpdateEnabledTechnologies(const base::ListValue& technologies); | |
145 | |
146 // Requests all properties for the service or device (called for new items). | |
147 void RequestProperties(ManagedState::ManagedType type, | |
148 const std::string& path); | |
149 | |
150 // Called when Shill returns the properties for a service or device. | |
151 void GetPropertiesCallback(ManagedState::ManagedType type, | |
152 const std::string& path, | |
153 DBusMethodCallStatus call_status, | |
154 const base::DictionaryValue& properties); | |
155 | |
156 // Parses the properties for the service or device. Mostly calls | |
157 // managed->PropertyChanged(key, value) for each dictionary entry. | |
158 void ParseProperties(ManagedState* managed, | |
159 const base::DictionaryValue& properties); | |
160 | |
161 // Callback invoked when a watched service property changes. Calls | |
162 // network->PropertyChanged(key, value) and signals observers. | |
163 void NetworkServicePropertyChanged(const std::string& path, | |
gauravsh
2012/10/25 23:41:05
NIT: service_path
stevenjb
2012/10/26 21:36:39
Done.
| |
164 const std::string& key, | |
165 const base::Value& value); | |
166 | |
167 // Callback for getting the IPConfig property of a Network. Handled here | |
168 // instead of in NetworkState so that all asynchronous requests are done | |
169 // in a single place (also simplifies NetworkState considerably). | |
170 void GetIPConfigCallback(const std::string& service_path, | |
gauravsh
2012/10/25 23:41:05
I think it would be useful to group the ordering o
stevenjb
2012/10/26 21:36:39
This has been refactored
| |
171 DBusMethodCallStatus call_status, | |
172 const base::DictionaryValue& properties); | |
173 | |
174 // Request an immediate network scan. | |
175 void RequestScan() const; | |
176 | |
177 // Test accessors | |
178 int NumObservedNetworksForTest() const { return observed_networks_.size(); } | |
179 | |
180 // Convenience pointer for ShillManagerClient | |
181 ShillManagerClient* shill_manager_; | |
182 | |
183 // Observer list | |
184 ObserverList<NetworkStateHandlerObserver> observers_; | |
185 | |
186 // Lists of managed states | |
187 NetworkStateList network_list_; | |
188 DeviceStateList device_list_; | |
189 | |
190 // Pending update count for each managed state type | |
191 std::map<ManagedState::ManagedType, int> pending_updates_; | |
192 | |
193 // Lists of available / enabled technologies | |
194 std::set<std::string> available_technologies_; | |
195 std::set<std::string> enabled_technologies_; | |
196 | |
197 // List of network services with Shill property changed observers | |
198 ShillServiceObserverMap observed_networks_; | |
199 | |
200 // Keep track of the active network for notifying observers when it changes. | |
201 std::string active_network_path_; | |
202 | |
203 // For Shill client callbacks | |
204 base::WeakPtrFactory<NetworkStateHandler> weak_ptr_factory_; | |
205 | |
206 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); | |
207 }; | |
208 | |
209 } // namespace chromeos | |
210 | |
211 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ | |
OLD | NEW |