Implement keycode text conversion functions for Ozone.
[chromium-blink-merge.git] / net / quic / quic_client_session.h
blob6b7dab68fdbf6e9f2d70fa44df6c1ad394a12b26
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_packet_reader.h"
25 #include "net/quic/quic_protocol.h"
26 #include "net/quic/quic_reliable_client_stream.h"
28 namespace net {
30 class CertVerifyResult;
31 class DatagramClientSocket;
32 class QuicConnectionHelper;
33 class QuicCryptoClientStreamFactory;
34 class QuicServerId;
35 class QuicServerInfo;
36 class QuicStreamFactory;
37 class SSLInfo;
38 class TransportSecurityState;
40 namespace test {
41 class QuicClientSessionPeer;
42 } // namespace test
44 class NET_EXPORT_PRIVATE QuicClientSession : public QuicClientSessionBase,
45 public QuicPacketReader::Visitor {
46 public:
47 // An interface for observing events on a session.
48 class NET_EXPORT_PRIVATE Observer {
49 public:
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 {
57 public:
58 StreamRequest();
59 ~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
70 // repeatedly.
71 void CancelRequest();
73 private:
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 TransportSecurityState* transport_security_state,
99 scoped_ptr<QuicServerInfo> server_info,
100 const QuicConfig& config,
101 const char* const connection_description,
102 base::TimeTicks dns_resolution_end_time,
103 base::TaskRunner* task_runner,
104 NetLog* net_log);
105 ~QuicClientSession() override;
107 // Initialize session's connection to |server_id|.
108 void InitializeSession(
109 const QuicServerId& server_id,
110 QuicCryptoClientConfig* config,
111 QuicCryptoClientStreamFactory* crypto_client_stream_factory);
113 void AddObserver(Observer* observer);
114 void RemoveObserver(Observer* observer);
116 // Attempts to create a new stream. If the stream can be
117 // created immediately, returns OK. If the open stream limit
118 // has been reached, returns ERR_IO_PENDING, and |request|
119 // will be added to the stream requets queue and will
120 // be completed asynchronously.
121 // TODO(rch): remove |stream| from this and use setter on |request|
122 // and fix in spdy too.
123 int TryCreateStream(StreamRequest* request,
124 QuicReliableClientStream** stream);
126 // Cancels the pending stream creation request.
127 void CancelRequest(StreamRequest* request);
129 // QuicSession methods:
130 void OnStreamFrames(const std::vector<QuicStreamFrame>& frames) override;
131 QuicReliableClientStream* CreateOutgoingDataStream() override;
132 QuicCryptoClientStream* GetCryptoStream() override;
133 void CloseStream(QuicStreamId stream_id) override;
134 void SendRstStream(QuicStreamId id,
135 QuicRstStreamErrorCode error,
136 QuicStreamOffset bytes_written) override;
137 void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) override;
138 void OnCryptoHandshakeMessageSent(
139 const CryptoHandshakeMessage& message) override;
140 void OnCryptoHandshakeMessageReceived(
141 const CryptoHandshakeMessage& message) override;
143 // QuicClientSessionBase methods:
144 void OnProofValid(const QuicCryptoClientConfig::CachedState& cached) override;
145 void OnProofVerifyDetailsAvailable(
146 const ProofVerifyDetails& verify_details) override;
148 // QuicConnectionVisitorInterface methods:
149 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override;
150 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override;
152 // QuicPacketReader::Visitor methods:
153 void OnReadError(int result) override;
154 bool OnPacket(const QuicEncryptedPacket& packet,
155 IPEndPoint local_address,
156 IPEndPoint peer_address) override;
158 // Gets the SSL connection information.
159 bool GetSSLInfo(SSLInfo* ssl_info) const;
161 // Performs a crypto handshake with the server.
162 int CryptoConnect(bool require_confirmation,
163 const CompletionCallback& callback);
165 // Resumes a crypto handshake with the server after a timeout.
166 int ResumeCryptoConnect(const CompletionCallback& callback);
168 // Causes the QuicConnectionHelper to start reading from the socket
169 // and passing the data along to the QuicConnection.
170 void StartReading();
172 // Close the session because of |error| and notifies the factory
173 // that this session has been closed, which will delete the session.
174 void CloseSessionOnError(int error, QuicErrorCode quic_error);
176 // Close the session because of |error| and notifies the factory later that
177 // this session has been closed, which will delete the session.
178 void CloseSessionOnErrorAndNotifyFactoryLater(int error,
179 QuicErrorCode quic_error);
181 base::Value* GetInfoAsValue(const std::set<HostPortPair>& aliases);
183 const BoundNetLog& net_log() const { return net_log_; }
185 base::WeakPtr<QuicClientSession> GetWeakPtr();
187 // Returns the number of client hello messages that have been sent on the
188 // crypto stream. If the handshake has completed then this is one greater
189 // than the number of round-trips needed for the handshake.
190 int GetNumSentClientHellos() const;
192 // Returns true if |hostname| may be pooled onto this session. If this
193 // is a secure QUIC session, then |hostname| must match the certificate
194 // presented during the handshake.
195 bool CanPool(const std::string& hostname, PrivacyMode privacy_mode) const;
197 const QuicServerId& server_id() const { return server_id_; }
199 protected:
200 // QuicSession methods:
201 QuicDataStream* CreateIncomingDataStream(QuicStreamId id) override;
203 private:
204 friend class test::QuicClientSessionPeer;
206 typedef std::set<Observer*> ObserverSet;
207 typedef std::list<StreamRequest*> StreamRequestQueue;
209 QuicReliableClientStream* CreateOutgoingReliableStreamImpl();
210 // A completion callback invoked when a read completes.
211 void OnReadComplete(int result);
213 void OnClosedStream();
215 // Close the session because of |error| and records it in UMA histogram.
216 void RecordAndCloseSessionOnError(int error, QuicErrorCode quic_error);
218 // A Session may be closed via any of three methods:
219 // OnConnectionClosed - called by the connection when the connection has been
220 // closed, perhaps due to a timeout or a protocol error.
221 // CloseSessionOnError - called from the owner of the session,
222 // the QuicStreamFactory, when there is an error.
223 // OnReadComplete - when there is a read error.
224 // This method closes all stream and performs any necessary cleanup.
225 void CloseSessionOnErrorInner(int net_error, QuicErrorCode quic_error);
227 void CloseAllStreams(int net_error);
228 void CloseAllObservers(int net_error);
230 // Notifies the factory that this session is going away and no more streams
231 // should be created from it. This needs to be called before closing any
232 // streams, because closing a stream may cause a new stream to be created.
233 void NotifyFactoryOfSessionGoingAway();
235 // Posts a task to notify the factory that this session has been closed.
236 void NotifyFactoryOfSessionClosedLater();
238 // Notifies the factory that this session has been closed which will
239 // delete |this|.
240 void NotifyFactoryOfSessionClosed();
242 void OnConnectTimeout();
244 QuicServerId server_id_;
245 bool require_confirmation_;
246 scoped_ptr<QuicCryptoClientStream> crypto_stream_;
247 QuicStreamFactory* stream_factory_;
248 scoped_ptr<DatagramClientSocket> socket_;
249 TransportSecurityState* transport_security_state_;
250 scoped_ptr<QuicServerInfo> server_info_;
251 scoped_ptr<CertVerifyResult> cert_verify_result_;
252 std::string pinning_failure_log_;
253 ObserverSet observers_;
254 StreamRequestQueue stream_requests_;
255 CompletionCallback callback_;
256 size_t num_total_streams_;
257 base::TaskRunner* task_runner_;
258 BoundNetLog net_log_;
259 QuicPacketReader packet_reader_;
260 base::TimeTicks dns_resolution_end_time_;
261 base::TimeTicks handshake_start_; // Time the handshake was started.
262 scoped_ptr<QuicConnectionLogger> logger_;
263 // True when the session is going away, and streams may no longer be created
264 // on this session. Existing stream will continue to be processed.
265 bool going_away_;
266 base::WeakPtrFactory<QuicClientSession> weak_factory_;
268 DISALLOW_COPY_AND_ASSIGN(QuicClientSession);
271 } // namespace net
273 #endif // NET_QUIC_QUIC_CLIENT_SESSION_H_