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 int Request(const HostPortPair
& host_port_pair
,
60 PrivacyMode privacy_mode
,
61 base::StringPiece origin_host
,
62 base::StringPiece method
,
63 const BoundNetLog
& net_log
,
64 const CompletionCallback
& callback
);
66 void OnRequestComplete(int rv
);
68 scoped_ptr
<QuicHttpStream
> ReleaseStream();
70 void set_stream(scoped_ptr
<QuicHttpStream
> stream
);
72 const std::string
origin_host() const { return origin_host_
; }
74 PrivacyMode
privacy_mode() const { return privacy_mode_
; }
76 const BoundNetLog
& net_log() const{
81 QuicStreamFactory
* factory_
;
82 HostPortPair host_port_pair_
;
83 std::string origin_host_
;
84 PrivacyMode privacy_mode_
;
86 CompletionCallback callback_
;
87 scoped_ptr
<QuicHttpStream
> stream_
;
89 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest
);
92 // A factory for creating new QuicHttpStreams on top of a pool of
93 // QuicClientSessions.
94 class NET_EXPORT_PRIVATE QuicStreamFactory
95 : public NetworkChangeNotifier::IPAddressObserver
,
96 public CertDatabase::Observer
{
99 HostResolver
* host_resolver
,
100 ClientSocketFactory
* client_socket_factory
,
101 base::WeakPtr
<HttpServerProperties
> http_server_properties
,
102 CertVerifier
* cert_verifier
,
103 ChannelIDService
* channel_id_service
,
104 TransportSecurityState
* transport_security_state
,
105 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory
,
106 QuicRandom
* random_generator
,
108 size_t max_packet_length
,
109 const std::string
& user_agent_id
,
110 const QuicVersionVector
& supported_versions
,
111 bool enable_port_selection
,
112 bool always_require_handshake_confirmation
,
113 bool disable_connection_pooling
,
114 float load_server_info_timeout_srtt_multiplier
,
115 bool enable_connection_racing
,
116 bool enable_non_blocking_io
,
117 bool disable_disk_cache
,
118 int max_number_of_lossy_connections
,
119 float packet_loss_threshold
,
120 int socket_receive_buffer_size
,
121 const QuicTagVector
& connection_options
);
122 ~QuicStreamFactory() override
;
124 // Creates a new QuicHttpStream to |host_port_pair| which will be
125 // owned by |request|. |is_https| specifies if the protocol is https or not.
126 // If a matching session already exists, this method will return OK. If no
127 // matching session exists, this will return ERR_IO_PENDING and will invoke
128 // OnRequestComplete asynchronously.
129 int Create(const HostPortPair
& host_port_pair
,
131 PrivacyMode privacy_mode
,
132 base::StringPiece origin_host
,
133 base::StringPiece method
,
134 const BoundNetLog
& net_log
,
135 QuicStreamRequest
* request
);
137 // Returns false if |packet_loss_rate| is less than |packet_loss_threshold_|
138 // otherwise it returns true and closes the session and marks QUIC as recently
139 // broken for the port of the session. Increments
140 // |number_of_lossy_connections_| by port.
141 bool OnHandshakeConfirmed(QuicClientSession
* session
, float packet_loss_rate
);
143 // Returns true if QUIC is disabled for this port.
144 bool IsQuicDisabled(uint16 port
);
146 // Called by a session when it becomes idle.
147 void OnIdleSession(QuicClientSession
* session
);
149 // Called by a session when it is going away and no more streams should be
151 void OnSessionGoingAway(QuicClientSession
* session
);
153 // Called by a session after it shuts down.
154 void OnSessionClosed(QuicClientSession
* session
);
156 // Called by a session whose connection has timed out.
157 void OnSessionConnectTimeout(QuicClientSession
* session
);
159 // Cancels a pending request.
160 void CancelRequest(QuicStreamRequest
* request
);
162 // Closes all current sessions.
163 void CloseAllSessions(int error
);
165 scoped_ptr
<base::Value
> QuicStreamFactoryInfoToValue() const;
167 // Delete all cached state objects in |crypto_config_|.
168 void ClearCachedStatesInCryptoConfig();
170 // NetworkChangeNotifier::IPAddressObserver methods:
172 // Until the servers support roaming, close all connections when the local
173 // IP address changes.
174 void OnIPAddressChanged() override
;
176 // CertDatabase::Observer methods:
178 // We close all sessions when certificate database is changed.
179 void OnCertAdded(const X509Certificate
* cert
) override
;
180 void OnCACertChanged(const X509Certificate
* cert
) override
;
182 bool require_confirmation() const {
183 return require_confirmation_
;
186 void set_require_confirmation(bool require_confirmation
);
188 QuicConnectionHelper
* helper() { return helper_
.get(); }
190 bool enable_port_selection() const { return enable_port_selection_
; }
192 bool has_quic_server_info_factory() {
193 return quic_server_info_factory_
!= NULL
;
196 void set_quic_server_info_factory(
197 QuicServerInfoFactory
* quic_server_info_factory
) {
198 DCHECK(!quic_server_info_factory_
);
199 quic_server_info_factory_
= quic_server_info_factory
;
202 bool enable_connection_racing() const { return enable_connection_racing_
; }
203 void set_enable_connection_racing(bool enable_connection_racing
) {
204 enable_connection_racing_
= enable_connection_racing
;
209 friend class test::QuicStreamFactoryPeer
;
211 // The key used to find session by ip. Includes
212 // the ip address, port, and scheme.
213 struct NET_EXPORT_PRIVATE IpAliasKey
{
215 IpAliasKey(IPEndPoint ip_endpoint
, bool is_https
);
218 IPEndPoint ip_endpoint
;
221 // Needed to be an element of std::set.
222 bool operator<(const IpAliasKey
&other
) const;
223 bool operator==(const IpAliasKey
&other
) const;
226 typedef std::map
<QuicServerId
, QuicClientSession
*> SessionMap
;
227 typedef std::map
<QuicClientSession
*, QuicServerId
> SessionIdMap
;
228 typedef std::set
<QuicServerId
> AliasSet
;
229 typedef std::map
<QuicClientSession
*, AliasSet
> SessionAliasMap
;
230 typedef std::set
<QuicClientSession
*> SessionSet
;
231 typedef std::map
<IpAliasKey
, SessionSet
> IPAliasMap
;
232 typedef std::map
<QuicServerId
, QuicCryptoClientConfig
*> CryptoConfigMap
;
233 typedef std::set
<Job
*> JobSet
;
234 typedef std::map
<QuicServerId
, JobSet
> JobMap
;
235 typedef std::map
<QuicStreamRequest
*, QuicServerId
> RequestMap
;
236 typedef std::set
<QuicStreamRequest
*> RequestSet
;
237 typedef std::map
<QuicServerId
, RequestSet
> ServerIDRequestsMap
;
239 // Creates a job which doesn't wait for server config to be loaded from the
240 // disk cache. This job is started via a PostTask.
241 void CreateAuxilaryJob(const QuicServerId server_id
,
242 bool server_and_origin_have_same_host
,
244 const BoundNetLog
& net_log
);
246 // Returns a newly created QuicHttpStream owned by the caller.
247 scoped_ptr
<QuicHttpStream
> CreateFromSession(QuicClientSession
*);
249 bool OnResolution(const QuicServerId
& server_id
,
250 const AddressList
& address_list
);
251 void OnJobComplete(Job
* job
, int rv
);
252 bool HasActiveSession(const QuicServerId
& server_id
) const;
253 bool HasActiveJob(const QuicServerId
& server_id
) const;
254 int CreateSession(const QuicServerId
& server_id
,
255 scoped_ptr
<QuicServerInfo
> quic_server_info
,
256 const AddressList
& address_list
,
257 base::TimeTicks dns_resolution_end_time
,
258 const BoundNetLog
& net_log
,
259 QuicClientSession
** session
);
260 void ActivateSession(const QuicServerId
& key
,
261 QuicClientSession
* session
);
263 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there
264 // is no |http_server_properties_| or if |http_server_properties_| doesn't
265 // have ServerNetworkStats for the given |server_id|.
266 int64
GetServerNetworkStatsSmoothedRttInMicroseconds(
267 const QuicServerId
& server_id
) const;
270 bool WasQuicRecentlyBroken(const QuicServerId
& server_id
) const;
271 bool CryptoConfigCacheIsEmpty(const QuicServerId
& server_id
);
273 // Initializes the cached state associated with |server_id| in
274 // |crypto_config_| with the information in |server_info|.
275 void InitializeCachedStateInCryptoConfig(
276 const QuicServerId
& server_id
,
277 const scoped_ptr
<QuicServerInfo
>& server_info
);
279 void ProcessGoingAwaySession(QuicClientSession
* session
,
280 const QuicServerId
& server_id
,
281 bool was_session_active
);
283 bool require_confirmation_
;
284 HostResolver
* host_resolver_
;
285 ClientSocketFactory
* client_socket_factory_
;
286 base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
287 TransportSecurityState
* transport_security_state_
;
288 QuicServerInfoFactory
* quic_server_info_factory_
;
289 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory_
;
290 QuicRandom
* random_generator_
;
291 scoped_ptr
<QuicClock
> clock_
;
292 const size_t max_packet_length_
;
294 // The helper used for all connections.
295 scoped_ptr
<QuicConnectionHelper
> helper_
;
297 // Contains owning pointers to all sessions that currently exist.
298 SessionIdMap all_sessions_
;
299 // Contains non-owning pointers to currently active session
300 // (not going away session, once they're implemented).
301 SessionMap active_sessions_
;
302 // Map from session to set of aliases that this session is known by.
303 SessionAliasMap session_aliases_
;
304 // Map from IP address to sessions which are connected to this address.
305 IPAliasMap ip_aliases_
;
307 // Origins which have gone away recently.
308 AliasSet gone_away_aliases_
;
310 const QuicConfig config_
;
311 QuicCryptoClientConfig crypto_config_
;
314 ServerIDRequestsMap job_requests_map_
;
315 RequestMap active_requests_
;
317 QuicVersionVector supported_versions_
;
319 // Determine if we should consistently select a client UDP port. If false,
320 // then we will just let the OS select a random client port for each new
322 bool enable_port_selection_
;
324 // Set if we always require handshake confirmation. If true, this will
325 // introduce at least one RTT for the handshake before the client sends data.
326 bool always_require_handshake_confirmation_
;
328 // Set if we do not want connection pooling.
329 bool disable_connection_pooling_
;
331 // Specifies the ratio between time to load QUIC server information from disk
332 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in
333 // milliseconds to wait for loading of QUIC server information. If we don't
334 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0.
335 float load_server_info_timeout_srtt_multiplier_
;
337 // Set if we want to race connections - one connection that sends
338 // INCHOATE_HELLO and another connection that sends CHLO after loading server
339 // config from the disk cache.
340 bool enable_connection_racing_
;
342 // Set if experimental non-blocking IO should be used on windows sockets.
343 bool enable_non_blocking_io_
;
345 // Set if we do not want to load server config from the disk cache.
346 bool disable_disk_cache_
;
348 // Set if we want to disable QUIC when there is high packet loss rate.
349 // Specifies the maximum number of connections with high packet loss in a row
350 // after which QUIC will be disabled.
351 int max_number_of_lossy_connections_
;
352 // Specifies packet loss rate in fraction after which a connection is closed
353 // and is considered as a lossy connection.
354 float packet_loss_threshold_
;
355 // Count number of lossy connections by port.
356 std::map
<uint16
, int> number_of_lossy_connections_
;
358 // Size of the UDP receive buffer.
359 int socket_receive_buffer_size_
;
361 // Each profile will (probably) have a unique port_seed_ value. This value
362 // is used to help seed a pseudo-random number generator (PortSuggester) so
363 // that we consistently (within this profile) suggest the same ephemeral
364 // port when we re-connect to any given server/port. The differences between
365 // profiles (probablistically) prevent two profiles from colliding in their
366 // ephemeral port requests.
369 // Local address of socket that was created in CreateSession.
370 IPEndPoint local_address_
;
371 bool check_persisted_supports_quic_
;
372 std::set
<HostPortPair
> quic_supported_servers_at_startup_
;
374 NetworkConnection network_connection_
;
376 base::TaskRunner
* task_runner_
;
378 base::WeakPtrFactory
<QuicStreamFactory
> weak_factory_
;
380 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory
);
385 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_