[Mac] Enable MacViews bookmark editor behind --enable-mac-views-dialogs.
[chromium-blink-merge.git] / net / quic / quic_chromium_client_session.h
blobfeb071042f26df69280ab0b8371ef09a502be281
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_CHROMIUM_CLIENT_SESSION_H_
11 #define NET_QUIC_QUIC_CHROMIUM_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 QuicChromiumClientSessionPeer;
42 } // namespace test
44 class NET_EXPORT_PRIVATE QuicChromiumClientSession
45 : public QuicClientSessionBase,
46 public QuicPacketReader::Visitor {
47 public:
48 // Reasons to disable QUIC, that is under certain pathological
49 // connection errors. Note: these values must be kept in sync with
50 // the corresponding values of QuicDisabledReason in:
51 // tools/metrics/histograms/histograms.xml
52 enum QuicDisabledReason {
53 QUIC_DISABLED_NOT = 0, // default, not disabled
54 QUIC_DISABLED_PUBLIC_RESET_POST_HANDSHAKE = 1,
55 QUIC_DISABLED_TIMEOUT_WITH_OPEN_STREAMS = 2,
56 QUIC_DISABLED_BAD_PACKET_LOSS_RATE = 3,
57 QUIC_DISABLED_MAX = 4,
60 // An interface for observing events on a session.
61 class NET_EXPORT_PRIVATE Observer {
62 public:
63 virtual ~Observer() {}
64 virtual void OnCryptoHandshakeConfirmed() = 0;
65 virtual void OnSessionClosed(int error) = 0;
68 // A helper class used to manage a request to create a stream.
69 class NET_EXPORT_PRIVATE StreamRequest {
70 public:
71 StreamRequest();
72 ~StreamRequest();
74 // Starts a request to create a stream. If OK is returned, then
75 // |stream| will be updated with the newly created stream. If
76 // ERR_IO_PENDING is returned, then when the request is eventuallly
77 // complete |callback| will be called.
78 int StartRequest(const base::WeakPtr<QuicChromiumClientSession>& session,
79 QuicReliableClientStream** stream,
80 const CompletionCallback& callback);
82 // Cancels any pending stream creation request. May be called
83 // repeatedly.
84 void CancelRequest();
86 private:
87 friend class QuicChromiumClientSession;
89 // Called by |session_| for an asynchronous request when the stream
90 // request has finished successfully.
91 void OnRequestCompleteSuccess(QuicReliableClientStream* stream);
93 // Called by |session_| for an asynchronous request when the stream
94 // request has finished with an error. Also called with ERR_ABORTED
95 // if |session_| is destroyed while the stream request is still pending.
96 void OnRequestCompleteFailure(int rv);
98 base::WeakPtr<QuicChromiumClientSession> session_;
99 CompletionCallback callback_;
100 QuicReliableClientStream** stream_;
102 DISALLOW_COPY_AND_ASSIGN(StreamRequest);
105 // Constructs a new session which will own |connection|, but not
106 // |stream_factory|, which must outlive this session.
107 // TODO(rch): decouple the factory from the session via a Delegate interface.
108 QuicChromiumClientSession(
109 QuicConnection* connection,
110 scoped_ptr<DatagramClientSocket> socket,
111 QuicStreamFactory* stream_factory,
112 QuicCryptoClientStreamFactory* crypto_client_stream_factory,
113 TransportSecurityState* transport_security_state,
114 scoped_ptr<QuicServerInfo> server_info,
115 const QuicServerId& server_id,
116 int cert_verify_flags,
117 const QuicConfig& config,
118 QuicCryptoClientConfig* crypto_config,
119 const char* const connection_description,
120 base::TimeTicks dns_resolution_end_time,
121 base::TaskRunner* task_runner,
122 NetLog* net_log);
123 ~QuicChromiumClientSession() override;
125 void AddObserver(Observer* observer);
126 void RemoveObserver(Observer* observer);
128 // Attempts to create a new stream. If the stream can be
129 // created immediately, returns OK. If the open stream limit
130 // has been reached, returns ERR_IO_PENDING, and |request|
131 // will be added to the stream requets queue and will
132 // be completed asynchronously.
133 // TODO(rch): remove |stream| from this and use setter on |request|
134 // and fix in spdy too.
135 int TryCreateStream(StreamRequest* request,
136 QuicReliableClientStream** stream);
138 // Cancels the pending stream creation request.
139 void CancelRequest(StreamRequest* request);
141 // QuicSession methods:
142 void OnStreamFrame(const QuicStreamFrame& frame) override;
143 QuicReliableClientStream* CreateOutgoingDynamicStream() override;
144 QuicCryptoClientStream* GetCryptoStream() override;
145 void CloseStream(QuicStreamId stream_id) override;
146 void SendRstStream(QuicStreamId id,
147 QuicRstStreamErrorCode error,
148 QuicStreamOffset bytes_written) override;
149 void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) override;
150 void OnCryptoHandshakeMessageSent(
151 const CryptoHandshakeMessage& message) override;
152 void OnCryptoHandshakeMessageReceived(
153 const CryptoHandshakeMessage& message) override;
155 // QuicClientSessionBase methods:
156 void OnProofValid(const QuicCryptoClientConfig::CachedState& cached) override;
157 void OnProofVerifyDetailsAvailable(
158 const ProofVerifyDetails& verify_details) override;
160 // QuicConnectionVisitorInterface methods:
161 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override;
162 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override;
164 // QuicPacketReader::Visitor methods:
165 void OnReadError(int result) override;
166 bool OnPacket(const QuicEncryptedPacket& packet,
167 IPEndPoint local_address,
168 IPEndPoint peer_address) override;
170 // Gets the SSL connection information.
171 bool GetSSLInfo(SSLInfo* ssl_info) const;
173 // Performs a crypto handshake with the server.
174 int CryptoConnect(bool require_confirmation,
175 const CompletionCallback& callback);
177 // Resumes a crypto handshake with the server after a timeout.
178 int ResumeCryptoConnect(const CompletionCallback& callback);
180 // Causes the QuicConnectionHelper to start reading from the socket
181 // and passing the data along to the QuicConnection.
182 void StartReading();
184 // Close the session because of |error| and notifies the factory
185 // that this session has been closed, which will delete the session.
186 void CloseSessionOnError(int error, QuicErrorCode quic_error);
188 // Close the session because of |error| and notifies the factory later that
189 // this session has been closed, which will delete the session.
190 void CloseSessionOnErrorAndNotifyFactoryLater(int error,
191 QuicErrorCode quic_error);
193 scoped_ptr<base::Value> GetInfoAsValue(const std::set<HostPortPair>& aliases);
195 const BoundNetLog& net_log() const { return net_log_; }
197 base::WeakPtr<QuicChromiumClientSession> GetWeakPtr();
199 // Returns the number of client hello messages that have been sent on the
200 // crypto stream. If the handshake has completed then this is one greater
201 // than the number of round-trips needed for the handshake.
202 int GetNumSentClientHellos() const;
204 // Returns true if |hostname| may be pooled onto this session. If this
205 // is a secure QUIC session, then |hostname| must match the certificate
206 // presented during the handshake.
207 bool CanPool(const std::string& hostname, PrivacyMode privacy_mode) const;
209 const QuicServerId& server_id() const { return server_id_; }
211 QuicDisabledReason disabled_reason() const { return disabled_reason_; }
213 protected:
214 // QuicSession methods:
215 QuicDataStream* CreateIncomingDynamicStream(QuicStreamId id) override;
217 private:
218 friend class test::QuicChromiumClientSessionPeer;
220 typedef std::set<Observer*> ObserverSet;
221 typedef std::list<StreamRequest*> StreamRequestQueue;
223 QuicReliableClientStream* CreateOutgoingReliableStreamImpl();
224 // A completion callback invoked when a read completes.
225 void OnReadComplete(int result);
227 void OnClosedStream();
229 // Close the session because of |error| and records it in UMA histogram.
230 void RecordAndCloseSessionOnError(int error, QuicErrorCode quic_error);
232 // A Session may be closed via any of three methods:
233 // OnConnectionClosed - called by the connection when the connection has been
234 // closed, perhaps due to a timeout or a protocol error.
235 // CloseSessionOnError - called from the owner of the session,
236 // the QuicStreamFactory, when there is an error.
237 // OnReadComplete - when there is a read error.
238 // This method closes all stream and performs any necessary cleanup.
239 void CloseSessionOnErrorInner(int net_error, QuicErrorCode quic_error);
241 void CloseAllStreams(int net_error);
242 void CloseAllObservers(int net_error);
244 // Notifies the factory that this session is going away and no more streams
245 // should be created from it. This needs to be called before closing any
246 // streams, because closing a stream may cause a new stream to be created.
247 void NotifyFactoryOfSessionGoingAway();
249 // Posts a task to notify the factory that this session has been closed.
250 void NotifyFactoryOfSessionClosedLater();
252 // Notifies the factory that this session has been closed which will
253 // delete |this|.
254 void NotifyFactoryOfSessionClosed();
256 void OnConnectTimeout();
258 QuicServerId server_id_;
259 bool require_confirmation_;
260 scoped_ptr<QuicCryptoClientStream> crypto_stream_;
261 QuicStreamFactory* stream_factory_;
262 scoped_ptr<DatagramClientSocket> socket_;
263 TransportSecurityState* transport_security_state_;
264 scoped_ptr<QuicServerInfo> server_info_;
265 scoped_ptr<CertVerifyResult> cert_verify_result_;
266 std::string pinning_failure_log_;
267 ObserverSet observers_;
268 StreamRequestQueue stream_requests_;
269 CompletionCallback callback_;
270 size_t num_total_streams_;
271 base::TaskRunner* task_runner_;
272 BoundNetLog net_log_;
273 QuicPacketReader packet_reader_;
274 base::TimeTicks dns_resolution_end_time_;
275 base::TimeTicks handshake_start_; // Time the handshake was started.
276 scoped_ptr<QuicConnectionLogger> logger_;
277 // True when the session is going away, and streams may no longer be created
278 // on this session. Existing stream will continue to be processed.
279 bool going_away_;
280 QuicDisabledReason disabled_reason_;
281 base::WeakPtrFactory<QuicChromiumClientSession> weak_factory_;
283 DISALLOW_COPY_AND_ASSIGN(QuicChromiumClientSession);
286 } // namespace net
288 #endif // NET_QUIC_QUIC_CHROMIUM_CLIENT_SESSION_H_