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 "chrome/browser/media/router/discovery/discovery_network_info.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "net/base/ip_address.h" |
| 19 #include "net/base/network_change_notifier.h" |
| 20 |
| 21 // Tracks the set of active network interfaces that can be used for local |
| 22 // discovery. If the list of interfaces changes, then |
| 23 // DiscoveryNetworkMonitor::Observer is called with the instance of the monitor. |
| 24 // Only one instance of this will be created per browser process. |
| 25 // |
| 26 // This class is not thread-safe, except for adding and removing observers. |
| 27 // Most of the work done by the monitor is done on the IO thread, which includes |
| 28 // updating the current network ID. Therefore |GetNetworkId| should only be |
| 29 // called from the IO thread. All observers will be notified of network changes |
| 30 // on the thread from which they registered. |
| 31 class DiscoveryNetworkMonitor |
| 32 : public net::NetworkChangeNotifier::NetworkChangeObserver { |
| 33 public: |
| 34 using NetworkInfoFunction = std::vector<DiscoveryNetworkInfo> (*)(); |
| 35 using NetworkRefreshCompleteCallback = base::Callback<void()>; |
| 36 class Observer { |
| 37 public: |
| 38 // Called when the list of connected interfaces has changed. |
| 39 virtual void OnNetworksChanged(const DiscoveryNetworkMonitor& monitor) = 0; |
| 40 |
| 41 protected: |
| 42 ~Observer() = default; |
| 43 }; |
| 44 |
| 45 // Constants for the special states of network Id. |
| 46 // Note: The extra const stops MSVC from thinking this can't be |
| 47 // constexpr. |
| 48 static constexpr char const kNetworkIdDisconnected[] = "__disconnected__"; |
| 49 static constexpr char const kNetworkIdUnknown[] = "__unknown__"; |
| 50 |
| 51 static DiscoveryNetworkMonitor* GetInstance(); |
| 52 |
| 53 void RebindNetworkChangeObserverForTest(); |
| 54 void SetNetworkInfoFunctionForTest(NetworkInfoFunction); |
| 55 |
| 56 void AddObserver(Observer* const observer); |
| 57 void RemoveObserver(Observer* const observer); |
| 58 |
| 59 // Forces a query of the current network state. |callback| will be called |
| 60 // after the refresh. This can be called from any thread and |callback| will |
| 61 // be executed on the calling thread. |
| 62 void Refresh(NetworkRefreshCompleteCallback callback); |
| 63 |
| 64 // Computes a stable string identifier from the list of connected interfaces. |
| 65 // Returns kNetworkIdDisconnected if there are no connected interfaces or |
| 66 // kNetworkIdUnknown if the identifier could not be computed. This should be |
| 67 // called from the IO thread. |
| 68 const std::string& GetNetworkId() const; |
| 69 |
| 70 private: |
| 71 friend class DiscoveryNetworkMonitorTest; |
| 72 friend struct base::LazyInstanceTraitsBase<DiscoveryNetworkMonitor>; |
| 73 |
| 74 DiscoveryNetworkMonitor(); |
| 75 ~DiscoveryNetworkMonitor() override; |
| 76 |
| 77 // net::NetworkChangeNotifier::NetworkChangeObserver |
| 78 void OnNetworkChanged( |
| 79 net::NetworkChangeNotifier::ConnectionType type) override; |
| 80 |
| 81 void UpdateNetworkInfo(); |
| 82 |
| 83 // A hashed representation of the set of networks to which we are connected. |
| 84 // This may also be |kNetworkIdDisconnected| if no interfaces are connected or |
| 85 // |kNetworkIdUnknown| if we can't determine the set of networks. |
| 86 std::string network_id_; |
| 87 |
| 88 // The list of observers which have registered interest in when |network_id_| |
| 89 // changes. |
| 90 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; |
| 91 |
| 92 // Function used to get information about the networks to which we are |
| 93 // connected. |
| 94 NetworkInfoFunction network_info_function_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor); |
| 97 }; |
| 98 |
| 99 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ |
OLD | NEW |