blob: f9e7a318b3bedf0a74bcf6117edac7cfb2617fcf [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
8#include <map>
[email protected]41d6b172013-01-29 16:10:579#include <string>
[email protected]6e12d702013-11-13 00:17:1710#include <vector>
[email protected]e13201d82012-12-12 05:00:3211
[email protected]e13201d82012-12-12 05:00:3212#include "base/memory/weak_ptr.h"
13#include "net/base/address_list.h"
14#include "net/base/completion_callback.h"
15#include "net/base/host_port_pair.h"
16#include "net/base/net_log.h"
[email protected]f698a012013-05-06 20:18:5917#include "net/base/network_change_notifier.h"
[email protected]d7d1e50b2013-11-25 22:08:0918#include "net/cert/cert_database.h"
[email protected]e13201d82012-12-12 05:00:3219#include "net/proxy/proxy_server.h"
[email protected]ef95114d2013-04-17 17:57:0120#include "net/quic/quic_config.h"
21#include "net/quic/quic_crypto_stream.h"
[email protected]e13201d82012-12-12 05:00:3222#include "net/quic/quic_http_stream.h"
23#include "net/quic/quic_protocol.h"
24
25namespace net {
26
[email protected]6d1b4ed2013-07-10 03:57:5427class CertVerifier;
[email protected]e13201d82012-12-12 05:00:3228class ClientSocketFactory;
[email protected]6d1b4ed2013-07-10 03:57:5429class HostResolver;
[email protected]77c6c162013-08-17 02:57:4530class HttpServerProperties;
[email protected]e13201d82012-12-12 05:00:3231class QuicClock;
32class QuicClientSession;
[email protected]2cfc6bb82013-10-27 03:40:4433class QuicConnectionHelper;
[email protected]e8ff26842013-03-22 21:02:0534class QuicCryptoClientStreamFactory;
[email protected]9558c5d32012-12-22 00:08:1435class QuicRandom;
[email protected]e13201d82012-12-12 05:00:3236class QuicStreamFactory;
37
[email protected]c49ff182013-09-28 08:33:2638namespace test {
39class QuicStreamFactoryPeer;
40} // namespace test
41
[email protected]e13201d82012-12-12 05:00:3242// Encapsulates a pending request for a QuicHttpStream.
43// If the request is still pending when it is destroyed, it will
44// cancel the request with the factory.
45class NET_EXPORT_PRIVATE QuicStreamRequest {
46 public:
47 explicit QuicStreamRequest(QuicStreamFactory* factory);
48 ~QuicStreamRequest();
49
[email protected]6d1b4ed2013-07-10 03:57:5450 // For http, |is_https| is false and |cert_verifier| can be null.
[email protected]e13201d82012-12-12 05:00:3251 int Request(const HostPortProxyPair& host_port_proxy_pair,
[email protected]6d1b4ed2013-07-10 03:57:5452 bool is_https,
53 CertVerifier* cert_verifier,
[email protected]e13201d82012-12-12 05:00:3254 const BoundNetLog& net_log,
55 const CompletionCallback& callback);
56
57 void OnRequestComplete(int rv);
58
59 scoped_ptr<QuicHttpStream> ReleaseStream();
60
61 void set_stream(scoped_ptr<QuicHttpStream> stream);
62
63 const BoundNetLog& net_log() const{
64 return net_log_;
65 }
66
67 private:
68 QuicStreamFactory* factory_;
69 HostPortProxyPair host_port_proxy_pair_;
[email protected]6d1b4ed2013-07-10 03:57:5470 bool is_https_;
71 CertVerifier* cert_verifier_;
[email protected]e13201d82012-12-12 05:00:3272 BoundNetLog net_log_;
73 CompletionCallback callback_;
74 scoped_ptr<QuicHttpStream> stream_;
75
76 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
77};
78
79// A factory for creating new QuicHttpStreams on top of a pool of
80// QuicClientSessions.
[email protected]f698a012013-05-06 20:18:5981class NET_EXPORT_PRIVATE QuicStreamFactory
[email protected]d7d1e50b2013-11-25 22:08:0982 : public NetworkChangeNotifier::IPAddressObserver,
83 public CertDatabase::Observer {
[email protected]e13201d82012-12-12 05:00:3284 public:
[email protected]e8ff26842013-03-22 21:02:0585 QuicStreamFactory(
86 HostResolver* host_resolver,
87 ClientSocketFactory* client_socket_factory,
[email protected]77c6c162013-08-17 02:57:4588 base::WeakPtr<HttpServerProperties> http_server_properties,
[email protected]e8ff26842013-03-22 21:02:0589 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
90 QuicRandom* random_generator,
[email protected]256fe9b2013-11-27 01:58:0291 QuicClock* clock,
[email protected]1e960032013-12-20 19:00:2092 size_t max_packet_length,
[email protected]376d38a2014-01-22 03:47:3593 const QuicVersionVector& supported_versions,
94 bool enable_port_selection);
[email protected]e13201d82012-12-12 05:00:3295 virtual ~QuicStreamFactory();
96
97 // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:5498 // owned by |request|. |is_https| specifies if the protocol is https or not.
99 // |cert_verifier| is used by ProofVerifier for verifying the certificate
100 // chain and signature. For http, this can be null. If a matching session
101 // already exists, this method will return OK. If no matching session exists,
102 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
103 // asynchronously.
[email protected]e13201d82012-12-12 05:00:32104 int Create(const HostPortProxyPair& host_port_proxy_pair,
[email protected]6d1b4ed2013-07-10 03:57:54105 bool is_https,
106 CertVerifier* cert_verifier,
[email protected]e13201d82012-12-12 05:00:32107 const BoundNetLog& net_log,
108 QuicStreamRequest* request);
109
110 // Returns a newly created QuicHttpStream owned by the caller, if a
111 // matching session already exists. Returns NULL otherwise.
112 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
113 const HostPortProxyPair& host_port_proxy_pair,
114 const BoundNetLog& net_log);
115
116 // Called by a session when it becomes idle.
117 void OnIdleSession(QuicClientSession* session);
118
[email protected]4d283b32013-10-17 12:57:27119 // Called by a session when it is going away and no more streams should be
120 // created on it.
121 void OnSessionGoingAway(QuicClientSession* session);
122
[email protected]e13201d82012-12-12 05:00:32123 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27124 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32125
126 // Cancels a pending request.
127 void CancelRequest(QuicStreamRequest* request);
128
[email protected]56dfb902013-01-03 23:17:55129 // Closes all current sessions.
130 void CloseAllSessions(int error);
131
[email protected]c5b061b2013-01-05 00:31:34132 base::Value* QuicStreamFactoryInfoToValue() const;
133
[email protected]f698a012013-05-06 20:18:59134 // NetworkChangeNotifier::IPAddressObserver methods:
135
136 // Until the servers support roaming, close all connections when the local
137 // IP address changes.
138 virtual void OnIPAddressChanged() OVERRIDE;
139
[email protected]d7d1e50b2013-11-25 22:08:09140 // CertDatabase::Observer methods:
141
142 // We close all sessions when certificate database is changed.
143 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
144 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
145
[email protected]11c05872013-08-20 02:04:12146 bool require_confirmation() const { return require_confirmation_; }
147
148 void set_require_confirmation(bool require_confirmation) {
149 require_confirmation_ = require_confirmation;
150 }
151
[email protected]2cfc6bb82013-10-27 03:40:44152 QuicConnectionHelper* helper() { return helper_.get(); }
153
[email protected]376d38a2014-01-22 03:47:35154 bool enable_port_selection() const { return enable_port_selection_; }
155
[email protected]e13201d82012-12-12 05:00:32156 private:
157 class Job;
[email protected]c49ff182013-09-28 08:33:26158 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32159
160 typedef std::map<HostPortProxyPair, QuicClientSession*> SessionMap;
161 typedef std::set<HostPortProxyPair> AliasSet;
162 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
163 typedef std::set<QuicClientSession*> SessionSet;
[email protected]eed749f92013-12-23 18:57:38164 typedef std::map<IPEndPoint, SessionSet> IPAliasMap;
[email protected]ef95114d2013-04-17 17:57:01165 typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap;
[email protected]c49ff182013-09-28 08:33:26166 typedef std::map<HostPortPair, HostPortProxyPair> CanonicalHostMap;
[email protected]e13201d82012-12-12 05:00:32167 typedef std::map<HostPortProxyPair, Job*> JobMap;
168 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
169 typedef std::set<QuicStreamRequest*> RequestSet;
170 typedef std::map<Job*, RequestSet> JobRequestsMap;
171
[email protected]eed749f92013-12-23 18:57:38172 bool OnResolution(const HostPortProxyPair& host_port_proxy_pair,
173 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32174 void OnJobComplete(Job* job, int rv);
175 bool HasActiveSession(const HostPortProxyPair& host_port_proxy_pair);
176 bool HasActiveJob(const HostPortProxyPair& host_port_proxy_pair);
[email protected]338e7982013-12-13 11:15:32177 int CreateSession(const HostPortProxyPair& host_port_proxy_pair,
178 bool is_https,
179 CertVerifier* cert_verifier,
180 const AddressList& address_list,
181 const BoundNetLog& net_log,
182 QuicClientSession** session);
[email protected]e13201d82012-12-12 05:00:32183 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair,
184 QuicClientSession* session);
185
[email protected]ef95114d2013-04-17 17:57:01186 QuicCryptoClientConfig* GetOrCreateCryptoConfig(
187 const HostPortProxyPair& host_port_proxy_pair);
188
[email protected]c49ff182013-09-28 08:33:26189 // If |host_port_proxy_pair| suffix contains ".c.youtube.com" (in future we
190 // could support other suffixes), then populate |crypto_config| with a
191 // canonical server config data from |canonical_hostname_to_origin_map_| for
192 // that suffix.
193 void PopulateFromCanonicalConfig(
194 const HostPortProxyPair& host_port_proxy_pair,
195 QuicCryptoClientConfig* crypto_config);
196
[email protected]11c05872013-08-20 02:04:12197 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32198 HostResolver* host_resolver_;
199 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45200 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]e8ff26842013-03-22 21:02:05201 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14202 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25203 scoped_ptr<QuicClock> clock_;
[email protected]256fe9b2013-11-27 01:58:02204 const size_t max_packet_length_;
[email protected]e13201d82012-12-12 05:00:32205
[email protected]2cfc6bb82013-10-27 03:40:44206 // The helper used for all connections.
207 scoped_ptr<QuicConnectionHelper> helper_;
208
[email protected]e13201d82012-12-12 05:00:32209 // Contains owning pointers to all sessions that currently exist.
210 SessionSet all_sessions_;
211 // Contains non-owning pointers to currently active session
212 // (not going away session, once they're implemented).
213 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38214 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32215 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38216 // Map from IP address to sessions which are connected to this address.
217 IPAliasMap ip_aliases_;
[email protected]e13201d82012-12-12 05:00:32218
[email protected]ef95114d2013-04-17 17:57:01219 // Contains owning pointers to QuicCryptoClientConfig. QuicCryptoClientConfig
220 // contains configuration and cached state about servers.
221 // TODO(rtenneti): Persist all_crypto_configs_ to disk and decide when to
222 // clear the data in the map.
223 CryptoConfigMap all_crypto_configs_;
224
[email protected]c49ff182013-09-28 08:33:26225 // Contains a map of servers which could share the same server config. Map
226 // from a Canonical host/port (host is some postfix of host names) to an
227 // actual origin, which has a plausible set of initial certificates (or at
228 // least server public key).
229 CanonicalHostMap canonical_hostname_to_origin_map_;
230
[email protected]6e12d702013-11-13 00:17:17231 // Contains list of suffixes (for exmaple ".c.youtube.com",
232 // ".googlevideo.com") of cannoncial hostnames.
233 std::vector<std::string> cannoncial_suffixes_;
234
[email protected]b064310782013-05-30 21:12:17235 QuicConfig config_;
236
[email protected]e13201d82012-12-12 05:00:32237 JobMap active_jobs_;
238 JobRequestsMap job_requests_map_;
239 RequestMap active_requests_;
240
[email protected]1e960032013-12-20 19:00:20241 QuicVersionVector supported_versions_;
[email protected]e13201d82012-12-12 05:00:32242
[email protected]376d38a2014-01-22 03:47:35243 // Determine if we should consistently select a client UDP port. If false,
244 // then we will just let the OS select a random client port for each new
245 // connection.
246 bool enable_port_selection_;
247
[email protected]337e1452013-12-16 23:57:50248 // Each profile will (probably) have a unique port_seed_ value. This value is
249 // used to help seed a pseudo-random number generator (PortSuggester) so that
250 // we consistently (within this profile) suggest the same ephemeral port when
251 // we re-connect to any given server/port. The differences between profiles
252 // (probablistically) prevent two profiles from colliding in their ephemeral
253 // port requests.
254 uint64 port_seed_;
[email protected]7034cf12013-12-13 22:47:07255
[email protected]1e960032013-12-20 19:00:20256 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
257
[email protected]e13201d82012-12-12 05:00:32258 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
259};
260
261} // namespace net
262
263#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_