Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / net / socket / websocket_transport_client_socket_pool.h
blob21fe8dd58771c6e6531752655736f03f08601d2f
1 // Copyright 2014 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.
5 #ifndef NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_
6 #define NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <string>
13 #include "base/basictypes.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
19 #include "net/base/net_export.h"
20 #include "net/log/net_log.h"
21 #include "net/socket/client_socket_pool.h"
22 #include "net/socket/client_socket_pool_base.h"
23 #include "net/socket/transport_client_socket_pool.h"
25 namespace net {
27 class ClientSocketFactory;
28 class HostResolver;
29 class NetLog;
30 class WebSocketEndpointLockManager;
31 class WebSocketTransportConnectSubJob;
33 // WebSocketTransportConnectJob handles the host resolution necessary for socket
34 // creation and the TCP connect. WebSocketTransportConnectJob also has fallback
35 // logic for IPv6 connect() timeouts (which may happen due to networks / routers
36 // with broken IPv6 support). Those timeouts take 20s, so rather than make the
37 // user wait 20s for the timeout to fire, we use a fallback timer
38 // (kIPv6FallbackTimerInMs) and start a connect() to an IPv4 address if the
39 // timer fires. Then we race the IPv4 connect(s) against the IPv6 connect(s) and
40 // use the socket that completes successfully first or fails last.
41 class NET_EXPORT_PRIVATE WebSocketTransportConnectJob : public ConnectJob {
42 public:
43 WebSocketTransportConnectJob(
44 const std::string& group_name,
45 RequestPriority priority,
46 const scoped_refptr<TransportSocketParams>& params,
47 base::TimeDelta timeout_duration,
48 const CompletionCallback& callback,
49 ClientSocketFactory* client_socket_factory,
50 HostResolver* host_resolver,
51 ClientSocketHandle* handle,
52 Delegate* delegate,
53 NetLog* pool_net_log,
54 const BoundNetLog& request_net_log);
55 ~WebSocketTransportConnectJob() override;
57 // Unlike normal socket pools, the WebSocketTransportClientPool uses
58 // early-binding of sockets.
59 ClientSocketHandle* handle() const { return handle_; }
61 // Stash the callback from RequestSocket() here for convenience.
62 const CompletionCallback& callback() const { return callback_; }
64 const BoundNetLog& request_net_log() const { return request_net_log_; }
66 // ConnectJob methods.
67 LoadState GetLoadState() const override;
69 private:
70 friend class WebSocketTransportConnectSubJob;
71 friend class TransportConnectJobHelper;
72 friend class WebSocketEndpointLockManager;
74 // Although it is not strictly necessary, it makes the code simpler if each
75 // subjob knows what type it is.
76 enum SubJobType { SUB_JOB_IPV4, SUB_JOB_IPV6 };
78 int DoResolveHost();
79 int DoResolveHostComplete(int result);
80 int DoTransportConnect();
81 int DoTransportConnectComplete(int result);
83 // Called back from a SubJob when it completes.
84 void OnSubJobComplete(int result, WebSocketTransportConnectSubJob* job);
86 // Called from |fallback_timer_|.
87 void StartIPv4JobAsync();
89 // Begins the host resolution and the TCP connect. Returns OK on success
90 // and ERR_IO_PENDING if it cannot immediately service the request.
91 // Otherwise, it returns a net error code.
92 int ConnectInternal() override;
94 TransportConnectJobHelper helper_;
96 // The addresses are divided into IPv4 and IPv6, which are performed partially
97 // in parallel. If the list of IPv6 addresses is non-empty, then the IPv6 jobs
98 // go first, followed after |kIPv6FallbackTimerInMs| by the IPv4
99 // addresses. First sub-job to establish a connection wins.
100 scoped_ptr<WebSocketTransportConnectSubJob> ipv4_job_;
101 scoped_ptr<WebSocketTransportConnectSubJob> ipv6_job_;
103 base::OneShotTimer<WebSocketTransportConnectJob> fallback_timer_;
104 TransportConnectJobHelper::ConnectionLatencyHistogram race_result_;
105 ClientSocketHandle* const handle_;
106 CompletionCallback callback_;
107 BoundNetLog request_net_log_;
109 bool had_ipv4_;
110 bool had_ipv6_;
112 DISALLOW_COPY_AND_ASSIGN(WebSocketTransportConnectJob);
115 class NET_EXPORT_PRIVATE WebSocketTransportClientSocketPool
116 : public TransportClientSocketPool {
117 public:
118 WebSocketTransportClientSocketPool(int max_sockets,
119 int max_sockets_per_group,
120 HostResolver* host_resolver,
121 ClientSocketFactory* client_socket_factory,
122 NetLog* net_log);
124 ~WebSocketTransportClientSocketPool() override;
126 // Allow another connection to be started to the IPEndPoint that this |handle|
127 // is connected to. Used when the WebSocket handshake completes successfully.
128 // This only works if the socket is connected, however the caller does not
129 // need to explicitly check for this. Instead, ensure that dead sockets are
130 // returned to ReleaseSocket() in a timely fashion.
131 static void UnlockEndpoint(ClientSocketHandle* handle);
133 // ClientSocketPool implementation.
134 int RequestSocket(const std::string& group_name,
135 const void* resolve_info,
136 RequestPriority priority,
137 ClientSocketHandle* handle,
138 const CompletionCallback& callback,
139 const BoundNetLog& net_log) override;
140 void RequestSockets(const std::string& group_name,
141 const void* params,
142 int num_sockets,
143 const BoundNetLog& net_log) override;
144 void CancelRequest(const std::string& group_name,
145 ClientSocketHandle* handle) override;
146 void ReleaseSocket(const std::string& group_name,
147 scoped_ptr<StreamSocket> socket,
148 int id) override;
149 void FlushWithError(int error) override;
150 void CloseIdleSockets() override;
151 int IdleSocketCount() const override;
152 int IdleSocketCountInGroup(const std::string& group_name) const override;
153 LoadState GetLoadState(const std::string& group_name,
154 const ClientSocketHandle* handle) const override;
155 scoped_ptr<base::DictionaryValue> GetInfoAsValue(
156 const std::string& name,
157 const std::string& type,
158 bool include_nested_pools) const override;
159 base::TimeDelta ConnectionTimeout() const override;
161 // HigherLayeredPool implementation.
162 bool IsStalled() const override;
164 private:
165 class ConnectJobDelegate : public ConnectJob::Delegate {
166 public:
167 explicit ConnectJobDelegate(WebSocketTransportClientSocketPool* owner);
168 ~ConnectJobDelegate() override;
170 void OnConnectJobComplete(int result, ConnectJob* job) override;
172 private:
173 WebSocketTransportClientSocketPool* owner_;
175 DISALLOW_COPY_AND_ASSIGN(ConnectJobDelegate);
178 // Store the arguments from a call to RequestSocket() that has stalled so we
179 // can replay it when there are available socket slots.
180 struct StalledRequest {
181 StalledRequest(const scoped_refptr<TransportSocketParams>& params,
182 RequestPriority priority,
183 ClientSocketHandle* handle,
184 const CompletionCallback& callback,
185 const BoundNetLog& net_log);
186 ~StalledRequest();
187 const scoped_refptr<TransportSocketParams> params;
188 const RequestPriority priority;
189 ClientSocketHandle* const handle;
190 const CompletionCallback callback;
191 const BoundNetLog net_log;
193 friend class ConnectJobDelegate;
194 typedef std::map<const ClientSocketHandle*, WebSocketTransportConnectJob*>
195 PendingConnectsMap;
196 // This is a list so that we can remove requests from the middle, and also
197 // so that iterators are not invalidated unless the corresponding request is
198 // removed.
199 typedef std::list<StalledRequest> StalledRequestQueue;
200 typedef std::map<const ClientSocketHandle*, StalledRequestQueue::iterator>
201 StalledRequestMap;
203 void OnConnectJobComplete(int result, WebSocketTransportConnectJob* job);
204 void InvokeUserCallbackLater(ClientSocketHandle* handle,
205 const CompletionCallback& callback,
206 int rv);
207 void InvokeUserCallback(ClientSocketHandle* handle,
208 const CompletionCallback& callback,
209 int rv);
210 bool ReachedMaxSocketsLimit() const;
211 void HandOutSocket(scoped_ptr<StreamSocket> socket,
212 const LoadTimingInfo::ConnectTiming& connect_timing,
213 ClientSocketHandle* handle,
214 const BoundNetLog& net_log);
215 void AddJob(ClientSocketHandle* handle,
216 scoped_ptr<WebSocketTransportConnectJob> connect_job);
217 bool DeleteJob(ClientSocketHandle* handle);
218 const WebSocketTransportConnectJob* LookupConnectJob(
219 const ClientSocketHandle* handle) const;
220 void ActivateStalledRequest();
221 bool DeleteStalledRequest(ClientSocketHandle* handle);
223 ConnectJobDelegate connect_job_delegate_;
224 std::set<const ClientSocketHandle*> pending_callbacks_;
225 PendingConnectsMap pending_connects_;
226 StalledRequestQueue stalled_request_queue_;
227 StalledRequestMap stalled_request_map_;
228 NetLog* const pool_net_log_;
229 ClientSocketFactory* const client_socket_factory_;
230 HostResolver* const host_resolver_;
231 const int max_sockets_;
232 int handed_out_socket_count_;
233 bool flushing_;
235 base::WeakPtrFactory<WebSocketTransportClientSocketPool> weak_factory_;
237 DISALLOW_COPY_AND_ASSIGN(WebSocketTransportClientSocketPool);
240 } // namespace net
242 #endif // NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_