| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // A ClientSocketPoolBase is used to restrict the number of sockets open at | 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. | 6 // a time. It also maintains a list of idle persistent sockets for reuse. |
| 7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle | 7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle |
| 8 // the core logic of (1) restricting the number of active (connected or | 8 // the core logic of (1) restricting the number of active (connected or |
| 9 // connecting) sockets per "group" (generally speaking, the hostname), (2) | 9 // connecting) sockets per "group" (generally speaking, the hostname), (2) |
| 10 // maintaining a per-group list of idle, persistent sockets for reuse, and (3) | 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. | 11 // limiting the total number of active sockets in the system. |
| 12 // | 12 // |
| 13 // ClientSocketPoolBase abstracts socket connection details behind ConnectJob, | 13 // ClientSocketPoolBase abstracts socket connection details behind ConnectJob, |
| 14 // ConnectJobFactory, and SocketParams. When a socket "slot" becomes available, | 14 // ConnectJobFactory, and SocketParams. When a socket "slot" becomes available, |
| 15 // the ClientSocketPoolBase will ask the ConnectJobFactory to create a | 15 // the ClientSocketPoolBase will ask the ConnectJobFactory to create a |
| 16 // ConnectJob with a SocketParams. Subclasses of ClientSocketPool should | 16 // ConnectJob with a SocketParams. Subclasses of ClientSocketPool should |
| 17 // implement their socket specific connection by subclassing ConnectJob and | 17 // implement their socket specific connection by subclassing ConnectJob and |
| 18 // implementing ConnectJob::ConnectInternal(). They can control the parameters | 18 // implementing ConnectJob::ConnectInternal(). They can control the parameters |
| 19 // passed to each new ConnectJob instance via their ConnectJobFactory subclass | 19 // passed to each new ConnectJob instance via their ConnectJobFactory subclass |
| 20 // and templated SocketParams parameter. | 20 // and templated SocketParams parameter. |
| 21 // | 21 // |
| 22 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 22 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 23 #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 23 #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 24 | 24 |
| 25 #include <stddef.h> | 25 #include <stddef.h> |
| 26 #include <stdint.h> | 26 #include <stdint.h> |
| 27 |
| 27 #include <cstddef> | 28 #include <cstddef> |
| 28 #include <deque> | 29 #include <deque> |
| 29 #include <list> | 30 #include <list> |
| 30 #include <map> | 31 #include <map> |
| 32 #include <memory> |
| 31 #include <set> | 33 #include <set> |
| 32 #include <string> | 34 #include <string> |
| 33 #include <utility> | 35 #include <utility> |
| 34 #include <vector> | 36 #include <vector> |
| 35 | 37 |
| 36 #include "base/macros.h" | 38 #include "base/macros.h" |
| 37 #include "base/memory/ref_counted.h" | 39 #include "base/memory/ref_counted.h" |
| 38 #include "base/memory/scoped_ptr.h" | |
| 39 #include "base/memory/weak_ptr.h" | 40 #include "base/memory/weak_ptr.h" |
| 40 #include "base/time/time.h" | 41 #include "base/time/time.h" |
| 41 #include "base/timer/timer.h" | 42 #include "base/timer/timer.h" |
| 42 #include "net/base/address_list.h" | 43 #include "net/base/address_list.h" |
| 43 #include "net/base/completion_callback.h" | 44 #include "net/base/completion_callback.h" |
| 44 #include "net/base/load_states.h" | 45 #include "net/base/load_states.h" |
| 45 #include "net/base/load_timing_info.h" | 46 #include "net/base/load_timing_info.h" |
| 46 #include "net/base/net_errors.h" | 47 #include "net/base/net_errors.h" |
| 47 #include "net/base/net_export.h" | 48 #include "net/base/net_export.h" |
| 48 #include "net/base/network_change_notifier.h" | 49 #include "net/base/network_change_notifier.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 61 // The connection may involve host resolution, tcp connection, ssl connection, | 62 // The connection may involve host resolution, tcp connection, ssl connection, |
| 62 // etc. | 63 // etc. |
| 63 class NET_EXPORT_PRIVATE ConnectJob { | 64 class NET_EXPORT_PRIVATE ConnectJob { |
| 64 public: | 65 public: |
| 65 class NET_EXPORT_PRIVATE Delegate { | 66 class NET_EXPORT_PRIVATE Delegate { |
| 66 public: | 67 public: |
| 67 Delegate() {} | 68 Delegate() {} |
| 68 virtual ~Delegate() {} | 69 virtual ~Delegate() {} |
| 69 | 70 |
| 70 // Alerts the delegate that the connection completed. |job| must | 71 // Alerts the delegate that the connection completed. |job| must |
| 71 // be destroyed by the delegate. A scoped_ptr<> isn't used because | 72 // be destroyed by the delegate. A std::unique_ptr<> isn't used because |
| 72 // the caller of this function doesn't own |job|. | 73 // the caller of this function doesn't own |job|. |
| 73 virtual void OnConnectJobComplete(int result, | 74 virtual void OnConnectJobComplete(int result, |
| 74 ConnectJob* job) = 0; | 75 ConnectJob* job) = 0; |
| 75 | 76 |
| 76 private: | 77 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(Delegate); | 78 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 // A |timeout_duration| of 0 corresponds to no timeout. | 81 // A |timeout_duration| of 0 corresponds to no timeout. |
| 81 ConnectJob(const std::string& group_name, | 82 ConnectJob(const std::string& group_name, |
| 82 base::TimeDelta timeout_duration, | 83 base::TimeDelta timeout_duration, |
| 83 RequestPriority priority, | 84 RequestPriority priority, |
| 84 ClientSocketPool::RespectLimits respect_limits, | 85 ClientSocketPool::RespectLimits respect_limits, |
| 85 Delegate* delegate, | 86 Delegate* delegate, |
| 86 const BoundNetLog& net_log); | 87 const BoundNetLog& net_log); |
| 87 virtual ~ConnectJob(); | 88 virtual ~ConnectJob(); |
| 88 | 89 |
| 89 // Accessors | 90 // Accessors |
| 90 const std::string& group_name() const { return group_name_; } | 91 const std::string& group_name() const { return group_name_; } |
| 91 const BoundNetLog& net_log() { return net_log_; } | 92 const BoundNetLog& net_log() { return net_log_; } |
| 92 | 93 |
| 93 // Releases ownership of the underlying socket to the caller. | 94 // Releases ownership of the underlying socket to the caller. |
| 94 // Returns the released socket, or NULL if there was a connection | 95 // Returns the released socket, or NULL if there was a connection |
| 95 // error. | 96 // error. |
| 96 scoped_ptr<StreamSocket> PassSocket(); | 97 std::unique_ptr<StreamSocket> PassSocket(); |
| 97 | 98 |
| 98 // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it | 99 // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it |
| 99 // cannot complete synchronously without blocking, or another net error code | 100 // cannot complete synchronously without blocking, or another net error code |
| 100 // on error. In asynchronous completion, the ConnectJob will notify | 101 // on error. In asynchronous completion, the ConnectJob will notify |
| 101 // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous | 102 // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous |
| 102 // completion, ReleaseSocket() can be called to acquire the connected socket | 103 // completion, ReleaseSocket() can be called to acquire the connected socket |
| 103 // if it succeeded. | 104 // if it succeeded. |
| 104 int Connect(); | 105 int Connect(); |
| 105 | 106 |
| 106 virtual LoadState GetLoadState() const = 0; | 107 virtual LoadState GetLoadState() const = 0; |
| 107 | 108 |
| 108 // If Connect returns an error (or OnConnectJobComplete reports an error | 109 // If Connect returns an error (or OnConnectJobComplete reports an error |
| 109 // result) this method will be called, allowing the pool to add | 110 // result) this method will be called, allowing the pool to add |
| 110 // additional error state to the ClientSocketHandle (post late-binding). | 111 // additional error state to the ClientSocketHandle (post late-binding). |
| 111 virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} | 112 virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} |
| 112 | 113 |
| 113 const LoadTimingInfo::ConnectTiming& connect_timing() const { | 114 const LoadTimingInfo::ConnectTiming& connect_timing() const { |
| 114 return connect_timing_; | 115 return connect_timing_; |
| 115 } | 116 } |
| 116 | 117 |
| 117 const BoundNetLog& net_log() const { return net_log_; } | 118 const BoundNetLog& net_log() const { return net_log_; } |
| 118 | 119 |
| 119 protected: | 120 protected: |
| 120 RequestPriority priority() const { return priority_; } | 121 RequestPriority priority() const { return priority_; } |
| 121 ClientSocketPool::RespectLimits respect_limits() const { | 122 ClientSocketPool::RespectLimits respect_limits() const { |
| 122 return respect_limits_; | 123 return respect_limits_; |
| 123 } | 124 } |
| 124 void SetSocket(scoped_ptr<StreamSocket> socket); | 125 void SetSocket(std::unique_ptr<StreamSocket> socket); |
| 125 StreamSocket* socket() { return socket_.get(); } | 126 StreamSocket* socket() { return socket_.get(); } |
| 126 void NotifyDelegateOfCompletion(int rv); | 127 void NotifyDelegateOfCompletion(int rv); |
| 127 void ResetTimer(base::TimeDelta remainingTime); | 128 void ResetTimer(base::TimeDelta remainingTime); |
| 128 | 129 |
| 129 // Connection establishment timing information. | 130 // Connection establishment timing information. |
| 130 LoadTimingInfo::ConnectTiming connect_timing_; | 131 LoadTimingInfo::ConnectTiming connect_timing_; |
| 131 | 132 |
| 132 private: | 133 private: |
| 133 virtual int ConnectInternal() = 0; | 134 virtual int ConnectInternal() = 0; |
| 134 | 135 |
| 135 void LogConnectStart(); | 136 void LogConnectStart(); |
| 136 void LogConnectCompletion(int net_error); | 137 void LogConnectCompletion(int net_error); |
| 137 | 138 |
| 138 // Alerts the delegate that the ConnectJob has timed out. | 139 // Alerts the delegate that the ConnectJob has timed out. |
| 139 void OnTimeout(); | 140 void OnTimeout(); |
| 140 | 141 |
| 141 const std::string group_name_; | 142 const std::string group_name_; |
| 142 const base::TimeDelta timeout_duration_; | 143 const base::TimeDelta timeout_duration_; |
| 143 // TODO(akalin): Support reprioritization. | 144 // TODO(akalin): Support reprioritization. |
| 144 const RequestPriority priority_; | 145 const RequestPriority priority_; |
| 145 const ClientSocketPool::RespectLimits respect_limits_; | 146 const ClientSocketPool::RespectLimits respect_limits_; |
| 146 // Timer to abort jobs that take too long. | 147 // Timer to abort jobs that take too long. |
| 147 base::OneShotTimer timer_; | 148 base::OneShotTimer timer_; |
| 148 Delegate* delegate_; | 149 Delegate* delegate_; |
| 149 scoped_ptr<StreamSocket> socket_; | 150 std::unique_ptr<StreamSocket> socket_; |
| 150 BoundNetLog net_log_; | 151 BoundNetLog net_log_; |
| 151 // A ConnectJob is idle until Connect() has been called. | 152 // A ConnectJob is idle until Connect() has been called. |
| 152 bool idle_; | 153 bool idle_; |
| 153 | 154 |
| 154 DISALLOW_COPY_AND_ASSIGN(ConnectJob); | 155 DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
| 155 }; | 156 }; |
| 156 | 157 |
| 157 namespace internal { | 158 namespace internal { |
| 158 | 159 |
| 159 // ClientSocketPoolBaseHelper is an internal class that implements almost all | 160 // ClientSocketPoolBaseHelper is an internal class that implements almost all |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 Liveness liveness_ = ALIVE; | 216 Liveness liveness_ = ALIVE; |
| 216 | 217 |
| 217 DISALLOW_COPY_AND_ASSIGN(Request); | 218 DISALLOW_COPY_AND_ASSIGN(Request); |
| 218 }; | 219 }; |
| 219 | 220 |
| 220 class ConnectJobFactory { | 221 class ConnectJobFactory { |
| 221 public: | 222 public: |
| 222 ConnectJobFactory() {} | 223 ConnectJobFactory() {} |
| 223 virtual ~ConnectJobFactory() {} | 224 virtual ~ConnectJobFactory() {} |
| 224 | 225 |
| 225 virtual scoped_ptr<ConnectJob> NewConnectJob( | 226 virtual std::unique_ptr<ConnectJob> NewConnectJob( |
| 226 const std::string& group_name, | 227 const std::string& group_name, |
| 227 const Request& request, | 228 const Request& request, |
| 228 ConnectJob::Delegate* delegate) const = 0; | 229 ConnectJob::Delegate* delegate) const = 0; |
| 229 | 230 |
| 230 virtual base::TimeDelta ConnectionTimeout() const = 0; | 231 virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 231 | 232 |
| 232 private: | 233 private: |
| 233 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); | 234 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 234 }; | 235 }; |
| 235 | 236 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 250 // See LowerLayeredPool::IsStalled for documentation on this function. | 251 // See LowerLayeredPool::IsStalled for documentation on this function. |
| 251 bool IsStalled() const; | 252 bool IsStalled() const; |
| 252 | 253 |
| 253 // See LowerLayeredPool for documentation on these functions. It is expected | 254 // See LowerLayeredPool for documentation on these functions. It is expected |
| 254 // in the destructor that no higher layer pools remain. | 255 // in the destructor that no higher layer pools remain. |
| 255 void AddHigherLayeredPool(HigherLayeredPool* higher_pool); | 256 void AddHigherLayeredPool(HigherLayeredPool* higher_pool); |
| 256 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); | 257 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); |
| 257 | 258 |
| 258 // See ClientSocketPool::RequestSocket for documentation on this function. | 259 // See ClientSocketPool::RequestSocket for documentation on this function. |
| 259 int RequestSocket(const std::string& group_name, | 260 int RequestSocket(const std::string& group_name, |
| 260 scoped_ptr<const Request> request); | 261 std::unique_ptr<const Request> request); |
| 261 | 262 |
| 262 // See ClientSocketPool::RequestSocket for documentation on this function. | 263 // See ClientSocketPool::RequestSocket for documentation on this function. |
| 263 void RequestSockets(const std::string& group_name, | 264 void RequestSockets(const std::string& group_name, |
| 264 const Request& request, | 265 const Request& request, |
| 265 int num_sockets); | 266 int num_sockets); |
| 266 | 267 |
| 267 // See ClientSocketPool::CancelRequest for documentation on this function. | 268 // See ClientSocketPool::CancelRequest for documentation on this function. |
| 268 void CancelRequest(const std::string& group_name, | 269 void CancelRequest(const std::string& group_name, |
| 269 ClientSocketHandle* handle); | 270 ClientSocketHandle* handle); |
| 270 | 271 |
| 271 // See ClientSocketPool::ReleaseSocket for documentation on this function. | 272 // See ClientSocketPool::ReleaseSocket for documentation on this function. |
| 272 void ReleaseSocket(const std::string& group_name, | 273 void ReleaseSocket(const std::string& group_name, |
| 273 scoped_ptr<StreamSocket> socket, | 274 std::unique_ptr<StreamSocket> socket, |
| 274 int id); | 275 int id); |
| 275 | 276 |
| 276 // See ClientSocketPool::FlushWithError for documentation on this function. | 277 // See ClientSocketPool::FlushWithError for documentation on this function. |
| 277 void FlushWithError(int error); | 278 void FlushWithError(int error); |
| 278 | 279 |
| 279 // See ClientSocketPool::CloseIdleSockets for documentation on this function. | 280 // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
| 280 void CloseIdleSockets(); | 281 void CloseIdleSockets(); |
| 281 | 282 |
| 282 // See ClientSocketPool::IdleSocketCount() for documentation on this function. | 283 // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
| 283 int idle_socket_count() const { | 284 int idle_socket_count() const { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we | 330 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we |
| 330 // should keep an ordered list of idle sockets, and close them in order. | 331 // should keep an ordered list of idle sockets, and close them in order. |
| 331 // Requires maintaining more state. It's not clear if it's worth it since | 332 // Requires maintaining more state. It's not clear if it's worth it since |
| 332 // I'm not sure if we hit this situation often. | 333 // I'm not sure if we hit this situation often. |
| 333 bool CloseOneIdleSocket(); | 334 bool CloseOneIdleSocket(); |
| 334 | 335 |
| 335 // Checks higher layered pools to see if they can close an idle connection. | 336 // Checks higher layered pools to see if they can close an idle connection. |
| 336 bool CloseOneIdleConnectionInHigherLayeredPool(); | 337 bool CloseOneIdleConnectionInHigherLayeredPool(); |
| 337 | 338 |
| 338 // See ClientSocketPool::GetInfoAsValue for documentation on this function. | 339 // See ClientSocketPool::GetInfoAsValue for documentation on this function. |
| 339 scoped_ptr<base::DictionaryValue> GetInfoAsValue( | 340 std::unique_ptr<base::DictionaryValue> GetInfoAsValue( |
| 340 const std::string& name, | 341 const std::string& name, |
| 341 const std::string& type) const; | 342 const std::string& type) const; |
| 342 | 343 |
| 343 base::TimeDelta ConnectionTimeout() const { | 344 base::TimeDelta ConnectionTimeout() const { |
| 344 return connect_job_factory_->ConnectionTimeout(); | 345 return connect_job_factory_->ConnectionTimeout(); |
| 345 } | 346 } |
| 346 | 347 |
| 347 static bool connect_backup_jobs_enabled(); | 348 static bool connect_backup_jobs_enabled(); |
| 348 static bool set_connect_backup_jobs_enabled(bool enabled); | 349 static bool set_connect_backup_jobs_enabled(bool enabled); |
| 349 | 350 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 void StartBackupJobTimer(const std::string& group_name, | 427 void StartBackupJobTimer(const std::string& group_name, |
| 427 ClientSocketPoolBaseHelper* pool); | 428 ClientSocketPoolBaseHelper* pool); |
| 428 | 429 |
| 429 bool BackupJobTimerIsRunning() const; | 430 bool BackupJobTimerIsRunning() const; |
| 430 | 431 |
| 431 // If there's a ConnectJob that's never been assigned to Request, | 432 // If there's a ConnectJob that's never been assigned to Request, |
| 432 // decrements |unassigned_job_count_| and returns true. | 433 // decrements |unassigned_job_count_| and returns true. |
| 433 // Otherwise, returns false. | 434 // Otherwise, returns false. |
| 434 bool TryToUseUnassignedConnectJob(); | 435 bool TryToUseUnassignedConnectJob(); |
| 435 | 436 |
| 436 void AddJob(scoped_ptr<ConnectJob> job, bool is_preconnect); | 437 void AddJob(std::unique_ptr<ConnectJob> job, bool is_preconnect); |
| 437 // Remove |job| from this group, which must already own |job|. | 438 // Remove |job| from this group, which must already own |job|. |
| 438 void RemoveJob(ConnectJob* job); | 439 void RemoveJob(ConnectJob* job); |
| 439 void RemoveAllJobs(); | 440 void RemoveAllJobs(); |
| 440 | 441 |
| 441 bool has_pending_requests() const { | 442 bool has_pending_requests() const { |
| 442 return !pending_requests_.empty(); | 443 return !pending_requests_.empty(); |
| 443 } | 444 } |
| 444 | 445 |
| 445 size_t pending_request_count() const { | 446 size_t pending_request_count() const { |
| 446 return pending_requests_.size(); | 447 return pending_requests_.size(); |
| 447 } | 448 } |
| 448 | 449 |
| 449 // Gets (but does not remove) the next pending request. Returns | 450 // Gets (but does not remove) the next pending request. Returns |
| 450 // NULL if there are no pending requests. | 451 // NULL if there are no pending requests. |
| 451 const Request* GetNextPendingRequest() const; | 452 const Request* GetNextPendingRequest() const; |
| 452 | 453 |
| 453 // Returns true if there is a connect job for |handle|. | 454 // Returns true if there is a connect job for |handle|. |
| 454 bool HasConnectJobForHandle(const ClientSocketHandle* handle) const; | 455 bool HasConnectJobForHandle(const ClientSocketHandle* handle) const; |
| 455 | 456 |
| 456 // Inserts the request into the queue based on priority | 457 // Inserts the request into the queue based on priority |
| 457 // order. Older requests are prioritized over requests of equal | 458 // order. Older requests are prioritized over requests of equal |
| 458 // priority. | 459 // priority. |
| 459 void InsertPendingRequest(scoped_ptr<const Request> request); | 460 void InsertPendingRequest(std::unique_ptr<const Request> request); |
| 460 | 461 |
| 461 // Gets and removes the next pending request. Returns NULL if | 462 // Gets and removes the next pending request. Returns NULL if |
| 462 // there are no pending requests. | 463 // there are no pending requests. |
| 463 scoped_ptr<const Request> PopNextPendingRequest(); | 464 std::unique_ptr<const Request> PopNextPendingRequest(); |
| 464 | 465 |
| 465 // Finds the pending request for |handle| and removes it. Returns | 466 // Finds the pending request for |handle| and removes it. Returns |
| 466 // the removed pending request, or NULL if there was none. | 467 // the removed pending request, or NULL if there was none. |
| 467 scoped_ptr<const Request> FindAndRemovePendingRequest( | 468 std::unique_ptr<const Request> FindAndRemovePendingRequest( |
| 468 ClientSocketHandle* handle); | 469 ClientSocketHandle* handle); |
| 469 | 470 |
| 470 void IncrementActiveSocketCount() { active_socket_count_++; } | 471 void IncrementActiveSocketCount() { active_socket_count_++; } |
| 471 void DecrementActiveSocketCount() { active_socket_count_--; } | 472 void DecrementActiveSocketCount() { active_socket_count_--; } |
| 472 | 473 |
| 473 int unassigned_job_count() const { return unassigned_job_count_; } | 474 int unassigned_job_count() const { return unassigned_job_count_; } |
| 474 const std::list<ConnectJob*>& jobs() const { return jobs_; } | 475 const std::list<ConnectJob*>& jobs() const { return jobs_; } |
| 475 const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; } | 476 const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; } |
| 476 int active_socket_count() const { return active_socket_count_; } | 477 int active_socket_count() const { return active_socket_count_; } |
| 477 std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; } | 478 std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; } |
| 478 | 479 |
| 479 private: | 480 private: |
| 480 // Returns the iterator's pending request after removing it from | 481 // Returns the iterator's pending request after removing it from |
| 481 // the queue. | 482 // the queue. |
| 482 scoped_ptr<const Request> RemovePendingRequest( | 483 std::unique_ptr<const Request> RemovePendingRequest( |
| 483 const RequestQueue::Pointer& pointer); | 484 const RequestQueue::Pointer& pointer); |
| 484 | 485 |
| 485 // Called when the backup socket timer fires. | 486 // Called when the backup socket timer fires. |
| 486 void OnBackupJobTimerFired( | 487 void OnBackupJobTimerFired( |
| 487 std::string group_name, | 488 std::string group_name, |
| 488 ClientSocketPoolBaseHelper* pool); | 489 ClientSocketPoolBaseHelper* pool); |
| 489 | 490 |
| 490 // Checks that |unassigned_job_count_| does not execeed the number of | 491 // Checks that |unassigned_job_count_| does not execeed the number of |
| 491 // ConnectJobs. | 492 // ConnectJobs. |
| 492 void SanityCheck(); | 493 void SanityCheck(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 // Removes |job| from |group|, which must already own |job|. | 551 // Removes |job| from |group|, which must already own |job|. |
| 551 void RemoveConnectJob(ConnectJob* job, Group* group); | 552 void RemoveConnectJob(ConnectJob* job, Group* group); |
| 552 | 553 |
| 553 // Tries to see if we can handle any more requests for |group|. | 554 // Tries to see if we can handle any more requests for |group|. |
| 554 void OnAvailableSocketSlot(const std::string& group_name, Group* group); | 555 void OnAvailableSocketSlot(const std::string& group_name, Group* group); |
| 555 | 556 |
| 556 // Process a pending socket request for a group. | 557 // Process a pending socket request for a group. |
| 557 void ProcessPendingRequest(const std::string& group_name, Group* group); | 558 void ProcessPendingRequest(const std::string& group_name, Group* group); |
| 558 | 559 |
| 559 // Assigns |socket| to |handle| and updates |group|'s counters appropriately. | 560 // Assigns |socket| to |handle| and updates |group|'s counters appropriately. |
| 560 void HandOutSocket(scoped_ptr<StreamSocket> socket, | 561 void HandOutSocket(std::unique_ptr<StreamSocket> socket, |
| 561 ClientSocketHandle::SocketReuseType reuse_type, | 562 ClientSocketHandle::SocketReuseType reuse_type, |
| 562 const LoadTimingInfo::ConnectTiming& connect_timing, | 563 const LoadTimingInfo::ConnectTiming& connect_timing, |
| 563 ClientSocketHandle* handle, | 564 ClientSocketHandle* handle, |
| 564 base::TimeDelta time_idle, | 565 base::TimeDelta time_idle, |
| 565 Group* group, | 566 Group* group, |
| 566 const BoundNetLog& net_log); | 567 const BoundNetLog& net_log); |
| 567 | 568 |
| 568 // Adds |socket| to the list of idle sockets for |group|. | 569 // Adds |socket| to the list of idle sockets for |group|. |
| 569 void AddIdleSocket(scoped_ptr<StreamSocket> socket, Group* group); | 570 void AddIdleSocket(std::unique_ptr<StreamSocket> socket, Group* group); |
| 570 | 571 |
| 571 // Iterates through |group_map_|, canceling all ConnectJobs and deleting | 572 // Iterates through |group_map_|, canceling all ConnectJobs and deleting |
| 572 // groups if they are no longer needed. | 573 // groups if they are no longer needed. |
| 573 void CancelAllConnectJobs(); | 574 void CancelAllConnectJobs(); |
| 574 | 575 |
| 575 // Iterates through |group_map_|, posting |error| callbacks for all | 576 // Iterates through |group_map_|, posting |error| callbacks for all |
| 576 // requests, and then deleting groups if they are no longer needed. | 577 // requests, and then deleting groups if they are no longer needed. |
| 577 void CancelAllRequestsWithError(int error); | 578 void CancelAllRequestsWithError(int error); |
| 578 | 579 |
| 579 // Returns true if we can't create any more sockets due to the total limit. | 580 // Returns true if we can't create any more sockets due to the total limit. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 // The maximum number of sockets kept per group. | 644 // The maximum number of sockets kept per group. |
| 644 const int max_sockets_per_group_; | 645 const int max_sockets_per_group_; |
| 645 | 646 |
| 646 // Whether to use timer to cleanup idle sockets. | 647 // Whether to use timer to cleanup idle sockets. |
| 647 bool use_cleanup_timer_; | 648 bool use_cleanup_timer_; |
| 648 | 649 |
| 649 // The time to wait until closing idle sockets. | 650 // The time to wait until closing idle sockets. |
| 650 const base::TimeDelta unused_idle_socket_timeout_; | 651 const base::TimeDelta unused_idle_socket_timeout_; |
| 651 const base::TimeDelta used_idle_socket_timeout_; | 652 const base::TimeDelta used_idle_socket_timeout_; |
| 652 | 653 |
| 653 const scoped_ptr<ConnectJobFactory> connect_job_factory_; | 654 const std::unique_ptr<ConnectJobFactory> connect_job_factory_; |
| 654 | 655 |
| 655 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool | 656 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool |
| 656 bool connect_backup_jobs_enabled_; | 657 bool connect_backup_jobs_enabled_; |
| 657 | 658 |
| 658 // A unique id for the pool. It gets incremented every time we | 659 // A unique id for the pool. It gets incremented every time we |
| 659 // FlushWithError() the pool. This is so that when sockets get released back | 660 // FlushWithError() the pool. This is so that when sockets get released back |
| 660 // to the pool, we can make sure that they are discarded rather than reused. | 661 // to the pool, we can make sure that they are discarded rather than reused. |
| 661 int pool_generation_number_; | 662 int pool_generation_number_; |
| 662 | 663 |
| 663 // Used to add |this| as a higher layer pool on top of lower layer pools. May | 664 // Used to add |this| as a higher layer pool on top of lower layer pools. May |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 | 705 |
| 705 private: | 706 private: |
| 706 const scoped_refptr<SocketParams> params_; | 707 const scoped_refptr<SocketParams> params_; |
| 707 }; | 708 }; |
| 708 | 709 |
| 709 class ConnectJobFactory { | 710 class ConnectJobFactory { |
| 710 public: | 711 public: |
| 711 ConnectJobFactory() {} | 712 ConnectJobFactory() {} |
| 712 virtual ~ConnectJobFactory() {} | 713 virtual ~ConnectJobFactory() {} |
| 713 | 714 |
| 714 virtual scoped_ptr<ConnectJob> NewConnectJob( | 715 virtual std::unique_ptr<ConnectJob> NewConnectJob( |
| 715 const std::string& group_name, | 716 const std::string& group_name, |
| 716 const Request& request, | 717 const Request& request, |
| 717 ConnectJob::Delegate* delegate) const = 0; | 718 ConnectJob::Delegate* delegate) const = 0; |
| 718 | 719 |
| 719 virtual base::TimeDelta ConnectionTimeout() const = 0; | 720 virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 720 | 721 |
| 721 private: | 722 private: |
| 722 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); | 723 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 723 }; | 724 }; |
| 724 | 725 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 | 759 |
| 759 // RequestSocket bundles up the parameters into a Request and then forwards to | 760 // RequestSocket bundles up the parameters into a Request and then forwards to |
| 760 // ClientSocketPoolBaseHelper::RequestSocket(). | 761 // ClientSocketPoolBaseHelper::RequestSocket(). |
| 761 int RequestSocket(const std::string& group_name, | 762 int RequestSocket(const std::string& group_name, |
| 762 const scoped_refptr<SocketParams>& params, | 763 const scoped_refptr<SocketParams>& params, |
| 763 RequestPriority priority, | 764 RequestPriority priority, |
| 764 ClientSocketPool::RespectLimits respect_limits, | 765 ClientSocketPool::RespectLimits respect_limits, |
| 765 ClientSocketHandle* handle, | 766 ClientSocketHandle* handle, |
| 766 const CompletionCallback& callback, | 767 const CompletionCallback& callback, |
| 767 const BoundNetLog& net_log) { | 768 const BoundNetLog& net_log) { |
| 768 scoped_ptr<const Request> request(new Request( | 769 std::unique_ptr<const Request> request(new Request( |
| 769 handle, callback, priority, respect_limits, | 770 handle, callback, priority, respect_limits, |
| 770 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); | 771 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); |
| 771 return helper_.RequestSocket(group_name, std::move(request)); | 772 return helper_.RequestSocket(group_name, std::move(request)); |
| 772 } | 773 } |
| 773 | 774 |
| 774 // RequestSockets bundles up the parameters into a Request and then forwards | 775 // RequestSockets bundles up the parameters into a Request and then forwards |
| 775 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the | 776 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the |
| 776 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. | 777 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. |
| 777 void RequestSockets(const std::string& group_name, | 778 void RequestSockets(const std::string& group_name, |
| 778 const scoped_refptr<SocketParams>& params, | 779 const scoped_refptr<SocketParams>& params, |
| 779 int num_sockets, | 780 int num_sockets, |
| 780 const BoundNetLog& net_log) { | 781 const BoundNetLog& net_log) { |
| 781 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE, | 782 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE, |
| 782 ClientSocketPool::RespectLimits::ENABLED, | 783 ClientSocketPool::RespectLimits::ENABLED, |
| 783 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, | 784 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, |
| 784 params, net_log); | 785 params, net_log); |
| 785 helper_.RequestSockets(group_name, request, num_sockets); | 786 helper_.RequestSockets(group_name, request, num_sockets); |
| 786 } | 787 } |
| 787 | 788 |
| 788 void CancelRequest(const std::string& group_name, | 789 void CancelRequest(const std::string& group_name, |
| 789 ClientSocketHandle* handle) { | 790 ClientSocketHandle* handle) { |
| 790 return helper_.CancelRequest(group_name, handle); | 791 return helper_.CancelRequest(group_name, handle); |
| 791 } | 792 } |
| 792 | 793 |
| 793 void ReleaseSocket(const std::string& group_name, | 794 void ReleaseSocket(const std::string& group_name, |
| 794 scoped_ptr<StreamSocket> socket, | 795 std::unique_ptr<StreamSocket> socket, |
| 795 int id) { | 796 int id) { |
| 796 return helper_.ReleaseSocket(group_name, std::move(socket), id); | 797 return helper_.ReleaseSocket(group_name, std::move(socket), id); |
| 797 } | 798 } |
| 798 | 799 |
| 799 void FlushWithError(int error) { helper_.FlushWithError(error); } | 800 void FlushWithError(int error) { helper_.FlushWithError(error); } |
| 800 | 801 |
| 801 bool IsStalled() const { return helper_.IsStalled(); } | 802 bool IsStalled() const { return helper_.IsStalled(); } |
| 802 | 803 |
| 803 void CloseIdleSockets() { return helper_.CloseIdleSockets(); } | 804 void CloseIdleSockets() { return helper_.CloseIdleSockets(); } |
| 804 | 805 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 830 } | 831 } |
| 831 | 832 |
| 832 bool HasGroup(const std::string& group_name) const { | 833 bool HasGroup(const std::string& group_name) const { |
| 833 return helper_.HasGroup(group_name); | 834 return helper_.HasGroup(group_name); |
| 834 } | 835 } |
| 835 | 836 |
| 836 void CleanupIdleSockets(bool force) { | 837 void CleanupIdleSockets(bool force) { |
| 837 return helper_.CleanupIdleSockets(force); | 838 return helper_.CleanupIdleSockets(force); |
| 838 } | 839 } |
| 839 | 840 |
| 840 scoped_ptr<base::DictionaryValue> GetInfoAsValue(const std::string& name, | 841 std::unique_ptr<base::DictionaryValue> GetInfoAsValue( |
| 841 const std::string& type) const { | 842 const std::string& name, |
| 843 const std::string& type) const { |
| 842 return helper_.GetInfoAsValue(name, type); | 844 return helper_.GetInfoAsValue(name, type); |
| 843 } | 845 } |
| 844 | 846 |
| 845 base::TimeDelta ConnectionTimeout() const { | 847 base::TimeDelta ConnectionTimeout() const { |
| 846 return helper_.ConnectionTimeout(); | 848 return helper_.ConnectionTimeout(); |
| 847 } | 849 } |
| 848 | 850 |
| 849 void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } | 851 void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } |
| 850 | 852 |
| 851 bool CloseOneIdleSocket() { return helper_.CloseOneIdleSocket(); } | 853 bool CloseOneIdleSocket() { return helper_.CloseOneIdleSocket(); } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 863 class ConnectJobFactoryAdaptor | 865 class ConnectJobFactoryAdaptor |
| 864 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { | 866 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
| 865 public: | 867 public: |
| 866 typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory | 868 typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory |
| 867 ConnectJobFactory; | 869 ConnectJobFactory; |
| 868 | 870 |
| 869 explicit ConnectJobFactoryAdaptor(ConnectJobFactory* connect_job_factory) | 871 explicit ConnectJobFactoryAdaptor(ConnectJobFactory* connect_job_factory) |
| 870 : connect_job_factory_(connect_job_factory) {} | 872 : connect_job_factory_(connect_job_factory) {} |
| 871 ~ConnectJobFactoryAdaptor() override {} | 873 ~ConnectJobFactoryAdaptor() override {} |
| 872 | 874 |
| 873 scoped_ptr<ConnectJob> NewConnectJob( | 875 std::unique_ptr<ConnectJob> NewConnectJob( |
| 874 const std::string& group_name, | 876 const std::string& group_name, |
| 875 const internal::ClientSocketPoolBaseHelper::Request& request, | 877 const internal::ClientSocketPoolBaseHelper::Request& request, |
| 876 ConnectJob::Delegate* delegate) const override { | 878 ConnectJob::Delegate* delegate) const override { |
| 877 const Request& casted_request = static_cast<const Request&>(request); | 879 const Request& casted_request = static_cast<const Request&>(request); |
| 878 return connect_job_factory_->NewConnectJob( | 880 return connect_job_factory_->NewConnectJob( |
| 879 group_name, casted_request, delegate); | 881 group_name, casted_request, delegate); |
| 880 } | 882 } |
| 881 | 883 |
| 882 base::TimeDelta ConnectionTimeout() const override { | 884 base::TimeDelta ConnectionTimeout() const override { |
| 883 return connect_job_factory_->ConnectionTimeout(); | 885 return connect_job_factory_->ConnectionTimeout(); |
| 884 } | 886 } |
| 885 | 887 |
| 886 const scoped_ptr<ConnectJobFactory> connect_job_factory_; | 888 const std::unique_ptr<ConnectJobFactory> connect_job_factory_; |
| 887 }; | 889 }; |
| 888 | 890 |
| 889 internal::ClientSocketPoolBaseHelper helper_; | 891 internal::ClientSocketPoolBaseHelper helper_; |
| 890 | 892 |
| 891 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 893 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 892 }; | 894 }; |
| 893 | 895 |
| 894 } // namespace net | 896 } // namespace net |
| 895 | 897 |
| 896 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 898 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| OLD | NEW |