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