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/sockets_dispatcher.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "chrome/common/render_messages.h" | |
9 #include "ipc/ipc_message.h" | |
10 #include "ipc/ipc_message_macros.h" | |
11 | |
12 P2PSocketsDispatcher::P2PSocketsDispatcher(int32 routing_id) | |
13 : channel_(NULL), | |
14 routing_id_(routing_id), | |
15 message_loop_(NULL) { | |
16 } | |
17 | |
18 P2PSocketsDispatcher::~P2PSocketsDispatcher() { | |
19 } | |
20 | |
21 bool P2PSocketsDispatcher::OnMessageReceived(const IPC::Message& message) { | |
22 bool handled = true; | |
23 IPC_BEGIN_MESSAGE_MAP(P2PSocketsDispatcher, message) | |
24 IPC_MESSAGE_HANDLER(ViewMsg_P2P_OnSocketCreated, OnSocketCreated) | |
25 IPC_MESSAGE_HANDLER(ViewMsg_P2P_OnError, OnError) | |
26 IPC_MESSAGE_HANDLER(ViewMsg_P2P_OnDataReceived, OnDataReceived) | |
27 IPC_MESSAGE_UNHANDLED(handled = false) | |
28 IPC_END_MESSAGE_MAP() | |
29 return handled; | |
30 } | |
31 | |
32 void P2PSocketsDispatcher::OnFilterAdded(IPC::Channel* channel) { | |
33 // Captures the message loop for IPC. | |
34 message_loop_ = MessageLoop::current(); | |
35 channel_ = channel; | |
36 } | |
37 | |
38 void P2PSocketsDispatcher::OnFilterRemoved() { | |
39 channel_ = NULL; | |
40 } | |
41 | |
42 void P2PSocketsDispatcher::OnChannelClosing() { | |
43 channel_ = NULL; | |
44 } | |
45 | |
46 P2PSocketClient* P2PSocketsDispatcher::CreateSocket( | |
47 P2PSocketType type, P2PSocketAddress address, | |
48 P2PSocketClient::Delegate* delegate) { | |
49 P2PSocketClient* socket = new P2PSocketClient(this); | |
50 socket->Init(type, address, delegate); | |
51 return socket; | |
52 } | |
53 | |
54 int P2PSocketsDispatcher::RegisterClient(P2PSocketClient* client) { | |
55 return clients_.Add(client); | |
56 } | |
57 | |
58 void P2PSocketsDispatcher::UnregisterClient(int id) { | |
59 clients_.Remove(id); | |
60 } | |
61 | |
62 void P2PSocketsDispatcher::Send(IPC::Message* msg) { | |
63 msg->set_routing_id(routing_id_); | |
64 channel_->Send(msg); | |
65 } | |
66 | |
67 MessageLoop* P2PSocketsDispatcher::message_loop() { | |
68 return message_loop_; | |
69 } | |
70 | |
71 void P2PSocketsDispatcher::OnSocketCreated( | |
72 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
| |
73 P2PSocketClient* client = GetClient(socket_id); | |
74 if (client) { | |
75 client->OnSocketCreated(address); | |
76 } | |
77 } | |
78 | |
79 void P2PSocketsDispatcher::OnError(int socket_id) { | |
80 P2PSocketClient* client = GetClient(socket_id); | |
81 if (client) { | |
82 client->OnError(); | |
83 } | |
84 } | |
85 | |
86 void P2PSocketsDispatcher::OnDataReceived( | |
87 int socket_id, P2PSocketAddress address, | |
88 const std::vector<char>& data) { | |
89 P2PSocketClient* client = GetClient(socket_id); | |
90 if (client) { | |
91 client->OnDataReceived(address, data); | |
92 } | |
93 } | |
94 | |
95 P2PSocketClient* P2PSocketsDispatcher::GetClient(int socket_id) { | |
96 P2PSocketClient* client = clients_.Lookup(socket_id); | |
97 if (client == NULL) { | |
98 // This may happen if the socket was closed, but the browser side | |
99 // 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.
| |
100 // message to the renderer. | |
101 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.
| |
102 "doesn't exist anymore."; | |
103 return NULL; | |
104 } | |
105 | |
106 return client; | |
107 } | |
OLD | NEW |