Revert of Add support for escaped target names in isolate driver. (patchset #6 id...
[chromium-blink-merge.git] / net / quic / quic_client_session.h
blobb5590692528f00be6db99a072bf96e1550e92b01
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 "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"
27 namespace net {
29 class CertVerifyResult;
30 class DatagramClientSocket;
31 class QuicConnectionHelper;
32 class QuicCryptoClientStreamFactory;
33 class QuicServerId;
34 class QuicServerInfo;
35 class QuicStreamFactory;
36 class SSLInfo;
37 class TransportSecurityState;
39 namespace test {
40 class QuicClientSessionPeer;
41 } // namespace test
43 class NET_EXPORT_PRIVATE QuicClientSession : public QuicClientSessionBase {
44 public:
45 // An interface for observing events on a session.
46 class NET_EXPORT_PRIVATE Observer {
47 public:
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 {
55 public:
56 StreamRequest();
57 ~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
68 // repeatedly.
69 void CancelRequest();
71 private:
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::TimeTicks dns_resolution_end_time,
101 base::TaskRunner* task_runner,
102 NetLog* net_log);
103 ~QuicClientSession() override;
105 // Initialize session's connection to |server_id|.
106 void InitializeSession(
107 const QuicServerId& server_id,
108 QuicCryptoClientConfig* config,
109 QuicCryptoClientStreamFactory* crypto_client_stream_factory);
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* CreateOutgoingDataStream() 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;
140 bool GetSSLInfo(SSLInfo* ssl_info) const override;
142 // QuicClientSessionBase methods:
143 void OnProofValid(const QuicCryptoClientConfig::CachedState& cached) override;
144 void OnProofVerifyDetailsAvailable(
145 const ProofVerifyDetails& verify_details) override;
147 // QuicConnectionVisitorInterface methods:
148 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override;
149 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override;
151 // Performs a crypto handshake with the server.
152 int CryptoConnect(bool require_confirmation,
153 const CompletionCallback& callback);
155 // Resumes a crypto handshake with the server after a timeout.
156 int ResumeCryptoConnect(const CompletionCallback& callback);
158 // Causes the QuicConnectionHelper to start reading from the socket
159 // and passing the data along to the QuicConnection.
160 void StartReading();
162 // Close the session because of |error| and notifies the factory
163 // that this session has been closed, which will delete the session.
164 void CloseSessionOnError(int error);
166 base::Value* GetInfoAsValue(const std::set<HostPortPair>& aliases);
168 const BoundNetLog& net_log() const { return net_log_; }
170 base::WeakPtr<QuicClientSession> GetWeakPtr();
172 // Returns the number of client hello messages that have been sent on the
173 // crypto stream. If the handshake has completed then this is one greater
174 // than the number of round-trips needed for the handshake.
175 int GetNumSentClientHellos() const;
177 // Returns true if |hostname| may be pooled onto this session. If this
178 // is a secure QUIC session, then |hostname| must match the certificate
179 // presented during the handshake.
180 bool CanPool(const std::string& hostname, PrivacyMode privacy_mode) const;
182 const QuicServerId& server_id() const { return server_id_; }
184 protected:
185 // QuicSession methods:
186 QuicDataStream* CreateIncomingDataStream(QuicStreamId id) override;
188 private:
189 friend class test::QuicClientSessionPeer;
191 typedef std::set<Observer*> ObserverSet;
192 typedef std::list<StreamRequest*> StreamRequestQueue;
194 QuicReliableClientStream* CreateOutgoingReliableStreamImpl();
195 // A completion callback invoked when a read completes.
196 void OnReadComplete(int result);
198 void OnClosedStream();
200 // A Session may be closed via any of three methods:
201 // OnConnectionClosed - called by the connection when the connection has been
202 // closed, perhaps due to a timeout or a protocol error.
203 // CloseSessionOnError - called from the owner of the session,
204 // the QuicStreamFactory, when there is an error.
205 // OnReadComplete - when there is a read error.
206 // This method closes all stream and performs any necessary cleanup.
207 void CloseSessionOnErrorInner(int net_error, QuicErrorCode quic_error);
209 void CloseAllStreams(int net_error);
210 void CloseAllObservers(int net_error);
212 // Notifies the factory that this session is going away and no more streams
213 // should be created from it. This needs to be called before closing any
214 // streams, because closing a stream may cause a new stream to be created.
215 void NotifyFactoryOfSessionGoingAway();
217 // Posts a task to notify the factory that this session has been closed.
218 void NotifyFactoryOfSessionClosedLater();
220 // Notifies the factory that this session has been closed which will
221 // delete |this|.
222 void NotifyFactoryOfSessionClosed();
224 void OnConnectTimeout();
226 QuicServerId server_id_;
227 bool require_confirmation_;
228 scoped_ptr<QuicCryptoClientStream> crypto_stream_;
229 QuicStreamFactory* stream_factory_;
230 scoped_ptr<DatagramClientSocket> socket_;
231 scoped_refptr<IOBufferWithSize> read_buffer_;
232 TransportSecurityState* transport_security_state_;
233 scoped_ptr<QuicServerInfo> server_info_;
234 scoped_ptr<CertVerifyResult> cert_verify_result_;
235 std::string pinning_failure_log_;
236 ObserverSet observers_;
237 StreamRequestQueue stream_requests_;
238 bool read_pending_;
239 CompletionCallback callback_;
240 size_t num_total_streams_;
241 base::TaskRunner* task_runner_;
242 BoundNetLog net_log_;
243 base::TimeTicks dns_resolution_end_time_;
244 base::TimeTicks handshake_start_; // Time the handshake was started.
245 scoped_ptr<QuicConnectionLogger> logger_;
246 // Number of packets read in the current read loop.
247 size_t num_packets_read_;
248 // True when the session is going away, and streams may no longer be created
249 // on this session. Existing stream will continue to be processed.
250 bool going_away_;
251 base::WeakPtrFactory<QuicClientSession> weak_factory_;
253 DISALLOW_COPY_AND_ASSIGN(QuicClientSession);
256 } // namespace net
258 #endif // NET_QUIC_QUIC_CLIENT_SESSION_H_