blob: b7936c34cc47218559532b37b436d344b97a2e45 [file] [log] [blame]
[email protected]e13201d82012-12-12 05:00:321// 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#ifndef NET_QUIC_QUIC_STREAM_FACTORY_H_
6#define NET_QUIC_QUIC_STREAM_FACTORY_H_
7
[email protected]1cd2a5f2014-03-14 06:33:258#include <list>
[email protected]e13201d82012-12-12 05:00:329#include <map>
[email protected]41d6b172013-01-29 16:10:5710#include <string>
[email protected]6e12d702013-11-13 00:17:1711#include <vector>
[email protected]e13201d82012-12-12 05:00:3212
[email protected]e8cf7555b2014-02-28 23:52:5313#include "base/logging.h"
[email protected]e13201d82012-12-12 05:00:3214#include "base/memory/weak_ptr.h"
15#include "net/base/address_list.h"
16#include "net/base/completion_callback.h"
17#include "net/base/host_port_pair.h"
18#include "net/base/net_log.h"
[email protected]f698a012013-05-06 20:18:5919#include "net/base/network_change_notifier.h"
[email protected]d7d1e50b2013-11-25 22:08:0920#include "net/cert/cert_database.h"
[email protected]e13201d82012-12-12 05:00:3221#include "net/proxy/proxy_server.h"
[email protected]ef95114d2013-04-17 17:57:0122#include "net/quic/quic_config.h"
23#include "net/quic/quic_crypto_stream.h"
[email protected]e13201d82012-12-12 05:00:3224#include "net/quic/quic_http_stream.h"
25#include "net/quic/quic_protocol.h"
26
27namespace net {
28
[email protected]6d1b4ed2013-07-10 03:57:5429class CertVerifier;
[email protected]e13201d82012-12-12 05:00:3230class ClientSocketFactory;
[email protected]6d1b4ed2013-07-10 03:57:5431class HostResolver;
[email protected]77c6c162013-08-17 02:57:4532class HttpServerProperties;
[email protected]e13201d82012-12-12 05:00:3233class QuicClock;
34class QuicClientSession;
[email protected]2cfc6bb82013-10-27 03:40:4435class QuicConnectionHelper;
[email protected]e8ff26842013-03-22 21:02:0536class QuicCryptoClientStreamFactory;
[email protected]9558c5d32012-12-22 00:08:1437class QuicRandom;
[email protected]7832eeb2014-01-25 10:10:4338class QuicServerInfoFactory;
[email protected]257f24f2014-04-01 09:15:3739class QuicServerId;
[email protected]e13201d82012-12-12 05:00:3240class QuicStreamFactory;
41
[email protected]c49ff182013-09-28 08:33:2642namespace test {
43class QuicStreamFactoryPeer;
44} // namespace test
45
[email protected]e13201d82012-12-12 05:00:3246// Encapsulates a pending request for a QuicHttpStream.
47// If the request is still pending when it is destroyed, it will
48// cancel the request with the factory.
49class NET_EXPORT_PRIVATE QuicStreamRequest {
50 public:
51 explicit QuicStreamRequest(QuicStreamFactory* factory);
52 ~QuicStreamRequest();
53
[email protected]6d1b4ed2013-07-10 03:57:5454 // For http, |is_https| is false and |cert_verifier| can be null.
[email protected]bf4ea2f2014-03-10 22:57:5355 int Request(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:5456 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:2857 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:5958 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:3259 const BoundNetLog& net_log,
60 const CompletionCallback& callback);
61
62 void OnRequestComplete(int rv);
63
64 scoped_ptr<QuicHttpStream> ReleaseStream();
65
66 void set_stream(scoped_ptr<QuicHttpStream> stream);
67
68 const BoundNetLog& net_log() const{
69 return net_log_;
70 }
71
72 private:
73 QuicStreamFactory* factory_;
[email protected]bf4ea2f2014-03-10 22:57:5374 HostPortPair host_port_pair_;
[email protected]6d1b4ed2013-07-10 03:57:5475 bool is_https_;
[email protected]e13201d82012-12-12 05:00:3276 BoundNetLog net_log_;
77 CompletionCallback callback_;
78 scoped_ptr<QuicHttpStream> stream_;
79
80 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
81};
82
83// A factory for creating new QuicHttpStreams on top of a pool of
84// QuicClientSessions.
[email protected]f698a012013-05-06 20:18:5985class NET_EXPORT_PRIVATE QuicStreamFactory
[email protected]d7d1e50b2013-11-25 22:08:0986 : public NetworkChangeNotifier::IPAddressObserver,
87 public CertDatabase::Observer {
[email protected]e13201d82012-12-12 05:00:3288 public:
[email protected]e8ff26842013-03-22 21:02:0589 QuicStreamFactory(
90 HostResolver* host_resolver,
91 ClientSocketFactory* client_socket_factory,
[email protected]77c6c162013-08-17 02:57:4592 base::WeakPtr<HttpServerProperties> http_server_properties,
[email protected]59c0bbd2014-03-22 04:08:1293 CertVerifier* cert_verifier,
[email protected]e8ff26842013-03-22 21:02:0594 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
95 QuicRandom* random_generator,
[email protected]256fe9b2013-11-27 01:58:0296 QuicClock* clock,
[email protected]1e960032013-12-20 19:00:2097 size_t max_packet_length,
[email protected]376d38a2014-01-22 03:47:3598 const QuicVersionVector& supported_versions,
[email protected]c80f7c92014-02-27 13:12:0299 bool enable_port_selection,
[email protected]ebb3ad52014-04-30 20:01:39100 bool enable_pacing,
101 bool enable_time_based_loss_detection);
[email protected]e13201d82012-12-12 05:00:32102 virtual ~QuicStreamFactory();
103
[email protected]bf4ea2f2014-03-10 22:57:53104 // Creates a new QuicHttpStream to |host_port_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:54105 // owned by |request|. |is_https| specifies if the protocol is https or not.
106 // |cert_verifier| is used by ProofVerifier for verifying the certificate
107 // chain and signature. For http, this can be null. If a matching session
108 // already exists, this method will return OK. If no matching session exists,
109 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
110 // asynchronously.
[email protected]bf4ea2f2014-03-10 22:57:53111 int Create(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:54112 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:28113 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:59114 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:32115 const BoundNetLog& net_log,
116 QuicStreamRequest* request);
117
[email protected]e13201d82012-12-12 05:00:32118 // Called by a session when it becomes idle.
119 void OnIdleSession(QuicClientSession* session);
120
[email protected]4d283b32013-10-17 12:57:27121 // Called by a session when it is going away and no more streams should be
122 // created on it.
123 void OnSessionGoingAway(QuicClientSession* session);
124
[email protected]e13201d82012-12-12 05:00:32125 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27126 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32127
128 // Cancels a pending request.
129 void CancelRequest(QuicStreamRequest* request);
130
[email protected]56dfb902013-01-03 23:17:55131 // Closes all current sessions.
132 void CloseAllSessions(int error);
133
[email protected]c5b061b2013-01-05 00:31:34134 base::Value* QuicStreamFactoryInfoToValue() const;
135
[email protected]f7e21a432014-04-21 22:17:57136 // Delete all cached state objects in |crypto_config_|.
[email protected]60cf50e2014-04-28 23:23:18137 void ClearCachedStatesInCryptoConfig();
[email protected]f7e21a432014-04-21 22:17:57138
[email protected]f698a012013-05-06 20:18:59139 // NetworkChangeNotifier::IPAddressObserver methods:
140
141 // Until the servers support roaming, close all connections when the local
142 // IP address changes.
143 virtual void OnIPAddressChanged() OVERRIDE;
144
[email protected]d7d1e50b2013-11-25 22:08:09145 // CertDatabase::Observer methods:
146
147 // We close all sessions when certificate database is changed.
148 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
149 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
150
[email protected]11c05872013-08-20 02:04:12151 bool require_confirmation() const { return require_confirmation_; }
152
153 void set_require_confirmation(bool require_confirmation) {
154 require_confirmation_ = require_confirmation;
155 }
156
[email protected]2cfc6bb82013-10-27 03:40:44157 QuicConnectionHelper* helper() { return helper_.get(); }
158
[email protected]376d38a2014-01-22 03:47:35159 bool enable_port_selection() const { return enable_port_selection_; }
160
[email protected]e8cf7555b2014-02-28 23:52:53161 void set_quic_server_info_factory(
162 QuicServerInfoFactory* quic_server_info_factory) {
163 DCHECK(!quic_server_info_factory_);
164 quic_server_info_factory_ = quic_server_info_factory;
165 }
166
[email protected]c80f7c92014-02-27 13:12:02167 bool enable_pacing() const { return enable_pacing_; }
168
[email protected]e13201d82012-12-12 05:00:32169 private:
170 class Job;
[email protected]c49ff182013-09-28 08:33:26171 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32172
[email protected]9dd3ff0f2014-03-26 09:51:28173 // The key used to find session by ip. Includes
[email protected]df157d9d2014-03-10 07:27:27174 // the ip address, port, and scheme.
175 struct NET_EXPORT_PRIVATE IpAliasKey {
176 IpAliasKey();
177 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
178 ~IpAliasKey();
179
180 IPEndPoint ip_endpoint;
181 bool is_https;
182
183 // Needed to be an element of std::set.
184 bool operator<(const IpAliasKey &other) const;
185 bool operator==(const IpAliasKey &other) const;
186 };
187
[email protected]257f24f2014-04-01 09:15:37188 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
[email protected]4d590c9c2014-05-02 05:14:33189 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
[email protected]257f24f2014-04-01 09:15:37190 typedef std::set<QuicServerId> AliasSet;
[email protected]e13201d82012-12-12 05:00:32191 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
192 typedef std::set<QuicClientSession*> SessionSet;
[email protected]df157d9d2014-03-10 07:27:27193 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
[email protected]257f24f2014-04-01 09:15:37194 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
195 typedef std::map<QuicServerId, Job*> JobMap;
[email protected]e13201d82012-12-12 05:00:32196 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
197 typedef std::set<QuicStreamRequest*> RequestSet;
198 typedef std::map<Job*, RequestSet> JobRequestsMap;
199
[email protected]df157d9d2014-03-10 07:27:27200 // Returns a newly created QuicHttpStream owned by the caller, if a
201 // matching session already exists. Returns NULL otherwise.
[email protected]257f24f2014-04-01 09:15:37202 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
[email protected]df157d9d2014-03-10 07:27:27203 const BoundNetLog& net_log);
204
[email protected]257f24f2014-04-01 09:15:37205 bool OnResolution(const QuicServerId& server_id,
[email protected]eed749f92013-12-23 18:57:38206 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32207 void OnJobComplete(Job* job, int rv);
[email protected]257f24f2014-04-01 09:15:37208 bool HasActiveSession(const QuicServerId& server_id) const;
209 bool HasActiveJob(const QuicServerId& server_id) const;
210 int CreateSession(const QuicServerId& server_id,
[email protected]17bf15c2014-03-14 10:08:04211 scoped_ptr<QuicServerInfo> quic_server_info,
[email protected]338e7982013-12-13 11:15:32212 const AddressList& address_list,
213 const BoundNetLog& net_log,
214 QuicClientSession** session);
[email protected]257f24f2014-04-01 09:15:37215 void ActivateSession(const QuicServerId& key,
[email protected]e13201d82012-12-12 05:00:32216 QuicClientSession* session);
217
[email protected]257f24f2014-04-01 09:15:37218 // Initializes the cached state associated with |server_id| in
[email protected]59c0bbd2014-03-22 04:08:12219 // |crypto_config_| with the information in |server_info|.
[email protected]60cf50e2014-04-28 23:23:18220 void InitializeCachedStateInCryptoConfig(
221 const QuicServerId& server_id,
222 const scoped_ptr<QuicServerInfo>& server_info);
[email protected]b694e48c2014-03-18 17:10:13223
[email protected]4d590c9c2014-05-02 05:14:33224 void ProcessGoingAwaySession(QuicClientSession* session,
225 const QuicServerId& server_id);
226
[email protected]11c05872013-08-20 02:04:12227 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32228 HostResolver* host_resolver_;
229 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45230 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]59c0bbd2014-03-22 04:08:12231 CertVerifier* cert_verifier_;
[email protected]7832eeb2014-01-25 10:10:43232 QuicServerInfoFactory* quic_server_info_factory_;
[email protected]e8ff26842013-03-22 21:02:05233 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14234 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25235 scoped_ptr<QuicClock> clock_;
[email protected]256fe9b2013-11-27 01:58:02236 const size_t max_packet_length_;
[email protected]e13201d82012-12-12 05:00:32237
[email protected]2cfc6bb82013-10-27 03:40:44238 // The helper used for all connections.
239 scoped_ptr<QuicConnectionHelper> helper_;
240
[email protected]e13201d82012-12-12 05:00:32241 // Contains owning pointers to all sessions that currently exist.
[email protected]4d590c9c2014-05-02 05:14:33242 SessionIdMap all_sessions_;
[email protected]e13201d82012-12-12 05:00:32243 // Contains non-owning pointers to currently active session
244 // (not going away session, once they're implemented).
245 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38246 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32247 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38248 // Map from IP address to sessions which are connected to this address.
249 IPAliasMap ip_aliases_;
[email protected]e13201d82012-12-12 05:00:32250
[email protected]d8e2abf82014-03-06 10:30:10251 // Origins which have gone away recently.
252 AliasSet gone_away_aliases_;
253
[email protected]b064310782013-05-30 21:12:17254 QuicConfig config_;
[email protected]59c0bbd2014-03-22 04:08:12255 QuicCryptoClientConfig crypto_config_;
[email protected]b064310782013-05-30 21:12:17256
[email protected]e13201d82012-12-12 05:00:32257 JobMap active_jobs_;
258 JobRequestsMap job_requests_map_;
259 RequestMap active_requests_;
260
[email protected]1e960032013-12-20 19:00:20261 QuicVersionVector supported_versions_;
[email protected]e13201d82012-12-12 05:00:32262
[email protected]376d38a2014-01-22 03:47:35263 // Determine if we should consistently select a client UDP port. If false,
264 // then we will just let the OS select a random client port for each new
265 // connection.
266 bool enable_port_selection_;
267
[email protected]c80f7c92014-02-27 13:12:02268 // True if packet pacing should be advertised during the crypto handshake.
269 bool enable_pacing_;
270
[email protected]337e1452013-12-16 23:57:50271 // Each profile will (probably) have a unique port_seed_ value. This value is
272 // used to help seed a pseudo-random number generator (PortSuggester) so that
273 // we consistently (within this profile) suggest the same ephemeral port when
274 // we re-connect to any given server/port. The differences between profiles
275 // (probablistically) prevent two profiles from colliding in their ephemeral
276 // port requests.
277 uint64 port_seed_;
[email protected]7034cf12013-12-13 22:47:07278
[email protected]1e960032013-12-20 19:00:20279 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
280
[email protected]e13201d82012-12-12 05:00:32281 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
282};
283
284} // namespace net
285
286#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_