Chromium Code Reviews| Index: chromeos/network/network_state_handler.h |
| diff --git a/chromeos/network/network_state_handler.h b/chromeos/network/network_state_handler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c7ebd5c65bb65ab1432edbdcc8251683635c592c |
| --- /dev/null |
| +++ b/chromeos/network/network_state_handler.h |
| @@ -0,0 +1,205 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ |
| +#define CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ |
| + |
| +#include <list> |
| +#include <map> |
| +#include <set> |
| +#include <string> |
| + |
| +#include "base/gtest_prod_util.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/observer_list.h" |
| +#include "chromeos/chromeos_export.h" |
| +#include "chromeos/dbus/dbus_method_call_status.h" |
| +#include "chromeos/dbus/shill_property_changed_observer.h" |
| +#include "chromeos/network/managed_state.h" |
| + |
| +namespace base { |
| +class DictionaryValue; |
| +class ListValue; |
| +class Value; |
| +} |
| + |
| +namespace chromeos { |
| + |
| +class DeviceState; |
| +class NetworkState; |
| +class NetworkStateHandlerObserver; |
| +class NetworkServiceObserver; |
| +class ShillManagerClient; |
| + |
| +// Class for tracking the list of visible networks and their state. |
| +// |
| +// This class maps essential state from the connection manager (Shill) for |
| +// each visible network. It is not used to change the state of services or |
| +// devices, only global (manager) state. |
| +// |
| +// All getters return the currently cached state. This class is expected to |
| +// keep states up to date by managing the appropriate Shill observers. |
| +// It will send invoke its own more specific observer methods when the |
| +// specified changes occur. |
| +// |
| +// This class must not outlive the ShillManagerClient instance. |
| +class CHROMEOS_EXPORT NetworkStateHandler |
| + : 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.
|
| + public: |
| + 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
|
| + typedef std::vector<NetworkState*> NetworkStateList; |
| + typedef std::vector<DeviceState*> DeviceStateList; |
| + |
| + NetworkStateHandler(); |
| + ~NetworkStateHandler(); |
| + |
| + // Initialize NetworkStateHandler. Sends an initial property request. |
| + void Init(); |
| + |
| + // Add remove observers. |
| + void AddObserver(NetworkStateHandlerObserver* observer); |
| + void RemoveObserver(NetworkStateHandlerObserver* observer); |
| + |
| + // Returns true if |technology| is enabled / available. |
| + bool TechnologyAvailable(const std::string& technology) const; |
| + bool TechnologyEnabled(const std::string& technology) const; |
| + |
| + // Asynchronously sets the enabled state for |technology|. |
| + // Note: Modifes Manager state. TODO(stevenjb): Add a completion callback. |
| + void SetTechnologyEnabled(const std::string& technology, bool enabled); |
| + |
| + // Finds and returns a device by |path|. Returns NULL if not found. |
| + const DeviceState* GetDeviceState(const std::string& path) const; |
| + |
| + // Finds and returns a device by |type|. Returns NULL if not found. |
| + const DeviceState* GetDeviceStateByType(const std::string& type) const; |
| + |
| + // Finds and returns a network by |path|. Returns NULL if not found. |
| + const NetworkState* GetNetworkState(const std::string& path) const; |
| + |
| + // Returns the "active" network (first network in the list if connected). |
| + const NetworkState* ActiveNetwork() const; |
| + |
| + // Returns the first connected network of type |type|, otherwise NULL. |
| + // An empty type will return any connected non-ethernet network. |
| + const NetworkState* ConnectedNetworkByType(const std::string& type) const; |
| + |
| + // Returns the first connecting network of type |type|, otherwise NULL. |
| + // An empty type will return any connecting non-ethernet network. |
| + const NetworkState* ConnectingNetworkByType(const std::string& type) const; |
| + |
| + // Returns the hardware (MAC) address for the first connected network |
| + // matching |type|, or an empty string if none. |
| + std::string HardwareAddress(const std::string& type) const; |
| + // Same as above but in aa:bb format. |
| + std::string HardwareAddressFormatted(const std::string& type) const; |
| + |
| + // Returns the list of networks. This also sends a scan request to Shill. |
| + const NetworkStateList& GetNetworkList() const; |
| + |
| + // ShillPropertyChangedObserver overrides. |
| + virtual void OnPropertyChanged(const std::string& key, |
| + const base::Value& value) OVERRIDE; |
| + |
| +private: |
| + FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub); |
| + |
| + typedef std::map<std::string, NetworkServiceObserver*> |
| + NetworkServiceObserverMap; |
| + |
| + // Non-const getters for managed entries. |
| + DeviceState* GetModifiableDeviceState(const std::string& path); |
| + NetworkState* GetModifiableNetworkState(const std::string& path); |
| + |
| + // Callback for dbus method fetching properties. |
| + void ManagerPropertiesCallback(DBusMethodCallStatus call_status, |
| + const base::DictionaryValue& properties); |
| + // Called form OnPropertyChanged() and ManagerPropertiesCallback(). |
| + // Returns true if observers should be notified. |
| + bool ManagerPropertyChanged(const std::string& key, const base::Value& value); |
| + |
| + // Casts the list specified by |type| to a list of base ManagedState pointers. |
| + ManagedStateList* GetManagedList(ManagedState::ManagedType type); |
| + |
| + // Adds new entries to the managed list specified by |type| and deletes any |
| + // entries that are no longer in the list. |
| + void UpdateManagedList(ManagedState::ManagedType type, |
| + const base::ListValue& entries); |
| + |
| + // Updates the Shill service property observers to observe any connected |
| + // or connecting networks. |
| + void UpdateObservedNetworkServices(); |
| + |
| + // Sets |available_technologies_| to contain only entries in |technologies|. |
| + void UpdateAvailableTechnologies(const base::ListValue& technologies); |
| + |
| + // Sets |enabled_technologies_| to contain only entries in |technologies|. |
| + void UpdateEnabledTechnologies(const base::ListValue& technologies); |
| + |
| + // Requests all properties for the service or device (called for new items). |
| + void RequestProperties(ManagedState::ManagedType type, |
| + const std::string& path); |
| + |
| + // Called when Shill returns the properties for a service or device. |
| + void GetPropertiesCallback(ManagedState::ManagedType type, |
| + const std::string& path, |
| + DBusMethodCallStatus call_status, |
| + const base::DictionaryValue& properties); |
| + |
| + // Parses the properties for the service or device. Mostly calls |
| + // managed->PropertyChanged(key, value) for each dictionary entry. |
| + void ParseProperties(ManagedState* managed, |
| + const base::DictionaryValue& properties); |
| + |
| + // Callback invoked when a watched service property changes. Calls |
| + // network->PropertyChanged(key, value) and signals observers. |
| + void NetworkServicePropertyChanged(const std::string& path, |
| + const std::string& key, |
| + const base::Value& value); |
| + |
| + // Callback for getting the IPConfig property of a Network. Handled here |
| + // instead of in NetworkState so that all asynchronous requests are done |
| + // in a single place (also simplifies NetworkState considerably). |
| + void GetIPConfigCallback(const std::string& path, |
| + DBusMethodCallStatus call_status, |
| + const base::DictionaryValue& properties); |
| + |
| + // Request an immediate network scan. |
| + void RequestScan() const; |
| + |
| + // Test accessors |
| + int NumObservedNetworksForTest() const { return observed_networks_.size(); } |
| + |
| + // Convenience pointer for ShillManagerClient |
| + ShillManagerClient* shill_manager_; |
| + |
| + // Observer list |
| + ObserverList<NetworkStateHandlerObserver> observers_; |
| + |
| + // Lists of managed states |
| + NetworkStateList network_list_; |
| + DeviceStateList device_list_; |
| + |
| + // Pending update count for each managed state type |
| + std::map<ManagedState::ManagedType, int> pending_updates_; |
| + |
| + // Lists of available / enabled technologies |
| + std::set<std::string> available_technologies_; |
| + std::set<std::string> enabled_technologies_; |
| + |
| + // List of network services with Shill property changed observers |
| + NetworkServiceObserverMap observed_networks_; |
| + |
| + // Keep track of the active network for notifying observers when it changes. |
| + std::string active_network_path_; |
| + |
| + // For Shill client callbacks |
| + base::WeakPtrFactory<NetworkStateHandler> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler); |
| +}; |
| + |
| +} // namespace chromeos |
| + |
| +#endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_ |