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 #include "net/socket/client_socket_pool_manager_impl.h"
6 
7 #include <algorithm>
8 #include <utility>
9 
10 #include "base/check_op.h"
11 #include "base/values.h"
12 #include "net/base/proxy_server.h"
13 #include "net/http/http_network_session.h"
14 #include "net/socket/socks_connect_job.h"
15 #include "net/socket/ssl_connect_job.h"
16 #include "net/socket/transport_client_socket_pool.h"
17 #include "net/socket/transport_connect_job.h"
18 #include "net/socket/websocket_transport_client_socket_pool.h"
19 
20 namespace net {
21 
22 class SocketPerformanceWatcherFactory;
23 
ClientSocketPoolManagerImpl(const CommonConnectJobParams & common_connect_job_params,const CommonConnectJobParams & websocket_common_connect_job_params,HttpNetworkSession::SocketPoolType pool_type)24 ClientSocketPoolManagerImpl::ClientSocketPoolManagerImpl(
25     const CommonConnectJobParams& common_connect_job_params,
26     const CommonConnectJobParams& websocket_common_connect_job_params,
27     HttpNetworkSession::SocketPoolType pool_type)
28     : common_connect_job_params_(common_connect_job_params),
29       websocket_common_connect_job_params_(websocket_common_connect_job_params),
30       pool_type_(pool_type) {
31   // |websocket_endpoint_lock_manager| must only be set for websocket
32   // connections.
33   DCHECK(!common_connect_job_params_.websocket_endpoint_lock_manager);
34   DCHECK(websocket_common_connect_job_params.websocket_endpoint_lock_manager);
35 }
36 
~ClientSocketPoolManagerImpl()37 ClientSocketPoolManagerImpl::~ClientSocketPoolManagerImpl() {
38   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
39 }
40 
FlushSocketPoolsWithError(int net_error,const char * net_log_reason_utf8)41 void ClientSocketPoolManagerImpl::FlushSocketPoolsWithError(
42     int net_error,
43     const char* net_log_reason_utf8) {
44   for (const auto& it : socket_pools_) {
45     it.second->FlushWithError(net_error, net_log_reason_utf8);
46   }
47 }
48 
CloseIdleSockets(const char * net_log_reason_utf8)49 void ClientSocketPoolManagerImpl::CloseIdleSockets(
50     const char* net_log_reason_utf8) {
51   for (const auto& it : socket_pools_) {
52     it.second->CloseIdleSockets(net_log_reason_utf8);
53   }
54 }
55 
GetSocketPool(const ProxyServer & proxy_server)56 ClientSocketPool* ClientSocketPoolManagerImpl::GetSocketPool(
57     const ProxyServer& proxy_server) {
58   SocketPoolMap::const_iterator it = socket_pools_.find(proxy_server);
59   if (it != socket_pools_.end())
60     return it->second.get();
61 
62   int sockets_per_proxy_server;
63   int sockets_per_group;
64   if (proxy_server.is_direct()) {
65     sockets_per_proxy_server = max_sockets_per_pool(pool_type_);
66     sockets_per_group = max_sockets_per_group(pool_type_);
67   } else {
68     sockets_per_proxy_server = max_sockets_per_proxy_server(pool_type_);
69     sockets_per_group =
70         std::min(sockets_per_proxy_server, max_sockets_per_group(pool_type_));
71   }
72 
73   std::unique_ptr<ClientSocketPool> new_pool;
74 
75   // Use specialized WebSockets pool for WebSockets when no proxy is in use.
76   if (pool_type_ == HttpNetworkSession::WEBSOCKET_SOCKET_POOL &&
77       proxy_server.is_direct()) {
78     new_pool = std::make_unique<WebSocketTransportClientSocketPool>(
79         sockets_per_proxy_server, sockets_per_group, proxy_server,
80         &websocket_common_connect_job_params_);
81   } else {
82     new_pool = std::make_unique<TransportClientSocketPool>(
83         sockets_per_proxy_server, sockets_per_group,
84         unused_idle_socket_timeout(pool_type_), proxy_server,
85         pool_type_ == HttpNetworkSession::WEBSOCKET_SOCKET_POOL,
86         &common_connect_job_params_);
87   }
88 
89   std::pair<SocketPoolMap::iterator, bool> ret =
90       socket_pools_.insert(std::make_pair(proxy_server, std::move(new_pool)));
91   return ret.first->second.get();
92 }
93 
94 std::unique_ptr<base::Value>
SocketPoolInfoToValue() const95 ClientSocketPoolManagerImpl::SocketPoolInfoToValue() const {
96   std::unique_ptr<base::ListValue> list(new base::ListValue());
97   for (const auto& socket_pool : socket_pools_) {
98     // TODO(menke): Is this really needed?
99     const char* type;
100     if (socket_pool.first.is_direct()) {
101       type = "transport_socket_pool";
102     } else if (socket_pool.first.is_socks()) {
103       type = "socks_socket_pool";
104     } else {
105       type = "http_proxy_socket_pool";
106     }
107     list->Append(
108         socket_pool.second->GetInfoAsValue(socket_pool.first.ToURI(), type));
109   }
110 
111   return std::move(list);
112 }
113 
DumpMemoryStats(base::trace_event::ProcessMemoryDump * pmd,const std::string & parent_dump_absolute_name) const114 void ClientSocketPoolManagerImpl::DumpMemoryStats(
115     base::trace_event::ProcessMemoryDump* pmd,
116     const std::string& parent_dump_absolute_name) const {
117   SocketPoolMap::const_iterator socket_pool =
118       socket_pools_.find(ProxyServer::Direct());
119   if (socket_pool == socket_pools_.end())
120     return;
121   socket_pool->second->DumpMemoryStats(pmd, parent_dump_absolute_name);
122 }
123 
124 }  // namespace net
125