blob: eb82659bbf82433f44f3a5855427a8181f7a15f0 [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]0c4017ca2014-06-06 03:30:4598 const std::string& user_agent_id,
[email protected]376d38a2014-01-22 03:47:3599 const QuicVersionVector& supported_versions,
[email protected]c80f7c92014-02-27 13:12:02100 bool enable_port_selection,
[email protected]ebb3ad52014-04-30 20:01:39101 bool enable_pacing,
102 bool enable_time_based_loss_detection);
[email protected]e13201d82012-12-12 05:00:32103 virtual ~QuicStreamFactory();
104
[email protected]bf4ea2f2014-03-10 22:57:53105 // Creates a new QuicHttpStream to |host_port_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:54106 // owned by |request|. |is_https| specifies if the protocol is https or not.
107 // |cert_verifier| is used by ProofVerifier for verifying the certificate
108 // chain and signature. For http, this can be null. If a matching session
109 // already exists, this method will return OK. If no matching session exists,
110 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
111 // asynchronously.
[email protected]bf4ea2f2014-03-10 22:57:53112 int Create(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:54113 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:28114 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:59115 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:32116 const BoundNetLog& net_log,
117 QuicStreamRequest* request);
118
[email protected]e13201d82012-12-12 05:00:32119 // Called by a session when it becomes idle.
120 void OnIdleSession(QuicClientSession* session);
121
[email protected]4d283b32013-10-17 12:57:27122 // Called by a session when it is going away and no more streams should be
123 // created on it.
124 void OnSessionGoingAway(QuicClientSession* session);
125
[email protected]e13201d82012-12-12 05:00:32126 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27127 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32128
[email protected]65768442014-06-06 23:37:03129 // Called by a session whose connection has timed out.
130 void OnSessionConnectTimeout(QuicClientSession* session);
131
[email protected]e13201d82012-12-12 05:00:32132 // Cancels a pending request.
133 void CancelRequest(QuicStreamRequest* request);
134
[email protected]56dfb902013-01-03 23:17:55135 // Closes all current sessions.
136 void CloseAllSessions(int error);
137
[email protected]c5b061b2013-01-05 00:31:34138 base::Value* QuicStreamFactoryInfoToValue() const;
139
[email protected]f7e21a432014-04-21 22:17:57140 // Delete all cached state objects in |crypto_config_|.
[email protected]60cf50e2014-04-28 23:23:18141 void ClearCachedStatesInCryptoConfig();
[email protected]f7e21a432014-04-21 22:17:57142
[email protected]f698a012013-05-06 20:18:59143 // NetworkChangeNotifier::IPAddressObserver methods:
144
145 // Until the servers support roaming, close all connections when the local
146 // IP address changes.
147 virtual void OnIPAddressChanged() OVERRIDE;
148
[email protected]d7d1e50b2013-11-25 22:08:09149 // CertDatabase::Observer methods:
150
151 // We close all sessions when certificate database is changed.
152 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
153 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
154
[email protected]11c05872013-08-20 02:04:12155 bool require_confirmation() const { return require_confirmation_; }
156
157 void set_require_confirmation(bool require_confirmation) {
158 require_confirmation_ = require_confirmation;
159 }
160
[email protected]2cfc6bb82013-10-27 03:40:44161 QuicConnectionHelper* helper() { return helper_.get(); }
162
[email protected]376d38a2014-01-22 03:47:35163 bool enable_port_selection() const { return enable_port_selection_; }
164
[email protected]a4205202014-06-02 16:03:08165 bool has_quic_server_info_factory() {
166 return quic_server_info_factory_ != NULL;
167 }
168
[email protected]e8cf7555b2014-02-28 23:52:53169 void set_quic_server_info_factory(
170 QuicServerInfoFactory* quic_server_info_factory) {
171 DCHECK(!quic_server_info_factory_);
172 quic_server_info_factory_ = quic_server_info_factory;
173 }
174
[email protected]e13201d82012-12-12 05:00:32175 private:
176 class Job;
[email protected]c49ff182013-09-28 08:33:26177 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32178
[email protected]9dd3ff0f2014-03-26 09:51:28179 // The key used to find session by ip. Includes
[email protected]df157d9d2014-03-10 07:27:27180 // the ip address, port, and scheme.
181 struct NET_EXPORT_PRIVATE IpAliasKey {
182 IpAliasKey();
183 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
184 ~IpAliasKey();
185
186 IPEndPoint ip_endpoint;
187 bool is_https;
188
189 // Needed to be an element of std::set.
190 bool operator<(const IpAliasKey &other) const;
191 bool operator==(const IpAliasKey &other) const;
192 };
193
[email protected]257f24f2014-04-01 09:15:37194 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
[email protected]4d590c9c2014-05-02 05:14:33195 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
[email protected]257f24f2014-04-01 09:15:37196 typedef std::set<QuicServerId> AliasSet;
[email protected]e13201d82012-12-12 05:00:32197 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
198 typedef std::set<QuicClientSession*> SessionSet;
[email protected]df157d9d2014-03-10 07:27:27199 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
[email protected]257f24f2014-04-01 09:15:37200 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
201 typedef std::map<QuicServerId, Job*> JobMap;
[email protected]e13201d82012-12-12 05:00:32202 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
203 typedef std::set<QuicStreamRequest*> RequestSet;
204 typedef std::map<Job*, RequestSet> JobRequestsMap;
205
[email protected]df157d9d2014-03-10 07:27:27206 // Returns a newly created QuicHttpStream owned by the caller, if a
207 // matching session already exists. Returns NULL otherwise.
[email protected]257f24f2014-04-01 09:15:37208 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
[email protected]df157d9d2014-03-10 07:27:27209 const BoundNetLog& net_log);
210
[email protected]257f24f2014-04-01 09:15:37211 bool OnResolution(const QuicServerId& server_id,
[email protected]eed749f92013-12-23 18:57:38212 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32213 void OnJobComplete(Job* job, int rv);
[email protected]257f24f2014-04-01 09:15:37214 bool HasActiveSession(const QuicServerId& server_id) const;
215 bool HasActiveJob(const QuicServerId& server_id) const;
216 int CreateSession(const QuicServerId& server_id,
[email protected]17bf15c2014-03-14 10:08:04217 scoped_ptr<QuicServerInfo> quic_server_info,
[email protected]338e7982013-12-13 11:15:32218 const AddressList& address_list,
219 const BoundNetLog& net_log,
220 QuicClientSession** session);
[email protected]257f24f2014-04-01 09:15:37221 void ActivateSession(const QuicServerId& key,
[email protected]e13201d82012-12-12 05:00:32222 QuicClientSession* session);
223
[email protected]257f24f2014-04-01 09:15:37224 // Initializes the cached state associated with |server_id| in
[email protected]59c0bbd2014-03-22 04:08:12225 // |crypto_config_| with the information in |server_info|.
[email protected]60cf50e2014-04-28 23:23:18226 void InitializeCachedStateInCryptoConfig(
227 const QuicServerId& server_id,
228 const scoped_ptr<QuicServerInfo>& server_info);
[email protected]b694e48c2014-03-18 17:10:13229
[email protected]4d590c9c2014-05-02 05:14:33230 void ProcessGoingAwaySession(QuicClientSession* session,
[email protected]eb71ab62014-05-23 07:57:53231 const QuicServerId& server_id,
232 bool was_session_active);
[email protected]4d590c9c2014-05-02 05:14:33233
[email protected]11c05872013-08-20 02:04:12234 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32235 HostResolver* host_resolver_;
236 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45237 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]59c0bbd2014-03-22 04:08:12238 CertVerifier* cert_verifier_;
[email protected]7832eeb2014-01-25 10:10:43239 QuicServerInfoFactory* quic_server_info_factory_;
[email protected]e8ff26842013-03-22 21:02:05240 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14241 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25242 scoped_ptr<QuicClock> clock_;
[email protected]256fe9b2013-11-27 01:58:02243 const size_t max_packet_length_;
[email protected]e13201d82012-12-12 05:00:32244
[email protected]2cfc6bb82013-10-27 03:40:44245 // The helper used for all connections.
246 scoped_ptr<QuicConnectionHelper> helper_;
247
[email protected]e13201d82012-12-12 05:00:32248 // Contains owning pointers to all sessions that currently exist.
[email protected]4d590c9c2014-05-02 05:14:33249 SessionIdMap all_sessions_;
[email protected]e13201d82012-12-12 05:00:32250 // Contains non-owning pointers to currently active session
251 // (not going away session, once they're implemented).
252 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38253 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32254 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38255 // Map from IP address to sessions which are connected to this address.
256 IPAliasMap ip_aliases_;
[email protected]e13201d82012-12-12 05:00:32257
[email protected]d8e2abf82014-03-06 10:30:10258 // Origins which have gone away recently.
259 AliasSet gone_away_aliases_;
260
[email protected]fd276a282014-06-11 04:26:14261 const QuicConfig config_;
[email protected]59c0bbd2014-03-22 04:08:12262 QuicCryptoClientConfig crypto_config_;
[email protected]b064310782013-05-30 21:12:17263
[email protected]e13201d82012-12-12 05:00:32264 JobMap active_jobs_;
265 JobRequestsMap job_requests_map_;
266 RequestMap active_requests_;
267
[email protected]1e960032013-12-20 19:00:20268 QuicVersionVector supported_versions_;
[email protected]e13201d82012-12-12 05:00:32269
[email protected]376d38a2014-01-22 03:47:35270 // Determine if we should consistently select a client UDP port. If false,
271 // then we will just let the OS select a random client port for each new
272 // connection.
273 bool enable_port_selection_;
274
[email protected]337e1452013-12-16 23:57:50275 // Each profile will (probably) have a unique port_seed_ value. This value is
276 // used to help seed a pseudo-random number generator (PortSuggester) so that
277 // we consistently (within this profile) suggest the same ephemeral port when
278 // we re-connect to any given server/port. The differences between profiles
279 // (probablistically) prevent two profiles from colliding in their ephemeral
280 // port requests.
281 uint64 port_seed_;
[email protected]7034cf12013-12-13 22:47:07282
[email protected]1e960032013-12-20 19:00:20283 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
284
[email protected]e13201d82012-12-12 05:00:32285 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
286};
287
288} // namespace net
289
290#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_