1 // Copyright (c) 2012 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 // P2PSocketDispatcher is a per-renderer object that dispatchers all 6 // P2P messages received from the browser and relays all P2P messages 7 // sent to the browser. P2PSocketClient instances register themselves 8 // with the dispatcher using RegisterClient() and UnregisterClient(). 9 // 10 // Relationship of classes. 11 // 12 // P2PSocketHost P2PSocketClient 13 // ^ ^ 14 // | | 15 // v IPC v 16 // P2PSocketDispatcherHost <---------> P2PSocketDispatcher 17 // 18 // P2PSocketDispatcher receives and dispatches messages on the 19 // IO thread. 20 21 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_P2P_SOCKET_DISPATCHER_H_ 22 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_P2P_SOCKET_DISPATCHER_H_ 23 24 #include <stdint.h> 25 26 #include "base/callback_forward.h" 27 #include "base/compiler_specific.h" 28 #include "base/macros.h" 29 #include "base/memory/ref_counted.h" 30 #include "base/observer_list_threadsafe.h" 31 #include "base/synchronization/lock.h" 32 #include "mojo/public/cpp/bindings/pending_receiver.h" 33 #include "mojo/public/cpp/bindings/receiver.h" 34 #include "mojo/public/cpp/bindings/shared_remote.h" 35 #include "net/base/ip_address.h" 36 #include "net/base/network_interfaces.h" 37 #include "services/network/public/cpp/p2p_socket_type.h" 38 #include "services/network/public/mojom/p2p.mojom-blink.h" 39 #include "third_party/blink/renderer/platform/p2p/network_list_manager.h" 40 #include "third_party/blink/renderer/platform/platform_export.h" 41 #include "third_party/blink/renderer/platform/wtf/vector.h" 42 43 namespace base { 44 class SingleThreadTaskRunner; 45 } // namespace base 46 47 namespace blink { 48 class NetworkListObserver; 49 } 50 51 namespace blink { 52 53 // This class is created on the main thread, but is used primarily on the 54 // WebRTC worker threads. 55 class PLATFORM_EXPORT P2PSocketDispatcher 56 : public base::RefCountedThreadSafe<P2PSocketDispatcher>, 57 public blink::NetworkListManager, 58 public network::mojom::blink::P2PNetworkNotificationClient { 59 public: 60 P2PSocketDispatcher(); 61 62 // blink::NetworkListManager interface: 63 void AddNetworkListObserver( 64 blink::NetworkListObserver* network_list_observer) override; 65 void RemoveNetworkListObserver( 66 blink::NetworkListObserver* network_list_observer) override; 67 68 network::mojom::blink::P2PSocketManager* GetP2PSocketManager(); 69 70 private: 71 friend class base::RefCountedThreadSafe<P2PSocketDispatcher>; 72 73 ~P2PSocketDispatcher() override; 74 75 // network::mojom::blink::P2PNetworkNotificationClient interface. 76 void NetworkListChanged( 77 const Vector<net::NetworkInterface>& networks, 78 const net::IPAddress& default_ipv4_local_address, 79 const net::IPAddress& default_ipv6_local_address) override; 80 81 void RequestInterfaceIfNecessary(); 82 void RequestNetworkEventsIfNecessary(); 83 84 void OnConnectionError(); 85 void ReconnectP2PSocketManager(); 86 87 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; 88 89 // TODO(crbug.com/787254): When moving NetworkListObserver to Oilpan, 90 // thread-safety needs to be taken into account. 91 scoped_refptr<base::ObserverListThreadSafe<blink::NetworkListObserver>> 92 network_list_observers_; 93 94 mojo::PendingReceiver<network::mojom::blink::P2PSocketManager> 95 p2p_socket_manager_receiver_; 96 mojo::SharedRemote<network::mojom::blink::P2PSocketManager> 97 p2p_socket_manager_; 98 base::Lock p2p_socket_manager_lock_; 99 100 // Cached from last |NetworkListChanged| call. 101 Vector<net::NetworkInterface> networks_; 102 net::IPAddress default_ipv4_local_address_; 103 net::IPAddress default_ipv6_local_address_; 104 105 mojo::Receiver<network::mojom::blink::P2PNetworkNotificationClient> 106 network_notification_client_receiver_{this}; 107 108 DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcher); 109 }; 110 111 } // namespace blink 112 113 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_P2P_SOCKET_DISPATCHER_H_ 114