xref: /aosp_15_r20/external/cronet/net/quic/quic_session_pool_session_attempt.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 #include <set>
6 #include <string>
7 
8 #include "base/memory/raw_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/time/time.h"
11 #include "net/base/completion_once_callback.h"
12 #include "net/base/connection_endpoint_metadata.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/network_handle.h"
15 #include "net/quic/quic_chromium_client_session.h"
16 #include "net/quic/quic_session_pool.h"
17 #include "net/quic/quic_session_pool_job.h"
18 #include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"
19 
20 #ifndef NET_QUIC_QUIC_SESSION_POOL_SESSION_ATTEMPT_H_
21 #define NET_QUIC_QUIC_SESSION_POOL_SESSION_ATTEMPT_H_
22 
23 namespace net {
24 
25 // Handles a single attempt to create a new QUIC session for an endpoint.
26 // On success, the new session is activated unless another session has been
27 // activated for the same endpoint. When failed on the default network, it may
28 // retry on an alternate network if the system supports non-default networks.
29 class QuicSessionPool::SessionAttempt {
30  public:
31   // Create a SessionAttempt for a direct connection.
32   SessionAttempt(Job* job,
33                  IPEndPoint ip_endpoint,
34                  ConnectionEndpointMetadata metadata,
35                  quic::ParsedQuicVersion quic_version,
36                  int cert_verify_flags,
37                  base::TimeTicks dns_resolution_start_time,
38                  base::TimeTicks dns_resolution_end_time,
39                  bool retry_on_alternate_network_before_handshake,
40                  bool use_dns_aliases,
41                  std::set<std::string> dns_aliases);
42   // Create a SessionAttempt for a connection proxied over the given stream.
43   SessionAttempt(Job* job,
44                  IPEndPoint local_endpoint,
45                  IPEndPoint proxy_peer_endpoint,
46                  quic::ParsedQuicVersion quic_version,
47                  int cert_verify_flags,
48                  std::unique_ptr<QuicChromiumClientStream::Handle> proxy_stream,
49                  const HttpUserAgentSettings* http_user_agent_settings);
50 
51   ~SessionAttempt();
52 
53   SessionAttempt(const SessionAttempt&) = delete;
54   SessionAttempt& operator=(const SessionAttempt&) = delete;
55 
56   int Start(CompletionOnceCallback callback);
57 
session_creation_finished()58   bool session_creation_finished() const { return session_creation_finished_; }
59 
session()60   QuicChromiumClientSession* session() const { return session_.get(); }
61 
62  private:
63   enum class State {
64     kNone,
65     kCreateSession,
66     kCreateSessionComplete,
67     kCryptoConnect,
68     kConfirmConnection,
69   };
70 
pool()71   QuicSessionPool* pool() { return job_->pool(); }
key()72   const QuicSessionAliasKey& key() { return job_->key(); }
net_log()73   const NetLogWithSource& net_log() { return job_->net_log(); }
74 
75   int DoLoop(int rv);
76 
77   int DoCreateSession();
78   int DoCreateSessionComplete(int rv);
79   int DoCryptoConnect(int rv);
80   int DoConfirmConnection(int rv);
81 
82   void OnCreateSessionComplete(int rv);
83   void OnCryptoConnectComplete(int rv);
84 
85   const raw_ptr<Job> job_;
86 
87   const IPEndPoint ip_endpoint_;
88   const ConnectionEndpointMetadata metadata_;
89   const quic::ParsedQuicVersion quic_version_;
90   const int cert_verify_flags_;
91   const base::TimeTicks dns_resolution_start_time_;
92   const base::TimeTicks dns_resolution_end_time_;
93   const bool was_alternative_service_recently_broken_;
94   const bool retry_on_alternate_network_before_handshake_;
95   const bool use_dns_aliases_;
96   std::set<std::string> dns_aliases_;
97 
98   // Fields only used for session attempts to a proxy.
99   std::unique_ptr<QuicChromiumClientStream::Handle> proxy_stream_;
100   const raw_ptr<const HttpUserAgentSettings> http_user_agent_settings_;
101   const IPEndPoint local_endpoint_;
102 
103   State next_state_ = State::kNone;
104   bool in_loop_ = false;
105 
106   raw_ptr<QuicChromiumClientSession> session_ = nullptr;
107   bool session_creation_finished_ = false;
108   bool connection_retried_ = false;
109 
110   base::TimeTicks quic_connection_start_time_;
111 
112   // If connection migraiton is supported, |network_| denotes the network on
113   // which |session_| is created.
114   handles::NetworkHandle network_ = handles::kInvalidNetworkHandle;
115 
116   CompletionOnceCallback callback_;
117 
118   base::WeakPtrFactory<SessionAttempt> weak_ptr_factory_{this};
119 };
120 
121 }  // namespace net
122 
123 #endif  // NET_QUIC_QUIC_SESSION_POOL_SESSION_ATTEMPT_H_
124