| 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) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 ConnectJob* job) = 0; | 74 ConnectJob* job) = 0; |
| 75 | 75 |
| 76 private: | 76 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(Delegate); | 77 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 // A |timeout_duration| of 0 corresponds to no timeout. | 80 // A |timeout_duration| of 0 corresponds to no timeout. |
| 81 ConnectJob(const std::string& group_name, | 81 ConnectJob(const std::string& group_name, |
| 82 base::TimeDelta timeout_duration, | 82 base::TimeDelta timeout_duration, |
| 83 RequestPriority priority, | 83 RequestPriority priority, |
| 84 bool ignore_limits, |
| 84 Delegate* delegate, | 85 Delegate* delegate, |
| 85 const BoundNetLog& net_log); | 86 const BoundNetLog& net_log); |
| 86 virtual ~ConnectJob(); | 87 virtual ~ConnectJob(); |
| 87 | 88 |
| 88 // Accessors | 89 // Accessors |
| 89 const std::string& group_name() const { return group_name_; } | 90 const std::string& group_name() const { return group_name_; } |
| 90 const BoundNetLog& net_log() { return net_log_; } | 91 const BoundNetLog& net_log() { return net_log_; } |
| 91 | 92 |
| 92 // Releases ownership of the underlying socket to the caller. | 93 // Releases ownership of the underlying socket to the caller. |
| 93 // Returns the released socket, or NULL if there was a connection | 94 // Returns the released socket, or NULL if there was a connection |
| (...skipping 16 matching lines...) Expand all Loading... |
| 110 virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} | 111 virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} |
| 111 | 112 |
| 112 const LoadTimingInfo::ConnectTiming& connect_timing() const { | 113 const LoadTimingInfo::ConnectTiming& connect_timing() const { |
| 113 return connect_timing_; | 114 return connect_timing_; |
| 114 } | 115 } |
| 115 | 116 |
| 116 const BoundNetLog& net_log() const { return net_log_; } | 117 const BoundNetLog& net_log() const { return net_log_; } |
| 117 | 118 |
| 118 protected: | 119 protected: |
| 119 RequestPriority priority() const { return priority_; } | 120 RequestPriority priority() const { return priority_; } |
| 121 bool ignore_limits() const { return ignore_limits_; } |
| 120 void SetSocket(scoped_ptr<StreamSocket> socket); | 122 void SetSocket(scoped_ptr<StreamSocket> socket); |
| 121 StreamSocket* socket() { return socket_.get(); } | 123 StreamSocket* socket() { return socket_.get(); } |
| 122 void NotifyDelegateOfCompletion(int rv); | 124 void NotifyDelegateOfCompletion(int rv); |
| 123 void ResetTimer(base::TimeDelta remainingTime); | 125 void ResetTimer(base::TimeDelta remainingTime); |
| 124 | 126 |
| 125 // Connection establishment timing information. | 127 // Connection establishment timing information. |
| 126 LoadTimingInfo::ConnectTiming connect_timing_; | 128 LoadTimingInfo::ConnectTiming connect_timing_; |
| 127 | 129 |
| 128 private: | 130 private: |
| 129 virtual int ConnectInternal() = 0; | 131 virtual int ConnectInternal() = 0; |
| 130 | 132 |
| 131 void LogConnectStart(); | 133 void LogConnectStart(); |
| 132 void LogConnectCompletion(int net_error); | 134 void LogConnectCompletion(int net_error); |
| 133 | 135 |
| 134 // Alerts the delegate that the ConnectJob has timed out. | 136 // Alerts the delegate that the ConnectJob has timed out. |
| 135 void OnTimeout(); | 137 void OnTimeout(); |
| 136 | 138 |
| 137 const std::string group_name_; | 139 const std::string group_name_; |
| 138 const base::TimeDelta timeout_duration_; | 140 const base::TimeDelta timeout_duration_; |
| 139 // TODO(akalin): Support reprioritization. | 141 // TODO(akalin): Support reprioritization. |
| 140 const RequestPriority priority_; | 142 const RequestPriority priority_; |
| 143 const bool ignore_limits_; |
| 141 // Timer to abort jobs that take too long. | 144 // Timer to abort jobs that take too long. |
| 142 base::OneShotTimer timer_; | 145 base::OneShotTimer timer_; |
| 143 Delegate* delegate_; | 146 Delegate* delegate_; |
| 144 scoped_ptr<StreamSocket> socket_; | 147 scoped_ptr<StreamSocket> socket_; |
| 145 BoundNetLog net_log_; | 148 BoundNetLog net_log_; |
| 146 // A ConnectJob is idle until Connect() has been called. | 149 // A ConnectJob is idle until Connect() has been called. |
| 147 bool idle_; | 150 bool idle_; |
| 148 | 151 |
| 149 DISALLOW_COPY_AND_ASSIGN(ConnectJob); | 152 DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
| 150 }; | 153 }; |
| (...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 } // namespace internal | 676 } // namespace internal |
| 674 | 677 |
| 675 template <typename SocketParams> | 678 template <typename SocketParams> |
| 676 class ClientSocketPoolBase { | 679 class ClientSocketPoolBase { |
| 677 public: | 680 public: |
| 678 class Request : public internal::ClientSocketPoolBaseHelper::Request { | 681 class Request : public internal::ClientSocketPoolBaseHelper::Request { |
| 679 public: | 682 public: |
| 680 Request(ClientSocketHandle* handle, | 683 Request(ClientSocketHandle* handle, |
| 681 const CompletionCallback& callback, | 684 const CompletionCallback& callback, |
| 682 RequestPriority priority, | 685 RequestPriority priority, |
| 686 bool ignore_limits, |
| 683 internal::ClientSocketPoolBaseHelper::Flags flags, | 687 internal::ClientSocketPoolBaseHelper::Flags flags, |
| 684 bool ignore_limits, | |
| 685 const scoped_refptr<SocketParams>& params, | 688 const scoped_refptr<SocketParams>& params, |
| 686 const BoundNetLog& net_log) | 689 const BoundNetLog& net_log) |
| 687 : internal::ClientSocketPoolBaseHelper::Request( | 690 : internal::ClientSocketPoolBaseHelper::Request(handle, |
| 688 handle, callback, priority, ignore_limits, flags, net_log), | 691 callback, |
| 692 priority, |
| 693 ignore_limits, |
| 694 flags, |
| 695 net_log), |
| 689 params_(params) {} | 696 params_(params) {} |
| 690 | 697 |
| 691 const scoped_refptr<SocketParams>& params() const { return params_; } | 698 const scoped_refptr<SocketParams>& params() const { return params_; } |
| 692 | 699 |
| 693 private: | 700 private: |
| 694 const scoped_refptr<SocketParams> params_; | 701 const scoped_refptr<SocketParams> params_; |
| 695 }; | 702 }; |
| 696 | 703 |
| 697 class ConnectJobFactory { | 704 class ConnectJobFactory { |
| 698 public: | 705 public: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 | 749 |
| 743 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) { | 750 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) { |
| 744 helper_.RemoveHigherLayeredPool(higher_pool); | 751 helper_.RemoveHigherLayeredPool(higher_pool); |
| 745 } | 752 } |
| 746 | 753 |
| 747 // RequestSocket bundles up the parameters into a Request and then forwards to | 754 // RequestSocket bundles up the parameters into a Request and then forwards to |
| 748 // ClientSocketPoolBaseHelper::RequestSocket(). | 755 // ClientSocketPoolBaseHelper::RequestSocket(). |
| 749 int RequestSocket(const std::string& group_name, | 756 int RequestSocket(const std::string& group_name, |
| 750 const scoped_refptr<SocketParams>& params, | 757 const scoped_refptr<SocketParams>& params, |
| 751 RequestPriority priority, | 758 RequestPriority priority, |
| 759 bool ignore_limits, |
| 752 ClientSocketHandle* handle, | 760 ClientSocketHandle* handle, |
| 753 const CompletionCallback& callback, | 761 const CompletionCallback& callback, |
| 754 const BoundNetLog& net_log) { | 762 const BoundNetLog& net_log) { |
| 755 scoped_ptr<const Request> request( | 763 scoped_ptr<const Request> request(new Request( |
| 756 new Request(handle, callback, priority, | 764 handle, callback, priority, ignore_limits, |
| 757 internal::ClientSocketPoolBaseHelper::NORMAL, | 765 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); |
| 758 params->ignore_limits(), | |
| 759 params, net_log)); | |
| 760 return helper_.RequestSocket(group_name, std::move(request)); | 766 return helper_.RequestSocket(group_name, std::move(request)); |
| 761 } | 767 } |
| 762 | 768 |
| 763 // RequestSockets bundles up the parameters into a Request and then forwards | 769 // RequestSockets bundles up the parameters into a Request and then forwards |
| 764 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the | 770 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the |
| 765 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. | 771 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. |
| 766 void RequestSockets(const std::string& group_name, | 772 void RequestSockets(const std::string& group_name, |
| 767 const scoped_refptr<SocketParams>& params, | 773 const scoped_refptr<SocketParams>& params, |
| 768 int num_sockets, | 774 int num_sockets, |
| 769 const BoundNetLog& net_log) { | 775 const BoundNetLog& net_log) { |
| 770 const Request request(NULL /* no handle */, CompletionCallback(), IDLE, | 776 const Request request(NULL /* no handle */, CompletionCallback(), IDLE, |
| 777 false /* ignore_limits */, |
| 771 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, | 778 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, |
| 772 params->ignore_limits(), params, net_log); | 779 params, net_log); |
| 773 helper_.RequestSockets(group_name, request, num_sockets); | 780 helper_.RequestSockets(group_name, request, num_sockets); |
| 774 } | 781 } |
| 775 | 782 |
| 776 void CancelRequest(const std::string& group_name, | 783 void CancelRequest(const std::string& group_name, |
| 777 ClientSocketHandle* handle) { | 784 ClientSocketHandle* handle) { |
| 778 return helper_.CancelRequest(group_name, handle); | 785 return helper_.CancelRequest(group_name, handle); |
| 779 } | 786 } |
| 780 | 787 |
| 781 void ReleaseSocket(const std::string& group_name, | 788 void ReleaseSocket(const std::string& group_name, |
| 782 scoped_ptr<StreamSocket> socket, | 789 scoped_ptr<StreamSocket> socket, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 875 }; | 882 }; |
| 876 | 883 |
| 877 internal::ClientSocketPoolBaseHelper helper_; | 884 internal::ClientSocketPoolBaseHelper helper_; |
| 878 | 885 |
| 879 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 886 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 880 }; | 887 }; |
| 881 | 888 |
| 882 } // namespace net | 889 } // namespace net |
| 883 | 890 |
| 884 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 891 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| OLD | NEW |