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 ClientSocketFactory
;
32 class HttpServerProperties
;
34 class QuicClientSession
;
35 class QuicConnectionHelper
;
36 class QuicCryptoClientStreamFactory
;
38 class QuicServerInfoFactory
;
40 class QuicStreamFactory
;
43 class QuicStreamFactoryPeer
;
46 // Encapsulates a pending request for a QuicHttpStream.
47 // If the request is still pending when it is destroyed, it will
48 // cancel the request with the factory.
49 class NET_EXPORT_PRIVATE QuicStreamRequest
{
51 explicit QuicStreamRequest(QuicStreamFactory
* factory
);
54 // For http, |is_https| is false and |cert_verifier| can be null.
55 int Request(const HostPortPair
& host_port_pair
,
57 PrivacyMode privacy_mode
,
58 base::StringPiece method
,
59 const BoundNetLog
& net_log
,
60 const CompletionCallback
& callback
);
62 void OnRequestComplete(int rv
);
64 scoped_ptr
<QuicHttpStream
> ReleaseStream();
66 void set_stream(scoped_ptr
<QuicHttpStream
> stream
);
68 const BoundNetLog
& net_log() const{
73 QuicStreamFactory
* factory_
;
74 HostPortPair host_port_pair_
;
77 CompletionCallback callback_
;
78 scoped_ptr
<QuicHttpStream
> stream_
;
80 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest
);
83 // A factory for creating new QuicHttpStreams on top of a pool of
84 // QuicClientSessions.
85 class NET_EXPORT_PRIVATE QuicStreamFactory
86 : public NetworkChangeNotifier::IPAddressObserver
,
87 public CertDatabase::Observer
{
90 HostResolver
* host_resolver
,
91 ClientSocketFactory
* client_socket_factory
,
92 base::WeakPtr
<HttpServerProperties
> http_server_properties
,
93 CertVerifier
* cert_verifier
,
94 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory
,
95 QuicRandom
* random_generator
,
97 size_t max_packet_length
,
98 const QuicVersionVector
& supported_versions
,
99 bool enable_port_selection
,
101 virtual ~QuicStreamFactory();
103 // Creates a new QuicHttpStream to |host_port_pair| which will be
104 // owned by |request|. |is_https| specifies if the protocol is https or not.
105 // |cert_verifier| is used by ProofVerifier for verifying the certificate
106 // chain and signature. For http, this can be null. If a matching session
107 // already exists, this method will return OK. If no matching session exists,
108 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
110 int Create(const HostPortPair
& host_port_pair
,
112 PrivacyMode privacy_mode
,
113 base::StringPiece method
,
114 const BoundNetLog
& net_log
,
115 QuicStreamRequest
* request
);
117 // Called by a session when it becomes idle.
118 void OnIdleSession(QuicClientSession
* session
);
120 // Called by a session when it is going away and no more streams should be
122 void OnSessionGoingAway(QuicClientSession
* session
);
124 // Called by a session after it shuts down.
125 void OnSessionClosed(QuicClientSession
* session
);
127 // Cancels a pending request.
128 void CancelRequest(QuicStreamRequest
* request
);
130 // Closes all current sessions.
131 void CloseAllSessions(int error
);
133 base::Value
* QuicStreamFactoryInfoToValue() const;
135 // NetworkChangeNotifier::IPAddressObserver methods:
137 // Until the servers support roaming, close all connections when the local
138 // IP address changes.
139 virtual void OnIPAddressChanged() OVERRIDE
;
141 // CertDatabase::Observer methods:
143 // We close all sessions when certificate database is changed.
144 virtual void OnCertAdded(const X509Certificate
* cert
) OVERRIDE
;
145 virtual void OnCACertChanged(const X509Certificate
* cert
) OVERRIDE
;
147 bool require_confirmation() const { return require_confirmation_
; }
149 void set_require_confirmation(bool require_confirmation
) {
150 require_confirmation_
= require_confirmation
;
153 QuicConnectionHelper
* helper() { return helper_
.get(); }
155 bool enable_port_selection() const { return enable_port_selection_
; }
157 void set_quic_server_info_factory(
158 QuicServerInfoFactory
* quic_server_info_factory
) {
159 DCHECK(!quic_server_info_factory_
);
160 quic_server_info_factory_
= quic_server_info_factory
;
163 bool enable_pacing() const { return enable_pacing_
; }
167 friend class test::QuicStreamFactoryPeer
;
169 // The key used to find session by ip. Includes
170 // the ip address, port, and scheme.
171 struct NET_EXPORT_PRIVATE IpAliasKey
{
173 IpAliasKey(IPEndPoint ip_endpoint
, bool is_https
);
176 IPEndPoint ip_endpoint
;
179 // Needed to be an element of std::set.
180 bool operator<(const IpAliasKey
&other
) const;
181 bool operator==(const IpAliasKey
&other
) const;
184 typedef std::map
<QuicSessionKey
, QuicClientSession
*> SessionMap
;
185 typedef std::set
<QuicSessionKey
> AliasSet
;
186 typedef std::map
<QuicClientSession
*, AliasSet
> SessionAliasMap
;
187 typedef std::set
<QuicClientSession
*> SessionSet
;
188 typedef std::map
<IpAliasKey
, SessionSet
> IPAliasMap
;
189 typedef std::map
<QuicSessionKey
, QuicCryptoClientConfig
*> CryptoConfigMap
;
190 typedef std::map
<QuicSessionKey
, Job
*> JobMap
;
191 typedef std::map
<QuicStreamRequest
*, Job
*> RequestMap
;
192 typedef std::set
<QuicStreamRequest
*> RequestSet
;
193 typedef std::map
<Job
*, RequestSet
> JobRequestsMap
;
195 // Returns a newly created QuicHttpStream owned by the caller, if a
196 // matching session already exists. Returns NULL otherwise.
197 scoped_ptr
<QuicHttpStream
> CreateIfSessionExists(const QuicSessionKey
& key
,
198 const BoundNetLog
& net_log
);
200 bool OnResolution(const QuicSessionKey
& session_key
,
201 const AddressList
& address_list
);
202 void OnJobComplete(Job
* job
, int rv
);
203 bool HasActiveSession(const QuicSessionKey
& session_key
) const;
204 bool HasActiveJob(const QuicSessionKey
& session_key
) const;
205 int CreateSession(const QuicSessionKey
& session_key
,
206 scoped_ptr
<QuicServerInfo
> quic_server_info
,
207 const AddressList
& address_list
,
208 const BoundNetLog
& net_log
,
209 QuicClientSession
** session
);
210 void ActivateSession(const QuicSessionKey
& key
,
211 QuicClientSession
* session
);
213 // Initializes the cached state associated with |session_key| in
214 // |crypto_config_| with the information in |server_info|.
215 void InitializeCachedState(const QuicSessionKey
& session_key
,
216 const scoped_ptr
<QuicServerInfo
>& server_info
);
218 void ExpireBrokenAlternateProtocolMappings();
219 void ScheduleBrokenAlternateProtocolMappingsExpiration();
221 bool require_confirmation_
;
222 HostResolver
* host_resolver_
;
223 ClientSocketFactory
* client_socket_factory_
;
224 base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
225 CertVerifier
* cert_verifier_
;
226 QuicServerInfoFactory
* quic_server_info_factory_
;
227 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory_
;
228 QuicRandom
* random_generator_
;
229 scoped_ptr
<QuicClock
> clock_
;
230 const size_t max_packet_length_
;
232 // The helper used for all connections.
233 scoped_ptr
<QuicConnectionHelper
> helper_
;
235 // Contains owning pointers to all sessions that currently exist.
236 SessionSet all_sessions_
;
237 // Contains non-owning pointers to currently active session
238 // (not going away session, once they're implemented).
239 SessionMap active_sessions_
;
240 // Map from session to set of aliases that this session is known by.
241 SessionAliasMap session_aliases_
;
242 // Map from IP address to sessions which are connected to this address.
243 IPAliasMap ip_aliases_
;
245 // Origins which have gone away recently.
246 AliasSet gone_away_aliases_
;
248 // List of broken host:ports and the times when they can be expired.
249 struct BrokenAlternateProtocolEntry
{
251 base::TimeTicks when
;
253 typedef std::list
<BrokenAlternateProtocolEntry
>
254 BrokenAlternateProtocolList
;
255 BrokenAlternateProtocolList broken_alternate_protocol_list_
;
257 // Map from host:port to the number of times alternate protocol has
258 // been marked broken.
259 typedef std::map
<HostPortPair
, int> BrokenAlternateProtocolMap
;
260 BrokenAlternateProtocolMap broken_alternate_protocol_map_
;
263 QuicCryptoClientConfig crypto_config_
;
266 JobRequestsMap job_requests_map_
;
267 RequestMap active_requests_
;
269 QuicVersionVector supported_versions_
;
271 // Determine if we should consistently select a client UDP port. If false,
272 // then we will just let the OS select a random client port for each new
274 bool enable_port_selection_
;
276 // True if packet pacing should be advertised during the crypto handshake.
279 // Each profile will (probably) have a unique port_seed_ value. This value is
280 // used to help seed a pseudo-random number generator (PortSuggester) so that
281 // we consistently (within this profile) suggest the same ephemeral port when
282 // we re-connect to any given server/port. The differences between profiles
283 // (probablistically) prevent two profiles from colliding in their ephemeral
287 base::WeakPtrFactory
<QuicStreamFactory
> weak_factory_
;
289 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory
);
294 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_