7deb189dc8bc9f57176eb213002f33e7399c6485
[platform/framework/web/crosswalk.git] / src / net / quic / quic_session.h
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 // A QuicSession, which demuxes a single connection to individual streams.
6
7 #ifndef NET_QUIC_QUIC_SESSION_H_
8 #define NET_QUIC_QUIC_SESSION_H_
9
10 #include <vector>
11
12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/quic_connection.h"
17 #include "net/quic/quic_crypto_stream.h"
18 #include "net/quic/quic_data_stream.h"
19 #include "net/quic/quic_headers_stream.h"
20 #include "net/quic/quic_packet_creator.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_spdy_compressor.h"
23 #include "net/quic/quic_spdy_decompressor.h"
24 #include "net/quic/quic_write_blocked_list.h"
25 #include "net/quic/reliable_quic_stream.h"
26
27 namespace net {
28
29 class QuicCryptoStream;
30 class ReliableQuicStream;
31 class SSLInfo;
32 class VisitorShim;
33
34 namespace test {
35 class QuicSessionPeer;
36 }  // namespace test
37
38 class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
39  public:
40   // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
41   enum CryptoHandshakeEvent {
42     // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
43     // sent by a client and that subsequent packets will be encrypted. (Client
44     // only.)
45     ENCRYPTION_FIRST_ESTABLISHED,
46     // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
47     // the server and thus the encryption key has been updated. Therefore the
48     // connection should resend any packets that were sent under
49     // ENCRYPTION_INITIAL. (Client only.)
50     ENCRYPTION_REESTABLISHED,
51     // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
52     // our handshake. In a server it indicates that a full, valid client hello
53     // has been received. (Client and server.)
54     HANDSHAKE_CONFIRMED,
55   };
56
57   QuicSession(QuicConnection* connection,
58               const QuicConfig& config);
59
60   virtual ~QuicSession();
61
62   // QuicConnectionVisitorInterface methods:
63   virtual bool OnStreamFrames(
64       const std::vector<QuicStreamFrame>& frames) OVERRIDE;
65   virtual void OnRstStream(const QuicRstStreamFrame& frame) OVERRIDE;
66   virtual void OnGoAway(const QuicGoAwayFrame& frame) OVERRIDE;
67   virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
68   virtual void OnWriteBlocked() OVERRIDE {}
69   virtual void OnSuccessfulVersionNegotiation(
70       const QuicVersion& version) OVERRIDE {}
71   virtual bool OnCanWrite() OVERRIDE;
72   virtual bool HasPendingHandshake() const OVERRIDE;
73
74   // Called by the headers stream when headers have been received for a stream.
75   virtual void OnStreamHeaders(QuicStreamId stream_id,
76                                base::StringPiece headers_data);
77   // Called by the headers stream when headers with a priority have been
78   // received for this stream.  This method will only be called for server
79   // streams.
80   virtual void OnStreamHeadersPriority(QuicStreamId stream_id,
81                                        QuicPriority priority);
82   // Called by the headers stream when headers have been completely received
83   // for a stream.  |fin| will be true if the fin flag was set in the headers
84   // frame.
85   virtual void OnStreamHeadersComplete(QuicStreamId stream_id,
86                                        bool fin,
87                                        size_t frame_len);
88
89   // Called by streams when they want to write data to the peer.
90   // Returns a pair with the number of bytes consumed from data, and a boolean
91   // indicating if the fin bit was consumed.  This does not indicate the data
92   // has been sent on the wire: it may have been turned into a packet and queued
93   // if the socket was unexpectedly blocked.
94   // If provided, |ack_notifier_delegate| will be registered to be notified when
95   // we have seen ACKs for all packets resulting from this call. Not owned by
96   // this class.
97   virtual QuicConsumedData WritevData(
98       QuicStreamId id,
99       const struct iovec* iov,
100       int iov_count,
101       QuicStreamOffset offset,
102       bool fin,
103       QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
104
105   // Writes |headers| for the stream |id| to the dedicated headers stream.
106   // If |fin| is true, then no more data will be sent for the stream |id|.
107   size_t WriteHeaders(QuicStreamId id,
108                       const SpdyHeaderBlock& headers,
109                       bool fin);
110
111   // Called by streams when they want to close the stream in both directions.
112   virtual void SendRstStream(QuicStreamId id,
113                              QuicRstStreamErrorCode error,
114                              QuicStreamOffset bytes_written);
115
116   // Called when the session wants to go away and not accept any new streams.
117   void SendGoAway(QuicErrorCode error_code, const std::string& reason);
118
119   // Removes the stream associated with 'stream_id' from the active stream map.
120   virtual void CloseStream(QuicStreamId stream_id);
121
122   // Returns true if outgoing packets will be encrypted, even if the server
123   // hasn't confirmed the handshake yet.
124   virtual bool IsEncryptionEstablished();
125
126   // For a client, returns true if the server has confirmed our handshake. For
127   // a server, returns true if a full, valid client hello has been received.
128   virtual bool IsCryptoHandshakeConfirmed();
129
130   // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
131   virtual void OnConfigNegotiated();
132
133   // Called by the QuicCryptoStream when the handshake enters a new state.
134   //
135   // Clients will call this function in the order:
136   //   ENCRYPTION_FIRST_ESTABLISHED
137   //   zero or more ENCRYPTION_REESTABLISHED
138   //   HANDSHAKE_CONFIRMED
139   //
140   // Servers will simply call it once with HANDSHAKE_CONFIRMED.
141   virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
142
143   // Called by the QuicCryptoStream when a handshake message is sent.
144   virtual void OnCryptoHandshakeMessageSent(
145       const CryptoHandshakeMessage& message);
146
147   // Called by the QuicCryptoStream when a handshake message is received.
148   virtual void OnCryptoHandshakeMessageReceived(
149       const CryptoHandshakeMessage& message);
150
151   // Returns mutable config for this session. Returned config is owned
152   // by QuicSession.
153   QuicConfig* config();
154
155   // Returns true if the stream existed previously and has been closed.
156   // Returns false if the stream is still active or if the stream has
157   // not yet been created.
158   bool IsClosedStream(QuicStreamId id);
159
160   QuicConnection* connection() { return connection_.get(); }
161   const QuicConnection* connection() const { return connection_.get(); }
162   size_t num_active_requests() const { return stream_map_.size(); }
163   const IPEndPoint& peer_address() const {
164     return connection_->peer_address();
165   }
166   QuicGuid guid() const { return connection_->guid(); }
167
168   QuicPacketCreator::Options* options() { return connection()->options(); }
169
170   // Returns the number of currently open streams, including those which have
171   // been implicitly created.
172   virtual size_t GetNumOpenStreams() const;
173
174   void MarkWriteBlocked(QuicStreamId id, QuicPriority priority);
175
176   // Returns true if the session has data to be sent, either queued in the
177   // connection, or in a write-blocked stream.
178   bool HasDataToWrite() const;
179
180   // Marks that |stream_id| is blocked waiting to decompress the
181   // headers identified by |decompression_id|.
182   void MarkDecompressionBlocked(QuicHeaderId decompression_id,
183                                 QuicStreamId stream_id);
184
185   bool goaway_received() const {
186     return goaway_received_;
187   }
188
189   bool goaway_sent() const {
190     return goaway_sent_;
191   }
192
193   QuicSpdyDecompressor* decompressor() { return &decompressor_; }
194   QuicSpdyCompressor* compressor() { return &compressor_; }
195
196   // Gets the SSL connection information.
197   virtual bool GetSSLInfo(SSLInfo* ssl_info);
198
199   QuicErrorCode error() const { return error_; }
200
201   bool is_server() const { return connection_->is_server(); }
202
203  protected:
204   typedef base::hash_map<QuicStreamId, QuicDataStream*> DataStreamMap;
205
206   // Creates a new stream, owned by the caller, to handle a peer-initiated
207   // stream.  Returns NULL and does error handling if the stream can not be
208   // created.
209   virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) = 0;
210
211   // Create a new stream, owned by the caller, to handle a locally-initiated
212   // stream.  Returns NULL if max streams have already been opened.
213   virtual QuicDataStream* CreateOutgoingDataStream() = 0;
214
215   // Return the reserved crypto stream.
216   virtual QuicCryptoStream* GetCryptoStream() = 0;
217
218   // Adds 'stream' to the active stream map.
219   virtual void ActivateStream(QuicDataStream* stream);
220
221   // Returns the stream id for a new stream.
222   QuicStreamId GetNextStreamId();
223
224   QuicDataStream* GetIncomingDataStream(QuicStreamId stream_id);
225
226   QuicDataStream* GetDataStream(const QuicStreamId stream_id);
227
228   ReliableQuicStream* GetStream(const QuicStreamId stream_id);
229
230   // This is called after every call other than OnConnectionClose from the
231   // QuicConnectionVisitor to allow post-processing once the work has been done.
232   // In this case, it deletes streams given that it's safe to do so (no other
233   // operations are being done on the streams at this time)
234   virtual void PostProcessAfterData();
235
236   base::hash_map<QuicStreamId, QuicDataStream*>* streams() {
237     return &stream_map_;
238   }
239
240   const base::hash_map<QuicStreamId, QuicDataStream*>* streams() const {
241     return &stream_map_;
242   }
243
244   std::vector<QuicDataStream*>* closed_streams() { return &closed_streams_; }
245
246   size_t get_max_open_streams() const {
247     return max_open_streams_;
248   }
249
250  private:
251   friend class test::QuicSessionPeer;
252   friend class VisitorShim;
253
254   // Performs the work required to close |stream_id|.  If |locally_reset|
255   // then the stream has been reset by this endpoint, not by the peer.  This
256   // means the stream may become a zombie stream which needs to stay
257   // around until headers have been decompressed.
258   void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
259
260   // Adds |stream_id| to the zobmie stream map, closing the oldest
261   // zombie stream if the set is full.
262   void AddZombieStream(QuicStreamId stream_id);
263
264   // Closes the zombie stream |stream_id| and removes it from the zombie
265   // stream map.
266   void CloseZombieStream(QuicStreamId stream_id);
267
268   // Adds |stream_id| to the prematurely closed stream map, removing the
269   // oldest prematurely closed stream if the set is full.
270   void AddPrematurelyClosedStream(QuicStreamId stream_id);
271
272   scoped_ptr<QuicConnection> connection_;
273
274   scoped_ptr<QuicHeadersStream> headers_stream_;
275
276   // Tracks the last 20 streams which closed without decompressing headers.
277   // This is for best-effort detection of an unrecoverable compression context.
278   // Ideally this would be a linked_hash_set as the boolean is unused.
279   linked_hash_map<QuicStreamId, bool> prematurely_closed_streams_;
280
281   // Streams which have been locally reset before decompressing headers
282   // from the peer.  These streams need to stay open long enough to
283   // process any headers from the peer.
284   // Ideally this would be a linked_hash_set as the boolean is unused.
285   linked_hash_map<QuicStreamId, bool> zombie_streams_;
286
287   // A shim to stand between the connection and the session, to handle stream
288   // deletions.
289   scoped_ptr<VisitorShim> visitor_shim_;
290
291   std::vector<QuicDataStream*> closed_streams_;
292
293   QuicSpdyDecompressor decompressor_;
294   QuicSpdyCompressor compressor_;
295
296   QuicConfig config_;
297
298   // Returns the maximum number of streams this connection can open.
299   size_t max_open_streams_;
300
301   // Map from StreamId to pointers to streams that are owned by the caller.
302   DataStreamMap stream_map_;
303   QuicStreamId next_stream_id_;
304
305   // Set of stream ids that have been "implicitly created" by receipt
306   // of a stream id larger than the next expected stream id.
307   base::hash_set<QuicStreamId> implicitly_created_streams_;
308
309   // A list of streams which need to write more data.
310   QuicWriteBlockedList write_blocked_streams_;
311
312   // A map of headers waiting to be compressed, and the streams
313   // they are associated with.
314   map<uint32, QuicStreamId> decompression_blocked_streams_;
315
316   QuicStreamId largest_peer_created_stream_id_;
317
318   // The latched error with which the connection was closed.
319   QuicErrorCode error_;
320
321   // Whether a GoAway has been received.
322   bool goaway_received_;
323   // Whether a GoAway has been sent.
324   bool goaway_sent_;
325
326   // Indicate if there is pending data for the crypto stream.
327   bool has_pending_handshake_;
328
329   DISALLOW_COPY_AND_ASSIGN(QuicSession);
330 };
331
332 }  // namespace net
333
334 #endif  // NET_QUIC_QUIC_SESSION_H_