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