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.
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().
10 // Relationship of classes.
12 // P2PSocketHost P2PSocketClient
16 // P2PSocketDispatcherHost <---------> P2PSocketDispatcher
18 // P2PSocketDispatcher receives and dispatches messages on the
21 #ifndef CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_
22 #define CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_
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 "ipc/message_filter.h"
34 #include "net/base/net_util.h"
37 class MessageLoopProxy
;
46 class NetworkListObserver
;
47 class P2PAsyncAddressResolver
;
48 class P2PSocketClientImpl
;
51 class CONTENT_EXPORT P2PSocketDispatcher
: public IPC::MessageFilter
{
53 explicit P2PSocketDispatcher(base::MessageLoopProxy
* ipc_message_loop
);
55 // Add a new network list observer. Each observer is called
56 // immidiately after it is registered and then later whenever
57 // network configuration changes. Can be called on any thread. The
58 // observer is always called on the thread it was added.
59 void AddNetworkListObserver(NetworkListObserver
* network_list_observer
);
61 // Removes network list observer. Must be called on the thread on
62 // which the observer was added.
63 void RemoveNetworkListObserver(NetworkListObserver
* network_list_observer
);
66 virtual ~P2PSocketDispatcher();
69 friend class P2PAsyncAddressResolver
;
70 friend class P2PSocketClientImpl
;
72 // Send a message asynchronously.
73 virtual void Send(IPC::Message
* message
);
75 // IPC::MessageFilter override. Called on IO thread.
76 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
77 virtual void OnFilterAdded(IPC::Sender
* sender
) OVERRIDE
;
78 virtual void OnFilterRemoved() OVERRIDE
;
79 virtual void OnChannelClosing() OVERRIDE
;
81 // Returns the IO message loop.
82 base::MessageLoopProxy
* message_loop();
84 // Called by P2PSocketClient.
85 int RegisterClient(P2PSocketClientImpl
* client
);
86 void UnregisterClient(int id
);
87 void SendP2PMessage(IPC::Message
* msg
);
89 // Called by DnsRequest.
90 int RegisterHostAddressRequest(P2PAsyncAddressResolver
* request
);
91 void UnregisterHostAddressRequest(int id
);
93 // Incoming message handlers.
94 void OnNetworkListChanged(const net::NetworkInterfaceList
& networks
);
95 void OnGetHostAddressResult(int32 request_id
,
96 const net::IPAddressList
& addresses
);
97 void OnSocketCreated(int socket_id
,
98 const net::IPEndPoint
& local_address
,
99 const net::IPEndPoint
& remote_address
);
100 void OnIncomingTcpConnection(int socket_id
, const net::IPEndPoint
& address
);
101 void OnSendComplete(int socket_id
);
102 void OnError(int socket_id
);
103 void OnDataReceived(int socket_id
, const net::IPEndPoint
& address
,
104 const std::vector
<char>& data
,
105 const base::TimeTicks
& timestamp
);
107 P2PSocketClientImpl
* GetClient(int socket_id
);
109 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
110 IDMap
<P2PSocketClientImpl
> clients_
;
112 IDMap
<P2PAsyncAddressResolver
> host_address_requests_
;
114 bool network_notifications_started_
;
115 scoped_refptr
<ObserverListThreadSafe
<NetworkListObserver
> >
116 network_list_observers_
;
118 IPC::Sender
* sender_
;
120 DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcher
);
123 } // namespace content
125 #endif // CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_