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