xref: /aosp_15_r20/external/cronet/net/spdy/spdy_session.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_SPDY_SPDY_SESSION_H_
6 #define NET_SPDY_SPDY_SESSION_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <map>
12 #include <memory>
13 #include <optional>
14 #include <set>
15 #include <string>
16 #include <string_view>
17 #include <vector>
18 
19 #include "base/containers/circular_deque.h"
20 #include "base/feature_list.h"
21 #include "base/gtest_prod_util.h"
22 #include "base/memory/raw_ptr.h"
23 #include "base/memory/ref_counted.h"
24 #include "base/memory/scoped_refptr.h"
25 #include "base/memory/weak_ptr.h"
26 #include "base/metrics/field_trial_params.h"
27 #include "base/time/time.h"
28 #include "base/timer/timer.h"
29 #include "base/values.h"
30 #include "net/base/completion_once_callback.h"
31 #include "net/base/host_port_pair.h"
32 #include "net/base/io_buffer.h"
33 #include "net/base/load_states.h"
34 #include "net/base/load_timing_info.h"
35 #include "net/base/net_errors.h"
36 #include "net/base/net_export.h"
37 #include "net/base/network_change_notifier.h"
38 #include "net/base/request_priority.h"
39 #include "net/log/net_log_source.h"
40 #include "net/socket/client_socket_pool.h"
41 #include "net/socket/next_proto.h"
42 #include "net/socket/ssl_client_socket.h"
43 #include "net/socket/stream_socket.h"
44 #include "net/spdy/buffered_spdy_framer.h"
45 #include "net/spdy/http2_priority_dependencies.h"
46 #include "net/spdy/multiplexed_session.h"
47 #include "net/spdy/spdy_buffer.h"
48 #include "net/spdy/spdy_session_pool.h"
49 #include "net/spdy/spdy_stream.h"
50 #include "net/spdy/spdy_write_queue.h"
51 #include "net/ssl/ssl_config_service.h"
52 #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
53 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_alt_svc_wire_format.h"
54 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_framer.h"
55 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
56 #include "net/traffic_annotation/network_traffic_annotation.h"
57 #include "url/gurl.h"
58 #include "url/origin.h"
59 #include "url/scheme_host_port.h"
60 
61 namespace net {
62 
63 namespace test {
64 class SpdyStreamTest;
65 }
66 
67 // TLS and other layers will chunk data at 16KB. Making the max frame size too
68 // small will lead to increased CPU/byte cost and overhead on both client/server
69 // due to excessive frames to process. Making this larger has diminishing
70 // returns as the data will be chunked elsewhere. We also want to ensure we are
71 // >= 2860B (~2* MSS => 2 packets) to avoid delayed ACKs. We will also account
72 // for the frame header size of 9B to prevent fragmentation when this is added.
73 // As a result we will use a 16KB - 9B max data frame size.
74 const int kMaxSpdyFrameChunkSize = (16 * 1024) - 9;
75 
76 // Default value of spdy::SETTINGS_INITIAL_WINDOW_SIZE per protocol
77 // specification. A session is always created with this initial window size.
78 const int32_t kDefaultInitialWindowSize = 65535;
79 
80 // The default maximum number of concurrent streams we will create, unless the
81 // server sends a SETTINGS frame with a different value.
82 const size_t kDefaultInitialMaxConcurrentStreams = 100;
83 
84 // Used to override kDefaultInitialMaxConcurrentStreams.
85 NET_EXPORT BASE_DECLARE_FEATURE(kH2InitialMaxConcurrentStreamsOverride);
86 NET_EXPORT extern const base::FeatureParam<int> kH2InitialMaxConcurrentStreams;
87 
88 // If more than this many bytes have been read or more than that many
89 // milliseconds have passed, return ERR_IO_PENDING from ReadLoop.
90 const int kYieldAfterBytesRead = 32 * 1024;
91 const int kYieldAfterDurationMilliseconds = 20;
92 
93 // First and last valid stream IDs. As we always act as the client,
94 // start at 1 for the first stream id.
95 const spdy::SpdyStreamId kFirstStreamId = 1;
96 const spdy::SpdyStreamId kLastStreamId = 0x7fffffff;
97 
98 // Maximum number of capped frames that can be queued at any time.
99 // We measured how many queued capped frames were ever in the
100 // SpdyWriteQueue at one given time between 2019-08 and 2020-02.
101 // The numbers showed that in 99.94% of cases it would always
102 // stay below 10, and that it would exceed 1000 only in
103 // 10^-8 of cases. Therefore we picked 10000 as a number that will
104 // virtually never be hit in practice, while still preventing an
105 // attacker from growing this queue unboundedly.
106 const int kSpdySessionMaxQueuedCappedFrames = 10000;
107 
108 // Default time to delay sending small receive window updates (can be
109 // configured through SetTimeToBufferSmallWindowUpdates()). Usually window
110 // updates are sent when half of the receive window has been processed by
111 // the client but in the case of a client that consumes the data slowly,
112 // this strategy alone would make servers consider the connection or stream
113 // idle.
114 constexpr base::TimeDelta kDefaultTimeToBufferSmallWindowUpdates =
115     base::Seconds(5);
116 
117 class NetLog;
118 class NetworkQualityEstimator;
119 class SpdyStream;
120 class SSLInfo;
121 class TransportSecurityState;
122 
123 // NOTE: There is an enum called SpdyProtocolErrorDetails2 (also with numeric
124 // suffixes) in tools/metrics/histograms/enums.xml. Be sure to add new values
125 // there also.
126 enum SpdyProtocolErrorDetails {
127   // http2::Http2DecoderAdapter::SpdyFramerError mappings.
128   SPDY_ERROR_NO_ERROR = 0,
129   SPDY_ERROR_INVALID_STREAM_ID = 38,
130   SPDY_ERROR_INVALID_CONTROL_FRAME = 1,
131   SPDY_ERROR_CONTROL_PAYLOAD_TOO_LARGE = 2,
132   SPDY_ERROR_DECOMPRESS_FAILURE = 5,
133   SPDY_ERROR_INVALID_PADDING = 39,
134   SPDY_ERROR_INVALID_DATA_FRAME_FLAGS = 8,
135   SPDY_ERROR_UNEXPECTED_FRAME = 31,
136   SPDY_ERROR_INTERNAL_FRAMER_ERROR = 41,
137   SPDY_ERROR_INVALID_CONTROL_FRAME_SIZE = 37,
138   SPDY_ERROR_OVERSIZED_PAYLOAD = 40,
139 
140   // HttpDecoder or HttpDecoderAdapter error.
141   SPDY_ERROR_HPACK_INDEX_VARINT_ERROR = 43,
142   SPDY_ERROR_HPACK_NAME_LENGTH_VARINT_ERROR = 44,
143   SPDY_ERROR_HPACK_VALUE_LENGTH_VARINT_ERROR = 45,
144   SPDY_ERROR_HPACK_NAME_TOO_LONG = 46,
145   SPDY_ERROR_HPACK_VALUE_TOO_LONG = 47,
146   SPDY_ERROR_HPACK_NAME_HUFFMAN_ERROR = 48,
147   SPDY_ERROR_HPACK_VALUE_HUFFMAN_ERROR = 49,
148   SPDY_ERROR_HPACK_MISSING_DYNAMIC_TABLE_SIZE_UPDATE = 50,
149   SPDY_ERROR_HPACK_INVALID_INDEX = 51,
150   SPDY_ERROR_HPACK_INVALID_NAME_INDEX = 52,
151   SPDY_ERROR_HPACK_DYNAMIC_TABLE_SIZE_UPDATE_NOT_ALLOWED = 53,
152   SPDY_ERROR_HPACK_INITIAL_DYNAMIC_TABLE_SIZE_UPDATE_IS_ABOVE_LOW_WATER_MARK =
153       54,
154   SPDY_ERROR_HPACK_DYNAMIC_TABLE_SIZE_UPDATE_IS_ABOVE_ACKNOWLEDGED_SETTING = 55,
155   SPDY_ERROR_HPACK_TRUNCATED_BLOCK = 56,
156   SPDY_ERROR_HPACK_FRAGMENT_TOO_LONG = 57,
157   SPDY_ERROR_HPACK_COMPRESSED_HEADER_SIZE_EXCEEDS_LIMIT = 58,
158   SPDY_ERROR_STOP_PROCESSING = 59,
159   // spdy::SpdyErrorCode mappings.
160   STATUS_CODE_NO_ERROR = 41,
161   STATUS_CODE_PROTOCOL_ERROR = 11,
162   STATUS_CODE_INTERNAL_ERROR = 16,
163   STATUS_CODE_FLOW_CONTROL_ERROR = 17,
164   STATUS_CODE_SETTINGS_TIMEOUT = 32,
165   STATUS_CODE_STREAM_CLOSED = 12,
166   STATUS_CODE_FRAME_SIZE_ERROR = 21,
167   STATUS_CODE_REFUSED_STREAM = 13,
168   STATUS_CODE_CANCEL = 15,
169   STATUS_CODE_COMPRESSION_ERROR = 42,
170   STATUS_CODE_CONNECT_ERROR = 33,
171   STATUS_CODE_ENHANCE_YOUR_CALM = 34,
172   STATUS_CODE_INADEQUATE_SECURITY = 35,
173   STATUS_CODE_HTTP_1_1_REQUIRED = 36,
174   // Deprecated SpdyRstStrreamStatus mappings.
175   STATUS_CODE_UNSUPPORTED_VERSION = 14,
176   STATUS_CODE_STREAM_IN_USE = 18,
177   STATUS_CODE_STREAM_ALREADY_CLOSED = 19,
178 
179   // SpdySession errors
180   PROTOCOL_ERROR_UNEXPECTED_PING = 22,
181   PROTOCOL_ERROR_RST_STREAM_FOR_NON_ACTIVE_STREAM = 23,
182   PROTOCOL_ERROR_SPDY_COMPRESSION_FAILURE = 24,
183   PROTOCOL_ERROR_REQUEST_FOR_SECURE_CONTENT_OVER_INSECURE_SESSION = 25,
184   PROTOCOL_ERROR_SYN_REPLY_NOT_RECEIVED = 26,
185   PROTOCOL_ERROR_INVALID_WINDOW_UPDATE_SIZE = 27,
186   PROTOCOL_ERROR_RECEIVE_WINDOW_VIOLATION = 28,
187 
188   // Next free value.
189   NUM_SPDY_PROTOCOL_ERROR_DETAILS = 60,
190 };
191 SpdyProtocolErrorDetails NET_EXPORT_PRIVATE MapFramerErrorToProtocolError(
192     http2::Http2DecoderAdapter::SpdyFramerError error);
193 Error NET_EXPORT_PRIVATE
194 MapFramerErrorToNetError(http2::Http2DecoderAdapter::SpdyFramerError error);
195 SpdyProtocolErrorDetails NET_EXPORT_PRIVATE
196 MapRstStreamStatusToProtocolError(spdy::SpdyErrorCode error_code);
197 spdy::SpdyErrorCode NET_EXPORT_PRIVATE MapNetErrorToGoAwayStatus(Error err);
198 
199 // If these compile asserts fail then SpdyProtocolErrorDetails needs
200 // to be updated with new values, as do the mapping functions above.
201 static_assert(28 == http2::Http2DecoderAdapter::LAST_ERROR,
202               "SpdyProtocolErrorDetails / Spdy Errors mismatch");
203 static_assert(13 == spdy::SpdyErrorCode::ERROR_CODE_MAX,
204               "SpdyProtocolErrorDetails / spdy::SpdyErrorCode mismatch");
205 
206 // A helper class used to manage a request to create a stream.
207 class NET_EXPORT_PRIVATE SpdyStreamRequest {
208  public:
209   SpdyStreamRequest();
210 
211   SpdyStreamRequest(const SpdyStreamRequest&) = delete;
212   SpdyStreamRequest& operator=(const SpdyStreamRequest&) = delete;
213 
214   // Calls CancelRequest().
215   ~SpdyStreamRequest();
216 
217   // Returns the time when ConfirmHandshake() completed, if this request had to
218   // wait for ConfirmHandshake().
confirm_handshake_end()219   base::TimeTicks confirm_handshake_end() const {
220     return confirm_handshake_end_;
221   }
222 
223   // Starts the request to create a stream. If OK is returned, then
224   // ReleaseStream() may be called. If ERR_IO_PENDING is returned,
225   // then when the stream is created, |callback| will be called, at
226   // which point ReleaseStream() may be called. Otherwise, the stream
227   // is not created, an error is returned, and ReleaseStream() may not
228   // be called.
229   //
230   // If |can_send_early| is true, this request is allowed to be sent over
231   // TLS 1.3 0RTT without confirming the handshake.
232   //
233   // If OK is returned, must not be called again without
234   // ReleaseStream() being called first. If ERR_IO_PENDING is
235   // returned, must not be called again without CancelRequest() or
236   // ReleaseStream() being called first. Otherwise, in case of an
237   // immediate error, this may be called again.
238   int StartRequest(SpdyStreamType type,
239                    const base::WeakPtr<SpdySession>& session,
240                    const GURL& url,
241                    bool can_send_early,
242                    RequestPriority priority,
243                    const SocketTag& socket_tag,
244                    const NetLogWithSource& net_log,
245                    CompletionOnceCallback callback,
246                    const NetworkTrafficAnnotationTag& traffic_annotation,
247                    bool detect_broken_connection = false,
248                    base::TimeDelta heartbeat_interval = base::Seconds(0));
249 
250   // Cancels any pending stream creation request. May be called
251   // repeatedly.
252   void CancelRequest();
253 
254   // Transfers the created stream (guaranteed to not be NULL) to the
255   // caller. Must be called at most once after StartRequest() returns
256   // OK or |callback| is called with OK. The caller must immediately
257   // set a delegate for the returned stream (except for test code).
258   base::WeakPtr<SpdyStream> ReleaseStream();
259 
260   // Changes the priority of the stream, or changes the priority of the queued
261   // request in the session.
262   void SetPriority(RequestPriority priority);
263 
traffic_annotation()264   const NetworkTrafficAnnotationTag traffic_annotation() const {
265     return NetworkTrafficAnnotationTag(traffic_annotation_);
266   }
267 
268  private:
269   friend class SpdySession;
270 
271   void OnConfirmHandshakeComplete(int rv);
272 
273   // Called by |session_| when the stream attempt has finished
274   // successfully.
275   void OnRequestCompleteSuccess(const base::WeakPtr<SpdyStream>& stream);
276 
277   // Called by |session_| when the stream attempt has finished with an
278   // error. Also called with ERR_ABORTED if |session_| is destroyed
279   // while the stream attempt is still pending.
280   void OnRequestCompleteFailure(int rv);
281 
282   // Accessors called by |session_|.
type()283   SpdyStreamType type() const { return type_; }
url()284   const GURL& url() const { return url_; }
priority()285   RequestPriority priority() const { return priority_; }
net_log()286   const NetLogWithSource& net_log() const { return net_log_; }
287 
288   void Reset();
289 
290   SpdyStreamType type_;
291   base::WeakPtr<SpdySession> session_;
292   base::WeakPtr<SpdyStream> stream_;
293   GURL url_;
294   RequestPriority priority_;
295   SocketTag socket_tag_;
296   NetLogWithSource net_log_;
297   CompletionOnceCallback callback_;
298   MutableNetworkTrafficAnnotationTag traffic_annotation_;
299   base::TimeTicks confirm_handshake_end_;
300   bool detect_broken_connection_;
301   base::TimeDelta heartbeat_interval_;
302 
303   base::WeakPtrFactory<SpdyStreamRequest> weak_ptr_factory_{this};
304 };
305 
306 class NET_EXPORT SpdySession
307     : public BufferedSpdyFramerVisitorInterface,
308       public spdy::SpdyFramerDebugVisitorInterface,
309       public MultiplexedSession,
310       public HigherLayeredPool,
311       public NetworkChangeNotifier::DefaultNetworkActiveObserver {
312  public:
313   // TODO(akalin): Use base::TickClock when it becomes available.
314   typedef base::TimeTicks (*TimeFunc)();
315 
316   // Returns true if |new_hostname| can be pooled into an existing connection to
317   // |old_hostname| associated with |ssl_info|.
318   static bool CanPool(TransportSecurityState* transport_security_state,
319                       const SSLInfo& ssl_info,
320                       const SSLConfigService& ssl_config_service,
321                       std::string_view old_hostname,
322                       std::string_view new_hostname);
323 
324   // Create a new SpdySession.
325   // |spdy_session_key| is the host/port that this session connects to, privacy
326   // and proxy configuration settings that it's using.
327   // |net_log| is the NetLog that we log network events to.
328   SpdySession(const SpdySessionKey& spdy_session_key,
329               HttpServerProperties* http_server_properties,
330               TransportSecurityState* transport_security_state,
331               SSLConfigService* ssl_config_service,
332               const quic::ParsedQuicVersionVector& quic_supported_versions,
333               bool enable_sending_initial_data,
334               bool enable_ping_based_connection_checking,
335               bool is_http_enabled,
336               bool is_quic_enabled,
337               size_t session_max_recv_window_size,
338               int session_max_queued_capped_frames,
339               const spdy::SettingsMap& initial_settings,
340               bool enable_http2_settings_grease,
341               const std::optional<SpdySessionPool::GreasedHttp2Frame>&
342                   greased_http2_frame,
343               bool http2_end_stream_with_data_frame,
344               bool enable_priority_update,
345               TimeFunc time_func,
346               NetworkQualityEstimator* network_quality_estimator,
347               NetLog* net_log);
348 
349   ~SpdySession() override;
350 
host_port_pair()351   const HostPortPair& host_port_pair() const {
352     return spdy_session_key_.host_port_proxy_pair().first;
353   }
host_port_proxy_pair()354   const HostPortProxyPair& host_port_proxy_pair() const {
355     return spdy_session_key_.host_port_proxy_pair();
356   }
spdy_session_key()357   const SpdySessionKey& spdy_session_key() const { return spdy_session_key_; }
358 
359   // Initialize the session with the given connection.
360   //
361   // |pool| is the SpdySessionPool that owns us.  Its lifetime must
362   // strictly be greater than |this|.
363   //
364   // The session begins reading from |client_socket_handle| on a subsequent
365   // event loop iteration, so the SpdySession may close immediately afterwards
366   // if the first read of |client_socket_handle| fails.
367   void InitializeWithSocketHandle(
368       std::unique_ptr<ClientSocketHandle> client_socket_handle,
369       SpdySessionPool* pool);
370 
371   // Just like InitializeWithSocketHandle(), but for use when the session is not
372   // on top of a socket pool, but instead directly on top of a socket, which the
373   // session has sole ownership of, and is responsible for deleting directly
374   // itself.
375   void InitializeWithSocket(std::unique_ptr<StreamSocket> stream_socket,
376                             const LoadTimingInfo::ConnectTiming& connect_timing,
377                             SpdySessionPool* pool);
378 
379   // Parse ALPS application_data from TLS handshake.
380   // Returns OK on success.  Return a net error code on failure, and closes the
381   // connection with the same error code.
382   int ParseAlps();
383 
384   // Check to see if this SPDY session can support an additional domain.
385   // If the session is un-authenticated, then this call always returns true.
386   // For SSL-based sessions, verifies that the server certificate in use by
387   // this session provides authentication for the domain and no client
388   // certificate or channel ID was sent to the original server during the SSL
389   // handshake.  NOTE:  This function can have false negatives on some
390   // platforms.
391   // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch
392   // histogram because this function does more than verifying domain
393   // authentication now.
394   bool VerifyDomainAuthentication(std::string_view domain) const;
395 
396   // Pushes the given producer into the write queue for
397   // |stream|. |stream| is guaranteed to be activated before the
398   // producer is used to produce its frame.
399   void EnqueueStreamWrite(const base::WeakPtr<SpdyStream>& stream,
400                           spdy::SpdyFrameType frame_type,
401                           std::unique_ptr<SpdyBufferProducer> producer);
402 
403   // Returns true if this session is configured to send greased HTTP/2 frames.
404   // For more details on greased frames, see
405   // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00.
406   bool GreasedFramesEnabled() const;
407 
408   // Returns true if HEADERS frames on request streams should not have the
409   // END_STREAM flag set, but instead an empty DATA frame with END_STREAM should
410   // be sent afterwards to close the stream.  Does not apply to bidirectional or
411   // proxy streams.
EndStreamWithDataFrame()412   bool EndStreamWithDataFrame() const {
413     return http2_end_stream_with_data_frame_;
414   }
415 
416   // Send greased frame, that is, a frame of reserved type.
417   void EnqueueGreasedFrame(const base::WeakPtr<SpdyStream>& stream);
418 
419   // Returns whether HTTP/2 style priority information (stream dependency and
420   // weight fields in HEADERS frames, and PRIORITY frames) should be sent.  True
421   // unless |enable_priority_update_| is true and
422   // SETTINGS_DEPRECATE_HTTP2_PRIORITIES with value 1 has been received from
423   // server.  In particular, if it returns false, it will always return false
424   // afterwards.
425   bool ShouldSendHttp2Priority() const;
426 
427   // Returns whether PRIORITY_UPDATE frames should be sent.  False if
428   // |enable_priority_update_| is false.  Otherwise, true before SETTINGS frame
429   // is received from server, and true after SETTINGS frame is received if it
430   // contained SETTINGS_DEPRECATE_HTTP2_PRIORITIES with value 1.  In particular,
431   // if it returns false, it will always return false afterwards.
432   bool ShouldSendPriorityUpdate() const;
433 
434   // Runs the handshake to completion to confirm the handshake with the server.
435   // If ERR_IO_PENDING is returned, then when the handshake is confirmed,
436   // |callback| will be called.
437   int ConfirmHandshake(CompletionOnceCallback callback);
438 
439   // Creates and returns a HEADERS frame for |stream_id|.
440   std::unique_ptr<spdy::SpdySerializedFrame> CreateHeaders(
441       spdy::SpdyStreamId stream_id,
442       RequestPriority priority,
443       spdy::SpdyControlFlags flags,
444       spdy::Http2HeaderBlock headers,
445       NetLogSource source_dependency);
446 
447   // Creates and returns a SpdyBuffer holding a data frame with the given data.
448   // Sets |*effective_len| to number of bytes sent, and |*end_stream| to the
449   // value of the END_STREAM (also known as fin) flag.  Returns nullptr if
450   // session is draining or if session or stream is stalled by flow control.
451   std::unique_ptr<SpdyBuffer> CreateDataBuffer(spdy::SpdyStreamId stream_id,
452                                                IOBuffer* data,
453                                                int len,
454                                                spdy::SpdyDataFlags flags,
455                                                int* effective_len,
456                                                bool* end_stream);
457 
458   // Send PRIORITY frames according to the new priority of an existing stream.
459   void UpdateStreamPriority(SpdyStream* stream,
460                             RequestPriority old_priority,
461                             RequestPriority new_priority);
462 
463   // Close the stream with the given ID, which must exist and be
464   // active. Note that that stream may hold the last reference to the
465   // session.
466   void CloseActiveStream(spdy::SpdyStreamId stream_id, int status);
467 
468   // Close the given created stream, which must exist but not yet be
469   // active. Note that |stream| may hold the last reference to the
470   // session.
471   void CloseCreatedStream(const base::WeakPtr<SpdyStream>& stream, int status);
472 
473   // Send a RST_STREAM frame with the given status code and close the
474   // stream with the given ID, which must exist and be active. Note
475   // that that stream may hold the last reference to the session.
476   void ResetStream(spdy::SpdyStreamId stream_id,
477                    int error,
478                    const std::string& description);
479 
480   // Check if a stream is active.
481   bool IsStreamActive(spdy::SpdyStreamId stream_id) const;
482 
483   // The LoadState is used for informing the user of the current network
484   // status, such as "resolving host", "connecting", etc.
485   LoadState GetLoadState() const;
486 
487   // MultiplexedSession methods:
488   int GetRemoteEndpoint(IPEndPoint* endpoint) override;
489   bool GetSSLInfo(SSLInfo* ssl_info) const override;
490   std::string_view GetAcceptChViaAlps(
491       const url::SchemeHostPort& scheme_host_port) const override;
492 
493   // Returns the protocol negotiated via ALPN for the underlying socket.
494   NextProto GetNegotiatedProtocol() const;
495 
496   // Send a WINDOW_UPDATE frame for a stream. Called by a stream
497   // whenever receive window size is increased.
498   void SendStreamWindowUpdate(spdy::SpdyStreamId stream_id,
499                               uint32_t delta_window_size);
500 
501   // Configure the amount of time that small receive window updates should
502   // be accumulated over (defaults to kDefaultTimeToBufferSmallWindowUpdates).
SetTimeToBufferSmallWindowUpdates(const base::TimeDelta buffer_time)503   void SetTimeToBufferSmallWindowUpdates(const base::TimeDelta buffer_time) {
504     time_to_buffer_small_window_updates_ = buffer_time;
505   }
506 
507   // Returns the configured time that small receive window updates should
508   // be accumulated over.
TimeToBufferSmallWindowUpdates()509   base::TimeDelta TimeToBufferSmallWindowUpdates() const {
510     return time_to_buffer_small_window_updates_;
511   }
512 
513   // Accessors for the session's availability state.
IsAvailable()514   bool IsAvailable() const { return availability_state_ == STATE_AVAILABLE; }
IsGoingAway()515   bool IsGoingAway() const { return availability_state_ == STATE_GOING_AWAY; }
IsDraining()516   bool IsDraining() const { return availability_state_ == STATE_DRAINING; }
517 
518   // Closes this session. This will close all active streams and mark
519   // the session as permanently closed. Callers must assume that the
520   // session is destroyed after this is called. (However, it may not
521   // be destroyed right away, e.g. when a SpdySession function is
522   // present in the call stack.)
523   //
524   // |err| should be < ERR_IO_PENDING; this function is intended to be
525   // called on error.
526   // |description| indicates the reason for the error.
527   void CloseSessionOnError(Error err, const std::string& description);
528 
529   // Mark this session as unavailable, meaning that it will not be used to
530   // service new streams. Unlike when a GOAWAY frame is received, this function
531   // will not close any streams.
532   void MakeUnavailable();
533 
534   // Closes all active streams with stream id's greater than
535   // |last_good_stream_id|, as well as any created or pending
536   // streams. Must be called only when |availability_state_| >=
537   // STATE_GOING_AWAY. After this function, DcheckGoingAway() will
538   // pass. May be called multiple times.
539   void StartGoingAway(spdy::SpdyStreamId last_good_stream_id, Error status);
540 
541   // Must be called only when going away (i.e., DcheckGoingAway()
542   // passes). If there are no more active streams and the session
543   // isn't closed yet, close it.
544   void MaybeFinishGoingAway();
545 
546   // Retrieves information on the current state of the SPDY session as a
547   // Value.
548   base::Value::Dict GetInfoAsValue() const;
549 
550   // Indicates whether the session is being reused after having successfully
551   // used to send/receive data in the past or if the underlying socket was idle
552   // before being used for a SPDY session.
553   bool IsReused() const;
554 
555   // Returns true if the underlying transport socket ever had any reads or
556   // writes.
WasEverUsed()557   bool WasEverUsed() const { return socket_->WasEverUsed(); }
558 
559   // Returns the load timing information from the perspective of the given
560   // stream.  If it's not the first stream, the connection is considered reused
561   // for that stream.
562   //
563   // This uses a different notion of reuse than IsReused().  This function
564   // sets |socket_reused| to false only if |stream_id| is the ID of the first
565   // stream using the session.  IsReused(), on the other hand, indicates if the
566   // session has been used to send/receive data at all.
567   bool GetLoadTimingInfo(spdy::SpdyStreamId stream_id,
568                          LoadTimingInfo* load_timing_info) const;
569 
570   // Returns true if session is currently active.
is_active()571   bool is_active() const {
572     return !active_streams_.empty() || !created_streams_.empty();
573   }
574 
575   // True if the server supports WebSocket protocol.
support_websocket()576   bool support_websocket() const { return support_websocket_; }
577 
578   // Returns true if no stream in the session can send data due to
579   // session flow control.
IsSendStalled()580   bool IsSendStalled() const { return session_send_window_size_ == 0; }
581 
net_log()582   const NetLogWithSource& net_log() const { return net_log_; }
583 
584   int GetPeerAddress(IPEndPoint* address) const;
585   int GetLocalAddress(IPEndPoint* address) const;
586 
587   // Adds |alias| to set of aliases associated with this session.
588   void AddPooledAlias(const SpdySessionKey& alias_key);
589 
590   // Removes |alias| from set of aliases associated with this session.
591   void RemovePooledAlias(const SpdySessionKey& alias_key);
592 
593   // Returns the set of aliases associated with this session.
pooled_aliases()594   const std::set<SpdySessionKey>& pooled_aliases() const {
595     return pooled_aliases_;
596   }
597 
598   // https://http2.github.io/http2-spec/#TLSUsage mandates minimum security
599   // standards for TLS.
600   bool HasAcceptableTransportSecurity() const;
601 
602   // Must be used only by |pool_|.
603   base::WeakPtr<SpdySession> GetWeakPtr();
604 
605   // HigherLayeredPool implementation:
606   bool CloseOneIdleConnection() override;
607 
608   // Change this session's socket tag to |new_tag|. Returns true on success.
609   bool ChangeSocketTag(const SocketTag& new_tag);
610 
611   // Whether connection status monitoring is active or not.
612   bool IsBrokenConnectionDetectionEnabled() const;
613 
614  private:
615   friend class test::SpdyStreamTest;
616   friend class base::RefCounted<SpdySession>;
617   friend class HttpNetworkTransactionTest;
618   friend class HttpProxyClientSocketPoolTest;
619   friend class SpdyHttpStreamTest;
620   friend class SpdyNetworkTransactionTest;
621   friend class SpdyProxyClientSocketTest;
622   friend class SpdySessionPoolTest;
623   friend class SpdySessionTest;
624   friend class SpdyStreamRequest;
625 
626   using PendingStreamRequestQueue =
627       base::circular_deque<base::WeakPtr<SpdyStreamRequest>>;
628   using ActiveStreamMap = std::map<spdy::SpdyStreamId, SpdyStream*>;
629   using CreatedStreamSet = std::set<raw_ptr<SpdyStream, SetExperimental>>;
630 
631   enum AvailabilityState {
632     // The session is available in its socket pool and can be used
633     // freely.
634     STATE_AVAILABLE,
635     // The session can process data on existing streams but will
636     // refuse to create new ones.
637     STATE_GOING_AWAY,
638     // The session is draining its write queue in preparation of closing.
639     // Further writes will not be queued, and further reads will not be issued
640     // (though the remainder of a current read may be processed). The session
641     // will be destroyed by its write loop once the write queue is drained.
642     STATE_DRAINING,
643   };
644 
645   enum ReadState {
646     READ_STATE_DO_READ,
647     READ_STATE_DO_READ_COMPLETE,
648   };
649 
650   enum WriteState {
651     // There is no in-flight write and the write queue is empty.
652     WRITE_STATE_IDLE,
653     WRITE_STATE_DO_WRITE,
654     WRITE_STATE_DO_WRITE_COMPLETE,
655   };
656 
657   // Has the shared logic for the other two Initialize methods that call it.
658   void InitializeInternal(SpdySessionPool* pool);
659 
660   // Called by SpdyStreamRequest to start a request to create a
661   // stream. If OK is returned, then |stream| will be filled in with a
662   // valid stream. If ERR_IO_PENDING is returned, then
663   // |request->OnRequestComplete{Success,Failure}()| will be called
664   // when the stream is created (unless it is cancelled). Otherwise,
665   // no stream is created and the error is returned.
666   int TryCreateStream(const base::WeakPtr<SpdyStreamRequest>& request,
667                       base::WeakPtr<SpdyStream>* stream);
668 
669   // Actually create a stream into |stream|. Returns OK if successful;
670   // otherwise, returns an error and |stream| is not filled.
671   int CreateStream(const SpdyStreamRequest& request,
672                    base::WeakPtr<SpdyStream>* stream);
673 
674   // Called by SpdyStreamRequest to remove |request| from the stream
675   // creation queue. Returns whether a request was removed from the queue.
676   bool CancelStreamRequest(const base::WeakPtr<SpdyStreamRequest>& request);
677 
678   // Removes |request| from the stream creation queue and reinserts it into the
679   // queue at the new |priority|.
680   void ChangeStreamRequestPriority(
681       const base::WeakPtr<SpdyStreamRequest>& request,
682       RequestPriority priority);
683 
684   // Returns the next pending stream request to process, or NULL if
685   // there is none.
686   base::WeakPtr<SpdyStreamRequest> GetNextPendingStreamRequest();
687 
688   // Called when there is room to create more streams (e.g., a stream
689   // was closed). Processes as many pending stream requests as
690   // possible.
691   void ProcessPendingStreamRequests();
692 
693   // Close the stream pointed to by the given iterator. Note that that
694   // stream may hold the last reference to the session.
695   void CloseActiveStreamIterator(ActiveStreamMap::iterator it, int status);
696 
697   // Close the stream pointed to by the given iterator. Note that that
698   // stream may hold the last reference to the session.
699   void CloseCreatedStreamIterator(CreatedStreamSet::iterator it, int status);
700 
701   // Calls EnqueueResetStreamFrame() and then
702   // CloseActiveStreamIterator().
703   void ResetStreamIterator(ActiveStreamMap::iterator it,
704                            int status,
705                            const std::string& description);
706 
707   // Send a RST_STREAM frame with the given parameters. There should
708   // either be no active stream with the given ID, or that active
709   // stream should be closed shortly after this function is called.
710   void EnqueueResetStreamFrame(spdy::SpdyStreamId stream_id,
711                                RequestPriority priority,
712                                spdy::SpdyErrorCode error_code,
713                                const std::string& description);
714 
715   // Send a PRIORITY frame with the given parameters.
716   void EnqueuePriorityFrame(spdy::SpdyStreamId stream_id,
717                             spdy::SpdyStreamId dependency_id,
718                             int weight,
719                             bool exclusive);
720 
721   // Calls DoReadLoop. Use this function instead of DoReadLoop when
722   // posting a task to pump the read loop.
723   void PumpReadLoop(ReadState expected_read_state, int result);
724 
725   // Advance the ReadState state machine. |expected_read_state| is the
726   // expected starting read state.
727   //
728   // This function must always be called via PumpReadLoop().
729   int DoReadLoop(ReadState expected_read_state, int result);
730   // The implementations of the states of the ReadState state machine.
731   int DoRead();
732   int DoReadComplete(int result);
733 
734   // Calls DoWriteLoop. If |availability_state_| is STATE_DRAINING and no
735   // writes remain, the session is removed from the session pool and
736   // destroyed.
737   //
738   // Use this function instead of DoWriteLoop when posting a task to
739   // pump the write loop.
740   void PumpWriteLoop(WriteState expected_write_state, int result);
741 
742   // Iff the write loop is not currently active, posts a callback into
743   // PumpWriteLoop().
744   void MaybePostWriteLoop();
745 
746   // Advance the WriteState state machine. |expected_write_state| is
747   // the expected starting write state.
748   //
749   // This function must always be called via PumpWriteLoop().
750   int DoWriteLoop(WriteState expected_write_state, int result);
751   // The implementations of the states of the WriteState state machine.
752   int DoWrite();
753   int DoWriteComplete(int result);
754 
755   void NotifyRequestsOfConfirmation(int rv);
756 
757   // TODO(akalin): Rename the Send* and Write* functions below to
758   // Enqueue*.
759 
760   // Send initial data. Called when a connection is successfully
761   // established in InitializeWithSocket() and
762   // |enable_sending_initial_data_| is true.
763   void SendInitialData();
764 
765   // Handle SETTING.  Either when we send settings, or when we receive a
766   // SETTINGS control frame, update our SpdySession accordingly.
767   void HandleSetting(uint32_t id, uint32_t value);
768 
769   // Adjust the send window size of all ActiveStreams and PendingStreamRequests.
770   void UpdateStreamsSendWindowSize(int32_t delta_window_size);
771 
772   // Checks the connection status in an energy efficient manner:
773   // * If the radio is in full power mode, send the PING immediately
774   // * If the radio is in standby, record the event and send the PING once the
775   //   radio wakes up
776   // The radio status check is currently only implemented for Android devices,
777   // on all other platforms the radio is assumed to be always active (i.e., no
778   // batching happens).
779   void MaybeCheckConnectionStatus();
780   // Always checks the connection status and schedules the next check.
781   void CheckConnectionStatus();
782   // Send PING frame if all previous PING frames have been ACKed,
783   // all posted CheckPingStatus() tasks have been executed,
784   // and too long time has passed since last read from server.
785   void MaybeSendPrefacePing();
786 
787   // Send a single WINDOW_UPDATE frame.
788   void SendWindowUpdateFrame(spdy::SpdyStreamId stream_id,
789                              uint32_t delta_window_size,
790                              RequestPriority priority);
791 
792   // Send the PING frame.
793   void WritePingFrame(spdy::SpdyPingId unique_id, bool is_ack);
794 
795   // Post a CheckPingStatus call after delay. Don't post if there is already
796   // CheckPingStatus running.
797   void PlanToCheckPingStatus();
798 
799   // Check the status of the connection. It calls |CloseSessionOnError| if we
800   // haven't received any data in |kHungInterval| time period.
801   void CheckPingStatus(base::TimeTicks last_check_time);
802 
803   // Get a new stream id.
804   spdy::SpdyStreamId GetNewStreamId();
805 
806   // Pushes the given frame with the given priority into the write
807   // queue for the session.
808   void EnqueueSessionWrite(RequestPriority priority,
809                            spdy::SpdyFrameType frame_type,
810                            std::unique_ptr<spdy::SpdySerializedFrame> frame);
811 
812   // Puts |producer| associated with |stream| onto the write queue
813   // with the given priority.
814   void EnqueueWrite(RequestPriority priority,
815                     spdy::SpdyFrameType frame_type,
816                     std::unique_ptr<SpdyBufferProducer> producer,
817                     const base::WeakPtr<SpdyStream>& stream,
818                     const NetworkTrafficAnnotationTag& traffic_annotation);
819 
820   // Inserts a newly-created stream into |created_streams_|.
821   void InsertCreatedStream(std::unique_ptr<SpdyStream> stream);
822 
823   // Activates |stream| (which must be in |created_streams_|) by
824   // assigning it an ID and returns it.
825   std::unique_ptr<SpdyStream> ActivateCreatedStream(SpdyStream* stream);
826 
827   // Inserts a newly-activated stream into |active_streams_|.
828   void InsertActivatedStream(std::unique_ptr<SpdyStream> stream);
829 
830   // Remove all internal references to |stream|, call OnClose() on it,
831   // and process any pending stream requests before deleting it.  Note
832   // that |stream| may hold the last reference to the session.
833   void DeleteStream(std::unique_ptr<SpdyStream> stream, int status);
834 
835   void RecordHistograms();
836   void RecordProtocolErrorHistogram(SpdyProtocolErrorDetails details);
837 
838   // DCHECKs that |availability_state_| >= STATE_GOING_AWAY, that
839   // there are no pending stream creation requests, and that there are
840   // no created streams.
841   void DcheckGoingAway() const;
842 
843   // Calls DcheckGoingAway(), then DCHECKs that |availability_state_|
844   // == STATE_DRAINING and |error_on_close_| has a valid value.
845   void DcheckDraining() const;
846 
847   // If the session is already draining, does nothing. Otherwise, moves
848   // the session to the draining state.
849   void DoDrainSession(Error err, const std::string& description);
850 
851   // Called right before closing a (possibly-inactive) stream for a
852   // reason other than being requested to by the stream.
853   void LogAbandonedStream(SpdyStream* stream, Error status);
854 
855   // Called right before closing an active stream for a reason other
856   // than being requested to by the stream.
857   void LogAbandonedActiveStream(ActiveStreamMap::const_iterator it,
858                                 Error status);
859 
860   // Invokes a user callback for stream creation.  We provide this method so it
861   // can be deferred to the MessageLoop, so we avoid re-entrancy problems.
862   void CompleteStreamRequest(
863       const base::WeakPtr<SpdyStreamRequest>& pending_request);
864 
865   // BufferedSpdyFramerVisitorInterface:
866   void OnError(
867       http2::Http2DecoderAdapter::SpdyFramerError spdy_framer_error) override;
868   void OnStreamError(spdy::SpdyStreamId stream_id,
869                      const std::string& description) override;
870   void OnPing(spdy::SpdyPingId unique_id, bool is_ack) override;
871   void OnRstStream(spdy::SpdyStreamId stream_id,
872                    spdy::SpdyErrorCode error_code) override;
873   void OnGoAway(spdy::SpdyStreamId last_accepted_stream_id,
874                 spdy::SpdyErrorCode error_code,
875                 std::string_view debug_data) override;
876   void OnDataFrameHeader(spdy::SpdyStreamId stream_id,
877                          size_t length,
878                          bool fin) override;
879   void OnStreamFrameData(spdy::SpdyStreamId stream_id,
880                          const char* data,
881                          size_t len) override;
882   void OnStreamEnd(spdy::SpdyStreamId stream_id) override;
883   void OnStreamPadding(spdy::SpdyStreamId stream_id, size_t len) override;
884   void OnSettings() override;
885   void OnSettingsAck() override;
886   void OnSetting(spdy::SpdySettingsId id, uint32_t value) override;
887   void OnSettingsEnd() override;
888   void OnWindowUpdate(spdy::SpdyStreamId stream_id,
889                       int delta_window_size) override;
890   void OnPushPromise(spdy::SpdyStreamId stream_id,
891                      spdy::SpdyStreamId promised_stream_id,
892                      spdy::Http2HeaderBlock headers) override;
893   void OnHeaders(spdy::SpdyStreamId stream_id,
894                  bool has_priority,
895                  int weight,
896                  spdy::SpdyStreamId parent_stream_id,
897                  bool exclusive,
898                  bool fin,
899                  spdy::Http2HeaderBlock headers,
900                  base::TimeTicks recv_first_byte_time) override;
901   void OnAltSvc(spdy::SpdyStreamId stream_id,
902                 std::string_view origin,
903                 const spdy::SpdyAltSvcWireFormat::AlternativeServiceVector&
904                     altsvc_vector) override;
905   bool OnUnknownFrame(spdy::SpdyStreamId stream_id,
906                       uint8_t frame_type) override;
907 
908   // spdy::SpdyFramerDebugVisitorInterface
909   void OnSendCompressedFrame(spdy::SpdyStreamId stream_id,
910                              spdy::SpdyFrameType type,
911                              size_t payload_len,
912                              size_t frame_len) override;
913   void OnReceiveCompressedFrame(spdy::SpdyStreamId stream_id,
914                                 spdy::SpdyFrameType type,
915                                 size_t frame_len) override;
916 
917   // Called when bytes are consumed from a SpdyBuffer for a DATA frame
918   // that is to be written or is being written. Increases the send
919   // window size accordingly if some or all of the SpdyBuffer is being
920   // discarded.
921   //
922   // If session flow control is turned off, this must not be called.
923   void OnWriteBufferConsumed(size_t frame_payload_size,
924                              size_t consume_size,
925                              SpdyBuffer::ConsumeSource consume_source);
926 
927   // Called when the radio goes into full power mode. Currently implemented only
928   // for Android devices.
929   void OnDefaultNetworkActive() override;
930 
931   // Called by OnWindowUpdate() (which is in turn called by the
932   // framer) to increase this session's send window size by
933   // |delta_window_size| from a WINDOW_UPDATE frome, which must be at
934   // least 1. If |delta_window_size| would cause this session's send
935   // window size to overflow, does nothing.
936   //
937   // If session flow control is turned off, this must not be called.
938   void IncreaseSendWindowSize(int delta_window_size);
939 
940   // If session flow control is turned on, called by CreateDataFrame()
941   // (which is in turn called by a stream) to decrease this session's
942   // send window size by |delta_window_size|, which must be at least 1
943   // and at most kMaxSpdyFrameChunkSize.  |delta_window_size| must not
944   // cause this session's send window size to go negative.
945   //
946   // If session flow control is turned off, this must not be called.
947   void DecreaseSendWindowSize(int32_t delta_window_size);
948 
949   // Called when bytes are consumed by the delegate from a SpdyBuffer
950   // containing received data. Increases the receive window size
951   // accordingly.
952   //
953   // If session flow control is turned off, this must not be called.
954   void OnReadBufferConsumed(size_t consume_size,
955                             SpdyBuffer::ConsumeSource consume_source);
956 
957   // Called by OnReadBufferConsume to increase this session's receive
958   // window size by |delta_window_size|, which must be at least 1 and
959   // must not cause this session's receive window size to overflow,
960   // possibly also sending a WINDOW_UPDATE frame. Also called during
961   // initialization to set the initial receive window size.
962   //
963   // If session flow control is turned off, this must not be called.
964   void IncreaseRecvWindowSize(int32_t delta_window_size);
965 
966   // Called by OnStreamFrameData (which is in turn called by the
967   // framer) to decrease this session's receive window size by
968   // |delta_window_size|, which must be at least 1 and must not cause
969   // this session's receive window size to go negative.
970   //
971   // If session flow control is turned off, this must not be called.
972   void DecreaseRecvWindowSize(int32_t delta_window_size);
973 
974   // Queue a send-stalled stream for possibly resuming once we're not
975   // send-stalled anymore.
976   void QueueSendStalledStream(const SpdyStream& stream);
977 
978   // Go through the queue of send-stalled streams and try to resume as
979   // many as possible.
980   void ResumeSendStalledStreams();
981 
982   // Returns the next stream to possibly resume, or 0 if the queue is
983   // empty.
984   spdy::SpdyStreamId PopStreamToPossiblyResume();
985 
986   // Enables connection status monitoring, causing the session to periodically
987   // send a PING frame.
988   // This must be called at most once for each stream requiring it. If called,
989   // MaybeDisableBrokenConnectionDetection() will need to be called before
990   // closing the requesting stream.
991   // Note: `heartbeat_interval` should be considered a suggestion. The
992   // implementation, for example, could either:
993   // * Avoid sending a PING, if one has recently been transmitted or is
994   //   already in flight
995   // * Delay sending a PING, to avoid waking up the radio on mobile platforms
996   // Only the first value of `heartbeat_interval` is taken into account.
997   void EnableBrokenConnectionDetection(base::TimeDelta heartbeat_interval);
998 
999   // Requests to disable connection status monitoring. The service is disabled
1000   // only if no other active stream also requires it (an internal counter keeps
1001   // track of that).
1002   // This must be called once for each stream that requested it previously.
1003   void MaybeDisableBrokenConnectionDetection();
1004 
1005   // Whether Do{Read,Write}Loop() is in the call stack. Useful for
1006   // making sure we don't destroy ourselves prematurely in that case.
1007   bool in_io_loop_ = false;
1008 
1009   // The key used to identify this session.
1010   SpdySessionKey spdy_session_key_;
1011 
1012   // Set set of SpdySessionKeys for which this session has serviced
1013   // requests.
1014   std::set<SpdySessionKey> pooled_aliases_;
1015 
1016   // |pool_| owns us, therefore its lifetime must exceed ours.
1017   raw_ptr<SpdySessionPool> pool_ = nullptr;
1018   raw_ptr<HttpServerProperties> http_server_properties_;
1019 
1020   raw_ptr<TransportSecurityState> transport_security_state_;
1021   raw_ptr<SSLConfigService> ssl_config_service_;
1022 
1023   // One of these two owns the socket for this session, which is stored in
1024   // |socket_|. If |client_socket_handle_| is non-null, this session is on top
1025   // of a socket in a socket pool. If |owned_stream_socket_| is non-null, this
1026   // session is directly on top of a socket, which is not in a socket pool.
1027   std::unique_ptr<ClientSocketHandle> client_socket_handle_;
1028   std::unique_ptr<StreamSocket> owned_stream_socket_;
1029 
1030   // This is non-null only if |owned_stream_socket_| is non-null.
1031   std::unique_ptr<LoadTimingInfo::ConnectTiming> connect_timing_;
1032 
1033   // The socket for this session.
1034   raw_ptr<StreamSocket> socket_ = nullptr;
1035 
1036   // The read buffer used to read data from the socket.
1037   // Non-null if there is a Read() pending.
1038   scoped_refptr<IOBuffer> read_buffer_;
1039 
1040   spdy::SpdyStreamId stream_hi_water_mark_;  // The next stream id to use.
1041 
1042   // Queue, for each priority, of pending stream requests that have
1043   // not yet been satisfied.
1044   PendingStreamRequestQueue pending_create_stream_queues_[NUM_PRIORITIES];
1045 
1046   // Map from stream id to all active streams.  Streams are active in the sense
1047   // that they have a consumer (typically HttpNetworkTransaction and regardless
1048   // of whether or not there is currently any ongoing IO) or there are still
1049   // network events incoming even though the consumer has already gone away
1050   // (cancellation).
1051   //
1052   // |active_streams_| owns all its SpdyStream objects.
1053   //
1054   // TODO(willchan): Perhaps we should separate out cancelled streams and move
1055   // them into a separate ActiveStreamMap, and not deliver network events to
1056   // them?
1057   ActiveStreamMap active_streams_;
1058 
1059   // Set of all created streams but that have not yet sent any frames.
1060   //
1061   // |created_streams_| owns all its SpdyStream objects.
1062   CreatedStreamSet created_streams_;
1063 
1064   // The write queue.
1065   SpdyWriteQueue write_queue_;
1066 
1067   // Data for the frame we are currently sending.
1068 
1069   // The buffer we're currently writing.
1070   std::unique_ptr<SpdyBuffer> in_flight_write_;
1071   // The type of the frame in |in_flight_write_|.
1072   spdy::SpdyFrameType in_flight_write_frame_type_ = spdy::SpdyFrameType::DATA;
1073   // The size of the frame in |in_flight_write_|.
1074   size_t in_flight_write_frame_size_ = 0;
1075   // The stream to notify when |in_flight_write_| has been written to
1076   // the socket completely.
1077   base::WeakPtr<SpdyStream> in_flight_write_stream_;
1078 
1079   // Traffic annotation for the write in progress.
1080   MutableNetworkTrafficAnnotationTag in_flight_write_traffic_annotation_;
1081 
1082   // Spdy Frame state.
1083   std::unique_ptr<BufferedSpdyFramer> buffered_spdy_framer_;
1084 
1085   // The state variables.
1086   AvailabilityState availability_state_ = STATE_AVAILABLE;
1087   ReadState read_state_ = READ_STATE_DO_READ;
1088   WriteState write_state_ = WRITE_STATE_IDLE;
1089 
1090   // If the session is closing (i.e., |availability_state_| is STATE_DRAINING),
1091   // then |error_on_close_| holds the error with which it was closed, which
1092   // may be OK (upon a polite GOAWAY) or an error < ERR_IO_PENDING otherwise.
1093   // Initialized to OK.
1094   Error error_on_close_ = OK;
1095 
1096   // Settings that are sent in the initial SETTINGS frame
1097   // (if |enable_sending_initial_data_| is true),
1098   // and also control SpdySession parameters like initial receive window size
1099   // and maximum HPACK dynamic table size.
1100   const spdy::SettingsMap initial_settings_;
1101 
1102   // If true, a setting parameter with reserved identifier will be sent in every
1103   // initial SETTINGS frame, see
1104   // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00.
1105   // The setting identifier and value will be drawn independently for each
1106   // connection to prevent tracking of the client.
1107   const bool enable_http2_settings_grease_;
1108 
1109   // If set, an HTTP/2 frame with a reserved frame type will be sent after
1110   // every HTTP/2 SETTINGS frame and before every HTTP/2 DATA frame. See
1111   // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00.
1112   const std::optional<SpdySessionPool::GreasedHttp2Frame> greased_http2_frame_;
1113 
1114   // If set, the HEADERS frame carrying a request without body will not have the
1115   // END_STREAM flag set.  The stream will be closed by a subsequent empty DATA
1116   // frame with END_STREAM.  Does not affect bidirectional or proxy streams.
1117   // If unset, the HEADERS frame will have the END_STREAM flag set on.
1118   // This is useful in conjuction with |greased_http2_frame_| so that a frame
1119   // of reserved type can be sent out even on requests without a body.
1120   const bool http2_end_stream_with_data_frame_;
1121 
1122   // If true, enable sending PRIORITY_UPDATE frames until SETTINGS frame
1123   // arrives.  After SETTINGS frame arrives, do not send PRIORITY_UPDATE frames
1124   // any longer if SETTINGS_DEPRECATE_HTTP2_PRIORITIES is missing or has zero 0,
1125   // but continue and also stop sending HTTP/2-style priority information in
1126   // HEADERS frames and PRIORITY frames if it has value 1.
1127   const bool enable_priority_update_;
1128 
1129   // The value of the last received SETTINGS_DEPRECATE_HTTP2_PRIORITIES, with 0
1130   // mapping to false and 1 to true.  Initial value is false.
1131   bool deprecate_http2_priorities_ = false;
1132 
1133   // True if at least one SETTINGS frame has been received.
1134   bool settings_frame_received_ = false;
1135 
1136   // The callbacks to notify a request that the handshake has been confirmed.
1137   std::vector<CompletionOnceCallback> waiting_for_confirmation_callbacks_;
1138 
1139   // True if there is an ongoing handshake confirmation with outstanding
1140   // requests.
1141   bool in_confirm_handshake_ = false;
1142 
1143   // Limits
1144   size_t max_concurrent_streams_;
1145 
1146   // Some statistics counters for the session.
1147   int streams_initiated_count_ = 0;
1148 
1149   int streams_abandoned_count_ = 0;
1150 
1151   // True if there has been a ping sent for which we have not received a
1152   // response yet.  There is always at most one ping in flight.
1153   bool ping_in_flight_ = false;
1154 
1155   // Triggers periodic connection status checks.
1156   base::OneShotTimer heartbeat_timer_;
1157 
1158   // Period used by the connection status monitoring mechanism.
1159   base::TimeDelta heartbeat_interval_;
1160 
1161   // True if the connection status should be checked once the radio wakes up.
1162   bool check_connection_on_radio_wakeup_ = false;
1163 
1164   // This is the next ping_id (unique_id) to be sent in PING frame.
1165   spdy::SpdyPingId next_ping_id_ = 1;
1166 
1167   // This is the last time we have sent a PING.
1168   base::TimeTicks last_ping_sent_time_;
1169 
1170   // This is the last time we had read activity in the session.
1171   base::TimeTicks last_read_time_;
1172 
1173   // This is the length of the last compressed frame.
1174   size_t last_compressed_frame_len_ = 0;
1175 
1176   // True if there is a CheckPingStatus() task posted on the message loop.
1177   bool check_ping_status_pending_ = false;
1178 
1179   // Current send window size.  Zero unless session flow control is turned on.
1180   int32_t session_send_window_size_ = 0;
1181 
1182   // Maximum receive window size.  Each time a WINDOW_UPDATE is sent, it
1183   // restores the receive window size to this value.  Zero unless session flow
1184   // control is turned on.
1185   int32_t session_max_recv_window_size_;
1186 
1187   // Maximum number of capped frames that can be queued at any time.
1188   // Every time we try to enqueue a capped frame, we check that there aren't
1189   // more than this amount already queued, and close the connection if so.
1190   int session_max_queued_capped_frames_;
1191 
1192   // Number of active requests which asked for connection status monitoring.
1193   int broken_connection_detection_requests_ = 0;
1194 
1195   // Sum of |session_unacked_recv_window_bytes_| and current receive window
1196   // size.  Zero unless session flow control is turned on.
1197   // TODO(bnc): Rename or change semantics so that |window_size_| is actual
1198   // window size.
1199   int32_t session_recv_window_size_ = 0;
1200 
1201   // When bytes are consumed, SpdyIOBuffer destructor calls back to SpdySession,
1202   // and this member keeps count of them until the corresponding WINDOW_UPDATEs
1203   // are sent.  Zero unless session flow control is turned on.
1204   int32_t session_unacked_recv_window_bytes_ = 0;
1205 
1206   // Time of the last WINDOW_UPDATE for the receive window.
1207   base::TimeTicks last_recv_window_update_;
1208 
1209   // Time to accumilate small receive window updates for.
1210   base::TimeDelta time_to_buffer_small_window_updates_;
1211 
1212   // Initial send window size for this session's streams. Can be
1213   // changed by an arriving SETTINGS frame. Newly created streams use
1214   // this value for the initial send window size.
1215   int32_t stream_initial_send_window_size_;
1216 
1217   // The maximum HPACK dynamic table size the server is allowed to set.
1218   uint32_t max_header_table_size_;
1219 
1220   // Initial receive window size for this session's streams. There are
1221   // plans to add a command line switch that would cause a SETTINGS
1222   // frame with window size announcement to be sent on startup. Newly
1223   // created streams will use this value for the initial receive
1224   // window size.
1225   int32_t stream_max_recv_window_size_;
1226 
1227   // A queue of stream IDs that have been send-stalled at some point
1228   // in the past.
1229   base::circular_deque<spdy::SpdyStreamId>
1230       stream_send_unstall_queue_[NUM_PRIORITIES];
1231 
1232   NetLogWithSource net_log_;
1233 
1234   // Versions of QUIC which may be used.
1235   const quic::ParsedQuicVersionVector quic_supported_versions_;
1236 
1237   // Outside of tests, these should always be true.
1238   const bool enable_sending_initial_data_;
1239   const bool enable_ping_based_connection_checking_;
1240 
1241   const bool is_http2_enabled_;
1242   const bool is_quic_enabled_;
1243 
1244   // True if the server has advertised WebSocket support via
1245   // spdy::SETTINGS_ENABLE_CONNECT_PROTOCOL, see
1246   // https://tools.ietf.org/html/draft-ietf-httpbis-h2-websockets-00.
1247   bool support_websocket_ = false;
1248 
1249   // |connection_at_risk_of_loss_time_| is an optimization to avoid sending
1250   // wasteful preface pings (when we just got some data).
1251   //
1252   // If it is zero (the most conservative figure), then we always send the
1253   // preface ping (when none are in flight).
1254   //
1255   // It is common for TCP/IP sessions to time out in about 3-5 minutes.
1256   // Certainly if it has been more than 3 minutes, we do want to send a preface
1257   // ping.
1258   //
1259   // We don't think any connection will time out in under about 10 seconds. So
1260   // this might as well be set to something conservative like 10 seconds. Later,
1261   // we could adjust it to send fewer pings perhaps.
1262   base::TimeDelta connection_at_risk_of_loss_time_;
1263 
1264   // The amount of time that we are willing to tolerate with no activity (of any
1265   // form), while there is a ping in flight, before we declare the connection to
1266   // be hung. TODO(rtenneti): When hung, instead of resetting connection, race
1267   // to build a new connection, and see if that completes before we (finally)
1268   // get a PING response (http://crbug.com/127812).
1269   base::TimeDelta hung_interval_;
1270 
1271   TimeFunc time_func_;
1272 
1273   Http2PriorityDependencies priority_dependency_state_;
1274 
1275   // Map of origin to Accept-CH header field values received via ALPS.
1276   base::flat_map<url::SchemeHostPort, std::string>
1277       accept_ch_entries_received_via_alps_;
1278 
1279   // Network quality estimator to which the ping RTTs should be reported. May be
1280   // nullptr.
1281   raw_ptr<NetworkQualityEstimator> network_quality_estimator_;
1282 
1283   // Used for accessing the SpdySession from asynchronous tasks. An asynchronous
1284   // must check if its WeakPtr<SpdySession> is valid before accessing it, to
1285   // correctly handle the case where it became unavailable and was deleted.
1286   base::WeakPtrFactory<SpdySession> weak_factory_{this};
1287 };
1288 
1289 }  // namespace net
1290 
1291 #endif  // NET_SPDY_SPDY_SESSION_H_
1292