http_network_session.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_network_session.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <utility>
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/compiler_specific.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/debug/stack_trace.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/logging.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/stl_util.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/string_util.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/values.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_auth_handler_factory.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_response_body_drainer.h"
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_stream_factory_impl.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/url_security_manager.h"
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/proxy/proxy_service.h"
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/quic/crypto/quic_random.h"
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/quic/quic_clock.h"
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/quic/quic_crypto_client_stream_factory.h"
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/quic/quic_stream_factory.h"
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/socket/client_socket_factory.h"
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/socket/client_socket_pool_manager_impl.h"
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/socket/next_proto.h"
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/spdy/spdy_session_pool.h"
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
30
31net::ClientSocketPoolManager* CreateSocketPoolManager(
32    net::HttpNetworkSession::SocketPoolType pool_type,
33    const net::HttpNetworkSession::Params& params) {
34  // TODO(yutak): Differentiate WebSocket pool manager and allow more
35  // simultaneous connections for WebSockets.
36  return new net::ClientSocketPoolManagerImpl(
37      params.net_log,
38      params.client_socket_factory ?
39      params.client_socket_factory :
40      net::ClientSocketFactory::GetDefaultFactory(),
41      params.host_resolver,
42      params.cert_verifier,
43      params.server_bound_cert_service,
44      params.transport_security_state,
45      params.ssl_session_cache_shard,
46      params.proxy_service,
47      params.ssl_config_service,
48      pool_type);
49}
50
51}  // unnamed namespace
52
53namespace net {
54
55HttpNetworkSession::Params::Params()
56    : client_socket_factory(NULL),
57      host_resolver(NULL),
58      cert_verifier(NULL),
59      server_bound_cert_service(NULL),
60      transport_security_state(NULL),
61      proxy_service(NULL),
62      ssl_config_service(NULL),
63      http_auth_handler_factory(NULL),
64      network_delegate(NULL),
65      http_server_properties(NULL),
66      net_log(NULL),
67      host_mapping_rules(NULL),
68      force_http_pipelining(false),
69      ignore_certificate_errors(false),
70      http_pipelining_enabled(false),
71      testing_fixed_http_port(0),
72      testing_fixed_https_port(0),
73      max_spdy_sessions_per_domain(0),
74      force_spdy_single_domain(false),
75      enable_spdy_ip_pooling(true),
76      enable_spdy_credential_frames(false),
77      enable_spdy_compression(true),
78      enable_spdy_ping_based_connection_checking(true),
79      spdy_default_protocol(kProtoUnknown),
80      spdy_stream_initial_recv_window_size(0),
81      spdy_initial_max_concurrent_streams(0),
82      spdy_max_concurrent_streams_limit(0),
83      time_func(&base::TimeTicks::Now),
84      enable_quic(false),
85      quic_clock(NULL),
86      quic_random(NULL),
87      enable_user_alternate_protocol_ports(false),
88      quic_crypto_client_stream_factory(NULL) {
89}
90
91// TODO(mbelshe): Move the socket factories into HttpStreamFactory.
92HttpNetworkSession::HttpNetworkSession(const Params& params)
93    : net_log_(params.net_log),
94      network_delegate_(params.network_delegate),
95      http_server_properties_(params.http_server_properties),
96      cert_verifier_(params.cert_verifier),
97      http_auth_handler_factory_(params.http_auth_handler_factory),
98      force_http_pipelining_(params.force_http_pipelining),
99      proxy_service_(params.proxy_service),
100      ssl_config_service_(params.ssl_config_service),
101      normal_socket_pool_manager_(
102          CreateSocketPoolManager(NORMAL_SOCKET_POOL, params)),
103      websocket_socket_pool_manager_(
104          CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL, params)),
105      quic_stream_factory_(params.host_resolver,
106                           params.client_socket_factory ?
107                               params.client_socket_factory :
108                               net::ClientSocketFactory::GetDefaultFactory(),
109                           params.quic_crypto_client_stream_factory,
110                           params.quic_random ? params.quic_random :
111                               QuicRandom::GetInstance(),
112                           params.quic_clock ? params. quic_clock :
113                               new QuicClock()),
114      spdy_session_pool_(params.host_resolver,
115                         params.ssl_config_service,
116                         params.http_server_properties,
117                         params.max_spdy_sessions_per_domain,
118                         params.force_spdy_single_domain,
119                         params.enable_spdy_ip_pooling,
120                         params.enable_spdy_credential_frames,
121                         params.enable_spdy_compression,
122                         params.enable_spdy_ping_based_connection_checking,
123                         params.spdy_default_protocol,
124                         params.spdy_stream_initial_recv_window_size,
125                         params.spdy_initial_max_concurrent_streams,
126                         params.spdy_max_concurrent_streams_limit,
127                         params.time_func,
128                         params.trusted_spdy_proxy),
129      http_stream_factory_(new HttpStreamFactoryImpl(this)),
130      params_(params) {
131  DCHECK(proxy_service_);
132  DCHECK(ssl_config_service_.get());
133  CHECK(http_server_properties_);
134}
135
136HttpNetworkSession::~HttpNetworkSession() {
137  STLDeleteElements(&response_drainers_);
138  spdy_session_pool_.CloseAllSessions();
139}
140
141void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer* drainer) {
142  DCHECK(!ContainsKey(response_drainers_, drainer));
143  response_drainers_.insert(drainer);
144}
145
146void HttpNetworkSession::RemoveResponseDrainer(
147    HttpResponseBodyDrainer* drainer) {
148  DCHECK(ContainsKey(response_drainers_, drainer));
149  response_drainers_.erase(drainer);
150}
151
152TransportClientSocketPool* HttpNetworkSession::GetTransportSocketPool(
153    SocketPoolType pool_type) {
154  return GetSocketPoolManager(pool_type)->GetTransportSocketPool();
155}
156
157SSLClientSocketPool* HttpNetworkSession::GetSSLSocketPool(
158    SocketPoolType pool_type) {
159  return GetSocketPoolManager(pool_type)->GetSSLSocketPool();
160}
161
162SOCKSClientSocketPool* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
163    SocketPoolType pool_type,
164    const HostPortPair& socks_proxy) {
165  return GetSocketPoolManager(pool_type)->GetSocketPoolForSOCKSProxy(
166      socks_proxy);
167}
168
169HttpProxyClientSocketPool* HttpNetworkSession::GetSocketPoolForHTTPProxy(
170    SocketPoolType pool_type,
171    const HostPortPair& http_proxy) {
172  return GetSocketPoolManager(pool_type)->GetSocketPoolForHTTPProxy(http_proxy);
173}
174
175SSLClientSocketPool* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
176    SocketPoolType pool_type,
177    const HostPortPair& proxy_server) {
178  return GetSocketPoolManager(pool_type)->GetSocketPoolForSSLWithProxy(
179      proxy_server);
180}
181
182Value* HttpNetworkSession::SocketPoolInfoToValue() const {
183  // TODO(yutak): Should merge values from normal pools and WebSocket pools.
184  return normal_socket_pool_manager_->SocketPoolInfoToValue();
185}
186
187Value* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
188  return spdy_session_pool_.SpdySessionPoolInfoToValue();
189}
190
191Value* HttpNetworkSession::QuicInfoToValue() const {
192  base::DictionaryValue* dict = new base::DictionaryValue();
193  dict->Set("sessions", quic_stream_factory_.QuicStreamFactoryInfoToValue());
194  dict->SetBoolean("quic_enabled", params_.enable_quic);
195  dict->SetString("origin_to_force_quic_on",
196                  params_.origin_to_force_quic_on.ToString());
197  return dict;
198}
199
200void HttpNetworkSession::CloseAllConnections() {
201  normal_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
202  websocket_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
203  spdy_session_pool_.CloseCurrentSessions(ERR_ABORTED);
204  quic_stream_factory_.CloseAllSessions(ERR_ABORTED);
205}
206
207void HttpNetworkSession::CloseIdleConnections() {
208  normal_socket_pool_manager_->CloseIdleSockets();
209  websocket_socket_pool_manager_->CloseIdleSockets();
210  spdy_session_pool_.CloseIdleSessions();
211}
212
213ClientSocketPoolManager* HttpNetworkSession::GetSocketPoolManager(
214    SocketPoolType pool_type) {
215  switch (pool_type) {
216    case NORMAL_SOCKET_POOL:
217      return normal_socket_pool_manager_.get();
218    case WEBSOCKET_SOCKET_POOL:
219      return websocket_socket_pool_manager_.get();
220    default:
221      NOTREACHED();
222      break;
223  }
224  return NULL;
225}
226
227}  //  namespace net
228