Chromium Code Reviews| 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> | |
| 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 NetworkServiceObserver; | |
| 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 | |
| 44 // specified changes occur. | |
| 45 // | |
| 46 // This class must not outlive the ShillManagerClient instance. | |
| 47 class CHROMEOS_EXPORT NetworkStateHandler | |
| 48 : public ShillPropertyChangedObserver { | |
|
pneubeck (no reviews)
2012/10/25 14:42:17
If you want to make the public interface cleaner,
stevenjb
2012/10/25 22:05:22
Agreed, will do that in a followup.
| |
| 49 public: | |
| 50 typedef std::vector<ManagedState*> ManagedStateList; | |
|
pneubeck (no reviews)
2012/10/25 14:42:17
Why is scoped_vector<> high overhead? According to
stevenjb
2012/10/25 22:05:22
Yes, you're right, I was thinking of vector<scoped
pneubeck (no reviews)
2012/10/26 08:17:46
I added comments for the two changes that are nece
| |
| 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. | |
| 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; | |
| 74 | |
| 75 // Finds and returns a device by |type|. Returns NULL if not found. | |
| 76 const DeviceState* GetDeviceStateByType(const std::string& type) const; | |
| 77 | |
| 78 // Finds and returns a network by |path|. Returns NULL if not found. | |
| 79 const NetworkState* GetNetworkState(const std::string& path) const; | |
| 80 | |
| 81 // Returns the "active" network (first network in the list if connected). | |
| 82 const NetworkState* ActiveNetwork() const; | |
| 83 | |
| 84 // Returns the first connected network of type |type|, otherwise NULL. | |
| 85 // An empty type will return any connected non-ethernet network. | |
| 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 HardwareAddress(const std::string& type) const; | |
| 95 // Same as above but in aa:bb format. | |
| 96 std::string HardwareAddressFormatted(const std::string& type) const; | |
| 97 | |
| 98 // Returns the list of networks. This also sends a scan request to Shill. | |
| 99 const NetworkStateList& GetNetworkList() const; | |
| 100 | |
| 101 // ShillPropertyChangedObserver overrides. | |
| 102 virtual void OnPropertyChanged(const std::string& key, | |
| 103 const base::Value& value) OVERRIDE; | |
| 104 | |
| 105 private: | |
| 106 FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); | |
| 107 | |
| 108 typedef std::map<std::string, NetworkServiceObserver*> | |
| 109 NetworkServiceObserverMap; | |
| 110 | |
| 111 // Non-const getters for managed entries. | |
| 112 DeviceState* GetModifiableDeviceState(const std::string& path); | |
| 113 NetworkState* GetModifiableNetworkState(const std::string& path); | |
| 114 | |
| 115 // Callback for dbus method fetching properties. | |
| 116 void ManagerPropertiesCallback(DBusMethodCallStatus call_status, | |
| 117 const base::DictionaryValue& properties); | |
| 118 // Called form OnPropertyChanged() and ManagerPropertiesCallback(). | |
| 119 // Returns true if observers should be notified. | |
| 120 bool ManagerPropertyChanged(const std::string& key, const base::Value& value); | |
| 121 | |
| 122 // Casts the list specified by |type| to a list of base ManagedState pointers. | |
| 123 ManagedStateList* GetManagedList(ManagedState::ManagedType type); | |
| 124 | |
| 125 // Adds new entries to the managed list specified by |type| and deletes any | |
| 126 // entries that are no longer in the list. | |
| 127 void UpdateManagedList(ManagedState::ManagedType type, | |
| 128 const base::ListValue& entries); | |
| 129 | |
| 130 // Updates the Shill service property observers to observe any connected | |
| 131 // or connecting networks. | |
| 132 void UpdateObservedNetworkServices(); | |
| 133 | |
| 134 // Sets |available_technologies_| to contain only entries in |technologies|. | |
| 135 void UpdateAvailableTechnologies(const base::ListValue& technologies); | |
| 136 | |
| 137 // Sets |enabled_technologies_| to contain only entries in |technologies|. | |
| 138 void UpdateEnabledTechnologies(const base::ListValue& technologies); | |
| 139 | |
| 140 // Requests all properties for the service or device (called for new items). | |
| 141 void RequestProperties(ManagedState::ManagedType type, | |
| 142 const std::string& path); | |
| 143 | |
| 144 // Called when Shill returns the properties for a service or device. | |
| 145 void GetPropertiesCallback(ManagedState::ManagedType type, | |
| 146 const std::string& path, | |
| 147 DBusMethodCallStatus call_status, | |
| 148 const base::DictionaryValue& properties); | |
| 149 | |
| 150 // Parses the properties for the service or device. Mostly calls | |
| 151 // managed->PropertyChanged(key, value) for each dictionary entry. | |
| 152 void ParseProperties(ManagedState* managed, | |
| 153 const base::DictionaryValue& properties); | |
| 154 | |
| 155 // Callback invoked when a watched service property changes. Calls | |
| 156 // network->PropertyChanged(key, value) and signals observers. | |
| 157 void NetworkServicePropertyChanged(const std::string& path, | |
| 158 const std::string& key, | |
| 159 const base::Value& value); | |
| 160 | |
| 161 // Callback for getting the IPConfig property of a Network. Handled here | |
| 162 // instead of in NetworkState so that all asynchronous requests are done | |
| 163 // in a single place (also simplifies NetworkState considerably). | |
| 164 void GetIPConfigCallback(const std::string& path, | |
| 165 DBusMethodCallStatus call_status, | |
| 166 const base::DictionaryValue& properties); | |
| 167 | |
| 168 // Request an immediate network scan. | |
| 169 void RequestScan() const; | |
| 170 | |
| 171 // Test accessors | |
| 172 int NumObservedNetworksForTest() const { return observed_networks_.size(); } | |
| 173 | |
| 174 // Convenience pointer for ShillManagerClient | |
| 175 ShillManagerClient* shill_manager_; | |
| 176 | |
| 177 // Observer list | |
| 178 ObserverList<NetworkStateHandlerObserver> observers_; | |
| 179 | |
| 180 // Lists of managed states | |
| 181 NetworkStateList network_list_; | |
| 182 DeviceStateList device_list_; | |
| 183 | |
| 184 // Pending update count for each managed state type | |
| 185 std::map<ManagedState::ManagedType, int> pending_updates_; | |
| 186 | |
| 187 // Lists of available / enabled technologies | |
| 188 std::set<std::string> available_technologies_; | |
| 189 std::set<std::string> enabled_technologies_; | |
| 190 | |
| 191 // List of network services with Shill property changed observers | |
| 192 NetworkServiceObserverMap observed_networks_; | |
| 193 | |
| 194 // Keep track of the active network for notifying observers when it changes. | |
| 195 std::string active_network_path_; | |
| 196 | |
| 197 // For Shill client callbacks | |
| 198 base::WeakPtrFactory<NetworkStateHandler> weak_ptr_factory_; | |
| 199 | |
| 200 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); | |
| 201 }; | |
| 202 | |
| 203 } // namespace chromeos | |
| 204 | |
| 205 #endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ | |
| OLD | NEW |