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.
5 // A client specific QuicSession subclass. This class owns the underlying
6 // QuicConnection and QuicConnectionHelper objects. The connection stores
7 // a non-owning pointer to the helper so this session needs to ensure that
8 // the helper outlives the connection.
10 #ifndef NET_QUIC_QUIC_CLIENT_SESSION_H_
11 #define NET_QUIC_QUIC_CLIENT_SESSION_H_
15 #include "base/basictypes.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "net/base/completion_callback.h"
19 #include "net/proxy/proxy_server.h"
20 #include "net/quic/quic_connection_logger.h"
21 #include "net/quic/quic_crypto_client_stream.h"
22 #include "net/quic/quic_protocol.h"
23 #include "net/quic/quic_reliable_client_stream.h"
24 #include "net/quic/quic_session.h"
28 class DatagramClientSocket;
29 class QuicConnectionHelper;
30 class QuicCryptoClientStreamFactory;
31 class QuicDefaultPacketWriter;
32 class QuicStreamFactory;
36 class QuicClientSessionPeer;
39 class NET_EXPORT_PRIVATE QuicClientSession : public QuicSession {
41 // An interface for observing events on a session.
42 class NET_EXPORT_PRIVATE Observer {
44 virtual ~Observer() {}
45 virtual void OnCryptoHandshakeConfirmed() = 0;
46 virtual void OnSessionClosed(int error) = 0;
49 // A helper class used to manage a request to create a stream.
50 class NET_EXPORT_PRIVATE StreamRequest {
55 // Starts a request to create a stream. If OK is returned, then
56 // |stream| will be updated with the newly created stream. If
57 // ERR_IO_PENDING is returned, then when the request is eventuallly
58 // complete |callback| will be called.
59 int StartRequest(const base::WeakPtr<QuicClientSession>& session,
60 QuicReliableClientStream** stream,
61 const CompletionCallback& callback);
63 // Cancels any pending stream creation request. May be called
68 friend class QuicClientSession;
70 // Called by |session_| for an asynchronous request when the stream
71 // request has finished successfully.
72 void OnRequestCompleteSuccess(QuicReliableClientStream* stream);
74 // Called by |session_| for an asynchronous request when the stream
75 // request has finished with an error. Also called with ERR_ABORTED
76 // if |session_| is destroyed while the stream request is still pending.
77 void OnRequestCompleteFailure(int rv);
79 base::WeakPtr<QuicClientSession> session_;
80 CompletionCallback callback_;
81 QuicReliableClientStream** stream_;
83 DISALLOW_COPY_AND_ASSIGN(StreamRequest);
86 // Constructs a new session which will own |connection| and |helper|, but
87 // not |stream_factory|, which must outlive this session.
88 // TODO(rch): decouple the factory from the session via a Delegate interface.
89 QuicClientSession(QuicConnection* connection,
90 scoped_ptr<DatagramClientSocket> socket,
91 scoped_ptr<QuicDefaultPacketWriter> writer,
92 QuicStreamFactory* stream_factory,
93 QuicCryptoClientStreamFactory* crypto_client_stream_factory,
94 const std::string& server_hostname,
95 const QuicConfig& config,
96 QuicCryptoClientConfig* crypto_config,
99 virtual ~QuicClientSession();
101 void AddObserver(Observer* observer);
102 void RemoveObserver(Observer* observer);
104 // Attempts to create a new stream. If the stream can be
105 // created immediately, returns OK. If the open stream limit
106 // has been reached, returns ERR_IO_PENDING, and |request|
107 // will be added to the stream requets queue and will
108 // be completed asynchronously.
109 // TODO(rch): remove |stream| from this and use setter on |request|
110 // and fix in spdy too.
111 int TryCreateStream(StreamRequest* request,
112 QuicReliableClientStream** stream);
114 // Cancels the pending stream creation request.
115 void CancelRequest(StreamRequest* request);
117 // QuicSession methods:
118 virtual bool OnStreamFrames(
119 const std::vector<QuicStreamFrame>& frames) OVERRIDE;
120 virtual QuicReliableClientStream* CreateOutgoingDataStream() OVERRIDE;
121 virtual QuicCryptoClientStream* GetCryptoStream() OVERRIDE;
122 virtual void CloseStream(QuicStreamId stream_id) OVERRIDE;
123 virtual void SendRstStream(QuicStreamId id,
124 QuicRstStreamErrorCode error,
125 QuicStreamOffset bytes_written) OVERRIDE;
126 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) OVERRIDE;
127 virtual void OnCryptoHandshakeMessageSent(
128 const CryptoHandshakeMessage& message) OVERRIDE;
129 virtual void OnCryptoHandshakeMessageReceived(
130 const CryptoHandshakeMessage& message) OVERRIDE;
131 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
133 // QuicConnectionVisitorInterface methods:
134 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
135 virtual void OnSuccessfulVersionNegotiation(
136 const QuicVersion& version) OVERRIDE;
138 // Performs a crypto handshake with the server.
139 int CryptoConnect(bool require_confirmation,
140 const CompletionCallback& callback);
142 // Causes the QuicConnectionHelper to start reading from the socket
143 // and passing the data along to the QuicConnection.
146 // Close the session because of |error| and notifies the factory
147 // that this session has been closed, which will delete the session.
148 void CloseSessionOnError(int error);
150 base::Value* GetInfoAsValue(const std::set<HostPortProxyPair>& aliases) const;
152 const BoundNetLog& net_log() const { return net_log_; }
154 base::WeakPtr<QuicClientSession> GetWeakPtr();
156 // Returns the number of client hello messages that have been sent on the
157 // crypto stream. If the handshake has completed then this is one greater
158 // than the number of round-trips needed for the handshake.
159 int GetNumSentClientHellos() const;
161 // Returns true if |hostname| may be pooled onto this session. If this
162 // is a secure QUIC session, then |hostname| must match the certificate
163 // presented during the handshake.
164 bool CanPool(const std::string& hostname) const;
167 // QuicSession methods:
168 virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) OVERRIDE;
171 friend class test::QuicClientSessionPeer;
173 typedef std::set<Observer*> ObserverSet;
174 typedef std::list<StreamRequest*> StreamRequestQueue;
176 QuicReliableClientStream* CreateOutgoingReliableStreamImpl();
177 // A completion callback invoked when a read completes.
178 void OnReadComplete(int result);
180 void OnClosedStream();
182 // A Session may be closed via any of three methods:
183 // OnConnectionClosed - called by the connection when the connection has been
184 // closed, perhaps due to a timeout or a protocol error.
185 // CloseSessionOnError - called from the owner of the session,
186 // the QuicStreamFactory, when there is an error.
187 // OnReadComplete - when there is a read error.
188 // This method closes all stream and performs any necessary cleanup.
189 void CloseSessionOnErrorInner(int net_error, QuicErrorCode quic_error);
191 void CloseAllStreams(int net_error);
192 void CloseAllObservers(int net_error);
194 // Notifies the factory that this session is going away and no more streams
195 // should be created from it. This needs to be called before closing any
196 // streams, because closing a stream may cause a new stream to be created.
197 void NotifyFactoryOfSessionGoingAway();
199 // Posts a task to notify the factory that this session has been closed.
200 void NotifyFactoryOfSessionClosedLater();
202 // Notifies the factory that this session has been closed which will
204 void NotifyFactoryOfSessionClosed();
206 bool require_confirmation_;
207 scoped_ptr<QuicCryptoClientStream> crypto_stream_;
208 QuicStreamFactory* stream_factory_;
209 scoped_ptr<DatagramClientSocket> socket_;
210 scoped_ptr<QuicDefaultPacketWriter> writer_;
211 scoped_refptr<IOBufferWithSize> read_buffer_;
212 ObserverSet observers_;
213 StreamRequestQueue stream_requests_;
215 CompletionCallback callback_;
216 size_t num_total_streams_;
217 BoundNetLog net_log_;
218 QuicConnectionLogger logger_;
219 // Number of packets read in the current read loop.
220 size_t num_packets_read_;
221 base::WeakPtrFactory<QuicClientSession> weak_factory_;
223 DISALLOW_COPY_AND_ASSIGN(QuicClientSession);
228 #endif // NET_QUIC_QUIC_CLIENT_SESSION_H_