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 net::IPAddress ip_address); | |
29 ~NetworkInfo() = default; | |
30 | |
31 NetworkInfo(const NetworkInfo&); | |
32 NetworkInfo& operator=(const NetworkInfo&); | |
33 | |
34 bool operator==(const NetworkInfo&) const; | |
35 bool operator!=(const NetworkInfo&) const; | |
36 | |
37 // The index of the network interface. | |
38 uint32_t index = 0; | |
39 // The connection type. Must be CONNECTION_ETHERNET or CONNECTION_WIFI. | |
40 net::NetworkChangeNotifier::ConnectionType connection_type = | |
41 net::NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN; | |
42 // The name of the network interface. | |
43 std::string name; | |
44 // A unique identifier for the network interface. | |
45 // For WiFi networks, this is the SSID. | |
46 // For Ethernet networks, this is the MAC address. | |
47 std::string network_id; | |
48 // The IP address that is currently assigned to the interface. | |
49 // Currently local discovery is only supported over IPv4. | |
mark a. foltz
2017/04/18 20:37:01
I believe mDNS is supported over IPv6 - but am not
btolsch
2017/04/20 04:03:50
It is although I don't know if chromium uses it.
| |
50 net::IPAddress ip_address; | |
51 }; | |
52 | |
53 class NetworkInfoStrategy { | |
54 public: | |
55 virtual std::vector<NetworkInfo> GetNetworkInfo(); | |
56 }; | |
57 | |
58 // Tracks the set of active network interfaces that can be used for local | |
59 // discovery. If the list of interfaces changes, then | |
60 // DiscoveryNetworkMonitor::Observer is called with the instance of the manager. | |
61 // Only one instance of this should be created per browser process. | |
62 class DiscoveryNetworkMonitor | |
63 : public net::NetworkChangeNotifier::NetworkChangeObserver { | |
64 public: | |
65 using NetworkInfoCallback = base::Callback<std::vector<NetworkInfo>()>; | |
66 using NetworkRefreshCompleteCallback = base::Callback<void()>; | |
67 class Observer { | |
68 public: | |
69 // Called when the list of active networks has changed. | |
70 virtual void OnNetworksChanged(const DiscoveryNetworkMonitor& manager) = 0; | |
71 | |
72 protected: | |
73 ~Observer() = default; | |
74 }; | |
75 | |
76 static DiscoveryNetworkMonitor* GetInstance(); | |
77 static DiscoveryNetworkMonitor* GetInstanceForTest(NetworkInfoCallback); | |
78 | |
79 void RebindNetworkChangeObserverForTest(); | |
80 | |
81 void AddObserver(Observer* const observer); | |
82 void RemoveObserver(Observer* const observer); | |
83 | |
84 void ForceNetworkInfoRefresh(NetworkRefreshCompleteCallback callback); | |
mark a. foltz
2017/04/18 20:37:01
Refresh() and document forcing behavior?
btolsch
2017/04/20 04:03:50
Done.
| |
85 | |
86 // Returns the current list of active networks. | |
87 const std::vector<NetworkInfo>& GetNetworks() const { | |
88 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
89 return networks_; | |
90 } | |
91 | |
92 // Computes a stable string identifier from the list of active networks. | |
mark a. foltz
2017/04/18 20:37:01
s/active networks/connected interfaces/
btolsch
2017/04/20 04:03:50
Done.
| |
93 // Returns "disconnected" if there are no active networks or "unknown" if the | |
mark a. foltz
2017/04/18 20:37:01
Declare kConstant constexprs for these special val
btolsch
2017/04/20 04:03:50
Done.
| |
94 // identifier could not be computed. | |
95 const std::string GetNetworkId() const { | |
96 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
97 return network_id_; | |
98 } | |
99 | |
100 // net::NetworkChangeNotifier::NetworkChangeObserver | |
101 void OnNetworkChanged( | |
102 net::NetworkChangeNotifier::ConnectionType type) override; | |
103 | |
104 private: | |
105 friend struct base::LazyInstanceTraitsBase<DiscoveryNetworkMonitor>; | |
106 | |
107 DiscoveryNetworkMonitor(); | |
108 ~DiscoveryNetworkMonitor() override; | |
109 | |
110 std::string ComputeNetworkId( | |
111 const std::vector<NetworkInfo>& network_info_list); | |
112 void UpdateNetworkInfo(const NetworkRefreshCompleteCallback& callback); | |
113 | |
114 std::string network_id_; | |
115 std::vector<NetworkInfo> networks_; | |
116 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; | |
117 NetworkInfoCallback network_info_callback_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor); | |
120 }; | |
121 | |
122 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ | |
OLD | NEW |