OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/p2p/socket_dispatcher.h" | 5 #include "content/renderer/p2p/socket_dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
11 #include "content/common/p2p_messages.h" | 11 #include "content/common/p2p_messages.h" |
12 #include "content/renderer/p2p/host_address_request.h" | 12 #include "content/renderer/p2p/host_address_request.h" |
13 #include "content/renderer/p2p/network_list_observer.h" | 13 #include "content/renderer/p2p/network_list_observer.h" |
14 #include "content/renderer/p2p/socket_client.h" | 14 #include "content/renderer/p2p/socket_client_impl.h" |
15 #include "content/renderer/render_view_impl.h" | 15 #include "content/renderer/render_view_impl.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 P2PSocketDispatcher::P2PSocketDispatcher( | 19 P2PSocketDispatcher::P2PSocketDispatcher( |
20 base::MessageLoopProxy* ipc_message_loop) | 20 base::MessageLoopProxy* ipc_message_loop) |
21 : message_loop_(ipc_message_loop), | 21 : message_loop_(ipc_message_loop), |
22 network_notifications_started_(false), | 22 network_notifications_started_(false), |
23 network_list_observers_( | 23 network_list_observers_( |
24 new ObserverListThreadSafe<NetworkListObserver>()), | 24 new ObserverListThreadSafe<NetworkListObserver>()), |
25 channel_(NULL) { | 25 channel_(NULL) { |
26 } | 26 } |
27 | 27 |
28 P2PSocketDispatcher::~P2PSocketDispatcher() { | 28 P2PSocketDispatcher::~P2PSocketDispatcher() { |
29 network_list_observers_->AssertEmpty(); | 29 network_list_observers_->AssertEmpty(); |
30 for (IDMap<P2PSocketClient>::iterator i(&clients_); !i.IsAtEnd(); | 30 for (IDMap<P2PSocketClientImpl>::iterator i(&clients_); !i.IsAtEnd(); |
31 i.Advance()) { | 31 i.Advance()) { |
32 i.GetCurrentValue()->Detach(); | 32 i.GetCurrentValue()->Detach(); |
33 } | 33 } |
34 } | 34 } |
35 | 35 |
36 void P2PSocketDispatcher::AddNetworkListObserver( | 36 void P2PSocketDispatcher::AddNetworkListObserver( |
37 NetworkListObserver* network_list_observer) { | 37 NetworkListObserver* network_list_observer) { |
38 network_list_observers_->AddObserver(network_list_observer); | 38 network_list_observers_->AddObserver(network_list_observer); |
39 network_notifications_started_ = true; | 39 network_notifications_started_ = true; |
40 SendP2PMessage(new P2PHostMsg_StartNetworkNotifications()); | 40 SendP2PMessage(new P2PHostMsg_StartNetworkNotifications()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 81 } |
82 | 82 |
83 void P2PSocketDispatcher::OnChannelClosing() { | 83 void P2PSocketDispatcher::OnChannelClosing() { |
84 channel_ = NULL; | 84 channel_ = NULL; |
85 } | 85 } |
86 | 86 |
87 base::MessageLoopProxy* P2PSocketDispatcher::message_loop() { | 87 base::MessageLoopProxy* P2PSocketDispatcher::message_loop() { |
88 return message_loop_.get(); | 88 return message_loop_.get(); |
89 } | 89 } |
90 | 90 |
91 int P2PSocketDispatcher::RegisterClient(P2PSocketClient* client) { | 91 int P2PSocketDispatcher::RegisterClient(P2PSocketClientImpl* client) { |
92 DCHECK(message_loop_->BelongsToCurrentThread()); | 92 DCHECK(message_loop_->BelongsToCurrentThread()); |
93 return clients_.Add(client); | 93 return clients_.Add(client); |
94 } | 94 } |
95 | 95 |
96 void P2PSocketDispatcher::UnregisterClient(int id) { | 96 void P2PSocketDispatcher::UnregisterClient(int id) { |
97 DCHECK(message_loop_->BelongsToCurrentThread()); | 97 DCHECK(message_loop_->BelongsToCurrentThread()); |
98 clients_.Remove(id); | 98 clients_.Remove(id); |
99 } | 99 } |
100 | 100 |
101 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { | 101 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { |
(...skipping 30 matching lines...) Expand all Loading... |
132 if (!request) { | 132 if (!request) { |
133 VLOG(1) << "Received P2P message for socket that doesn't exist."; | 133 VLOG(1) << "Received P2P message for socket that doesn't exist."; |
134 return; | 134 return; |
135 } | 135 } |
136 | 136 |
137 request->OnResponse(address); | 137 request->OnResponse(address); |
138 } | 138 } |
139 | 139 |
140 void P2PSocketDispatcher::OnSocketCreated( | 140 void P2PSocketDispatcher::OnSocketCreated( |
141 int socket_id, const net::IPEndPoint& address) { | 141 int socket_id, const net::IPEndPoint& address) { |
142 P2PSocketClient* client = GetClient(socket_id); | 142 P2PSocketClientImpl* client = GetClient(socket_id); |
143 if (client) { | 143 if (client) { |
144 client->OnSocketCreated(address); | 144 client->OnSocketCreated(address); |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 void P2PSocketDispatcher::OnIncomingTcpConnection( | 148 void P2PSocketDispatcher::OnIncomingTcpConnection( |
149 int socket_id, const net::IPEndPoint& address) { | 149 int socket_id, const net::IPEndPoint& address) { |
150 P2PSocketClient* client = GetClient(socket_id); | 150 P2PSocketClientImpl* client = GetClient(socket_id); |
151 if (client) { | 151 if (client) { |
152 client->OnIncomingTcpConnection(address); | 152 client->OnIncomingTcpConnection(address); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 void P2PSocketDispatcher::OnSendComplete(int socket_id) { | 156 void P2PSocketDispatcher::OnSendComplete(int socket_id) { |
157 P2PSocketClient* client = GetClient(socket_id); | 157 P2PSocketClientImpl* client = GetClient(socket_id); |
158 if (client) { | 158 if (client) { |
159 client->OnSendComplete(); | 159 client->OnSendComplete(); |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 void P2PSocketDispatcher::OnError(int socket_id) { | 163 void P2PSocketDispatcher::OnError(int socket_id) { |
164 P2PSocketClient* client = GetClient(socket_id); | 164 P2PSocketClientImpl* client = GetClient(socket_id); |
165 if (client) { | 165 if (client) { |
166 client->OnError(); | 166 client->OnError(); |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 void P2PSocketDispatcher::OnDataReceived( | 170 void P2PSocketDispatcher::OnDataReceived( |
171 int socket_id, const net::IPEndPoint& address, | 171 int socket_id, const net::IPEndPoint& address, |
172 const std::vector<char>& data) { | 172 const std::vector<char>& data) { |
173 P2PSocketClient* client = GetClient(socket_id); | 173 P2PSocketClientImpl* client = GetClient(socket_id); |
174 if (client) { | 174 if (client) { |
175 client->OnDataReceived(address, data); | 175 client->OnDataReceived(address, data); |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 P2PSocketClient* P2PSocketDispatcher::GetClient(int socket_id) { | 179 P2PSocketClientImpl* P2PSocketDispatcher::GetClient(int socket_id) { |
180 P2PSocketClient* client = clients_.Lookup(socket_id); | 180 P2PSocketClientImpl* client = clients_.Lookup(socket_id); |
181 if (client == NULL) { | 181 if (client == NULL) { |
182 // This may happen if the socket was closed, but the browser side | 182 // This may happen if the socket was closed, but the browser side |
183 // hasn't processed the close message by the time it sends the | 183 // hasn't processed the close message by the time it sends the |
184 // message to the renderer. | 184 // message to the renderer. |
185 VLOG(1) << "Received P2P message for socket that doesn't exist."; | 185 VLOG(1) << "Received P2P message for socket that doesn't exist."; |
186 return NULL; | 186 return NULL; |
187 } | 187 } |
188 | 188 |
189 return client; | 189 return client; |
190 } | 190 } |
191 | 191 |
192 } // namespace content | 192 } // namespace content |
OLD | NEW |