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_
;
80 CompletionCallback callback_
;
81 scoped_ptr
<QuicHttpStream
> stream_
;
83 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest
);
86 // A factory for creating new QuicHttpStreams on top of a pool of
87 // QuicClientSessions.
88 class NET_EXPORT_PRIVATE QuicStreamFactory
89 : public NetworkChangeNotifier::IPAddressObserver
,
90 public CertDatabase::Observer
{
93 HostResolver
* host_resolver
,
94 ClientSocketFactory
* client_socket_factory
,
95 base::WeakPtr
<HttpServerProperties
> http_server_properties
,
96 CertVerifier
* cert_verifier
,
97 ChannelIDService
* channel_id_service
,
98 TransportSecurityState
* transport_security_state
,
99 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory
,
100 QuicRandom
* random_generator
,
102 size_t max_packet_length
,
103 const std::string
& user_agent_id
,
104 const QuicVersionVector
& supported_versions
,
105 bool enable_port_selection
,
106 bool always_require_handshake_confirmation
,
107 bool disable_connection_pooling
,
108 int load_server_info_timeout
,
109 float load_server_info_timeout_srtt_multiplier
,
110 bool enable_truncated_connection_ids
,
111 bool enable_connection_racing
,
112 bool disable_disk_cache
,
113 const QuicTagVector
& connection_options
);
114 ~QuicStreamFactory() override
;
116 // Creates a new QuicHttpStream to |host_port_pair| which will be
117 // owned by |request|. |is_https| specifies if the protocol is https or not.
118 // If a matching session already exists, this method will return OK. If no
119 // matching session exists, this will return ERR_IO_PENDING and will invoke
120 // OnRequestComplete asynchronously.
121 int Create(const HostPortPair
& host_port_pair
,
123 PrivacyMode privacy_mode
,
124 base::StringPiece method
,
125 const BoundNetLog
& net_log
,
126 QuicStreamRequest
* request
);
128 // Called by a session when it becomes idle.
129 void OnIdleSession(QuicClientSession
* session
);
131 // Called by a session when it is going away and no more streams should be
133 void OnSessionGoingAway(QuicClientSession
* session
);
135 // Called by a session after it shuts down.
136 void OnSessionClosed(QuicClientSession
* session
);
138 // Called by a session whose connection has timed out.
139 void OnSessionConnectTimeout(QuicClientSession
* session
);
141 // Cancels a pending request.
142 void CancelRequest(QuicStreamRequest
* request
);
144 // Closes all current sessions.
145 void CloseAllSessions(int error
);
147 base::Value
* QuicStreamFactoryInfoToValue() const;
149 // Delete all cached state objects in |crypto_config_|.
150 void ClearCachedStatesInCryptoConfig();
152 // NetworkChangeNotifier::IPAddressObserver methods:
154 // Until the servers support roaming, close all connections when the local
155 // IP address changes.
156 void OnIPAddressChanged() override
;
158 // CertDatabase::Observer methods:
160 // We close all sessions when certificate database is changed.
161 void OnCertAdded(const X509Certificate
* cert
) override
;
162 void OnCACertChanged(const X509Certificate
* cert
) override
;
164 bool require_confirmation() const {
165 return require_confirmation_
;
168 void set_require_confirmation(bool require_confirmation
);
170 QuicConnectionHelper
* helper() { return helper_
.get(); }
172 bool enable_port_selection() const { return enable_port_selection_
; }
174 bool has_quic_server_info_factory() {
175 return quic_server_info_factory_
!= NULL
;
178 void set_quic_server_info_factory(
179 QuicServerInfoFactory
* quic_server_info_factory
) {
180 DCHECK(!quic_server_info_factory_
);
181 quic_server_info_factory_
= quic_server_info_factory
;
184 bool enable_connection_racing() const { return enable_connection_racing_
; }
185 void set_enable_connection_racing(bool enable_connection_racing
) {
186 enable_connection_racing_
= enable_connection_racing
;
191 friend class test::QuicStreamFactoryPeer
;
193 // The key used to find session by ip. Includes
194 // the ip address, port, and scheme.
195 struct NET_EXPORT_PRIVATE IpAliasKey
{
197 IpAliasKey(IPEndPoint ip_endpoint
, bool is_https
);
200 IPEndPoint ip_endpoint
;
203 // Needed to be an element of std::set.
204 bool operator<(const IpAliasKey
&other
) const;
205 bool operator==(const IpAliasKey
&other
) const;
208 typedef std::map
<QuicServerId
, QuicClientSession
*> SessionMap
;
209 typedef std::map
<QuicClientSession
*, QuicServerId
> SessionIdMap
;
210 typedef std::set
<QuicServerId
> AliasSet
;
211 typedef std::map
<QuicClientSession
*, AliasSet
> SessionAliasMap
;
212 typedef std::set
<QuicClientSession
*> SessionSet
;
213 typedef std::map
<IpAliasKey
, SessionSet
> IPAliasMap
;
214 typedef std::map
<QuicServerId
, QuicCryptoClientConfig
*> CryptoConfigMap
;
215 typedef std::set
<Job
*> JobSet
;
216 typedef std::map
<QuicServerId
, JobSet
> JobMap
;
217 typedef std::map
<QuicStreamRequest
*, QuicServerId
> RequestMap
;
218 typedef std::set
<QuicStreamRequest
*> RequestSet
;
219 typedef std::map
<QuicServerId
, RequestSet
> ServerIDRequestsMap
;
221 // Creates a job which doesn't wait for server config to be loaded from the
222 // disk cache. This job is started via a PostTask.
223 void CreateAuxilaryJob(const QuicServerId server_id
,
225 const BoundNetLog
& net_log
);
227 // Returns a newly created QuicHttpStream owned by the caller, if a
228 // matching session already exists. Returns NULL otherwise.
229 scoped_ptr
<QuicHttpStream
> CreateIfSessionExists(const QuicServerId
& key
,
230 const BoundNetLog
& net_log
);
232 bool OnResolution(const QuicServerId
& server_id
,
233 const AddressList
& address_list
);
234 void OnJobComplete(Job
* job
, int rv
);
235 bool HasActiveSession(const QuicServerId
& server_id
) const;
236 bool HasActiveJob(const QuicServerId
& server_id
) const;
237 int CreateSession(const QuicServerId
& server_id
,
238 scoped_ptr
<QuicServerInfo
> quic_server_info
,
239 const AddressList
& address_list
,
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 WasAlternateProtocolRecentlyBroken(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 timeout in milliseconds to wait for loading of QUIC server
314 // information. If we don't want to timeout, set
315 // |load_server_info_timeout_ms_| to 0.
316 int load_server_info_timeout_ms_
;
318 // Specifies the ratio between time to load QUIC server information from disk
319 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in
320 // milliseconds to wait for loading of QUIC server information. If we don't
321 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0.
322 float load_server_info_timeout_srtt_multiplier_
;
324 // Set this for setting config's BytesForConnectionIdToSend (TCID param) to 0.
325 bool enable_truncated_connection_ids_
;
327 // Set if we want to race connections - one connection that sends
328 // INCHOATE_HELLO and another connection that sends CHLO after loading server
329 // config from the disk cache.
330 bool enable_connection_racing_
;
332 // Set if we do not want to load server config from the disk cache.
333 bool disable_disk_cache_
;
335 // Each profile will (probably) have a unique port_seed_ value. This value is
336 // used to help seed a pseudo-random number generator (PortSuggester) so that
337 // we consistently (within this profile) suggest the same ephemeral port when
338 // we re-connect to any given server/port. The differences between profiles
339 // (probablistically) prevent two profiles from colliding in their ephemeral
343 // Local address of socket that was created in CreateSession.
344 IPEndPoint local_address_
;
345 bool check_persisted_supports_quic_
;
346 std::set
<HostPortPair
> quic_supported_servers_at_startup_
;
348 NetworkConnection network_connection_
;
350 base::TaskRunner
* task_runner_
;
352 base::WeakPtrFactory
<QuicStreamFactory
> weak_factory_
;
354 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory
);
359 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_