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_HTTP_HTTP_NETWORK_SESSION_H_
6#define NET_HTTP_HTTP_NETWORK_SESSION_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/weak_ptr.h"
15#include "base/threading/non_thread_safe.h"
16#include "net/base/host_port_pair.h"
17#include "net/base/net_export.h"
18#include "net/dns/host_resolver.h"
19#include "net/http/http_auth_cache.h"
20#include "net/http/http_stream_factory.h"
21#include "net/quic/quic_stream_factory.h"
22#include "net/socket/next_proto.h"
23#include "net/spdy/spdy_session_pool.h"
24#include "net/ssl/ssl_client_auth_cache.h"
25
26namespace base {
27class Value;
28}
29
30namespace net {
31
32class CertVerifier;
33class ChannelIDService;
34class ClientSocketFactory;
35class ClientSocketPoolManager;
36class CTVerifier;
37class HostResolver;
38class HpackHuffmanAggregator;
39class HttpAuthHandlerFactory;
40class HttpNetworkSessionPeer;
41class HttpProxyClientSocketPool;
42class HttpResponseBodyDrainer;
43class HttpServerProperties;
44class NetLog;
45class NetworkDelegate;
46class ProxyDelegate;
47class ProxyService;
48class QuicClock;
49class QuicCryptoClientStreamFactory;
50class QuicServerInfoFactory;
51class SOCKSClientSocketPool;
52class SSLClientSocketPool;
53class SSLConfigService;
54class TransportClientSocketPool;
55class TransportSecurityState;
56
57// This class holds session objects used by HttpNetworkTransaction objects.
58class NET_EXPORT HttpNetworkSession
59    : public base::RefCounted<HttpNetworkSession>,
60      NON_EXPORTED_BASE(public base::NonThreadSafe) {
61 public:
62  struct NET_EXPORT Params {
63    Params();
64    ~Params();
65
66    ClientSocketFactory* client_socket_factory;
67    HostResolver* host_resolver;
68    CertVerifier* cert_verifier;
69    ChannelIDService* channel_id_service;
70    TransportSecurityState* transport_security_state;
71    CTVerifier* cert_transparency_verifier;
72    ProxyService* proxy_service;
73    std::string ssl_session_cache_shard;
74    SSLConfigService* ssl_config_service;
75    HttpAuthHandlerFactory* http_auth_handler_factory;
76    NetworkDelegate* network_delegate;
77    base::WeakPtr<HttpServerProperties> http_server_properties;
78    NetLog* net_log;
79    HostMappingRules* host_mapping_rules;
80    bool enable_ssl_connect_job_waiting;
81    bool ignore_certificate_errors;
82    uint16 testing_fixed_http_port;
83    uint16 testing_fixed_https_port;
84    bool enable_tcp_fast_open_for_ssl;
85
86    bool force_spdy_single_domain;
87    bool enable_spdy_compression;
88    bool enable_spdy_ping_based_connection_checking;
89    NextProto spdy_default_protocol;
90    // The protocols supported by NPN (next protocol negotiation) during the
91    // SSL handshake as well as by HTTP Alternate-Protocol.
92    // TODO(mmenke):  This is currently empty by default, and alternate
93    //                protocols are disabled.  We should use some reasonable
94    //                defaults.
95    NextProtoVector next_protos;
96    size_t spdy_stream_initial_recv_window_size;
97    size_t spdy_initial_max_concurrent_streams;
98    size_t spdy_max_concurrent_streams_limit;
99    SpdySessionPool::TimeFunc time_func;
100    std::string trusted_spdy_proxy;
101    // Controls whether or not ssl is used when in SPDY mode.
102    bool force_spdy_over_ssl;
103    // Controls whether or not SPDY is used without NPN.
104    bool force_spdy_always;
105    // URLs to exclude from forced SPDY.
106    std::set<HostPortPair> forced_spdy_exclusions;
107    // Noe: Using this in the case of NPN for HTTP only results in the browser
108    // trying SSL and then falling back to http.
109    bool use_alternate_protocols;
110    double alternate_protocol_probability_threshold;
111    bool enable_websocket_over_spdy;
112
113    bool enable_quic;
114    bool enable_quic_port_selection;
115    bool enable_quic_time_based_loss_detection;
116    bool quic_always_require_handshake_confirmation;
117    bool quic_disable_connection_pooling;
118    HostPortPair origin_to_force_quic_on;
119    QuicClock* quic_clock;  // Will be owned by QuicStreamFactory.
120    QuicRandom* quic_random;
121    size_t quic_max_packet_length;
122    std::string quic_user_agent_id;
123    bool enable_user_alternate_protocol_ports;
124    QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory;
125    QuicVersionVector quic_supported_versions;
126    QuicTagVector quic_connection_options;
127    ProxyDelegate* proxy_delegate;
128  };
129
130  enum SocketPoolType {
131    NORMAL_SOCKET_POOL,
132    WEBSOCKET_SOCKET_POOL,
133    NUM_SOCKET_POOL_TYPES
134  };
135
136  explicit HttpNetworkSession(const Params& params);
137
138  HttpAuthCache* http_auth_cache() { return &http_auth_cache_; }
139  SSLClientAuthCache* ssl_client_auth_cache() {
140    return &ssl_client_auth_cache_;
141  }
142
143  void AddResponseDrainer(HttpResponseBodyDrainer* drainer);
144
145  void RemoveResponseDrainer(HttpResponseBodyDrainer* drainer);
146
147  TransportClientSocketPool* GetTransportSocketPool(SocketPoolType pool_type);
148  SSLClientSocketPool* GetSSLSocketPool(SocketPoolType pool_type);
149  SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy(
150      SocketPoolType pool_type,
151      const HostPortPair& socks_proxy);
152  HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy(
153      SocketPoolType pool_type,
154      const HostPortPair& http_proxy);
155  SSLClientSocketPool* GetSocketPoolForSSLWithProxy(
156      SocketPoolType pool_type,
157      const HostPortPair& proxy_server);
158
159  CertVerifier* cert_verifier() { return cert_verifier_; }
160  ProxyService* proxy_service() { return proxy_service_; }
161  SSLConfigService* ssl_config_service() { return ssl_config_service_.get(); }
162  SpdySessionPool* spdy_session_pool() { return &spdy_session_pool_; }
163  QuicStreamFactory* quic_stream_factory() { return &quic_stream_factory_; }
164  HttpAuthHandlerFactory* http_auth_handler_factory() {
165    return http_auth_handler_factory_;
166  }
167  NetworkDelegate* network_delegate() {
168    return network_delegate_;
169  }
170  base::WeakPtr<HttpServerProperties> http_server_properties() {
171    return http_server_properties_;
172  }
173  HttpStreamFactory* http_stream_factory() {
174    return http_stream_factory_.get();
175  }
176  HttpStreamFactory* http_stream_factory_for_websocket() {
177    return http_stream_factory_for_websocket_.get();
178  }
179  NetLog* net_log() {
180    return net_log_;
181  }
182  HpackHuffmanAggregator* huffman_aggregator() {
183    return huffman_aggregator_.get();
184  }
185
186  // Creates a Value summary of the state of the socket pools. The caller is
187  // responsible for deleting the returned value.
188  base::Value* SocketPoolInfoToValue() const;
189
190  // Creates a Value summary of the state of the SPDY sessions. The caller is
191  // responsible for deleting the returned value.
192  base::Value* SpdySessionPoolInfoToValue() const;
193
194  // Creates a Value summary of the state of the QUIC sessions and
195  // configuration. The caller is responsible for deleting the returned value.
196  base::Value* QuicInfoToValue() const;
197
198  void CloseAllConnections();
199  void CloseIdleConnections();
200
201  // Returns the original Params used to construct this session.
202  const Params& params() const { return params_; }
203
204  bool IsProtocolEnabled(AlternateProtocol protocol) const;
205
206  void GetNextProtos(std::vector<std::string>* next_protos) const;
207
208  // Convenience function for searching through |params_| for
209  // |forced_spdy_exclusions|.
210  bool HasSpdyExclusion(HostPortPair host_port_pair) const;
211
212 private:
213  friend class base::RefCounted<HttpNetworkSession>;
214  friend class HttpNetworkSessionPeer;
215
216  ~HttpNetworkSession();
217
218  ClientSocketPoolManager* GetSocketPoolManager(SocketPoolType pool_type);
219
220  NetLog* const net_log_;
221  NetworkDelegate* const network_delegate_;
222  const base::WeakPtr<HttpServerProperties> http_server_properties_;
223  CertVerifier* const cert_verifier_;
224  HttpAuthHandlerFactory* const http_auth_handler_factory_;
225
226  // Not const since it's modified by HttpNetworkSessionPeer for testing.
227  ProxyService* proxy_service_;
228  const scoped_refptr<SSLConfigService> ssl_config_service_;
229
230  HttpAuthCache http_auth_cache_;
231  SSLClientAuthCache ssl_client_auth_cache_;
232  scoped_ptr<ClientSocketPoolManager> normal_socket_pool_manager_;
233  scoped_ptr<ClientSocketPoolManager> websocket_socket_pool_manager_;
234  QuicStreamFactory quic_stream_factory_;
235  SpdySessionPool spdy_session_pool_;
236  scoped_ptr<HttpStreamFactory> http_stream_factory_;
237  scoped_ptr<HttpStreamFactory> http_stream_factory_for_websocket_;
238  std::set<HttpResponseBodyDrainer*> response_drainers_;
239
240  // TODO(jgraettinger): Remove when Huffman collection is complete.
241  scoped_ptr<HpackHuffmanAggregator> huffman_aggregator_;
242
243  std::vector<std::string> next_protos_;
244  bool enabled_protocols_[NUM_VALID_ALTERNATE_PROTOCOLS];
245
246  Params params_;
247};
248
249}  // namespace net
250
251#endif  // NET_HTTP_HTTP_NETWORK_SESSION_H_
252