OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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 CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/lazy_instance.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/observer_list_threadsafe.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "net/base/ip_address.h" |
| 18 #include "net/base/network_change_notifier.h" |
| 19 |
| 20 // Represents a single network interface that can be used for discovery. |
| 21 struct NetworkInfo { |
| 22 public: |
| 23 NetworkInfo(); |
| 24 NetworkInfo(uint32_t index, |
| 25 net::NetworkChangeNotifier::ConnectionType connection_type, |
| 26 std::string name, |
| 27 std::string network_id); |
| 28 ~NetworkInfo() = default; |
| 29 |
| 30 NetworkInfo(const NetworkInfo&); |
| 31 NetworkInfo& operator=(const NetworkInfo&); |
| 32 |
| 33 bool operator==(const NetworkInfo&) const; |
| 34 bool operator!=(const NetworkInfo&) const; |
| 35 |
| 36 // The index of the network interface. |
| 37 uint32_t index = 0; |
| 38 // The connection type. Must be CONNECTION_ETHERNET or CONNECTION_WIFI. |
| 39 net::NetworkChangeNotifier::ConnectionType connection_type = |
| 40 net::NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN; |
| 41 // The name of the network interface. |
| 42 std::string name; |
| 43 // A unique identifier for the network interface. |
| 44 // For WiFi networks, this is the SSID. |
| 45 // For Ethernet networks, this is the MAC address. |
| 46 std::string network_id; |
| 47 }; |
| 48 |
| 49 // Tracks the set of active network interfaces that can be used for local |
| 50 // discovery. If the list of interfaces changes, then |
| 51 // DiscoveryNetworkMonitor::Observer is called with the instance of the manager. |
| 52 // Only one instance of this should be created per browser process. |
| 53 class DiscoveryNetworkMonitor |
| 54 : public net::NetworkChangeNotifier::NetworkChangeObserver { |
| 55 public: |
| 56 using NetworkInfoCallback = base::Callback<std::vector<NetworkInfo>()>; |
| 57 using NetworkRefreshCompleteCallback = base::Callback<void()>; |
| 58 class Observer { |
| 59 public: |
| 60 // Called when the list of connected interfaces has changed. |
| 61 virtual void OnNetworksChanged(const DiscoveryNetworkMonitor& manager) = 0; |
| 62 |
| 63 protected: |
| 64 ~Observer() = default; |
| 65 }; |
| 66 |
| 67 static DiscoveryNetworkMonitor* GetInstance(); |
| 68 static DiscoveryNetworkMonitor* GetInstanceForTest(NetworkInfoCallback); |
| 69 |
| 70 void RebindNetworkChangeObserverForTest(); |
| 71 |
| 72 void AddObserver(Observer* const observer); |
| 73 void RemoveObserver(Observer* const observer); |
| 74 |
| 75 // Forces a query of the network state to recompute |network_id_| and |
| 76 // |networks_|. |callback| will be called after the refresh. |
| 77 void Refresh(NetworkRefreshCompleteCallback callback); |
| 78 |
| 79 // Computes a stable string identifier from the list of connected interfaces. |
| 80 // Returns "disconnected" if there are no connected interfaces or "unknown" if |
| 81 // the identifier could not be computed. |
| 82 const std::string GetNetworkId() const { |
| 83 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 84 return network_id_; |
| 85 } |
| 86 |
| 87 // net::NetworkChangeNotifier::NetworkChangeObserver |
| 88 void OnNetworkChanged( |
| 89 net::NetworkChangeNotifier::ConnectionType type) override; |
| 90 |
| 91 // Constants for the special states of network Id. |
| 92 static const char kNetworkIdDisconnected[]; |
| 93 static const char kNetworkIdUnknown[]; |
| 94 |
| 95 private: |
| 96 friend struct base::LazyInstanceTraitsBase<DiscoveryNetworkMonitor>; |
| 97 |
| 98 DiscoveryNetworkMonitor(); |
| 99 ~DiscoveryNetworkMonitor() override; |
| 100 |
| 101 std::string ComputeNetworkId( |
| 102 const std::vector<NetworkInfo>& network_info_list); |
| 103 void UpdateNetworkInfo(const NetworkRefreshCompleteCallback& callback); |
| 104 |
| 105 std::string network_id_; |
| 106 std::vector<NetworkInfo> networks_; |
| 107 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; |
| 108 NetworkInfoCallback network_info_callback_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor); |
| 111 }; |
| 112 |
| 113 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ |
OLD | NEW |