1 // Copyright 2015 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_P2P_EMPTY_NETWORK_MANAGER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_P2P_EMPTY_NETWORK_MANAGER_H_
7 
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "third_party/blink/renderer/platform/platform_export.h"
12 #include "third_party/webrtc/rtc_base/network.h"
13 #include "third_party/webrtc/rtc_base/third_party/sigslot/sigslot.h"
14 
15 namespace rtc {
16 class IPAddress;
17 }  // namespace rtc
18 
19 namespace blink {
20 
21 // A NetworkManager implementation which handles the case where local address
22 // enumeration is not requested and just returns empty network lists. This class
23 // is not thread safe and should only be used by WebRTC's network thread.
24 class EmptyNetworkManager : public rtc::NetworkManagerBase,
25                             public sigslot::has_slots<> {
26  public:
27   // This class is created by WebRTC's signaling thread but used by WebRTC's
28   // worker thread |task_runner|.
29   PLATFORM_EXPORT explicit EmptyNetworkManager(
30       rtc::NetworkManager* network_manager);
31   PLATFORM_EXPORT ~EmptyNetworkManager() override;
32 
33   // rtc::NetworkManager:
34   void StartUpdating() override;
35   void StopUpdating() override;
36   void GetNetworks(NetworkList* networks) const override;
37   bool GetDefaultLocalAddress(int family,
38                               rtc::IPAddress* ipaddress) const override;
39 
40  private:
41   // Receive callback from the wrapped NetworkManager when the underneath
42   // network list is changed.
43   //
44   // We wait for this so that we don't signal networks changed before we have
45   // default IP addresses.
46   void OnNetworksChanged();
47 
48   THREAD_CHECKER(thread_checker_);
49 
50   // Whether we have fired the first SignalNetworksChanged.
51   // Used to ensure we only report metrics once.
52   bool sent_first_update_ = false;
53 
54   // SignalNetworksChanged will only be fired if there is any outstanding
55   // StartUpdating.
56   int start_count_ = 0;
57 
58   // |network_manager_| is just a reference, owned by
59   // PeerConnectionDependencyFactory.
60   rtc::NetworkManager* network_manager_;
61 
62   base::WeakPtrFactory<EmptyNetworkManager> weak_ptr_factory_{this};
63 
64   DISALLOW_COPY_AND_ASSIGN(EmptyNetworkManager);
65 };
66 
67 }  // namespace blink
68 
69 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_P2P_EMPTY_NETWORK_MANAGER_H_
70