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/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 method
,
62 const BoundNetLog
& net_log
,
63 const CompletionCallback
& callback
);
65 void OnRequestComplete(int rv
);
67 scoped_ptr
<QuicHttpStream
> ReleaseStream();
69 void set_stream(scoped_ptr
<QuicHttpStream
> stream
);
71 const BoundNetLog
& net_log() const{
76 QuicStreamFactory
* factory_
;
77 HostPortPair host_port_pair_
;
79 CompletionCallback callback_
;
80 scoped_ptr
<QuicHttpStream
> stream_
;
82 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest
);
85 // A factory for creating new QuicHttpStreams on top of a pool of
86 // QuicClientSessions.
87 class NET_EXPORT_PRIVATE QuicStreamFactory
88 : public NetworkChangeNotifier::IPAddressObserver
,
89 public CertDatabase::Observer
{
92 HostResolver
* host_resolver
,
93 ClientSocketFactory
* client_socket_factory
,
94 base::WeakPtr
<HttpServerProperties
> http_server_properties
,
95 CertVerifier
* cert_verifier
,
96 ChannelIDService
* channel_id_service
,
97 TransportSecurityState
* transport_security_state
,
98 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory
,
99 QuicRandom
* random_generator
,
101 size_t max_packet_length
,
102 const std::string
& user_agent_id
,
103 const QuicVersionVector
& supported_versions
,
104 bool enable_port_selection
,
105 bool always_require_handshake_confirmation
,
106 bool disable_connection_pooling
,
107 float load_server_info_timeout_srtt_multiplier
,
108 bool enable_connection_racing
,
109 bool enable_non_blocking_io
,
110 bool disable_disk_cache
,
111 int socket_receive_buffer_size
,
112 const QuicTagVector
& connection_options
);
113 ~QuicStreamFactory() override
;
115 // Creates a new QuicHttpStream to |host_port_pair| which will be
116 // owned by |request|. |is_https| specifies if the protocol is https or not.
117 // If a matching session already exists, this method will return OK. If no
118 // matching session exists, this will return ERR_IO_PENDING and will invoke
119 // OnRequestComplete asynchronously.
120 int Create(const HostPortPair
& host_port_pair
,
122 PrivacyMode privacy_mode
,
123 base::StringPiece method
,
124 const BoundNetLog
& net_log
,
125 QuicStreamRequest
* request
);
127 // Called by a session when it becomes idle.
128 void OnIdleSession(QuicClientSession
* session
);
130 // Called by a session when it is going away and no more streams should be
132 void OnSessionGoingAway(QuicClientSession
* session
);
134 // Called by a session after it shuts down.
135 void OnSessionClosed(QuicClientSession
* session
);
137 // Called by a session whose connection has timed out.
138 void OnSessionConnectTimeout(QuicClientSession
* session
);
140 // Cancels a pending request.
141 void CancelRequest(QuicStreamRequest
* request
);
143 // Closes all current sessions.
144 void CloseAllSessions(int error
);
146 base::Value
* QuicStreamFactoryInfoToValue() const;
148 // Delete all cached state objects in |crypto_config_|.
149 void ClearCachedStatesInCryptoConfig();
151 // NetworkChangeNotifier::IPAddressObserver methods:
153 // Until the servers support roaming, close all connections when the local
154 // IP address changes.
155 void OnIPAddressChanged() override
;
157 // CertDatabase::Observer methods:
159 // We close all sessions when certificate database is changed.
160 void OnCertAdded(const X509Certificate
* cert
) override
;
161 void OnCACertChanged(const X509Certificate
* cert
) override
;
163 bool require_confirmation() const {
164 return require_confirmation_
;
167 void set_require_confirmation(bool require_confirmation
);
169 QuicConnectionHelper
* helper() { return helper_
.get(); }
171 bool enable_port_selection() const { return enable_port_selection_
; }
173 bool has_quic_server_info_factory() {
174 return quic_server_info_factory_
!= NULL
;
177 void set_quic_server_info_factory(
178 QuicServerInfoFactory
* quic_server_info_factory
) {
179 DCHECK(!quic_server_info_factory_
);
180 quic_server_info_factory_
= quic_server_info_factory
;
183 bool enable_connection_racing() const { return enable_connection_racing_
; }
184 void set_enable_connection_racing(bool enable_connection_racing
) {
185 enable_connection_racing_
= enable_connection_racing
;
190 friend class test::QuicStreamFactoryPeer
;
192 // The key used to find session by ip. Includes
193 // the ip address, port, and scheme.
194 struct NET_EXPORT_PRIVATE IpAliasKey
{
196 IpAliasKey(IPEndPoint ip_endpoint
, bool is_https
);
199 IPEndPoint ip_endpoint
;
202 // Needed to be an element of std::set.
203 bool operator<(const IpAliasKey
&other
) const;
204 bool operator==(const IpAliasKey
&other
) const;
207 typedef std::map
<QuicServerId
, QuicClientSession
*> SessionMap
;
208 typedef std::map
<QuicClientSession
*, QuicServerId
> SessionIdMap
;
209 typedef std::set
<QuicServerId
> AliasSet
;
210 typedef std::map
<QuicClientSession
*, AliasSet
> SessionAliasMap
;
211 typedef std::set
<QuicClientSession
*> SessionSet
;
212 typedef std::map
<IpAliasKey
, SessionSet
> IPAliasMap
;
213 typedef std::map
<QuicServerId
, QuicCryptoClientConfig
*> CryptoConfigMap
;
214 typedef std::set
<Job
*> JobSet
;
215 typedef std::map
<QuicServerId
, JobSet
> JobMap
;
216 typedef std::map
<QuicStreamRequest
*, QuicServerId
> RequestMap
;
217 typedef std::set
<QuicStreamRequest
*> RequestSet
;
218 typedef std::map
<QuicServerId
, RequestSet
> ServerIDRequestsMap
;
220 // Creates a job which doesn't wait for server config to be loaded from the
221 // disk cache. This job is started via a PostTask.
222 void CreateAuxilaryJob(const QuicServerId server_id
,
224 const BoundNetLog
& net_log
);
226 // Returns a newly created QuicHttpStream owned by the caller, if a
227 // matching session already exists. Returns NULL otherwise.
228 scoped_ptr
<QuicHttpStream
> CreateIfSessionExists(const QuicServerId
& key
,
229 const BoundNetLog
& net_log
);
231 bool OnResolution(const QuicServerId
& server_id
,
232 const AddressList
& address_list
);
233 void OnJobComplete(Job
* job
, int rv
);
234 bool HasActiveSession(const QuicServerId
& server_id
) const;
235 bool HasActiveJob(const QuicServerId
& server_id
) const;
236 int CreateSession(const QuicServerId
& server_id
,
237 scoped_ptr
<QuicServerInfo
> quic_server_info
,
238 const AddressList
& address_list
,
239 base::TimeTicks dns_resolution_end_time
,
240 const BoundNetLog
& net_log
,
241 QuicClientSession
** session
);
242 void ActivateSession(const QuicServerId
& key
,
243 QuicClientSession
* session
);
245 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there
246 // is no |http_server_properties_| or if |http_server_properties_| doesn't
247 // have ServerNetworkStats for the given |server_id|.
248 int64
GetServerNetworkStatsSmoothedRttInMicroseconds(
249 const QuicServerId
& server_id
) const;
252 bool WasQuicRecentlyBroken(const QuicServerId
& server_id
) const;
253 bool CryptoConfigCacheIsEmpty(const QuicServerId
& server_id
);
255 // Initializes the cached state associated with |server_id| in
256 // |crypto_config_| with the information in |server_info|.
257 void InitializeCachedStateInCryptoConfig(
258 const QuicServerId
& server_id
,
259 const scoped_ptr
<QuicServerInfo
>& server_info
);
261 void ProcessGoingAwaySession(QuicClientSession
* session
,
262 const QuicServerId
& server_id
,
263 bool was_session_active
);
265 bool require_confirmation_
;
266 HostResolver
* host_resolver_
;
267 ClientSocketFactory
* client_socket_factory_
;
268 base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
269 TransportSecurityState
* transport_security_state_
;
270 QuicServerInfoFactory
* quic_server_info_factory_
;
271 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory_
;
272 QuicRandom
* random_generator_
;
273 scoped_ptr
<QuicClock
> clock_
;
274 const size_t max_packet_length_
;
276 // The helper used for all connections.
277 scoped_ptr
<QuicConnectionHelper
> helper_
;
279 // Contains owning pointers to all sessions that currently exist.
280 SessionIdMap all_sessions_
;
281 // Contains non-owning pointers to currently active session
282 // (not going away session, once they're implemented).
283 SessionMap active_sessions_
;
284 // Map from session to set of aliases that this session is known by.
285 SessionAliasMap session_aliases_
;
286 // Map from IP address to sessions which are connected to this address.
287 IPAliasMap ip_aliases_
;
289 // Origins which have gone away recently.
290 AliasSet gone_away_aliases_
;
292 const QuicConfig config_
;
293 QuicCryptoClientConfig crypto_config_
;
296 ServerIDRequestsMap job_requests_map_
;
297 RequestMap active_requests_
;
299 QuicVersionVector supported_versions_
;
301 // Determine if we should consistently select a client UDP port. If false,
302 // then we will just let the OS select a random client port for each new
304 bool enable_port_selection_
;
306 // Set if we always require handshake confirmation. If true, this will
307 // introduce at least one RTT for the handshake before the client sends data.
308 bool always_require_handshake_confirmation_
;
310 // Set if we do not want connection pooling.
311 bool disable_connection_pooling_
;
313 // Specifies the ratio between time to load QUIC server information from disk
314 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in
315 // milliseconds to wait for loading of QUIC server information. If we don't
316 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0.
317 float load_server_info_timeout_srtt_multiplier_
;
319 // Set if we want to race connections - one connection that sends
320 // INCHOATE_HELLO and another connection that sends CHLO after loading server
321 // config from the disk cache.
322 bool enable_connection_racing_
;
324 // Set if experimental non-blocking IO should be used on windows sockets.
325 bool enable_non_blocking_io_
;
327 // Set if we do not want to load server config from the disk cache.
328 bool disable_disk_cache_
;
330 // Size of the UDP receive buffer.
331 int socket_receive_buffer_size_
;
333 // Each profile will (probably) have a unique port_seed_ value. This value
334 // is used to help seed a pseudo-random number generator (PortSuggester) so
335 // that we consistently (within this profile) suggest the same ephemeral
336 // port when we re-connect to any given server/port. The differences between
337 // profiles (probablistically) prevent two profiles from colliding in their
338 // ephemeral port requests.
341 // Local address of socket that was created in CreateSession.
342 IPEndPoint local_address_
;
343 bool check_persisted_supports_quic_
;
344 std::set
<HostPortPair
> quic_supported_servers_at_startup_
;
346 NetworkConnection network_connection_
;
348 base::TaskRunner
* task_runner_
;
350 base::WeakPtrFactory
<QuicStreamFactory
> weak_factory_
;
352 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory
);
357 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_