Chromium Code Reviews| Index: chrome/renderer/p2p/sockets_dispatcher.cc |
| diff --git a/chrome/renderer/p2p/sockets_dispatcher.cc b/chrome/renderer/p2p/sockets_dispatcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eaf4d2a25b244802c92e7b68d28da71e5dc395e5 |
| --- /dev/null |
| +++ b/chrome/renderer/p2p/sockets_dispatcher.cc |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/renderer/p2p/sockets_dispatcher.h" |
| + |
| +#include "base/message_loop.h" |
| +#include "chrome/common/render_messages.h" |
| +#include "ipc/ipc_message.h" |
| +#include "ipc/ipc_message_macros.h" |
| + |
| +P2PSocketsDispatcher::P2PSocketsDispatcher(int32 routing_id) |
| + : channel_(NULL), |
| + routing_id_(routing_id), |
| + message_loop_(NULL) { |
| +} |
| + |
| +P2PSocketsDispatcher::~P2PSocketsDispatcher() { |
| +} |
| + |
| +bool P2PSocketsDispatcher::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(P2PSocketsDispatcher, message) |
| + IPC_MESSAGE_HANDLER(ViewMsg_P2P_OnSocketCreated, OnSocketCreated) |
| + IPC_MESSAGE_HANDLER(ViewMsg_P2P_OnError, OnError) |
| + IPC_MESSAGE_HANDLER(ViewMsg_P2P_OnDataReceived, OnDataReceived) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void P2PSocketsDispatcher::OnFilterAdded(IPC::Channel* channel) { |
| + // Captures the message loop for IPC. |
| + message_loop_ = MessageLoop::current(); |
| + channel_ = channel; |
| +} |
| + |
| +void P2PSocketsDispatcher::OnFilterRemoved() { |
| + channel_ = NULL; |
| +} |
| + |
| +void P2PSocketsDispatcher::OnChannelClosing() { |
| + channel_ = NULL; |
| +} |
| + |
| +P2PSocketClient* P2PSocketsDispatcher::CreateSocket( |
| + P2PSocketType type, P2PSocketAddress address, |
| + P2PSocketClient::Delegate* delegate) { |
| + P2PSocketClient* socket = new P2PSocketClient(this); |
| + socket->Init(type, address, delegate); |
| + return socket; |
| +} |
| + |
| +int P2PSocketsDispatcher::RegisterClient(P2PSocketClient* client) { |
| + return clients_.Add(client); |
| +} |
| + |
| +void P2PSocketsDispatcher::UnregisterClient(int id) { |
| + clients_.Remove(id); |
| +} |
| + |
| +void P2PSocketsDispatcher::Send(IPC::Message* msg) { |
| + msg->set_routing_id(routing_id_); |
| + channel_->Send(msg); |
| +} |
| + |
| +MessageLoop* P2PSocketsDispatcher::message_loop() { |
| + return message_loop_; |
| +} |
| + |
| +void P2PSocketsDispatcher::OnSocketCreated( |
| + int socket_id, P2PSocketAddress address) { |
|
awong
2011/03/01 20:21:55
We we care what threads these are called on? Shou
Sergey Ulanov
2011/03/02 16:12:29
Added DCHECK
|
| + P2PSocketClient* client = GetClient(socket_id); |
| + if (client) { |
| + client->OnSocketCreated(address); |
| + } |
| +} |
| + |
| +void P2PSocketsDispatcher::OnError(int socket_id) { |
| + P2PSocketClient* client = GetClient(socket_id); |
| + if (client) { |
| + client->OnError(); |
| + } |
| +} |
| + |
| +void P2PSocketsDispatcher::OnDataReceived( |
| + int socket_id, P2PSocketAddress address, |
| + const std::vector<char>& data) { |
| + P2PSocketClient* client = GetClient(socket_id); |
| + if (client) { |
| + client->OnDataReceived(address, data); |
| + } |
| +} |
| + |
| +P2PSocketClient* P2PSocketsDispatcher::GetClient(int socket_id) { |
| + P2PSocketClient* client = clients_.Lookup(socket_id); |
| + if (client == NULL) { |
| + // This may happen if the socket was closed, but the browser side |
| + // hasn't processed the close message by the time it send the |
|
awong
2011/03/01 20:21:55
send -> sends
Sergey Ulanov
2011/03/02 16:12:29
Done.
|
| + // message to the renderer. |
| + LOG(INFO) << "Received ViewMsg_P2P_OnDataReceived for socket that " |
|
awong
2011/03/01 20:21:55
This sounds more like a VLOG than an LOG_INFO).
Sergey Ulanov
2011/03/02 16:12:29
Done.
|
| + "doesn't exist anymore."; |
| + return NULL; |
| + } |
| + |
| + return client; |
| +} |