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 #ifndef NET_SOCKET_STREAM_ATTEMPT_H_ 6 #define NET_SOCKET_STREAM_ATTEMPT_H_ 7 8 #include <memory> 9 10 #include "base/memory/raw_ptr.h" 11 #include "base/memory/scoped_refptr.h" 12 #include "base/values.h" 13 #include "net/base/completion_once_callback.h" 14 #include "net/base/ip_endpoint.h" 15 #include "net/base/load_states.h" 16 #include "net/base/load_timing_info.h" 17 #include "net/base/net_export.h" 18 #include "net/log/net_log_event_type.h" 19 #include "net/log/net_log_with_source.h" 20 21 namespace net { 22 23 class ClientSocketFactory; 24 class HttpNetworkSession; 25 class SocketPerformanceWatcherFactory; 26 class SSLClientContext; 27 class SSLCertRequestInfo; 28 class StreamSocket; 29 class NetworkQualityEstimator; 30 class NetLog; 31 32 // Common parameters for StreamAttempt classes. 33 struct NET_EXPORT_PRIVATE StreamAttemptParams { 34 static StreamAttemptParams FromHttpNetworkSession( 35 HttpNetworkSession* session); 36 37 StreamAttemptParams( 38 ClientSocketFactory* client_socket_factory, 39 SSLClientContext* ssl_client_context, 40 SocketPerformanceWatcherFactory* socket_performance_watcher_factory, 41 NetworkQualityEstimator* network_quality_estimator, 42 NetLog* net_log); 43 44 raw_ptr<ClientSocketFactory> client_socket_factory; 45 raw_ptr<SSLClientContext> ssl_client_context; 46 raw_ptr<SocketPerformanceWatcherFactory> socket_performance_watcher_factory; 47 raw_ptr<NetworkQualityEstimator> network_quality_estimator; 48 raw_ptr<NetLog> net_log; 49 }; 50 51 // Represents a TCP or TLS connection attempt to a single IP endpoint. 52 class NET_EXPORT_PRIVATE StreamAttempt { 53 public: 54 // `params` must outlive `this`. 55 StreamAttempt(const StreamAttemptParams* params, 56 IPEndPoint ip_endpoint, 57 NetLogSourceType net_log_source_type, 58 NetLogEventType net_log_attempt_event_type, 59 const NetLogWithSource* net_log = nullptr); 60 61 StreamAttempt(const StreamAttempt&) = delete; 62 StreamAttempt& operator=(const StreamAttempt&) = delete; 63 64 virtual ~StreamAttempt(); 65 66 // Starts this connection attempt. When ERR_IO_PENDING is returned, the 67 // attempt completed synchronously and `callback` is never invoked. Otherwise, 68 // `callback` is invoked when the attempt completes. 69 int Start(CompletionOnceCallback callback); 70 71 // Returns the load state of this attempt. 72 virtual LoadState GetLoadState() const = 0; 73 74 virtual base::Value::Dict GetInfoAsValue() const = 0; 75 76 // If the attempt failed with ERR_SSL_CLIENT_AUTH_CERT_NEEDED, returns the 77 // SSLCertRequestInfo received. Otherwise, returns nullptr. 78 virtual scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo(); 79 stream_socket()80 StreamSocket* stream_socket() const { return stream_socket_.get(); } 81 82 std::unique_ptr<StreamSocket> ReleaseStreamSocket(); 83 ip_endpoint()84 const IPEndPoint& ip_endpoint() const { return ip_endpoint_; } 85 net_log()86 const NetLogWithSource& net_log() const { return net_log_; } 87 88 // Returns the connect timing information of this attempt. Should only be 89 // accessed after the attempt completed. DNS related fields are never set. connect_timing()90 const LoadTimingInfo::ConnectTiming& connect_timing() const { 91 return connect_timing_; 92 } 93 94 protected: 95 virtual int StartInternal() = 0; 96 97 // Called when `this` is started. Subclasses should implement this method 98 // to record a useful NetLog event. 99 virtual base::Value::Dict GetNetLogStartParams() = 0; 100 params()101 const StreamAttemptParams& params() { return *params_; } 102 103 void SetStreamSocket(std::unique_ptr<StreamSocket> stream_socket); 104 105 // Called by subclasses to notify the completion of this attempt. `this` may 106 // be deleted after calling this method. 107 void NotifyOfCompletion(int rv); 108 mutable_connect_timing()109 LoadTimingInfo::ConnectTiming& mutable_connect_timing() { 110 return connect_timing_; 111 } 112 113 private: 114 void LogCompletion(int rv); 115 116 const raw_ptr<const StreamAttemptParams> params_; 117 const IPEndPoint ip_endpoint_; 118 119 NetLogWithSource net_log_; 120 NetLogEventType net_log_attempt_event_type_; 121 122 // `callback_` is consumed when the attempt completes. 123 CompletionOnceCallback callback_; 124 125 std::unique_ptr<StreamSocket> stream_socket_; 126 127 LoadTimingInfo::ConnectTiming connect_timing_; 128 }; 129 130 } // namespace net 131 132 #endif // NET_SOCKET_STREAM_ATTEMPT_H_ 133