blob: e6368ca1c61cd8ce9b2f7f7fb7670003c3dfb480 [file] [log] [blame]
[email protected]ff579d42009-06-24 15:47:021// Copyright (c) 2006-2008 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.
[email protected]d80a4322009-08-14 07:07:494//
5// A ClientSocketPoolBase is used to restrict the number of sockets open at
6// a time. It also maintains a list of idle persistent sockets for reuse.
7// Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle
8// the core logic of (1) restricting the number of active (connected or
9// connecting) sockets per "group" (generally speaking, the hostname), (2)
10// maintaining a per-group list of idle, persistent sockets for reuse, and (3)
11// limiting the total number of active sockets in the system.
12//
13// ClientSocketPoolBase abstracts socket connection details behind ConnectJob,
14// ConnectJobFactory, and SocketParams. When a socket "slot" becomes available,
15// the ClientSocketPoolBase will ask the ConnectJobFactory to create a
16// ConnectJob with a SocketParams. Subclasses of ClientSocketPool should
17// implement their socket specific connection by subclassing ConnectJob and
18// implementing ConnectJob::ConnectInternal(). They can control the parameters
19// passed to each new ConnectJob instance via their ConnectJobFactory subclass
20// and templated SocketParams parameter.
21//
[email protected]ff579d42009-06-24 15:47:0222#ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_
23#define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_
24
25#include <deque>
26#include <map>
[email protected]5fc08e32009-07-15 17:09:5727#include <set>
[email protected]ff579d42009-06-24 15:47:0228#include <string>
29
30#include "base/basictypes.h"
31#include "base/scoped_ptr.h"
32#include "base/time.h"
33#include "base/timer.h"
34#include "net/base/address_list.h"
35#include "net/base/completion_callback.h"
[email protected]ec08bb22009-08-12 00:25:1236#include "net/base/load_log.h"
[email protected]ff579d42009-06-24 15:47:0237#include "net/base/load_states.h"
[email protected]d80a4322009-08-14 07:07:4938#include "net/base/net_errors.h"
[email protected]2ab05b52009-07-01 23:57:5839#include "net/socket/client_socket.h"
[email protected]ff579d42009-06-24 15:47:0240#include "net/socket/client_socket_pool.h"
41
42namespace net {
43
[email protected]ff579d42009-06-24 15:47:0244class ClientSocketHandle;
[email protected]ff579d42009-06-24 15:47:0245
46// ConnectJob provides an abstract interface for "connecting" a socket.
47// The connection may involve host resolution, tcp connection, ssl connection,
48// etc.
49class ConnectJob {
50 public:
[email protected]ab838892009-06-30 18:49:0551 class Delegate {
52 public:
53 Delegate() {}
54 virtual ~Delegate() {}
55
[email protected]2ab05b52009-07-01 23:57:5856 // Alerts the delegate that the connection completed.
57 virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0;
[email protected]ab838892009-06-30 18:49:0558
59 private:
60 DISALLOW_COPY_AND_ASSIGN(Delegate);
61 };
62
[email protected]974ebd62009-08-03 23:14:3463 // A |timeout_duration| of 0 corresponds to no timeout.
[email protected]2ab05b52009-07-01 23:57:5864 ConnectJob(const std::string& group_name,
65 const ClientSocketHandle* key_handle,
[email protected]974ebd62009-08-03 23:14:3466 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3067 Delegate* delegate,
68 LoadLog* load_log);
[email protected]2ab05b52009-07-01 23:57:5869 virtual ~ConnectJob();
[email protected]ff579d42009-06-24 15:47:0270
[email protected]2ab05b52009-07-01 23:57:5871 // Accessors
72 const std::string& group_name() const { return group_name_; }
[email protected]ab838892009-06-30 18:49:0573 LoadState load_state() const { return load_state_; }
[email protected]2ab05b52009-07-01 23:57:5874 const ClientSocketHandle* key_handle() const { return key_handle_; }
75
[email protected]8e12ae02009-07-02 16:15:0476 // Releases |socket_| to the client. On connection error, this should return
77 // NULL.
[email protected]2ab05b52009-07-01 23:57:5878 ClientSocket* ReleaseSocket() { return socket_.release(); }
[email protected]ab838892009-06-30 18:49:0579
[email protected]ff579d42009-06-24 15:47:0280 // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it
81 // cannot complete synchronously without blocking, or another net error code
[email protected]2ab05b52009-07-01 23:57:5882 // on error. In asynchronous completion, the ConnectJob will notify
83 // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous
84 // completion, ReleaseSocket() can be called to acquire the connected socket
85 // if it succeeded.
[email protected]974ebd62009-08-03 23:14:3486 int Connect();
[email protected]ff579d42009-06-24 15:47:0287
[email protected]fd7b7c92009-08-20 19:38:3088 LoadLog* load_log() { return load_log_; }
89
[email protected]ab838892009-06-30 18:49:0590 protected:
91 void set_load_state(LoadState load_state) { load_state_ = load_state; }
[email protected]2ab05b52009-07-01 23:57:5892 void set_socket(ClientSocket* socket) { socket_.reset(socket); }
93 ClientSocket* socket() { return socket_.get(); }
[email protected]fd7b7c92009-08-20 19:38:3094 void NotifyDelegateOfCompletion(int rv);
[email protected]ab838892009-06-30 18:49:0595
[email protected]ff579d42009-06-24 15:47:0296 private:
[email protected]974ebd62009-08-03 23:14:3497 virtual int ConnectInternal() = 0;
98
99 // Alerts the delegate that the ConnectJob has timed out.
100 void OnTimeout();
101
[email protected]2ab05b52009-07-01 23:57:58102 const std::string group_name_;
103 // Temporarily needed until we switch to late binding.
104 const ClientSocketHandle* const key_handle_;
[email protected]974ebd62009-08-03 23:14:34105 const base::TimeDelta timeout_duration_;
106 // Timer to abort jobs that take too long.
107 base::OneShotTimer<ConnectJob> timer_;
108 Delegate* delegate_;
[email protected]ab838892009-06-30 18:49:05109 LoadState load_state_;
[email protected]2ab05b52009-07-01 23:57:58110 scoped_ptr<ClientSocket> socket_;
[email protected]fd7b7c92009-08-20 19:38:30111 scoped_refptr<LoadLog> load_log_;
[email protected]ab838892009-06-30 18:49:05112
[email protected]ff579d42009-06-24 15:47:02113 DISALLOW_COPY_AND_ASSIGN(ConnectJob);
114};
115
[email protected]d80a4322009-08-14 07:07:49116namespace internal {
117
118// ClientSocketPoolBaseHelper is an internal class that implements almost all
119// the functionality from ClientSocketPoolBase without using templates.
120// ClientSocketPoolBase adds templated definitions built on top of
121// ClientSocketPoolBaseHelper. This class is not for external use, please use
122// ClientSocketPoolBase instead.
123class ClientSocketPoolBaseHelper
124 : public base::RefCounted<ClientSocketPoolBaseHelper>,
[email protected]ab838892009-06-30 18:49:05125 public ConnectJob::Delegate {
[email protected]ff579d42009-06-24 15:47:02126 public:
[email protected]d80a4322009-08-14 07:07:49127 class Request {
128 public:
[email protected]684970b2009-08-14 04:54:46129 Request(ClientSocketHandle* handle,
[email protected]ff579d42009-06-24 15:47:02130 CompletionCallback* callback,
131 int priority,
[email protected]684970b2009-08-14 04:54:46132 LoadLog* load_log)
[email protected]fd7b7c92009-08-20 19:38:30133 : handle_(handle), callback_(callback), priority_(priority),
134 load_log_(load_log) {}
[email protected]ff579d42009-06-24 15:47:02135
[email protected]d80a4322009-08-14 07:07:49136 virtual ~Request() {}
137
138 ClientSocketHandle* handle() const { return handle_; }
139 CompletionCallback* callback() const { return callback_; }
140 int priority() const { return priority_; }
141 LoadLog* load_log() const { return load_log_.get(); }
142
143 private:
144 ClientSocketHandle* const handle_;
145 CompletionCallback* const callback_;
146 const int priority_;
147 const scoped_refptr<LoadLog> load_log_;
148
149 DISALLOW_COPY_AND_ASSIGN(Request);
[email protected]ff579d42009-06-24 15:47:02150 };
151
152 class ConnectJobFactory {
153 public:
154 ConnectJobFactory() {}
155 virtual ~ConnectJobFactory() {}
156
157 virtual ConnectJob* NewConnectJob(
158 const std::string& group_name,
159 const Request& request,
[email protected]fd7b7c92009-08-20 19:38:30160 ConnectJob::Delegate* delegate,
161 LoadLog* load_log) const = 0;
[email protected]ff579d42009-06-24 15:47:02162
163 private:
164 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory);
165 };
166
[email protected]d80a4322009-08-14 07:07:49167 ClientSocketPoolBaseHelper(int max_sockets,
168 int max_sockets_per_group,
169 ConnectJobFactory* connect_job_factory);
[email protected]ff579d42009-06-24 15:47:02170
[email protected]d80a4322009-08-14 07:07:49171 ~ClientSocketPoolBaseHelper();
[email protected]ff579d42009-06-24 15:47:02172
[email protected]d80a4322009-08-14 07:07:49173 // See ClientSocketPool::RequestSocket for documentation on this function.
174 // Note that |request| must be heap allocated. If ERR_IO_PENDING is returned,
175 // then ClientSocketPoolBaseHelper takes ownership of |request|.
176 int RequestSocket(const std::string& group_name, const Request* request);
[email protected]ff579d42009-06-24 15:47:02177
[email protected]d80a4322009-08-14 07:07:49178 // See ClientSocketPool::CancelRequest for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02179 void CancelRequest(const std::string& group_name,
180 const ClientSocketHandle* handle);
181
[email protected]d80a4322009-08-14 07:07:49182 // See ClientSocketPool::ReleaseSocket for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02183 void ReleaseSocket(const std::string& group_name,
184 ClientSocket* socket);
185
[email protected]d80a4322009-08-14 07:07:49186 // See ClientSocketPool::CloseIdleSockets for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02187 void CloseIdleSockets();
188
[email protected]d80a4322009-08-14 07:07:49189 // See ClientSocketPool::IdleSocketCount() for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02190 int idle_socket_count() const {
191 return idle_socket_count_;
192 }
193
[email protected]d80a4322009-08-14 07:07:49194 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this
195 // function.
[email protected]ff579d42009-06-24 15:47:02196 int IdleSocketCountInGroup(const std::string& group_name) const;
197
[email protected]d80a4322009-08-14 07:07:49198 // See ClientSocketPool::GetLoadState() for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02199 LoadState GetLoadState(const std::string& group_name,
200 const ClientSocketHandle* handle) const;
201
[email protected]d80a4322009-08-14 07:07:49202 // ConnectJob::Delegate methods:
[email protected]2ab05b52009-07-01 23:57:58203 virtual void OnConnectJobComplete(int result, ConnectJob* job);
[email protected]ff579d42009-06-24 15:47:02204
[email protected]5fc08e32009-07-15 17:09:57205 // Enables late binding of sockets. In this mode, socket requests are
206 // decoupled from socket connection jobs. A socket request may initiate a
207 // socket connection job, but there is no guarantee that that socket
208 // connection will service the request (for example, a released socket may
209 // service the request sooner, or a higher priority request may come in
210 // afterward and receive the socket from the job).
211 static void EnableLateBindingOfSockets(bool enabled);
212
[email protected]211d2172009-07-22 15:48:53213 // For testing.
214 bool may_have_stalled_group() const { return may_have_stalled_group_; }
[email protected]d80a4322009-08-14 07:07:49215
[email protected]974ebd62009-08-03 23:14:34216 int NumConnectJobsInGroup(const std::string& group_name) const {
217 return group_map_.find(group_name)->second.jobs.size();
218 }
[email protected]211d2172009-07-22 15:48:53219
[email protected]ff579d42009-06-24 15:47:02220 private:
221 // Entry for a persistent socket which became idle at time |start_time|.
222 struct IdleSocket {
[email protected]5fc08e32009-07-15 17:09:57223 IdleSocket() : socket(NULL), used(false) {}
[email protected]ff579d42009-06-24 15:47:02224 ClientSocket* socket;
225 base::TimeTicks start_time;
[email protected]5fc08e32009-07-15 17:09:57226 bool used; // Indicates whether or not the socket has been used yet.
[email protected]ff579d42009-06-24 15:47:02227
228 // An idle socket should be removed if it can't be reused, or has been idle
229 // for too long. |now| is the current time value (TimeTicks::Now()).
230 //
231 // An idle socket can't be reused if it is disconnected or has received
232 // data unexpectedly (hence no longer idle). The unread data would be
233 // mistaken for the beginning of the next response if we were to reuse the
234 // socket for a new request.
235 bool ShouldCleanup(base::TimeTicks now) const;
236 };
237
[email protected]d80a4322009-08-14 07:07:49238 typedef std::deque<const Request*> RequestQueue;
239 typedef std::map<const ClientSocketHandle*, const Request*> RequestMap;
[email protected]ff579d42009-06-24 15:47:02240
241 // A Group is allocated per group_name when there are idle sockets or pending
242 // requests. Otherwise, the Group object is removed from the map.
243 struct Group {
[email protected]2ab05b52009-07-01 23:57:58244 Group() : active_socket_count(0) {}
245
246 bool IsEmpty() const {
[email protected]2b7523d2009-07-29 20:29:23247 return active_socket_count == 0 && idle_sockets.empty() && jobs.empty() &&
248 pending_requests.empty() && connecting_requests.empty();
[email protected]2ab05b52009-07-01 23:57:58249 }
250
251 bool HasAvailableSocketSlot(int max_sockets_per_group) const {
[email protected]5fc08e32009-07-15 17:09:57252 return active_socket_count + static_cast<int>(jobs.size()) <
[email protected]2ab05b52009-07-01 23:57:58253 max_sockets_per_group;
254 }
255
[email protected]211d2172009-07-22 15:48:53256 int TopPendingPriority() const {
[email protected]d80a4322009-08-14 07:07:49257 return pending_requests.front()->priority();
[email protected]211d2172009-07-22 15:48:53258 }
259
[email protected]ff579d42009-06-24 15:47:02260 std::deque<IdleSocket> idle_sockets;
[email protected]5fc08e32009-07-15 17:09:57261 std::set<const ConnectJob*> jobs;
[email protected]ff579d42009-06-24 15:47:02262 RequestQueue pending_requests;
263 RequestMap connecting_requests;
[email protected]2ab05b52009-07-01 23:57:58264 int active_socket_count; // number of active sockets used by clients
[email protected]ff579d42009-06-24 15:47:02265 };
266
267 typedef std::map<std::string, Group> GroupMap;
268
269 typedef std::map<const ClientSocketHandle*, ConnectJob*> ConnectJobMap;
[email protected]5fc08e32009-07-15 17:09:57270 typedef std::set<const ConnectJob*> ConnectJobSet;
[email protected]ff579d42009-06-24 15:47:02271
[email protected]d80a4322009-08-14 07:07:49272 static void InsertRequestIntoQueue(const Request* r,
[email protected]ff579d42009-06-24 15:47:02273 RequestQueue* pending_requests);
[email protected]fd7b7c92009-08-20 19:38:30274 static const Request* RemoveRequestFromQueue(RequestQueue::iterator it,
275 RequestQueue* pending_requests);
[email protected]ff579d42009-06-24 15:47:02276
277 // Closes all idle sockets if |force| is true. Else, only closes idle
278 // sockets that timed out or can't be reused.
279 void CleanupIdleSockets(bool force);
280
281 // Called when the number of idle sockets changes.
282 void IncrementIdleCount();
283 void DecrementIdleCount();
284
285 // Called via PostTask by ReleaseSocket.
286 void DoReleaseSocket(const std::string& group_name, ClientSocket* socket);
287
[email protected]211d2172009-07-22 15:48:53288 // Scans the group map for groups which have an available socket slot and
289 // at least one pending request. Returns number of groups found, and if found
290 // at least one, fills |group| and |group_name| with data of the stalled group
291 // having highest priority.
292 int FindTopStalledGroup(Group** group, std::string* group_name);
293
[email protected]ff579d42009-06-24 15:47:02294 // Called when timer_ fires. This method scans the idle sockets removing
295 // sockets that timed out or can't be reused.
296 void OnCleanupTimerFired() {
297 CleanupIdleSockets(false);
298 }
299
300 // Removes the ConnectJob corresponding to |handle| from the
[email protected]5fc08e32009-07-15 17:09:57301 // |connect_job_map_| or |connect_job_set_| depending on whether or not late
302 // binding is enabled. |job| must be non-NULL when late binding is
[email protected]d80a4322009-08-14 07:07:49303 // enabled. Also updates |group| if non-NULL. When late binding is disabled,
304 // this will also delete the Request from |group->connecting_requests|.
[email protected]5fc08e32009-07-15 17:09:57305 void RemoveConnectJob(const ClientSocketHandle* handle,
[email protected]974ebd62009-08-03 23:14:34306 const ConnectJob* job,
[email protected]5fc08e32009-07-15 17:09:57307 Group* group);
[email protected]ff579d42009-06-24 15:47:02308
[email protected]2ab05b52009-07-01 23:57:58309 // Same as OnAvailableSocketSlot except it looks up the Group first to see if
310 // it's there.
311 void MaybeOnAvailableSocketSlot(const std::string& group_name);
[email protected]ff579d42009-06-24 15:47:02312
[email protected]2ab05b52009-07-01 23:57:58313 // Might delete the Group from |group_map_|.
314 void OnAvailableSocketSlot(const std::string& group_name, Group* group);
[email protected]ff579d42009-06-24 15:47:02315
316 // Process a request from a group's pending_requests queue.
317 void ProcessPendingRequest(const std::string& group_name, Group* group);
318
[email protected]2ab05b52009-07-01 23:57:58319 // Assigns |socket| to |handle| and updates |group|'s counters appropriately.
320 void HandOutSocket(ClientSocket* socket,
321 bool reused,
322 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29323 base::TimeDelta time_idle,
[email protected]2ab05b52009-07-01 23:57:58324 Group* group);
325
[email protected]5fc08e32009-07-15 17:09:57326 // Adds |socket| to the list of idle sockets for |group|. |used| indicates
327 // whether or not the socket has previously been used.
328 void AddIdleSocket(ClientSocket* socket, bool used, Group* group);
329
330 // Iterates through |connect_job_map_|, canceling all ConnectJobs.
331 // Afterwards, it iterates through all groups and deletes them if they are no
332 // longer needed.
333 void CancelAllConnectJobs();
334
[email protected]211d2172009-07-22 15:48:53335 // Returns true if we can't create any more sockets due to the total limit.
336 // TODO(phajdan.jr): Also take idle sockets into account.
337 bool ReachedMaxSocketsLimit() const;
338
[email protected]ff579d42009-06-24 15:47:02339 GroupMap group_map_;
340
341 ConnectJobMap connect_job_map_;
342
343 // Timer used to periodically prune idle sockets that timed out or can't be
344 // reused.
[email protected]d80a4322009-08-14 07:07:49345 base::RepeatingTimer<ClientSocketPoolBaseHelper> timer_;
[email protected]ff579d42009-06-24 15:47:02346
347 // The total number of idle sockets in the system.
348 int idle_socket_count_;
349
[email protected]211d2172009-07-22 15:48:53350 // Number of connecting sockets across all groups.
351 int connecting_socket_count_;
352
353 // Number of connected sockets we handed out across all groups.
354 int handed_out_socket_count_;
355
356 // The maximum total number of sockets. See ReachedMaxSocketsLimit.
357 const int max_sockets_;
358
[email protected]ff579d42009-06-24 15:47:02359 // The maximum number of sockets kept per group.
360 const int max_sockets_per_group_;
361
[email protected]211d2172009-07-22 15:48:53362 // Until the maximum number of sockets limit is reached, a group can only
363 // have pending requests if it exceeds the "max sockets per group" limit.
364 //
365 // This means when a socket is released, the only pending requests that can
366 // be started next belong to the same group.
367 //
368 // However once the |max_sockets_| limit is reached, this stops being true:
369 // groups can now have pending requests without having first reached the
370 // |max_sockets_per_group_| limit. So choosing the next request involves
371 // selecting the highest priority request across *all* groups.
372 //
373 // Since reaching the maximum number of sockets is an edge case, we make note
374 // of when it happens, and thus avoid doing the slower "scan all groups"
375 // in the common case.
376 bool may_have_stalled_group_;
377
[email protected]ab838892009-06-30 18:49:05378 const scoped_ptr<ConnectJobFactory> connect_job_factory_;
[email protected]ff579d42009-06-24 15:47:02379
[email protected]5fc08e32009-07-15 17:09:57380 // Controls whether or not we use late binding of sockets.
381 static bool g_late_binding;
382
[email protected]d80a4322009-08-14 07:07:49383};
384
385} // namespace internal
386
387template <typename SocketParams>
388class ClientSocketPoolBase {
389 public:
390 class Request : public internal::ClientSocketPoolBaseHelper::Request {
391 public:
392 Request(ClientSocketHandle* handle,
393 CompletionCallback* callback,
394 int priority,
395 const SocketParams& params,
396 LoadLog* load_log)
397 : internal::ClientSocketPoolBaseHelper::Request(
398 handle, callback, priority, load_log),
399 params_(params) {}
400
401 const SocketParams& params() const { return params_; }
402
403 private:
404 SocketParams params_;
405 };
406
407 class ConnectJobFactory {
408 public:
409 ConnectJobFactory() {}
410 virtual ~ConnectJobFactory() {}
411
412 virtual ConnectJob* NewConnectJob(
413 const std::string& group_name,
414 const Request& request,
[email protected]fd7b7c92009-08-20 19:38:30415 ConnectJob::Delegate* delegate,
416 LoadLog* load_log) const = 0;
[email protected]d80a4322009-08-14 07:07:49417
418 private:
419 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory);
420 };
421
422 ClientSocketPoolBase(int max_sockets,
423 int max_sockets_per_group,
424 ConnectJobFactory* connect_job_factory)
425 : helper_(new internal::ClientSocketPoolBaseHelper(
426 max_sockets, max_sockets_per_group,
427 new ConnectJobFactoryAdaptor(connect_job_factory))) {}
428
429 ~ClientSocketPoolBase() {}
430
431 // These member functions simply forward to ClientSocketPoolBaseHelper.
432
433 // RequestSocket bundles up the parameters into a Request and then forwards to
434 // ClientSocketPoolBaseHelper::RequestSocket(). Note that the memory
435 // ownership is transferred in the asynchronous (ERR_IO_PENDING) case.
436 int RequestSocket(const std::string& group_name,
437 const SocketParams& params,
438 int priority,
439 ClientSocketHandle* handle,
440 CompletionCallback* callback,
441 LoadLog* load_log) {
442 scoped_ptr<Request> request(
443 new Request(handle, callback, priority, params, load_log));
[email protected]fd7b7c92009-08-20 19:38:30444 LoadLog::BeginEvent(load_log, LoadLog::TYPE_SOCKET_POOL);
[email protected]d80a4322009-08-14 07:07:49445 int rv = helper_->RequestSocket(group_name, request.get());
446 if (rv == ERR_IO_PENDING)
447 request.release();
[email protected]fd7b7c92009-08-20 19:38:30448 else
449 LoadLog::EndEvent(load_log, LoadLog::TYPE_SOCKET_POOL);
[email protected]d80a4322009-08-14 07:07:49450 return rv;
451 }
452
453 void CancelRequest(const std::string& group_name,
454 const ClientSocketHandle* handle) {
455 return helper_->CancelRequest(group_name, handle);
456 }
457
458 void ReleaseSocket(const std::string& group_name, ClientSocket* socket) {
459 return helper_->ReleaseSocket(group_name, socket);
460 }
461
462 void CloseIdleSockets() { return helper_->CloseIdleSockets(); }
463
464 int idle_socket_count() const { return helper_->idle_socket_count(); }
465
466 int IdleSocketCountInGroup(const std::string& group_name) const {
467 return helper_->IdleSocketCountInGroup(group_name);
468 }
469
470 LoadState GetLoadState(const std::string& group_name,
471 const ClientSocketHandle* handle) const {
472 return helper_->GetLoadState(group_name, handle);
473 }
474
475 virtual void OnConnectJobComplete(int result, ConnectJob* job) {
476 return helper_->OnConnectJobComplete(result, job);
477 }
478
479 // For testing.
480 bool may_have_stalled_group() const {
481 return helper_->may_have_stalled_group();
482 }
483
484 int NumConnectJobsInGroup(const std::string& group_name) const {
485 return helper_->NumConnectJobsInGroup(group_name);
486 }
487
488 private:
489 // This adaptor class exists to bridge the
490 // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and
491 // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the
492 // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to
493 // static_cast themselves.
494 class ConnectJobFactoryAdaptor
495 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory {
496 public:
497 typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory
498 ConnectJobFactory;
499
500 explicit ConnectJobFactoryAdaptor(
501 ConnectJobFactory* connect_job_factory)
502 : connect_job_factory_(connect_job_factory) {}
503 virtual ~ConnectJobFactoryAdaptor() {}
504
505 virtual ConnectJob* NewConnectJob(
506 const std::string& group_name,
507 const internal::ClientSocketPoolBaseHelper::Request& request,
[email protected]fd7b7c92009-08-20 19:38:30508 ConnectJob::Delegate* delegate,
509 LoadLog* load_log) const {
[email protected]d80a4322009-08-14 07:07:49510 const Request* casted_request = static_cast<const Request*>(&request);
511 return connect_job_factory_->NewConnectJob(
[email protected]fd7b7c92009-08-20 19:38:30512 group_name, *casted_request, delegate, load_log);
[email protected]d80a4322009-08-14 07:07:49513 }
514
515 const scoped_ptr<ConnectJobFactory> connect_job_factory_;
516 };
517
518 // One might ask why ClientSocketPoolBaseHelper is also refcounted if its
519 // containing ClientSocketPool is already refcounted. The reason is because
520 // DoReleaseSocket() posts a task. If ClientSocketPool gets deleted between
521 // the posting of the task and the execution, then we'll hit the DCHECK that
522 // |ClientSocketPoolBaseHelper::group_map_| is empty.
523 scoped_refptr<internal::ClientSocketPoolBaseHelper> helper_;
524
[email protected]ff579d42009-06-24 15:47:02525 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase);
526};
527
[email protected]d80a4322009-08-14 07:07:49528// Enables late binding of sockets. In this mode, socket requests are
529// decoupled from socket connection jobs. A socket request may initiate a
530// socket connection job, but there is no guarantee that that socket
531// connection will service the request (for example, a released socket may
532// service the request sooner, or a higher priority request may come in
533// afterward and receive the socket from the job).
534void EnableLateBindingOfSockets(bool enabled);
535
[email protected]ff579d42009-06-24 15:47:02536} // namespace net
537
538#endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_