OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef NET_QUIC_QUIC_STREAM_FACTORY_H_ | |
6 #define NET_QUIC_QUIC_STREAM_FACTORY_H_ | |
7 | |
8 #include <list> | |
9 #include <map> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
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" | |
26 | |
27 namespace net { | |
28 | |
29 class CertVerifier; | |
30 class ChannelIDService; | |
31 class ClientSocketFactory; | |
32 class HostResolver; | |
33 class HttpServerProperties; | |
34 class QuicClock; | |
35 class QuicClientSession; | |
36 class QuicConnectionHelper; | |
37 class QuicCryptoClientStreamFactory; | |
38 class QuicRandom; | |
39 class QuicServerInfoFactory; | |
40 class QuicServerId; | |
41 class QuicStreamFactory; | |
42 class TransportSecurityState; | |
43 | |
44 namespace test { | |
45 class QuicStreamFactoryPeer; | |
46 } // namespace test | |
47 | |
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 { | |
52 public: | |
53 explicit QuicStreamRequest(QuicStreamFactory* factory); | |
54 ~QuicStreamRequest(); | |
55 | |
56 // For http, |is_https| is false. | |
57 int Request(const HostPortPair& host_port_pair, | |
58 bool is_https, | |
59 PrivacyMode privacy_mode, | |
60 base::StringPiece method, | |
61 const BoundNetLog& net_log, | |
62 const CompletionCallback& callback); | |
63 | |
64 void OnRequestComplete(int rv); | |
65 | |
66 scoped_ptr<QuicHttpStream> ReleaseStream(); | |
67 | |
68 void set_stream(scoped_ptr<QuicHttpStream> stream); | |
69 | |
70 const BoundNetLog& net_log() const{ | |
71 return net_log_; | |
72 } | |
73 | |
74 private: | |
75 QuicStreamFactory* factory_; | |
76 HostPortPair host_port_pair_; | |
77 bool is_https_; | |
78 BoundNetLog net_log_; | |
79 CompletionCallback callback_; | |
80 scoped_ptr<QuicHttpStream> stream_; | |
81 | |
82 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest); | |
83 }; | |
84 | |
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 { | |
90 public: | |
91 QuicStreamFactory( | |
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, | |
100 QuicClock* clock, | |
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 int load_server_info_timeout, | |
108 float load_server_info_timeout_srtt_multiplier, | |
109 bool enable_truncated_connection_ids, | |
110 bool enable_connection_racing, | |
111 const QuicTagVector& connection_options); | |
112 ~QuicStreamFactory() override; | |
113 | |
114 // Creates a new QuicHttpStream to |host_port_pair| which will be | |
115 // owned by |request|. |is_https| specifies if the protocol is https or not. | |
116 // If a matching session already exists, this method will return OK. If no | |
117 // matching session exists, this will return ERR_IO_PENDING and will invoke | |
118 // OnRequestComplete asynchronously. | |
119 int Create(const HostPortPair& host_port_pair, | |
120 bool is_https, | |
121 PrivacyMode privacy_mode, | |
122 base::StringPiece method, | |
123 const BoundNetLog& net_log, | |
124 QuicStreamRequest* request); | |
125 | |
126 // Called by a session when it becomes idle. | |
127 void OnIdleSession(QuicClientSession* session); | |
128 | |
129 // Called by a session when it is going away and no more streams should be | |
130 // created on it. | |
131 void OnSessionGoingAway(QuicClientSession* session); | |
132 | |
133 // Called by a session after it shuts down. | |
134 void OnSessionClosed(QuicClientSession* session); | |
135 | |
136 // Called by a session whose connection has timed out. | |
137 void OnSessionConnectTimeout(QuicClientSession* session); | |
138 | |
139 // Cancels a pending request. | |
140 void CancelRequest(QuicStreamRequest* request); | |
141 | |
142 // Closes all current sessions. | |
143 void CloseAllSessions(int error); | |
144 | |
145 base::Value* QuicStreamFactoryInfoToValue() const; | |
146 | |
147 // Delete all cached state objects in |crypto_config_|. | |
148 void ClearCachedStatesInCryptoConfig(); | |
149 | |
150 // NetworkChangeNotifier::IPAddressObserver methods: | |
151 | |
152 // Until the servers support roaming, close all connections when the local | |
153 // IP address changes. | |
154 void OnIPAddressChanged() override; | |
155 | |
156 // CertDatabase::Observer methods: | |
157 | |
158 // We close all sessions when certificate database is changed. | |
159 void OnCertAdded(const X509Certificate* cert) override; | |
160 void OnCACertChanged(const X509Certificate* cert) override; | |
161 | |
162 bool require_confirmation() const { | |
163 return require_confirmation_; | |
164 } | |
165 | |
166 void set_require_confirmation(bool require_confirmation); | |
167 | |
168 QuicConnectionHelper* helper() { return helper_.get(); } | |
169 | |
170 bool enable_port_selection() const { return enable_port_selection_; } | |
171 | |
172 bool has_quic_server_info_factory() { | |
173 return quic_server_info_factory_ != NULL; | |
174 } | |
175 | |
176 void set_quic_server_info_factory( | |
177 QuicServerInfoFactory* quic_server_info_factory) { | |
178 DCHECK(!quic_server_info_factory_); | |
179 quic_server_info_factory_ = quic_server_info_factory; | |
180 } | |
181 | |
182 bool enable_connection_racing() const { return enable_connection_racing_; } | |
183 void set_enable_connection_racing(bool enable_connection_racing) { | |
184 enable_connection_racing_ = enable_connection_racing; | |
185 } | |
186 | |
187 private: | |
188 class Job; | |
189 friend class test::QuicStreamFactoryPeer; | |
190 | |
191 // The key used to find session by ip. Includes | |
192 // the ip address, port, and scheme. | |
193 struct NET_EXPORT_PRIVATE IpAliasKey { | |
194 IpAliasKey(); | |
195 IpAliasKey(IPEndPoint ip_endpoint, bool is_https); | |
196 ~IpAliasKey(); | |
197 | |
198 IPEndPoint ip_endpoint; | |
199 bool is_https; | |
200 | |
201 // Needed to be an element of std::set. | |
202 bool operator<(const IpAliasKey &other) const; | |
203 bool operator==(const IpAliasKey &other) const; | |
204 }; | |
205 | |
206 typedef std::map<QuicServerId, QuicClientSession*> SessionMap; | |
207 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap; | |
208 typedef std::set<QuicServerId> AliasSet; | |
209 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap; | |
210 typedef std::set<QuicClientSession*> SessionSet; | |
211 typedef std::map<IpAliasKey, SessionSet> IPAliasMap; | |
212 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap; | |
213 typedef std::set<Job*> JobSet; | |
214 typedef std::map<QuicServerId, JobSet> JobMap; | |
215 typedef std::map<QuicStreamRequest*, QuicServerId> RequestMap; | |
216 typedef std::set<QuicStreamRequest*> RequestSet; | |
217 typedef std::map<QuicServerId, RequestSet> ServerIDRequestsMap; | |
218 | |
219 // Creates a job which doesn't wait for server config to be loaded from the | |
220 // disk cache. This job is started via a PostTask. | |
221 void CreateAuxilaryJob(const QuicServerId server_id, | |
222 bool is_post, | |
223 const BoundNetLog& net_log); | |
224 | |
225 // Returns a newly created QuicHttpStream owned by the caller, if a | |
226 // matching session already exists. Returns NULL otherwise. | |
227 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key, | |
228 const BoundNetLog& net_log); | |
229 | |
230 bool OnResolution(const QuicServerId& server_id, | |
231 const AddressList& address_list); | |
232 void OnJobComplete(Job* job, int rv); | |
233 bool HasActiveSession(const QuicServerId& server_id) const; | |
234 bool HasActiveJob(const QuicServerId& server_id) const; | |
235 int CreateSession(const QuicServerId& server_id, | |
236 scoped_ptr<QuicServerInfo> quic_server_info, | |
237 const AddressList& address_list, | |
238 const BoundNetLog& net_log, | |
239 QuicClientSession** session); | |
240 void ActivateSession(const QuicServerId& key, | |
241 QuicClientSession* session); | |
242 | |
243 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there | |
244 // is no |http_server_properties_| or if |http_server_properties_| doesn't | |
245 // have ServerNetworkStats for the given |server_id|. | |
246 int64 GetServerNetworkStatsSmoothedRttInMicroseconds( | |
247 const QuicServerId& server_id) const; | |
248 | |
249 // Helped methods. | |
250 bool WasAlternateProtocolRecentlyBroken(const QuicServerId& server_id) const; | |
251 bool CryptoConfigCacheIsEmpty(const QuicServerId& server_id); | |
252 | |
253 // Initializes the cached state associated with |server_id| in | |
254 // |crypto_config_| with the information in |server_info|. | |
255 void InitializeCachedStateInCryptoConfig( | |
256 const QuicServerId& server_id, | |
257 const scoped_ptr<QuicServerInfo>& server_info); | |
258 | |
259 void ProcessGoingAwaySession(QuicClientSession* session, | |
260 const QuicServerId& server_id, | |
261 bool was_session_active); | |
262 | |
263 bool require_confirmation_; | |
264 HostResolver* host_resolver_; | |
265 ClientSocketFactory* client_socket_factory_; | |
266 base::WeakPtr<HttpServerProperties> http_server_properties_; | |
267 TransportSecurityState* transport_security_state_; | |
268 QuicServerInfoFactory* quic_server_info_factory_; | |
269 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_; | |
270 QuicRandom* random_generator_; | |
271 scoped_ptr<QuicClock> clock_; | |
272 const size_t max_packet_length_; | |
273 | |
274 // The helper used for all connections. | |
275 scoped_ptr<QuicConnectionHelper> helper_; | |
276 | |
277 // Contains owning pointers to all sessions that currently exist. | |
278 SessionIdMap all_sessions_; | |
279 // Contains non-owning pointers to currently active session | |
280 // (not going away session, once they're implemented). | |
281 SessionMap active_sessions_; | |
282 // Map from session to set of aliases that this session is known by. | |
283 SessionAliasMap session_aliases_; | |
284 // Map from IP address to sessions which are connected to this address. | |
285 IPAliasMap ip_aliases_; | |
286 | |
287 // Origins which have gone away recently. | |
288 AliasSet gone_away_aliases_; | |
289 | |
290 const QuicConfig config_; | |
291 QuicCryptoClientConfig crypto_config_; | |
292 | |
293 JobMap active_jobs_; | |
294 ServerIDRequestsMap job_requests_map_; | |
295 RequestMap active_requests_; | |
296 | |
297 QuicVersionVector supported_versions_; | |
298 | |
299 // Determine if we should consistently select a client UDP port. If false, | |
300 // then we will just let the OS select a random client port for each new | |
301 // connection. | |
302 bool enable_port_selection_; | |
303 | |
304 // Set if we always require handshake confirmation. If true, this will | |
305 // introduce at least one RTT for the handshake before the client sends data. | |
306 bool always_require_handshake_confirmation_; | |
307 | |
308 // Set if we do not want connection pooling. | |
309 bool disable_connection_pooling_; | |
310 | |
311 // Specifies the timeout in milliseconds to wait for loading of QUIC server | |
312 // information. If we don't want to timeout, set | |
313 // |load_server_info_timeout_ms_| to 0. | |
314 int load_server_info_timeout_ms_; | |
315 | |
316 // Specifies the ratio between time to load QUIC server information from disk | |
317 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in | |
318 // milliseconds to wait for loading of QUIC server information. If we don't | |
319 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0. | |
320 float load_server_info_timeout_srtt_multiplier_; | |
321 | |
322 // Set this for setting config's BytesForConnectionIdToSend (TCID param) to 0. | |
323 bool enable_truncated_connection_ids_; | |
324 | |
325 // Set if we want to race connections - one connection that sends | |
326 // INCHOATE_HELLO and another connection that sends CHLO after loading server | |
327 // config from the disk cache. | |
328 bool enable_connection_racing_; | |
329 | |
330 // Each profile will (probably) have a unique port_seed_ value. This value is | |
331 // used to help seed a pseudo-random number generator (PortSuggester) so that | |
332 // we consistently (within this profile) suggest the same ephemeral port when | |
333 // we re-connect to any given server/port. The differences between profiles | |
334 // (probablistically) prevent two profiles from colliding in their ephemeral | |
335 // port requests. | |
336 uint64 port_seed_; | |
337 | |
338 // Local address of socket that was created in CreateSession. | |
339 IPEndPoint local_address_; | |
340 bool check_persisted_supports_quic_; | |
341 std::set<HostPortPair> quic_supported_servers_at_startup_; | |
342 | |
343 base::TaskRunner* task_runner_; | |
344 | |
345 base::WeakPtrFactory<QuicStreamFactory> weak_factory_; | |
346 | |
347 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory); | |
348 }; | |
349 | |
350 } // namespace net | |
351 | |
352 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_ | |
OLD | NEW |