OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/socket/websocket_transport_client_socket_pool.h" | 5 #include "net/socket/websocket_transport_client_socket_pool.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "base/trace_event/trace_event.h" | 18 #include "base/trace_event/trace_event.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
21 #include "net/log/net_log.h" | 21 #include "net/log/net_log.h" |
| 22 #include "net/log/net_log_event_type.h" |
| 23 #include "net/log/net_log_source_type.h" |
22 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
23 #include "net/socket/client_socket_pool_base.h" | 25 #include "net/socket/client_socket_pool_base.h" |
24 #include "net/socket/websocket_endpoint_lock_manager.h" | 26 #include "net/socket/websocket_endpoint_lock_manager.h" |
25 #include "net/socket/websocket_transport_connect_sub_job.h" | 27 #include "net/socket/websocket_transport_connect_sub_job.h" |
26 | 28 |
27 namespace net { | 29 namespace net { |
28 | 30 |
29 WebSocketTransportConnectJob::WebSocketTransportConnectJob( | 31 WebSocketTransportConnectJob::WebSocketTransportConnectJob( |
30 const std::string& group_name, | 32 const std::string& group_name, |
31 RequestPriority priority, | 33 RequestPriority priority, |
32 ClientSocketPool::RespectLimits respect_limits, | 34 ClientSocketPool::RespectLimits respect_limits, |
33 const scoped_refptr<TransportSocketParams>& params, | 35 const scoped_refptr<TransportSocketParams>& params, |
34 base::TimeDelta timeout_duration, | 36 base::TimeDelta timeout_duration, |
35 const CompletionCallback& callback, | 37 const CompletionCallback& callback, |
36 ClientSocketFactory* client_socket_factory, | 38 ClientSocketFactory* client_socket_factory, |
37 HostResolver* host_resolver, | 39 HostResolver* host_resolver, |
38 ClientSocketHandle* handle, | 40 ClientSocketHandle* handle, |
39 Delegate* delegate, | 41 Delegate* delegate, |
40 NetLog* pool_net_log, | 42 NetLog* pool_net_log, |
41 const BoundNetLog& request_net_log) | 43 const BoundNetLog& request_net_log) |
42 : ConnectJob(group_name, | 44 : ConnectJob( |
43 timeout_duration, | 45 group_name, |
44 priority, | 46 timeout_duration, |
45 respect_limits, | 47 priority, |
46 delegate, | 48 respect_limits, |
47 BoundNetLog::Make(pool_net_log, NetLog::SOURCE_CONNECT_JOB)), | 49 delegate, |
| 50 BoundNetLog::Make(pool_net_log, NetLogSourceType::CONNECT_JOB)), |
48 params_(params), | 51 params_(params), |
49 resolver_(host_resolver), | 52 resolver_(host_resolver), |
50 client_socket_factory_(client_socket_factory), | 53 client_socket_factory_(client_socket_factory), |
51 next_state_(STATE_NONE), | 54 next_state_(STATE_NONE), |
52 race_result_(TransportConnectJob::RACE_UNKNOWN), | 55 race_result_(TransportConnectJob::RACE_UNKNOWN), |
53 handle_(handle), | 56 handle_(handle), |
54 callback_(callback), | 57 callback_(callback), |
55 request_net_log_(request_net_log), | 58 request_net_log_(request_net_log), |
56 had_ipv4_(false), | 59 had_ipv4_(false), |
57 had_ipv6_(false) {} | 60 had_ipv6_(false) {} |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 const BoundNetLog& request_net_log) { | 327 const BoundNetLog& request_net_log) { |
325 DCHECK(params); | 328 DCHECK(params); |
326 const scoped_refptr<TransportSocketParams>& casted_params = | 329 const scoped_refptr<TransportSocketParams>& casted_params = |
327 *static_cast<const scoped_refptr<TransportSocketParams>*>(params); | 330 *static_cast<const scoped_refptr<TransportSocketParams>*>(params); |
328 | 331 |
329 NetLogTcpClientSocketPoolRequestedSocket(request_net_log, &casted_params); | 332 NetLogTcpClientSocketPoolRequestedSocket(request_net_log, &casted_params); |
330 | 333 |
331 CHECK(!callback.is_null()); | 334 CHECK(!callback.is_null()); |
332 CHECK(handle); | 335 CHECK(handle); |
333 | 336 |
334 request_net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL); | 337 request_net_log.BeginEvent(NetLogEventType::SOCKET_POOL); |
335 | 338 |
336 if (ReachedMaxSocketsLimit() && | 339 if (ReachedMaxSocketsLimit() && |
337 respect_limits == ClientSocketPool::RespectLimits::ENABLED) { | 340 respect_limits == ClientSocketPool::RespectLimits::ENABLED) { |
338 request_net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS); | 341 request_net_log.AddEvent(NetLogEventType::SOCKET_POOL_STALLED_MAX_SOCKETS); |
339 // TODO(ricea): Use emplace_back when C++11 becomes allowed. | 342 // TODO(ricea): Use emplace_back when C++11 becomes allowed. |
340 StalledRequest request( | 343 StalledRequest request( |
341 casted_params, priority, handle, callback, request_net_log); | 344 casted_params, priority, handle, callback, request_net_log); |
342 stalled_request_queue_.push_back(request); | 345 stalled_request_queue_.push_back(request); |
343 StalledRequestQueue::iterator iterator = stalled_request_queue_.end(); | 346 StalledRequestQueue::iterator iterator = stalled_request_queue_.end(); |
344 --iterator; | 347 --iterator; |
345 DCHECK_EQ(handle, iterator->handle); | 348 DCHECK_EQ(handle, iterator->handle); |
346 // Because StalledRequestQueue is a std::list, its iterators are guaranteed | 349 // Because StalledRequestQueue is a std::list, its iterators are guaranteed |
347 // to remain valid as long as the elements are not removed. As long as | 350 // to remain valid as long as the elements are not removed. As long as |
348 // stalled_request_queue_ and stalled_request_map_ are updated in sync, it | 351 // stalled_request_queue_ and stalled_request_map_ are updated in sync, it |
349 // is safe to dereference an iterator in stalled_request_map_ to find the | 352 // is safe to dereference an iterator in stalled_request_map_ to find the |
350 // corresponding list element. | 353 // corresponding list element. |
351 stalled_request_map_.insert( | 354 stalled_request_map_.insert( |
352 StalledRequestMap::value_type(handle, iterator)); | 355 StalledRequestMap::value_type(handle, iterator)); |
353 return ERR_IO_PENDING; | 356 return ERR_IO_PENDING; |
354 } | 357 } |
355 | 358 |
356 std::unique_ptr<WebSocketTransportConnectJob> connect_job( | 359 std::unique_ptr<WebSocketTransportConnectJob> connect_job( |
357 new WebSocketTransportConnectJob( | 360 new WebSocketTransportConnectJob( |
358 group_name, priority, respect_limits, casted_params, | 361 group_name, priority, respect_limits, casted_params, |
359 ConnectionTimeout(), callback, client_socket_factory_, host_resolver_, | 362 ConnectionTimeout(), callback, client_socket_factory_, host_resolver_, |
360 handle, &connect_job_delegate_, pool_net_log_, request_net_log)); | 363 handle, &connect_job_delegate_, pool_net_log_, request_net_log)); |
361 | 364 |
362 int rv = connect_job->Connect(); | 365 int rv = connect_job->Connect(); |
363 // Regardless of the outcome of |connect_job|, it will always be bound to | 366 // Regardless of the outcome of |connect_job|, it will always be bound to |
364 // |handle|, since this pool uses early-binding. So the binding is logged | 367 // |handle|, since this pool uses early-binding. So the binding is logged |
365 // here, without waiting for the result. | 368 // here, without waiting for the result. |
366 request_net_log.AddEvent( | 369 request_net_log.AddEvent( |
367 NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, | 370 NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
368 connect_job->net_log().source().ToEventParametersCallback()); | 371 connect_job->net_log().source().ToEventParametersCallback()); |
369 if (rv == OK) { | 372 if (rv == OK) { |
370 HandOutSocket(connect_job->PassSocket(), | 373 HandOutSocket(connect_job->PassSocket(), |
371 connect_job->connect_timing(), | 374 connect_job->connect_timing(), |
372 handle, | 375 handle, |
373 request_net_log); | 376 request_net_log); |
374 request_net_log.EndEvent(NetLog::TYPE_SOCKET_POOL); | 377 request_net_log.EndEvent(NetLogEventType::SOCKET_POOL); |
375 } else if (rv == ERR_IO_PENDING) { | 378 } else if (rv == ERR_IO_PENDING) { |
376 // TODO(ricea): Implement backup job timer? | 379 // TODO(ricea): Implement backup job timer? |
377 AddJob(handle, std::move(connect_job)); | 380 AddJob(handle, std::move(connect_job)); |
378 } else { | 381 } else { |
379 std::unique_ptr<StreamSocket> error_socket; | 382 std::unique_ptr<StreamSocket> error_socket; |
380 connect_job->GetAdditionalErrorState(handle); | 383 connect_job->GetAdditionalErrorState(handle); |
381 error_socket = connect_job->PassSocket(); | 384 error_socket = connect_job->PassSocket(); |
382 if (error_socket) { | 385 if (error_socket) { |
383 HandOutSocket(std::move(error_socket), connect_job->connect_timing(), | 386 HandOutSocket(std::move(error_socket), connect_job->connect_timing(), |
384 handle, request_net_log); | 387 handle, request_net_log); |
385 } | 388 } |
386 } | 389 } |
387 | 390 |
388 if (rv != ERR_IO_PENDING) { | 391 if (rv != ERR_IO_PENDING) { |
389 request_net_log.EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); | 392 request_net_log.EndEventWithNetErrorCode(NetLogEventType::SOCKET_POOL, rv); |
390 } | 393 } |
391 | 394 |
392 return rv; | 395 return rv; |
393 } | 396 } |
394 | 397 |
395 void WebSocketTransportClientSocketPool::RequestSockets( | 398 void WebSocketTransportClientSocketPool::RequestSockets( |
396 const std::string& group_name, | 399 const std::string& group_name, |
397 const void* params, | 400 const void* params, |
398 int num_sockets, | 401 int num_sockets, |
399 const BoundNetLog& net_log) { | 402 const BoundNetLog& net_log) { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 CompletionCallback callback = job->callback(); | 523 CompletionCallback callback = job->callback(); |
521 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing(); | 524 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing(); |
522 | 525 |
523 ClientSocketHandle* const handle = job->handle(); | 526 ClientSocketHandle* const handle = job->handle(); |
524 bool handed_out_socket = false; | 527 bool handed_out_socket = false; |
525 | 528 |
526 if (result == OK) { | 529 if (result == OK) { |
527 DCHECK(socket.get()); | 530 DCHECK(socket.get()); |
528 handed_out_socket = true; | 531 handed_out_socket = true; |
529 HandOutSocket(std::move(socket), connect_timing, handle, request_net_log); | 532 HandOutSocket(std::move(socket), connect_timing, handle, request_net_log); |
530 request_net_log.EndEvent(NetLog::TYPE_SOCKET_POOL); | 533 request_net_log.EndEvent(NetLogEventType::SOCKET_POOL); |
531 } else { | 534 } else { |
532 // If we got a socket, it must contain error information so pass that | 535 // If we got a socket, it must contain error information so pass that |
533 // up so that the caller can retrieve it. | 536 // up so that the caller can retrieve it. |
534 job->GetAdditionalErrorState(handle); | 537 job->GetAdditionalErrorState(handle); |
535 if (socket.get()) { | 538 if (socket.get()) { |
536 handed_out_socket = true; | 539 handed_out_socket = true; |
537 HandOutSocket(std::move(socket), connect_timing, handle, request_net_log); | 540 HandOutSocket(std::move(socket), connect_timing, handle, request_net_log); |
538 } | 541 } |
539 request_net_log.EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, result); | 542 request_net_log.EndEventWithNetErrorCode(NetLogEventType::SOCKET_POOL, |
| 543 result); |
540 } | 544 } |
541 bool delete_succeeded = DeleteJob(handle); | 545 bool delete_succeeded = DeleteJob(handle); |
542 DCHECK(delete_succeeded); | 546 DCHECK(delete_succeeded); |
543 if (!handed_out_socket && !stalled_request_queue_.empty() && | 547 if (!handed_out_socket && !stalled_request_queue_.empty() && |
544 !ReachedMaxSocketsLimit()) | 548 !ReachedMaxSocketsLimit()) |
545 ActivateStalledRequest(); | 549 ActivateStalledRequest(); |
546 InvokeUserCallbackLater(handle, callback, result); | 550 InvokeUserCallbackLater(handle, callback, result); |
547 } | 551 } |
548 | 552 |
549 void WebSocketTransportClientSocketPool::InvokeUserCallbackLater( | 553 void WebSocketTransportClientSocketPool::InvokeUserCallbackLater( |
(...skipping 28 matching lines...) Expand all Loading... |
578 ClientSocketHandle* handle, | 582 ClientSocketHandle* handle, |
579 const BoundNetLog& net_log) { | 583 const BoundNetLog& net_log) { |
580 DCHECK(socket); | 584 DCHECK(socket); |
581 handle->SetSocket(std::move(socket)); | 585 handle->SetSocket(std::move(socket)); |
582 DCHECK_EQ(ClientSocketHandle::UNUSED, handle->reuse_type()); | 586 DCHECK_EQ(ClientSocketHandle::UNUSED, handle->reuse_type()); |
583 DCHECK_EQ(0, handle->idle_time().InMicroseconds()); | 587 DCHECK_EQ(0, handle->idle_time().InMicroseconds()); |
584 handle->set_pool_id(0); | 588 handle->set_pool_id(0); |
585 handle->set_connect_timing(connect_timing); | 589 handle->set_connect_timing(connect_timing); |
586 | 590 |
587 net_log.AddEvent( | 591 net_log.AddEvent( |
588 NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, | 592 NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
589 handle->socket()->NetLog().source().ToEventParametersCallback()); | 593 handle->socket()->NetLog().source().ToEventParametersCallback()); |
590 | 594 |
591 ++handed_out_socket_count_; | 595 ++handed_out_socket_count_; |
592 } | 596 } |
593 | 597 |
594 void WebSocketTransportClientSocketPool::AddJob( | 598 void WebSocketTransportClientSocketPool::AddJob( |
595 ClientSocketHandle* handle, | 599 ClientSocketHandle* handle, |
596 std::unique_ptr<WebSocketTransportConnectJob> connect_job) { | 600 std::unique_ptr<WebSocketTransportConnectJob> connect_job) { |
597 bool inserted = | 601 bool inserted = |
598 pending_connects_.insert(PendingConnectsMap::value_type( | 602 pending_connects_.insert(PendingConnectsMap::value_type( |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 handle(handle), | 684 handle(handle), |
681 callback(callback), | 685 callback(callback), |
682 net_log(net_log) {} | 686 net_log(net_log) {} |
683 | 687 |
684 WebSocketTransportClientSocketPool::StalledRequest::StalledRequest( | 688 WebSocketTransportClientSocketPool::StalledRequest::StalledRequest( |
685 const StalledRequest& other) = default; | 689 const StalledRequest& other) = default; |
686 | 690 |
687 WebSocketTransportClientSocketPool::StalledRequest::~StalledRequest() {} | 691 WebSocketTransportClientSocketPool::StalledRequest::~StalledRequest() {} |
688 | 692 |
689 } // namespace net | 693 } // namespace net |
OLD | NEW |