Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: chrome/browser/media/router/discovery/discovery_network_monitor.h

Issue 2750453002: Add DiscoveryNetworkMonitor implementation (Closed)
Patch Set: Respond to imcheng's comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.
mark a. foltz 2017/06/05 21:28:43 Nit: s/should/will/ since we enforce this through
btolsch 2017/06/05 22:38:33 Done.
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 static constexpr char kNetworkIdDisconnected[] = "disconnected";
mark a. foltz 2017/06/05 21:28:43 Nit: Adding non-lowercase-ascii characters to thes
btolsch 2017/06/05 22:38:33 Done.
47 static constexpr char kNetworkIdUnknown[] = "unknown";
48
49 static DiscoveryNetworkMonitor* GetInstance();
50
51 void RebindNetworkChangeObserverForTest();
52 void SetNetworkInfoFunctionForTest(NetworkInfoFunction);
53
54 void AddObserver(Observer* const observer);
55 void RemoveObserver(Observer* const observer);
56
57 // Forces a query of the current network state. |callback| will be called
58 // after the refresh. This can be called from any thread and |callback| will
59 // be executed on the calling thread.
60 void Refresh(NetworkRefreshCompleteCallback callback);
61
62 // Computes a stable string identifier from the list of connected interfaces.
63 // Returns kNetworkIdDisconnected if there are no connected interfaces or
64 // kNetworkIdUnknown if the identifier could not be computed. This should be
65 // called from the IO thread.
66 const std::string& GetNetworkId() const;
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();
79
80 // A hashed representation of the set of networks to which we are connected.
81 // This may also be |kNetworkIdDisconnected| if no interfaces are connected or
82 // |kNetworkIdUnknown| if we can't determine the set of networks.
83 std::string network_id_;
84
85 // The list of observers which have registered interest in when |network_id_|
86 // changes.
87 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_;
88
89 // Function used to get information about the networks to which we are
90 // connected.
91 NetworkInfoFunction network_info_function_;
92
93 DISALLOW_COPY_AND_ASSIGN(DiscoveryNetworkMonitor);
94 };
95
96 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DISCOVERY_NETWORK_MONITOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698