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

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

Issue 11614035: Improve NetworkStateHandler API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address feedback, state -> connection state Created 8 years 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ 5 #ifndef CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
6 #define CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ 6 #define CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
(...skipping 13 matching lines...) Expand all
24 class Value; 24 class Value;
25 } 25 }
26 26
27 namespace chromeos { 27 namespace chromeos {
28 28
29 class DeviceState; 29 class DeviceState;
30 class NetworkState; 30 class NetworkState;
31 class NetworkStateHandlerObserver; 31 class NetworkStateHandlerObserver;
32 class NetworkStateHandlerTest; 32 class NetworkStateHandlerTest;
33 33
34 // Class for tracking the list of visible networks and their state. 34 // Class for tracking the list of visible networks and their properties.
35 // 35 //
36 // This class maps essential state from the connection manager (Shill) for 36 // This class maps essential properties from the connection manager (Shill) for
37 // each visible network. It is not used to change the state of services or 37 // each visible network. It is not used to change the properties of services or
38 // devices, only global (manager) state. 38 // devices, only global (manager) properties.
39 // 39 //
40 // All getters return the currently cached state. This class is expected to 40 // All getters return the currently cached properties. This class is expected to
41 // keep states up to date by managing the appropriate Shill observers. 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 42 // It will invoke its own more specific observer methods when the specified
43 // changes occur. 43 // changes occur.
44 class CHROMEOS_EXPORT NetworkStateHandler 44 class CHROMEOS_EXPORT NetworkStateHandler
45 : public internal::ShillPropertyHandler::Listener { 45 : public internal::ShillPropertyHandler::Listener {
46 public: 46 public:
47 typedef std::vector<ManagedState*> ManagedStateList; 47 typedef std::vector<ManagedState*> ManagedStateList;
48 typedef std::vector<const NetworkState*> NetworkStateList; 48 typedef std::vector<const NetworkState*> NetworkStateList;
49 49
50 virtual ~NetworkStateHandler(); 50 virtual ~NetworkStateHandler();
51 51
52 // Sets the global instance. Must be called before any calls to Get(). 52 // Sets the global instance. Must be called before any calls to Get().
53 static void Initialize(); 53 static void Initialize();
54 54
55 // Destroys the global instance. 55 // Destroys the global instance.
56 static void Shutdown(); 56 static void Shutdown();
57 57
58 // Gets the global instance. Initialize() must be called first. 58 // Gets the global instance. Initialize() must be called first.
59 static NetworkStateHandler* Get(); 59 static NetworkStateHandler* Get();
60 60
61 // Add/remove observers. 61 // Add/remove observers.
62 void AddObserver(NetworkStateHandlerObserver* observer); 62 void AddObserver(NetworkStateHandlerObserver* observer);
63 void RemoveObserver(NetworkStateHandlerObserver* observer); 63 void RemoveObserver(NetworkStateHandlerObserver* observer);
64 64
65 // Returns true if |technology| is enabled / available. 65 // Returns true if |technology| is enabled / available.
66 bool TechnologyAvailable(const std::string& technology) const; 66 bool TechnologyAvailable(const std::string& technology) const;
67 bool TechnologyEnabled(const std::string& technology) const; 67 bool TechnologyEnabled(const std::string& technology) const;
68 68
69 // Asynchronously sets the enabled state for |technology|. 69 // Asynchronously sets the enabled property for |technology|.
70 // Note: Modifes Manager state. Calls |error_callback| on failure. 70 // Note: Modifies Manager state. Calls |error_callback| on failure.
71 void SetTechnologyEnabled( 71 void SetTechnologyEnabled(
72 const std::string& technology, 72 const std::string& technology,
73 bool enabled, 73 bool enabled,
74 const network_handler::ErrorCallback& error_callback); 74 const network_handler::ErrorCallback& error_callback);
75 75
76 // Finds and returns a device state by |device_path| or NULL if not found. 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; 77 const DeviceState* GetDeviceState(const std::string& device_path) const;
78 78
79 // Finds and returns a device state by |type|. Returns NULL if not found. 79 // Finds and returns a device state by |type|. Returns NULL if not found.
80 const DeviceState* GetDeviceStateByType(const std::string& type) const; 80 const DeviceState* GetDeviceStateByType(const std::string& type) const;
81 81
82 // Finds and returns a network state by |service_path| or NULL if not found. 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 83 // Note: NetworkState is frequently updated asynchronously, i.e. properties
84 // are not always updated all at once. This will contain the most recent 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, 85 // value for each property. To receive notifications when a property changes,
86 // observer this class and implement NetworkServiceChanged(). 86 // observer this class and implement NetworkPropertyChanged().
pneubeck (no reviews) 2012/12/19 19:40:06 nit: observer -> observe
stevenjb 2012/12/19 21:36:50 Done.
87 const NetworkState* GetNetworkState(const std::string& service_path) const; 87 const NetworkState* GetNetworkState(const std::string& service_path) const;
88 88
89 // Returns the "active" network (first network in the list if connected), 89 // Returns the default connected network (which includes VPNs) or NULL.
90 // NULL if none. 90 // This is equivalent to ConnectedNetworkByType(kMatchTypeDefault).
91 const NetworkState* ActiveNetwork() const; 91 const NetworkState* DefaultNetwork() const;
92 92
93 // Returns the first connected network of type |type|, otherwise NULL. 93 // Returns the primary connected network of type |type|, otherwise NULL.
94 // |type| can be a type defined in service_constants.h, or the additional
95 // match types defined in NetworkState; see NetworkState::MatchesType().
94 const NetworkState* ConnectedNetworkByType(const std::string& type) const; 96 const NetworkState* ConnectedNetworkByType(const std::string& type) const;
95 97
96 // Returns the first connecting network of type |type|, otherwise NULL. 98 // Like ConnectedNetworkByType() but returns the connecting network or NULL.
97 // An empty type will return any connecting non-ethernet network.
98 const NetworkState* ConnectingNetworkByType(const std::string& type) const; 99 const NetworkState* ConnectingNetworkByType(const std::string& type) const;
99 100
100 // Returns the hardware (MAC) address for the first connected network 101 // Returns the hardware (MAC) address for the first connected network
101 // matching |type|, or an empty string if none. 102 // matching |type|, or an empty string if none.
102 std::string HardwareAddressForType(const std::string& type) const; 103 std::string HardwareAddressForType(const std::string& type) const;
103 // Same as above but in aa:bb format. 104 // Same as above but in aa:bb format.
104 std::string FormattedHardwareAddressForType(const std::string& type) const; 105 std::string FormattedHardwareAddressForType(const std::string& type) const;
105 106
106 // Sets |list| to contain the list of networks. The returned list contains 107 // 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 // a copy of NetworkState pointers which should not be stored or used beyond
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 149
149 // Sets the IP Address for the network associated with |service_path|. 150 // Sets the IP Address for the network associated with |service_path|.
150 virtual void UpdateNetworkServiceIPAddress( 151 virtual void UpdateNetworkServiceIPAddress(
151 const std::string& service_path, 152 const std::string& service_path,
152 const std::string& ip_address) OVERRIDE; 153 const std::string& ip_address) OVERRIDE;
153 154
154 // Sends NetworkManagerChanged() to observers. 155 // Sends NetworkManagerChanged() to observers.
155 virtual void ManagerPropertyChanged() OVERRIDE; 156 virtual void ManagerPropertyChanged() OVERRIDE;
156 157
157 // Called by |shill_property_handler_| when the service or device list has 158 // 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 // changed and all entries have been updated. This updates the list and
159 // this notifies observers that the network list has changed, and if the 160 // notifies observers. If |type| == TYPE_NETWORK this also calls
160 // active network has changed sends that notification also. 161 // CheckDefaultNetworkChanged().
161 virtual void ManagedStateListChanged( 162 virtual void ManagedStateListChanged(
162 ManagedState::ManagedType type) OVERRIDE; 163 ManagedState::ManagedType type) OVERRIDE;
163 164
164 // Called in Initialize(). Called explicitly by tests after adding 165 // Called in Initialize(). Called explicitly by tests after adding
165 // test observers. 166 // test observers.
166 void InitShillPropertyHandler(); 167 void InitShillPropertyHandler();
167 168
168 private: 169 private:
169 friend class NetworkStateHandlerTest; 170 friend class NetworkStateHandlerTest;
170 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); 171 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub);
171 172
172 // Non-const getters for managed entries. These are const so that they can 173 // 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 // be called by Get[Network|Device]State, even though they return non-const
174 // pointers. 175 // pointers.
175 DeviceState* GetModifiableDeviceState(const std::string& device_path) const; 176 DeviceState* GetModifiableDeviceState(const std::string& device_path) const;
176 NetworkState* GetModifiableNetworkState( 177 NetworkState* GetModifiableNetworkState(
177 const std::string& service_path) const; 178 const std::string& service_path) const;
178 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list, 179 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list,
179 const std::string& path) const; 180 const std::string& path) const;
180 181
181 // Gets the list specified by |type|. 182 // Gets the list specified by |type|.
182 ManagedStateList* GetManagedList(ManagedState::ManagedType type); 183 ManagedStateList* GetManagedList(ManagedState::ManagedType type);
183 184
184 // Helper function called to parse |network| properties. 185 // Helper function called to parse |network| properties. Also calls
186 // OnNetworkConnectionStateChanged if the connection_state property changes.
pneubeck (no reviews) 2012/12/19 19:40:06 nit: OnNetworkConnectionStateChanged -> OnNetworkC
stevenjb 2012/12/19 21:36:50 Done.
185 bool ParseNetworkServiceProperty(NetworkState* network, 187 bool ParseNetworkServiceProperty(NetworkState* network,
186 const std::string& key, 188 const std::string& key,
187 const base::Value& value); 189 const base::Value& value);
188 190
191 // Helper function to notify observers. Calls CheckDefaultNetworkChanged().
192 bool OnNetworkConnectionStateChanged(NetworkState* network);
193
194 // Called whenever the default network may have changed. If it has changed,
195 // signals observers and returns true.
196 bool CheckDefaultNetworkChanged();
197
189 // Shill property handler instance, owned by this class. 198 // Shill property handler instance, owned by this class.
190 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_; 199 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
191 200
192 // Observer list 201 // Observer list
193 ObserverList<NetworkStateHandlerObserver> observers_; 202 ObserverList<NetworkStateHandlerObserver> observers_;
194 203
195 // Lists of managed states 204 // Lists of managed states
196 ManagedStateList network_list_; 205 ManagedStateList network_list_;
197 ManagedStateList device_list_; 206 ManagedStateList device_list_;
198 207
199 // Lists of available / enabled technologies 208 // Lists of available / enabled technologies
200 std::set<std::string> available_technologies_; 209 std::set<std::string> available_technologies_;
201 std::set<std::string> enabled_technologies_; 210 std::set<std::string> enabled_technologies_;
202 211
203 // Keeps track of the active network for notifying observers when it changes. 212 // Keeps track of the default network for notifying observers when it changes.
204 std::string active_network_path_; 213 std::string default_network_path_;
205 214
206 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); 215 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
207 }; 216 };
208 217
209 } // namespace chromeos 218 } // namespace chromeos
210 219
211 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ 220 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698