Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_stream_factory.h
blobdd0f5f9963326764dc0ae0c13b13f785691323d9
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/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"
27 namespace net {
29 class CertVerifier;
30 class ClientSocketFactory;
31 class HostResolver;
32 class HttpServerProperties;
33 class QuicClock;
34 class QuicClientSession;
35 class QuicConnectionHelper;
36 class QuicCryptoClientStreamFactory;
37 class QuicRandom;
38 class QuicServerInfoFactory;
39 class QuicServerId;
40 class QuicStreamFactory;
41 class ServerBoundCertService;
43 namespace test {
44 class QuicStreamFactoryPeer;
45 } // namespace test
47 // Encapsulates a pending request for a QuicHttpStream.
48 // If the request is still pending when it is destroyed, it will
49 // cancel the request with the factory.
50 class NET_EXPORT_PRIVATE QuicStreamRequest {
51 public:
52 explicit QuicStreamRequest(QuicStreamFactory* factory);
53 ~QuicStreamRequest();
55 // For http, |is_https| is false.
56 int Request(const HostPortPair& host_port_pair,
57 bool is_https,
58 PrivacyMode privacy_mode,
59 base::StringPiece method,
60 const BoundNetLog& net_log,
61 const CompletionCallback& callback);
63 void OnRequestComplete(int rv);
65 scoped_ptr<QuicHttpStream> ReleaseStream();
67 void set_stream(scoped_ptr<QuicHttpStream> stream);
69 const BoundNetLog& net_log() const{
70 return net_log_;
73 private:
74 QuicStreamFactory* factory_;
75 HostPortPair host_port_pair_;
76 bool is_https_;
77 BoundNetLog net_log_;
78 CompletionCallback callback_;
79 scoped_ptr<QuicHttpStream> stream_;
81 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
84 // A factory for creating new QuicHttpStreams on top of a pool of
85 // QuicClientSessions.
86 class NET_EXPORT_PRIVATE QuicStreamFactory
87 : public NetworkChangeNotifier::IPAddressObserver,
88 public CertDatabase::Observer {
89 public:
90 QuicStreamFactory(
91 HostResolver* host_resolver,
92 ClientSocketFactory* client_socket_factory,
93 base::WeakPtr<HttpServerProperties> http_server_properties,
94 CertVerifier* cert_verifier,
95 ServerBoundCertService* server_bound_cert_service,
96 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
97 QuicRandom* random_generator,
98 QuicClock* clock,
99 size_t max_packet_length,
100 const std::string& user_agent_id,
101 const QuicVersionVector& supported_versions,
102 bool enable_port_selection,
103 bool enable_pacing,
104 bool enable_time_based_loss_detection,
105 const QuicTagVector& connection_options);
106 virtual ~QuicStreamFactory();
108 // Creates a new QuicHttpStream to |host_port_pair| which will be
109 // owned by |request|. |is_https| specifies if the protocol is https or not.
110 // If a matching session already exists, this method will return OK. If no
111 // matching session exists, this will return ERR_IO_PENDING and will invoke
112 // OnRequestComplete asynchronously.
113 int Create(const HostPortPair& host_port_pair,
114 bool is_https,
115 PrivacyMode privacy_mode,
116 base::StringPiece method,
117 const BoundNetLog& net_log,
118 QuicStreamRequest* request);
120 // Called by a session when it becomes idle.
121 void OnIdleSession(QuicClientSession* session);
123 // Called by a session when it is going away and no more streams should be
124 // created on it.
125 void OnSessionGoingAway(QuicClientSession* session);
127 // Called by a session after it shuts down.
128 void OnSessionClosed(QuicClientSession* session);
130 // Called by a session whose connection has timed out.
131 void OnSessionConnectTimeout(QuicClientSession* session);
133 // Cancels a pending request.
134 void CancelRequest(QuicStreamRequest* request);
136 // Closes all current sessions.
137 void CloseAllSessions(int error);
139 base::Value* QuicStreamFactoryInfoToValue() const;
141 // Delete all cached state objects in |crypto_config_|.
142 void ClearCachedStatesInCryptoConfig();
144 // NetworkChangeNotifier::IPAddressObserver methods:
146 // Until the servers support roaming, close all connections when the local
147 // IP address changes.
148 virtual void OnIPAddressChanged() OVERRIDE;
150 // CertDatabase::Observer methods:
152 // We close all sessions when certificate database is changed.
153 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
154 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
156 bool require_confirmation() const { return require_confirmation_; }
158 void set_require_confirmation(bool require_confirmation) {
159 require_confirmation_ = require_confirmation;
162 QuicConnectionHelper* helper() { return helper_.get(); }
164 bool enable_port_selection() const { return enable_port_selection_; }
166 bool has_quic_server_info_factory() {
167 return quic_server_info_factory_ != NULL;
170 void set_quic_server_info_factory(
171 QuicServerInfoFactory* quic_server_info_factory) {
172 DCHECK(!quic_server_info_factory_);
173 quic_server_info_factory_ = quic_server_info_factory;
176 private:
177 class Job;
178 friend class test::QuicStreamFactoryPeer;
180 // The key used to find session by ip. Includes
181 // the ip address, port, and scheme.
182 struct NET_EXPORT_PRIVATE IpAliasKey {
183 IpAliasKey();
184 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
185 ~IpAliasKey();
187 IPEndPoint ip_endpoint;
188 bool is_https;
190 // Needed to be an element of std::set.
191 bool operator<(const IpAliasKey &other) const;
192 bool operator==(const IpAliasKey &other) const;
195 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
196 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
197 typedef std::set<QuicServerId> AliasSet;
198 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
199 typedef std::set<QuicClientSession*> SessionSet;
200 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
201 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
202 typedef std::map<QuicServerId, Job*> JobMap;
203 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
204 typedef std::set<QuicStreamRequest*> RequestSet;
205 typedef std::map<Job*, RequestSet> JobRequestsMap;
207 // Returns a newly created QuicHttpStream owned by the caller, if a
208 // matching session already exists. Returns NULL otherwise.
209 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
210 const BoundNetLog& net_log);
212 bool OnResolution(const QuicServerId& server_id,
213 const AddressList& address_list);
214 void OnJobComplete(Job* job, int rv);
215 bool HasActiveSession(const QuicServerId& server_id) const;
216 bool HasActiveJob(const QuicServerId& server_id) const;
217 int CreateSession(const QuicServerId& server_id,
218 scoped_ptr<QuicServerInfo> quic_server_info,
219 const AddressList& address_list,
220 const BoundNetLog& net_log,
221 QuicClientSession** session);
222 void ActivateSession(const QuicServerId& key,
223 QuicClientSession* session);
225 // Initializes the cached state associated with |server_id| in
226 // |crypto_config_| with the information in |server_info|.
227 void InitializeCachedStateInCryptoConfig(
228 const QuicServerId& server_id,
229 const scoped_ptr<QuicServerInfo>& server_info);
231 void ProcessGoingAwaySession(QuicClientSession* session,
232 const QuicServerId& server_id,
233 bool was_session_active);
235 bool require_confirmation_;
236 HostResolver* host_resolver_;
237 ClientSocketFactory* client_socket_factory_;
238 base::WeakPtr<HttpServerProperties> http_server_properties_;
239 QuicServerInfoFactory* quic_server_info_factory_;
240 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
241 QuicRandom* random_generator_;
242 scoped_ptr<QuicClock> clock_;
243 const size_t max_packet_length_;
245 // The helper used for all connections.
246 scoped_ptr<QuicConnectionHelper> helper_;
248 // Contains owning pointers to all sessions that currently exist.
249 SessionIdMap all_sessions_;
250 // Contains non-owning pointers to currently active session
251 // (not going away session, once they're implemented).
252 SessionMap active_sessions_;
253 // Map from session to set of aliases that this session is known by.
254 SessionAliasMap session_aliases_;
255 // Map from IP address to sessions which are connected to this address.
256 IPAliasMap ip_aliases_;
258 // Origins which have gone away recently.
259 AliasSet gone_away_aliases_;
261 const QuicConfig config_;
262 QuicCryptoClientConfig crypto_config_;
264 JobMap active_jobs_;
265 JobRequestsMap job_requests_map_;
266 RequestMap active_requests_;
268 QuicVersionVector supported_versions_;
270 // Determine if we should consistently select a client UDP port. If false,
271 // then we will just let the OS select a random client port for each new
272 // connection.
273 bool enable_port_selection_;
275 // Each profile will (probably) have a unique port_seed_ value. This value is
276 // used to help seed a pseudo-random number generator (PortSuggester) so that
277 // we consistently (within this profile) suggest the same ephemeral port when
278 // we re-connect to any given server/port. The differences between profiles
279 // (probablistically) prevent two profiles from colliding in their ephemeral
280 // port requests.
281 uint64 port_seed_;
283 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
285 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
288 } // namespace net
290 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_