1 // 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.
5 #ifndef NET_QUIC_QUIC_STREAM_FACTORY_H_
6 #define NET_QUIC_QUIC_STREAM_FACTORY_H_
12 #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"
17 #include "net/base/network_change_notifier.h"
18 #include "net/cert/cert_database.h"
19 #include "net/proxy/proxy_server.h"
20 #include "net/quic/quic_config.h"
21 #include "net/quic/quic_crypto_stream.h"
22 #include "net/quic/quic_http_stream.h"
23 #include "net/quic/quic_protocol.h"
28 class ClientSocketFactory;
30 class HttpServerProperties;
32 class QuicClientSession;
33 class QuicConnectionHelper;
34 class QuicCryptoClientStreamFactory;
36 class QuicServerInfoFactory;
37 class QuicStreamFactory;
40 class QuicStreamFactoryPeer;
43 // Encapsulates a pending request for a QuicHttpStream.
44 // If the request is still pending when it is destroyed, it will
45 // cancel the request with the factory.
46 class NET_EXPORT_PRIVATE QuicStreamRequest {
48 explicit QuicStreamRequest(QuicStreamFactory* factory);
51 // For http, |is_https| is false and |cert_verifier| can be null.
52 int Request(const HostPortProxyPair& host_port_proxy_pair,
54 base::StringPiece method,
55 CertVerifier* cert_verifier,
56 const BoundNetLog& net_log,
57 const CompletionCallback& callback);
59 void OnRequestComplete(int rv);
61 scoped_ptr<QuicHttpStream> ReleaseStream();
63 void set_stream(scoped_ptr<QuicHttpStream> stream);
65 const BoundNetLog& net_log() const{
70 QuicStreamFactory* factory_;
71 HostPortProxyPair host_port_proxy_pair_;
73 CertVerifier* cert_verifier_;
75 CompletionCallback callback_;
76 scoped_ptr<QuicHttpStream> stream_;
78 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
81 // A factory for creating new QuicHttpStreams on top of a pool of
82 // QuicClientSessions.
83 class NET_EXPORT_PRIVATE QuicStreamFactory
84 : public NetworkChangeNotifier::IPAddressObserver,
85 public CertDatabase::Observer {
88 HostResolver* host_resolver,
89 ClientSocketFactory* client_socket_factory,
90 base::WeakPtr<HttpServerProperties> http_server_properties,
91 QuicServerInfoFactory* quic_server_info_factory,
92 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
93 QuicRandom* random_generator,
95 size_t max_packet_length,
96 const QuicVersionVector& supported_versions,
97 bool enable_port_selection);
98 virtual ~QuicStreamFactory();
100 // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be
101 // owned by |request|. |is_https| specifies if the protocol is https or not.
102 // |cert_verifier| is used by ProofVerifier for verifying the certificate
103 // chain and signature. For http, this can be null. If a matching session
104 // already exists, this method will return OK. If no matching session exists,
105 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
107 int Create(const HostPortProxyPair& host_port_proxy_pair,
109 base::StringPiece method,
110 CertVerifier* cert_verifier,
111 const BoundNetLog& net_log,
112 QuicStreamRequest* request);
114 // Returns a newly created QuicHttpStream owned by the caller, if a
115 // matching session already exists. Returns NULL otherwise.
116 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
117 const HostPortProxyPair& host_port_proxy_pair,
118 const BoundNetLog& net_log);
120 // Called by a session when it becomes idle.
121 void OnIdleSession(QuicClientSession* session);
123 // Called by a session when it is going away and no more streams should be
125 void OnSessionGoingAway(QuicClientSession* session);
127 // Called by a session after it shuts down.
128 void OnSessionClosed(QuicClientSession* session);
130 // Cancels a pending request.
131 void CancelRequest(QuicStreamRequest* request);
133 // Closes all current sessions.
134 void CloseAllSessions(int error);
136 base::Value* QuicStreamFactoryInfoToValue() const;
138 // NetworkChangeNotifier::IPAddressObserver methods:
140 // Until the servers support roaming, close all connections when the local
141 // IP address changes.
142 virtual void OnIPAddressChanged() OVERRIDE;
144 // CertDatabase::Observer methods:
146 // We close all sessions when certificate database is changed.
147 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
148 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
150 bool require_confirmation() const { return require_confirmation_; }
152 void set_require_confirmation(bool require_confirmation) {
153 require_confirmation_ = require_confirmation;
156 QuicConnectionHelper* helper() { return helper_.get(); }
158 bool enable_port_selection() const { return enable_port_selection_; }
162 friend class test::QuicStreamFactoryPeer;
164 typedef std::map<HostPortProxyPair, QuicClientSession*> SessionMap;
165 typedef std::set<HostPortProxyPair> AliasSet;
166 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
167 typedef std::set<QuicClientSession*> SessionSet;
168 typedef std::map<IPEndPoint, SessionSet> IPAliasMap;
169 typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap;
170 typedef std::map<HostPortPair, HostPortProxyPair> CanonicalHostMap;
171 typedef std::map<HostPortProxyPair, Job*> JobMap;
172 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
173 typedef std::set<QuicStreamRequest*> RequestSet;
174 typedef std::map<Job*, RequestSet> JobRequestsMap;
176 bool OnResolution(const HostPortProxyPair& host_port_proxy_pair,
177 const AddressList& address_list);
178 void OnJobComplete(Job* job, int rv);
179 bool HasActiveSession(const HostPortProxyPair& host_port_proxy_pair);
180 bool HasActiveJob(const HostPortProxyPair& host_port_proxy_pair);
181 int CreateSession(const HostPortProxyPair& host_port_proxy_pair,
183 CertVerifier* cert_verifier,
184 const AddressList& address_list,
185 const BoundNetLog& net_log,
186 QuicClientSession** session);
187 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair,
188 QuicClientSession* session);
190 QuicCryptoClientConfig* GetOrCreateCryptoConfig(
191 const HostPortProxyPair& host_port_proxy_pair);
193 // If |host_port_proxy_pair| suffix contains ".c.youtube.com" (in future we
194 // could support other suffixes), then populate |crypto_config| with a
195 // canonical server config data from |canonical_hostname_to_origin_map_| for
197 void PopulateFromCanonicalConfig(
198 const HostPortProxyPair& host_port_proxy_pair,
199 QuicCryptoClientConfig* crypto_config);
201 bool require_confirmation_;
202 HostResolver* host_resolver_;
203 ClientSocketFactory* client_socket_factory_;
204 base::WeakPtr<HttpServerProperties> http_server_properties_;
205 QuicServerInfoFactory* quic_server_info_factory_;
206 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
207 QuicRandom* random_generator_;
208 scoped_ptr<QuicClock> clock_;
209 const size_t max_packet_length_;
211 // The helper used for all connections.
212 scoped_ptr<QuicConnectionHelper> helper_;
214 // Contains owning pointers to all sessions that currently exist.
215 SessionSet all_sessions_;
216 // Contains non-owning pointers to currently active session
217 // (not going away session, once they're implemented).
218 SessionMap active_sessions_;
219 // Map from session to set of aliases that this session is known by.
220 SessionAliasMap session_aliases_;
221 // Map from IP address to sessions which are connected to this address.
222 IPAliasMap ip_aliases_;
224 // Contains owning pointers to QuicCryptoClientConfig. QuicCryptoClientConfig
225 // contains configuration and cached state about servers.
226 // TODO(rtenneti): Persist all_crypto_configs_ to disk and decide when to
227 // clear the data in the map.
228 CryptoConfigMap all_crypto_configs_;
230 // Contains a map of servers which could share the same server config. Map
231 // from a Canonical host/port (host is some postfix of host names) to an
232 // actual origin, which has a plausible set of initial certificates (or at
233 // least server public key).
234 CanonicalHostMap canonical_hostname_to_origin_map_;
236 // Contains list of suffixes (for exmaple ".c.youtube.com",
237 // ".googlevideo.com") of canoncial hostnames.
238 std::vector<std::string> canoncial_suffixes_;
243 JobRequestsMap job_requests_map_;
244 RequestMap active_requests_;
246 QuicVersionVector supported_versions_;
248 // Determine if we should consistently select a client UDP port. If false,
249 // then we will just let the OS select a random client port for each new
251 bool enable_port_selection_;
253 // Each profile will (probably) have a unique port_seed_ value. This value is
254 // used to help seed a pseudo-random number generator (PortSuggester) so that
255 // we consistently (within this profile) suggest the same ephemeral port when
256 // we re-connect to any given server/port. The differences between profiles
257 // (probablistically) prevent two profiles from colliding in their ephemeral
261 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
263 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
268 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_