blob: b8623099e4b6f7fb85d317957ed12bf6b8d1dd40 [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]6b8a3c742014-07-25 00:25:3530class ChannelIDService;
[email protected]e13201d82012-12-12 05:00:3231class ClientSocketFactory;
[email protected]6d1b4ed2013-07-10 03:57:5432class HostResolver;
[email protected]77c6c162013-08-17 02:57:4533class HttpServerProperties;
[email protected]e13201d82012-12-12 05:00:3234class QuicClock;
35class QuicClientSession;
[email protected]2cfc6bb82013-10-27 03:40:4436class QuicConnectionHelper;
[email protected]e8ff26842013-03-22 21:02:0537class QuicCryptoClientStreamFactory;
[email protected]9558c5d32012-12-22 00:08:1438class QuicRandom;
[email protected]7832eeb2014-01-25 10:10:4339class QuicServerInfoFactory;
[email protected]257f24f2014-04-01 09:15:3740class QuicServerId;
[email protected]e13201d82012-12-12 05:00:3241class QuicStreamFactory;
[email protected]080b77932014-08-04 01:22:4642class TransportSecurityState;
[email protected]e13201d82012-12-12 05:00:3243
[email protected]c49ff182013-09-28 08:33:2644namespace test {
45class QuicStreamFactoryPeer;
46} // namespace test
47
[email protected]e13201d82012-12-12 05:00:3248// Encapsulates a pending request for a QuicHttpStream.
49// If the request is still pending when it is destroyed, it will
50// cancel the request with the factory.
51class NET_EXPORT_PRIVATE QuicStreamRequest {
52 public:
53 explicit QuicStreamRequest(QuicStreamFactory* factory);
54 ~QuicStreamRequest();
55
[email protected]0cceb922014-07-01 02:00:5656 // For http, |is_https| is false.
[email protected]bf4ea2f2014-03-10 22:57:5357 int Request(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:5458 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:2859 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:5960 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:3261 const BoundNetLog& net_log,
62 const CompletionCallback& callback);
63
64 void OnRequestComplete(int rv);
65
66 scoped_ptr<QuicHttpStream> ReleaseStream();
67
68 void set_stream(scoped_ptr<QuicHttpStream> stream);
69
70 const BoundNetLog& net_log() const{
71 return net_log_;
72 }
73
74 private:
75 QuicStreamFactory* factory_;
[email protected]bf4ea2f2014-03-10 22:57:5376 HostPortPair host_port_pair_;
[email protected]6d1b4ed2013-07-10 03:57:5477 bool is_https_;
[email protected]e13201d82012-12-12 05:00:3278 BoundNetLog net_log_;
79 CompletionCallback callback_;
80 scoped_ptr<QuicHttpStream> stream_;
81
82 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
83};
84
85// A factory for creating new QuicHttpStreams on top of a pool of
86// QuicClientSessions.
[email protected]f698a012013-05-06 20:18:5987class NET_EXPORT_PRIVATE QuicStreamFactory
[email protected]d7d1e50b2013-11-25 22:08:0988 : public NetworkChangeNotifier::IPAddressObserver,
89 public CertDatabase::Observer {
[email protected]e13201d82012-12-12 05:00:3290 public:
[email protected]e8ff26842013-03-22 21:02:0591 QuicStreamFactory(
92 HostResolver* host_resolver,
93 ClientSocketFactory* client_socket_factory,
[email protected]77c6c162013-08-17 02:57:4594 base::WeakPtr<HttpServerProperties> http_server_properties,
[email protected]59c0bbd2014-03-22 04:08:1295 CertVerifier* cert_verifier,
[email protected]6b8a3c742014-07-25 00:25:3596 ChannelIDService* channel_id_service,
[email protected]080b77932014-08-04 01:22:4697 TransportSecurityState* transport_security_state,
[email protected]e8ff26842013-03-22 21:02:0598 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
99 QuicRandom* random_generator,
[email protected]256fe9b2013-11-27 01:58:02100 QuicClock* clock,
[email protected]1e960032013-12-20 19:00:20101 size_t max_packet_length,
[email protected]0c4017ca2014-06-06 03:30:45102 const std::string& user_agent_id,
[email protected]376d38a2014-01-22 03:47:35103 const QuicVersionVector& supported_versions,
[email protected]c80f7c92014-02-27 13:12:02104 bool enable_port_selection,
jri2b966f22014-09-02 22:25:36105 bool always_require_handshake_confirmation,
jri584002d12014-09-09 00:51:28106 bool disable_connection_pooling,
rtenneti38f5cd52014-10-28 20:28:28107 int load_server_info_timeout,
rtennetib4af4ed2014-12-19 00:35:58108 bool disable_loading_server_info_for_new_servers,
rtenneti2912825c2015-01-06 01:19:46109 float load_server_info_timeout_srtt_multiplier,
[email protected]4b4efab32014-07-01 02:36:16110 const QuicTagVector& connection_options);
dchengb03027d2014-10-21 12:00:20111 ~QuicStreamFactory() override;
[email protected]e13201d82012-12-12 05:00:32112
[email protected]bf4ea2f2014-03-10 22:57:53113 // Creates a new QuicHttpStream to |host_port_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:54114 // owned by |request|. |is_https| specifies if the protocol is https or not.
[email protected]0cceb922014-07-01 02:00:56115 // If a matching session already exists, this method will return OK. If no
116 // matching session exists, this will return ERR_IO_PENDING and will invoke
117 // OnRequestComplete asynchronously.
[email protected]bf4ea2f2014-03-10 22:57:53118 int Create(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:54119 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:28120 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:59121 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:32122 const BoundNetLog& net_log,
123 QuicStreamRequest* request);
124
[email protected]e13201d82012-12-12 05:00:32125 // Called by a session when it becomes idle.
126 void OnIdleSession(QuicClientSession* session);
127
[email protected]4d283b32013-10-17 12:57:27128 // Called by a session when it is going away and no more streams should be
129 // created on it.
130 void OnSessionGoingAway(QuicClientSession* session);
131
[email protected]e13201d82012-12-12 05:00:32132 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27133 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32134
[email protected]65768442014-06-06 23:37:03135 // Called by a session whose connection has timed out.
136 void OnSessionConnectTimeout(QuicClientSession* session);
137
[email protected]e13201d82012-12-12 05:00:32138 // Cancels a pending request.
139 void CancelRequest(QuicStreamRequest* request);
140
[email protected]56dfb902013-01-03 23:17:55141 // Closes all current sessions.
142 void CloseAllSessions(int error);
143
[email protected]c5b061b2013-01-05 00:31:34144 base::Value* QuicStreamFactoryInfoToValue() const;
145
[email protected]f7e21a432014-04-21 22:17:57146 // Delete all cached state objects in |crypto_config_|.
[email protected]60cf50e2014-04-28 23:23:18147 void ClearCachedStatesInCryptoConfig();
[email protected]f7e21a432014-04-21 22:17:57148
[email protected]f698a012013-05-06 20:18:59149 // NetworkChangeNotifier::IPAddressObserver methods:
150
151 // Until the servers support roaming, close all connections when the local
152 // IP address changes.
dchengb03027d2014-10-21 12:00:20153 void OnIPAddressChanged() override;
[email protected]f698a012013-05-06 20:18:59154
[email protected]d7d1e50b2013-11-25 22:08:09155 // CertDatabase::Observer methods:
156
157 // We close all sessions when certificate database is changed.
dchengb03027d2014-10-21 12:00:20158 void OnCertAdded(const X509Certificate* cert) override;
159 void OnCACertChanged(const X509Certificate* cert) override;
[email protected]d7d1e50b2013-11-25 22:08:09160
jri2b966f22014-09-02 22:25:36161 bool require_confirmation() const {
162 return require_confirmation_;
163 }
[email protected]11c05872013-08-20 02:04:12164
rtennetifc47e0e2014-09-26 02:54:05165 void set_require_confirmation(bool require_confirmation);
[email protected]11c05872013-08-20 02:04:12166
[email protected]2cfc6bb82013-10-27 03:40:44167 QuicConnectionHelper* helper() { return helper_.get(); }
168
[email protected]376d38a2014-01-22 03:47:35169 bool enable_port_selection() const { return enable_port_selection_; }
170
[email protected]a4205202014-06-02 16:03:08171 bool has_quic_server_info_factory() {
172 return quic_server_info_factory_ != NULL;
173 }
174
[email protected]e8cf7555b2014-02-28 23:52:53175 void set_quic_server_info_factory(
176 QuicServerInfoFactory* quic_server_info_factory) {
177 DCHECK(!quic_server_info_factory_);
178 quic_server_info_factory_ = quic_server_info_factory;
179 }
180
[email protected]e13201d82012-12-12 05:00:32181 private:
182 class Job;
[email protected]c49ff182013-09-28 08:33:26183 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32184
[email protected]9dd3ff0f2014-03-26 09:51:28185 // The key used to find session by ip. Includes
[email protected]df157d9d2014-03-10 07:27:27186 // the ip address, port, and scheme.
187 struct NET_EXPORT_PRIVATE IpAliasKey {
188 IpAliasKey();
189 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
190 ~IpAliasKey();
191
192 IPEndPoint ip_endpoint;
193 bool is_https;
194
195 // Needed to be an element of std::set.
196 bool operator<(const IpAliasKey &other) const;
197 bool operator==(const IpAliasKey &other) const;
198 };
199
[email protected]257f24f2014-04-01 09:15:37200 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
[email protected]4d590c9c2014-05-02 05:14:33201 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
[email protected]257f24f2014-04-01 09:15:37202 typedef std::set<QuicServerId> AliasSet;
[email protected]e13201d82012-12-12 05:00:32203 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
204 typedef std::set<QuicClientSession*> SessionSet;
[email protected]df157d9d2014-03-10 07:27:27205 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
[email protected]257f24f2014-04-01 09:15:37206 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
207 typedef std::map<QuicServerId, Job*> JobMap;
[email protected]e13201d82012-12-12 05:00:32208 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
209 typedef std::set<QuicStreamRequest*> RequestSet;
210 typedef std::map<Job*, RequestSet> JobRequestsMap;
211
[email protected]df157d9d2014-03-10 07:27:27212 // Returns a newly created QuicHttpStream owned by the caller, if a
213 // matching session already exists. Returns NULL otherwise.
[email protected]257f24f2014-04-01 09:15:37214 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
[email protected]df157d9d2014-03-10 07:27:27215 const BoundNetLog& net_log);
216
[email protected]257f24f2014-04-01 09:15:37217 bool OnResolution(const QuicServerId& server_id,
[email protected]eed749f92013-12-23 18:57:38218 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32219 void OnJobComplete(Job* job, int rv);
[email protected]257f24f2014-04-01 09:15:37220 bool HasActiveSession(const QuicServerId& server_id) const;
221 bool HasActiveJob(const QuicServerId& server_id) const;
222 int CreateSession(const QuicServerId& server_id,
[email protected]17bf15c2014-03-14 10:08:04223 scoped_ptr<QuicServerInfo> quic_server_info,
[email protected]338e7982013-12-13 11:15:32224 const AddressList& address_list,
225 const BoundNetLog& net_log,
226 QuicClientSession** session);
[email protected]257f24f2014-04-01 09:15:37227 void ActivateSession(const QuicServerId& key,
[email protected]e13201d82012-12-12 05:00:32228 QuicClientSession* session);
229
rtenneti2912825c2015-01-06 01:19:46230 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there
231 // is no |http_server_properties_| or if |http_server_properties_| doesn't
232 // have ServerNetworkStats for the given |server_id|.
233 int64 GetServerNetworkStatsSmoothedRttInMicroseconds(
234 const QuicServerId& server_id) const;
235
[email protected]257f24f2014-04-01 09:15:37236 // Initializes the cached state associated with |server_id| in
[email protected]59c0bbd2014-03-22 04:08:12237 // |crypto_config_| with the information in |server_info|.
[email protected]60cf50e2014-04-28 23:23:18238 void InitializeCachedStateInCryptoConfig(
239 const QuicServerId& server_id,
240 const scoped_ptr<QuicServerInfo>& server_info);
[email protected]b694e48c2014-03-18 17:10:13241
[email protected]4d590c9c2014-05-02 05:14:33242 void ProcessGoingAwaySession(QuicClientSession* session,
[email protected]eb71ab62014-05-23 07:57:53243 const QuicServerId& server_id,
244 bool was_session_active);
[email protected]4d590c9c2014-05-02 05:14:33245
[email protected]11c05872013-08-20 02:04:12246 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32247 HostResolver* host_resolver_;
248 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45249 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]5db452202014-08-19 05:22:15250 TransportSecurityState* transport_security_state_;
[email protected]7832eeb2014-01-25 10:10:43251 QuicServerInfoFactory* quic_server_info_factory_;
[email protected]e8ff26842013-03-22 21:02:05252 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14253 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25254 scoped_ptr<QuicClock> clock_;
[email protected]256fe9b2013-11-27 01:58:02255 const size_t max_packet_length_;
[email protected]e13201d82012-12-12 05:00:32256
[email protected]2cfc6bb82013-10-27 03:40:44257 // The helper used for all connections.
258 scoped_ptr<QuicConnectionHelper> helper_;
259
[email protected]e13201d82012-12-12 05:00:32260 // Contains owning pointers to all sessions that currently exist.
[email protected]4d590c9c2014-05-02 05:14:33261 SessionIdMap all_sessions_;
[email protected]e13201d82012-12-12 05:00:32262 // Contains non-owning pointers to currently active session
263 // (not going away session, once they're implemented).
264 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38265 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32266 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38267 // Map from IP address to sessions which are connected to this address.
268 IPAliasMap ip_aliases_;
[email protected]e13201d82012-12-12 05:00:32269
[email protected]d8e2abf82014-03-06 10:30:10270 // Origins which have gone away recently.
271 AliasSet gone_away_aliases_;
272
[email protected]fd276a282014-06-11 04:26:14273 const QuicConfig config_;
[email protected]59c0bbd2014-03-22 04:08:12274 QuicCryptoClientConfig crypto_config_;
[email protected]b064310782013-05-30 21:12:17275
[email protected]e13201d82012-12-12 05:00:32276 JobMap active_jobs_;
277 JobRequestsMap job_requests_map_;
278 RequestMap active_requests_;
279
[email protected]1e960032013-12-20 19:00:20280 QuicVersionVector supported_versions_;
[email protected]e13201d82012-12-12 05:00:32281
[email protected]376d38a2014-01-22 03:47:35282 // Determine if we should consistently select a client UDP port. If false,
283 // then we will just let the OS select a random client port for each new
284 // connection.
285 bool enable_port_selection_;
286
jri2b966f22014-09-02 22:25:36287 // Set if we always require handshake confirmation. If true, this will
288 // introduce at least one RTT for the handshake before the client sends data.
289 bool always_require_handshake_confirmation_;
290
jri584002d12014-09-09 00:51:28291 // Set if we do not want connection pooling.
292 bool disable_connection_pooling_;
293
rtenneti38f5cd52014-10-28 20:28:28294 // Specifies the timeout in milliseconds to wait for loading of QUIC server
295 // information. If we don't want to timeout, set
296 // |load_server_info_timeout_ms_| to 0.
297 int load_server_info_timeout_ms_;
298
rtennetib4af4ed2014-12-19 00:35:58299 // Set to disable loading of QUIC server information from disk cache for new
300 // servers. New servers are those servers for which there is no QUIC protocol
301 // entry in AlternateProtocolMap.
302 bool disable_loading_server_info_for_new_servers_;
303
rtenneti2912825c2015-01-06 01:19:46304 // Specifies the ratio between time to load QUIC server information from disk
305 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in
306 // milliseconds to wait for loading of QUIC server information. If we don't
307 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0.
308 float load_server_info_timeout_srtt_multiplier_;
309
[email protected]337e1452013-12-16 23:57:50310 // Each profile will (probably) have a unique port_seed_ value. This value is
311 // used to help seed a pseudo-random number generator (PortSuggester) so that
312 // we consistently (within this profile) suggest the same ephemeral port when
313 // we re-connect to any given server/port. The differences between profiles
314 // (probablistically) prevent two profiles from colliding in their ephemeral
315 // port requests.
316 uint64 port_seed_;
[email protected]7034cf12013-12-13 22:47:07317
rtennetifc47e0e2014-09-26 02:54:05318 // Local address of socket that was created in CreateSession.
319 IPEndPoint local_address_;
320 bool check_persisted_supports_quic_;
rtenneti1681f852014-11-13 20:34:03321 std::set<HostPortPair> quic_supported_servers_at_startup_;
rtennetifc47e0e2014-09-26 02:54:05322
rtenneti38f5cd52014-10-28 20:28:28323 base::TaskRunner* task_runner_;
324
[email protected]1e960032013-12-20 19:00:20325 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
326
[email protected]e13201d82012-12-12 05:00:32327 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
328};
329
330} // namespace net
331
332#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_