xref: /aosp_15_r20/external/cronet/net/quic/quic_session_pool_job.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_QUIC_QUIC_SESSION_POOL_JOB_H_
6 #define NET_QUIC_QUIC_SESSION_POOL_JOB_H_
7 
8 #include "base/memory/raw_ptr.h"
9 #include "net/base/net_error_details.h"
10 #include "net/base/request_priority.h"
11 #include "net/log/net_log_with_source.h"
12 #include "net/quic/quic_session_pool.h"
13 
14 namespace net {
15 
16 // Responsible for creating a new QUIC session to the specified server, and for
17 // notifying any associated requests when complete.
18 //
19 // A single Job can be associated with any number of `QuicSessionRequest`
20 // instances, and will update all of them with its progress, calling some or
21 // all of their `OnHostResolution()` or `OnQuicSessionCreationComplete()`
22 // methods, as indicated by calls to `ExpectOnHostResolution()` and
23 // `ExpectQuicSessionCreation()`, respectively.
24 //
25 // When the session is confirmed, the job will call the pool's
26 // `ActivateSession` method before invoking the callback from `Run`.
27 //
28 // The |client_config_handle| is not actually used, but serves to keep the
29 // corresponding CryptoClientConfig alive until the Job completes.
30 class QuicSessionPool::Job {
31  public:
32   Job(QuicSessionPool* pool,
33       const QuicSessionAliasKey& key,
34       std::unique_ptr<CryptoClientConfigHandle> client_config_handle,
35       RequestPriority priority,
36       const NetLogWithSource& net_log);
37 
38   Job(const Job&) = delete;
39   Job& operator=(const Job&) = delete;
40 
41   virtual ~Job();
42 
43   // Run the job. This should be called as soon as the job is created, then any
44   // associated requests added with `AddRequest()`.
45   virtual int Run(CompletionOnceCallback callback) = 0;
46 
47   // Add a new request associated with this Job.
48   void AddRequest(QuicSessionRequest* request);
49 
50   // Remove a request associated with this job. The request must already be
51   // associated with the job.
52   void RemoveRequest(QuicSessionRequest* request);
53 
54   // Update the priority of this Job.
55   void SetPriority(RequestPriority priority);
56 
57   // Add information about the new session to `details`. Must be called after
58   // the run has completed.
59   virtual void PopulateNetErrorDetails(NetErrorDetails* details) const = 0;
60 
key()61   const QuicSessionAliasKey& key() const { return key_; }
net_log()62   const NetLogWithSource& net_log() const { return net_log_; }
requests()63   const std::set<raw_ptr<QuicSessionRequest, SetExperimental>>& requests() {
64     return requests_;
65   }
priority()66   RequestPriority priority() const { return priority_; }
pool()67   QuicSessionPool* pool() const { return pool_.get(); }
68 
69   // Associate this job with another source.
70   void AssociateWithNetLogSource(
71       const NetLogWithSource& http_stream_job_net_log) const;
72 
73  protected:
74   // Set a new `QuicSessionRequest`'s expectations about which callbacks
75   // will be invoked. This is called in `AddRequest`.
76   virtual void SetRequestExpectations(QuicSessionRequest* request) = 0;
77 
78   // Update the priority of any ongoing work in this job.
79   virtual void UpdatePriority(RequestPriority old_priority,
80                               RequestPriority new_priority);
81 
82   const raw_ptr<QuicSessionPool> pool_;
83   const QuicSessionAliasKey key_;
84   const std::unique_ptr<CryptoClientConfigHandle> client_config_handle_;
85   RequestPriority priority_;
86   const NetLogWithSource net_log_;
87   std::set<raw_ptr<QuicSessionRequest, SetExperimental>> requests_;
88 };
89 
90 }  // namespace net
91 
92 #endif  // NET_QUIC_QUIC_SESSION_POOL_JOB_H_
93