| OLD | NEW |
| 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 Loading... |
| 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 // observe this class and implement NetworkPropertyChanged(). |
| 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 matching |type|, otherwise NULL. |
| 94 // |type| can be a type defined in service_constants.h, or one of the |
| 95 // following special constants: |
| 96 // * kMatchTypeDefault returns the default (active) network |
| 97 // * kMatchTypeNonVirtual returns the primary connected non virtual network |
| 98 // * kMatchTypeWireless returns the primary connected wireless network |
| 94 const NetworkState* ConnectedNetworkByType(const std::string& type) const; | 99 const NetworkState* ConnectedNetworkByType(const std::string& type) const; |
| 95 | 100 |
| 96 // Returns the first connecting network of type |type|, otherwise NULL. | 101 // Like ConnectedNetworkByType() but returns a connecting network or NULL. |
| 97 // An empty type will return any connecting non-ethernet network. | |
| 98 const NetworkState* ConnectingNetworkByType(const std::string& type) const; | 102 const NetworkState* ConnectingNetworkByType(const std::string& type) const; |
| 99 | 103 |
| 100 // Returns the hardware (MAC) address for the first connected network | 104 // Returns the hardware (MAC) address for the first connected network |
| 101 // matching |type|, or an empty string if none. | 105 // matching |type|, or an empty string if none. |
| 102 std::string HardwareAddressForType(const std::string& type) const; | 106 std::string HardwareAddressForType(const std::string& type) const; |
| 103 // Same as above but in aa:bb format. | 107 // Same as above but in aa:bb format. |
| 104 std::string FormattedHardwareAddressForType(const std::string& type) const; | 108 std::string FormattedHardwareAddressForType(const std::string& type) const; |
| 105 | 109 |
| 106 // Sets |list| to contain the list of networks. The returned list contains | 110 // 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 | 111 // 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 | 112 // the scope of the calling function (i.e. they may later become invalid, but |
| 109 // only on the UI thread). | 113 // only on the UI thread). |
| 110 void GetNetworkList(NetworkStateList* list) const; | 114 void GetNetworkList(NetworkStateList* list) const; |
| 111 | 115 |
| 112 // Requests a scan of wifi networks. This may trigger updates to the network | 116 // Requests a scan of wifi networks. This may trigger updates to the network |
| 113 // list, which will trigger the appropriate observer calls. | 117 // list, which will trigger the appropriate observer calls. |
| 114 // Returns true if a scan was requested. | 118 // Returns true if a scan was requested. |
| 115 bool RequestWifiScan() const; | 119 bool RequestWifiScan() const; |
| 116 | 120 |
| 121 static const char kMatchTypeDefault[]; |
| 122 static const char kMatchTypeWireless[]; |
| 123 static const char kMatchTypeNonVirtual[]; |
| 124 |
| 117 protected: | 125 protected: |
| 118 NetworkStateHandler(); | 126 NetworkStateHandler(); |
| 119 | 127 |
| 120 // ShillPropertyHandler::Listener overrides. | 128 // ShillPropertyHandler::Listener overrides. |
| 121 | 129 |
| 122 // This adds new entries to the managed list specified by |type| and deletes | 130 // This adds new entries to the managed list specified by |type| and deletes |
| 123 // any entries that are no longer in the list. | 131 // any entries that are no longer in the list. |
| 124 virtual void UpdateManagedList(ManagedState::ManagedType type, | 132 virtual void UpdateManagedList(ManagedState::ManagedType type, |
| 125 const base::ListValue& entries) OVERRIDE; | 133 const base::ListValue& entries) OVERRIDE; |
| 126 | 134 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 148 | 156 |
| 149 // Sets the IP Address for the network associated with |service_path|. | 157 // Sets the IP Address for the network associated with |service_path|. |
| 150 virtual void UpdateNetworkServiceIPAddress( | 158 virtual void UpdateNetworkServiceIPAddress( |
| 151 const std::string& service_path, | 159 const std::string& service_path, |
| 152 const std::string& ip_address) OVERRIDE; | 160 const std::string& ip_address) OVERRIDE; |
| 153 | 161 |
| 154 // Sends NetworkManagerChanged() to observers. | 162 // Sends NetworkManagerChanged() to observers. |
| 155 virtual void ManagerPropertyChanged() OVERRIDE; | 163 virtual void ManagerPropertyChanged() OVERRIDE; |
| 156 | 164 |
| 157 // Called by |shill_property_handler_| when the service or device list has | 165 // Called by |shill_property_handler_| when the service or device list has |
| 158 // changed and all entries have been updated. If |type| == TYPE_NETWORK, | 166 // 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 | 167 // notifies observers. If |type| == TYPE_NETWORK this also calls |
| 160 // active network has changed sends that notification also. | 168 // CheckDefaultNetworkChanged(). |
| 161 virtual void ManagedStateListChanged( | 169 virtual void ManagedStateListChanged( |
| 162 ManagedState::ManagedType type) OVERRIDE; | 170 ManagedState::ManagedType type) OVERRIDE; |
| 163 | 171 |
| 164 // Called in Initialize(). Called explicitly by tests after adding | 172 // Called in Initialize(). Called explicitly by tests after adding |
| 165 // test observers. | 173 // test observers. |
| 166 void InitShillPropertyHandler(); | 174 void InitShillPropertyHandler(); |
| 167 | 175 |
| 168 private: | 176 private: |
| 169 friend class NetworkStateHandlerTest; | 177 friend class NetworkStateHandlerTest; |
| 170 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); | 178 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); |
| 171 | 179 |
| 172 // Non-const getters for managed entries. These are const so that they can | 180 // 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 | 181 // be called by Get[Network|Device]State, even though they return non-const |
| 174 // pointers. | 182 // pointers. |
| 175 DeviceState* GetModifiableDeviceState(const std::string& device_path) const; | 183 DeviceState* GetModifiableDeviceState(const std::string& device_path) const; |
| 176 NetworkState* GetModifiableNetworkState( | 184 NetworkState* GetModifiableNetworkState( |
| 177 const std::string& service_path) const; | 185 const std::string& service_path) const; |
| 178 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list, | 186 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list, |
| 179 const std::string& path) const; | 187 const std::string& path) const; |
| 180 | 188 |
| 181 // Gets the list specified by |type|. | 189 // Gets the list specified by |type|. |
| 182 ManagedStateList* GetManagedList(ManagedState::ManagedType type); | 190 ManagedStateList* GetManagedList(ManagedState::ManagedType type); |
| 183 | 191 |
| 184 // Helper function called to parse |network| properties. | 192 // Helper function called to parse |network| properties. Also calls |
| 193 // OnNetworkConnectionStateChanged() if the connection_state property changes. |
| 185 bool ParseNetworkServiceProperty(NetworkState* network, | 194 bool ParseNetworkServiceProperty(NetworkState* network, |
| 186 const std::string& key, | 195 const std::string& key, |
| 187 const base::Value& value); | 196 const base::Value& value); |
| 188 | 197 |
| 198 // Helper function to notify observers. Calls CheckDefaultNetworkChanged(). |
| 199 void OnNetworkConnectionStateChanged(NetworkState* network); |
| 200 |
| 201 // If the default network changed returns true and sets |
| 202 // |default_network_path_|. |
| 203 bool CheckDefaultNetworkChanged(); |
| 204 |
| 205 // Logs an event and notifies observers. |
| 206 void OnDefaultNetworkChanged(); |
| 207 |
| 189 // Shill property handler instance, owned by this class. | 208 // Shill property handler instance, owned by this class. |
| 190 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_; | 209 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_; |
| 191 | 210 |
| 192 // Observer list | 211 // Observer list |
| 193 ObserverList<NetworkStateHandlerObserver> observers_; | 212 ObserverList<NetworkStateHandlerObserver> observers_; |
| 194 | 213 |
| 195 // Lists of managed states | 214 // Lists of managed states |
| 196 ManagedStateList network_list_; | 215 ManagedStateList network_list_; |
| 197 ManagedStateList device_list_; | 216 ManagedStateList device_list_; |
| 198 | 217 |
| 199 // Lists of available / enabled technologies | 218 // Lists of available / enabled technologies |
| 200 std::set<std::string> available_technologies_; | 219 std::set<std::string> available_technologies_; |
| 201 std::set<std::string> enabled_technologies_; | 220 std::set<std::string> enabled_technologies_; |
| 202 | 221 |
| 203 // Keeps track of the active network for notifying observers when it changes. | 222 // Keeps track of the default network for notifying observers when it changes. |
| 204 std::string active_network_path_; | 223 std::string default_network_path_; |
| 205 | 224 |
| 206 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); | 225 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); |
| 207 }; | 226 }; |
| 208 | 227 |
| 209 } // namespace chromeos | 228 } // namespace chromeos |
| 210 | 229 |
| 211 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ | 230 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ |
| OLD | NEW |