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/network_change_notifier.h"
19 #include "net/cert/cert_database.h"
20 #include "net/log/net_log.h"
21 #include "net/proxy/proxy_server.h"
22 #include "net/quic/network_connection.h"
23 #include "net/quic/quic_config.h"
24 #include "net/quic/quic_crypto_stream.h"
25 #include "net/quic/quic_http_stream.h"
26 #include "net/quic/quic_protocol.h"
31 class ChannelIDService
;
32 class ClientSocketFactory
;
34 class HttpServerProperties
;
36 class QuicClientSession
;
37 class QuicConnectionHelper
;
38 class QuicCryptoClientStreamFactory
;
40 class QuicServerInfoFactory
;
42 class QuicStreamFactory
;
43 class TransportSecurityState
;
46 class QuicStreamFactoryPeer
;
49 // Encapsulates a pending request for a QuicHttpStream.
50 // If the request is still pending when it is destroyed, it will
51 // cancel the request with the factory.
52 class NET_EXPORT_PRIVATE QuicStreamRequest
{
54 explicit QuicStreamRequest(QuicStreamFactory
* factory
);
57 // For http, |is_https| is false.
58 // |cert_verify_flags| is bitwise OR'd of CertVerifier::VerifyFlags and it is
59 // passed to CertVerifier::Verify.
60 int Request(const HostPortPair
& host_port_pair
,
62 PrivacyMode privacy_mode
,
63 int cert_verify_flags
,
64 base::StringPiece origin_host
,
65 base::StringPiece method
,
66 const BoundNetLog
& net_log
,
67 const CompletionCallback
& callback
);
69 void OnRequestComplete(int rv
);
71 scoped_ptr
<QuicHttpStream
> ReleaseStream();
73 void set_stream(scoped_ptr
<QuicHttpStream
> stream
);
75 const std::string
origin_host() const { return origin_host_
; }
77 PrivacyMode
privacy_mode() const { return privacy_mode_
; }
79 const BoundNetLog
& net_log() const{
84 QuicStreamFactory
* factory_
;
85 HostPortPair host_port_pair_
;
86 std::string origin_host_
;
87 PrivacyMode privacy_mode_
;
89 CompletionCallback callback_
;
90 scoped_ptr
<QuicHttpStream
> stream_
;
92 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest
);
95 // A factory for creating new QuicHttpStreams on top of a pool of
96 // QuicClientSessions.
97 class NET_EXPORT_PRIVATE QuicStreamFactory
98 : public NetworkChangeNotifier::IPAddressObserver
,
99 public CertDatabase::Observer
{
102 HostResolver
* host_resolver
,
103 ClientSocketFactory
* client_socket_factory
,
104 base::WeakPtr
<HttpServerProperties
> http_server_properties
,
105 CertVerifier
* cert_verifier
,
106 ChannelIDService
* channel_id_service
,
107 TransportSecurityState
* transport_security_state
,
108 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory
,
109 QuicRandom
* random_generator
,
111 size_t max_packet_length
,
112 const std::string
& user_agent_id
,
113 const QuicVersionVector
& supported_versions
,
114 bool enable_port_selection
,
115 bool always_require_handshake_confirmation
,
116 bool disable_connection_pooling
,
117 float load_server_info_timeout_srtt_multiplier
,
118 bool enable_connection_racing
,
119 bool enable_non_blocking_io
,
120 bool disable_disk_cache
,
122 int max_number_of_lossy_connections
,
123 float packet_loss_threshold
,
124 int socket_receive_buffer_size
,
125 const QuicTagVector
& connection_options
);
126 ~QuicStreamFactory() override
;
128 // Creates a new QuicHttpStream to |host_port_pair| which will be
129 // owned by |request|. |is_https| specifies if the protocol is https or not.
130 // If a matching session already exists, this method will return OK. If no
131 // matching session exists, this will return ERR_IO_PENDING and will invoke
132 // OnRequestComplete asynchronously.
133 int Create(const HostPortPair
& host_port_pair
,
135 PrivacyMode privacy_mode
,
136 int cert_verify_flags
,
137 base::StringPiece origin_host
,
138 base::StringPiece method
,
139 const BoundNetLog
& net_log
,
140 QuicStreamRequest
* request
);
142 // If |packet_loss_rate| is greater than or equal to |packet_loss_threshold_|
143 // it marks QUIC as recently broken for the port of the session. Increments
144 // |number_of_lossy_connections_| by port. If |number_of_lossy_connections_|
145 // is greater than or equal to |max_number_of_lossy_connections_| then it
146 // disables QUIC. If QUIC is disabled then it closes the connection.
148 // Returns true if QUIC is disabled for the port of the session.
149 bool OnHandshakeConfirmed(QuicClientSession
* session
, float packet_loss_rate
);
151 // Returns true if QUIC is disabled for this port.
152 bool IsQuicDisabled(uint16 port
);
154 // Called by a session when it becomes idle.
155 void OnIdleSession(QuicClientSession
* session
);
157 // Called by a session when it is going away and no more streams should be
159 void OnSessionGoingAway(QuicClientSession
* session
);
161 // Called by a session after it shuts down.
162 void OnSessionClosed(QuicClientSession
* session
);
164 // Called by a session whose connection has timed out.
165 void OnSessionConnectTimeout(QuicClientSession
* session
);
167 // Cancels a pending request.
168 void CancelRequest(QuicStreamRequest
* request
);
170 // Closes all current sessions.
171 void CloseAllSessions(int error
);
173 scoped_ptr
<base::Value
> QuicStreamFactoryInfoToValue() const;
175 // Delete all cached state objects in |crypto_config_|.
176 void ClearCachedStatesInCryptoConfig();
178 // NetworkChangeNotifier::IPAddressObserver methods:
180 // Until the servers support roaming, close all connections when the local
181 // IP address changes.
182 void OnIPAddressChanged() override
;
184 // CertDatabase::Observer methods:
186 // We close all sessions when certificate database is changed.
187 void OnCertAdded(const X509Certificate
* cert
) override
;
188 void OnCACertChanged(const X509Certificate
* cert
) override
;
190 bool require_confirmation() const {
191 return require_confirmation_
;
194 void set_require_confirmation(bool require_confirmation
);
196 QuicConnectionHelper
* helper() { return helper_
.get(); }
198 bool enable_port_selection() const { return enable_port_selection_
; }
200 bool has_quic_server_info_factory() {
201 return quic_server_info_factory_
!= NULL
;
204 void set_quic_server_info_factory(
205 QuicServerInfoFactory
* quic_server_info_factory
) {
206 DCHECK(!quic_server_info_factory_
);
207 quic_server_info_factory_
= quic_server_info_factory
;
210 bool enable_connection_racing() const { return enable_connection_racing_
; }
211 void set_enable_connection_racing(bool enable_connection_racing
) {
212 enable_connection_racing_
= enable_connection_racing
;
217 friend class test::QuicStreamFactoryPeer
;
219 // The key used to find session by ip. Includes
220 // the ip address, port, and scheme.
221 struct NET_EXPORT_PRIVATE IpAliasKey
{
223 IpAliasKey(IPEndPoint ip_endpoint
, bool is_https
);
226 IPEndPoint ip_endpoint
;
229 // Needed to be an element of std::set.
230 bool operator<(const IpAliasKey
&other
) const;
231 bool operator==(const IpAliasKey
&other
) const;
234 typedef std::map
<QuicServerId
, QuicClientSession
*> SessionMap
;
235 typedef std::map
<QuicClientSession
*, QuicServerId
> SessionIdMap
;
236 typedef std::set
<QuicServerId
> AliasSet
;
237 typedef std::map
<QuicClientSession
*, AliasSet
> SessionAliasMap
;
238 typedef std::set
<QuicClientSession
*> SessionSet
;
239 typedef std::map
<IpAliasKey
, SessionSet
> IPAliasMap
;
240 typedef std::map
<QuicServerId
, QuicCryptoClientConfig
*> CryptoConfigMap
;
241 typedef std::set
<Job
*> JobSet
;
242 typedef std::map
<QuicServerId
, JobSet
> JobMap
;
243 typedef std::map
<QuicStreamRequest
*, QuicServerId
> RequestMap
;
244 typedef std::set
<QuicStreamRequest
*> RequestSet
;
245 typedef std::map
<QuicServerId
, RequestSet
> ServerIDRequestsMap
;
247 // Creates a job which doesn't wait for server config to be loaded from the
248 // disk cache. This job is started via a PostTask.
249 void CreateAuxilaryJob(const QuicServerId server_id
,
250 int cert_verify_flags
,
251 bool server_and_origin_have_same_host
,
253 const BoundNetLog
& net_log
);
255 // Returns a newly created QuicHttpStream owned by the caller.
256 scoped_ptr
<QuicHttpStream
> CreateFromSession(QuicClientSession
*);
258 bool OnResolution(const QuicServerId
& server_id
,
259 const AddressList
& address_list
);
260 void OnJobComplete(Job
* job
, int rv
);
261 bool HasActiveSession(const QuicServerId
& server_id
) const;
262 bool HasActiveJob(const QuicServerId
& server_id
) const;
263 int CreateSession(const QuicServerId
& server_id
,
264 int cert_verify_flags
,
265 scoped_ptr
<QuicServerInfo
> quic_server_info
,
266 const AddressList
& address_list
,
267 base::TimeTicks dns_resolution_end_time
,
268 const BoundNetLog
& net_log
,
269 QuicClientSession
** session
);
270 void ActivateSession(const QuicServerId
& key
,
271 QuicClientSession
* session
);
273 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there
274 // is no |http_server_properties_| or if |http_server_properties_| doesn't
275 // have ServerNetworkStats for the given |server_id|.
276 int64
GetServerNetworkStatsSmoothedRttInMicroseconds(
277 const QuicServerId
& server_id
) const;
280 bool WasQuicRecentlyBroken(const QuicServerId
& server_id
) const;
281 bool CryptoConfigCacheIsEmpty(const QuicServerId
& server_id
);
283 // Initializes the cached state associated with |server_id| in
284 // |crypto_config_| with the information in |server_info|.
285 void InitializeCachedStateInCryptoConfig(
286 const QuicServerId
& server_id
,
287 const scoped_ptr
<QuicServerInfo
>& server_info
);
289 void ProcessGoingAwaySession(QuicClientSession
* session
,
290 const QuicServerId
& server_id
,
291 bool was_session_active
);
293 bool require_confirmation_
;
294 HostResolver
* host_resolver_
;
295 ClientSocketFactory
* client_socket_factory_
;
296 base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
297 TransportSecurityState
* transport_security_state_
;
298 QuicServerInfoFactory
* quic_server_info_factory_
;
299 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory_
;
300 QuicRandom
* random_generator_
;
301 scoped_ptr
<QuicClock
> clock_
;
302 const size_t max_packet_length_
;
304 // The helper used for all connections.
305 scoped_ptr
<QuicConnectionHelper
> helper_
;
307 // Contains owning pointers to all sessions that currently exist.
308 SessionIdMap all_sessions_
;
309 // Contains non-owning pointers to currently active session
310 // (not going away session, once they're implemented).
311 SessionMap active_sessions_
;
312 // Map from session to set of aliases that this session is known by.
313 SessionAliasMap session_aliases_
;
314 // Map from IP address to sessions which are connected to this address.
315 IPAliasMap ip_aliases_
;
317 // Origins which have gone away recently.
318 AliasSet gone_away_aliases_
;
320 const QuicConfig config_
;
321 QuicCryptoClientConfig crypto_config_
;
324 ServerIDRequestsMap job_requests_map_
;
325 RequestMap active_requests_
;
327 QuicVersionVector supported_versions_
;
329 // Determine if we should consistently select a client UDP port. If false,
330 // then we will just let the OS select a random client port for each new
332 bool enable_port_selection_
;
334 // Set if we always require handshake confirmation. If true, this will
335 // introduce at least one RTT for the handshake before the client sends data.
336 bool always_require_handshake_confirmation_
;
338 // Set if we do not want connection pooling.
339 bool disable_connection_pooling_
;
341 // Specifies the ratio between time to load QUIC server information from disk
342 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in
343 // milliseconds to wait for loading of QUIC server information. If we don't
344 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0.
345 float load_server_info_timeout_srtt_multiplier_
;
347 // Set if we want to race connections - one connection that sends
348 // INCHOATE_HELLO and another connection that sends CHLO after loading server
349 // config from the disk cache.
350 bool enable_connection_racing_
;
352 // Set if experimental non-blocking IO should be used on windows sockets.
353 bool enable_non_blocking_io_
;
355 // Set if we do not want to load server config from the disk cache.
356 bool disable_disk_cache_
;
358 // Set if AES-GCM should be preferred, even if there is no hardware support.
361 // Set if we want to disable QUIC when there is high packet loss rate.
362 // Specifies the maximum number of connections with high packet loss in a row
363 // after which QUIC will be disabled.
364 int max_number_of_lossy_connections_
;
365 // Specifies packet loss rate in fraction after which a connection is closed
366 // and is considered as a lossy connection.
367 float packet_loss_threshold_
;
368 // Count number of lossy connections by port.
369 std::map
<uint16
, int> number_of_lossy_connections_
;
371 // Size of the UDP receive buffer.
372 int socket_receive_buffer_size_
;
374 // Each profile will (probably) have a unique port_seed_ value. This value
375 // is used to help seed a pseudo-random number generator (PortSuggester) so
376 // that we consistently (within this profile) suggest the same ephemeral
377 // port when we re-connect to any given server/port. The differences between
378 // profiles (probablistically) prevent two profiles from colliding in their
379 // ephemeral port requests.
382 // Local address of socket that was created in CreateSession.
383 IPEndPoint local_address_
;
384 bool check_persisted_supports_quic_
;
385 std::set
<HostPortPair
> quic_supported_servers_at_startup_
;
387 NetworkConnection network_connection_
;
389 base::TaskRunner
* task_runner_
;
391 base::WeakPtrFactory
<QuicStreamFactory
> weak_factory_
;
393 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory
);
398 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_