Chromium Code Reviews| 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 should 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 NetworkInfoCallback = | |
| 35 base::Callback<std::vector<DiscoveryNetworkInfo>()>; | |
| 36 using NetworkRefreshCompleteCallback = base::Callback<void()>; | |
| 37 class Observer { | |
| 38 public: | |
| 39 // Called when the list of connected interfaces has changed. | |
| 40 virtual void OnNetworksChanged(const DiscoveryNetworkMonitor& monitor) = 0; | |
| 41 | |
| 42 protected: | |
| 43 ~Observer() = default; | |
| 44 }; | |
| 45 | |
| 46 static DiscoveryNetworkMonitor* GetInstance(); | |
| 47 static DiscoveryNetworkMonitor* GetInstanceForTest(NetworkInfoCallback); | |
| 48 | |
| 49 void RebindNetworkChangeObserverForTest(); | |
| 50 | |
| 51 void AddObserver(Observer* const observer); | |
| 52 void RemoveObserver(Observer* const observer); | |
| 53 | |
| 54 // Forces a query of the current network state. |callback| will be called | |
| 55 // after the refresh. This should be called from the UI thread. | |
| 56 void Refresh(NetworkRefreshCompleteCallback callback); | |
|
imcheng
2017/05/26 23:49:01
|callback| could be const ref?
btolsch
2017/05/30 09:54:30
From //docs/callback.md, base::Callback should be
| |
| 57 | |
| 58 // Computes a stable string identifier from the list of connected interfaces. | |
| 59 // Returns kNetworkIdDisconnected if there are no connected interfaces or | |
| 60 // kNetworkIdUnknown if the identifier could not be computed. This should be | |
| 61 // called from the IO thread. | |
| 62 const std::string& GetNetworkId() const; | |
| 63 | |
| 64 // Constants for the special states of network Id. | |
|
mark a. foltz
2017/05/26 21:38:55
Constants should be declared before functions:
ht
btolsch
2017/05/26 23:38:10
Done.
| |
| 65 static const char kNetworkIdDisconnected[]; | |
| 66 static const char kNetworkIdUnknown[]; | |
| 67 | |
| 68 private: | |
| 69 friend struct base::LazyInstanceTraitsBase<DiscoveryNetworkMonitor>; | |
| 70 | |
| 71 DiscoveryNetworkMonitor(); | |
| 72 ~DiscoveryNetworkMonitor() override; | |
| 73 | |
| 74 // net::NetworkChangeNotifier::NetworkChangeObserver | |
| 75 void OnNetworkChanged( | |
| 76 net::NetworkChangeNotifier::ConnectionType type) override; | |
| 77 | |
| 78 void UpdateNetworkInfo(const NetworkRefreshCompleteCallback& callback); | |
| 79 | |
| 80 std::string network_id_; | |
|
mark a. foltz
2017/05/26 21:38:55
Can you add documentation for these fields?
btolsch
2017/05/26 23:38:10
Done.
| |
| 81 std::vector<DiscoveryNetworkInfo> networks_; | |
| 82 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; | |
| 83 NetworkInfoCallback network_info_callback_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor); | |
| 86 }; | |
| 87 | |
| 88 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_ | |
| OLD | NEW |