[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 4 | // |
| 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] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 22 | #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 23 | #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 24 | #pragma once |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 25 | |
| 26 | #include <deque> |
| 27 | #include <map> |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 28 | #include <set> |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 29 | #include <string> |
| 30 | |
| 31 | #include "base/basictypes.h" |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 32 | #include "base/ref_counted.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 33 | #include "base/scoped_ptr.h" |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 34 | #include "base/task.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 35 | #include "base/time.h" |
| 36 | #include "base/timer.h" |
| 37 | #include "net/base/address_list.h" |
| 38 | #include "net/base/completion_callback.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 39 | #include "net/base/load_states.h" |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 40 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 41 | #include "net/base/net_log.h" |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 42 | #include "net/base/network_change_notifier.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 43 | #include "net/base/request_priority.h" |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 44 | #include "net/socket/client_socket.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 45 | #include "net/socket/client_socket_pool.h" |
| 46 | |
| 47 | namespace net { |
| 48 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 49 | class ClientSocketHandle; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 50 | |
| 51 | // ConnectJob provides an abstract interface for "connecting" a socket. |
| 52 | // The connection may involve host resolution, tcp connection, ssl connection, |
| 53 | // etc. |
| 54 | class ConnectJob { |
| 55 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 56 | class Delegate { |
| 57 | public: |
| 58 | Delegate() {} |
| 59 | virtual ~Delegate() {} |
| 60 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 61 | // Alerts the delegate that the connection completed. |
| 62 | virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 66 | }; |
| 67 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 68 | // A |timeout_duration| of 0 corresponds to no timeout. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 69 | ConnectJob(const std::string& group_name, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 70 | base::TimeDelta timeout_duration, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 71 | Delegate* delegate, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 72 | const BoundNetLog& net_log); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 73 | virtual ~ConnectJob(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 74 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 75 | // Accessors |
| 76 | const std::string& group_name() const { return group_name_; } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 77 | const BoundNetLog& net_log() { return net_log_; } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 78 | |
[email protected] | 8e12ae0 | 2009-07-02 16:15:04 | [diff] [blame] | 79 | // Releases |socket_| to the client. On connection error, this should return |
| 80 | // NULL. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 81 | ClientSocket* ReleaseSocket() { return socket_.release(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 82 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 83 | // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it |
| 84 | // cannot complete synchronously without blocking, or another net error code |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 85 | // on error. In asynchronous completion, the ConnectJob will notify |
| 86 | // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous |
| 87 | // completion, ReleaseSocket() can be called to acquire the connected socket |
| 88 | // if it succeeded. |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 89 | int Connect(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 90 | |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 91 | virtual LoadState GetLoadState() const = 0; |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 92 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 93 | // If Connect returns an error (or OnConnectJobComplete reports an error |
| 94 | // result) this method will be called, allowing the pool to add |
| 95 | // additional error state to the ClientSocketHandle (post late-binding). |
| 96 | virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} |
| 97 | |
[email protected] | 0496f9a3 | 2010-09-30 16:08:07 | [diff] [blame] | 98 | const BoundNetLog& net_log() const { return net_log_; } |
| 99 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 100 | protected: |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 101 | void set_socket(ClientSocket* socket); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 102 | ClientSocket* socket() { return socket_.get(); } |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 103 | void NotifyDelegateOfCompletion(int rv); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 104 | void ResetTimer(base::TimeDelta remainingTime); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 105 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 106 | private: |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 107 | virtual int ConnectInternal() = 0; |
| 108 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 109 | void LogConnectStart(); |
| 110 | void LogConnectCompletion(int net_error); |
| 111 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 112 | // Alerts the delegate that the ConnectJob has timed out. |
| 113 | void OnTimeout(); |
| 114 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 115 | const std::string group_name_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 116 | const base::TimeDelta timeout_duration_; |
| 117 | // Timer to abort jobs that take too long. |
| 118 | base::OneShotTimer<ConnectJob> timer_; |
| 119 | Delegate* delegate_; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 120 | scoped_ptr<ClientSocket> socket_; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 121 | BoundNetLog net_log_; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 122 | // A ConnectJob is idle until Connect() has been called. |
| 123 | bool idle_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 124 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 125 | DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
| 126 | }; |
| 127 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 128 | namespace internal { |
| 129 | |
| 130 | // ClientSocketPoolBaseHelper is an internal class that implements almost all |
| 131 | // the functionality from ClientSocketPoolBase without using templates. |
| 132 | // ClientSocketPoolBase adds templated definitions built on top of |
| 133 | // ClientSocketPoolBaseHelper. This class is not for external use, please use |
| 134 | // ClientSocketPoolBase instead. |
| 135 | class ClientSocketPoolBaseHelper |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 136 | : public ConnectJob::Delegate, |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 137 | public NetworkChangeNotifier::Observer { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 138 | public: |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 139 | class Request { |
| 140 | public: |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 141 | Request(ClientSocketHandle* handle, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 142 | CompletionCallback* callback, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 143 | RequestPriority priority, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 144 | const BoundNetLog& net_log); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 145 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 146 | virtual ~Request(); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 147 | |
| 148 | ClientSocketHandle* handle() const { return handle_; } |
| 149 | CompletionCallback* callback() const { return callback_; } |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 150 | RequestPriority priority() const { return priority_; } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 151 | const BoundNetLog& net_log() const { return net_log_; } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 152 | |
| 153 | private: |
| 154 | ClientSocketHandle* const handle_; |
| 155 | CompletionCallback* const callback_; |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 156 | const RequestPriority priority_; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 157 | BoundNetLog net_log_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 158 | |
| 159 | DISALLOW_COPY_AND_ASSIGN(Request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | class ConnectJobFactory { |
| 163 | public: |
| 164 | ConnectJobFactory() {} |
| 165 | virtual ~ConnectJobFactory() {} |
| 166 | |
| 167 | virtual ConnectJob* NewConnectJob( |
| 168 | const std::string& group_name, |
| 169 | const Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 170 | ConnectJob::Delegate* delegate) const = 0; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 171 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 172 | virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 173 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 174 | private: |
| 175 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 176 | }; |
| 177 | |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 178 | ClientSocketPoolBaseHelper( |
| 179 | int max_sockets, |
| 180 | int max_sockets_per_group, |
| 181 | base::TimeDelta unused_idle_socket_timeout, |
| 182 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 183 | ConnectJobFactory* connect_job_factory); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 184 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 185 | ~ClientSocketPoolBaseHelper(); |
| 186 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 187 | // See ClientSocketPool::RequestSocket for documentation on this function. |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 188 | // ClientSocketPoolBaseHelper takes ownership of |request|, which must be |
| 189 | // heap allocated. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 190 | int RequestSocket(const std::string& group_name, const Request* request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 191 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 192 | // See ClientSocketPool::CancelRequest for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 193 | void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 194 | ClientSocketHandle* handle); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 195 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 196 | // See ClientSocketPool::ReleaseSocket for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 197 | void ReleaseSocket(const std::string& group_name, |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 198 | ClientSocket* socket, |
| 199 | int id); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 200 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 201 | // See ClientSocketPool::Flush for documentation on this function. |
| 202 | void Flush(); |
[email protected] | 241c5c2c | 2010-06-21 18:46:00 | [diff] [blame] | 203 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 204 | // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 205 | void CloseIdleSockets(); |
| 206 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 207 | // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 208 | int idle_socket_count() const { |
| 209 | return idle_socket_count_; |
| 210 | } |
| 211 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 212 | // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this |
| 213 | // function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 214 | int IdleSocketCountInGroup(const std::string& group_name) const; |
| 215 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 216 | // See ClientSocketPool::GetLoadState() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 217 | LoadState GetLoadState(const std::string& group_name, |
| 218 | const ClientSocketHandle* handle) const; |
| 219 | |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 220 | int ConnectRetryIntervalMs() const { |
| 221 | // TODO(mbelshe): Make this tuned dynamically based on measured RTT. |
| 222 | // For now, just use the max retry interval. |
| 223 | return ClientSocketPool::kMaxConnectRetryIntervalMs; |
| 224 | } |
| 225 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 226 | // ConnectJob::Delegate methods: |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 227 | virtual void OnConnectJobComplete(int result, ConnectJob* job); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 228 | |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 229 | // NetworkChangeNotifier::Observer methods: |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 230 | virtual void OnIPAddressChanged(); |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 231 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 232 | int NumConnectJobsInGroup(const std::string& group_name) const { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 233 | return group_map_.find(group_name)->second->jobs().size(); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 234 | } |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 235 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 236 | bool HasGroup(const std::string& group_name) const; |
| 237 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 238 | // Closes all idle sockets if |force| is true. Else, only closes idle |
| 239 | // sockets that timed out or can't be reused. Made public for testing. |
| 240 | void CleanupIdleSockets(bool force); |
| 241 | |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 242 | // See ClientSocketPool::GetInfoAsValue for documentation on this function. |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 243 | DictionaryValue* GetInfoAsValue(const std::string& name, |
| 244 | const std::string& type) const; |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 245 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 246 | base::TimeDelta ConnectionTimeout() const { |
| 247 | return connect_job_factory_->ConnectionTimeout(); |
| 248 | } |
| 249 | |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 250 | static void set_connect_backup_jobs_enabled(bool enabled); |
| 251 | void EnableConnectBackupJobs(); |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 252 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 253 | private: |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 254 | friend class base::RefCounted<ClientSocketPoolBaseHelper>; |
| 255 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 256 | // Entry for a persistent socket which became idle at time |start_time|. |
| 257 | struct IdleSocket { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 258 | IdleSocket() : socket(NULL) {} |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 259 | ClientSocket* socket; |
| 260 | base::TimeTicks start_time; |
| 261 | |
| 262 | // An idle socket should be removed if it can't be reused, or has been idle |
| 263 | // for too long. |now| is the current time value (TimeTicks::Now()). |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 264 | // |timeout| is the length of time to wait before timing out an idle socket. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 265 | // |
| 266 | // An idle socket can't be reused if it is disconnected or has received |
| 267 | // data unexpectedly (hence no longer idle). The unread data would be |
| 268 | // mistaken for the beginning of the next response if we were to reuse the |
| 269 | // socket for a new request. |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 270 | bool ShouldCleanup(base::TimeTicks now, base::TimeDelta timeout) const; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 271 | }; |
| 272 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 273 | typedef std::deque<const Request*> RequestQueue; |
| 274 | typedef std::map<const ClientSocketHandle*, const Request*> RequestMap; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 275 | |
| 276 | // A Group is allocated per group_name when there are idle sockets or pending |
| 277 | // requests. Otherwise, the Group object is removed from the map. |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 278 | // |active_socket_count| tracks the number of sockets held by clients. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 279 | class Group { |
| 280 | public: |
| 281 | Group(); |
| 282 | ~Group(); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 283 | |
| 284 | bool IsEmpty() const { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 285 | return active_socket_count_ == 0 && idle_sockets_.empty() && |
| 286 | jobs_.empty() && pending_requests_.empty(); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | bool HasAvailableSocketSlot(int max_sockets_per_group) const { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 290 | return active_socket_count_ + static_cast<int>(jobs_.size()) < |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 291 | max_sockets_per_group; |
| 292 | } |
| 293 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 294 | bool IsStalled(int max_sockets_per_group) const { |
| 295 | return HasAvailableSocketSlot(max_sockets_per_group) && |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 296 | pending_requests_.size() > jobs_.size(); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 299 | RequestPriority TopPendingPriority() const { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 300 | return pending_requests_.front()->priority(); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 303 | bool HasBackupJob() const { return !method_factory_.empty(); } |
| 304 | |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 305 | void CleanupBackupJob() { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 306 | method_factory_.RevokeAll(); |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 309 | // Set a timer to create a backup socket if it takes too long to create one. |
| 310 | void StartBackupSocketTimer(const std::string& group_name, |
| 311 | ClientSocketPoolBaseHelper* pool); |
| 312 | |
| 313 | // Called when the backup socket timer fires. |
| 314 | void OnBackupSocketTimerFired( |
| 315 | std::string group_name, |
| 316 | ClientSocketPoolBaseHelper* pool); |
| 317 | |
| 318 | void AddJob(const ConnectJob* job) { jobs_.insert(job); } |
| 319 | void RemoveJob(const ConnectJob* job) { jobs_.erase(job); } |
| 320 | void RemoveAllJobs(); |
| 321 | |
| 322 | void IncrementActiveSocketCount() { active_socket_count_++; } |
| 323 | void DecrementActiveSocketCount() { active_socket_count_--; } |
| 324 | |
| 325 | const std::set<const ConnectJob*>& jobs() const { return jobs_; } |
| 326 | const std::deque<IdleSocket>& idle_sockets() const { return idle_sockets_; } |
| 327 | const RequestQueue& pending_requests() const { return pending_requests_; } |
| 328 | int active_socket_count() const { return active_socket_count_; } |
| 329 | RequestQueue* mutable_pending_requests() { return &pending_requests_; } |
| 330 | std::deque<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; } |
| 331 | |
| 332 | private: |
| 333 | std::deque<IdleSocket> idle_sockets_; |
| 334 | std::set<const ConnectJob*> jobs_; |
| 335 | RequestQueue pending_requests_; |
| 336 | int active_socket_count_; // number of active sockets used by clients |
| 337 | // A factory to pin the backup_job tasks. |
| 338 | ScopedRunnableMethodFactory<Group> method_factory_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 339 | }; |
| 340 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 341 | typedef std::map<std::string, Group*> GroupMap; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 342 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 343 | typedef std::set<const ConnectJob*> ConnectJobSet; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 344 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 345 | struct CallbackResultPair { |
| 346 | CallbackResultPair() : callback(NULL), result(OK) {} |
| 347 | CallbackResultPair(CompletionCallback* callback_in, int result_in) |
| 348 | : callback(callback_in), result(result_in) {} |
| 349 | |
| 350 | CompletionCallback* callback; |
| 351 | int result; |
| 352 | }; |
| 353 | |
| 354 | typedef std::map<const ClientSocketHandle*, CallbackResultPair> |
| 355 | PendingCallbackMap; |
| 356 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 357 | static void InsertRequestIntoQueue(const Request* r, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 358 | RequestQueue* pending_requests); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 359 | static const Request* RemoveRequestFromQueue(RequestQueue::iterator it, |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 360 | Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 361 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 362 | Group* GetOrCreateGroup(const std::string& group_name); |
| 363 | void RemoveGroup(const std::string& group_name); |
| 364 | void RemoveGroup(GroupMap::iterator it); |
| 365 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 366 | // Called when the number of idle sockets changes. |
| 367 | void IncrementIdleCount(); |
| 368 | void DecrementIdleCount(); |
| 369 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 370 | // Scans the group map for groups which have an available socket slot and |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 371 | // at least one pending request. Returns true if any groups are stalled, and |
| 372 | // if so, fills |group| and |group_name| with data of the stalled group |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 373 | // having highest priority. |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 374 | bool FindTopStalledGroup(Group** group, std::string* group_name); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 375 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 376 | // Called when timer_ fires. This method scans the idle sockets removing |
| 377 | // sockets that timed out or can't be reused. |
| 378 | void OnCleanupTimerFired() { |
| 379 | CleanupIdleSockets(false); |
| 380 | } |
| 381 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 382 | // Removes |job| from |connect_job_set_|. Also updates |group| if non-NULL. |
| 383 | void RemoveConnectJob(const ConnectJob* job, Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 384 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 385 | // Tries to see if we can handle any more requests for |group|. |
| 386 | void OnAvailableSocketSlot(const std::string& group_name, Group* group); |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 387 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 388 | // Process a pending socket request for a group. |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 389 | void ProcessPendingRequest(const std::string& group_name, Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 390 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 391 | // Assigns |socket| to |handle| and updates |group|'s counters appropriately. |
| 392 | void HandOutSocket(ClientSocket* socket, |
| 393 | bool reused, |
| 394 | ClientSocketHandle* handle, |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 395 | base::TimeDelta time_idle, |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 396 | Group* group, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 397 | const BoundNetLog& net_log); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 398 | |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 399 | // Adds |socket| to the list of idle sockets for |group|. |
| 400 | void AddIdleSocket(ClientSocket* socket, Group* group); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 401 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 402 | // Iterates through |group_map_|, canceling all ConnectJobs and deleting |
| 403 | // groups if they are no longer needed. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 404 | void CancelAllConnectJobs(); |
| 405 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 406 | // Iterates through |group_map_|, posting ERR_ABORTED callbacks for all |
| 407 | // requests, and then deleting groups if they are no longer needed. |
| 408 | void AbortAllRequests(); |
| 409 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 410 | // Returns true if we can't create any more sockets due to the total limit. |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 411 | bool ReachedMaxSocketsLimit() const; |
| 412 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 413 | // This is the internal implementation of RequestSocket(). It differs in that |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 414 | // it does not handle logging into NetLog of the queueing status of |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 415 | // |request|. |
| 416 | int RequestSocketInternal(const std::string& group_name, |
| 417 | const Request* request); |
| 418 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 419 | // Assigns an idle socket for the group to the request. |
| 420 | // Returns |true| if an idle socket is available, false otherwise. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 421 | bool AssignIdleSocketToGroup(const Request* request, Group* group); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 422 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 423 | static void LogBoundConnectJobToRequest( |
| 424 | const NetLog::Source& connect_job_source, const Request* request); |
| 425 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 426 | // Closes one idle socket. Picks the first one encountered. |
| 427 | // TODO(willchan): Consider a better algorithm for doing this. Perhaps we |
| 428 | // should keep an ordered list of idle sockets, and close them in order. |
| 429 | // Requires maintaining more state. It's not clear if it's worth it since |
| 430 | // I'm not sure if we hit this situation often. |
| 431 | void CloseOneIdleSocket(); |
| 432 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 433 | // Checks if there are stalled socket groups that should be notified |
| 434 | // for possible wakeup. |
| 435 | void CheckForStalledSocketGroups(); |
| 436 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 437 | // Posts a task to call InvokeUserCallback() on the next iteration through the |
| 438 | // current message loop. Inserts |callback| into |pending_callback_map_|, |
| 439 | // keyed by |handle|. |
| 440 | void InvokeUserCallbackLater( |
| 441 | ClientSocketHandle* handle, CompletionCallback* callback, int rv); |
| 442 | |
| 443 | // Invokes the user callback for |handle|. By the time this task has run, |
| 444 | // it's possible that the request has been cancelled, so |handle| may not |
| 445 | // exist in |pending_callback_map_|. We look up the callback and result code |
| 446 | // in |pending_callback_map_|. |
| 447 | void InvokeUserCallback(ClientSocketHandle* handle); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 448 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 449 | GroupMap group_map_; |
| 450 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 451 | // Map of the ClientSocketHandles for which we have a pending Task to invoke a |
| 452 | // callback. This is necessary since, before we invoke said callback, it's |
| 453 | // possible that the request is cancelled. |
| 454 | PendingCallbackMap pending_callback_map_; |
| 455 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 456 | // Timer used to periodically prune idle sockets that timed out or can't be |
| 457 | // reused. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 458 | base::RepeatingTimer<ClientSocketPoolBaseHelper> timer_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 459 | |
| 460 | // The total number of idle sockets in the system. |
| 461 | int idle_socket_count_; |
| 462 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 463 | // Number of connecting sockets across all groups. |
| 464 | int connecting_socket_count_; |
| 465 | |
| 466 | // Number of connected sockets we handed out across all groups. |
| 467 | int handed_out_socket_count_; |
| 468 | |
| 469 | // The maximum total number of sockets. See ReachedMaxSocketsLimit. |
| 470 | const int max_sockets_; |
| 471 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 472 | // The maximum number of sockets kept per group. |
| 473 | const int max_sockets_per_group_; |
| 474 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 475 | // The time to wait until closing idle sockets. |
| 476 | const base::TimeDelta unused_idle_socket_timeout_; |
| 477 | const base::TimeDelta used_idle_socket_timeout_; |
| 478 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 479 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 480 | |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 481 | // TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 482 | bool connect_backup_jobs_enabled_; |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 483 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 484 | // A unique id for the pool. It gets incremented every time we Flush() the |
| 485 | // pool. This is so that when sockets get released back to the pool, we can |
| 486 | // make sure that they are discarded rather than reused. |
| 487 | int pool_generation_number_; |
[email protected] | 09d6ecb0 | 2010-07-22 20:10:45 | [diff] [blame] | 488 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 489 | ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; |
| 490 | |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 491 | DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | } // namespace internal |
| 495 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 496 | // The maximum duration, in seconds, to keep used idle persistent sockets alive. |
| 497 | static const int kUsedIdleSocketTimeout = 300; // 5 minutes |
| 498 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 499 | template <typename SocketParams> |
| 500 | class ClientSocketPoolBase { |
| 501 | public: |
| 502 | class Request : public internal::ClientSocketPoolBaseHelper::Request { |
| 503 | public: |
| 504 | Request(ClientSocketHandle* handle, |
| 505 | CompletionCallback* callback, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 506 | RequestPriority priority, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 507 | const scoped_refptr<SocketParams>& params, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 508 | const BoundNetLog& net_log) |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 509 | : internal::ClientSocketPoolBaseHelper::Request( |
[email protected] | 9008c86f | 2010-08-06 07:10:24 | [diff] [blame] | 510 | handle, callback, priority, net_log), |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 511 | params_(params) {} |
| 512 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 513 | const scoped_refptr<SocketParams>& params() const { return params_; } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 514 | |
| 515 | private: |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 516 | scoped_refptr<SocketParams> params_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | class ConnectJobFactory { |
| 520 | public: |
| 521 | ConnectJobFactory() {} |
| 522 | virtual ~ConnectJobFactory() {} |
| 523 | |
| 524 | virtual ConnectJob* NewConnectJob( |
| 525 | const std::string& group_name, |
| 526 | const Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 527 | ConnectJob::Delegate* delegate) const = 0; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 528 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 529 | virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 530 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 531 | private: |
| 532 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 533 | }; |
| 534 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 535 | // |max_sockets| is the maximum number of sockets to be maintained by this |
| 536 | // ClientSocketPool. |max_sockets_per_group| specifies the maximum number of |
| 537 | // sockets a "group" can have. |unused_idle_socket_timeout| specifies how |
| 538 | // long to leave an unused idle socket open before closing it. |
| 539 | // |used_idle_socket_timeout| specifies how long to leave a previously used |
| 540 | // idle socket open before closing it. |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 541 | ClientSocketPoolBase( |
| 542 | int max_sockets, |
| 543 | int max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 544 | ClientSocketPoolHistograms* histograms, |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 545 | base::TimeDelta unused_idle_socket_timeout, |
| 546 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 547 | ConnectJobFactory* connect_job_factory) |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 548 | : histograms_(histograms), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 549 | helper_(max_sockets, max_sockets_per_group, |
| 550 | unused_idle_socket_timeout, used_idle_socket_timeout, |
| 551 | new ConnectJobFactoryAdaptor(connect_job_factory)) {} |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 552 | |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 553 | virtual ~ClientSocketPoolBase() {} |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 554 | |
| 555 | // These member functions simply forward to ClientSocketPoolBaseHelper. |
| 556 | |
| 557 | // RequestSocket bundles up the parameters into a Request and then forwards to |
| 558 | // ClientSocketPoolBaseHelper::RequestSocket(). Note that the memory |
| 559 | // ownership is transferred in the asynchronous (ERR_IO_PENDING) case. |
| 560 | int RequestSocket(const std::string& group_name, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 561 | const scoped_refptr<SocketParams>& params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 562 | RequestPriority priority, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 563 | ClientSocketHandle* handle, |
| 564 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 565 | const BoundNetLog& net_log) { |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 566 | Request* request = new Request(handle, callback, priority, params, net_log); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 567 | return helper_.RequestSocket(group_name, request); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 571 | ClientSocketHandle* handle) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 572 | return helper_.CancelRequest(group_name, handle); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 573 | } |
| 574 | |
[email protected] | 241c5c2c | 2010-06-21 18:46:00 | [diff] [blame] | 575 | void ReleaseSocket(const std::string& group_name, ClientSocket* socket, |
| 576 | int id) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 577 | return helper_.ReleaseSocket(group_name, socket, id); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 578 | } |
| 579 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 580 | void CloseIdleSockets() { return helper_.CloseIdleSockets(); } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 581 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 582 | int idle_socket_count() const { return helper_.idle_socket_count(); } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 583 | |
| 584 | int IdleSocketCountInGroup(const std::string& group_name) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 585 | return helper_.IdleSocketCountInGroup(group_name); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | LoadState GetLoadState(const std::string& group_name, |
| 589 | const ClientSocketHandle* handle) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 590 | return helper_.GetLoadState(group_name, handle); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | virtual void OnConnectJobComplete(int result, ConnectJob* job) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 594 | return helper_.OnConnectJobComplete(result, job); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 595 | } |
| 596 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 597 | int NumConnectJobsInGroup(const std::string& group_name) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 598 | return helper_.NumConnectJobsInGroup(group_name); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 599 | } |
| 600 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 601 | bool HasGroup(const std::string& group_name) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 602 | return helper_.HasGroup(group_name); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 603 | } |
| 604 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 605 | void CleanupIdleSockets(bool force) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 606 | return helper_.CleanupIdleSockets(force); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 607 | } |
| 608 | |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 609 | DictionaryValue* GetInfoAsValue(const std::string& name, |
| 610 | const std::string& type) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 611 | return helper_.GetInfoAsValue(name, type); |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 612 | } |
| 613 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 614 | base::TimeDelta ConnectionTimeout() const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 615 | return helper_.ConnectionTimeout(); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 616 | } |
| 617 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 618 | ClientSocketPoolHistograms* histograms() const { |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 619 | return histograms_; |
| 620 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 621 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 622 | void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 623 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 624 | void Flush() { helper_.Flush(); } |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 625 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 626 | private: |
| 627 | // This adaptor class exists to bridge the |
| 628 | // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and |
| 629 | // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the |
| 630 | // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to |
| 631 | // static_cast themselves. |
| 632 | class ConnectJobFactoryAdaptor |
| 633 | : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
| 634 | public: |
| 635 | typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory |
| 636 | ConnectJobFactory; |
| 637 | |
[email protected] | 9008c86f | 2010-08-06 07:10:24 | [diff] [blame] | 638 | explicit ConnectJobFactoryAdaptor(ConnectJobFactory* connect_job_factory) |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 639 | : connect_job_factory_(connect_job_factory) {} |
| 640 | virtual ~ConnectJobFactoryAdaptor() {} |
| 641 | |
| 642 | virtual ConnectJob* NewConnectJob( |
| 643 | const std::string& group_name, |
| 644 | const internal::ClientSocketPoolBaseHelper::Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 645 | ConnectJob::Delegate* delegate) const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 646 | const Request* casted_request = static_cast<const Request*>(&request); |
| 647 | return connect_job_factory_->NewConnectJob( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 648 | group_name, *casted_request, delegate); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 649 | } |
| 650 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 651 | virtual base::TimeDelta ConnectionTimeout() const { |
| 652 | return connect_job_factory_->ConnectionTimeout(); |
| 653 | } |
| 654 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 655 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
| 656 | }; |
| 657 | |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 658 | // Histograms for the pool |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 659 | ClientSocketPoolHistograms* const histograms_; |
| 660 | internal::ClientSocketPoolBaseHelper helper_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 661 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 662 | DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 663 | }; |
| 664 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 665 | } // namespace net |
| 666 | |
| 667 | #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |