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 ChannelIDService
;
31 class ClientSocketFactory
;
33 class HttpServerProperties
;
35 class QuicClientSession
;
36 class QuicConnectionHelper
;
37 class QuicCryptoClientStreamFactory
;
39 class QuicServerInfoFactory
;
41 class QuicStreamFactory
;
42 class TransportSecurityState
;
45 class QuicStreamFactoryPeer
;
48 // Encapsulates a pending request for a QuicHttpStream.
49 // If the request is still pending when it is destroyed, it will
50 // cancel the request with the factory.
51 class NET_EXPORT_PRIVATE QuicStreamRequest
{
53 explicit QuicStreamRequest(QuicStreamFactory
* factory
);
56 // For http, |is_https| is false.
57 int Request(const HostPortPair
& host_port_pair
,
59 PrivacyMode privacy_mode
,
60 base::StringPiece method
,
61 const BoundNetLog
& net_log
,
62 const CompletionCallback
& callback
);
64 void OnRequestComplete(int rv
);
66 scoped_ptr
<QuicHttpStream
> ReleaseStream();
68 void set_stream(scoped_ptr
<QuicHttpStream
> stream
);
70 const BoundNetLog
& net_log() const{
75 QuicStreamFactory
* factory_
;
76 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 const QuicTagVector
& connection_options
);
108 ~QuicStreamFactory() override
;
110 // Creates a new QuicHttpStream to |host_port_pair| which will be
111 // owned by |request|. |is_https| specifies if the protocol is https or not.
112 // If a matching session already exists, this method will return OK. If no
113 // matching session exists, this will return ERR_IO_PENDING and will invoke
114 // OnRequestComplete asynchronously.
115 int Create(const HostPortPair
& host_port_pair
,
117 PrivacyMode privacy_mode
,
118 base::StringPiece method
,
119 const BoundNetLog
& net_log
,
120 QuicStreamRequest
* request
);
122 // Called by a session when it becomes idle.
123 void OnIdleSession(QuicClientSession
* session
);
125 // Called by a session when it is going away and no more streams should be
127 void OnSessionGoingAway(QuicClientSession
* session
);
129 // Called by a session after it shuts down.
130 void OnSessionClosed(QuicClientSession
* session
);
132 // Called by a session whose connection has timed out.
133 void OnSessionConnectTimeout(QuicClientSession
* session
);
135 // Cancels a pending request.
136 void CancelRequest(QuicStreamRequest
* request
);
138 // Closes all current sessions.
139 void CloseAllSessions(int error
);
141 base::Value
* QuicStreamFactoryInfoToValue() const;
143 // Delete all cached state objects in |crypto_config_|.
144 void ClearCachedStatesInCryptoConfig();
146 // NetworkChangeNotifier::IPAddressObserver methods:
148 // Until the servers support roaming, close all connections when the local
149 // IP address changes.
150 void OnIPAddressChanged() override
;
152 // CertDatabase::Observer methods:
154 // We close all sessions when certificate database is changed.
155 void OnCertAdded(const X509Certificate
* cert
) override
;
156 void OnCACertChanged(const X509Certificate
* cert
) override
;
158 bool require_confirmation() const {
159 return require_confirmation_
;
162 void set_require_confirmation(bool require_confirmation
);
164 QuicConnectionHelper
* helper() { return helper_
.get(); }
166 bool enable_port_selection() const { return enable_port_selection_
; }
168 bool has_quic_server_info_factory() {
169 return quic_server_info_factory_
!= NULL
;
172 void set_quic_server_info_factory(
173 QuicServerInfoFactory
* quic_server_info_factory
) {
174 DCHECK(!quic_server_info_factory_
);
175 quic_server_info_factory_
= quic_server_info_factory
;
180 friend class test::QuicStreamFactoryPeer
;
182 // The key used to find session by ip. Includes
183 // the ip address, port, and scheme.
184 struct NET_EXPORT_PRIVATE IpAliasKey
{
186 IpAliasKey(IPEndPoint ip_endpoint
, bool is_https
);
189 IPEndPoint ip_endpoint
;
192 // Needed to be an element of std::set.
193 bool operator<(const IpAliasKey
&other
) const;
194 bool operator==(const IpAliasKey
&other
) const;
197 typedef std::map
<QuicServerId
, QuicClientSession
*> SessionMap
;
198 typedef std::map
<QuicClientSession
*, QuicServerId
> SessionIdMap
;
199 typedef std::set
<QuicServerId
> AliasSet
;
200 typedef std::map
<QuicClientSession
*, AliasSet
> SessionAliasMap
;
201 typedef std::set
<QuicClientSession
*> SessionSet
;
202 typedef std::map
<IpAliasKey
, SessionSet
> IPAliasMap
;
203 typedef std::map
<QuicServerId
, QuicCryptoClientConfig
*> CryptoConfigMap
;
204 typedef std::map
<QuicServerId
, Job
*> JobMap
;
205 typedef std::map
<QuicStreamRequest
*, Job
*> RequestMap
;
206 typedef std::set
<QuicStreamRequest
*> RequestSet
;
207 typedef std::map
<Job
*, RequestSet
> JobRequestsMap
;
209 // Returns a newly created QuicHttpStream owned by the caller, if a
210 // matching session already exists. Returns NULL otherwise.
211 scoped_ptr
<QuicHttpStream
> CreateIfSessionExists(const QuicServerId
& key
,
212 const BoundNetLog
& net_log
);
214 bool OnResolution(const QuicServerId
& server_id
,
215 const AddressList
& address_list
);
216 void OnJobComplete(Job
* job
, int rv
);
217 bool HasActiveSession(const QuicServerId
& server_id
) const;
218 bool HasActiveJob(const QuicServerId
& server_id
) const;
219 int CreateSession(const QuicServerId
& server_id
,
220 scoped_ptr
<QuicServerInfo
> quic_server_info
,
221 const AddressList
& address_list
,
222 const BoundNetLog
& net_log
,
223 QuicClientSession
** session
);
224 void ActivateSession(const QuicServerId
& key
,
225 QuicClientSession
* session
);
227 // Initializes the cached state associated with |server_id| in
228 // |crypto_config_| with the information in |server_info|.
229 void InitializeCachedStateInCryptoConfig(
230 const QuicServerId
& server_id
,
231 const scoped_ptr
<QuicServerInfo
>& server_info
);
233 void ProcessGoingAwaySession(QuicClientSession
* session
,
234 const QuicServerId
& server_id
,
235 bool was_session_active
);
237 bool require_confirmation_
;
238 HostResolver
* host_resolver_
;
239 ClientSocketFactory
* client_socket_factory_
;
240 base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
241 TransportSecurityState
* transport_security_state_
;
242 QuicServerInfoFactory
* quic_server_info_factory_
;
243 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory_
;
244 QuicRandom
* random_generator_
;
245 scoped_ptr
<QuicClock
> clock_
;
246 const size_t max_packet_length_
;
248 // The helper used for all connections.
249 scoped_ptr
<QuicConnectionHelper
> helper_
;
251 // Contains owning pointers to all sessions that currently exist.
252 SessionIdMap all_sessions_
;
253 // Contains non-owning pointers to currently active session
254 // (not going away session, once they're implemented).
255 SessionMap active_sessions_
;
256 // Map from session to set of aliases that this session is known by.
257 SessionAliasMap session_aliases_
;
258 // Map from IP address to sessions which are connected to this address.
259 IPAliasMap ip_aliases_
;
261 // Origins which have gone away recently.
262 AliasSet gone_away_aliases_
;
264 const QuicConfig config_
;
265 QuicCryptoClientConfig crypto_config_
;
268 JobRequestsMap job_requests_map_
;
269 RequestMap active_requests_
;
271 QuicVersionVector supported_versions_
;
273 // Determine if we should consistently select a client UDP port. If false,
274 // then we will just let the OS select a random client port for each new
276 bool enable_port_selection_
;
278 // Set if we always require handshake confirmation. If true, this will
279 // introduce at least one RTT for the handshake before the client sends data.
280 bool always_require_handshake_confirmation_
;
282 // Set if we do not want connection pooling.
283 bool disable_connection_pooling_
;
285 // Each profile will (probably) have a unique port_seed_ value. This value is
286 // used to help seed a pseudo-random number generator (PortSuggester) so that
287 // we consistently (within this profile) suggest the same ephemeral port when
288 // we re-connect to any given server/port. The differences between profiles
289 // (probablistically) prevent two profiles from colliding in their ephemeral
293 // Local address of socket that was created in CreateSession.
294 IPEndPoint local_address_
;
295 bool check_persisted_supports_quic_
;
297 base::WeakPtrFactory
<QuicStreamFactory
> weak_factory_
;
299 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory
);
304 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_