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 P2PSocketClient* P2PSocketDispatcher::CreateSocket( |
| 36 P2PSocketType type, P2PSocketAddress address, |
| 37 P2PSocketClient::Delegate* delegate) { |
| 38 P2PSocketClient* socket = new P2PSocketClient(this); |
| 39 socket->Init(type, address, delegate); |
| 40 return socket; |
| 41 } |
| 42 |
| 43 int P2PSocketDispatcher::RegisterClient(P2PSocketClient* client) { |
| 44 return clients_.Add(client); |
| 45 } |
| 46 |
| 47 void P2PSocketDispatcher::UnregisterClient(int id) { |
| 48 clients_.Remove(id); |
| 49 } |
| 50 |
| 51 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { |
| 52 msg->set_routing_id(routing_id()); |
| 53 Send(msg); |
| 54 } |
| 55 |
| 56 base::MessageLoopProxy* P2PSocketDispatcher::message_loop() { |
| 57 return message_loop_; |
| 58 } |
| 59 |
| 60 void P2PSocketDispatcher::OnSocketCreated( |
| 61 int socket_id, P2PSocketAddress address) { |
| 62 P2PSocketClient* client = GetClient(socket_id); |
| 63 if (client) { |
| 64 client->OnSocketCreated(address); |
| 65 } |
| 66 } |
| 67 |
| 68 void P2PSocketDispatcher::OnError(int socket_id) { |
| 69 P2PSocketClient* client = GetClient(socket_id); |
| 70 if (client) { |
| 71 client->OnError(); |
| 72 } |
| 73 } |
| 74 |
| 75 void P2PSocketDispatcher::OnDataReceived( |
| 76 int socket_id, P2PSocketAddress address, |
| 77 const std::vector<char>& data) { |
| 78 P2PSocketClient* client = GetClient(socket_id); |
| 79 if (client) { |
| 80 client->OnDataReceived(address, data); |
| 81 } |
| 82 } |
| 83 |
| 84 P2PSocketClient* P2PSocketDispatcher::GetClient(int socket_id) { |
| 85 P2PSocketClient* client = clients_.Lookup(socket_id); |
| 86 if (client == NULL) { |
| 87 // This may happen if the socket was closed, but the browser side |
| 88 // hasn't processed the close message by the time it sends the |
| 89 // message to the renderer. |
| 90 VLOG(1) << "Received P2P message for socket that doesn't exist."; |
| 91 return NULL; |
| 92 } |
| 93 |
| 94 return client; |
| 95 } |
OLD | NEW |