OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #include "content/browser/renderer_host/p2p_sockets_host.h" |
| 6 |
| 7 #include "content/browser/renderer_host/p2p_socket_host.h" |
| 8 #include "content/common/p2p_messages.h" |
| 9 |
| 10 P2PSocketsHost::P2PSocketsHost() { |
| 11 } |
| 12 |
| 13 P2PSocketsHost::~P2PSocketsHost() { |
| 14 } |
| 15 |
| 16 void P2PSocketsHost::OnChannelClosing() { |
| 17 BrowserMessageFilter::OnChannelClosing(); |
| 18 |
| 19 // Since the IPC channel is gone, close pending connections. |
| 20 for (IDMap<P2PSocketHost>::iterator i(&sockets_); !i.IsAtEnd(); i.Advance()) { |
| 21 sockets_.Remove(i.GetCurrentKey()); |
| 22 } |
| 23 } |
| 24 |
| 25 void P2PSocketsHost::OnDestruct() const { |
| 26 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 27 } |
| 28 |
| 29 bool P2PSocketsHost::OnMessageReceived(const IPC::Message& message, |
| 30 bool* message_was_ok) { |
| 31 bool handled = true; |
| 32 IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketsHost, message, *message_was_ok) |
| 33 IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket) |
| 34 IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend) |
| 35 IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket) |
| 36 IPC_MESSAGE_UNHANDLED(handled = false) |
| 37 IPC_END_MESSAGE_MAP_EX() |
| 38 return handled; |
| 39 } |
| 40 |
| 41 void P2PSocketsHost::OnCreateSocket( |
| 42 const IPC::Message& msg, P2PSocketType type, int socket_id, |
| 43 P2PSocketAddress remote_address) { |
| 44 if (sockets_.Lookup(socket_id)) { |
| 45 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket " |
| 46 "that already exists."; |
| 47 return; |
| 48 } |
| 49 |
| 50 if (type != P2P_SOCKET_UDP) { |
| 51 Send(new P2PMsg_OnError(msg.routing_id(), socket_id)); |
| 52 return; |
| 53 } |
| 54 |
| 55 scoped_ptr<P2PSocketHost> socket( |
| 56 P2PSocketHost::Create(this, msg.routing_id(), socket_id, type)); |
| 57 |
| 58 if (!socket.get()) { |
| 59 Send(new P2PMsg_OnError(msg.routing_id(), socket_id)); |
| 60 return; |
| 61 } |
| 62 |
| 63 if (socket->Init()) { |
| 64 sockets_.AddWithID(socket.release(), socket_id); |
| 65 } |
| 66 } |
| 67 |
| 68 void P2PSocketsHost::OnSend(const IPC::Message& msg, int socket_id, |
| 69 P2PSocketAddress socket_address, |
| 70 const std::vector<char>& data) { |
| 71 P2PSocketHost* socket = sockets_.Lookup(socket_id); |
| 72 if (!socket) { |
| 73 LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id."; |
| 74 return; |
| 75 } |
| 76 socket->Send(socket_address, data); |
| 77 } |
| 78 |
| 79 void P2PSocketsHost::OnDestroySocket(const IPC::Message& msg, int socket_id) { |
| 80 sockets_.Remove(socket_id); |
| 81 } |
OLD | NEW |