Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: chromeos/network/network_state_manager.h

Issue 11192024: Add chromeos::NetworkStateManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup, comment, and add unit tests Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_MANAGER_H_
6 #define CHROMEOS_NETWORK_NETWORK_STATE_MANAGER_H_
7
8 #include <list>
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 // Class for tracking the list of visible networks and their state.
Greg Spencer (Chromium) 2012/10/23 23:19:10 Move down to class declaration.
stevenjb 2012/10/25 00:41:58 Done.
22 // This class maps essential state from the connection manager (Shill) for
23 // each visible network. It is not used to change state, and should not be
pneubeck (no reviews) 2012/10/24 14:41:42 Actually, SetTechnologyEnabled does change state.
stevenjb 2012/10/25 00:41:58 Done.
24 // used to track local state for networks.
25 // This class must not outlive the ShillManagerClient instance.
pneubeck (no reviews) 2012/10/24 14:41:42 Maybe give also a comment on what the class guaran
stevenjb 2012/10/25 00:41:58 We will ensure this is not true.
26
27 namespace base {
28 class DictionaryValue;
29 class ListValue;
30 class Value;
31 }
32
33 namespace chromeos {
34
35 class DeviceState;
36 class NetworkState;
37 class NetworkServiceObserver;
38 class ShillManagerClient;
39
40 class CHROMEOS_EXPORT NetworkStateManager
stevenjb 2012/10/23 22:59:38 Per discussion, will rename this NetworkStateHandl
stevenjb 2012/10/25 00:41:58 Done.
41 : public ShillPropertyChangedObserver {
42 public:
43 typedef std::list<ManagedState*> ManagedStateList;
44 typedef std::list<NetworkState*> NetworkStateList;
45 typedef std::list<DeviceState*> DeviceStateList;
46
47 // Observer class for all network state changes, including changes to
48 // active (connecting or connected) services.
49 class Observer {
Greg Spencer (Chromium) 2012/10/23 23:19:10 It may be better to move this outside of the class
stevenjb 2012/10/25 00:41:58 Done.
50 public:
pneubeck (no reviews) 2012/10/24 14:41:42 You could disallow copy/assign here, otherwise the
stevenjb 2012/10/25 00:41:58 Done
51 virtual ~Observer() {}
52
53 // If |property| is non empty than that property has changed. Otherwise
54 // multiple properties may have changed.
55 virtual void NetworkManagerChanged(const std::string& property) {}
56 // The list of networks changed.
57 virtual void NetworkListChanged(const NetworkStateList& networks) {}
58 // The active network changed. |network| will be NULL if there is no longer
59 // an active network.
60 virtual void ActiveNetworkChanged(const NetworkState* network) {}
61 // The state of the active network changed.
62 virtual void ActiveNetworkStateChanged(const NetworkState* network) {}
63 // A network service property changed. |property| is the name of the changed
64 // property (from service_constants.h). Note: for the active network,
65 // this will be called in *addition* to ActiveNetworkStateChanged() if
66 // property == flimflam::kStateProperty.
67 virtual void NetworkServicePropertyChanged(const NetworkState* network,
68 const std::string& property) {}
69 };
70
71 NetworkStateManager();
72 ~NetworkStateManager();
73
74 // Initialize NetworkStateManager. Sends an initial property request.
75 void Init();
76
77 // Add remove observers.
78 void AddObserver(Observer* observer);
79 void RemoveObserver(Observer* observer);
80
81 // Request an immediate network scan.
82 void RequestScan() const;
stevenjb 2012/10/23 23:12:09 Will remove and make implicit in GetNetworkList().
stevenjb 2012/10/25 00:41:58 Done.
83
84 // Manage the state of enabled / available technologies.
pneubeck (no reviews) 2012/10/24 14:41:42 A bit misleading. Better split into separate comme
85 bool TechnologyAvailable(const std::string& technology) const;
86 bool TechnologyEnabled(const std::string& technology) const;
87 void SetTechnologyEnabled(const std::string& technology, bool enabled);
stevenjb 2012/10/23 23:12:09 Add TODO: Add an error handler callback for SetTec
pneubeck (no reviews) 2012/10/24 14:41:42 Emphasize, that this does not change which technol
Greg Spencer (Chromium) 2012/10/24 22:27:42 As a general rule, how will we know about DBus cal
stevenjb 2012/10/25 00:41:58 It is the responsibility of this class to maintain
Greg Spencer (Chromium) 2012/10/25 21:51:42 On 2012/10/25 00:41:58, stevenjb (chromium) wrote:
88
89 // Finds and returns a device by |path|. Returns NULL if not found.
90 const DeviceState* GetDeviceState(const std::string& path) const;
91
92 // Finds and returns a device by |type|. Returns NULL if not found.
93 const DeviceState* GetDeviceStateByType(const std::string& type) const;
94
95 // Finds and returns a network by |path|. Returns NULL if not found.
96 const NetworkState* GetNetworkState(const std::string& path) const;
97
98 // Returns the "active" network (first network in the list if connected).
Greg Spencer (Chromium) 2012/10/25 21:51:42 What does this return if not connected? (I'm assum
stevenjb 2012/10/25 22:05:22 Done.
99 const NetworkState* ActiveNetwork() const;
100
101 // Returns the first connected network of type |type|, otherwise NULL.
102 // An empty type will return any connected non-ethernet network.
103 const NetworkState* ConnectedNetworkByType(const std::string& type) const;
104
105 // Returns the first connecting network of type |type|, otherwise NULL.
106 // An empty type will return any connecting non-ethernet network.
107 const NetworkState* ConnectingNetworkByType(const std::string& type) const;
108
109 // Returns the hardware (MAC) address for the first connected network
110 // matching |type|, or an empty string if none.
111 std::string HardwareAddress(const std::string& type) const;
112 // Same as above but in aa:bb format.
113 std::string HardwareAddressFormatted(const std::string& type) const;
114
115 const NetworkStateList& network_list() const { return network_list_; }
stevenjb 2012/10/23 23:12:09 Will change to GetNetworkList() and have this requ
stevenjb 2012/10/25 00:41:58 Done.
116
117 // ShillPropertyChangedObserver overrides.
118 virtual void OnPropertyChanged(const std::string& key,
119 const base::Value& value);
Greg Spencer (Chromium) 2012/10/23 23:19:10 Add OVERRIDE
stevenjb 2012/10/25 00:41:58 Done.
120
121 private:
pneubeck (no reviews) 2012/10/24 14:41:42 Since this is a rather central manager. Should the
stevenjb 2012/10/25 00:41:58 In general in Chrome we rely on fast compilation t
122 FRIEND_TEST_ALL_PREFIXES(NetworkStateManagerTest, NetworkStateManagerStub);
123
124 typedef std::map<std::string, NetworkServiceObserver*>
125 NetworkServiceObservers;
Greg Spencer (Chromium) 2012/10/23 23:19:10 nit: NetworkServiceObserverMap instead?
stevenjb 2012/10/25 00:41:58 Done.
126
127 // Non-const getters for managed entries.
128 DeviceState* GetModifiableDeviceState(const std::string& path);
pneubeck (no reviews) 2012/10/24 14:41:42 Make these two methods const (using const_iterator
stevenjb 2012/10/25 00:41:58 The const cast is for the return values, to avoid
129 NetworkState* GetModifiableNetworkState(const std::string& path);
130
131 // Callback for dbus method fetching properties.
132 void ManagerPropertiesCallback(DBusMethodCallStatus call_status,
133 const base::DictionaryValue& properties);
134 // Called form OnPropertyChanged() and ManagerPropertiesCallback().
135 // Returns true if observers should be notified.
136 bool ManagerPropertyChanged(const std::string& key, const base::Value& value);
137
138 // Casts the list specified by |type| to a list of base ManagedState pointers.
139 ManagedStateList* GetManagedList(ManagedState::ManagedType type);
140
141 // Adds new entries to the managed list specified by |type| and deletes any
142 // entries that are no longer in the list.
143 void UpdateManagedList(ManagedState::ManagedType type,
144 const base::ListValue& entries);
145
146 // Updates the Shill service property observers to observe any connected
147 // or connecting networks.
148 void UpdateObservedNetworkServices();
149
150 // Sets |available_technologies_| to contain only entries in |technologies|.
151 void UpdateAvailableTechnologies(const base::ListValue& technologies);
152
153 // Sets |enabled_technologies_| to contain only entries in |technologies|.
154 void UpdateEnabledTechnologies(const base::ListValue& technologies);
155
156 // Requests all properties for the service or device (called for new items).
157 void RequestProperties(ManagedState::ManagedType type,
158 const std::string& path);
159
160 // Called when Shill returns the properties for a service or device.
161 void GetPropertiesCallback(ManagedState::ManagedType type,
162 const std::string& path,
163 DBusMethodCallStatus call_status,
164 const base::DictionaryValue& properties);
165
166 // Parses the properties for the service or device. Mostly calls
167 // managed->PropertyChanged(key, value) for each dictionary entry.
168 void ParseProperties(ManagedState* managed,
169 const base::DictionaryValue& properties);
170
171 // Callback invoked when a watched service property changes. Calls
172 // network->PropertyChanged(key, value) and signals observers.
173 void NetworkServicePropertyChanged(const std::string& path,
174 const std::string& key,
175 const base::Value& value);
176
177 // Callback for getting the IPConfig property of a Network. Handled here
178 // instead of in NetworkState so that all asynchronous requests are done
179 // in a single place (also simplifies NetworkState considerably).
180 void GetIPConfigCallback(const std::string& path,
181 DBusMethodCallStatus call_status,
182 const base::DictionaryValue& properties);
183
184 // Test accessors
185 int NumObservedNetworksForTest() const { return observed_networks_.size(); }
186
187 // Convenince pointer for ShillManagerClient
pneubeck (no reviews) 2012/10/24 14:41:42 nit: Convenience
stevenjb 2012/10/25 00:41:58 Done.
188 ShillManagerClient* shill_manager_;
189
190 // Observer list
191 ObserverList<Observer> observers_;
192
193 // Lists of managed states
194 NetworkStateList network_list_;
pneubeck (no reviews) 2012/10/24 14:41:42 These lists are never modified, but only new ones
stevenjb 2012/10/25 00:41:58 Evolutionary artifact. scoped_vector<> is high ove
195 DeviceStateList device_list_;
196
197 // Pending update count for each managed state type
198 std::map<ManagedState::ManagedType, int> pending_updates_;
199
200 // Lists of available / enabled technologies
201 std::set<std::string> available_technologies_;
202 std::set<std::string> enabled_technologies_;
203
204 // List of network services with Shill property changed observers
205 NetworkServiceObservers observed_networks_;
206
207 // Keep track of the active network for notifying observers when it changes.
208 std::string active_network_path_;
209
210 // For Shill client callbacks
211 base::WeakPtrFactory<NetworkStateManager> weak_ptr_factory_;
212
213 DISALLOW_COPY_AND_ASSIGN(NetworkStateManager);
214 };
215
216 } // namespace chromeos
217
218 #endif // CHROMEOS_NETWORK_NETWORK_STATE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698