blob: a9be577b5e53740eb958146edfad75b10de362f3 [file] [log] [blame]
[email protected]e13201d82012-12-12 05:00:321// 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]41d6b172013-01-29 16:10:579#include <string>
[email protected]6e12d702013-11-13 00:17:1710#include <vector>
[email protected]e13201d82012-12-12 05:00:3211
[email protected]e13201d82012-12-12 05:00:3212#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]f698a012013-05-06 20:18:5917#include "net/base/network_change_notifier.h"
[email protected]e13201d82012-12-12 05:00:3218#include "net/proxy/proxy_server.h"
[email protected]ef95114d2013-04-17 17:57:0119#include "net/quic/quic_config.h"
20#include "net/quic/quic_crypto_stream.h"
[email protected]e13201d82012-12-12 05:00:3221#include "net/quic/quic_http_stream.h"
22#include "net/quic/quic_protocol.h"
23
24namespace net {
25
[email protected]6d1b4ed2013-07-10 03:57:5426class CertVerifier;
[email protected]e13201d82012-12-12 05:00:3227class ClientSocketFactory;
[email protected]6d1b4ed2013-07-10 03:57:5428class HostResolver;
[email protected]77c6c162013-08-17 02:57:4529class HttpServerProperties;
[email protected]e13201d82012-12-12 05:00:3230class QuicClock;
31class QuicClientSession;
[email protected]2cfc6bb82013-10-27 03:40:4432class QuicConnectionHelper;
[email protected]e8ff26842013-03-22 21:02:0533class QuicCryptoClientStreamFactory;
[email protected]9558c5d32012-12-22 00:08:1434class QuicRandom;
[email protected]e13201d82012-12-12 05:00:3235class QuicStreamFactory;
36
[email protected]c49ff182013-09-28 08:33:2637namespace test {
38class QuicStreamFactoryPeer;
39} // namespace test
40
[email protected]e13201d82012-12-12 05:00:3241// 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.
44class NET_EXPORT_PRIVATE QuicStreamRequest {
45 public:
46 explicit QuicStreamRequest(QuicStreamFactory* factory);
47 ~QuicStreamRequest();
48
[email protected]6d1b4ed2013-07-10 03:57:5449 // For http, |is_https| is false and |cert_verifier| can be null.
[email protected]e13201d82012-12-12 05:00:3250 int Request(const HostPortProxyPair& host_port_proxy_pair,
[email protected]6d1b4ed2013-07-10 03:57:5451 bool is_https,
52 CertVerifier* cert_verifier,
[email protected]e13201d82012-12-12 05:00:3253 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]6d1b4ed2013-07-10 03:57:5469 bool is_https_;
70 CertVerifier* cert_verifier_;
[email protected]e13201d82012-12-12 05:00:3271 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]f698a012013-05-06 20:18:5980class NET_EXPORT_PRIVATE QuicStreamFactory
81 : public NetworkChangeNotifier::IPAddressObserver {
[email protected]e13201d82012-12-12 05:00:3282 public:
[email protected]e8ff26842013-03-22 21:02:0583 QuicStreamFactory(
84 HostResolver* host_resolver,
85 ClientSocketFactory* client_socket_factory,
[email protected]77c6c162013-08-17 02:57:4586 base::WeakPtr<HttpServerProperties> http_server_properties,
[email protected]e8ff26842013-03-22 21:02:0587 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
88 QuicRandom* random_generator,
89 QuicClock* clock);
[email protected]e13201d82012-12-12 05:00:3290 virtual ~QuicStreamFactory();
91
92 // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:5493 // 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]e13201d82012-12-12 05:00:3299 int Create(const HostPortProxyPair& host_port_proxy_pair,
[email protected]6d1b4ed2013-07-10 03:57:54100 bool is_https,
101 CertVerifier* cert_verifier,
[email protected]e13201d82012-12-12 05:00:32102 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]4d283b32013-10-17 12:57:27114 // 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]e13201d82012-12-12 05:00:32118 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27119 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32120
121 // Cancels a pending request.
122 void CancelRequest(QuicStreamRequest* request);
123
[email protected]56dfb902013-01-03 23:17:55124 // Closes all current sessions.
125 void CloseAllSessions(int error);
126
[email protected]c5b061b2013-01-05 00:31:34127 base::Value* QuicStreamFactoryInfoToValue() const;
128
[email protected]f698a012013-05-06 20:18:59129 // 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]11c05872013-08-20 02:04:12135 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]2cfc6bb82013-10-27 03:40:44141 QuicConnectionHelper* helper() { return helper_.get(); }
142
[email protected]e13201d82012-12-12 05:00:32143 private:
144 class Job;
[email protected]c49ff182013-09-28 08:33:26145 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32146
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]ef95114d2013-04-17 17:57:01151 typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap;
[email protected]c49ff182013-09-28 08:33:26152 typedef std::map<HostPortPair, HostPortProxyPair> CanonicalHostMap;
[email protected]e13201d82012-12-12 05:00:32153 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]ef95114d2013-04-17 17:57:01161 QuicClientSession* CreateSession(
162 const HostPortProxyPair& host_port_proxy_pair,
[email protected]6d1b4ed2013-07-10 03:57:54163 bool is_https,
164 CertVerifier* cert_verifier,
[email protected]ef95114d2013-04-17 17:57:01165 const AddressList& address_list,
166 const BoundNetLog& net_log);
[email protected]e13201d82012-12-12 05:00:32167 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair,
168 QuicClientSession* session);
169
[email protected]ef95114d2013-04-17 17:57:01170 QuicCryptoClientConfig* GetOrCreateCryptoConfig(
171 const HostPortProxyPair& host_port_proxy_pair);
172
[email protected]c49ff182013-09-28 08:33:26173 // 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]11c05872013-08-20 02:04:12181 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32182 HostResolver* host_resolver_;
183 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45184 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]e8ff26842013-03-22 21:02:05185 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14186 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25187 scoped_ptr<QuicClock> clock_;
[email protected]e13201d82012-12-12 05:00:32188
[email protected]2cfc6bb82013-10-27 03:40:44189 // The helper used for all connections.
190 scoped_ptr<QuicConnectionHelper> helper_;
191
[email protected]e13201d82012-12-12 05:00:32192 // 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]ef95114d2013-04-17 17:57:01199 // 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]c49ff182013-09-28 08:33:26205 // 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]6e12d702013-11-13 00:17:17211 // 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]b064310782013-05-30 21:12:17215 QuicConfig config_;
216
[email protected]e13201d82012-12-12 05:00:32217 JobMap active_jobs_;
218 JobRequestsMap job_requests_map_;
219 RequestMap active_requests_;
220
[email protected]e13201d82012-12-12 05:00:32221 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_