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_NET_DISCOVERY_NETWORK_MONITOR_H_ | |
6 #define CHROME_BROWSER_NET_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 "net/base/ip_address.h" | |
17 #include "net/base/network_change_notifier.h" | |
18 | |
19 // Represents a single network interface that can be used for discovery. | |
20 struct NetworkInfo { | |
21 public: | |
22 NetworkInfo(); | |
23 NetworkInfo(uint32_t index, | |
24 net::NetworkChangeNotifier::ConnectionType connection_type, | |
25 std::string name, | |
26 std::string network_id, | |
27 net::IPAddress ip_address); | |
28 ~NetworkInfo() = default; | |
29 | |
30 NetworkInfo(const NetworkInfo&); | |
31 NetworkInfo& operator=(const NetworkInfo&); | |
32 | |
33 // The index of the network interface. | |
34 uint32_t index = 0; | |
35 // The connection type. Must be CONNECTION_ETHERNET or CONNECTION_WIFI. | |
36 net::NetworkChangeNotifier::ConnectionType connection_type = | |
37 net::NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN; | |
38 // The name of the network interface. | |
39 std::string name; | |
40 // A unique identifier for the network interface. | |
41 // For WiFi networks, this is the SSID. | |
42 // (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.
| |
43 // For Ethernet networks, this is the MAC address. | |
44 // (TODO: How to get?) | |
mark a. foltz
2017/03/18 19:58:22
Remove TODO?
btolsch
2017/04/03 10:16:36
Done.
| |
45 std::string network_id; | |
46 // The IP address that is currently assigned to the interface. | |
47 // Currently DIAL discovery is only supported over IPv4. | |
48 net::IPAddress ip_address; | |
49 }; | |
50 | |
51 class NetworkInfoStrategy { | |
52 public: | |
53 virtual std::vector<NetworkInfo> GetNetworkInfo(); | |
54 }; | |
55 | |
56 // 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.
| |
57 // discovery. If the list of interfaces changes, then | |
58 // DiscoveryNetworkMonitor::Observer is called with the instance of the manager. | |
59 // Only one instance of this should be created per browser process. | |
60 class DiscoveryNetworkMonitor | |
61 : public net::NetworkChangeNotifier::NetworkChangeObserver { | |
62 public: | |
63 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.
| |
64 using NetworkRefreshCompleteCallback = base::Callback<void()>; | |
65 class Observer { | |
66 public: | |
67 // Called when the list of active networks has changed. | |
68 virtual void OnNetworksChanged(const DiscoveryNetworkMonitor& manager) = 0; | |
69 | |
70 protected: | |
71 ~Observer() = default; | |
72 }; | |
73 | |
74 static DiscoveryNetworkMonitor* GetInstance(); | |
75 static DiscoveryNetworkMonitor* GetInstanceForTest(NetworkInfoStrategy); | |
76 | |
77 void RebindNetworkChangeObserverForTest(); | |
78 | |
79 void AddObserver(Observer* const observer); | |
80 void RemoveObserver(Observer* const observer); | |
81 | |
82 void ForceNetworkInfoRefresh(NetworkRefreshCompleteCallback callback); | |
83 | |
84 // Returns the current list of active networks. | |
85 const std::vector<NetworkInfo>& GetNetworks() const { | |
86 base::AutoLock auto_lock(network_info_lock_); | |
87 return networks_; | |
88 } | |
89 | |
90 // Computes a stable string identifier from the list of active networks. | |
91 // Returns "disconnected" if there are no active networks or "unknown" if the | |
92 // identifier could not be computed. | |
93 const std::string GetNetworkId() const { | |
94 base::AutoLock auto_lock(network_info_lock_); | |
95 return network_id_; | |
96 } | |
97 | |
98 // net::NetworkChangeNotifier::NetworkChangeObserver | |
99 void OnNetworkChanged( | |
100 net::NetworkChangeNotifier::ConnectionType type) override; | |
101 | |
102 private: | |
103 friend struct base::LazyInstanceTraitsBase<DiscoveryNetworkMonitor>; | |
104 | |
105 DiscoveryNetworkMonitor(); | |
106 ~DiscoveryNetworkMonitor() override; | |
107 | |
108 std::string ComputeNetworkId( | |
109 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
| |
110 void UpdateNetworkInfo(); | |
111 void UpdateNetworkInfoWithObservers(); | |
112 void UpdateNetworkInfoWithCallback(NetworkRefreshCompleteCallback callback); | |
113 | |
114 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
| |
115 std::string network_id_ = "disconnected"; | |
116 std::vector<NetworkInfo> networks_; | |
117 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; | |
118 NetworkInfoStrategy network_info_strategy_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor); | |
121 }; | |
122 | |
123 #endif // CHROME_BROWSER_NET_DISCOVERY_NETWORK_MONITOR_H_ | |
OLD | NEW |