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.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_(MessageLoop::current()) { |
| 15 } |
| 16 |
| 17 P2PSocketDispatcher::~P2PSocketDispatcher() { |
| 18 } |
| 19 |
| 20 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 21 bool handled = true; |
| 22 IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcher, message) |
| 23 IPC_MESSAGE_HANDLER(P2PMsg_OnSocketCreated, OnSocketCreated) |
| 24 IPC_MESSAGE_HANDLER(P2PMsg_OnError, OnError) |
| 25 IPC_MESSAGE_HANDLER(P2PMsg_OnDataReceived, OnDataReceived) |
| 26 IPC_MESSAGE_UNHANDLED(handled = false) |
| 27 IPC_END_MESSAGE_MAP() |
| 28 return handled; |
| 29 } |
| 30 |
| 31 P2PSocketClient* P2PSocketDispatcher::CreateSocket( |
| 32 P2PSocketType type, P2PSocketAddress address, |
| 33 P2PSocketClient::Delegate* delegate) { |
| 34 P2PSocketClient* socket = new P2PSocketClient(this); |
| 35 socket->Init(type, address, delegate); |
| 36 return socket; |
| 37 } |
| 38 |
| 39 int P2PSocketDispatcher::RegisterClient(P2PSocketClient* client) { |
| 40 return clients_.Add(client); |
| 41 } |
| 42 |
| 43 void P2PSocketDispatcher::UnregisterClient(int id) { |
| 44 clients_.Remove(id); |
| 45 } |
| 46 |
| 47 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { |
| 48 msg->set_routing_id(routing_id()); |
| 49 Send(msg); |
| 50 } |
| 51 |
| 52 MessageLoop* P2PSocketDispatcher::message_loop() { |
| 53 return message_loop_; |
| 54 } |
| 55 |
| 56 void P2PSocketDispatcher::OnSocketCreated( |
| 57 int socket_id, P2PSocketAddress address) { |
| 58 P2PSocketClient* client = GetClient(socket_id); |
| 59 if (client) { |
| 60 client->OnSocketCreated(address); |
| 61 } |
| 62 } |
| 63 |
| 64 void P2PSocketDispatcher::OnError(int socket_id) { |
| 65 P2PSocketClient* client = GetClient(socket_id); |
| 66 if (client) { |
| 67 client->OnError(); |
| 68 } |
| 69 } |
| 70 |
| 71 void P2PSocketDispatcher::OnDataReceived( |
| 72 int socket_id, P2PSocketAddress address, |
| 73 const std::vector<char>& data) { |
| 74 P2PSocketClient* client = GetClient(socket_id); |
| 75 if (client) { |
| 76 client->OnDataReceived(address, data); |
| 77 } |
| 78 } |
| 79 |
| 80 P2PSocketClient* P2PSocketDispatcher::GetClient(int socket_id) { |
| 81 P2PSocketClient* client = clients_.Lookup(socket_id); |
| 82 if (client == NULL) { |
| 83 // This may happen if the socket was closed, but the browser side |
| 84 // hasn't processed the close message by the time it sends the |
| 85 // message to the renderer. |
| 86 VLOG(1) << "Received P2P message for socket that doesn't exist."; |
| 87 return NULL; |
| 88 } |
| 89 |
| 90 return client; |
| 91 } |
OLD | NEW |