Upstream version 7.36.149.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 <list>
9 #include <map>
10 #include <string>
11 #include <vector>
12
13 #include "base/logging.h"
14 #include "base/memory/weak_ptr.h"
15 #include "net/base/address_list.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/host_port_pair.h"
18 #include "net/base/net_log.h"
19 #include "net/base/network_change_notifier.h"
20 #include "net/cert/cert_database.h"
21 #include "net/proxy/proxy_server.h"
22 #include "net/quic/quic_config.h"
23 #include "net/quic/quic_crypto_stream.h"
24 #include "net/quic/quic_http_stream.h"
25 #include "net/quic/quic_protocol.h"
26
27 namespace net {
28
29 class CertVerifier;
30 class ClientSocketFactory;
31 class HostResolver;
32 class HttpServerProperties;
33 class QuicClock;
34 class QuicClientSession;
35 class QuicConnectionHelper;
36 class QuicCryptoClientStreamFactory;
37 class QuicRandom;
38 class QuicServerInfoFactory;
39 class QuicServerId;
40 class QuicStreamFactory;
41
42 namespace test {
43 class QuicStreamFactoryPeer;
44 }  // namespace test
45
46 // Encapsulates a pending request for a QuicHttpStream.
47 // If the request is still pending when it is destroyed, it will
48 // cancel the request with the factory.
49 class NET_EXPORT_PRIVATE QuicStreamRequest {
50  public:
51   explicit QuicStreamRequest(QuicStreamFactory* factory);
52   ~QuicStreamRequest();
53
54   // For http, |is_https| is false and |cert_verifier| can be null.
55   int Request(const HostPortPair& host_port_pair,
56               bool is_https,
57               PrivacyMode privacy_mode,
58               base::StringPiece method,
59               const BoundNetLog& net_log,
60               const CompletionCallback& callback);
61
62   void OnRequestComplete(int rv);
63
64   scoped_ptr<QuicHttpStream> ReleaseStream();
65
66   void set_stream(scoped_ptr<QuicHttpStream> stream);
67
68   const BoundNetLog& net_log() const{
69     return net_log_;
70   }
71
72  private:
73   QuicStreamFactory* factory_;
74   HostPortPair host_port_pair_;
75   bool is_https_;
76   BoundNetLog net_log_;
77   CompletionCallback callback_;
78   scoped_ptr<QuicHttpStream> stream_;
79
80   DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
81 };
82
83 // A factory for creating new QuicHttpStreams on top of a pool of
84 // QuicClientSessions.
85 class NET_EXPORT_PRIVATE QuicStreamFactory
86     : public NetworkChangeNotifier::IPAddressObserver,
87       public CertDatabase::Observer {
88  public:
89   QuicStreamFactory(
90       HostResolver* host_resolver,
91       ClientSocketFactory* client_socket_factory,
92       base::WeakPtr<HttpServerProperties> http_server_properties,
93       CertVerifier* cert_verifier,
94       QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
95       QuicRandom* random_generator,
96       QuicClock* clock,
97       size_t max_packet_length,
98       const QuicVersionVector& supported_versions,
99       bool enable_port_selection,
100       bool enable_pacing,
101       bool enable_time_based_loss_detection);
102   virtual ~QuicStreamFactory();
103
104   // Creates a new QuicHttpStream to |host_port_pair| which will be
105   // owned by |request|. |is_https| specifies if the protocol is https or not.
106   // |cert_verifier| is used by ProofVerifier for verifying the certificate
107   // chain and signature. For http, this can be null. If a matching session
108   // already exists, this method will return OK.  If no matching session exists,
109   // this will return ERR_IO_PENDING and will invoke OnRequestComplete
110   // asynchronously.
111   int Create(const HostPortPair& host_port_pair,
112              bool is_https,
113              PrivacyMode privacy_mode,
114              base::StringPiece method,
115              const BoundNetLog& net_log,
116              QuicStreamRequest* request);
117
118   // Called by a session when it becomes idle.
119   void OnIdleSession(QuicClientSession* session);
120
121   // Called by a session when it is going away and no more streams should be
122   // created on it.
123   void OnSessionGoingAway(QuicClientSession* session);
124
125   // Called by a session after it shuts down.
126   void OnSessionClosed(QuicClientSession* session);
127
128   // Cancels a pending request.
129   void CancelRequest(QuicStreamRequest* request);
130
131   // Closes all current sessions.
132   void CloseAllSessions(int error);
133
134   base::Value* QuicStreamFactoryInfoToValue() const;
135
136   // Delete all cached state objects in |crypto_config_|.
137   void ClearCachedStatesInCryptoConfig();
138
139   // NetworkChangeNotifier::IPAddressObserver methods:
140
141   // Until the servers support roaming, close all connections when the local
142   // IP address changes.
143   virtual void OnIPAddressChanged() OVERRIDE;
144
145   // CertDatabase::Observer methods:
146
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;
150
151   bool require_confirmation() const { return require_confirmation_; }
152
153   void set_require_confirmation(bool require_confirmation) {
154     require_confirmation_ = require_confirmation;
155   }
156
157   QuicConnectionHelper* helper() { return helper_.get(); }
158
159   bool enable_port_selection() const { return enable_port_selection_; }
160
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;
165   }
166
167   bool enable_pacing() const { return enable_pacing_; }
168
169  private:
170   class Job;
171   friend class test::QuicStreamFactoryPeer;
172
173   // The key used to find session by ip. Includes
174   // the ip address, port, and scheme.
175   struct NET_EXPORT_PRIVATE IpAliasKey {
176     IpAliasKey();
177     IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
178     ~IpAliasKey();
179
180     IPEndPoint ip_endpoint;
181     bool is_https;
182
183     // Needed to be an element of std::set.
184     bool operator<(const IpAliasKey &other) const;
185     bool operator==(const IpAliasKey &other) const;
186   };
187
188   typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
189   typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
190   typedef std::set<QuicServerId> AliasSet;
191   typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
192   typedef std::set<QuicClientSession*> SessionSet;
193   typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
194   typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
195   typedef std::map<QuicServerId, Job*> JobMap;
196   typedef std::map<QuicStreamRequest*, Job*> RequestMap;
197   typedef std::set<QuicStreamRequest*> RequestSet;
198   typedef std::map<Job*, RequestSet> JobRequestsMap;
199
200   // Returns a newly created QuicHttpStream owned by the caller, if a
201   // matching session already exists.  Returns NULL otherwise.
202   scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
203                                                    const BoundNetLog& net_log);
204
205   bool OnResolution(const QuicServerId& server_id,
206                     const AddressList& address_list);
207   void OnJobComplete(Job* job, int rv);
208   bool HasActiveSession(const QuicServerId& server_id) const;
209   bool HasActiveJob(const QuicServerId& server_id) const;
210   int CreateSession(const QuicServerId& server_id,
211                     scoped_ptr<QuicServerInfo> quic_server_info,
212                     const AddressList& address_list,
213                     const BoundNetLog& net_log,
214                     QuicClientSession** session);
215   void ActivateSession(const QuicServerId& key,
216                        QuicClientSession* session);
217
218   // Initializes the cached state associated with |server_id| in
219   // |crypto_config_| with the information in |server_info|.
220   void InitializeCachedStateInCryptoConfig(
221       const QuicServerId& server_id,
222       const scoped_ptr<QuicServerInfo>& server_info);
223
224   void ProcessGoingAwaySession(QuicClientSession* session,
225                                const QuicServerId& server_id);
226
227   bool require_confirmation_;
228   HostResolver* host_resolver_;
229   ClientSocketFactory* client_socket_factory_;
230   base::WeakPtr<HttpServerProperties> http_server_properties_;
231   CertVerifier* cert_verifier_;
232   QuicServerInfoFactory* quic_server_info_factory_;
233   QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
234   QuicRandom* random_generator_;
235   scoped_ptr<QuicClock> clock_;
236   const size_t max_packet_length_;
237
238   // The helper used for all connections.
239   scoped_ptr<QuicConnectionHelper> helper_;
240
241   // Contains owning pointers to all sessions that currently exist.
242   SessionIdMap all_sessions_;
243   // Contains non-owning pointers to currently active session
244   // (not going away session, once they're implemented).
245   SessionMap active_sessions_;
246   // Map from session to set of aliases that this session is known by.
247   SessionAliasMap session_aliases_;
248   // Map from IP address to sessions which are connected to this address.
249   IPAliasMap ip_aliases_;
250
251   // Origins which have gone away recently.
252   AliasSet gone_away_aliases_;
253
254   QuicConfig config_;
255   QuicCryptoClientConfig crypto_config_;
256
257   JobMap active_jobs_;
258   JobRequestsMap job_requests_map_;
259   RequestMap active_requests_;
260
261   QuicVersionVector supported_versions_;
262
263   // Determine if we should consistently select a client UDP port. If false,
264   // then we will just let the OS select a random client port for each new
265   // connection.
266   bool enable_port_selection_;
267
268   // True if packet pacing should be advertised during the crypto handshake.
269   bool enable_pacing_;
270
271   // Each profile will (probably) have a unique port_seed_ value.  This value is
272   // used to help seed a pseudo-random number generator (PortSuggester) so that
273   // we consistently (within this profile) suggest the same ephemeral port when
274   // we re-connect to any given server/port.  The differences between profiles
275   // (probablistically) prevent two profiles from colliding in their ephemeral
276   // port requests.
277   uint64 port_seed_;
278
279   base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
280
281   DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
282 };
283
284 }  // namespace net
285
286 #endif  // NET_QUIC_QUIC_STREAM_FACTORY_H_