1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
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 <deque>
9#include <map>
10#include <set>
11#include <string>
12
13#include "base/basictypes.h"
14#include "base/gtest_prod_util.h"
15#include "base/memory/ref_counted.h"
16#include "base/memory/scoped_ptr.h"
17#include "base/memory/weak_ptr.h"
18#include "base/time/time.h"
19#include "net/base/io_buffer.h"
20#include "net/base/load_states.h"
21#include "net/base/net_errors.h"
22#include "net/base/net_export.h"
23#include "net/base/request_priority.h"
24#include "net/socket/client_socket_handle.h"
25#include "net/socket/client_socket_pool.h"
26#include "net/socket/next_proto.h"
27#include "net/socket/ssl_client_socket.h"
28#include "net/socket/stream_socket.h"
29#include "net/spdy/buffered_spdy_framer.h"
30#include "net/spdy/spdy_buffer.h"
31#include "net/spdy/spdy_framer.h"
32#include "net/spdy/spdy_header_block.h"
33#include "net/spdy/spdy_protocol.h"
34#include "net/spdy/spdy_session_pool.h"
35#include "net/spdy/spdy_stream.h"
36#include "net/spdy/spdy_write_queue.h"
37#include "net/ssl/ssl_config_service.h"
38#include "url/gurl.h"
39
40namespace net {
41
42// This is somewhat arbitrary and not really fixed, but it will always work
43// reasonably with ethernet. Chop the world into 2-packet chunks.  This is
44// somewhat arbitrary, but is reasonably small and ensures that we elicit
45// ACKs quickly from TCP (because TCP tries to only ACK every other packet).
46const int kMss = 1430;
47// The 8 is the size of the SPDY frame header.
48const int kMaxSpdyFrameChunkSize = (2 * kMss) - 8;
49
50// Maximum number of concurrent streams we will create, unless the server
51// sends a SETTINGS frame with a different value.
52const size_t kInitialMaxConcurrentStreams = 100;
53
54// Specifies the maxiumum concurrent streams server could send (via push).
55const int kMaxConcurrentPushedStreams = 1000;
56
57// Specifies the maximum number of bytes to read synchronously before
58// yielding.
59const int kMaxReadBytesWithoutYielding = 32 * 1024;
60
61// The initial receive window size for both streams and sessions.
62const int32 kDefaultInitialRecvWindowSize = 10 * 1024 * 1024;  // 10MB
63
64class BoundNetLog;
65struct LoadTimingInfo;
66class SpdyStream;
67class SSLInfo;
68
69// NOTE: There's an enum of the same name (also with numeric suffixes)
70// in histograms.xml.
71//
72// WARNING: DO NOT INSERT ENUMS INTO THIS LIST! Add only to the end.
73enum SpdyProtocolErrorDetails {
74  // SpdyFramer::SpdyErrors
75  SPDY_ERROR_NO_ERROR,
76  SPDY_ERROR_INVALID_CONTROL_FRAME,
77  SPDY_ERROR_CONTROL_PAYLOAD_TOO_LARGE,
78  SPDY_ERROR_ZLIB_INIT_FAILURE,
79  SPDY_ERROR_UNSUPPORTED_VERSION,
80  SPDY_ERROR_DECOMPRESS_FAILURE,
81  SPDY_ERROR_COMPRESS_FAILURE,
82  SPDY_ERROR_CREDENTIAL_FRAME_CORRUPT,
83  SPDY_ERROR_INVALID_DATA_FRAME_FLAGS,
84  SPDY_ERROR_INVALID_CONTROL_FRAME_FLAGS,
85  // SpdyRstStreamStatus
86  STATUS_CODE_INVALID,
87  STATUS_CODE_PROTOCOL_ERROR,
88  STATUS_CODE_INVALID_STREAM,
89  STATUS_CODE_REFUSED_STREAM,
90  STATUS_CODE_UNSUPPORTED_VERSION,
91  STATUS_CODE_CANCEL,
92  STATUS_CODE_INTERNAL_ERROR,
93  STATUS_CODE_FLOW_CONTROL_ERROR,
94  STATUS_CODE_STREAM_IN_USE,
95  STATUS_CODE_STREAM_ALREADY_CLOSED,
96  STATUS_CODE_INVALID_CREDENTIALS,
97  STATUS_CODE_FRAME_TOO_LARGE,
98  // SpdySession errors
99  PROTOCOL_ERROR_UNEXPECTED_PING,
100  PROTOCOL_ERROR_RST_STREAM_FOR_NON_ACTIVE_STREAM,
101  PROTOCOL_ERROR_SPDY_COMPRESSION_FAILURE,
102  PROTOCOL_ERROR_REQUEST_FOR_SECURE_CONTENT_OVER_INSECURE_SESSION,
103  PROTOCOL_ERROR_SYN_REPLY_NOT_RECEIVED,
104  PROTOCOL_ERROR_INVALID_WINDOW_UPDATE_SIZE,
105  PROTOCOL_ERROR_RECEIVE_WINDOW_VIOLATION,
106  NUM_SPDY_PROTOCOL_ERROR_DETAILS
107};
108
109COMPILE_ASSERT(STATUS_CODE_INVALID ==
110               static_cast<SpdyProtocolErrorDetails>(SpdyFramer::LAST_ERROR),
111               SpdyProtocolErrorDetails_SpdyErrors_mismatch);
112
113COMPILE_ASSERT(PROTOCOL_ERROR_UNEXPECTED_PING ==
114               static_cast<SpdyProtocolErrorDetails>(
115                   RST_STREAM_NUM_STATUS_CODES + STATUS_CODE_INVALID),
116               SpdyProtocolErrorDetails_SpdyErrors_mismatch);
117
118// A helper class used to manage a request to create a stream.
119class NET_EXPORT_PRIVATE SpdyStreamRequest {
120 public:
121  SpdyStreamRequest();
122  // Calls CancelRequest().
123  ~SpdyStreamRequest();
124
125  // Starts the request to create a stream. If OK is returned, then
126  // ReleaseStream() may be called. If ERR_IO_PENDING is returned,
127  // then when the stream is created, |callback| will be called, at
128  // which point ReleaseStream() may be called. Otherwise, the stream
129  // is not created, an error is returned, and ReleaseStream() may not
130  // be called.
131  //
132  // If OK is returned, must not be called again without
133  // ReleaseStream() being called first. If ERR_IO_PENDING is
134  // returned, must not be called again without CancelRequest() or
135  // ReleaseStream() being called first. Otherwise, in case of an
136  // immediate error, this may be called again.
137  int StartRequest(SpdyStreamType type,
138                   const base::WeakPtr<SpdySession>& session,
139                   const GURL& url,
140                   RequestPriority priority,
141                   const BoundNetLog& net_log,
142                   const CompletionCallback& callback);
143
144  // Cancels any pending stream creation request. May be called
145  // repeatedly.
146  void CancelRequest();
147
148  // Transfers the created stream (guaranteed to not be NULL) to the
149  // caller. Must be called at most once after StartRequest() returns
150  // OK or |callback| is called with OK. The caller must immediately
151  // set a delegate for the returned stream (except for test code).
152  base::WeakPtr<SpdyStream> ReleaseStream();
153
154 private:
155  friend class SpdySession;
156
157  // Called by |session_| when the stream attempt has finished
158  // successfully.
159  void OnRequestCompleteSuccess(const base::WeakPtr<SpdyStream>& stream);
160
161  // Called by |session_| when the stream attempt has finished with an
162  // error. Also called with ERR_ABORTED if |session_| is destroyed
163  // while the stream attempt is still pending.
164  void OnRequestCompleteFailure(int rv);
165
166  // Accessors called by |session_|.
167  SpdyStreamType type() const { return type_; }
168  const GURL& url() const { return url_; }
169  RequestPriority priority() const { return priority_; }
170  const BoundNetLog& net_log() const { return net_log_; }
171
172  void Reset();
173
174  base::WeakPtrFactory<SpdyStreamRequest> weak_ptr_factory_;
175  SpdyStreamType type_;
176  base::WeakPtr<SpdySession> session_;
177  base::WeakPtr<SpdyStream> stream_;
178  GURL url_;
179  RequestPriority priority_;
180  BoundNetLog net_log_;
181  CompletionCallback callback_;
182
183  DISALLOW_COPY_AND_ASSIGN(SpdyStreamRequest);
184};
185
186class NET_EXPORT SpdySession : public BufferedSpdyFramerVisitorInterface,
187                               public SpdyFramerDebugVisitorInterface,
188                               public HigherLayeredPool {
189 public:
190  // TODO(akalin): Use base::TickClock when it becomes available.
191  typedef base::TimeTicks (*TimeFunc)(void);
192
193  // How we handle flow control (version-dependent).
194  enum FlowControlState {
195    FLOW_CONTROL_NONE,
196    FLOW_CONTROL_STREAM,
197    FLOW_CONTROL_STREAM_AND_SESSION
198  };
199
200  // Create a new SpdySession.
201  // |spdy_session_key| is the host/port that this session connects to, privacy
202  // and proxy configuration settings that it's using.
203  // |session| is the HttpNetworkSession.  |net_log| is the NetLog that we log
204  // network events to.
205  SpdySession(const SpdySessionKey& spdy_session_key,
206              const base::WeakPtr<HttpServerProperties>& http_server_properties,
207              bool verify_domain_authentication,
208              bool enable_sending_initial_data,
209              bool enable_compression,
210              bool enable_ping_based_connection_checking,
211              NextProto default_protocol,
212              size_t stream_initial_recv_window_size,
213              size_t initial_max_concurrent_streams,
214              size_t max_concurrent_streams_limit,
215              TimeFunc time_func,
216              const HostPortPair& trusted_spdy_proxy,
217              NetLog* net_log);
218
219  virtual ~SpdySession();
220
221  const HostPortPair& host_port_pair() const {
222    return spdy_session_key_.host_port_proxy_pair().first;
223  }
224  const HostPortProxyPair& host_port_proxy_pair() const {
225    return spdy_session_key_.host_port_proxy_pair();
226  }
227  const SpdySessionKey& spdy_session_key() const {
228    return spdy_session_key_;
229  }
230  // Get a pushed stream for a given |url|.  If the server initiates a
231  // stream, it might already exist for a given path.  The server
232  // might also not have initiated the stream yet, but indicated it
233  // will via X-Associated-Content.  Returns OK if a stream was found
234  // and put into |spdy_stream|, or if one was not found but it is
235  // okay to create a new stream (in which case |spdy_stream| is
236  // reset).  Returns an error (not ERR_IO_PENDING) otherwise, and
237  // resets |spdy_stream|.
238  int GetPushStream(
239      const GURL& url,
240      base::WeakPtr<SpdyStream>* spdy_stream,
241      const BoundNetLog& stream_net_log);
242
243  // Initialize the session with the given connection. |is_secure|
244  // must indicate whether |connection| uses an SSL socket or not; it
245  // is usually true, but it can be false for testing or when SPDY is
246  // configured to work with non-secure sockets.
247  //
248  // |pool| is the SpdySessionPool that owns us.  Its lifetime must
249  // strictly be greater than |this|.
250  //
251  // |certificate_error_code| must either be OK or less than
252  // ERR_IO_PENDING.
253  //
254  // Returns OK on success, or an error on failure. Never returns
255  // ERR_IO_PENDING. If an error is returned, the session must be
256  // destroyed immediately.
257  Error InitializeWithSocket(scoped_ptr<ClientSocketHandle> connection,
258                             SpdySessionPool* pool,
259                             bool is_secure,
260                             int certificate_error_code);
261
262  // Returns the protocol used by this session. Always between
263  // kProtoSPDYMinimumVersion and kProtoSPDYMaximumVersion.
264  NextProto protocol() const { return protocol_; }
265
266  // Check to see if this SPDY session can support an additional domain.
267  // If the session is un-authenticated, then this call always returns true.
268  // For SSL-based sessions, verifies that the server certificate in use by
269  // this session provides authentication for the domain and no client
270  // certificate or channel ID was sent to the original server during the SSL
271  // handshake.  NOTE:  This function can have false negatives on some
272  // platforms.
273  // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch
274  // histogram because this function does more than verifying domain
275  // authentication now.
276  bool VerifyDomainAuthentication(const std::string& domain);
277
278  // Pushes the given producer into the write queue for
279  // |stream|. |stream| is guaranteed to be activated before the
280  // producer is used to produce its frame.
281  void EnqueueStreamWrite(const base::WeakPtr<SpdyStream>& stream,
282                          SpdyFrameType frame_type,
283                          scoped_ptr<SpdyBufferProducer> producer);
284
285  // Creates and returns a SYN frame for |stream_id|.
286  scoped_ptr<SpdyFrame> CreateSynStream(
287      SpdyStreamId stream_id,
288      RequestPriority priority,
289      uint8 credential_slot,
290      SpdyControlFlags flags,
291      const SpdyHeaderBlock& headers);
292
293  // Creates and returns a SpdyBuffer holding a data frame with the
294  // given data. May return NULL if stalled by flow control.
295  scoped_ptr<SpdyBuffer> CreateDataBuffer(SpdyStreamId stream_id,
296                                          IOBuffer* data,
297                                          int len,
298                                          SpdyDataFlags flags);
299
300  // Close the stream with the given ID, which must exist and be
301  // active. Note that that stream may hold the last reference to the
302  // session.
303  void CloseActiveStream(SpdyStreamId stream_id, int status);
304
305  // Close the given created stream, which must exist but not yet be
306  // active. Note that |stream| may hold the last reference to the
307  // session.
308  void CloseCreatedStream(const base::WeakPtr<SpdyStream>& stream, int status);
309
310  // Send a RST_STREAM frame with the given status code and close the
311  // stream with the given ID, which must exist and be active. Note
312  // that that stream may hold the last reference to the session.
313  void ResetStream(SpdyStreamId stream_id,
314                   SpdyRstStreamStatus status,
315                   const std::string& description);
316
317  // Check if a stream is active.
318  bool IsStreamActive(SpdyStreamId stream_id) const;
319
320  // The LoadState is used for informing the user of the current network
321  // status, such as "resolving host", "connecting", etc.
322  LoadState GetLoadState() const;
323
324  // Fills SSL info in |ssl_info| and returns true when SSL is in use.
325  bool GetSSLInfo(SSLInfo* ssl_info,
326                  bool* was_npn_negotiated,
327                  NextProto* protocol_negotiated);
328
329  // Fills SSL Certificate Request info |cert_request_info| and returns
330  // true when SSL is in use.
331  bool GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info);
332
333  // Send a WINDOW_UPDATE frame for a stream. Called by a stream
334  // whenever receive window size is increased.
335  void SendStreamWindowUpdate(SpdyStreamId stream_id,
336                              uint32 delta_window_size);
337
338  // Whether the stream is closed, i.e. it has stopped processing data
339  // and is about to be destroyed.
340  //
341  // TODO(akalin): This is only used in tests. Remove this function
342  // and have tests test the WeakPtr instead.
343  bool IsClosed() const { return availability_state_ == STATE_CLOSED; }
344
345  // Closes this session. This will close all active streams and mark
346  // the session as permanently closed. Callers must assume that the
347  // session is destroyed after this is called. (However, it may not
348  // be destroyed right away, e.g. when a SpdySession function is
349  // present in the call stack.)
350  //
351  // |err| should be < ERR_IO_PENDING; this function is intended to be
352  // called on error.
353  // |description| indicates the reason for the error.
354  void CloseSessionOnError(Error err, const std::string& description);
355
356  // Retrieves information on the current state of the SPDY session as a
357  // Value.  Caller takes possession of the returned value.
358  base::Value* GetInfoAsValue() const;
359
360  // Indicates whether the session is being reused after having successfully
361  // used to send/receive data in the past.
362  bool IsReused() const;
363
364  // Returns true if the underlying transport socket ever had any reads or
365  // writes.
366  bool WasEverUsed() const {
367    return connection_->socket()->WasEverUsed();
368  }
369
370  // Returns the load timing information from the perspective of the given
371  // stream.  If it's not the first stream, the connection is considered reused
372  // for that stream.
373  //
374  // This uses a different notion of reuse than IsReused().  This function
375  // sets |socket_reused| to false only if |stream_id| is the ID of the first
376  // stream using the session.  IsReused(), on the other hand, indicates if the
377  // session has been used to send/receive data at all.
378  bool GetLoadTimingInfo(SpdyStreamId stream_id,
379                         LoadTimingInfo* load_timing_info) const;
380
381  // Returns true if session is not currently active
382  bool is_active() const {
383    return !active_streams_.empty() || !created_streams_.empty();
384  }
385
386  // Access to the number of active and pending streams.  These are primarily
387  // available for testing and diagnostics.
388  size_t num_active_streams() const { return active_streams_.size(); }
389  size_t num_unclaimed_pushed_streams() const {
390      return unclaimed_pushed_streams_.size();
391  }
392  size_t num_created_streams() const { return created_streams_.size(); }
393
394  size_t pending_create_stream_queue_size(RequestPriority priority) const {
395    DCHECK_GE(priority, MINIMUM_PRIORITY);
396    DCHECK_LE(priority, MAXIMUM_PRIORITY);
397    return pending_create_stream_queues_[priority].size();
398  }
399
400  // Returns the (version-dependent) flow control state.
401  FlowControlState flow_control_state() const {
402    return flow_control_state_;
403  }
404
405  // Returns the current |stream_initial_send_window_size_|.
406  int32 stream_initial_send_window_size() const {
407    return stream_initial_send_window_size_;
408  }
409
410  // Returns the current |stream_initial_recv_window_size_|.
411  int32 stream_initial_recv_window_size() const {
412    return stream_initial_recv_window_size_;
413  }
414
415  // Returns true if no stream in the session can send data due to
416  // session flow control.
417  bool IsSendStalled() const {
418    return
419        flow_control_state_ == FLOW_CONTROL_STREAM_AND_SESSION &&
420        session_send_window_size_ == 0;
421  }
422
423  const BoundNetLog& net_log() const { return net_log_; }
424
425  int GetPeerAddress(IPEndPoint* address) const;
426  int GetLocalAddress(IPEndPoint* address) const;
427
428  // Adds |alias| to set of aliases associated with this session.
429  void AddPooledAlias(const SpdySessionKey& alias_key);
430
431  // Returns the set of aliases associated with this session.
432  const std::set<SpdySessionKey>& pooled_aliases() const {
433    return pooled_aliases_;
434  }
435
436  SpdyMajorVersion GetProtocolVersion() const;
437
438  size_t GetDataFrameMinimumSize() const {
439    return buffered_spdy_framer_->GetDataFrameMinimumSize();
440  }
441
442  size_t GetControlFrameHeaderSize() const {
443    return buffered_spdy_framer_->GetControlFrameHeaderSize();
444  }
445
446  size_t GetFrameMinimumSize() const {
447    return buffered_spdy_framer_->GetFrameMinimumSize();
448  }
449
450  size_t GetFrameMaximumSize() const {
451    return buffered_spdy_framer_->GetFrameMaximumSize();
452  }
453
454  size_t GetDataFrameMaximumPayload() const {
455    return buffered_spdy_framer_->GetDataFrameMaximumPayload();
456  }
457
458  // Must be used only by |pool_|.
459  base::WeakPtr<SpdySession> GetWeakPtr();
460
461  // HigherLayeredPool implementation:
462  virtual bool CloseOneIdleConnection() OVERRIDE;
463
464 private:
465  friend class base::RefCounted<SpdySession>;
466  friend class SpdyStreamRequest;
467  friend class SpdySessionTest;
468
469  // Allow tests to access our innards for testing purposes.
470  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, ClientPing);
471  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, FailedPing);
472  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, GetActivePushStream);
473  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, DeleteExpiredPushStreams);
474  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, ProtocolNegotiation);
475  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, ClearSettings);
476  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, AdjustRecvWindowSize);
477  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, AdjustSendWindowSize);
478  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlInactiveStream);
479  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlNoReceiveLeaks);
480  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlNoSendLeaks);
481  FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlEndToEnd);
482
483  typedef std::deque<base::WeakPtr<SpdyStreamRequest> >
484      PendingStreamRequestQueue;
485
486  struct ActiveStreamInfo {
487    ActiveStreamInfo();
488    explicit ActiveStreamInfo(SpdyStream* stream);
489    ~ActiveStreamInfo();
490
491    SpdyStream* stream;
492    bool waiting_for_syn_reply;
493  };
494  typedef std::map<SpdyStreamId, ActiveStreamInfo> ActiveStreamMap;
495
496  struct PushedStreamInfo {
497    PushedStreamInfo();
498    PushedStreamInfo(SpdyStreamId stream_id, base::TimeTicks creation_time);
499    ~PushedStreamInfo();
500
501    SpdyStreamId stream_id;
502    base::TimeTicks creation_time;
503  };
504  typedef std::map<GURL, PushedStreamInfo> PushedStreamMap;
505
506  typedef std::set<SpdyStream*> CreatedStreamSet;
507
508  enum AvailabilityState {
509    // The session is available in its socket pool and can be used
510    // freely.
511    STATE_AVAILABLE,
512    // The session can process data on existing streams but will
513    // refuse to create new ones.
514    STATE_GOING_AWAY,
515    // The session has been closed, is waiting to be deleted, and will
516    // refuse to process any more data.
517    STATE_CLOSED
518  };
519
520  enum ReadState {
521    READ_STATE_DO_READ,
522    READ_STATE_DO_READ_COMPLETE,
523  };
524
525  enum WriteState {
526    // There is no in-flight write and the write queue is empty.
527    WRITE_STATE_IDLE,
528    WRITE_STATE_DO_WRITE,
529    WRITE_STATE_DO_WRITE_COMPLETE,
530  };
531
532  // The return value of DoCloseSession() describing what was done.
533  enum CloseSessionResult {
534    // The session was already closed so nothing was done.
535    SESSION_ALREADY_CLOSED,
536    // The session was moved into the closed state but was not removed
537    // from |pool_| (because we're in an IO loop).
538    SESSION_CLOSED_BUT_NOT_REMOVED,
539    // The session was moved into the closed state and removed from
540    // |pool_|.
541    SESSION_CLOSED_AND_REMOVED,
542  };
543
544  // Checks whether a stream for the given |url| can be created or
545  // retrieved from the set of unclaimed push streams. Returns OK if
546  // so. Otherwise, the session is closed and an error <
547  // ERR_IO_PENDING is returned.
548  Error TryAccessStream(const GURL& url);
549
550  // Called by SpdyStreamRequest to start a request to create a
551  // stream. If OK is returned, then |stream| will be filled in with a
552  // valid stream. If ERR_IO_PENDING is returned, then
553  // |request->OnRequestComplete{Success,Failure}()| will be called
554  // when the stream is created (unless it is cancelled). Otherwise,
555  // no stream is created and the error is returned.
556  int TryCreateStream(const base::WeakPtr<SpdyStreamRequest>& request,
557                      base::WeakPtr<SpdyStream>* stream);
558
559  // Actually create a stream into |stream|. Returns OK if successful;
560  // otherwise, returns an error and |stream| is not filled.
561  int CreateStream(const SpdyStreamRequest& request,
562                   base::WeakPtr<SpdyStream>* stream);
563
564  // Called by SpdyStreamRequest to remove |request| from the stream
565  // creation queue.
566  void CancelStreamRequest(const base::WeakPtr<SpdyStreamRequest>& request);
567
568  // Returns the next pending stream request to process, or NULL if
569  // there is none.
570  base::WeakPtr<SpdyStreamRequest> GetNextPendingStreamRequest();
571
572  // Called when there is room to create more streams (e.g., a stream
573  // was closed). Processes as many pending stream requests as
574  // possible.
575  void ProcessPendingStreamRequests();
576
577  // Close the stream pointed to by the given iterator. Note that that
578  // stream may hold the last reference to the session.
579  void CloseActiveStreamIterator(ActiveStreamMap::iterator it, int status);
580
581  // Close the stream pointed to by the given iterator. Note that that
582  // stream may hold the last reference to the session.
583  void CloseCreatedStreamIterator(CreatedStreamSet::iterator it, int status);
584
585  // Calls EnqueueResetStreamFrame() and then
586  // CloseActiveStreamIterator().
587  void ResetStreamIterator(ActiveStreamMap::iterator it,
588                           SpdyRstStreamStatus status,
589                           const std::string& description);
590
591  // Send a RST_STREAM frame with the given parameters. There should
592  // either be no active stream with the given ID, or that active
593  // stream should be closed shortly after this function is called.
594  //
595  // TODO(akalin): Rename this to EnqueueResetStreamFrame().
596  void EnqueueResetStreamFrame(SpdyStreamId stream_id,
597                               RequestPriority priority,
598                               SpdyRstStreamStatus status,
599                               const std::string& description);
600
601  // Calls DoReadLoop and then if |availability_state_| is
602  // STATE_CLOSED, calls RemoveFromPool().
603  //
604  // Use this function instead of DoReadLoop when posting a task to
605  // pump the read loop.
606  void PumpReadLoop(ReadState expected_read_state, int result);
607
608  // Advance the ReadState state machine. |expected_read_state| is the
609  // expected starting read state.
610  //
611  // This function must always be called via PumpReadLoop() except for
612  // from InitializeWithSocket().
613  int DoReadLoop(ReadState expected_read_state, int result);
614  // The implementations of the states of the ReadState state machine.
615  int DoRead();
616  int DoReadComplete(int result);
617
618  // Calls DoWriteLoop and then if |availability_state_| is
619  // STATE_CLOSED, calls RemoveFromPool().
620  //
621  // Use this function instead of DoWriteLoop when posting a task to
622  // pump the write loop.
623  void PumpWriteLoop(WriteState expected_write_state, int result);
624
625  // Advance the WriteState state machine. |expected_write_state| is
626  // the expected starting write state.
627  //
628  // This function must always be called via PumpWriteLoop().
629  int DoWriteLoop(WriteState expected_write_state, int result);
630  // The implementations of the states of the WriteState state machine.
631  int DoWrite();
632  int DoWriteComplete(int result);
633
634  // TODO(akalin): Rename the Send* and Write* functions below to
635  // Enqueue*.
636
637  // Send initial data. Called when a connection is successfully
638  // established in InitializeWithSocket() and
639  // |enable_sending_initial_data_| is true.
640  void SendInitialData();
641
642  // Helper method to send a SETTINGS frame.
643  void SendSettings(const SettingsMap& settings);
644
645  // Handle SETTING.  Either when we send settings, or when we receive a
646  // SETTINGS control frame, update our SpdySession accordingly.
647  void HandleSetting(uint32 id, uint32 value);
648
649  // Adjust the send window size of all ActiveStreams and PendingStreamRequests.
650  void UpdateStreamsSendWindowSize(int32 delta_window_size);
651
652  // Send the PING (preface-PING) frame.
653  void SendPrefacePingIfNoneInFlight();
654
655  // Send PING if there are no PINGs in flight and we haven't heard from server.
656  void SendPrefacePing();
657
658  // Send a single WINDOW_UPDATE frame.
659  void SendWindowUpdateFrame(SpdyStreamId stream_id, uint32 delta_window_size,
660                             RequestPriority priority);
661
662  // Send the PING frame.
663  void WritePingFrame(uint32 unique_id);
664
665  // Post a CheckPingStatus call after delay. Don't post if there is already
666  // CheckPingStatus running.
667  void PlanToCheckPingStatus();
668
669  // Check the status of the connection. It calls |CloseSessionOnError| if we
670  // haven't received any data in |kHungInterval| time period.
671  void CheckPingStatus(base::TimeTicks last_check_time);
672
673  // Get a new stream id.
674  int GetNewStreamId();
675
676  // Pushes the given frame with the given priority into the write
677  // queue for the session.
678  void EnqueueSessionWrite(RequestPriority priority,
679                           SpdyFrameType frame_type,
680                           scoped_ptr<SpdyFrame> frame);
681
682  // Puts |producer| associated with |stream| onto the write queue
683  // with the given priority.
684  void EnqueueWrite(RequestPriority priority,
685                    SpdyFrameType frame_type,
686                    scoped_ptr<SpdyBufferProducer> producer,
687                    const base::WeakPtr<SpdyStream>& stream);
688
689  // Inserts a newly-created stream into |created_streams_|.
690  void InsertCreatedStream(scoped_ptr<SpdyStream> stream);
691
692  // Activates |stream| (which must be in |created_streams_|) by
693  // assigning it an ID and returns it.
694  scoped_ptr<SpdyStream> ActivateCreatedStream(SpdyStream* stream);
695
696  // Inserts a newly-activated stream into |active_streams_|.
697  void InsertActivatedStream(scoped_ptr<SpdyStream> stream);
698
699  // Remove all internal references to |stream|, call OnClose() on it,
700  // and process any pending stream requests before deleting it.  Note
701  // that |stream| may hold the last reference to the session.
702  void DeleteStream(scoped_ptr<SpdyStream> stream, int status);
703
704  // Check if we have a pending pushed-stream for this url
705  // Returns the stream if found (and returns it from the pending
706  // list). Returns NULL otherwise.
707  base::WeakPtr<SpdyStream> GetActivePushStream(const GURL& url);
708
709  // Delegates to |stream->OnInitialResponseHeadersReceived()|. If an
710  // error is returned, the last reference to |this| may have been
711  // released.
712  int OnInitialResponseHeadersReceived(const SpdyHeaderBlock& response_headers,
713                                       base::Time response_time,
714                                       base::TimeTicks recv_first_byte_time,
715                                       SpdyStream* stream);
716
717  void RecordPingRTTHistogram(base::TimeDelta duration);
718  void RecordHistograms();
719  void RecordProtocolErrorHistogram(SpdyProtocolErrorDetails details);
720
721  // DCHECKs that |availability_state_| >= STATE_GOING_AWAY, that
722  // there are no pending stream creation requests, and that there are
723  // no created streams.
724  void DcheckGoingAway() const;
725
726  // Calls DcheckGoingAway(), then DCHECKs that |availability_state_|
727  // == STATE_CLOSED, |error_on_close_| has a valid value, that there
728  // are no active streams or unclaimed pushed streams, and that the
729  // write queue is empty.
730  void DcheckClosed() const;
731
732  // Closes all active streams with stream id's greater than
733  // |last_good_stream_id|, as well as any created or pending
734  // streams. Must be called only when |availability_state_| >=
735  // STATE_GOING_AWAY. After this function, DcheckGoingAway() will
736  // pass. May be called multiple times.
737  void StartGoingAway(SpdyStreamId last_good_stream_id, Error status);
738
739  // Must be called only when going away (i.e., DcheckGoingAway()
740  // passes). If there are no more active streams and the session
741  // isn't closed yet, close it.
742  void MaybeFinishGoingAway();
743
744  // If the stream is already closed, does nothing. Otherwise, moves
745  // the session to a closed state. Then, if we're in an IO loop,
746  // returns (as the IO loop will do the pool removal itself when its
747  // done). Otherwise, also removes |this| from |pool_|. The returned
748  // result describes what was done.
749  CloseSessionResult DoCloseSession(Error err, const std::string& description);
750
751  // Remove this session from its pool, which must exist. Must be
752  // called only when the session is closed.
753  //
754  // Must be called only via Pump{Read,Write}Loop() or
755  // DoCloseSession().
756  void RemoveFromPool();
757
758  // Called right before closing a (possibly-inactive) stream for a
759  // reason other than being requested to by the stream.
760  void LogAbandonedStream(SpdyStream* stream, Error status);
761
762  // Called right before closing an active stream for a reason other
763  // than being requested to by the stream.
764  void LogAbandonedActiveStream(ActiveStreamMap::const_iterator it,
765                                Error status);
766
767  // Invokes a user callback for stream creation.  We provide this method so it
768  // can be deferred to the MessageLoop, so we avoid re-entrancy problems.
769  void CompleteStreamRequest(
770      const base::WeakPtr<SpdyStreamRequest>& pending_request);
771
772  // Remove old unclaimed pushed streams.
773  void DeleteExpiredPushedStreams();
774
775  // BufferedSpdyFramerVisitorInterface:
776  virtual void OnError(SpdyFramer::SpdyError error_code) OVERRIDE;
777  virtual void OnStreamError(SpdyStreamId stream_id,
778                             const std::string& description) OVERRIDE;
779  virtual void OnPing(uint32 unique_id) OVERRIDE;
780  virtual void OnRstStream(SpdyStreamId stream_id,
781                           SpdyRstStreamStatus status) OVERRIDE;
782  virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
783                        SpdyGoAwayStatus status) OVERRIDE;
784  virtual void OnDataFrameHeader(SpdyStreamId stream_id,
785                                 size_t length,
786                                 bool fin) OVERRIDE;
787  virtual void OnStreamFrameData(SpdyStreamId stream_id,
788                                 const char* data,
789                                 size_t len,
790                                 bool fin) OVERRIDE;
791  virtual void OnSettings(bool clear_persisted) OVERRIDE;
792  virtual void OnSetting(
793      SpdySettingsIds id, uint8 flags, uint32 value) OVERRIDE;
794  virtual void OnWindowUpdate(SpdyStreamId stream_id,
795                              uint32 delta_window_size) OVERRIDE;
796  virtual void OnPushPromise(SpdyStreamId stream_id,
797                             SpdyStreamId promised_stream_id) OVERRIDE;
798  virtual void OnSynStream(SpdyStreamId stream_id,
799                           SpdyStreamId associated_stream_id,
800                           SpdyPriority priority,
801                           uint8 credential_slot,
802                           bool fin,
803                           bool unidirectional,
804                           const SpdyHeaderBlock& headers) OVERRIDE;
805  virtual void OnSynReply(
806      SpdyStreamId stream_id,
807      bool fin,
808      const SpdyHeaderBlock& headers) OVERRIDE;
809  virtual void OnHeaders(
810      SpdyStreamId stream_id,
811      bool fin,
812      const SpdyHeaderBlock& headers) OVERRIDE;
813
814  // SpdyFramerDebugVisitorInterface
815  virtual void OnSendCompressedFrame(
816      SpdyStreamId stream_id,
817      SpdyFrameType type,
818      size_t payload_len,
819      size_t frame_len) OVERRIDE;
820  virtual void OnReceiveCompressedFrame(
821      SpdyStreamId stream_id,
822      SpdyFrameType type,
823      size_t frame_len) OVERRIDE;
824
825  // Called when bytes are consumed from a SpdyBuffer for a DATA frame
826  // that is to be written or is being written. Increases the send
827  // window size accordingly if some or all of the SpdyBuffer is being
828  // discarded.
829  //
830  // If session flow control is turned off, this must not be called.
831  void OnWriteBufferConsumed(size_t frame_payload_size,
832                             size_t consume_size,
833                             SpdyBuffer::ConsumeSource consume_source);
834
835  // Called by OnWindowUpdate() (which is in turn called by the
836  // framer) to increase this session's send window size by
837  // |delta_window_size| from a WINDOW_UPDATE frome, which must be at
838  // least 1. If |delta_window_size| would cause this session's send
839  // window size to overflow, does nothing.
840  //
841  // If session flow control is turned off, this must not be called.
842  void IncreaseSendWindowSize(int32 delta_window_size);
843
844  // If session flow control is turned on, called by CreateDataFrame()
845  // (which is in turn called by a stream) to decrease this session's
846  // send window size by |delta_window_size|, which must be at least 1
847  // and at most kMaxSpdyFrameChunkSize.  |delta_window_size| must not
848  // cause this session's send window size to go negative.
849  //
850  // If session flow control is turned off, this must not be called.
851  void DecreaseSendWindowSize(int32 delta_window_size);
852
853  // Called when bytes are consumed by the delegate from a SpdyBuffer
854  // containing received data. Increases the receive window size
855  // accordingly.
856  //
857  // If session flow control is turned off, this must not be called.
858  void OnReadBufferConsumed(size_t consume_size,
859                            SpdyBuffer::ConsumeSource consume_source);
860
861  // Called by OnReadBufferConsume to increase this session's receive
862  // window size by |delta_window_size|, which must be at least 1 and
863  // must not cause this session's receive window size to overflow,
864  // possibly also sending a WINDOW_UPDATE frame. Also called during
865  // initialization to set the initial receive window size.
866  //
867  // If session flow control is turned off, this must not be called.
868  void IncreaseRecvWindowSize(int32 delta_window_size);
869
870  // Called by OnStreamFrameData (which is in turn called by the
871  // framer) to decrease this session's receive window size by
872  // |delta_window_size|, which must be at least 1 and must not cause
873  // this session's receive window size to go negative.
874  //
875  // If session flow control is turned off, this must not be called.
876  void DecreaseRecvWindowSize(int32 delta_window_size);
877
878  // Queue a send-stalled stream for possibly resuming once we're not
879  // send-stalled anymore.
880  void QueueSendStalledStream(const SpdyStream& stream);
881
882  // Go through the queue of send-stalled streams and try to resume as
883  // many as possible.
884  void ResumeSendStalledStreams();
885
886  // Returns the next stream to possibly resume, or 0 if the queue is
887  // empty.
888  SpdyStreamId PopStreamToPossiblyResume();
889
890  // --------------------------
891  // Helper methods for testing
892  // --------------------------
893
894  void set_connection_at_risk_of_loss_time(base::TimeDelta duration) {
895    connection_at_risk_of_loss_time_ = duration;
896  }
897
898  void set_hung_interval(base::TimeDelta duration) {
899    hung_interval_ = duration;
900  }
901
902  int64 pings_in_flight() const { return pings_in_flight_; }
903
904  uint32 next_ping_id() const { return next_ping_id_; }
905
906  base::TimeTicks last_activity_time() const { return last_activity_time_; }
907
908  bool check_ping_status_pending() const { return check_ping_status_pending_; }
909
910  size_t max_concurrent_streams() const { return max_concurrent_streams_; }
911
912  // Returns the SSLClientSocket that this SPDY session sits on top of,
913  // or NULL, if the transport is not SSL.
914  SSLClientSocket* GetSSLClientSocket() const;
915
916  // Used for posting asynchronous IO tasks.  We use this even though
917  // SpdySession is refcounted because we don't need to keep the SpdySession
918  // alive if the last reference is within a RunnableMethod.  Just revoke the
919  // method.
920  base::WeakPtrFactory<SpdySession> weak_factory_;
921
922  // Whether Do{Read,Write}Loop() is in the call stack. Useful for
923  // making sure we don't destroy ourselves prematurely in that case.
924  bool in_io_loop_;
925
926  // The key used to identify this session.
927  const SpdySessionKey spdy_session_key_;
928
929  // Set set of SpdySessionKeys for which this session has serviced
930  // requests.
931  std::set<SpdySessionKey> pooled_aliases_;
932
933  // |pool_| owns us, therefore its lifetime must exceed ours.  We set
934  // this to NULL after we are removed from the pool.
935  SpdySessionPool* pool_;
936  const base::WeakPtr<HttpServerProperties> http_server_properties_;
937
938  // The socket handle for this session.
939  scoped_ptr<ClientSocketHandle> connection_;
940
941  // The read buffer used to read data from the socket.
942  scoped_refptr<IOBuffer> read_buffer_;
943
944  int stream_hi_water_mark_;  // The next stream id to use.
945
946  // Queue, for each priority, of pending stream requests that have
947  // not yet been satisfied.
948  PendingStreamRequestQueue pending_create_stream_queues_[NUM_PRIORITIES];
949
950  // Map from stream id to all active streams.  Streams are active in the sense
951  // that they have a consumer (typically SpdyNetworkTransaction and regardless
952  // of whether or not there is currently any ongoing IO [might be waiting for
953  // the server to start pushing the stream]) or there are still network events
954  // incoming even though the consumer has already gone away (cancellation).
955  //
956  // |active_streams_| owns all its SpdyStream objects.
957  //
958  // TODO(willchan): Perhaps we should separate out cancelled streams and move
959  // them into a separate ActiveStreamMap, and not deliver network events to
960  // them?
961  ActiveStreamMap active_streams_;
962
963  // (Bijective) map from the URL to the ID of the streams that have
964  // already started to be pushed by the server, but do not have
965  // consumers yet. Contains a subset of |active_streams_|.
966  PushedStreamMap unclaimed_pushed_streams_;
967
968  // Set of all created streams but that have not yet sent any frames.
969  //
970  // |created_streams_| owns all its SpdyStream objects.
971  CreatedStreamSet created_streams_;
972
973  // The write queue.
974  SpdyWriteQueue write_queue_;
975
976  // Data for the frame we are currently sending.
977
978  // The buffer we're currently writing.
979  scoped_ptr<SpdyBuffer> in_flight_write_;
980  // The type of the frame in |in_flight_write_|.
981  SpdyFrameType in_flight_write_frame_type_;
982  // The size of the frame in |in_flight_write_|.
983  size_t in_flight_write_frame_size_;
984  // The stream to notify when |in_flight_write_| has been written to
985  // the socket completely.
986  base::WeakPtr<SpdyStream> in_flight_write_stream_;
987
988  // Flag if we're using an SSL connection for this SpdySession.
989  bool is_secure_;
990
991  // Certificate error code when using a secure connection.
992  int certificate_error_code_;
993
994  // Spdy Frame state.
995  scoped_ptr<BufferedSpdyFramer> buffered_spdy_framer_;
996
997  // The state variables.
998  AvailabilityState availability_state_;
999  ReadState read_state_;
1000  WriteState write_state_;
1001
1002  // If the session was closed (i.e., |availability_state_| is
1003  // STATE_CLOSED), then |error_on_close_| holds the error with which
1004  // it was closed, which is < ERR_IO_PENDING. Otherwise, it is set to
1005  // OK.
1006  Error error_on_close_;
1007
1008  // Limits
1009  size_t max_concurrent_streams_;  // 0 if no limit
1010  size_t max_concurrent_streams_limit_;
1011
1012  // Some statistics counters for the session.
1013  int streams_initiated_count_;
1014  int streams_pushed_count_;
1015  int streams_pushed_and_claimed_count_;
1016  int streams_abandoned_count_;
1017
1018  // |total_bytes_received_| keeps track of all the bytes read by the
1019  // SpdySession. It is used by the |Net.SpdySettingsCwnd...| histograms.
1020  int total_bytes_received_;
1021
1022  bool sent_settings_;      // Did this session send settings when it started.
1023  bool received_settings_;  // Did this session receive at least one settings
1024                            // frame.
1025  int stalled_streams_;     // Count of streams that were ever stalled.
1026
1027  // Count of all pings on the wire, for which we have not gotten a response.
1028  int64 pings_in_flight_;
1029
1030  // This is the next ping_id (unique_id) to be sent in PING frame.
1031  uint32 next_ping_id_;
1032
1033  // This is the last time we have sent a PING.
1034  base::TimeTicks last_ping_sent_time_;
1035
1036  // This is the last time we had activity in the session.
1037  base::TimeTicks last_activity_time_;
1038
1039  // This is the length of the last compressed frame.
1040  size_t last_compressed_frame_len_;
1041
1042  // This is the next time that unclaimed push streams should be checked for
1043  // expirations.
1044  base::TimeTicks next_unclaimed_push_stream_sweep_time_;
1045
1046  // Indicate if we have already scheduled a delayed task to check the ping
1047  // status.
1048  bool check_ping_status_pending_;
1049
1050  // Whether to send the (HTTP/2) connection header prefix.
1051  bool send_connection_header_prefix_;
1052
1053  // The (version-dependent) flow control state.
1054  FlowControlState flow_control_state_;
1055
1056  // Initial send window size for this session's streams. Can be
1057  // changed by an arriving SETTINGS frame. Newly created streams use
1058  // this value for the initial send window size.
1059  int32 stream_initial_send_window_size_;
1060
1061  // Initial receive window size for this session's streams. There are
1062  // plans to add a command line switch that would cause a SETTINGS
1063  // frame with window size announcement to be sent on startup. Newly
1064  // created streams will use this value for the initial receive
1065  // window size.
1066  int32 stream_initial_recv_window_size_;
1067
1068  // Session flow control variables. All zero unless session flow
1069  // control is turned on.
1070  int32 session_send_window_size_;
1071  int32 session_recv_window_size_;
1072  int32 session_unacked_recv_window_bytes_;
1073
1074  // A queue of stream IDs that have been send-stalled at some point
1075  // in the past.
1076  std::deque<SpdyStreamId> stream_send_unstall_queue_[NUM_PRIORITIES];
1077
1078  BoundNetLog net_log_;
1079
1080  // Outside of tests, these should always be true.
1081  bool verify_domain_authentication_;
1082  bool enable_sending_initial_data_;
1083  bool enable_compression_;
1084  bool enable_ping_based_connection_checking_;
1085
1086  // The SPDY protocol used. Always between kProtoSPDYMinimumVersion and
1087  // kProtoSPDYMaximumVersion.
1088  NextProto protocol_;
1089
1090  // |connection_at_risk_of_loss_time_| is an optimization to avoid sending
1091  // wasteful preface pings (when we just got some data).
1092  //
1093  // If it is zero (the most conservative figure), then we always send the
1094  // preface ping (when none are in flight).
1095  //
1096  // It is common for TCP/IP sessions to time out in about 3-5 minutes.
1097  // Certainly if it has been more than 3 minutes, we do want to send a preface
1098  // ping.
1099  //
1100  // We don't think any connection will time out in under about 10 seconds. So
1101  // this might as well be set to something conservative like 10 seconds. Later,
1102  // we could adjust it to send fewer pings perhaps.
1103  base::TimeDelta connection_at_risk_of_loss_time_;
1104
1105  // The amount of time that we are willing to tolerate with no activity (of any
1106  // form), while there is a ping in flight, before we declare the connection to
1107  // be hung. TODO(rtenneti): When hung, instead of resetting connection, race
1108  // to build a new connection, and see if that completes before we (finally)
1109  // get a PING response (http://crbug.com/127812).
1110  base::TimeDelta hung_interval_;
1111
1112  // This SPDY proxy is allowed to push resources from origins that are
1113  // different from those of their associated streams.
1114  HostPortPair trusted_spdy_proxy_;
1115
1116  TimeFunc time_func_;
1117};
1118
1119}  // namespace net
1120
1121#endif  // NET_SPDY_SPDY_SESSION_H_
1122