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_packet_reader.h"
25 #include "net/quic/quic_protocol.h"
26 #include "net/quic/quic_reliable_client_stream.h"
30 class CertVerifyResult
;
31 class DatagramClientSocket
;
32 class QuicConnectionHelper
;
33 class QuicCryptoClientStreamFactory
;
36 class QuicStreamFactory
;
38 class TransportSecurityState
;
41 class QuicClientSessionPeer
;
44 class NET_EXPORT_PRIVATE QuicClientSession
: public QuicClientSessionBase
,
45 public QuicPacketReader::Visitor
{
47 // An interface for observing events on a session.
48 class NET_EXPORT_PRIVATE Observer
{
50 virtual ~Observer() {}
51 virtual void OnCryptoHandshakeConfirmed() = 0;
52 virtual void OnSessionClosed(int error
) = 0;
55 // A helper class used to manage a request to create a stream.
56 class NET_EXPORT_PRIVATE StreamRequest
{
61 // Starts a request to create a stream. If OK is returned, then
62 // |stream| will be updated with the newly created stream. If
63 // ERR_IO_PENDING is returned, then when the request is eventuallly
64 // complete |callback| will be called.
65 int StartRequest(const base::WeakPtr
<QuicClientSession
>& session
,
66 QuicReliableClientStream
** stream
,
67 const CompletionCallback
& callback
);
69 // Cancels any pending stream creation request. May be called
74 friend class QuicClientSession
;
76 // Called by |session_| for an asynchronous request when the stream
77 // request has finished successfully.
78 void OnRequestCompleteSuccess(QuicReliableClientStream
* stream
);
80 // Called by |session_| for an asynchronous request when the stream
81 // request has finished with an error. Also called with ERR_ABORTED
82 // if |session_| is destroyed while the stream request is still pending.
83 void OnRequestCompleteFailure(int rv
);
85 base::WeakPtr
<QuicClientSession
> session_
;
86 CompletionCallback callback_
;
87 QuicReliableClientStream
** stream_
;
89 DISALLOW_COPY_AND_ASSIGN(StreamRequest
);
92 // Constructs a new session which will own |connection|, but not
93 // |stream_factory|, which must outlive this session.
94 // TODO(rch): decouple the factory from the session via a Delegate interface.
95 QuicClientSession(QuicConnection
* connection
,
96 scoped_ptr
<DatagramClientSocket
> socket
,
97 QuicStreamFactory
* stream_factory
,
98 QuicCryptoClientStreamFactory
* crypto_client_stream_factory
,
99 TransportSecurityState
* transport_security_state
,
100 scoped_ptr
<QuicServerInfo
> server_info
,
101 const QuicServerId
& server_id
,
102 int cert_verify_flags
,
103 const QuicConfig
& config
,
104 QuicCryptoClientConfig
* crypto_config
,
105 const char* const connection_description
,
106 base::TimeTicks dns_resolution_end_time
,
107 base::TaskRunner
* task_runner
,
109 ~QuicClientSession() override
;
111 void AddObserver(Observer
* observer
);
112 void RemoveObserver(Observer
* observer
);
114 // Attempts to create a new stream. If the stream can be
115 // created immediately, returns OK. If the open stream limit
116 // has been reached, returns ERR_IO_PENDING, and |request|
117 // will be added to the stream requets queue and will
118 // be completed asynchronously.
119 // TODO(rch): remove |stream| from this and use setter on |request|
120 // and fix in spdy too.
121 int TryCreateStream(StreamRequest
* request
,
122 QuicReliableClientStream
** stream
);
124 // Cancels the pending stream creation request.
125 void CancelRequest(StreamRequest
* request
);
127 // QuicSession methods:
128 void OnStreamFrames(const std::vector
<QuicStreamFrame
>& frames
) override
;
129 QuicReliableClientStream
* CreateOutgoingDynamicStream() override
;
130 QuicCryptoClientStream
* GetCryptoStream() override
;
131 void CloseStream(QuicStreamId stream_id
) override
;
132 void SendRstStream(QuicStreamId id
,
133 QuicRstStreamErrorCode error
,
134 QuicStreamOffset bytes_written
) override
;
135 void OnCryptoHandshakeEvent(CryptoHandshakeEvent event
) override
;
136 void OnCryptoHandshakeMessageSent(
137 const CryptoHandshakeMessage
& message
) override
;
138 void OnCryptoHandshakeMessageReceived(
139 const CryptoHandshakeMessage
& message
) 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 // QuicPacketReader::Visitor methods:
151 void OnReadError(int result
) override
;
152 bool OnPacket(const QuicEncryptedPacket
& packet
,
153 IPEndPoint local_address
,
154 IPEndPoint peer_address
) override
;
156 // Gets the SSL connection information.
157 bool GetSSLInfo(SSLInfo
* ssl_info
) const;
159 // Performs a crypto handshake with the server.
160 int CryptoConnect(bool require_confirmation
,
161 const CompletionCallback
& callback
);
163 // Resumes a crypto handshake with the server after a timeout.
164 int ResumeCryptoConnect(const CompletionCallback
& callback
);
166 // Causes the QuicConnectionHelper to start reading from the socket
167 // and passing the data along to the QuicConnection.
170 // Close the session because of |error| and notifies the factory
171 // that this session has been closed, which will delete the session.
172 void CloseSessionOnError(int error
, QuicErrorCode quic_error
);
174 // Close the session because of |error| and notifies the factory later that
175 // this session has been closed, which will delete the session.
176 void CloseSessionOnErrorAndNotifyFactoryLater(int error
,
177 QuicErrorCode quic_error
);
179 scoped_ptr
<base::Value
> GetInfoAsValue(const std::set
<HostPortPair
>& aliases
);
181 const BoundNetLog
& net_log() const { return net_log_
; }
183 base::WeakPtr
<QuicClientSession
> GetWeakPtr();
185 // Returns the number of client hello messages that have been sent on the
186 // crypto stream. If the handshake has completed then this is one greater
187 // than the number of round-trips needed for the handshake.
188 int GetNumSentClientHellos() const;
190 // Returns true if |hostname| may be pooled onto this session. If this
191 // is a secure QUIC session, then |hostname| must match the certificate
192 // presented during the handshake.
193 bool CanPool(const std::string
& hostname
, PrivacyMode privacy_mode
) const;
195 const QuicServerId
& server_id() const { return server_id_
; }
198 // QuicSession methods:
199 QuicDataStream
* CreateIncomingDynamicStream(QuicStreamId id
) override
;
202 friend class test::QuicClientSessionPeer
;
204 typedef std::set
<Observer
*> ObserverSet
;
205 typedef std::list
<StreamRequest
*> StreamRequestQueue
;
207 QuicReliableClientStream
* CreateOutgoingReliableStreamImpl();
208 // A completion callback invoked when a read completes.
209 void OnReadComplete(int result
);
211 void OnClosedStream();
213 // Close the session because of |error| and records it in UMA histogram.
214 void RecordAndCloseSessionOnError(int error
, QuicErrorCode quic_error
);
216 // A Session may be closed via any of three methods:
217 // OnConnectionClosed - called by the connection when the connection has been
218 // closed, perhaps due to a timeout or a protocol error.
219 // CloseSessionOnError - called from the owner of the session,
220 // the QuicStreamFactory, when there is an error.
221 // OnReadComplete - when there is a read error.
222 // This method closes all stream and performs any necessary cleanup.
223 void CloseSessionOnErrorInner(int net_error
, QuicErrorCode quic_error
);
225 void CloseAllStreams(int net_error
);
226 void CloseAllObservers(int net_error
);
228 // Notifies the factory that this session is going away and no more streams
229 // should be created from it. This needs to be called before closing any
230 // streams, because closing a stream may cause a new stream to be created.
231 void NotifyFactoryOfSessionGoingAway();
233 // Posts a task to notify the factory that this session has been closed.
234 void NotifyFactoryOfSessionClosedLater();
236 // Notifies the factory that this session has been closed which will
238 void NotifyFactoryOfSessionClosed();
240 void OnConnectTimeout();
242 QuicServerId server_id_
;
243 bool require_confirmation_
;
244 scoped_ptr
<QuicCryptoClientStream
> crypto_stream_
;
245 QuicStreamFactory
* stream_factory_
;
246 scoped_ptr
<DatagramClientSocket
> socket_
;
247 TransportSecurityState
* transport_security_state_
;
248 scoped_ptr
<QuicServerInfo
> server_info_
;
249 scoped_ptr
<CertVerifyResult
> cert_verify_result_
;
250 std::string pinning_failure_log_
;
251 ObserverSet observers_
;
252 StreamRequestQueue stream_requests_
;
253 CompletionCallback callback_
;
254 size_t num_total_streams_
;
255 base::TaskRunner
* task_runner_
;
256 BoundNetLog net_log_
;
257 QuicPacketReader packet_reader_
;
258 base::TimeTicks dns_resolution_end_time_
;
259 base::TimeTicks handshake_start_
; // Time the handshake was started.
260 scoped_ptr
<QuicConnectionLogger
> logger_
;
261 // True when the session is going away, and streams may no longer be created
262 // on this session. Existing stream will continue to be processed.
264 base::WeakPtrFactory
<QuicClientSession
> weak_factory_
;
266 DISALLOW_COPY_AND_ASSIGN(QuicClientSession
);
271 #endif // NET_QUIC_QUIC_CLIENT_SESSION_H_