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