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 #include "content/renderer/p2p/socket_dispatcher.h"
8 #include "base/memory/ref_counted.h"
9 #include "content/child/child_process.h"
10 #include "content/common/p2p_messages.h"
11 #include "content/renderer/p2p/host_address_request.h"
12 #include "content/renderer/p2p/network_list_observer.h"
13 #include "content/renderer/p2p/socket_client_impl.h"
14 #include "content/renderer/render_view_impl.h"
15 #include "ipc/ipc_sender.h"
19 P2PSocketDispatcher::P2PSocketDispatcher(
20 base::SingleThreadTaskRunner
* ipc_task_runner
)
21 : ipc_task_runner_(ipc_task_runner
),
22 network_notifications_started_(false),
23 network_list_observers_(
24 new base::ObserverListThreadSafe
<NetworkListObserver
>()),
28 P2PSocketDispatcher::~P2PSocketDispatcher() {
29 network_list_observers_
->AssertEmpty();
30 for (IDMap
<P2PSocketClientImpl
>::iterator
i(&clients_
); !i
.IsAtEnd();
32 i
.GetCurrentValue()->Detach();
36 void P2PSocketDispatcher::AddNetworkListObserver(
37 NetworkListObserver
* network_list_observer
) {
38 network_list_observers_
->AddObserver(network_list_observer
);
39 network_notifications_started_
= true;
40 SendP2PMessage(new P2PHostMsg_StartNetworkNotifications());
43 void P2PSocketDispatcher::RemoveNetworkListObserver(
44 NetworkListObserver
* network_list_observer
) {
45 network_list_observers_
->RemoveObserver(network_list_observer
);
48 void P2PSocketDispatcher::Send(IPC::Message
* message
) {
49 DCHECK(ipc_task_runner_
->BelongsToCurrentThread());
51 DLOG(WARNING
) << "P2PSocketDispatcher::Send() - Sender closed.";
56 sender_
->Send(message
);
59 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message
& message
) {
61 IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcher
, message
)
62 IPC_MESSAGE_HANDLER(P2PMsg_NetworkListChanged
, OnNetworkListChanged
)
63 IPC_MESSAGE_HANDLER(P2PMsg_GetHostAddressResult
, OnGetHostAddressResult
)
64 IPC_MESSAGE_HANDLER(P2PMsg_OnSocketCreated
, OnSocketCreated
)
65 IPC_MESSAGE_HANDLER(P2PMsg_OnIncomingTcpConnection
, OnIncomingTcpConnection
)
66 IPC_MESSAGE_HANDLER(P2PMsg_OnSendComplete
, OnSendComplete
)
67 IPC_MESSAGE_HANDLER(P2PMsg_OnError
, OnError
)
68 IPC_MESSAGE_HANDLER(P2PMsg_OnDataReceived
, OnDataReceived
)
69 IPC_MESSAGE_UNHANDLED(handled
= false)
74 void P2PSocketDispatcher::OnFilterAdded(IPC::Sender
* sender
) {
75 DVLOG(1) << "P2PSocketDispatcher::OnFilterAdded()";
79 void P2PSocketDispatcher::OnFilterRemoved() {
83 void P2PSocketDispatcher::OnChannelConnected(int32 peer_id
) {
87 void P2PSocketDispatcher::OnChannelClosing() {
92 base::SingleThreadTaskRunner
* P2PSocketDispatcher::task_runner() {
93 return ipc_task_runner_
.get();
96 int P2PSocketDispatcher::RegisterClient(P2PSocketClientImpl
* client
) {
97 DCHECK(ipc_task_runner_
->BelongsToCurrentThread());
98 return clients_
.Add(client
);
101 void P2PSocketDispatcher::UnregisterClient(int id
) {
102 DCHECK(ipc_task_runner_
->BelongsToCurrentThread());
106 void P2PSocketDispatcher::SendP2PMessage(IPC::Message
* msg
) {
107 if (!ipc_task_runner_
->BelongsToCurrentThread()) {
108 ipc_task_runner_
->PostTask(
109 FROM_HERE
, base::Bind(&P2PSocketDispatcher::Send
, this, msg
));
115 int P2PSocketDispatcher::RegisterHostAddressRequest(
116 P2PAsyncAddressResolver
* request
) {
117 DCHECK(ipc_task_runner_
->BelongsToCurrentThread());
118 return host_address_requests_
.Add(request
);
121 void P2PSocketDispatcher::UnregisterHostAddressRequest(int id
) {
122 DCHECK(ipc_task_runner_
->BelongsToCurrentThread());
123 host_address_requests_
.Remove(id
);
126 void P2PSocketDispatcher::OnNetworkListChanged(
127 const net::NetworkInterfaceList
& networks
) {
128 network_list_observers_
->Notify(
129 FROM_HERE
, &NetworkListObserver::OnNetworkListChanged
, networks
);
132 void P2PSocketDispatcher::OnGetHostAddressResult(
134 const net::IPAddressList
& addresses
) {
135 P2PAsyncAddressResolver
* request
= host_address_requests_
.Lookup(request_id
);
137 DVLOG(1) << "Received P2P message for socket that doesn't exist.";
141 request
->OnResponse(addresses
);
144 void P2PSocketDispatcher::OnSocketCreated(
146 const net::IPEndPoint
& local_address
,
147 const net::IPEndPoint
& remote_address
) {
148 P2PSocketClientImpl
* client
= GetClient(socket_id
);
150 client
->OnSocketCreated(local_address
, remote_address
);
154 void P2PSocketDispatcher::OnIncomingTcpConnection(
155 int socket_id
, const net::IPEndPoint
& address
) {
156 P2PSocketClientImpl
* client
= GetClient(socket_id
);
158 client
->OnIncomingTcpConnection(address
);
162 void P2PSocketDispatcher::OnSendComplete(
164 const P2PSendPacketMetrics
& send_metrics
) {
165 P2PSocketClientImpl
* client
= GetClient(socket_id
);
167 client
->OnSendComplete(send_metrics
);
171 void P2PSocketDispatcher::OnError(int socket_id
) {
172 P2PSocketClientImpl
* client
= GetClient(socket_id
);
178 void P2PSocketDispatcher::OnDataReceived(
179 int socket_id
, const net::IPEndPoint
& address
,
180 const std::vector
<char>& data
,
181 const base::TimeTicks
& timestamp
) {
182 P2PSocketClientImpl
* client
= GetClient(socket_id
);
184 client
->OnDataReceived(address
, data
, timestamp
);
188 P2PSocketClientImpl
* P2PSocketDispatcher::GetClient(int socket_id
) {
189 P2PSocketClientImpl
* client
= clients_
.Lookup(socket_id
);
190 if (client
== NULL
) {
191 // This may happen if the socket was closed, but the browser side
192 // hasn't processed the close message by the time it sends the
193 // message to the renderer.
194 DVLOG(1) << "Received P2P message for socket that doesn't exist.";
201 } // namespace content