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