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 <map> |
| 9 #include <set> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 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 |
| 19 namespace base { |
| 20 class DictionaryValue; |
| 21 class ListValue; |
| 22 class Value; |
| 23 } |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 class DeviceState; |
| 28 class NetworkState; |
| 29 class NetworkStateHandlerObserver; |
| 30 |
| 31 namespace internal { |
| 32 class NetworkStateHandlerImpl; |
| 33 } |
| 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 invoke its own more specific observer methods when the specified |
| 44 // changes occur. |
| 45 class CHROMEOS_EXPORT NetworkStateHandler { |
| 46 public: |
| 47 typedef std::vector<ManagedState*> ManagedStateList; |
| 48 typedef std::vector<const NetworkState*> NetworkStateList; |
| 49 |
| 50 NetworkStateHandler(); |
| 51 ~NetworkStateHandler(); |
| 52 |
| 53 // Initialize NetworkStateHandlerImpl. |
| 54 void Init(); |
| 55 |
| 56 // Add/remove observers. |
| 57 void AddObserver(NetworkStateHandlerObserver* observer); |
| 58 void RemoveObserver(NetworkStateHandlerObserver* observer); |
| 59 |
| 60 // Returns true if |technology| is enabled / available. |
| 61 bool TechnologyAvailable(const std::string& technology) const; |
| 62 bool TechnologyEnabled(const std::string& technology) const; |
| 63 |
| 64 // Asynchronously sets the enabled state for |technology|. |
| 65 // Note: Modifes Manager state. TODO(stevenjb): Add a completion callback. |
| 66 void SetTechnologyEnabled(const std::string& technology, bool enabled); |
| 67 |
| 68 // Finds and returns a device state by |device_path| or NULL if not found. |
| 69 const DeviceState* GetDeviceState(const std::string& device_path) const; |
| 70 |
| 71 // Finds and returns a device state by |type|. Returns NULL if not found. |
| 72 const DeviceState* GetDeviceStateByType(const std::string& type) const; |
| 73 |
| 74 // Finds and returns a network state by |service_path| or NULL if not found. |
| 75 // Note: NetworkState is frequently updated asynchronously, i.e. properties |
| 76 // are not always updated all at once. This will contain the most recent |
| 77 // value for each state. To receive notifications when the state changes, |
| 78 // observer this class and implement NetworkServiceChanged(). |
| 79 const NetworkState* GetNetworkState(const std::string& service_path) const; |
| 80 |
| 81 // Returns the "active" network (first network in the list if connected), |
| 82 // NULL if none. |
| 83 const NetworkState* ActiveNetwork() const; |
| 84 |
| 85 // Returns the first connected network of type |type|, otherwise NULL. |
| 86 const NetworkState* ConnectedNetworkByType(const std::string& type) const; |
| 87 |
| 88 // Returns the first connecting network of type |type|, otherwise NULL. |
| 89 // An empty type will return any connecting non-ethernet network. |
| 90 const NetworkState* ConnectingNetworkByType(const std::string& type) const; |
| 91 |
| 92 // Returns the hardware (MAC) address for the first connected network |
| 93 // matching |type|, or an empty string if none. |
| 94 std::string HardwareAddressForType(const std::string& type) const; |
| 95 // Same as above but in aa:bb format. |
| 96 std::string FormattedHardwareAddressForType(const std::string& type) const; |
| 97 |
| 98 // Sets |list| to contain the list of networks. The returned list contains |
| 99 // a copy of NetworkState pointers which should not be stored or used beyond |
| 100 // the scope of the calling function (i.e. they may later become invalid, but |
| 101 // only on the UI thread). This also sends a scan request to Shill which may |
| 102 // trigger updates to the networks (which will trigger the appropriate |
| 103 // observer calls). |
| 104 void GetNetworkList(NetworkStateList* list) const; |
| 105 |
| 106 private: |
| 107 friend class internal::NetworkStateHandlerImpl; |
| 108 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); |
| 109 |
| 110 void NotifyManagerChanged(); |
| 111 |
| 112 // Non-const getters for managed entries. These are const so that they can |
| 113 // be called by Get[Network|Device]State, even though they return non-const |
| 114 // pointers. |
| 115 DeviceState* GetModifiableDeviceState(const std::string& device_path) const; |
| 116 NetworkState* GetModifiableNetworkState( |
| 117 const std::string& service_path) const; |
| 118 ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list, |
| 119 const std::string& path) const; |
| 120 |
| 121 // Casts the list specified by |type| to a list of base ManagedState pointers. |
| 122 ManagedStateList* GetManagedList(ManagedState::ManagedType type); |
| 123 |
| 124 // Adds new entries to the managed list specified by |type| and deletes any |
| 125 // entries that are no longer in the list. |
| 126 void UpdateManagedList(ManagedState::ManagedType type, |
| 127 const base::ListValue& entries); |
| 128 |
| 129 // Sets |available_technologies_| to contain only entries in |technologies|. |
| 130 void SetAvailableTechnologies(const base::ListValue& technologies); |
| 131 |
| 132 // Sets |enabled_technologies_| to contain only entries in |technologies|. |
| 133 void SetEnabledTechnologies(const base::ListValue& technologies); |
| 134 |
| 135 // Parses the properties for the service or device. Mostly calls |
| 136 // managed->PropertyChanged(key, value) for each dictionary entry. |
| 137 void ParseProperties(ManagedState::ManagedType type, |
| 138 const std::string& path, |
| 139 const base::DictionaryValue& properties); |
| 140 |
| 141 // Callback invoked when a watched service property changes. Calls |
| 142 // network->PropertyChanged(key, value) and signals observers. |
| 143 void NetworkServicePropertyChanged(const std::string& service_path, |
| 144 const std::string& key, |
| 145 const base::Value& value); |
| 146 |
| 147 // Sets the IP Address for |service_path|. |
| 148 void SetServiceIPAddress(const std::string& service_path, |
| 149 const std::string& ip_address); |
| 150 |
| 151 // Notifies observers that the network list has changed, and if the active |
| 152 // network has changed send that notification also. |
| 153 void NotifyNetworkServiceObservers(); |
| 154 |
| 155 // Test accessors |
| 156 size_t NumObservedNetworksForTest() const; |
| 157 |
| 158 scoped_ptr<internal::NetworkStateHandlerImpl> impl_; |
| 159 |
| 160 // Observer list |
| 161 ObserverList<NetworkStateHandlerObserver> observers_; |
| 162 |
| 163 // Lists of managed states |
| 164 ManagedStateList network_list_; |
| 165 ManagedStateList device_list_; |
| 166 |
| 167 // Lists of available / enabled technologies |
| 168 std::set<std::string> available_technologies_; |
| 169 std::set<std::string> enabled_technologies_; |
| 170 |
| 171 // Keeps track of the active network for notifying observers when it changes. |
| 172 std::string active_network_path_; |
| 173 |
| 174 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); |
| 175 }; |
| 176 |
| 177 } // namespace chromeos |
| 178 |
| 179 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ |
OLD | NEW |