1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/socket/stream_attempt.h"
6
7 #include <memory>
8
9 #include "net/base/completion_once_callback.h"
10 #include "net/base/ip_endpoint.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_network_session.h"
13 #include "net/log/net_log.h"
14 #include "net/log/net_log_with_source.h"
15 #include "net/socket/stream_socket.h"
16 #include "net/ssl/ssl_cert_request_info.h"
17
18 namespace net {
19
20 // static
FromHttpNetworkSession(HttpNetworkSession * session)21 StreamAttemptParams StreamAttemptParams::FromHttpNetworkSession(
22 HttpNetworkSession* session) {
23 return StreamAttemptParams(
24 session->context().client_socket_factory, session->ssl_client_context(),
25 session->context().socket_performance_watcher_factory,
26 session->context().network_quality_estimator, session->net_log());
27 }
28
StreamAttemptParams(ClientSocketFactory * client_socket_factory,SSLClientContext * ssl_client_context,SocketPerformanceWatcherFactory * socket_performance_watcher_factory,NetworkQualityEstimator * network_quality_estimator,NetLog * net_log)29 StreamAttemptParams::StreamAttemptParams(
30 ClientSocketFactory* client_socket_factory,
31 SSLClientContext* ssl_client_context,
32 SocketPerformanceWatcherFactory* socket_performance_watcher_factory,
33 NetworkQualityEstimator* network_quality_estimator,
34 NetLog* net_log)
35 : client_socket_factory(client_socket_factory),
36 ssl_client_context(ssl_client_context),
37 socket_performance_watcher_factory(socket_performance_watcher_factory),
38 network_quality_estimator(network_quality_estimator),
39 net_log(net_log) {}
40
StreamAttempt(const StreamAttemptParams * params,IPEndPoint ip_endpoint,NetLogSourceType net_log_source_type,NetLogEventType net_log_attempt_event_type,const NetLogWithSource * net_log)41 StreamAttempt::StreamAttempt(const StreamAttemptParams* params,
42 IPEndPoint ip_endpoint,
43 NetLogSourceType net_log_source_type,
44 NetLogEventType net_log_attempt_event_type,
45 const NetLogWithSource* net_log)
46 : params_(params),
47 ip_endpoint_(ip_endpoint),
48 net_log_(net_log ? *net_log
49 : NetLogWithSource::Make(params->net_log,
50 net_log_source_type)),
51 net_log_attempt_event_type_(net_log_attempt_event_type) {}
52
~StreamAttempt()53 StreamAttempt::~StreamAttempt() {
54 // Log this attempt as aborted if the attempt was still in-progress when
55 // destroyed.
56 if (callback_) {
57 LogCompletion(ERR_ABORTED);
58 }
59 }
60
Start(CompletionOnceCallback callback)61 int StreamAttempt::Start(CompletionOnceCallback callback) {
62 net_log().BeginEvent(net_log_attempt_event_type_,
63 [&] { return GetNetLogStartParams(); });
64
65 int rv = StartInternal();
66 if (rv != ERR_IO_PENDING) {
67 LogCompletion(rv);
68 } else {
69 callback_ = std::move(callback);
70 }
71 return rv;
72 }
73
ReleaseStreamSocket()74 std::unique_ptr<StreamSocket> StreamAttempt::ReleaseStreamSocket() {
75 return std::move(stream_socket_);
76 }
77
GetCertRequestInfo()78 scoped_refptr<SSLCertRequestInfo> StreamAttempt::GetCertRequestInfo() {
79 return nullptr;
80 }
81
SetStreamSocket(std::unique_ptr<StreamSocket> socket)82 void StreamAttempt::SetStreamSocket(std::unique_ptr<StreamSocket> socket) {
83 stream_socket_ = std::move(socket);
84 }
85
NotifyOfCompletion(int rv)86 void StreamAttempt::NotifyOfCompletion(int rv) {
87 CHECK(callback_);
88
89 LogCompletion(rv);
90 std::move(callback_).Run(rv);
91 // `this` may be deleted.
92 }
93
LogCompletion(int rv)94 void StreamAttempt::LogCompletion(int rv) {
95 connect_timing_.connect_end = base::TimeTicks::Now();
96 net_log().EndEventWithNetErrorCode(net_log_attempt_event_type_, rv);
97 }
98
99 } // namespace net
100