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

Unified Diff: chromeos/network/network_state_handler.h

Issue 11192024: Add chromeos::NetworkStateManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Separate out NetworkStateHandlerImpl, address feedback Created 8 years, 2 months 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 side-by-side diff with in-line comments
Download patch
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..cfa85548bcdbe9d70dffad64a46ed1cf284e23df
--- /dev/null
+++ b/chromeos/network/network_state_handler.h
@@ -0,0 +1,179 @@
+// 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 <map>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "base/gtest_prod_util.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/observer_list.h"
+#include "chromeos/chromeos_export.h"
+#include "chromeos/network/managed_state.h"
+
+namespace base {
+class DictionaryValue;
+class ListValue;
+class Value;
+}
+
+namespace chromeos {
+
+class DeviceState;
+class NetworkState;
+class NetworkStateHandlerObserver;
+
+namespace internal {
+class NetworkStateHandlerImpl;
+}
+
+// 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 invoke its own more specific observer methods when the specified
+// changes occur.
+class CHROMEOS_EXPORT NetworkStateHandler {
+ public:
+ typedef std::vector<ManagedState*> ManagedStateList;
+ typedef std::vector<const NetworkState*> NetworkStateList;
+
+ NetworkStateHandler();
+ ~NetworkStateHandler();
+
+ // Initialize NetworkStateHandlerImpl.
+ 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 state by |device_path| or NULL if not found.
+ const DeviceState* GetDeviceState(const std::string& device_path) const;
+
+ // Finds and returns a device state by |type|. Returns NULL if not found.
+ const DeviceState* GetDeviceStateByType(const std::string& type) const;
+
+ // Finds and returns a network state by |service_path| or NULL if not found.
+ // Note: NetworkState is frequently updated asynchronously, i.e. properties
+ // are not always updated all at once. This will contain the most recent
+ // value for each state. To receive notifications when the state changes,
+ // observer this class and implement NetworkServiceChanged().
+ const NetworkState* GetNetworkState(const std::string& service_path) const;
+
+ // Returns the "active" network (first network in the list if connected),
+ // NULL if none.
+ const NetworkState* ActiveNetwork() const;
+
+ // Returns the first connected network of type |type|, otherwise NULL.
+ 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 HardwareAddressForType(const std::string& type) const;
+ // Same as above but in aa:bb format.
+ std::string FormattedHardwareAddressForType(const std::string& type) const;
+
+ // Sets |list| to contain the list of networks. The returned list contains
+ // a copy of NetworkState pointers which should not be stored or used beyond
+ // the scope of the calling function (i.e. they may later become invalid, but
+ // only on the UI thread). This also sends a scan request to Shill which may
+ // trigger updates to the networks (which will trigger the appropriate
+ // observer calls).
+ void GetNetworkList(NetworkStateList* list) const;
+
+private:
+ friend class internal::NetworkStateHandlerImpl;
+ FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub);
+
+ void NotifyManagerChanged();
+
+ // Non-const getters for managed entries. These are const so that they can
+ // be called by Get[Network|Device]State, even though they return non-const
+ // pointers.
+ DeviceState* GetModifiableDeviceState(const std::string& device_path) const;
+ NetworkState* GetModifiableNetworkState(
+ const std::string& service_path) const;
+ ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list,
+ const std::string& path) const;
+
+ // 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);
+
+ // Sets |available_technologies_| to contain only entries in |technologies|.
+ void SetAvailableTechnologies(const base::ListValue& technologies);
+
+ // Sets |enabled_technologies_| to contain only entries in |technologies|.
+ void SetEnabledTechnologies(const base::ListValue& technologies);
+
+ // Parses the properties for the service or device. Mostly calls
+ // managed->PropertyChanged(key, value) for each dictionary entry.
+ void ParseProperties(ManagedState::ManagedType type,
+ const std::string& path,
+ 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& service_path,
+ const std::string& key,
+ const base::Value& value);
+
+ // Sets the IP Address for |service_path|.
+ void SetServiceIPAddress(const std::string& service_path,
+ const std::string& ip_address);
+
+ // Notifies observers that the network list has changed, and if the active
+ // network has changed send that notification also.
+ void NotifyNetworkServiceObservers();
+
+ // Test accessors
+ size_t NumObservedNetworksForTest() const;
+
+ scoped_ptr<internal::NetworkStateHandlerImpl> impl_;
+
+ // Observer list
+ ObserverList<NetworkStateHandlerObserver> observers_;
+
+ // Lists of managed states
+ ManagedStateList network_list_;
+ ManagedStateList device_list_;
+
+ // Lists of available / enabled technologies
+ std::set<std::string> available_technologies_;
+ std::set<std::string> enabled_technologies_;
+
+ // Keeps track of the active network for notifying observers when it changes.
+ std::string active_network_path_;
+
+ DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
+};
+
+} // namespace chromeos
+
+#endif // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_

Powered by Google App Engine
This is Rietveld 408576698