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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 private: | 77 private: |
78 DISALLOW_COPY_AND_ASSIGN(Delegate); | 78 DISALLOW_COPY_AND_ASSIGN(Delegate); |
79 }; | 79 }; |
80 | 80 |
81 // A |timeout_duration| of 0 corresponds to no timeout. | 81 // A |timeout_duration| of 0 corresponds to no timeout. |
82 ConnectJob(const std::string& group_name, | 82 ConnectJob(const std::string& group_name, |
83 base::TimeDelta timeout_duration, | 83 base::TimeDelta timeout_duration, |
84 RequestPriority priority, | 84 RequestPriority priority, |
85 ClientSocketPool::RespectLimits respect_limits, | 85 ClientSocketPool::RespectLimits respect_limits, |
86 Delegate* delegate, | 86 Delegate* delegate, |
87 const BoundNetLog& net_log); | 87 const NetLogWithSource& net_log); |
88 virtual ~ConnectJob(); | 88 virtual ~ConnectJob(); |
89 | 89 |
90 // Accessors | 90 // Accessors |
91 const std::string& group_name() const { return group_name_; } | 91 const std::string& group_name() const { return group_name_; } |
92 const BoundNetLog& net_log() { return net_log_; } | 92 const NetLogWithSource& net_log() { return net_log_; } |
93 | 93 |
94 // Releases ownership of the underlying socket to the caller. | 94 // Releases ownership of the underlying socket to the caller. |
95 // Returns the released socket, or NULL if there was a connection | 95 // Returns the released socket, or NULL if there was a connection |
96 // error. | 96 // error. |
97 std::unique_ptr<StreamSocket> PassSocket(); | 97 std::unique_ptr<StreamSocket> PassSocket(); |
98 | 98 |
99 // 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 |
100 // cannot complete synchronously without blocking, or another net error code | 100 // cannot complete synchronously without blocking, or another net error code |
101 // on error. In asynchronous completion, the ConnectJob will notify | 101 // on error. In asynchronous completion, the ConnectJob will notify |
102 // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous | 102 // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous |
103 // completion, ReleaseSocket() can be called to acquire the connected socket | 103 // completion, ReleaseSocket() can be called to acquire the connected socket |
104 // if it succeeded. | 104 // if it succeeded. |
105 int Connect(); | 105 int Connect(); |
106 | 106 |
107 virtual LoadState GetLoadState() const = 0; | 107 virtual LoadState GetLoadState() const = 0; |
108 | 108 |
109 // If Connect returns an error (or OnConnectJobComplete reports an error | 109 // If Connect returns an error (or OnConnectJobComplete reports an error |
110 // result) this method will be called, allowing the pool to add | 110 // result) this method will be called, allowing the pool to add |
111 // additional error state to the ClientSocketHandle (post late-binding). | 111 // additional error state to the ClientSocketHandle (post late-binding). |
112 virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} | 112 virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} |
113 | 113 |
114 const LoadTimingInfo::ConnectTiming& connect_timing() const { | 114 const LoadTimingInfo::ConnectTiming& connect_timing() const { |
115 return connect_timing_; | 115 return connect_timing_; |
116 } | 116 } |
117 | 117 |
118 const BoundNetLog& net_log() const { return net_log_; } | 118 const NetLogWithSource& net_log() const { return net_log_; } |
119 | 119 |
120 protected: | 120 protected: |
121 RequestPriority priority() const { return priority_; } | 121 RequestPriority priority() const { return priority_; } |
122 ClientSocketPool::RespectLimits respect_limits() const { | 122 ClientSocketPool::RespectLimits respect_limits() const { |
123 return respect_limits_; | 123 return respect_limits_; |
124 } | 124 } |
125 void SetSocket(std::unique_ptr<StreamSocket> socket); | 125 void SetSocket(std::unique_ptr<StreamSocket> socket); |
126 StreamSocket* socket() { return socket_.get(); } | 126 StreamSocket* socket() { return socket_.get(); } |
127 void NotifyDelegateOfCompletion(int rv); | 127 void NotifyDelegateOfCompletion(int rv); |
128 void ResetTimer(base::TimeDelta remainingTime); | 128 void ResetTimer(base::TimeDelta remainingTime); |
(...skipping 12 matching lines...) Expand all Loading... |
141 | 141 |
142 const std::string group_name_; | 142 const std::string group_name_; |
143 const base::TimeDelta timeout_duration_; | 143 const base::TimeDelta timeout_duration_; |
144 // TODO(akalin): Support reprioritization. | 144 // TODO(akalin): Support reprioritization. |
145 const RequestPriority priority_; | 145 const RequestPriority priority_; |
146 const ClientSocketPool::RespectLimits respect_limits_; | 146 const ClientSocketPool::RespectLimits respect_limits_; |
147 // Timer to abort jobs that take too long. | 147 // Timer to abort jobs that take too long. |
148 base::OneShotTimer timer_; | 148 base::OneShotTimer timer_; |
149 Delegate* delegate_; | 149 Delegate* delegate_; |
150 std::unique_ptr<StreamSocket> socket_; | 150 std::unique_ptr<StreamSocket> socket_; |
151 BoundNetLog net_log_; | 151 NetLogWithSource net_log_; |
152 // A ConnectJob is idle until Connect() has been called. | 152 // A ConnectJob is idle until Connect() has been called. |
153 bool idle_; | 153 bool idle_; |
154 | 154 |
155 DISALLOW_COPY_AND_ASSIGN(ConnectJob); | 155 DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
156 }; | 156 }; |
157 | 157 |
158 namespace internal { | 158 namespace internal { |
159 | 159 |
160 // ClientSocketPoolBaseHelper is an internal class that implements almost all | 160 // ClientSocketPoolBaseHelper is an internal class that implements almost all |
161 // the functionality from ClientSocketPoolBase without using templates. | 161 // the functionality from ClientSocketPoolBase without using templates. |
(...skipping 12 matching lines...) Expand all Loading... |
174 NO_IDLE_SOCKETS = 0x1, // Do not return an idle socket. Create a new one. | 174 NO_IDLE_SOCKETS = 0x1, // Do not return an idle socket. Create a new one. |
175 }; | 175 }; |
176 | 176 |
177 class NET_EXPORT_PRIVATE Request { | 177 class NET_EXPORT_PRIVATE Request { |
178 public: | 178 public: |
179 Request(ClientSocketHandle* handle, | 179 Request(ClientSocketHandle* handle, |
180 const CompletionCallback& callback, | 180 const CompletionCallback& callback, |
181 RequestPriority priority, | 181 RequestPriority priority, |
182 ClientSocketPool::RespectLimits respect_limits, | 182 ClientSocketPool::RespectLimits respect_limits, |
183 Flags flags, | 183 Flags flags, |
184 const BoundNetLog& net_log); | 184 const NetLogWithSource& net_log); |
185 | 185 |
186 virtual ~Request(); | 186 virtual ~Request(); |
187 | 187 |
188 ClientSocketHandle* handle() const { return handle_; } | 188 ClientSocketHandle* handle() const { return handle_; } |
189 const CompletionCallback& callback() const { return callback_; } | 189 const CompletionCallback& callback() const { return callback_; } |
190 RequestPriority priority() const { return priority_; } | 190 RequestPriority priority() const { return priority_; } |
191 ClientSocketPool::RespectLimits respect_limits() const { | 191 ClientSocketPool::RespectLimits respect_limits() const { |
192 return respect_limits_; | 192 return respect_limits_; |
193 } | 193 } |
194 Flags flags() const { return flags_; } | 194 Flags flags() const { return flags_; } |
195 const BoundNetLog& net_log() const { return net_log_; } | 195 const NetLogWithSource& net_log() const { return net_log_; } |
196 | 196 |
197 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 197 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
198 void CrashIfInvalid() const; | 198 void CrashIfInvalid() const; |
199 | 199 |
200 private: | 200 private: |
201 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 201 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
202 enum Liveness { | 202 enum Liveness { |
203 ALIVE = 0xCA11AB13, | 203 ALIVE = 0xCA11AB13, |
204 DEAD = 0xDEADBEEF, | 204 DEAD = 0xDEADBEEF, |
205 }; | 205 }; |
206 | 206 |
207 ClientSocketHandle* const handle_; | 207 ClientSocketHandle* const handle_; |
208 const CompletionCallback callback_; | 208 const CompletionCallback callback_; |
209 // TODO(akalin): Support reprioritization. | 209 // TODO(akalin): Support reprioritization. |
210 const RequestPriority priority_; | 210 const RequestPriority priority_; |
211 const ClientSocketPool::RespectLimits respect_limits_; | 211 const ClientSocketPool::RespectLimits respect_limits_; |
212 const Flags flags_; | 212 const Flags flags_; |
213 const BoundNetLog net_log_; | 213 const NetLogWithSource net_log_; |
214 | 214 |
215 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 215 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
216 Liveness liveness_ = ALIVE; | 216 Liveness liveness_ = ALIVE; |
217 | 217 |
218 DISALLOW_COPY_AND_ASSIGN(Request); | 218 DISALLOW_COPY_AND_ASSIGN(Request); |
219 }; | 219 }; |
220 | 220 |
221 class ConnectJobFactory { | 221 class ConnectJobFactory { |
222 public: | 222 public: |
223 ConnectJobFactory() {} | 223 ConnectJobFactory() {} |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 // Process a pending socket request for a group. | 540 // Process a pending socket request for a group. |
541 void ProcessPendingRequest(const std::string& group_name, Group* group); | 541 void ProcessPendingRequest(const std::string& group_name, Group* group); |
542 | 542 |
543 // Assigns |socket| to |handle| and updates |group|'s counters appropriately. | 543 // Assigns |socket| to |handle| and updates |group|'s counters appropriately. |
544 void HandOutSocket(std::unique_ptr<StreamSocket> socket, | 544 void HandOutSocket(std::unique_ptr<StreamSocket> socket, |
545 ClientSocketHandle::SocketReuseType reuse_type, | 545 ClientSocketHandle::SocketReuseType reuse_type, |
546 const LoadTimingInfo::ConnectTiming& connect_timing, | 546 const LoadTimingInfo::ConnectTiming& connect_timing, |
547 ClientSocketHandle* handle, | 547 ClientSocketHandle* handle, |
548 base::TimeDelta time_idle, | 548 base::TimeDelta time_idle, |
549 Group* group, | 549 Group* group, |
550 const BoundNetLog& net_log); | 550 const NetLogWithSource& net_log); |
551 | 551 |
552 // Adds |socket| to the list of idle sockets for |group|. | 552 // Adds |socket| to the list of idle sockets for |group|. |
553 void AddIdleSocket(std::unique_ptr<StreamSocket> socket, Group* group); | 553 void AddIdleSocket(std::unique_ptr<StreamSocket> socket, Group* group); |
554 | 554 |
555 // Iterates through |group_map_|, canceling all ConnectJobs and deleting | 555 // Iterates through |group_map_|, canceling all ConnectJobs and deleting |
556 // groups if they are no longer needed. | 556 // groups if they are no longer needed. |
557 void CancelAllConnectJobs(); | 557 void CancelAllConnectJobs(); |
558 | 558 |
559 // Iterates through |group_map_|, posting |error| callbacks for all | 559 // Iterates through |group_map_|, posting |error| callbacks for all |
560 // requests, and then deleting groups if they are no longer needed. | 560 // requests, and then deleting groups if they are no longer needed. |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 class ClientSocketPoolBase { | 661 class ClientSocketPoolBase { |
662 public: | 662 public: |
663 class Request : public internal::ClientSocketPoolBaseHelper::Request { | 663 class Request : public internal::ClientSocketPoolBaseHelper::Request { |
664 public: | 664 public: |
665 Request(ClientSocketHandle* handle, | 665 Request(ClientSocketHandle* handle, |
666 const CompletionCallback& callback, | 666 const CompletionCallback& callback, |
667 RequestPriority priority, | 667 RequestPriority priority, |
668 ClientSocketPool::RespectLimits respect_limits, | 668 ClientSocketPool::RespectLimits respect_limits, |
669 internal::ClientSocketPoolBaseHelper::Flags flags, | 669 internal::ClientSocketPoolBaseHelper::Flags flags, |
670 const scoped_refptr<SocketParams>& params, | 670 const scoped_refptr<SocketParams>& params, |
671 const BoundNetLog& net_log) | 671 const NetLogWithSource& net_log) |
672 : internal::ClientSocketPoolBaseHelper::Request(handle, | 672 : internal::ClientSocketPoolBaseHelper::Request(handle, |
673 callback, | 673 callback, |
674 priority, | 674 priority, |
675 respect_limits, | 675 respect_limits, |
676 flags, | 676 flags, |
677 net_log), | 677 net_log), |
678 params_(params) {} | 678 params_(params) {} |
679 | 679 |
680 const scoped_refptr<SocketParams>& params() const { return params_; } | 680 const scoped_refptr<SocketParams>& params() const { return params_; } |
681 | 681 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 } | 734 } |
735 | 735 |
736 // RequestSocket bundles up the parameters into a Request and then forwards to | 736 // RequestSocket bundles up the parameters into a Request and then forwards to |
737 // ClientSocketPoolBaseHelper::RequestSocket(). | 737 // ClientSocketPoolBaseHelper::RequestSocket(). |
738 int RequestSocket(const std::string& group_name, | 738 int RequestSocket(const std::string& group_name, |
739 const scoped_refptr<SocketParams>& params, | 739 const scoped_refptr<SocketParams>& params, |
740 RequestPriority priority, | 740 RequestPriority priority, |
741 ClientSocketPool::RespectLimits respect_limits, | 741 ClientSocketPool::RespectLimits respect_limits, |
742 ClientSocketHandle* handle, | 742 ClientSocketHandle* handle, |
743 const CompletionCallback& callback, | 743 const CompletionCallback& callback, |
744 const BoundNetLog& net_log) { | 744 const NetLogWithSource& net_log) { |
745 std::unique_ptr<const Request> request(new Request( | 745 std::unique_ptr<const Request> request(new Request( |
746 handle, callback, priority, respect_limits, | 746 handle, callback, priority, respect_limits, |
747 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); | 747 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); |
748 return helper_.RequestSocket(group_name, std::move(request)); | 748 return helper_.RequestSocket(group_name, std::move(request)); |
749 } | 749 } |
750 | 750 |
751 // RequestSockets bundles up the parameters into a Request and then forwards | 751 // RequestSockets bundles up the parameters into a Request and then forwards |
752 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the | 752 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the |
753 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. | 753 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. |
754 void RequestSockets(const std::string& group_name, | 754 void RequestSockets(const std::string& group_name, |
755 const scoped_refptr<SocketParams>& params, | 755 const scoped_refptr<SocketParams>& params, |
756 int num_sockets, | 756 int num_sockets, |
757 const BoundNetLog& net_log) { | 757 const NetLogWithSource& net_log) { |
758 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE, | 758 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE, |
759 ClientSocketPool::RespectLimits::ENABLED, | 759 ClientSocketPool::RespectLimits::ENABLED, |
760 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, | 760 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, |
761 params, net_log); | 761 params, net_log); |
762 helper_.RequestSockets(group_name, request, num_sockets); | 762 helper_.RequestSockets(group_name, request, num_sockets); |
763 } | 763 } |
764 | 764 |
765 void CancelRequest(const std::string& group_name, | 765 void CancelRequest(const std::string& group_name, |
766 ClientSocketHandle* handle) { | 766 ClientSocketHandle* handle) { |
767 return helper_.CancelRequest(group_name, handle); | 767 return helper_.CancelRequest(group_name, handle); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 }; | 865 }; |
866 | 866 |
867 internal::ClientSocketPoolBaseHelper helper_; | 867 internal::ClientSocketPoolBaseHelper helper_; |
868 | 868 |
869 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 869 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
870 }; | 870 }; |
871 | 871 |
872 } // namespace net | 872 } // namespace net |
873 | 873 |
874 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 874 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
OLD | NEW |