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/browser/renderer_host/p2p/socket_dispatcher_host.h" | 5 #include "content/browser/renderer_host/p2p/socket_dispatcher_host.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/stl_util.h" | |
11 #include "content/browser/bad_message.h" | 10 #include "content/browser/bad_message.h" |
12 #include "content/browser/renderer_host/p2p/socket_host.h" | 11 #include "content/browser/renderer_host/p2p/socket_host.h" |
13 #include "content/common/p2p_messages.h" | 12 #include "content/common/p2p_messages.h" |
14 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/resource_context.h" | 14 #include "content/public/browser/resource_context.h" |
16 #include "net/base/address_list.h" | 15 #include "net/base/address_list.h" |
17 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
18 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
19 #include "net/base/network_interfaces.h" | 18 #include "net/base/network_interfaces.h" |
20 #include "net/base/sys_addrinfo.h" | 19 #include "net/base/sys_addrinfo.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 : BrowserMessageFilter(P2PMsgStart), | 113 : BrowserMessageFilter(P2PMsgStart), |
115 resource_context_(resource_context), | 114 resource_context_(resource_context), |
116 url_context_(url_context), | 115 url_context_(url_context), |
117 monitoring_networks_(false), | 116 monitoring_networks_(false), |
118 dump_incoming_rtp_packet_(false), | 117 dump_incoming_rtp_packet_(false), |
119 dump_outgoing_rtp_packet_(false) { | 118 dump_outgoing_rtp_packet_(false) { |
120 } | 119 } |
121 | 120 |
122 void P2PSocketDispatcherHost::OnChannelClosing() { | 121 void P2PSocketDispatcherHost::OnChannelClosing() { |
123 // Since the IPC sender is gone, close pending connections. | 122 // Since the IPC sender is gone, close pending connections. |
124 base::STLDeleteContainerPairSecondPointers(sockets_.begin(), sockets_.end()); | |
125 sockets_.clear(); | 123 sockets_.clear(); |
126 | 124 |
127 base::STLDeleteContainerPointers(dns_requests_.begin(), dns_requests_.end()); | 125 base::STLDeleteContainerPointers(dns_requests_.begin(), dns_requests_.end()); |
128 dns_requests_.clear(); | 126 dns_requests_.clear(); |
129 | 127 |
130 if (monitoring_networks_) { | 128 if (monitoring_networks_) { |
131 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 129 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
132 monitoring_networks_ = false; | 130 monitoring_networks_ = false; |
133 } | 131 } |
134 } | 132 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() { | 196 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() { |
199 DCHECK(sockets_.empty()); | 197 DCHECK(sockets_.empty()); |
200 DCHECK(dns_requests_.empty()); | 198 DCHECK(dns_requests_.empty()); |
201 | 199 |
202 if (monitoring_networks_) | 200 if (monitoring_networks_) |
203 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 201 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
204 } | 202 } |
205 | 203 |
206 P2PSocketHost* P2PSocketDispatcherHost::LookupSocket(int socket_id) { | 204 P2PSocketHost* P2PSocketDispatcherHost::LookupSocket(int socket_id) { |
207 SocketsMap::iterator it = sockets_.find(socket_id); | 205 SocketsMap::iterator it = sockets_.find(socket_id); |
208 return (it == sockets_.end()) ? NULL : it->second; | 206 return (it == sockets_.end()) ? nullptr : it->second.get(); |
209 } | 207 } |
210 | 208 |
211 void P2PSocketDispatcherHost::OnStartNetworkNotifications() { | 209 void P2PSocketDispatcherHost::OnStartNetworkNotifications() { |
212 if (!monitoring_networks_) { | 210 if (!monitoring_networks_) { |
213 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 211 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
214 monitoring_networks_ = true; | 212 monitoring_networks_ = true; |
215 } | 213 } |
216 | 214 |
217 BrowserThread::PostTask( | 215 BrowserThread::PostTask( |
218 BrowserThread::FILE, FROM_HERE, base::Bind( | 216 BrowserThread::FILE, FROM_HERE, base::Bind( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 std::unique_ptr<P2PSocketHost> socket(P2PSocketHost::Create( | 255 std::unique_ptr<P2PSocketHost> socket(P2PSocketHost::Create( |
258 this, socket_id, type, url_context_.get(), &throttler_)); | 256 this, socket_id, type, url_context_.get(), &throttler_)); |
259 | 257 |
260 if (!socket) { | 258 if (!socket) { |
261 Send(new P2PMsg_OnError(socket_id)); | 259 Send(new P2PMsg_OnError(socket_id)); |
262 return; | 260 return; |
263 } | 261 } |
264 | 262 |
265 if (socket->Init(local_address, port_range.min_port, port_range.max_port, | 263 if (socket->Init(local_address, port_range.min_port, port_range.max_port, |
266 remote_address)) { | 264 remote_address)) { |
267 sockets_[socket_id] = socket.release(); | 265 sockets_[socket_id] = std::move(socket); |
268 | 266 |
269 if (dump_incoming_rtp_packet_ || dump_outgoing_rtp_packet_) { | 267 if (dump_incoming_rtp_packet_ || dump_outgoing_rtp_packet_) { |
270 sockets_[socket_id]->StartRtpDump(dump_incoming_rtp_packet_, | 268 sockets_[socket_id]->StartRtpDump(dump_incoming_rtp_packet_, |
271 dump_outgoing_rtp_packet_, | 269 dump_outgoing_rtp_packet_, |
272 packet_callback_); | 270 packet_callback_); |
273 } | 271 } |
274 } | 272 } |
275 } | 273 } |
276 | 274 |
277 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection( | 275 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection( |
278 int listen_socket_id, const net::IPEndPoint& remote_address, | 276 int listen_socket_id, const net::IPEndPoint& remote_address, |
279 int connected_socket_id) { | 277 int connected_socket_id) { |
280 P2PSocketHost* socket = LookupSocket(listen_socket_id); | 278 P2PSocketHost* socket = LookupSocket(listen_socket_id); |
281 if (!socket) { | 279 if (!socket) { |
282 LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection " | 280 LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection " |
283 "for invalid listen_socket_id."; | 281 "for invalid listen_socket_id."; |
284 return; | 282 return; |
285 } | 283 } |
286 if (LookupSocket(connected_socket_id) != NULL) { | 284 if (LookupSocket(connected_socket_id) != nullptr) { |
287 LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection " | 285 LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection " |
288 "for duplicated connected_socket_id."; | 286 "for duplicated connected_socket_id."; |
289 return; | 287 return; |
290 } | 288 } |
291 | 289 |
292 P2PSocketHost* accepted_connection = | 290 std::unique_ptr<P2PSocketHost> accepted_connection( |
293 socket->AcceptIncomingTcpConnection(remote_address, connected_socket_id); | 291 socket->AcceptIncomingTcpConnection(remote_address, connected_socket_id)); |
294 if (accepted_connection) { | 292 if (accepted_connection) { |
295 sockets_[connected_socket_id] = accepted_connection; | 293 sockets_[connected_socket_id] = std::move(accepted_connection); |
296 } | 294 } |
297 } | 295 } |
298 | 296 |
299 void P2PSocketDispatcherHost::OnSend(int socket_id, | 297 void P2PSocketDispatcherHost::OnSend(int socket_id, |
300 const net::IPEndPoint& socket_address, | 298 const net::IPEndPoint& socket_address, |
301 const std::vector<char>& data, | 299 const std::vector<char>& data, |
302 const rtc::PacketOptions& options, | 300 const rtc::PacketOptions& options, |
303 uint64_t packet_id) { | 301 uint64_t packet_id) { |
304 P2PSocketHost* socket = LookupSocket(socket_id); | 302 P2PSocketHost* socket = LookupSocket(socket_id); |
305 if (!socket) { | 303 if (!socket) { |
306 LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id."; | 304 LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id."; |
307 return; | 305 return; |
308 } | 306 } |
309 | 307 |
310 if (data.size() > kMaximumPacketSize) { | 308 if (data.size() > kMaximumPacketSize) { |
311 LOG(ERROR) << "Received P2PHostMsg_Send with a packet that is too big: " | 309 LOG(ERROR) << "Received P2PHostMsg_Send with a packet that is too big: " |
312 << data.size(); | 310 << data.size(); |
313 Send(new P2PMsg_OnError(socket_id)); | 311 Send(new P2PMsg_OnError(socket_id)); |
314 delete socket; | 312 sockets_.erase(socket_id); // deletes the socket |
315 sockets_.erase(socket_id); | |
316 return; | 313 return; |
317 } | 314 } |
318 | 315 |
319 socket->Send(socket_address, data, options, packet_id); | 316 socket->Send(socket_address, data, options, packet_id); |
320 } | 317 } |
321 | 318 |
322 void P2PSocketDispatcherHost::OnSetOption(int socket_id, | 319 void P2PSocketDispatcherHost::OnSetOption(int socket_id, |
323 P2PSocketOption option, | 320 P2PSocketOption option, |
324 int value) { | 321 int value) { |
325 P2PSocketHost* socket = LookupSocket(socket_id); | 322 P2PSocketHost* socket = LookupSocket(socket_id); |
326 if (!socket) { | 323 if (!socket) { |
327 LOG(ERROR) << "Received P2PHostMsg_SetOption for invalid socket_id."; | 324 LOG(ERROR) << "Received P2PHostMsg_SetOption for invalid socket_id."; |
328 return; | 325 return; |
329 } | 326 } |
330 | 327 |
331 socket->SetOption(option, value); | 328 socket->SetOption(option, value); |
332 } | 329 } |
333 | 330 |
334 void P2PSocketDispatcherHost::OnDestroySocket(int socket_id) { | 331 void P2PSocketDispatcherHost::OnDestroySocket(int socket_id) { |
335 SocketsMap::iterator it = sockets_.find(socket_id); | 332 SocketsMap::iterator it = sockets_.find(socket_id); |
336 if (it != sockets_.end()) { | 333 if (it != sockets_.end()) { |
337 delete it->second; | 334 sockets_.erase(it); // deletes the socket |
338 sockets_.erase(it); | |
339 } else { | 335 } else { |
340 LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id."; | 336 LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id."; |
341 } | 337 } |
342 } | 338 } |
343 | 339 |
344 void P2PSocketDispatcherHost::DoGetNetworkList() { | 340 void P2PSocketDispatcherHost::DoGetNetworkList() { |
345 net::NetworkInterfaceList list; | 341 net::NetworkInterfaceList list; |
346 if (!net::GetNetworkList(&list, net::EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { | 342 if (!net::GetNetworkList(&list, net::EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { |
347 LOG(ERROR) << "GetNetworkList failed."; | 343 LOG(ERROR) << "GetNetworkList failed."; |
348 return; | 344 return; |
(...skipping 15 matching lines...) Expand all Loading... |
364 } | 360 } |
365 | 361 |
366 net::IPAddress P2PSocketDispatcherHost::GetDefaultLocalAddress(int family) { | 362 net::IPAddress P2PSocketDispatcherHost::GetDefaultLocalAddress(int family) { |
367 DCHECK(family == AF_INET || family == AF_INET6); | 363 DCHECK(family == AF_INET || family == AF_INET6); |
368 | 364 |
369 // Creation and connection of a UDP socket might be janky. | 365 // Creation and connection of a UDP socket might be janky. |
370 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 366 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
371 | 367 |
372 std::unique_ptr<net::DatagramClientSocket> socket( | 368 std::unique_ptr<net::DatagramClientSocket> socket( |
373 net::ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket( | 369 net::ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket( |
374 net::DatagramSocket::DEFAULT_BIND, net::RandIntCallback(), NULL, | 370 net::DatagramSocket::DEFAULT_BIND, net::RandIntCallback(), nullptr, |
375 net::NetLog::Source())); | 371 net::NetLog::Source())); |
376 | 372 |
377 net::IPAddress ip_address; | 373 net::IPAddress ip_address; |
378 if (family == AF_INET) { | 374 if (family == AF_INET) { |
379 ip_address = net::IPAddress(kPublicIPv4Host); | 375 ip_address = net::IPAddress(kPublicIPv4Host); |
380 } else { | 376 } else { |
381 ip_address = net::IPAddress(kPublicIPv6Host); | 377 ip_address = net::IPAddress(kPublicIPv6Host); |
382 } | 378 } |
383 | 379 |
384 if (socket->Connect(net::IPEndPoint(ip_address, kPublicPort)) != net::OK) { | 380 if (socket->Connect(net::IPEndPoint(ip_address, kPublicPort)) != net::OK) { |
(...skipping 28 matching lines...) Expand all Loading... |
413 | 409 |
414 if (!dump_incoming_rtp_packet_ && !dump_outgoing_rtp_packet_) | 410 if (!dump_incoming_rtp_packet_ && !dump_outgoing_rtp_packet_) |
415 packet_callback_.Reset(); | 411 packet_callback_.Reset(); |
416 | 412 |
417 for (SocketsMap::iterator it = sockets_.begin(); it != sockets_.end(); ++it) | 413 for (SocketsMap::iterator it = sockets_.begin(); it != sockets_.end(); ++it) |
418 it->second->StopRtpDump(incoming, outgoing); | 414 it->second->StopRtpDump(incoming, outgoing); |
419 } | 415 } |
420 } | 416 } |
421 | 417 |
422 } // namespace content | 418 } // namespace content |
OLD | NEW |