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 "base/time/time.h"
19 #include "net/base/completion_callback.h"
20 #include "net/proxy/proxy_server.h"
21 #include "net/quic/quic_client_session_base.h"
22 #include "net/quic/quic_connection_logger.h"
23 #include "net/quic/quic_crypto_client_stream.h"
24 #include "net/quic/quic_protocol.h"
25 #include "net/quic/quic_reliable_client_stream.h"
29 class CertVerifyResult
;
30 class DatagramClientSocket
;
31 class QuicConnectionHelper
;
32 class QuicCryptoClientStreamFactory
;
35 class QuicStreamFactory
;
37 class TransportSecurityState
;
40 class QuicClientSessionPeer
;
43 class NET_EXPORT_PRIVATE QuicClientSession
: public QuicClientSessionBase
{
45 // An interface for observing events on a session.
46 class NET_EXPORT_PRIVATE Observer
{
48 virtual ~Observer() {}
49 virtual void OnCryptoHandshakeConfirmed() = 0;
50 virtual void OnSessionClosed(int error
) = 0;
53 // A helper class used to manage a request to create a stream.
54 class NET_EXPORT_PRIVATE StreamRequest
{
59 // Starts a request to create a stream. If OK is returned, then
60 // |stream| will be updated with the newly created stream. If
61 // ERR_IO_PENDING is returned, then when the request is eventuallly
62 // complete |callback| will be called.
63 int StartRequest(const base::WeakPtr
<QuicClientSession
>& session
,
64 QuicReliableClientStream
** stream
,
65 const CompletionCallback
& callback
);
67 // Cancels any pending stream creation request. May be called
72 friend class QuicClientSession
;
74 // Called by |session_| for an asynchronous request when the stream
75 // request has finished successfully.
76 void OnRequestCompleteSuccess(QuicReliableClientStream
* stream
);
78 // Called by |session_| for an asynchronous request when the stream
79 // request has finished with an error. Also called with ERR_ABORTED
80 // if |session_| is destroyed while the stream request is still pending.
81 void OnRequestCompleteFailure(int rv
);
83 base::WeakPtr
<QuicClientSession
> session_
;
84 CompletionCallback callback_
;
85 QuicReliableClientStream
** stream_
;
87 DISALLOW_COPY_AND_ASSIGN(StreamRequest
);
90 // Constructs a new session which will own |connection|, but not
91 // |stream_factory|, which must outlive this session.
92 // TODO(rch): decouple the factory from the session via a Delegate interface.
93 QuicClientSession(QuicConnection
* connection
,
94 scoped_ptr
<DatagramClientSocket
> socket
,
95 QuicStreamFactory
* stream_factory
,
96 TransportSecurityState
* transport_security_state
,
97 scoped_ptr
<QuicServerInfo
> server_info
,
98 const QuicConfig
& config
,
99 const char* const connection_description
,
100 base::TaskRunner
* task_runner
,
102 ~QuicClientSession() override
;
104 // Initialize session's connection to |server_id|.
105 void InitializeSession(
106 const QuicServerId
& server_id
,
107 QuicCryptoClientConfig
* config
,
108 QuicCryptoClientStreamFactory
* crypto_client_stream_factory
);
110 void AddObserver(Observer
* observer
);
111 void RemoveObserver(Observer
* observer
);
113 // Attempts to create a new stream. If the stream can be
114 // created immediately, returns OK. If the open stream limit
115 // has been reached, returns ERR_IO_PENDING, and |request|
116 // will be added to the stream requets queue and will
117 // be completed asynchronously.
118 // TODO(rch): remove |stream| from this and use setter on |request|
119 // and fix in spdy too.
120 int TryCreateStream(StreamRequest
* request
,
121 QuicReliableClientStream
** stream
);
123 // Cancels the pending stream creation request.
124 void CancelRequest(StreamRequest
* request
);
126 // QuicSession methods:
127 void OnStreamFrames(const std::vector
<QuicStreamFrame
>& frames
) override
;
128 QuicReliableClientStream
* CreateOutgoingDataStream() override
;
129 QuicCryptoClientStream
* GetCryptoStream() override
;
130 void CloseStream(QuicStreamId stream_id
) override
;
131 void SendRstStream(QuicStreamId id
,
132 QuicRstStreamErrorCode error
,
133 QuicStreamOffset bytes_written
) override
;
134 void OnCryptoHandshakeEvent(CryptoHandshakeEvent event
) override
;
135 void OnCryptoHandshakeMessageSent(
136 const CryptoHandshakeMessage
& message
) override
;
137 void OnCryptoHandshakeMessageReceived(
138 const CryptoHandshakeMessage
& message
) override
;
139 bool GetSSLInfo(SSLInfo
* ssl_info
) const override
;
141 // QuicClientSessionBase methods:
142 void OnProofValid(const QuicCryptoClientConfig::CachedState
& cached
) override
;
143 void OnProofVerifyDetailsAvailable(
144 const ProofVerifyDetails
& verify_details
) override
;
146 // QuicConnectionVisitorInterface methods:
147 void OnConnectionClosed(QuicErrorCode error
, bool from_peer
) override
;
148 void OnSuccessfulVersionNegotiation(const QuicVersion
& version
) override
;
150 // Performs a crypto handshake with the server.
151 int CryptoConnect(bool require_confirmation
,
152 const CompletionCallback
& callback
);
154 // Resumes a crypto handshake with the server after a timeout.
155 int ResumeCryptoConnect(const CompletionCallback
& callback
);
157 // Causes the QuicConnectionHelper to start reading from the socket
158 // and passing the data along to the QuicConnection.
161 // Close the session because of |error| and notifies the factory
162 // that this session has been closed, which will delete the session.
163 void CloseSessionOnError(int error
);
165 base::Value
* GetInfoAsValue(const std::set
<HostPortPair
>& aliases
);
167 const BoundNetLog
& net_log() const { return net_log_
; }
169 base::WeakPtr
<QuicClientSession
> GetWeakPtr();
171 // Returns the number of client hello messages that have been sent on the
172 // crypto stream. If the handshake has completed then this is one greater
173 // than the number of round-trips needed for the handshake.
174 int GetNumSentClientHellos() const;
176 // Returns true if |hostname| may be pooled onto this session. If this
177 // is a secure QUIC session, then |hostname| must match the certificate
178 // presented during the handshake.
179 bool CanPool(const std::string
& hostname
, PrivacyMode privacy_mode
) const;
181 const QuicServerId
& server_id() const { return server_id_
; }
184 // QuicSession methods:
185 QuicDataStream
* CreateIncomingDataStream(QuicStreamId id
) override
;
188 friend class test::QuicClientSessionPeer
;
190 typedef std::set
<Observer
*> ObserverSet
;
191 typedef std::list
<StreamRequest
*> StreamRequestQueue
;
193 QuicReliableClientStream
* CreateOutgoingReliableStreamImpl();
194 // A completion callback invoked when a read completes.
195 void OnReadComplete(int result
);
197 void OnClosedStream();
199 // A Session may be closed via any of three methods:
200 // OnConnectionClosed - called by the connection when the connection has been
201 // closed, perhaps due to a timeout or a protocol error.
202 // CloseSessionOnError - called from the owner of the session,
203 // the QuicStreamFactory, when there is an error.
204 // OnReadComplete - when there is a read error.
205 // This method closes all stream and performs any necessary cleanup.
206 void CloseSessionOnErrorInner(int net_error
, QuicErrorCode quic_error
);
208 void CloseAllStreams(int net_error
);
209 void CloseAllObservers(int net_error
);
211 // Notifies the factory that this session is going away and no more streams
212 // should be created from it. This needs to be called before closing any
213 // streams, because closing a stream may cause a new stream to be created.
214 void NotifyFactoryOfSessionGoingAway();
216 // Posts a task to notify the factory that this session has been closed.
217 void NotifyFactoryOfSessionClosedLater();
219 // Notifies the factory that this session has been closed which will
221 void NotifyFactoryOfSessionClosed();
223 void OnConnectTimeout();
225 QuicServerId server_id_
;
226 bool require_confirmation_
;
227 scoped_ptr
<QuicCryptoClientStream
> crypto_stream_
;
228 QuicStreamFactory
* stream_factory_
;
229 scoped_ptr
<DatagramClientSocket
> socket_
;
230 scoped_refptr
<IOBufferWithSize
> read_buffer_
;
231 TransportSecurityState
* transport_security_state_
;
232 scoped_ptr
<QuicServerInfo
> server_info_
;
233 scoped_ptr
<CertVerifyResult
> cert_verify_result_
;
234 std::string pinning_failure_log_
;
235 ObserverSet observers_
;
236 StreamRequestQueue stream_requests_
;
238 CompletionCallback callback_
;
239 size_t num_total_streams_
;
240 base::TaskRunner
* task_runner_
;
241 BoundNetLog net_log_
;
242 base::TimeTicks handshake_start_
; // Time the handshake was started.
243 QuicConnectionLogger
* logger_
; // Owned by |connection_|.
244 // Number of packets read in the current read loop.
245 size_t num_packets_read_
;
246 // True when the session is going away, and streams may no longer be created
247 // on this session. Existing stream will continue to be processed.
249 base::WeakPtrFactory
<QuicClientSession
> weak_factory_
;
251 DISALLOW_COPY_AND_ASSIGN(QuicClientSession
);
256 #endif // NET_QUIC_QUIC_CLIENT_SESSION_H_