rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / net / quic / quic_stream_factory.h
blob33c9898102c4be2e57f7ddfb3ad03b1e028a5dce
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 <map>
9 #include <string>
10 #include <vector>
12 #include "base/logging.h"
13 #include "base/memory/weak_ptr.h"
14 #include "net/base/address_list.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/host_port_pair.h"
17 #include "net/base/net_log.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/cert/cert_database.h"
20 #include "net/proxy/proxy_server.h"
21 #include "net/quic/quic_config.h"
22 #include "net/quic/quic_crypto_stream.h"
23 #include "net/quic/quic_http_stream.h"
24 #include "net/quic/quic_protocol.h"
26 namespace net {
28 class CertVerifier;
29 class ClientSocketFactory;
30 class HostResolver;
31 class HttpServerProperties;
32 class QuicClock;
33 class QuicClientSession;
34 class QuicConnectionHelper;
35 class QuicCryptoClientStreamFactory;
36 class QuicRandom;
37 class QuicServerInfoFactory;
38 class QuicStreamFactory;
40 namespace test {
41 class QuicStreamFactoryPeer;
42 } // namespace test
44 // Encapsulates a pending request for a QuicHttpStream.
45 // If the request is still pending when it is destroyed, it will
46 // cancel the request with the factory.
47 class NET_EXPORT_PRIVATE QuicStreamRequest {
48 public:
49 explicit QuicStreamRequest(QuicStreamFactory* factory);
50 ~QuicStreamRequest();
52 // For http, |is_https| is false and |cert_verifier| can be null.
53 int Request(const HostPortProxyPair& host_port_proxy_pair,
54 bool is_https,
55 base::StringPiece method,
56 CertVerifier* cert_verifier,
57 const BoundNetLog& net_log,
58 const CompletionCallback& callback);
60 void OnRequestComplete(int rv);
62 scoped_ptr<QuicHttpStream> ReleaseStream();
64 void set_stream(scoped_ptr<QuicHttpStream> stream);
66 const BoundNetLog& net_log() const{
67 return net_log_;
70 private:
71 QuicStreamFactory* factory_;
72 HostPortProxyPair host_port_proxy_pair_;
73 bool is_https_;
74 CertVerifier* cert_verifier_;
75 BoundNetLog net_log_;
76 CompletionCallback callback_;
77 scoped_ptr<QuicHttpStream> stream_;
79 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
82 // A factory for creating new QuicHttpStreams on top of a pool of
83 // QuicClientSessions.
84 class NET_EXPORT_PRIVATE QuicStreamFactory
85 : public NetworkChangeNotifier::IPAddressObserver,
86 public CertDatabase::Observer {
87 public:
88 QuicStreamFactory(
89 HostResolver* host_resolver,
90 ClientSocketFactory* client_socket_factory,
91 base::WeakPtr<HttpServerProperties> http_server_properties,
92 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
93 QuicRandom* random_generator,
94 QuicClock* clock,
95 size_t max_packet_length,
96 const QuicVersionVector& supported_versions,
97 bool enable_port_selection,
98 bool enable_pacing);
99 virtual ~QuicStreamFactory();
101 // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be
102 // owned by |request|. |is_https| specifies if the protocol is https or not.
103 // |cert_verifier| is used by ProofVerifier for verifying the certificate
104 // chain and signature. For http, this can be null. If a matching session
105 // already exists, this method will return OK. If no matching session exists,
106 // this will return ERR_IO_PENDING and will invoke OnRequestComplete
107 // asynchronously.
108 int Create(const HostPortProxyPair& host_port_proxy_pair,
109 bool is_https,
110 base::StringPiece method,
111 CertVerifier* cert_verifier,
112 const BoundNetLog& net_log,
113 QuicStreamRequest* request);
115 // Returns a newly created QuicHttpStream owned by the caller, if a
116 // matching session already exists. Returns NULL otherwise.
117 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
118 const HostPortProxyPair& host_port_proxy_pair,
119 const BoundNetLog& net_log);
121 // Called by a session when it becomes idle.
122 void OnIdleSession(QuicClientSession* session);
124 // Called by a session when it is going away and no more streams should be
125 // created on it.
126 void OnSessionGoingAway(QuicClientSession* session);
128 // Called by a session after it shuts down.
129 void OnSessionClosed(QuicClientSession* session);
131 // Cancels a pending request.
132 void CancelRequest(QuicStreamRequest* request);
134 // Closes all current sessions.
135 void CloseAllSessions(int error);
137 base::Value* QuicStreamFactoryInfoToValue() const;
139 // NetworkChangeNotifier::IPAddressObserver methods:
141 // Until the servers support roaming, close all connections when the local
142 // IP address changes.
143 virtual void OnIPAddressChanged() OVERRIDE;
145 // CertDatabase::Observer methods:
147 // We close all sessions when certificate database is changed.
148 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE;
149 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE;
151 bool require_confirmation() const { return require_confirmation_; }
153 void set_require_confirmation(bool require_confirmation) {
154 require_confirmation_ = require_confirmation;
157 QuicConnectionHelper* helper() { return helper_.get(); }
159 bool enable_port_selection() const { return enable_port_selection_; }
161 void set_quic_server_info_factory(
162 QuicServerInfoFactory* quic_server_info_factory) {
163 DCHECK(!quic_server_info_factory_);
164 quic_server_info_factory_ = quic_server_info_factory;
167 bool enable_pacing() const { return enable_pacing_; }
169 private:
170 class Job;
171 friend class test::QuicStreamFactoryPeer;
173 typedef std::map<HostPortProxyPair, QuicClientSession*> SessionMap;
174 typedef std::set<HostPortProxyPair> AliasSet;
175 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
176 typedef std::set<QuicClientSession*> SessionSet;
177 typedef std::map<IPEndPoint, SessionSet> IPAliasMap;
178 typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap;
179 typedef std::map<HostPortPair, HostPortProxyPair> CanonicalHostMap;
180 typedef std::map<HostPortProxyPair, Job*> JobMap;
181 typedef std::map<QuicStreamRequest*, Job*> RequestMap;
182 typedef std::set<QuicStreamRequest*> RequestSet;
183 typedef std::map<Job*, RequestSet> JobRequestsMap;
185 bool OnResolution(const HostPortProxyPair& host_port_proxy_pair,
186 const AddressList& address_list);
187 void OnJobComplete(Job* job, int rv);
188 bool HasActiveSession(const HostPortProxyPair& host_port_proxy_pair);
189 bool HasActiveJob(const HostPortProxyPair& host_port_proxy_pair);
190 int CreateSession(const HostPortProxyPair& host_port_proxy_pair,
191 bool is_https,
192 CertVerifier* cert_verifier,
193 const AddressList& address_list,
194 const BoundNetLog& net_log,
195 QuicClientSession** session);
196 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair,
197 QuicClientSession* session);
199 QuicCryptoClientConfig* GetOrCreateCryptoConfig(
200 const HostPortProxyPair& host_port_proxy_pair);
202 // If |host_port_proxy_pair| suffix contains ".c.youtube.com" (in future we
203 // could support other suffixes), then populate |crypto_config| with a
204 // canonical server config data from |canonical_hostname_to_origin_map_| for
205 // that suffix.
206 void PopulateFromCanonicalConfig(
207 const HostPortProxyPair& host_port_proxy_pair,
208 QuicCryptoClientConfig* crypto_config);
210 bool require_confirmation_;
211 HostResolver* host_resolver_;
212 ClientSocketFactory* client_socket_factory_;
213 base::WeakPtr<HttpServerProperties> http_server_properties_;
214 QuicServerInfoFactory* quic_server_info_factory_;
215 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
216 QuicRandom* random_generator_;
217 scoped_ptr<QuicClock> clock_;
218 const size_t max_packet_length_;
220 // The helper used for all connections.
221 scoped_ptr<QuicConnectionHelper> helper_;
223 // Contains owning pointers to all sessions that currently exist.
224 SessionSet all_sessions_;
225 // Contains non-owning pointers to currently active session
226 // (not going away session, once they're implemented).
227 SessionMap active_sessions_;
228 // Map from session to set of aliases that this session is known by.
229 SessionAliasMap session_aliases_;
230 // Map from IP address to sessions which are connected to this address.
231 IPAliasMap ip_aliases_;
233 // Contains owning pointers to QuicCryptoClientConfig. QuicCryptoClientConfig
234 // contains configuration and cached state about servers.
235 // TODO(rtenneti): Persist all_crypto_configs_ to disk and decide when to
236 // clear the data in the map.
237 CryptoConfigMap all_crypto_configs_;
239 // Contains a map of servers which could share the same server config. Map
240 // from a Canonical host/port (host is some postfix of host names) to an
241 // actual origin, which has a plausible set of initial certificates (or at
242 // least server public key).
243 CanonicalHostMap canonical_hostname_to_origin_map_;
245 // Contains list of suffixes (for exmaple ".c.youtube.com",
246 // ".googlevideo.com") of canoncial hostnames.
247 std::vector<std::string> canoncial_suffixes_;
249 QuicConfig config_;
251 JobMap active_jobs_;
252 JobRequestsMap job_requests_map_;
253 RequestMap active_requests_;
255 QuicVersionVector supported_versions_;
257 // Determine if we should consistently select a client UDP port. If false,
258 // then we will just let the OS select a random client port for each new
259 // connection.
260 bool enable_port_selection_;
262 // True if packet pacing should be advertised during the crypto handshake.
263 bool enable_pacing_;
265 // Each profile will (probably) have a unique port_seed_ value. This value is
266 // used to help seed a pseudo-random number generator (PortSuggester) so that
267 // we consistently (within this profile) suggest the same ephemeral port when
268 // we re-connect to any given server/port. The differences between profiles
269 // (probablistically) prevent two profiles from colliding in their ephemeral
270 // port requests.
271 uint64 port_seed_;
273 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
275 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
278 } // namespace net
280 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_