blob: 7ee3681230b62c3b4c707b259f9ed195b854b242 [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,
[email protected]4b4efab32014-07-01 02:36:16107 const QuicTagVector& connection_options);
dchengb03027d2014-10-21 12:00:20108 ~QuicStreamFactory() override;
[email protected]e13201d82012-12-12 05:00:32109
[email protected]bf4ea2f2014-03-10 22:57:53110 // Creates a new QuicHttpStream to |host_port_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:54111 // owned by |request|. |is_https| specifies if the protocol is https or not.
[email protected]0cceb922014-07-01 02:00:56112 // If a matching session already exists, this method will return OK. If no
113 // matching session exists, this will return ERR_IO_PENDING and will invoke
114 // OnRequestComplete asynchronously.
[email protected]bf4ea2f2014-03-10 22:57:53115 int Create(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:54116 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:28117 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:59118 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:32119 const BoundNetLog& net_log,
120 QuicStreamRequest* request);
121
[email protected]e13201d82012-12-12 05:00:32122 // Called by a session when it becomes idle.
123 void OnIdleSession(QuicClientSession* session);
124
[email protected]4d283b32013-10-17 12:57:27125 // Called by a session when it is going away and no more streams should be
126 // created on it.
127 void OnSessionGoingAway(QuicClientSession* session);
128
[email protected]e13201d82012-12-12 05:00:32129 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27130 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32131
[email protected]65768442014-06-06 23:37:03132 // Called by a session whose connection has timed out.
133 void OnSessionConnectTimeout(QuicClientSession* session);
134
[email protected]e13201d82012-12-12 05:00:32135 // Cancels a pending request.
136 void CancelRequest(QuicStreamRequest* request);
137
[email protected]56dfb902013-01-03 23:17:55138 // Closes all current sessions.
139 void CloseAllSessions(int error);
140
[email protected]c5b061b2013-01-05 00:31:34141 base::Value* QuicStreamFactoryInfoToValue() const;
142
[email protected]f7e21a432014-04-21 22:17:57143 // Delete all cached state objects in |crypto_config_|.
[email protected]60cf50e2014-04-28 23:23:18144 void ClearCachedStatesInCryptoConfig();
[email protected]f7e21a432014-04-21 22:17:57145
[email protected]f698a012013-05-06 20:18:59146 // NetworkChangeNotifier::IPAddressObserver methods:
147
148 // Until the servers support roaming, close all connections when the local
149 // IP address changes.
dchengb03027d2014-10-21 12:00:20150 void OnIPAddressChanged() override;
[email protected]f698a012013-05-06 20:18:59151
[email protected]d7d1e50b2013-11-25 22:08:09152 // CertDatabase::Observer methods:
153
154 // We close all sessions when certificate database is changed.
dchengb03027d2014-10-21 12:00:20155 void OnCertAdded(const X509Certificate* cert) override;
156 void OnCACertChanged(const X509Certificate* cert) override;
[email protected]d7d1e50b2013-11-25 22:08:09157
jri2b966f22014-09-02 22:25:36158 bool require_confirmation() const {
159 return require_confirmation_;
160 }
[email protected]11c05872013-08-20 02:04:12161
rtennetifc47e0e2014-09-26 02:54:05162 void set_require_confirmation(bool require_confirmation);
[email protected]11c05872013-08-20 02:04:12163
[email protected]2cfc6bb82013-10-27 03:40:44164 QuicConnectionHelper* helper() { return helper_.get(); }
165
[email protected]376d38a2014-01-22 03:47:35166 bool enable_port_selection() const { return enable_port_selection_; }
167
[email protected]a4205202014-06-02 16:03:08168 bool has_quic_server_info_factory() {
169 return quic_server_info_factory_ != NULL;
170 }
171
[email protected]e8cf7555b2014-02-28 23:52:53172 void set_quic_server_info_factory(
173 QuicServerInfoFactory* quic_server_info_factory) {
174 DCHECK(!quic_server_info_factory_);
175 quic_server_info_factory_ = quic_server_info_factory;
176 }
177
[email protected]e13201d82012-12-12 05:00:32178 private:
179 class Job;
[email protected]c49ff182013-09-28 08:33:26180 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32181
[email protected]9dd3ff0f2014-03-26 09:51:28182 // The key used to find session by ip. Includes
[email protected]df157d9d2014-03-10 07:27:27183 // the ip address, port, and scheme.
184 struct NET_EXPORT_PRIVATE IpAliasKey {
185 IpAliasKey();
186 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
187 ~IpAliasKey();
188
189 IPEndPoint ip_endpoint;
190 bool is_https;
191
192 // Needed to be an element of std::set.
193 bool operator<(const IpAliasKey &other) const;
194 bool operator==(const IpAliasKey &other) const;
195 };
196
[email protected]257f24f2014-04-01 09:15:37197 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
[email protected]4d590c9c2014-05-02 05:14:33198 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
[email protected]257f24f2014-04-01 09:15:37199 typedef std::set<QuicServerId> AliasSet;
[email protected]e13201d82012-12-12 05:00:32200 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
201 typedef std::set<QuicClientSession*> SessionSet;
[email protected]df157d9d2014-03-10 07:27:27202 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
[email protected]257f24f2014-04-01 09:15:37203 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
204 typedef std::map<QuicServerId, Job*> JobMap;
[email protected]e13201d82012-12-12 05:00:32205 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
206 typedef std::set<QuicStreamRequest*> RequestSet;
207 typedef std::map<Job*, RequestSet> JobRequestsMap;
208
[email protected]df157d9d2014-03-10 07:27:27209 // Returns a newly created QuicHttpStream owned by the caller, if a
210 // matching session already exists. Returns NULL otherwise.
[email protected]257f24f2014-04-01 09:15:37211 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
[email protected]df157d9d2014-03-10 07:27:27212 const BoundNetLog& net_log);
213
[email protected]257f24f2014-04-01 09:15:37214 bool OnResolution(const QuicServerId& server_id,
[email protected]eed749f92013-12-23 18:57:38215 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32216 void OnJobComplete(Job* job, int rv);
[email protected]257f24f2014-04-01 09:15:37217 bool HasActiveSession(const QuicServerId& server_id) const;
218 bool HasActiveJob(const QuicServerId& server_id) const;
219 int CreateSession(const QuicServerId& server_id,
[email protected]17bf15c2014-03-14 10:08:04220 scoped_ptr<QuicServerInfo> quic_server_info,
[email protected]338e7982013-12-13 11:15:32221 const AddressList& address_list,
222 const BoundNetLog& net_log,
223 QuicClientSession** session);
[email protected]257f24f2014-04-01 09:15:37224 void ActivateSession(const QuicServerId& key,
[email protected]e13201d82012-12-12 05:00:32225 QuicClientSession* session);
226
[email protected]257f24f2014-04-01 09:15:37227 // Initializes the cached state associated with |server_id| in
[email protected]59c0bbd2014-03-22 04:08:12228 // |crypto_config_| with the information in |server_info|.
[email protected]60cf50e2014-04-28 23:23:18229 void InitializeCachedStateInCryptoConfig(
230 const QuicServerId& server_id,
231 const scoped_ptr<QuicServerInfo>& server_info);
[email protected]b694e48c2014-03-18 17:10:13232
[email protected]4d590c9c2014-05-02 05:14:33233 void ProcessGoingAwaySession(QuicClientSession* session,
[email protected]eb71ab62014-05-23 07:57:53234 const QuicServerId& server_id,
235 bool was_session_active);
[email protected]4d590c9c2014-05-02 05:14:33236
[email protected]11c05872013-08-20 02:04:12237 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32238 HostResolver* host_resolver_;
239 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45240 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]5db452202014-08-19 05:22:15241 TransportSecurityState* transport_security_state_;
[email protected]7832eeb2014-01-25 10:10:43242 QuicServerInfoFactory* quic_server_info_factory_;
[email protected]e8ff26842013-03-22 21:02:05243 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14244 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25245 scoped_ptr<QuicClock> clock_;
[email protected]256fe9b2013-11-27 01:58:02246 const size_t max_packet_length_;
[email protected]e13201d82012-12-12 05:00:32247
[email protected]2cfc6bb82013-10-27 03:40:44248 // The helper used for all connections.
249 scoped_ptr<QuicConnectionHelper> helper_;
250
[email protected]e13201d82012-12-12 05:00:32251 // Contains owning pointers to all sessions that currently exist.
[email protected]4d590c9c2014-05-02 05:14:33252 SessionIdMap all_sessions_;
[email protected]e13201d82012-12-12 05:00:32253 // Contains non-owning pointers to currently active session
254 // (not going away session, once they're implemented).
255 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38256 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32257 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38258 // Map from IP address to sessions which are connected to this address.
259 IPAliasMap ip_aliases_;
[email protected]e13201d82012-12-12 05:00:32260
[email protected]d8e2abf82014-03-06 10:30:10261 // Origins which have gone away recently.
262 AliasSet gone_away_aliases_;
263
[email protected]fd276a282014-06-11 04:26:14264 const QuicConfig config_;
[email protected]59c0bbd2014-03-22 04:08:12265 QuicCryptoClientConfig crypto_config_;
[email protected]b064310782013-05-30 21:12:17266
[email protected]e13201d82012-12-12 05:00:32267 JobMap active_jobs_;
268 JobRequestsMap job_requests_map_;
269 RequestMap active_requests_;
270
[email protected]1e960032013-12-20 19:00:20271 QuicVersionVector supported_versions_;
[email protected]e13201d82012-12-12 05:00:32272
[email protected]376d38a2014-01-22 03:47:35273 // Determine if we should consistently select a client UDP port. If false,
274 // then we will just let the OS select a random client port for each new
275 // connection.
276 bool enable_port_selection_;
277
jri2b966f22014-09-02 22:25:36278 // Set if we always require handshake confirmation. If true, this will
279 // introduce at least one RTT for the handshake before the client sends data.
280 bool always_require_handshake_confirmation_;
281
jri584002d12014-09-09 00:51:28282 // Set if we do not want connection pooling.
283 bool disable_connection_pooling_;
284
[email protected]337e1452013-12-16 23:57:50285 // Each profile will (probably) have a unique port_seed_ value. This value is
286 // used to help seed a pseudo-random number generator (PortSuggester) so that
287 // we consistently (within this profile) suggest the same ephemeral port when
288 // we re-connect to any given server/port. The differences between profiles
289 // (probablistically) prevent two profiles from colliding in their ephemeral
290 // port requests.
291 uint64 port_seed_;
[email protected]7034cf12013-12-13 22:47:07292
rtennetifc47e0e2014-09-26 02:54:05293 // Local address of socket that was created in CreateSession.
294 IPEndPoint local_address_;
295 bool check_persisted_supports_quic_;
296
[email protected]1e960032013-12-20 19:00:20297 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
298
[email protected]e13201d82012-12-12 05:00:32299 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
300};
301
302} // namespace net
303
304#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_