Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 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_HTTP_HTTP_STREAM_POOL_JOB_H_ |
| 6 | #define NET_HTTP_HTTP_STREAM_POOL_JOB_H_ |
| 7 | |
| 8 | #include <memory> |
Kenichi Ishibashi | 7a7b7a41 | 2025-08-05 08:00:44 | [diff] [blame] | 9 | #include <vector> |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 10 | |
| 11 | #include "base/memory/raw_ptr.h" |
Kenichi Ishibashi | 3ac361a | 2025-08-05 11:50:25 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
Kenichi Ishibashi | e1486c8 | 2025-08-05 09:27:01 | [diff] [blame] | 13 | #include "base/time/time.h" |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 14 | #include "net/base/net_error_details.h" |
| 15 | #include "net/base/net_export.h" |
| 16 | #include "net/dns/public/resolve_error_info.h" |
| 17 | #include "net/http/http_stream_pool.h" |
| 18 | #include "net/socket/connection_attempts.h" |
| 19 | #include "net/socket/next_proto.h" |
| 20 | #include "net/socket/stream_socket.h" |
| 21 | #include "net/ssl/ssl_info.h" |
Kenichi Ishibashi | 7a7b7a41 | 2025-08-05 08:00:44 | [diff] [blame] | 22 | #include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h" |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 23 | |
| 24 | namespace net { |
| 25 | |
| 26 | class HttpStream; |
| 27 | class SSLCertRequestInfo; |
Kenichi Ishibashi | 7a7b7a41 | 2025-08-05 08:00:44 | [diff] [blame] | 28 | class NetLogWithSource; |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 29 | struct NetErrorDetails; |
| 30 | |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 31 | // Used by a `Delegate` to handle a stream request or a preconnect for a |
| 32 | // destination. The destination could be the origin or alternative services. |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 33 | class HttpStreamPool::Job { |
| 34 | public: |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 35 | // Interface to report Job's results. JobController is the only implementation |
| 36 | // of this interface other than tests. We abstract the interface to avoid a |
| 37 | // circular dependency. |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 38 | class NET_EXPORT_PRIVATE Delegate { |
| 39 | public: |
| 40 | virtual ~Delegate() = default; |
| 41 | |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 42 | // Returns the priority of the job. |
| 43 | virtual RequestPriority priority() const = 0; |
| 44 | |
| 45 | // Returns whether the limits should be respected. |
| 46 | virtual RespectLimits respect_limits() const = 0; |
| 47 | |
| 48 | // Returns allowed bad certificates. |
| 49 | virtual const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs() |
| 50 | const = 0; |
| 51 | |
| 52 | // True when IP-based pooling is enabled. |
Kenichi Ishibashi | be5f26f | 2025-08-05 00:48:04 | [diff] [blame^] | 53 | virtual bool enable_ip_based_pooling_for_h2() const = 0; |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 54 | |
| 55 | // True when alternative services is enabled. |
| 56 | virtual bool enable_alternative_services() const = 0; |
| 57 | |
Kenichi Ishibashi | 0c22fa15 | 2025-08-05 10:47:07 | [diff] [blame] | 58 | // Returns the set of ALPNs that are allowed for this job. |
| 59 | virtual NextProtoSet allowed_alpns() const = 0; |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 60 | |
| 61 | // Returns the proxy info. |
| 62 | virtual const ProxyInfo& proxy_info() const = 0; |
| 63 | |
Kenichi Ishibashi | 4b517e7 | 2025-08-05 20:05:22 | [diff] [blame] | 64 | virtual const NetLogWithSource& net_log() const = 0; |
| 65 | |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 66 | // Callback methods: Only one of these methods will be called. |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 67 | // Called when a stream is ready. |
| 68 | virtual void OnStreamReady(Job* job, |
| 69 | std::unique_ptr<HttpStream> stream, |
| 70 | NextProto negotiated_protocol) = 0; |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 71 | // Called when stream attempts failed. |
| 72 | virtual void OnStreamFailed(Job* job, |
| 73 | int status, |
| 74 | const NetErrorDetails& net_error_details, |
| 75 | ResolveErrorInfo resolve_error_info) = 0; |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 76 | // Called when a stream attempt has failed due to a certificate error. |
| 77 | virtual void OnCertificateError(Job* job, |
| 78 | int status, |
| 79 | const SSLInfo& ssl_info) = 0; |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 80 | // Called when a stream attempt has requested a client certificate. |
| 81 | virtual void OnNeedsClientAuth(Job* job, SSLCertRequestInfo* cert_info) = 0; |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 82 | |
| 83 | // Called when the preconnect has completed. |
| 84 | virtual void OnPreconnectComplete(Job* job, int status) = 0; |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 85 | }; |
| 86 | |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 87 | // `delegate` must outlive `this`. For a stream request, `num_streams` must |
| 88 | // not be specified. For a preconnect, `num_streams` must be specified. |
Kenichi Ishibashi | 7a7b7a41 | 2025-08-05 08:00:44 | [diff] [blame] | 89 | Job(Delegate* delegate, |
Kenichi Ishibashi | fc26240 | 2025-08-05 06:33:02 | [diff] [blame] | 90 | JobType type, |
Kenichi Ishibashi | cd7f174a | 2025-08-05 08:29:43 | [diff] [blame] | 91 | Group* group, |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 92 | quic::ParsedQuicVersion quic_version, |
Kenichi Ishibashi | 6e3f2ecc | 2025-08-05 10:35:10 | [diff] [blame] | 93 | NextProto expected_protocol, |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 94 | const NetLogWithSource& request_net_log, |
| 95 | size_t num_streams = 0); |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 96 | |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 97 | Job& operator=(const Job&) = delete; |
| 98 | |
| 99 | ~Job(); |
| 100 | |
Kenichi Ishibashi | 7a7b7a41 | 2025-08-05 08:00:44 | [diff] [blame] | 101 | // Starts this job. |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 102 | void Start(); |
Kenichi Ishibashi | 7a7b7a41 | 2025-08-05 08:00:44 | [diff] [blame] | 103 | |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 104 | // Returns the LoadState of this job. |
| 105 | LoadState GetLoadState() const; |
| 106 | |
| 107 | // Called when the priority of this job changes. |
| 108 | void SetPriority(RequestPriority priority); |
| 109 | |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 110 | // Add connection attempts to the job. |
| 111 | void AddConnectionAttempts(const ConnectionAttempts& attempts); |
| 112 | |
| 113 | // Called by the associated AttemptManager when a stream is ready. |
| 114 | void OnStreamReady(std::unique_ptr<HttpStream> stream, |
| 115 | NextProto negotiated_protocol); |
| 116 | |
| 117 | // Called by the associated AttemptManager when stream attempts failed. |
| 118 | void OnStreamFailed(int rv, |
| 119 | const NetErrorDetails& net_error_details, |
| 120 | ResolveErrorInfo resolve_error_info); |
| 121 | |
| 122 | // Called by the associated AttemptManager when an stream attempt has failed |
| 123 | // due to a certificate error. |
| 124 | void OnCertificateError(int status, const SSLInfo& ssl_info); |
| 125 | |
| 126 | // Called by the associated AttemptManager when an stream attempt has |
| 127 | // requested a client certificate. |
| 128 | void OnNeedsClientAuth(SSLCertRequestInfo* cert_info); |
| 129 | |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 130 | // Called by the associated AttemptManager when the preconnect completed. |
| 131 | void OnPreconnectComplete(int status); |
| 132 | |
Kenichi Ishibashi | 2ef8936 | 2025-08-05 06:12:11 | [diff] [blame] | 133 | // Helper method to call OnPreconnectComplete asynchronously. Used to avoid |
| 134 | // a dangling pointer since calling `delegate_->OnPreconnectComplete()` |
| 135 | // deletes `this` synchronously. |
| 136 | void CallOnPreconnectCompleteLater(int status); |
| 137 | |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 138 | RequestPriority priority() const { return delegate_->priority(); } |
Kenichi Ishibashi | f1343756 | 2025-08-05 11:16:39 | [diff] [blame] | 139 | |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 140 | RespectLimits respect_limits() const { return delegate_->respect_limits(); } |
Kenichi Ishibashi | d1939d0 | 2025-08-05 21:02:48 | [diff] [blame] | 141 | |
Kenichi Ishibashi | be5f26f | 2025-08-05 00:48:04 | [diff] [blame^] | 142 | bool enable_ip_based_pooling_for_h2() const { |
| 143 | return delegate_->enable_ip_based_pooling_for_h2(); |
Kenichi Ishibashi | d1939d0 | 2025-08-05 21:02:48 | [diff] [blame] | 144 | } |
| 145 | |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 146 | bool enable_alternative_services() const { |
| 147 | return delegate_->enable_alternative_services(); |
| 148 | } |
| 149 | |
| 150 | const ProxyInfo& proxy_info() const { return delegate_->proxy_info(); } |
Kenichi Ishibashi | f9856f4 | 2025-08-05 09:38:31 | [diff] [blame] | 151 | |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 152 | const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs() const { |
| 153 | return delegate_->allowed_bad_certs(); |
| 154 | } |
| 155 | |
| 156 | const NetLogWithSource& delegate_net_log() const { |
| 157 | return delegate_->net_log(); |
| 158 | } |
| 159 | |
Kenichi Ishibashi | 4b517e7 | 2025-08-05 20:05:22 | [diff] [blame] | 160 | const NetLogWithSource& net_log() const { return job_net_log_; } |
| 161 | |
Kenichi Ishibashi | 5423f3f | 2025-08-05 03:06:39 | [diff] [blame] | 162 | const NetLogWithSource& request_net_log() const { return request_net_log_; } |
| 163 | |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 164 | quic::ParsedQuicVersion quic_version() const { return quic_version_; } |
| 165 | |
Kenichi Ishibashi | 6feb68d | 2025-08-05 01:57:29 | [diff] [blame] | 166 | const NextProtoSet& allowed_alpns() const { return allowed_alpns_; } |
| 167 | |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 168 | size_t num_streams() const { return num_streams_; } |
| 169 | |
Kenichi Ishibashi | fc26240 | 2025-08-05 06:33:02 | [diff] [blame] | 170 | JobType type() const { return type_; } |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 171 | |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 172 | const ConnectionAttempts& connection_attempts() const { |
| 173 | return connection_attempts_; |
| 174 | } |
| 175 | |
Kenichi Ishibashi | e1486c8 | 2025-08-05 09:27:01 | [diff] [blame] | 176 | base::TimeTicks create_time() const { return create_time_; } |
| 177 | |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 178 | private: |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 179 | const raw_ptr<Delegate> delegate_; |
Kenichi Ishibashi | fc26240 | 2025-08-05 06:33:02 | [diff] [blame] | 180 | const JobType type_; |
Kenichi Ishibashi | b3b94888 | 2025-08-05 07:32:52 | [diff] [blame] | 181 | raw_ptr<AttemptManager> attempt_manager_; |
| 182 | |
Kenichi Ishibashi | 8527116b | 2025-08-05 00:42:14 | [diff] [blame] | 183 | const quic::ParsedQuicVersion quic_version_; |
Kenichi Ishibashi | 4886fe767 | 2025-08-05 11:16:47 | [diff] [blame] | 184 | const NextProtoSet allowed_alpns_; |
Kenichi Ishibashi | 4b517e7 | 2025-08-05 20:05:22 | [diff] [blame] | 185 | const NetLogWithSource request_net_log_; |
| 186 | const NetLogWithSource job_net_log_; |
Kenichi Ishibashi | f2c0091 | 2025-08-05 05:22:24 | [diff] [blame] | 187 | const size_t num_streams_; |
Kenichi Ishibashi | e1486c8 | 2025-08-05 09:27:01 | [diff] [blame] | 188 | const base::TimeTicks create_time_; |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 189 | |
Kenichi Ishibashi | b5c00a8 | 2025-08-05 07:21:33 | [diff] [blame] | 190 | std::optional<int> result_; |
Kenichi Ishibashi | 9306d533 | 2025-08-05 06:59:08 | [diff] [blame] | 191 | std::optional<NextProto> negotiated_protocol_; |
Kenichi Ishibashi | b5c00a8 | 2025-08-05 07:21:33 | [diff] [blame] | 192 | |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 193 | ConnectionAttempts connection_attempts_; |
Kenichi Ishibashi | 3ac361a | 2025-08-05 11:50:25 | [diff] [blame] | 194 | |
| 195 | base::WeakPtrFactory<Job> weak_ptr_factory_{this}; |
Kenichi Ishibashi | fb675ae | 2025-08-05 07:21:33 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // namespace net |
| 199 | |
| 200 | #endif // NET_HTTP_HTTP_STREAM_POOL_JOB_H_ |