blob: 709cbb0d500b0514564a8a9c2ea887a09750c327 [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,
93 const QuicVersionVector& supported_versions);
[email protected]e13201d82012-12-12 05:00:3294 virtual ~QuicStreamFactory();
95
96 // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:5497 // owned by |request|. |is_https| specifies if the protocol is https or not.
98 // |cert_verifier| is used by ProofVerifier for verifying the certificate
99 // chain and signature. For http, this can be null. If a matching session
100 // already exists, this method will return OK. If no matching session exists,
101 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
102 // asynchronously.
[email protected]e13201d82012-12-12 05:00:32103 int Create(const HostPortProxyPair& host_port_proxy_pair,
[email protected]6d1b4ed2013-07-10 03:57:54104 bool is_https,
105 CertVerifier* cert_verifier,
[email protected]e13201d82012-12-12 05:00:32106 const BoundNetLog& net_log,
107 QuicStreamRequest* request);
108
109 // Returns a newly created QuicHttpStream owned by the caller, if a
110 // matching session already exists. Returns NULL otherwise.
111 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
112 const HostPortProxyPair& host_port_proxy_pair,
113 const BoundNetLog& net_log);
114
115 // Called by a session when it becomes idle.
116 void OnIdleSession(QuicClientSession* session);
117
[email protected]4d283b32013-10-17 12:57:27118 // Called by a session when it is going away and no more streams should be
119 // created on it.
120 void OnSessionGoingAway(QuicClientSession* session);
121
[email protected]e13201d82012-12-12 05:00:32122 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27123 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32124
125 // Cancels a pending request.
126 void CancelRequest(QuicStreamRequest* request);
127
[email protected]56dfb902013-01-03 23:17:55128 // Closes all current sessions.
129 void CloseAllSessions(int error);
130
[email protected]c5b061b2013-01-05 00:31:34131 base::Value* QuicStreamFactoryInfoToValue() const;
132
[email protected]f698a012013-05-06 20:18:59133 // NetworkChangeNotifier::IPAddressObserver methods:
134
135 // Until the servers support roaming, close all connections when the local
136 // IP address changes.
137 virtual void OnIPAddressChanged() OVERRIDE;
138
[email protected]d7d1e50b2013-11-25 22:08:09139 // CertDatabase::Observer methods:
140
141 // We close all sessions when certificate database is changed.
142 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
143 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
144
[email protected]11c05872013-08-20 02:04:12145 bool require_confirmation() const { return require_confirmation_; }
146
147 void set_require_confirmation(bool require_confirmation) {
148 require_confirmation_ = require_confirmation;
149 }
150
[email protected]2cfc6bb82013-10-27 03:40:44151 QuicConnectionHelper* helper() { return helper_.get(); }
152
[email protected]e13201d82012-12-12 05:00:32153 private:
154 class Job;
[email protected]c49ff182013-09-28 08:33:26155 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32156
157 typedef std::map<HostPortProxyPair, QuicClientSession*> SessionMap;
158 typedef std::set<HostPortProxyPair> AliasSet;
159 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
160 typedef std::set<QuicClientSession*> SessionSet;
[email protected]eed749f92013-12-23 18:57:38161 typedef std::map<IPEndPoint, SessionSet> IPAliasMap;
[email protected]ef95114d2013-04-17 17:57:01162 typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap;
[email protected]c49ff182013-09-28 08:33:26163 typedef std::map<HostPortPair, HostPortProxyPair> CanonicalHostMap;
[email protected]e13201d82012-12-12 05:00:32164 typedef std::map<HostPortProxyPair, Job*> JobMap;
165 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
166 typedef std::set<QuicStreamRequest*> RequestSet;
167 typedef std::map<Job*, RequestSet> JobRequestsMap;
168
[email protected]eed749f92013-12-23 18:57:38169 bool OnResolution(const HostPortProxyPair& host_port_proxy_pair,
170 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32171 void OnJobComplete(Job* job, int rv);
172 bool HasActiveSession(const HostPortProxyPair& host_port_proxy_pair);
173 bool HasActiveJob(const HostPortProxyPair& host_port_proxy_pair);
[email protected]338e7982013-12-13 11:15:32174 int CreateSession(const HostPortProxyPair& host_port_proxy_pair,
175 bool is_https,
176 CertVerifier* cert_verifier,
177 const AddressList& address_list,
178 const BoundNetLog& net_log,
179 QuicClientSession** session);
[email protected]e13201d82012-12-12 05:00:32180 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair,
181 QuicClientSession* session);
182
[email protected]ef95114d2013-04-17 17:57:01183 QuicCryptoClientConfig* GetOrCreateCryptoConfig(
184 const HostPortProxyPair& host_port_proxy_pair);
185
[email protected]c49ff182013-09-28 08:33:26186 // If |host_port_proxy_pair| suffix contains ".c.youtube.com" (in future we
187 // could support other suffixes), then populate |crypto_config| with a
188 // canonical server config data from |canonical_hostname_to_origin_map_| for
189 // that suffix.
190 void PopulateFromCanonicalConfig(
191 const HostPortProxyPair& host_port_proxy_pair,
192 QuicCryptoClientConfig* crypto_config);
193
[email protected]11c05872013-08-20 02:04:12194 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32195 HostResolver* host_resolver_;
196 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45197 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]e8ff26842013-03-22 21:02:05198 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14199 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25200 scoped_ptr<QuicClock> clock_;
[email protected]256fe9b2013-11-27 01:58:02201 const size_t max_packet_length_;
[email protected]e13201d82012-12-12 05:00:32202
[email protected]2cfc6bb82013-10-27 03:40:44203 // The helper used for all connections.
204 scoped_ptr<QuicConnectionHelper> helper_;
205
[email protected]e13201d82012-12-12 05:00:32206 // Contains owning pointers to all sessions that currently exist.
207 SessionSet all_sessions_;
208 // Contains non-owning pointers to currently active session
209 // (not going away session, once they're implemented).
210 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38211 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32212 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38213 // Map from IP address to sessions which are connected to this address.
214 IPAliasMap ip_aliases_;
[email protected]e13201d82012-12-12 05:00:32215
[email protected]ef95114d2013-04-17 17:57:01216 // Contains owning pointers to QuicCryptoClientConfig. QuicCryptoClientConfig
217 // contains configuration and cached state about servers.
218 // TODO(rtenneti): Persist all_crypto_configs_ to disk and decide when to
219 // clear the data in the map.
220 CryptoConfigMap all_crypto_configs_;
221
[email protected]c49ff182013-09-28 08:33:26222 // Contains a map of servers which could share the same server config. Map
223 // from a Canonical host/port (host is some postfix of host names) to an
224 // actual origin, which has a plausible set of initial certificates (or at
225 // least server public key).
226 CanonicalHostMap canonical_hostname_to_origin_map_;
227
[email protected]6e12d702013-11-13 00:17:17228 // Contains list of suffixes (for exmaple ".c.youtube.com",
229 // ".googlevideo.com") of cannoncial hostnames.
230 std::vector<std::string> cannoncial_suffixes_;
231
[email protected]b064310782013-05-30 21:12:17232 QuicConfig config_;
233
[email protected]e13201d82012-12-12 05:00:32234 JobMap active_jobs_;
235 JobRequestsMap job_requests_map_;
236 RequestMap active_requests_;
237
[email protected]1e960032013-12-20 19:00:20238 QuicVersionVector supported_versions_;
[email protected]e13201d82012-12-12 05:00:32239
[email protected]337e1452013-12-16 23:57:50240 // Each profile will (probably) have a unique port_seed_ value. This value is
241 // used to help seed a pseudo-random number generator (PortSuggester) so that
242 // we consistently (within this profile) suggest the same ephemeral port when
243 // we re-connect to any given server/port. The differences between profiles
244 // (probablistically) prevent two profiles from colliding in their ephemeral
245 // port requests.
246 uint64 port_seed_;
[email protected]7034cf12013-12-13 22:47:07247
[email protected]1e960032013-12-20 19:00:20248 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
249
[email protected]e13201d82012-12-12 05:00:32250 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
251};
252
253} // namespace net
254
255#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_