• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
9 #include <vector>
10 
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "net/base/net_error_details.h"
14 #include "net/base/net_export.h"
15 #include "net/dns/public/resolve_error_info.h"
16 #include "net/http/http_stream_pool.h"
17 #include "net/socket/connection_attempts.h"
18 #include "net/socket/next_proto.h"
19 #include "net/socket/stream_socket.h"
20 #include "net/ssl/ssl_info.h"
21 #include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"
22 
23 namespace net {
24 
25 class HttpStream;
26 class SSLCertRequestInfo;
27 class NetLogWithSource;
28 struct NetErrorDetails;
29 
30 // Used by a `Delegate` to handle a stream request for a destination. The
31 // destination could be the origin or alternative services.
32 class HttpStreamPool::Job {
33  public:
34   // Interface to report Job's results. JobController is the only implementation
35   // of this interface other than tests. We abstract the interface to avoid a
36   // circular dependency.
37   class NET_EXPORT_PRIVATE Delegate {
38    public:
39     virtual ~Delegate() = default;
40 
41     // Returns the priority of the job.
42     virtual RequestPriority priority() const = 0;
43 
44     // Returns whether the limits should be respected.
45     virtual RespectLimits respect_limits() const = 0;
46 
47     // Returns allowed bad certificates.
48     virtual const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs()
49         const = 0;
50 
51     // True when IP-based pooling is enabled.
52     virtual bool enable_ip_based_pooling() const = 0;
53 
54     // True when alternative services is enabled.
55     virtual bool enable_alternative_services() const = 0;
56 
57     // True when HTTP/1.1 is allowed.
58     virtual bool is_http1_allowed() const = 0;
59 
60     // Returns the proxy info.
61     virtual const ProxyInfo& proxy_info() const = 0;
62 
63     // Callback methods: Only one of these methods will be called.
64     // Called when a stream is ready.
65     virtual void OnStreamReady(Job* job,
66                                std::unique_ptr<HttpStream> stream,
67                                NextProto negotiated_protocol) = 0;
68     // Called when stream attempts failed.
69     virtual void OnStreamFailed(Job* job,
70                                 int status,
71                                 const NetErrorDetails& net_error_details,
72                                 ResolveErrorInfo resolve_error_info) = 0;
73     // Called when a stream attempt has failed due to a certificate error.
74     virtual void OnCertificateError(Job* job,
75                                     int status,
76                                     const SSLInfo& ssl_info) = 0;
77     // Called when a stream attempt has requested a client certificate.
78     virtual void OnNeedsClientAuth(Job* job, SSLCertRequestInfo* cert_info) = 0;
79   };
80 
81   // `delegate` must outlive `this`.
82   Job(Delegate* delegate,
83       AttemptManager* attempt_manager,
84       quic::ParsedQuicVersion quic_version,
85       NextProto expected_protocol,
86       const NetLogWithSource& net_log);
87 
88   Job& operator=(const Job&) = delete;
89 
90   ~Job();
91 
92   // Starts this job.
93   void Start();
94 
95   // Returns the LoadState of this job.
96   LoadState GetLoadState() const;
97 
98   // Called when the priority of this job changes.
99   void SetPriority(RequestPriority priority);
100 
101   // Add connection attempts to the job.
102   void AddConnectionAttempts(const ConnectionAttempts& attempts);
103 
104   // Called by the associated AttemptManager when a stream is ready.
105   void OnStreamReady(std::unique_ptr<HttpStream> stream,
106                      NextProto negotiated_protocol);
107 
108   // Called by the associated AttemptManager when stream attempts failed.
109   void OnStreamFailed(int rv,
110                       const NetErrorDetails& net_error_details,
111                       ResolveErrorInfo resolve_error_info);
112 
113   // Called by the associated AttemptManager when an stream attempt has failed
114   // due to a certificate error.
115   void OnCertificateError(int status, const SSLInfo& ssl_info);
116 
117   // Called by the associated AttemptManager when an stream attempt has
118   // requested a client certificate.
119   void OnNeedsClientAuth(SSLCertRequestInfo* cert_info);
120 
priority()121   RequestPriority priority() const { return delegate_->priority(); }
122 
respect_limits()123   RespectLimits respect_limits() const { return delegate_->respect_limits(); }
124 
enable_ip_based_pooling()125   bool enable_ip_based_pooling() const {
126     return delegate_->enable_ip_based_pooling();
127   }
128 
enable_alternative_services()129   bool enable_alternative_services() const {
130     return delegate_->enable_alternative_services();
131   }
132 
proxy_info()133   const ProxyInfo& proxy_info() const { return delegate_->proxy_info(); }
134 
allowed_alpns()135   const NextProtoSet& allowed_alpns() const { return allowed_alpns_; }
136 
connection_attempts()137   const ConnectionAttempts& connection_attempts() const {
138     return connection_attempts_;
139   }
140 
141  private:
142   const raw_ptr<Delegate> delegate_;
143   raw_ptr<AttemptManager> attempt_manager_;
144   const quic::ParsedQuicVersion quic_version_;
145   const NextProtoSet allowed_alpns_;
146   const NetLogWithSource net_log_;
147 
148   ConnectionAttempts connection_attempts_;
149 
150   base::WeakPtrFactory<Job> weak_ptr_factory_{this};
151 };
152 
153 }  // namespace net
154 
155 #endif  // NET_HTTP_HTTP_STREAM_POOL_JOB_H_
156