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