Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / net / quic / quic_client_session.h
blobebc7092a5043569a2f83fd4d33501f21c7e7392a
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_connection_logger.h"
21 #include "net/quic/quic_crypto_client_stream.h"
22 #include "net/quic/quic_protocol.h"
23 #include "net/quic/quic_reliable_client_stream.h"
24 #include "net/quic/quic_session.h"
26 namespace net {
28 class DatagramClientSocket;
29 class QuicConnectionHelper;
30 class QuicCryptoClientStreamFactory;
31 class QuicDefaultPacketWriter;
32 class QuicStreamFactory;
33 class SSLInfo;
35 namespace test {
36 class QuicClientSessionPeer;
37 } // namespace test
39 class NET_EXPORT_PRIVATE QuicClientSession : public QuicSession {
40 public:
41 // An interface for observing events on a session.
42 class NET_EXPORT_PRIVATE Observer {
43 public:
44 virtual ~Observer() {}
45 virtual void OnCryptoHandshakeConfirmed() = 0;
46 virtual void OnSessionClosed(int error) = 0;
49 // A helper class used to manage a request to create a stream.
50 class NET_EXPORT_PRIVATE StreamRequest {
51 public:
52 StreamRequest();
53 ~StreamRequest();
55 // Starts a request to create a stream. If OK is returned, then
56 // |stream| will be updated with the newly created stream. If
57 // ERR_IO_PENDING is returned, then when the request is eventuallly
58 // complete |callback| will be called.
59 int StartRequest(const base::WeakPtr<QuicClientSession>& session,
60 QuicReliableClientStream** stream,
61 const CompletionCallback& callback);
63 // Cancels any pending stream creation request. May be called
64 // repeatedly.
65 void CancelRequest();
67 private:
68 friend class QuicClientSession;
70 // Called by |session_| for an asynchronous request when the stream
71 // request has finished successfully.
72 void OnRequestCompleteSuccess(QuicReliableClientStream* stream);
74 // Called by |session_| for an asynchronous request when the stream
75 // request has finished with an error. Also called with ERR_ABORTED
76 // if |session_| is destroyed while the stream request is still pending.
77 void OnRequestCompleteFailure(int rv);
79 base::WeakPtr<QuicClientSession> session_;
80 CompletionCallback callback_;
81 QuicReliableClientStream** stream_;
83 DISALLOW_COPY_AND_ASSIGN(StreamRequest);
86 // Constructs a new session which will own |connection| and |helper|, but
87 // not |stream_factory|, which must outlive this session.
88 // TODO(rch): decouple the factory from the session via a Delegate interface.
89 QuicClientSession(QuicConnection* connection,
90 scoped_ptr<DatagramClientSocket> socket,
91 scoped_ptr<QuicDefaultPacketWriter> writer,
92 QuicStreamFactory* stream_factory,
93 QuicCryptoClientStreamFactory* crypto_client_stream_factory,
94 const std::string& server_hostname,
95 const QuicConfig& config,
96 QuicCryptoClientConfig* crypto_config,
97 NetLog* net_log);
99 virtual ~QuicClientSession();
101 void AddObserver(Observer* observer);
102 void RemoveObserver(Observer* observer);
104 // Attempts to create a new stream. If the stream can be
105 // created immediately, returns OK. If the open stream limit
106 // has been reached, returns ERR_IO_PENDING, and |request|
107 // will be added to the stream requets queue and will
108 // be completed asynchronously.
109 // TODO(rch): remove |stream| from this and use setter on |request|
110 // and fix in spdy too.
111 int TryCreateStream(StreamRequest* request,
112 QuicReliableClientStream** stream);
114 // Cancels the pending stream creation request.
115 void CancelRequest(StreamRequest* request);
117 // QuicSession methods:
118 virtual bool OnStreamFrames(
119 const std::vector<QuicStreamFrame>& frames) OVERRIDE;
120 virtual QuicReliableClientStream* CreateOutgoingDataStream() OVERRIDE;
121 virtual QuicCryptoClientStream* GetCryptoStream() OVERRIDE;
122 virtual void CloseStream(QuicStreamId stream_id) OVERRIDE;
123 virtual void SendRstStream(QuicStreamId id,
124 QuicRstStreamErrorCode error) OVERRIDE;
125 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) OVERRIDE;
126 virtual void OnCryptoHandshakeMessageSent(
127 const CryptoHandshakeMessage& message) OVERRIDE;
128 virtual void OnCryptoHandshakeMessageReceived(
129 const CryptoHandshakeMessage& message) OVERRIDE;
130 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
132 // QuicConnectionVisitorInterface methods:
133 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
134 virtual void OnSuccessfulVersionNegotiation(
135 const QuicVersion& version) OVERRIDE;
137 // Performs a crypto handshake with the server.
138 int CryptoConnect(bool require_confirmation,
139 const CompletionCallback& callback);
141 // Causes the QuicConnectionHelper to start reading from the socket
142 // and passing the data along to the QuicConnection.
143 void StartReading();
145 // Close the session because of |error| and notifies the factory
146 // that this session has been closed, which will delete the session.
147 void CloseSessionOnError(int error);
149 base::Value* GetInfoAsValue(const std::set<HostPortProxyPair>& aliases) const;
151 const BoundNetLog& net_log() const { return net_log_; }
153 base::WeakPtr<QuicClientSession> GetWeakPtr();
155 // Returns the number of client hello messages that have been sent on the
156 // crypto stream. If the handshake has completed then this is one greater
157 // than the number of round-trips needed for the handshake.
158 int GetNumSentClientHellos() const;
160 // Returns true if |hostname| may be pooled onto this session. If this
161 // is a secure QUIC session, then |hostname| must match the certificate
162 // presented during the handshake.
163 bool CanPool(const std::string& hostname) const;
165 protected:
166 // QuicSession methods:
167 virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) OVERRIDE;
169 private:
170 friend class test::QuicClientSessionPeer;
172 typedef std::set<Observer*> ObserverSet;
173 typedef std::list<StreamRequest*> StreamRequestQueue;
175 QuicReliableClientStream* CreateOutgoingReliableStreamImpl();
176 // A completion callback invoked when a read completes.
177 void OnReadComplete(int result);
179 void OnClosedStream();
181 // A Session may be closed via any of three methods:
182 // OnConnectionClosed - called by the connection when the connection has been
183 // closed, perhaps due to a timeout or a protocol error.
184 // CloseSessionOnError - called from the owner of the session,
185 // the QuicStreamFactory, when there is an error.
186 // OnReadComplete - when there is a read error.
187 // This method closes all stream and performs any necessary cleanup.
188 void CloseSessionOnErrorInner(int net_error, QuicErrorCode quic_error);
190 void CloseAllStreams(int net_error);
191 void CloseAllObservers(int net_error);
193 // Notifies the factory that this session is going away and no more streams
194 // should be created from it. This needs to be called before closing any
195 // streams, because closing a stream may cause a new stream to be created.
196 void NotifyFactoryOfSessionGoingAway();
198 // Posts a task to notify the factory that this session has been closed.
199 void NotifyFactoryOfSessionClosedLater();
201 // Notifies the factory that this session has been closed which will
202 // delete |this|.
203 void NotifyFactoryOfSessionClosed();
205 bool require_confirmation_;
206 scoped_ptr<QuicCryptoClientStream> crypto_stream_;
207 QuicStreamFactory* stream_factory_;
208 scoped_ptr<DatagramClientSocket> socket_;
209 scoped_ptr<QuicDefaultPacketWriter> writer_;
210 scoped_refptr<IOBufferWithSize> read_buffer_;
211 ObserverSet observers_;
212 StreamRequestQueue stream_requests_;
213 bool read_pending_;
214 CompletionCallback callback_;
215 size_t num_total_streams_;
216 BoundNetLog net_log_;
217 QuicConnectionLogger logger_;
218 // Number of packets read in the current read loop.
219 size_t num_packets_read_;
220 base::WeakPtrFactory<QuicClientSession> weak_factory_;
222 DISALLOW_COPY_AND_ASSIGN(QuicClientSession);
225 } // namespace net
227 #endif // NET_QUIC_QUIC_CLIENT_SESSION_H_