Chromium Code Reviews| Index: chrome/browser/net/discovery_network_monitor.h |
| diff --git a/chrome/browser/net/discovery_network_monitor.h b/chrome/browser/net/discovery_network_monitor.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..be33238403c15fa3ddb6fd6d1cf4527fccec62c7 |
| --- /dev/null |
| +++ b/chrome/browser/net/discovery_network_monitor.h |
| @@ -0,0 +1,123 @@ |
| +// Copyright (c) 2017 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 CHROME_BROWSER_NET_DISCOVERY_NETWORK_MONITOR_H_ |
| +#define CHROME_BROWSER_NET_DISCOVERY_NETWORK_MONITOR_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/macros.h" |
| +#include "base/observer_list_threadsafe.h" |
| +#include "base/synchronization/lock.h" |
| +#include "net/base/ip_address.h" |
| +#include "net/base/network_change_notifier.h" |
| + |
| +// Represents a single network interface that can be used for discovery. |
| +struct NetworkInfo { |
| + public: |
| + NetworkInfo(); |
| + NetworkInfo(uint32_t index, |
| + net::NetworkChangeNotifier::ConnectionType connection_type, |
| + std::string name, |
| + std::string network_id, |
| + net::IPAddress ip_address); |
| + ~NetworkInfo() = default; |
| + |
| + NetworkInfo(const NetworkInfo&); |
| + NetworkInfo& operator=(const NetworkInfo&); |
| + |
| + // The index of the network interface. |
| + uint32_t index = 0; |
| + // The connection type. Must be CONNECTION_ETHERNET or CONNECTION_WIFI. |
| + net::NetworkChangeNotifier::ConnectionType connection_type = |
| + net::NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN; |
| + // The name of the network interface. |
| + std::string name; |
| + // A unique identifier for the network interface. |
| + // For WiFi networks, this is the SSID. |
| + // (TODO: How to get on Mac?) |
|
mark a. foltz
2017/03/18 19:58:22
Move to discovery_network_list_wifi.h?
btolsch
2017/04/03 10:16:36
Done.
|
| + // For Ethernet networks, this is the MAC address. |
| + // (TODO: How to get?) |
|
mark a. foltz
2017/03/18 19:58:22
Remove TODO?
btolsch
2017/04/03 10:16:36
Done.
|
| + std::string network_id; |
| + // The IP address that is currently assigned to the interface. |
| + // Currently DIAL discovery is only supported over IPv4. |
| + net::IPAddress ip_address; |
| +}; |
| + |
| +class NetworkInfoStrategy { |
| + public: |
| + virtual std::vector<NetworkInfo> GetNetworkInfo(); |
| +}; |
| + |
| +// Tracks the set of active network interfaces that can be used for DIAL |
|
mark a. foltz
2017/03/18 19:58:22
s/DIAL/local/
btolsch
2017/04/03 10:16:36
Done.
|
| +// discovery. If the list of interfaces changes, then |
| +// DiscoveryNetworkMonitor::Observer is called with the instance of the manager. |
| +// Only one instance of this should be created per browser process. |
| +class DiscoveryNetworkMonitor |
| + : public net::NetworkChangeNotifier::NetworkChangeObserver { |
| + public: |
| + using NetworkInfoStrategy = base::Callback<std::vector<NetworkInfo>()>; |
|
mark a. foltz
2017/03/18 19:58:22
NetworkInfoCallback to avoid confusion with the in
btolsch
2017/04/03 10:16:36
Done.
|
| + using NetworkRefreshCompleteCallback = base::Callback<void()>; |
| + class Observer { |
| + public: |
| + // Called when the list of active networks has changed. |
| + virtual void OnNetworksChanged(const DiscoveryNetworkMonitor& manager) = 0; |
| + |
| + protected: |
| + ~Observer() = default; |
| + }; |
| + |
| + static DiscoveryNetworkMonitor* GetInstance(); |
| + static DiscoveryNetworkMonitor* GetInstanceForTest(NetworkInfoStrategy); |
| + |
| + void RebindNetworkChangeObserverForTest(); |
| + |
| + void AddObserver(Observer* const observer); |
| + void RemoveObserver(Observer* const observer); |
| + |
| + void ForceNetworkInfoRefresh(NetworkRefreshCompleteCallback callback); |
| + |
| + // Returns the current list of active networks. |
| + const std::vector<NetworkInfo>& GetNetworks() const { |
| + base::AutoLock auto_lock(network_info_lock_); |
| + return networks_; |
| + } |
| + |
| + // Computes a stable string identifier from the list of active networks. |
| + // Returns "disconnected" if there are no active networks or "unknown" if the |
| + // identifier could not be computed. |
| + const std::string GetNetworkId() const { |
| + base::AutoLock auto_lock(network_info_lock_); |
| + return network_id_; |
| + } |
| + |
| + // net::NetworkChangeNotifier::NetworkChangeObserver |
| + void OnNetworkChanged( |
| + net::NetworkChangeNotifier::ConnectionType type) override; |
| + |
| + private: |
| + friend struct base::LazyInstanceTraitsBase<DiscoveryNetworkMonitor>; |
| + |
| + DiscoveryNetworkMonitor(); |
| + ~DiscoveryNetworkMonitor() override; |
| + |
| + std::string ComputeNetworkId( |
| + const std::vector<NetworkInfo>& network_info_list); |
|
mark a. foltz
2017/03/18 19:58:21
When would this be computed over anything but |net
btolsch
2017/04/03 10:16:36
To minimize the lock section, this is computed on
|
| + void UpdateNetworkInfo(); |
| + void UpdateNetworkInfoWithObservers(); |
| + void UpdateNetworkInfoWithCallback(NetworkRefreshCompleteCallback callback); |
| + |
| + mutable base::Lock network_info_lock_; // Guards network_id_ and networks_. |
|
imcheng
2017/03/14 22:45:35
Does this need lock if we restricted usage of this
btolsch
2017/04/03 10:16:36
No we wouldn't. I was thinking it was simpler to
|
| + std::string network_id_ = "disconnected"; |
| + std::vector<NetworkInfo> networks_; |
| + scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; |
| + NetworkInfoStrategy network_info_strategy_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor); |
| +}; |
| + |
| +#endif // CHROME_BROWSER_NET_DISCOVERY_NETWORK_MONITOR_H_ |