blob: 3df8da0b6ec244ffc788dd1cf48860e70288c9ee [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;
42
[email protected]c49ff182013-09-28 08:33:2643namespace test {
44class QuicStreamFactoryPeer;
45} // namespace test
46
[email protected]e13201d82012-12-12 05:00:3247// Encapsulates a pending request for a QuicHttpStream.
48// If the request is still pending when it is destroyed, it will
49// cancel the request with the factory.
50class NET_EXPORT_PRIVATE QuicStreamRequest {
51 public:
52 explicit QuicStreamRequest(QuicStreamFactory* factory);
53 ~QuicStreamRequest();
54
[email protected]0cceb922014-07-01 02:00:5655 // For http, |is_https| is false.
[email protected]bf4ea2f2014-03-10 22:57:5356 int Request(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:5457 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:2858 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:5959 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:3260 const BoundNetLog& net_log,
61 const CompletionCallback& callback);
62
63 void OnRequestComplete(int rv);
64
65 scoped_ptr<QuicHttpStream> ReleaseStream();
66
67 void set_stream(scoped_ptr<QuicHttpStream> stream);
68
69 const BoundNetLog& net_log() const{
70 return net_log_;
71 }
72
73 private:
74 QuicStreamFactory* factory_;
[email protected]bf4ea2f2014-03-10 22:57:5375 HostPortPair host_port_pair_;
[email protected]6d1b4ed2013-07-10 03:57:5476 bool is_https_;
[email protected]e13201d82012-12-12 05:00:3277 BoundNetLog net_log_;
78 CompletionCallback callback_;
79 scoped_ptr<QuicHttpStream> stream_;
80
81 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
82};
83
84// A factory for creating new QuicHttpStreams on top of a pool of
85// QuicClientSessions.
[email protected]f698a012013-05-06 20:18:5986class NET_EXPORT_PRIVATE QuicStreamFactory
[email protected]d7d1e50b2013-11-25 22:08:0987 : public NetworkChangeNotifier::IPAddressObserver,
88 public CertDatabase::Observer {
[email protected]e13201d82012-12-12 05:00:3289 public:
[email protected]e8ff26842013-03-22 21:02:0590 QuicStreamFactory(
91 HostResolver* host_resolver,
92 ClientSocketFactory* client_socket_factory,
[email protected]77c6c162013-08-17 02:57:4593 base::WeakPtr<HttpServerProperties> http_server_properties,
[email protected]59c0bbd2014-03-22 04:08:1294 CertVerifier* cert_verifier,
[email protected]6b8a3c742014-07-25 00:25:3595 ChannelIDService* channel_id_service,
[email protected]e8ff26842013-03-22 21:02:0596 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
97 QuicRandom* random_generator,
[email protected]256fe9b2013-11-27 01:58:0298 QuicClock* clock,
[email protected]1e960032013-12-20 19:00:2099 size_t max_packet_length,
[email protected]0c4017ca2014-06-06 03:30:45100 const std::string& user_agent_id,
[email protected]376d38a2014-01-22 03:47:35101 const QuicVersionVector& supported_versions,
[email protected]c80f7c92014-02-27 13:12:02102 bool enable_port_selection,
[email protected]ebb3ad52014-04-30 20:01:39103 bool enable_pacing,
[email protected]488a0e252014-06-25 04:37:44104 bool enable_time_based_loss_detection,
[email protected]4b4efab32014-07-01 02:36:16105 const QuicTagVector& connection_options);
[email protected]e13201d82012-12-12 05:00:32106 virtual ~QuicStreamFactory();
107
[email protected]bf4ea2f2014-03-10 22:57:53108 // Creates a new QuicHttpStream to |host_port_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:54109 // owned by |request|. |is_https| specifies if the protocol is https or not.
[email protected]0cceb922014-07-01 02:00:56110 // If a matching session already exists, this method will return OK. If no
111 // matching session exists, this will return ERR_IO_PENDING and will invoke
112 // OnRequestComplete asynchronously.
[email protected]bf4ea2f2014-03-10 22:57:53113 int Create(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:54114 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:28115 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:59116 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:32117 const BoundNetLog& net_log,
118 QuicStreamRequest* request);
119
[email protected]e13201d82012-12-12 05:00:32120 // Called by a session when it becomes idle.
121 void OnIdleSession(QuicClientSession* session);
122
[email protected]4d283b32013-10-17 12:57:27123 // Called by a session when it is going away and no more streams should be
124 // created on it.
125 void OnSessionGoingAway(QuicClientSession* session);
126
[email protected]e13201d82012-12-12 05:00:32127 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27128 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32129
[email protected]65768442014-06-06 23:37:03130 // Called by a session whose connection has timed out.
131 void OnSessionConnectTimeout(QuicClientSession* session);
132
[email protected]e13201d82012-12-12 05:00:32133 // Cancels a pending request.
134 void CancelRequest(QuicStreamRequest* request);
135
[email protected]56dfb902013-01-03 23:17:55136 // Closes all current sessions.
137 void CloseAllSessions(int error);
138
[email protected]c5b061b2013-01-05 00:31:34139 base::Value* QuicStreamFactoryInfoToValue() const;
140
[email protected]f7e21a432014-04-21 22:17:57141 // Delete all cached state objects in |crypto_config_|.
[email protected]60cf50e2014-04-28 23:23:18142 void ClearCachedStatesInCryptoConfig();
[email protected]f7e21a432014-04-21 22:17:57143
[email protected]f698a012013-05-06 20:18:59144 // NetworkChangeNotifier::IPAddressObserver methods:
145
146 // Until the servers support roaming, close all connections when the local
147 // IP address changes.
148 virtual void OnIPAddressChanged() OVERRIDE;
149
[email protected]d7d1e50b2013-11-25 22:08:09150 // CertDatabase::Observer methods:
151
152 // We close all sessions when certificate database is changed.
153 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
154 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
155
[email protected]11c05872013-08-20 02:04:12156 bool require_confirmation() const { return require_confirmation_; }
157
158 void set_require_confirmation(bool require_confirmation) {
159 require_confirmation_ = require_confirmation;
160 }
161
[email protected]2cfc6bb82013-10-27 03:40:44162 QuicConnectionHelper* helper() { return helper_.get(); }
163
[email protected]376d38a2014-01-22 03:47:35164 bool enable_port_selection() const { return enable_port_selection_; }
165
[email protected]a4205202014-06-02 16:03:08166 bool has_quic_server_info_factory() {
167 return quic_server_info_factory_ != NULL;
168 }
169
[email protected]e8cf7555b2014-02-28 23:52:53170 void set_quic_server_info_factory(
171 QuicServerInfoFactory* quic_server_info_factory) {
172 DCHECK(!quic_server_info_factory_);
173 quic_server_info_factory_ = quic_server_info_factory;
174 }
175
[email protected]e13201d82012-12-12 05:00:32176 private:
177 class Job;
[email protected]c49ff182013-09-28 08:33:26178 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32179
[email protected]9dd3ff0f2014-03-26 09:51:28180 // The key used to find session by ip. Includes
[email protected]df157d9d2014-03-10 07:27:27181 // the ip address, port, and scheme.
182 struct NET_EXPORT_PRIVATE IpAliasKey {
183 IpAliasKey();
184 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
185 ~IpAliasKey();
186
187 IPEndPoint ip_endpoint;
188 bool is_https;
189
190 // Needed to be an element of std::set.
191 bool operator<(const IpAliasKey &other) const;
192 bool operator==(const IpAliasKey &other) const;
193 };
194
[email protected]257f24f2014-04-01 09:15:37195 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
[email protected]4d590c9c2014-05-02 05:14:33196 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
[email protected]257f24f2014-04-01 09:15:37197 typedef std::set<QuicServerId> AliasSet;
[email protected]e13201d82012-12-12 05:00:32198 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
199 typedef std::set<QuicClientSession*> SessionSet;
[email protected]df157d9d2014-03-10 07:27:27200 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
[email protected]257f24f2014-04-01 09:15:37201 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
202 typedef std::map<QuicServerId, Job*> JobMap;
[email protected]e13201d82012-12-12 05:00:32203 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
204 typedef std::set<QuicStreamRequest*> RequestSet;
205 typedef std::map<Job*, RequestSet> JobRequestsMap;
206
[email protected]df157d9d2014-03-10 07:27:27207 // Returns a newly created QuicHttpStream owned by the caller, if a
208 // matching session already exists. Returns NULL otherwise.
[email protected]257f24f2014-04-01 09:15:37209 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
[email protected]df157d9d2014-03-10 07:27:27210 const BoundNetLog& net_log);
211
[email protected]257f24f2014-04-01 09:15:37212 bool OnResolution(const QuicServerId& server_id,
[email protected]eed749f92013-12-23 18:57:38213 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32214 void OnJobComplete(Job* job, int rv);
[email protected]257f24f2014-04-01 09:15:37215 bool HasActiveSession(const QuicServerId& server_id) const;
216 bool HasActiveJob(const QuicServerId& server_id) const;
217 int CreateSession(const QuicServerId& server_id,
[email protected]17bf15c2014-03-14 10:08:04218 scoped_ptr<QuicServerInfo> quic_server_info,
[email protected]338e7982013-12-13 11:15:32219 const AddressList& address_list,
220 const BoundNetLog& net_log,
221 QuicClientSession** session);
[email protected]257f24f2014-04-01 09:15:37222 void ActivateSession(const QuicServerId& key,
[email protected]e13201d82012-12-12 05:00:32223 QuicClientSession* session);
224
[email protected]257f24f2014-04-01 09:15:37225 // Initializes the cached state associated with |server_id| in
[email protected]59c0bbd2014-03-22 04:08:12226 // |crypto_config_| with the information in |server_info|.
[email protected]60cf50e2014-04-28 23:23:18227 void InitializeCachedStateInCryptoConfig(
228 const QuicServerId& server_id,
229 const scoped_ptr<QuicServerInfo>& server_info);
[email protected]b694e48c2014-03-18 17:10:13230
[email protected]4d590c9c2014-05-02 05:14:33231 void ProcessGoingAwaySession(QuicClientSession* session,
[email protected]eb71ab62014-05-23 07:57:53232 const QuicServerId& server_id,
233 bool was_session_active);
[email protected]4d590c9c2014-05-02 05:14:33234
[email protected]11c05872013-08-20 02:04:12235 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32236 HostResolver* host_resolver_;
237 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45238 base::WeakPtr<HttpServerProperties> http_server_properties_;
[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_