connect_job.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2018 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. #include "net/socket/connect_job.h"
  5. #include <set>
  6. #include <utility>
  7. #include "base/trace_event/trace_event.h"
  8. #include "net/base/connection_endpoint_metadata.h"
  9. #include "net/base/net_errors.h"
  10. #include "net/base/trace_constants.h"
  11. #include "net/dns/host_resolver_results.h"
  12. #include "net/dns/public/secure_dns_policy.h"
  13. #include "net/http/http_auth_controller.h"
  14. #include "net/http/http_proxy_connect_job.h"
  15. #include "net/log/net_log.h"
  16. #include "net/log/net_log_event_type.h"
  17. #include "net/socket/client_socket_handle.h"
  18. #include "net/socket/socket_tag.h"
  19. #include "net/socket/socks_connect_job.h"
  20. #include "net/socket/ssl_connect_job.h"
  21. #include "net/socket/stream_socket.h"
  22. #include "net/socket/transport_connect_job.h"
  23. #include "net/traffic_annotation/network_traffic_annotation.h"
  24. namespace net {
  25. CommonConnectJobParams::CommonConnectJobParams(
  26. ClientSocketFactory* client_socket_factory,
  27. HostResolver* host_resolver,
  28. HttpAuthCache* http_auth_cache,
  29. HttpAuthHandlerFactory* http_auth_handler_factory,
  30. SpdySessionPool* spdy_session_pool,
  31. const quic::ParsedQuicVersionVector* quic_supported_versions,
  32. QuicStreamFactory* quic_stream_factory,
  33. ProxyDelegate* proxy_delegate,
  34. const HttpUserAgentSettings* http_user_agent_settings,
  35. SSLClientContext* ssl_client_context,
  36. SocketPerformanceWatcherFactory* socket_performance_watcher_factory,
  37. NetworkQualityEstimator* network_quality_estimator,
  38. NetLog* net_log,
  39. WebSocketEndpointLockManager* websocket_endpoint_lock_manager)
  40. : client_socket_factory(client_socket_factory),
  41. host_resolver(host_resolver),
  42. http_auth_cache(http_auth_cache),
  43. http_auth_handler_factory(http_auth_handler_factory),
  44. spdy_session_pool(spdy_session_pool),
  45. quic_supported_versions(quic_supported_versions),
  46. quic_stream_factory(quic_stream_factory),
  47. proxy_delegate(proxy_delegate),
  48. http_user_agent_settings(http_user_agent_settings),
  49. ssl_client_context(ssl_client_context),
  50. socket_performance_watcher_factory(socket_performance_watcher_factory),
  51. network_quality_estimator(network_quality_estimator),
  52. net_log(net_log),
  53. websocket_endpoint_lock_manager(websocket_endpoint_lock_manager) {}
  54. CommonConnectJobParams::CommonConnectJobParams(
  55. const CommonConnectJobParams& other) = default;
  56. CommonConnectJobParams::~CommonConnectJobParams() = default;
  57. CommonConnectJobParams& CommonConnectJobParams::operator=(
  58. const CommonConnectJobParams& other) = default;
  59. ConnectJob::ConnectJob(RequestPriority priority,
  60. const SocketTag& socket_tag,
  61. base::TimeDelta timeout_duration,
  62. const CommonConnectJobParams* common_connect_job_params,
  63. Delegate* delegate,
  64. const NetLogWithSource* net_log,
  65. NetLogSourceType net_log_source_type,
  66. NetLogEventType net_log_connect_event_type)
  67. : timeout_duration_(timeout_duration),
  68. priority_(priority),
  69. socket_tag_(socket_tag),
  70. common_connect_job_params_(common_connect_job_params),
  71. delegate_(delegate),
  72. top_level_job_(net_log == nullptr),
  73. net_log_(net_log
  74. ? *net_log
  75. : NetLogWithSource::Make(common_connect_job_params->net_log,
  76. net_log_source_type)),
  77. net_log_connect_event_type_(net_log_connect_event_type) {
  78. DCHECK(delegate);
  79. if (top_level_job_)
  80. net_log_.BeginEvent(NetLogEventType::CONNECT_JOB);
  81. }
  82. ConnectJob::~ConnectJob() {
  83. // Log end of Connect event if ConnectJob was still in-progress when
  84. // destroyed.
  85. if (delegate_)
  86. LogConnectCompletion(ERR_ABORTED);
  87. if (top_level_job_)
  88. net_log().EndEvent(NetLogEventType::CONNECT_JOB);
  89. }
  90. std::unique_ptr<StreamSocket> ConnectJob::PassSocket() {
  91. return std::move(socket_);
  92. }
  93. void ConnectJob::ChangePriority(RequestPriority priority) {
  94. priority_ = priority;
  95. ChangePriorityInternal(priority);
  96. }
  97. int ConnectJob::Connect() {
  98. if (!timeout_duration_.is_zero())
  99. timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
  100. LogConnectStart();
  101. int rv = ConnectInternal();
  102. if (rv != ERR_IO_PENDING) {
  103. LogConnectCompletion(rv);
  104. delegate_ = nullptr;
  105. }
  106. return rv;
  107. }
  108. ConnectionAttempts ConnectJob::GetConnectionAttempts() const {
  109. // Return empty list by default - used by proxy classes.
  110. return ConnectionAttempts();
  111. }
  112. bool ConnectJob::IsSSLError() const {
  113. return false;
  114. }
  115. scoped_refptr<SSLCertRequestInfo> ConnectJob::GetCertRequestInfo() {
  116. return nullptr;
  117. }
  118. void ConnectJob::set_done_closure(base::OnceClosure done_closure) {
  119. done_closure_ = base::ScopedClosureRunner(std::move(done_closure));
  120. }
  121. absl::optional<HostResolverEndpointResult>
  122. ConnectJob::GetHostResolverEndpointResult() const {
  123. return absl::nullopt;
  124. }
  125. void ConnectJob::SetSocket(std::unique_ptr<StreamSocket> socket,
  126. absl::optional<std::set<std::string>> dns_aliases) {
  127. if (socket) {
  128. net_log().AddEvent(NetLogEventType::CONNECT_JOB_SET_SOCKET);
  129. if (dns_aliases)
  130. socket->SetDnsAliases(std::move(dns_aliases.value()));
  131. }
  132. socket_ = std::move(socket);
  133. }
  134. void ConnectJob::NotifyDelegateOfCompletion(int rv) {
  135. TRACE_EVENT0(NetTracingCategory(), "ConnectJob::NotifyDelegateOfCompletion");
  136. // The delegate will own |this|.
  137. Delegate* delegate = delegate_;
  138. delegate_ = nullptr;
  139. LogConnectCompletion(rv);
  140. delegate->OnConnectJobComplete(rv, this);
  141. }
  142. void ConnectJob::NotifyDelegateOfProxyAuth(
  143. const HttpResponseInfo& response,
  144. HttpAuthController* auth_controller,
  145. base::OnceClosure restart_with_auth_callback) {
  146. delegate_->OnNeedsProxyAuth(response, auth_controller,
  147. std::move(restart_with_auth_callback), this);
  148. }
  149. void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
  150. timer_.Stop();
  151. if (!remaining_time.is_zero())
  152. timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
  153. }
  154. bool ConnectJob::TimerIsRunning() const {
  155. return timer_.IsRunning();
  156. }
  157. void ConnectJob::LogConnectStart() {
  158. connect_timing_.connect_start = base::TimeTicks::Now();
  159. net_log().BeginEvent(net_log_connect_event_type_);
  160. }
  161. void ConnectJob::LogConnectCompletion(int net_error) {
  162. connect_timing_.connect_end = base::TimeTicks::Now();
  163. net_log().EndEventWithNetErrorCode(net_log_connect_event_type_, net_error);
  164. }
  165. void ConnectJob::OnTimeout() {
  166. // Make sure the socket is NULL before calling into |delegate|.
  167. SetSocket(nullptr, absl::nullopt /* dns_aliases */);
  168. OnTimedOutInternal();
  169. net_log_.AddEvent(NetLogEventType::CONNECT_JOB_TIMED_OUT);
  170. NotifyDelegateOfCompletion(ERR_TIMED_OUT);
  171. }
  172. void ConnectJob::OnTimedOutInternal() {}
  173. } // namespace net