| 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 "chrome/renderer/p2p/socket_dispatcher.h" | |
| 6 | |
| 7 #include "base/message_loop_proxy.h" | |
| 8 #include "content/common/p2p_messages.h" | |
| 9 #include "ipc/ipc_message.h" | |
| 10 #include "ipc/ipc_message_macros.h" | |
| 11 | |
| 12 P2PSocketDispatcher::P2PSocketDispatcher(RenderView* render_view) | |
| 13 : RenderViewObserver(render_view), | |
| 14 message_loop_(base::MessageLoopProxy::CreateForCurrentThread()) { | |
| 15 } | |
| 16 | |
| 17 P2PSocketDispatcher::~P2PSocketDispatcher() { | |
| 18 for (IDMap<P2PSocketClient>::iterator i(&clients_); !i.IsAtEnd(); | |
| 19 i.Advance()) { | |
| 20 i.GetCurrentValue()->Detach(); | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) { | |
| 25 bool handled = true; | |
| 26 IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcher, message) | |
| 27 IPC_MESSAGE_HANDLER(P2PMsg_OnSocketCreated, OnSocketCreated) | |
| 28 IPC_MESSAGE_HANDLER(P2PMsg_OnError, OnError) | |
| 29 IPC_MESSAGE_HANDLER(P2PMsg_OnDataReceived, OnDataReceived) | |
| 30 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 31 IPC_END_MESSAGE_MAP() | |
| 32 return handled; | |
| 33 } | |
| 34 | |
| 35 int P2PSocketDispatcher::RegisterClient(P2PSocketClient* client) { | |
| 36 return clients_.Add(client); | |
| 37 } | |
| 38 | |
| 39 void P2PSocketDispatcher::UnregisterClient(int id) { | |
| 40 clients_.Remove(id); | |
| 41 } | |
| 42 | |
| 43 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { | |
| 44 msg->set_routing_id(routing_id()); | |
| 45 Send(msg); | |
| 46 } | |
| 47 | |
| 48 base::MessageLoopProxy* P2PSocketDispatcher::message_loop() { | |
| 49 return message_loop_; | |
| 50 } | |
| 51 | |
| 52 void P2PSocketDispatcher::OnSocketCreated( | |
| 53 int socket_id, const net::IPEndPoint& address) { | |
| 54 P2PSocketClient* client = GetClient(socket_id); | |
| 55 if (client) { | |
| 56 client->OnSocketCreated(address); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 void P2PSocketDispatcher::OnError(int socket_id) { | |
| 61 P2PSocketClient* client = GetClient(socket_id); | |
| 62 if (client) { | |
| 63 client->OnError(); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void P2PSocketDispatcher::OnDataReceived( | |
| 68 int socket_id, const net::IPEndPoint& address, | |
| 69 const std::vector<char>& data) { | |
| 70 P2PSocketClient* client = GetClient(socket_id); | |
| 71 if (client) { | |
| 72 client->OnDataReceived(address, data); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 P2PSocketClient* P2PSocketDispatcher::GetClient(int socket_id) { | |
| 77 P2PSocketClient* client = clients_.Lookup(socket_id); | |
| 78 if (client == NULL) { | |
| 79 // This may happen if the socket was closed, but the browser side | |
| 80 // hasn't processed the close message by the time it sends the | |
| 81 // message to the renderer. | |
| 82 VLOG(1) << "Received P2P message for socket that doesn't exist."; | |
| 83 return NULL; | |
| 84 } | |
| 85 | |
| 86 return client; | |
| 87 } | |
| OLD | NEW |