Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / net / quic / quic_client_session.h
blob801ad07ba52215c2b3a9584c6a85d365c31ca7af
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 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_
13 #include <string>
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_client_session_base.h"
21 #include "net/quic/quic_connection_logger.h"
22 #include "net/quic/quic_crypto_client_stream.h"
23 #include "net/quic/quic_protocol.h"
24 #include "net/quic/quic_reliable_client_stream.h"
26 namespace net {
28 class CertVerifyResult;
29 class DatagramClientSocket;
30 class QuicConnectionHelper;
31 class QuicCryptoClientStreamFactory;
32 class QuicDefaultPacketWriter;
33 class QuicSessionKey;
34 class QuicServerInfo;
35 class QuicStreamFactory;
36 class SSLInfo;
38 namespace test {
39 class QuicClientSessionPeer;
40 } // namespace test
42 class NET_EXPORT_PRIVATE QuicClientSession : public QuicClientSessionBase {
43 public:
44 // An interface for observing events on a session.
45 class NET_EXPORT_PRIVATE Observer {
46 public:
47 virtual ~Observer() {}
48 virtual void OnCryptoHandshakeConfirmed() = 0;
49 virtual void OnSessionClosed(int error) = 0;
52 // A helper class used to manage a request to create a stream.
53 class NET_EXPORT_PRIVATE StreamRequest {
54 public:
55 StreamRequest();
56 ~StreamRequest();
58 // Starts a request to create a stream. If OK is returned, then
59 // |stream| will be updated with the newly created stream. If
60 // ERR_IO_PENDING is returned, then when the request is eventuallly
61 // complete |callback| will be called.
62 int StartRequest(const base::WeakPtr<QuicClientSession>& session,
63 QuicReliableClientStream** stream,
64 const CompletionCallback& callback);
66 // Cancels any pending stream creation request. May be called
67 // repeatedly.
68 void CancelRequest();
70 private:
71 friend class QuicClientSession;
73 // Called by |session_| for an asynchronous request when the stream
74 // request has finished successfully.
75 void OnRequestCompleteSuccess(QuicReliableClientStream* stream);
77 // Called by |session_| for an asynchronous request when the stream
78 // request has finished with an error. Also called with ERR_ABORTED
79 // if |session_| is destroyed while the stream request is still pending.
80 void OnRequestCompleteFailure(int rv);
82 base::WeakPtr<QuicClientSession> session_;
83 CompletionCallback callback_;
84 QuicReliableClientStream** stream_;
86 DISALLOW_COPY_AND_ASSIGN(StreamRequest);
89 // Constructs a new session which will own |connection| and |helper|, but
90 // not |stream_factory|, which must outlive this session.
91 // TODO(rch): decouple the factory from the session via a Delegate interface.
92 QuicClientSession(QuicConnection* connection,
93 scoped_ptr<DatagramClientSocket> socket,
94 scoped_ptr<QuicDefaultPacketWriter> writer,
95 QuicStreamFactory* stream_factory,
96 QuicCryptoClientStreamFactory* crypto_client_stream_factory,
97 scoped_ptr<QuicServerInfo> server_info,
98 const QuicSessionKey& server_key,
99 const QuicConfig& config,
100 QuicCryptoClientConfig* crypto_config,
101 NetLog* net_log);
103 virtual ~QuicClientSession();
105 void AddObserver(Observer* observer);
106 void RemoveObserver(Observer* observer);
108 // Attempts to create a new stream. If the stream can be
109 // created immediately, returns OK. If the open stream limit
110 // has been reached, returns ERR_IO_PENDING, and |request|
111 // will be added to the stream requets queue and will
112 // be completed asynchronously.
113 // TODO(rch): remove |stream| from this and use setter on |request|
114 // and fix in spdy too.
115 int TryCreateStream(StreamRequest* request,
116 QuicReliableClientStream** stream);
118 // Cancels the pending stream creation request.
119 void CancelRequest(StreamRequest* request);
121 // QuicSession methods:
122 virtual bool OnStreamFrames(
123 const std::vector<QuicStreamFrame>& frames) OVERRIDE;
124 virtual QuicReliableClientStream* CreateOutgoingDataStream() OVERRIDE;
125 virtual QuicCryptoClientStream* GetCryptoStream() OVERRIDE;
126 virtual void CloseStream(QuicStreamId stream_id) OVERRIDE;
127 virtual void SendRstStream(QuicStreamId id,
128 QuicRstStreamErrorCode error,
129 QuicStreamOffset bytes_written) OVERRIDE;
130 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) OVERRIDE;
131 virtual void OnCryptoHandshakeMessageSent(
132 const CryptoHandshakeMessage& message) OVERRIDE;
133 virtual void OnCryptoHandshakeMessageReceived(
134 const CryptoHandshakeMessage& message) OVERRIDE;
135 virtual bool GetSSLInfo(SSLInfo* ssl_info) const OVERRIDE;
137 // QuicClientSessionBase methods:
138 virtual void OnProofValid(
139 const QuicCryptoClientConfig::CachedState& cached) OVERRIDE;
140 virtual void OnProofVerifyDetailsAvailable(
141 const ProofVerifyDetails& verify_details) OVERRIDE;
143 // QuicConnectionVisitorInterface methods:
144 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
145 virtual void OnSuccessfulVersionNegotiation(
146 const QuicVersion& version) OVERRIDE;
148 // Performs a crypto handshake with the server.
149 int CryptoConnect(bool require_confirmation,
150 const CompletionCallback& callback);
152 // Causes the QuicConnectionHelper to start reading from the socket
153 // and passing the data along to the QuicConnection.
154 void StartReading();
156 // Close the session because of |error| and notifies the factory
157 // that this session has been closed, which will delete the session.
158 void CloseSessionOnError(int error);
160 base::Value* GetInfoAsValue(const std::set<HostPortPair>& aliases) const;
162 const BoundNetLog& net_log() const { return net_log_; }
164 base::WeakPtr<QuicClientSession> GetWeakPtr();
166 // Returns the number of client hello messages that have been sent on the
167 // crypto stream. If the handshake has completed then this is one greater
168 // than the number of round-trips needed for the handshake.
169 int GetNumSentClientHellos() const;
171 // Returns true if |hostname| may be pooled onto this session. If this
172 // is a secure QUIC session, then |hostname| must match the certificate
173 // presented during the handshake.
174 bool CanPool(const std::string& hostname) const;
176 protected:
177 // QuicSession methods:
178 virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) OVERRIDE;
180 private:
181 friend class test::QuicClientSessionPeer;
183 typedef std::set<Observer*> ObserverSet;
184 typedef std::list<StreamRequest*> StreamRequestQueue;
186 QuicReliableClientStream* CreateOutgoingReliableStreamImpl();
187 // A completion callback invoked when a read completes.
188 void OnReadComplete(int result);
190 void OnClosedStream();
192 // A Session may be closed via any of three methods:
193 // OnConnectionClosed - called by the connection when the connection has been
194 // closed, perhaps due to a timeout or a protocol error.
195 // CloseSessionOnError - called from the owner of the session,
196 // the QuicStreamFactory, when there is an error.
197 // OnReadComplete - when there is a read error.
198 // This method closes all stream and performs any necessary cleanup.
199 void CloseSessionOnErrorInner(int net_error, QuicErrorCode quic_error);
201 void CloseAllStreams(int net_error);
202 void CloseAllObservers(int net_error);
204 // Notifies the factory that this session is going away and no more streams
205 // should be created from it. This needs to be called before closing any
206 // streams, because closing a stream may cause a new stream to be created.
207 void NotifyFactoryOfSessionGoingAway();
209 // Posts a task to notify the factory that this session has been closed.
210 void NotifyFactoryOfSessionClosedLater();
212 // Notifies the factory that this session has been closed which will
213 // delete |this|.
214 void NotifyFactoryOfSessionClosed();
216 bool require_confirmation_;
217 scoped_ptr<QuicCryptoClientStream> crypto_stream_;
218 QuicStreamFactory* stream_factory_;
219 scoped_ptr<DatagramClientSocket> socket_;
220 scoped_ptr<QuicDefaultPacketWriter> writer_;
221 scoped_refptr<IOBufferWithSize> read_buffer_;
222 scoped_ptr<QuicServerInfo> server_info_;
223 scoped_ptr<CertVerifyResult> cert_verify_result_;
224 ObserverSet observers_;
225 StreamRequestQueue stream_requests_;
226 bool read_pending_;
227 CompletionCallback callback_;
228 size_t num_total_streams_;
229 BoundNetLog net_log_;
230 QuicConnectionLogger logger_;
231 // Number of packets read in the current read loop.
232 size_t num_packets_read_;
233 // True when the session is going away, and streams may no longer be created
234 // on this session. Existing stream will continue to be processed.
235 bool going_away_;
236 base::WeakPtrFactory<QuicClientSession> weak_factory_;
238 DISALLOW_COPY_AND_ASSIGN(QuicClientSession);
241 } // namespace net
243 #endif // NET_QUIC_QUIC_CLIENT_SESSION_H_