[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 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 <map> |
[email protected] | 41d6b17 | 2013-01-29 16:10:57 | [diff] [blame] | 9 | #include <string> |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 10 | #include <vector> |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 11 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
| 13 | #include "net/base/address_list.h" |
| 14 | #include "net/base/completion_callback.h" |
| 15 | #include "net/base/host_port_pair.h" |
| 16 | #include "net/base/net_log.h" |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 17 | #include "net/base/network_change_notifier.h" |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 18 | #include "net/cert/cert_database.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 19 | #include "net/proxy/proxy_server.h" |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 20 | #include "net/quic/quic_config.h" |
| 21 | #include "net/quic/quic_crypto_stream.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 22 | #include "net/quic/quic_http_stream.h" |
| 23 | #include "net/quic/quic_protocol.h" |
| 24 | |
| 25 | namespace net { |
| 26 | |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 27 | class CertVerifier; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 28 | class ClientSocketFactory; |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 29 | class HostResolver; |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 30 | class HttpServerProperties; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 31 | class QuicClock; |
| 32 | class QuicClientSession; |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 33 | class QuicConnectionHelper; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 34 | class QuicCryptoClientStreamFactory; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 35 | class QuicRandom; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 36 | class QuicStreamFactory; |
| 37 | |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 38 | namespace test { |
| 39 | class QuicStreamFactoryPeer; |
| 40 | } // namespace test |
| 41 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 42 | // Encapsulates a pending request for a QuicHttpStream. |
| 43 | // If the request is still pending when it is destroyed, it will |
| 44 | // cancel the request with the factory. |
| 45 | class NET_EXPORT_PRIVATE QuicStreamRequest { |
| 46 | public: |
| 47 | explicit QuicStreamRequest(QuicStreamFactory* factory); |
| 48 | ~QuicStreamRequest(); |
| 49 | |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 50 | // For http, |is_https| is false and |cert_verifier| can be null. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 51 | int Request(const HostPortProxyPair& host_port_proxy_pair, |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 52 | bool is_https, |
| 53 | CertVerifier* cert_verifier, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 54 | const BoundNetLog& net_log, |
| 55 | const CompletionCallback& callback); |
| 56 | |
| 57 | void OnRequestComplete(int rv); |
| 58 | |
| 59 | scoped_ptr<QuicHttpStream> ReleaseStream(); |
| 60 | |
| 61 | void set_stream(scoped_ptr<QuicHttpStream> stream); |
| 62 | |
| 63 | const BoundNetLog& net_log() const{ |
| 64 | return net_log_; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | QuicStreamFactory* factory_; |
| 69 | HostPortProxyPair host_port_proxy_pair_; |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 70 | bool is_https_; |
| 71 | CertVerifier* cert_verifier_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 72 | BoundNetLog net_log_; |
| 73 | CompletionCallback callback_; |
| 74 | scoped_ptr<QuicHttpStream> stream_; |
| 75 | |
| 76 | DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest); |
| 77 | }; |
| 78 | |
| 79 | // A factory for creating new QuicHttpStreams on top of a pool of |
| 80 | // QuicClientSessions. |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 81 | class NET_EXPORT_PRIVATE QuicStreamFactory |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 82 | : public NetworkChangeNotifier::IPAddressObserver, |
| 83 | public CertDatabase::Observer { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 84 | public: |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 85 | QuicStreamFactory( |
| 86 | HostResolver* host_resolver, |
| 87 | ClientSocketFactory* client_socket_factory, |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 88 | base::WeakPtr<HttpServerProperties> http_server_properties, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 89 | QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory, |
| 90 | QuicRandom* random_generator, |
[email protected] | 256fe9b | 2013-11-27 01:58:02 | [diff] [blame] | 91 | QuicClock* clock, |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 92 | size_t max_packet_length, |
| 93 | const QuicVersionVector& supported_versions); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 94 | virtual ~QuicStreamFactory(); |
| 95 | |
| 96 | // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 97 | // owned by |request|. |is_https| specifies if the protocol is https or not. |
| 98 | // |cert_verifier| is used by ProofVerifier for verifying the certificate |
| 99 | // chain and signature. For http, this can be null. If a matching session |
| 100 | // already exists, this method will return OK. If no matching session exists, |
| 101 | // this will return ERR_IO_PENDING and will invoke OnRequestComplete |
| 102 | // asynchronously. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 103 | int Create(const HostPortProxyPair& host_port_proxy_pair, |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 104 | bool is_https, |
| 105 | CertVerifier* cert_verifier, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 106 | const BoundNetLog& net_log, |
| 107 | QuicStreamRequest* request); |
| 108 | |
| 109 | // Returns a newly created QuicHttpStream owned by the caller, if a |
| 110 | // matching session already exists. Returns NULL otherwise. |
| 111 | scoped_ptr<QuicHttpStream> CreateIfSessionExists( |
| 112 | const HostPortProxyPair& host_port_proxy_pair, |
| 113 | const BoundNetLog& net_log); |
| 114 | |
| 115 | // Called by a session when it becomes idle. |
| 116 | void OnIdleSession(QuicClientSession* session); |
| 117 | |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 118 | // Called by a session when it is going away and no more streams should be |
| 119 | // created on it. |
| 120 | void OnSessionGoingAway(QuicClientSession* session); |
| 121 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 122 | // Called by a session after it shuts down. |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 123 | void OnSessionClosed(QuicClientSession* session); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 124 | |
| 125 | // Cancels a pending request. |
| 126 | void CancelRequest(QuicStreamRequest* request); |
| 127 | |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 128 | // Closes all current sessions. |
| 129 | void CloseAllSessions(int error); |
| 130 | |
[email protected] | c5b061b | 2013-01-05 00:31:34 | [diff] [blame] | 131 | base::Value* QuicStreamFactoryInfoToValue() const; |
| 132 | |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 133 | // NetworkChangeNotifier::IPAddressObserver methods: |
| 134 | |
| 135 | // Until the servers support roaming, close all connections when the local |
| 136 | // IP address changes. |
| 137 | virtual void OnIPAddressChanged() OVERRIDE; |
| 138 | |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 139 | // CertDatabase::Observer methods: |
| 140 | |
| 141 | // We close all sessions when certificate database is changed. |
| 142 | virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE; |
| 143 | virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE; |
| 144 | |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 145 | bool require_confirmation() const { return require_confirmation_; } |
| 146 | |
| 147 | void set_require_confirmation(bool require_confirmation) { |
| 148 | require_confirmation_ = require_confirmation; |
| 149 | } |
| 150 | |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 151 | QuicConnectionHelper* helper() { return helper_.get(); } |
| 152 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 153 | private: |
| 154 | class Job; |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 155 | friend class test::QuicStreamFactoryPeer; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 156 | |
| 157 | typedef std::map<HostPortProxyPair, QuicClientSession*> SessionMap; |
| 158 | typedef std::set<HostPortProxyPair> AliasSet; |
| 159 | typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap; |
| 160 | typedef std::set<QuicClientSession*> SessionSet; |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame^] | 161 | typedef std::map<IPEndPoint, SessionSet> IPAliasMap; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 162 | typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap; |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 163 | typedef std::map<HostPortPair, HostPortProxyPair> CanonicalHostMap; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 164 | typedef std::map<HostPortProxyPair, Job*> JobMap; |
| 165 | typedef std::map<QuicStreamRequest*, Job*> RequestMap; |
| 166 | typedef std::set<QuicStreamRequest*> RequestSet; |
| 167 | typedef std::map<Job*, RequestSet> JobRequestsMap; |
| 168 | |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame^] | 169 | bool OnResolution(const HostPortProxyPair& host_port_proxy_pair, |
| 170 | const AddressList& address_list); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 171 | void OnJobComplete(Job* job, int rv); |
| 172 | bool HasActiveSession(const HostPortProxyPair& host_port_proxy_pair); |
| 173 | bool HasActiveJob(const HostPortProxyPair& host_port_proxy_pair); |
[email protected] | 338e798 | 2013-12-13 11:15:32 | [diff] [blame] | 174 | int CreateSession(const HostPortProxyPair& host_port_proxy_pair, |
| 175 | bool is_https, |
| 176 | CertVerifier* cert_verifier, |
| 177 | const AddressList& address_list, |
| 178 | const BoundNetLog& net_log, |
| 179 | QuicClientSession** session); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 180 | void ActivateSession(const HostPortProxyPair& host_port_proxy_pair, |
| 181 | QuicClientSession* session); |
| 182 | |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 183 | QuicCryptoClientConfig* GetOrCreateCryptoConfig( |
| 184 | const HostPortProxyPair& host_port_proxy_pair); |
| 185 | |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 186 | // If |host_port_proxy_pair| suffix contains ".c.youtube.com" (in future we |
| 187 | // could support other suffixes), then populate |crypto_config| with a |
| 188 | // canonical server config data from |canonical_hostname_to_origin_map_| for |
| 189 | // that suffix. |
| 190 | void PopulateFromCanonicalConfig( |
| 191 | const HostPortProxyPair& host_port_proxy_pair, |
| 192 | QuicCryptoClientConfig* crypto_config); |
| 193 | |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 194 | bool require_confirmation_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 195 | HostResolver* host_resolver_; |
| 196 | ClientSocketFactory* client_socket_factory_; |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 197 | base::WeakPtr<HttpServerProperties> http_server_properties_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 198 | QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 199 | QuicRandom* random_generator_; |
[email protected] | f1e97e9 | 2012-12-16 04:53:25 | [diff] [blame] | 200 | scoped_ptr<QuicClock> clock_; |
[email protected] | 256fe9b | 2013-11-27 01:58:02 | [diff] [blame] | 201 | const size_t max_packet_length_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 202 | |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 203 | // The helper used for all connections. |
| 204 | scoped_ptr<QuicConnectionHelper> helper_; |
| 205 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 206 | // Contains owning pointers to all sessions that currently exist. |
| 207 | SessionSet all_sessions_; |
| 208 | // Contains non-owning pointers to currently active session |
| 209 | // (not going away session, once they're implemented). |
| 210 | SessionMap active_sessions_; |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame^] | 211 | // Map from session to set of aliases that this session is known by. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 212 | SessionAliasMap session_aliases_; |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame^] | 213 | // Map from IP address to sessions which are connected to this address. |
| 214 | IPAliasMap ip_aliases_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 215 | |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 216 | // Contains owning pointers to QuicCryptoClientConfig. QuicCryptoClientConfig |
| 217 | // contains configuration and cached state about servers. |
| 218 | // TODO(rtenneti): Persist all_crypto_configs_ to disk and decide when to |
| 219 | // clear the data in the map. |
| 220 | CryptoConfigMap all_crypto_configs_; |
| 221 | |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 222 | // Contains a map of servers which could share the same server config. Map |
| 223 | // from a Canonical host/port (host is some postfix of host names) to an |
| 224 | // actual origin, which has a plausible set of initial certificates (or at |
| 225 | // least server public key). |
| 226 | CanonicalHostMap canonical_hostname_to_origin_map_; |
| 227 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 228 | // Contains list of suffixes (for exmaple ".c.youtube.com", |
| 229 | // ".googlevideo.com") of cannoncial hostnames. |
| 230 | std::vector<std::string> cannoncial_suffixes_; |
| 231 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 232 | QuicConfig config_; |
| 233 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 234 | JobMap active_jobs_; |
| 235 | JobRequestsMap job_requests_map_; |
| 236 | RequestMap active_requests_; |
| 237 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 238 | QuicVersionVector supported_versions_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 239 | |
[email protected] | 337e145 | 2013-12-16 23:57:50 | [diff] [blame] | 240 | // Each profile will (probably) have a unique port_seed_ value. This value is |
| 241 | // used to help seed a pseudo-random number generator (PortSuggester) so that |
| 242 | // we consistently (within this profile) suggest the same ephemeral port when |
| 243 | // we re-connect to any given server/port. The differences between profiles |
| 244 | // (probablistically) prevent two profiles from colliding in their ephemeral |
| 245 | // port requests. |
| 246 | uint64 port_seed_; |
[email protected] | 7034cf1 | 2013-12-13 22:47:07 | [diff] [blame] | 247 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 248 | base::WeakPtrFactory<QuicStreamFactory> weak_factory_; |
| 249 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 250 | DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory); |
| 251 | }; |
| 252 | |
| 253 | } // namespace net |
| 254 | |
| 255 | #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_ |