Chromium Code Reviews| Index: chrome/browser/media/router/discovery/discovery_network_monitor.h |
| diff --git a/chrome/browser/media/router/discovery/discovery_network_monitor.h b/chrome/browser/media/router/discovery/discovery_network_monitor.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..df67790f32c19c6c0815e5a3d9d3a6bd3394eb25 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/discovery/discovery_network_monitor.h |
| @@ -0,0 +1,113 @@ |
| +// 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_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ |
| +#define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_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 "content/public/browser/browser_thread.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. |
|
mark a. foltz
2017/05/04 23:16:44
...for local discovery
btolsch
2017/05/23 08:55:03
Done.
|
| +struct NetworkInfo { |
| + public: |
| + NetworkInfo(); |
| + NetworkInfo(uint32_t index, |
| + net::NetworkChangeNotifier::ConnectionType connection_type, |
| + std::string name, |
| + std::string network_id); |
| + ~NetworkInfo() = default; |
| + |
| + NetworkInfo(const NetworkInfo&); |
| + NetworkInfo& operator=(const NetworkInfo&); |
| + |
| + bool operator==(const NetworkInfo&) const; |
| + bool operator!=(const NetworkInfo&) const; |
| + |
| + // 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. |
|
mark a. foltz
2017/05/04 23:16:43
network interface and its connected network.
btolsch
2017/05/23 08:55:03
Done.
|
| + // For WiFi networks, this is the SSID. |
| + // For Ethernet networks, this is the MAC address. |
| + std::string network_id; |
| +}; |
| + |
| +// Tracks the set of active network interfaces that can be used for local |
| +// discovery. If the list of interfaces changes, then |
| +// DiscoveryNetworkMonitor::Observer is called with the instance of the manager. |
|
mark a. foltz
2017/05/04 23:16:44
s/manager/monitor/ here and elsewhere
btolsch
2017/05/23 08:55:03
Done.
|
| +// Only one instance of this should be created per browser process. |
|
mark a. foltz
2017/05/04 23:16:44
Please document threading assumptions for this cla
btolsch
2017/05/23 08:55:03
Done.
|
| +class DiscoveryNetworkMonitor |
| + : public net::NetworkChangeNotifier::NetworkChangeObserver { |
| + public: |
| + using NetworkInfoCallback = base::Callback<std::vector<NetworkInfo>()>; |
| + using NetworkRefreshCompleteCallback = base::Callback<void()>; |
| + class Observer { |
| + public: |
| + // Called when the list of connected interfaces has changed. |
| + virtual void OnNetworksChanged(const DiscoveryNetworkMonitor& manager) = 0; |
| + |
| + protected: |
| + ~Observer() = default; |
| + }; |
| + |
| + static DiscoveryNetworkMonitor* GetInstance(); |
| + static DiscoveryNetworkMonitor* GetInstanceForTest(NetworkInfoCallback); |
| + |
| + void RebindNetworkChangeObserverForTest(); |
| + |
| + void AddObserver(Observer* const observer); |
| + void RemoveObserver(Observer* const observer); |
| + |
| + // Forces a query of the network state to recompute |network_id_| and |
| + // |networks_|. |callback| will be called after the refresh. |
|
mark a. foltz
2017/05/04 23:16:43
Is the point to trigger OnNetworksChanged if there
btolsch
2017/05/23 08:55:03
The internal state update and observer calls could
|
| + void Refresh(NetworkRefreshCompleteCallback callback); |
| + |
| + // Computes a stable string identifier from the list of connected interfaces. |
| + // Returns "disconnected" if there are no connected interfaces or "unknown" if |
|
mark a. foltz
2017/05/04 23:16:43
s/"disconnected"/kNetworkIdDisconnected/ etc
btolsch
2017/05/23 08:55:03
Done.
|
| + // the identifier could not be computed. |
| + const std::string GetNetworkId() const { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + return network_id_; |
|
mark a. foltz
2017/05/04 23:16:43
I think the DCHECK() disqualifies this from inlini
btolsch
2017/05/23 08:55:03
Done.
|
| + } |
| + |
| + // net::NetworkChangeNotifier::NetworkChangeObserver |
| + void OnNetworkChanged( |
| + net::NetworkChangeNotifier::ConnectionType type) override; |
|
mark a. foltz
2017/05/04 23:16:42
Can this be private?
btolsch
2017/05/23 08:55:03
Done.
|
| + |
| + // Constants for the special states of network Id. |
| + static const char kNetworkIdDisconnected[]; |
| + static const char kNetworkIdUnknown[]; |
| + |
| + private: |
| + friend struct base::LazyInstanceTraitsBase<DiscoveryNetworkMonitor>; |
| + |
| + DiscoveryNetworkMonitor(); |
| + ~DiscoveryNetworkMonitor() override; |
| + |
| + std::string ComputeNetworkId( |
| + const std::vector<NetworkInfo>& network_info_list); |
| + void UpdateNetworkInfo(const NetworkRefreshCompleteCallback& callback); |
| + |
| + std::string network_id_; |
| + std::vector<NetworkInfo> networks_; |
| + scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; |
| + NetworkInfoCallback network_info_callback_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor); |
| +}; |
| + |
| +#endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ |