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