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_
13 #include "base/logging.h"
14 #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"
19 #include "net/base/network_change_notifier.h"
20 #include "net/cert/cert_database.h"
21 #include "net/proxy/proxy_server.h"
22 #include "net/quic/quic_config.h"
23 #include "net/quic/quic_crypto_stream.h"
24 #include "net/quic/quic_http_stream.h"
25 #include "net/quic/quic_protocol.h"
30 class ClientSocketFactory;
32 class HttpServerProperties;
34 class QuicClientSession;
35 class QuicConnectionHelper;
36 class QuicCryptoClientStreamFactory;
38 class QuicServerInfoFactory;
40 class QuicStreamFactory;
43 class QuicStreamFactoryPeer;
46 // 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.
49 class NET_EXPORT_PRIVATE QuicStreamRequest {
51 explicit QuicStreamRequest(QuicStreamFactory* factory);
54 // For http, |is_https| is false and |cert_verifier| can be null.
55 int Request(const HostPortPair& host_port_pair,
57 PrivacyMode privacy_mode,
58 base::StringPiece method,
59 const BoundNetLog& net_log,
60 const CompletionCallback& callback);
62 void OnRequestComplete(int rv);
64 scoped_ptr<QuicHttpStream> ReleaseStream();
66 void set_stream(scoped_ptr<QuicHttpStream> stream);
68 const BoundNetLog& net_log() const{
73 QuicStreamFactory* factory_;
74 HostPortPair host_port_pair_;
77 CompletionCallback callback_;
78 scoped_ptr<QuicHttpStream> stream_;
80 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
83 // A factory for creating new QuicHttpStreams on top of a pool of
84 // QuicClientSessions.
85 class NET_EXPORT_PRIVATE QuicStreamFactory
86 : public NetworkChangeNotifier::IPAddressObserver,
87 public CertDatabase::Observer {
90 HostResolver* host_resolver,
91 ClientSocketFactory* client_socket_factory,
92 base::WeakPtr<HttpServerProperties> http_server_properties,
93 CertVerifier* cert_verifier,
94 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
95 QuicRandom* random_generator,
97 size_t max_packet_length,
98 const QuicVersionVector& supported_versions,
99 bool enable_port_selection,
101 bool enable_time_based_loss_detection);
102 virtual ~QuicStreamFactory();
104 // Creates a new QuicHttpStream to |host_port_pair| which will be
105 // owned by |request|. |is_https| specifies if the protocol is https or not.
106 // |cert_verifier| is used by ProofVerifier for verifying the certificate
107 // chain and signature. For http, this can be null. If a matching session
108 // already exists, this method will return OK. If no matching session exists,
109 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
111 int Create(const HostPortPair& host_port_pair,
113 PrivacyMode privacy_mode,
114 base::StringPiece method,
115 const BoundNetLog& net_log,
116 QuicStreamRequest* request);
118 // Called by a session when it becomes idle.
119 void OnIdleSession(QuicClientSession* session);
121 // Called by a session when it is going away and no more streams should be
123 void OnSessionGoingAway(QuicClientSession* session);
125 // Called by a session after it shuts down.
126 void OnSessionClosed(QuicClientSession* session);
128 // Cancels a pending request.
129 void CancelRequest(QuicStreamRequest* request);
131 // Closes all current sessions.
132 void CloseAllSessions(int error);
134 base::Value* QuicStreamFactoryInfoToValue() const;
136 // Delete all cached state objects in |crypto_config_|.
137 void ClearCachedStatesInCryptoConfig();
139 // NetworkChangeNotifier::IPAddressObserver methods:
141 // Until the servers support roaming, close all connections when the local
142 // IP address changes.
143 virtual void OnIPAddressChanged() OVERRIDE;
145 // CertDatabase::Observer methods:
147 // We close all sessions when certificate database is changed.
148 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
149 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
151 bool require_confirmation() const { return require_confirmation_; }
153 void set_require_confirmation(bool require_confirmation) {
154 require_confirmation_ = require_confirmation;
157 QuicConnectionHelper* helper() { return helper_.get(); }
159 bool enable_port_selection() const { return enable_port_selection_; }
161 void set_quic_server_info_factory(
162 QuicServerInfoFactory* quic_server_info_factory) {
163 DCHECK(!quic_server_info_factory_);
164 quic_server_info_factory_ = quic_server_info_factory;
167 bool enable_pacing() const { return enable_pacing_; }
171 friend class test::QuicStreamFactoryPeer;
173 // The key used to find session by ip. Includes
174 // the ip address, port, and scheme.
175 struct NET_EXPORT_PRIVATE IpAliasKey {
177 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
180 IPEndPoint ip_endpoint;
183 // Needed to be an element of std::set.
184 bool operator<(const IpAliasKey &other) const;
185 bool operator==(const IpAliasKey &other) const;
188 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
189 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
190 typedef std::set<QuicServerId> AliasSet;
191 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
192 typedef std::set<QuicClientSession*> SessionSet;
193 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
194 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
195 typedef std::map<QuicServerId, Job*> JobMap;
196 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
197 typedef std::set<QuicStreamRequest*> RequestSet;
198 typedef std::map<Job*, RequestSet> JobRequestsMap;
200 // Returns a newly created QuicHttpStream owned by the caller, if a
201 // matching session already exists. Returns NULL otherwise.
202 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
203 const BoundNetLog& net_log);
205 bool OnResolution(const QuicServerId& server_id,
206 const AddressList& address_list);
207 void OnJobComplete(Job* job, int rv);
208 bool HasActiveSession(const QuicServerId& server_id) const;
209 bool HasActiveJob(const QuicServerId& server_id) const;
210 int CreateSession(const QuicServerId& server_id,
211 scoped_ptr<QuicServerInfo> quic_server_info,
212 const AddressList& address_list,
213 const BoundNetLog& net_log,
214 QuicClientSession** session);
215 void ActivateSession(const QuicServerId& key,
216 QuicClientSession* session);
218 // Initializes the cached state associated with |server_id| in
219 // |crypto_config_| with the information in |server_info|.
220 void InitializeCachedStateInCryptoConfig(
221 const QuicServerId& server_id,
222 const scoped_ptr<QuicServerInfo>& server_info);
224 void ProcessGoingAwaySession(QuicClientSession* session,
225 const QuicServerId& server_id);
227 bool require_confirmation_;
228 HostResolver* host_resolver_;
229 ClientSocketFactory* client_socket_factory_;
230 base::WeakPtr<HttpServerProperties> http_server_properties_;
231 CertVerifier* cert_verifier_;
232 QuicServerInfoFactory* quic_server_info_factory_;
233 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
234 QuicRandom* random_generator_;
235 scoped_ptr<QuicClock> clock_;
236 const size_t max_packet_length_;
238 // The helper used for all connections.
239 scoped_ptr<QuicConnectionHelper> helper_;
241 // Contains owning pointers to all sessions that currently exist.
242 SessionIdMap all_sessions_;
243 // Contains non-owning pointers to currently active session
244 // (not going away session, once they're implemented).
245 SessionMap active_sessions_;
246 // Map from session to set of aliases that this session is known by.
247 SessionAliasMap session_aliases_;
248 // Map from IP address to sessions which are connected to this address.
249 IPAliasMap ip_aliases_;
251 // Origins which have gone away recently.
252 AliasSet gone_away_aliases_;
255 QuicCryptoClientConfig crypto_config_;
258 JobRequestsMap job_requests_map_;
259 RequestMap active_requests_;
261 QuicVersionVector supported_versions_;
263 // Determine if we should consistently select a client UDP port. If false,
264 // then we will just let the OS select a random client port for each new
266 bool enable_port_selection_;
268 // True if packet pacing should be advertised during the crypto handshake.
271 // Each profile will (probably) have a unique port_seed_ value. This value is
272 // used to help seed a pseudo-random number generator (PortSuggester) so that
273 // we consistently (within this profile) suggest the same ephemeral port when
274 // we re-connect to any given server/port. The differences between profiles
275 // (probablistically) prevent two profiles from colliding in their ephemeral
279 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
281 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
286 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_