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 CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_ 22 #define CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_ 23 24 #include <vector> 25 26 #include "base/callback_forward.h" 27 #include "base/compiler_specific.h" 28 #include "base/id_map.h" 29 #include "base/observer_list_threadsafe.h" 30 #include "base/synchronization/lock.h" 31 #include "content/common/content_export.h" 32 #include "content/common/p2p_socket_type.h" 33 #include "content/renderer/p2p/network_list_manager.h" 34 #include "ipc/message_filter.h" 35 #include "net/base/net_util.h" 36 37 namespace base { 38 class MessageLoopProxy; 39 } // namespace base 40 41 namespace net { 42 class IPEndPoint; 43 } // namespace net 44 45 namespace content { 46 47 class NetworkListObserver; 48 class P2PAsyncAddressResolver; 49 class P2PSocketClientImpl; 50 class RenderViewImpl; 51 52 class CONTENT_EXPORT P2PSocketDispatcher : public IPC::MessageFilter, 53 public NetworkListManager { 54 public: 55 explicit P2PSocketDispatcher(base::MessageLoopProxy* ipc_message_loop); 56 57 // NetworkListManager interface: 58 virtual void AddNetworkListObserver( 59 NetworkListObserver* network_list_observer) OVERRIDE; 60 virtual void RemoveNetworkListObserver( 61 NetworkListObserver* network_list_observer) OVERRIDE; 62 63 protected: 64 virtual ~P2PSocketDispatcher(); 65 66 private: 67 friend class P2PAsyncAddressResolver; 68 friend class P2PSocketClientImpl; 69 70 // Send a message asynchronously. 71 virtual void Send(IPC::Message* message); 72 73 // IPC::MessageFilter override. Called on IO thread. 74 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 75 virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE; 76 virtual void OnFilterRemoved() OVERRIDE; 77 virtual void OnChannelClosing() OVERRIDE; 78 79 // Returns the IO message loop. 80 base::MessageLoopProxy* message_loop(); 81 82 // Called by P2PSocketClient. 83 int RegisterClient(P2PSocketClientImpl* client); 84 void UnregisterClient(int id); 85 void SendP2PMessage(IPC::Message* msg); 86 87 // Called by DnsRequest. 88 int RegisterHostAddressRequest(P2PAsyncAddressResolver* request); 89 void UnregisterHostAddressRequest(int id); 90 91 // Incoming message handlers. 92 void OnNetworkListChanged(const net::NetworkInterfaceList& networks); 93 void OnGetHostAddressResult(int32 request_id, 94 const net::IPAddressList& addresses); 95 void OnSocketCreated(int socket_id, 96 const net::IPEndPoint& local_address, 97 const net::IPEndPoint& remote_address); 98 void OnIncomingTcpConnection(int socket_id, const net::IPEndPoint& address); 99 void OnSendComplete(int socket_id); 100 void OnError(int socket_id); 101 void OnDataReceived(int socket_id, const net::IPEndPoint& address, 102 const std::vector<char>& data, 103 const base::TimeTicks& timestamp); 104 105 P2PSocketClientImpl* GetClient(int socket_id); 106 107 scoped_refptr<base::MessageLoopProxy> message_loop_; 108 IDMap<P2PSocketClientImpl> clients_; 109 110 IDMap<P2PAsyncAddressResolver> host_address_requests_; 111 112 bool network_notifications_started_; 113 scoped_refptr<ObserverListThreadSafe<NetworkListObserver> > 114 network_list_observers_; 115 116 IPC::Sender* sender_; 117 118 DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcher); 119 }; 120 121 } // namespace content 122 123 #endif // CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_ 124