Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / socket / client_socket_handle.h
bloba4f7befbcac8654003d7da28b39c90a471337f7c
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
3 // found in the LICENSE file.
5 #ifndef NET_SOCKET_CLIENT_SOCKET_HANDLE_H_
6 #define NET_SOCKET_CLIENT_SOCKET_HANDLE_H_
8 #include <string>
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time/time.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/ip_endpoint.h"
16 #include "net/base/load_states.h"
17 #include "net/base/load_timing_info.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_export.h"
20 #include "net/base/request_priority.h"
21 #include "net/http/http_response_info.h"
22 #include "net/log/net_log.h"
23 #include "net/socket/client_socket_pool.h"
24 #include "net/socket/connection_attempts.h"
25 #include "net/socket/stream_socket.h"
26 #include "net/ssl/ssl_failure_state.h"
28 namespace net {
30 // A container for a StreamSocket.
32 // The handle's |group_name| uniquely identifies the origin and type of the
33 // connection. It is used by the ClientSocketPool to group similar connected
34 // client socket objects.
36 class NET_EXPORT ClientSocketHandle {
37 public:
38 enum SocketReuseType {
39 UNUSED = 0, // unused socket that just finished connecting
40 UNUSED_IDLE, // unused socket that has been idle for awhile
41 REUSED_IDLE, // previously used socket
42 NUM_TYPES,
45 ClientSocketHandle();
46 ~ClientSocketHandle();
48 // Initializes a ClientSocketHandle object, which involves talking to the
49 // ClientSocketPool to obtain a connected socket, possibly reusing one. This
50 // method returns either OK or ERR_IO_PENDING. On ERR_IO_PENDING, |priority|
51 // is used to determine the placement in ClientSocketPool's wait list.
53 // If this method succeeds, then the socket member will be set to an existing
54 // connected socket if an existing connected socket was available to reuse,
55 // otherwise it will be set to a new connected socket. Consumers can then
56 // call is_reused() to see if the socket was reused. If not reusing an
57 // existing socket, ClientSocketPool may need to establish a new
58 // connection using |socket_params|.
60 // This method returns ERR_IO_PENDING if it cannot complete synchronously, in
61 // which case the consumer will be notified of completion via |callback|.
63 // If the pool was not able to reuse an existing socket, the new socket
64 // may report a recoverable error. In this case, the return value will
65 // indicate an error and the socket member will be set. If it is determined
66 // that the error is not recoverable, the Disconnect method should be used
67 // on the socket, so that it does not get reused.
69 // A non-recoverable error may set additional state in the ClientSocketHandle
70 // to allow the caller to determine what went wrong.
72 // Init may be called multiple times.
74 // Profiling information for the request is saved to |net_log| if non-NULL.
76 template <typename PoolType>
77 int Init(const std::string& group_name,
78 const scoped_refptr<typename PoolType::SocketParams>& socket_params,
79 RequestPriority priority,
80 const CompletionCallback& callback,
81 PoolType* pool,
82 const BoundNetLog& net_log);
84 // An initialized handle can be reset, which causes it to return to the
85 // un-initialized state. This releases the underlying socket, which in the
86 // case of a socket that still has an established connection, indicates that
87 // the socket may be kept alive for use by a subsequent ClientSocketHandle.
89 // NOTE: To prevent the socket from being kept alive, be sure to call its
90 // Disconnect method. This will result in the ClientSocketPool deleting the
91 // StreamSocket.
92 void Reset();
94 // Used after Init() is called, but before the ClientSocketPool has
95 // initialized the ClientSocketHandle.
96 LoadState GetLoadState() const;
98 bool IsPoolStalled() const;
100 // Adds a higher layered pool on top of the socket pool that |socket_| belongs
101 // to. At most one higher layered pool can be added to a
102 // ClientSocketHandle at a time. On destruction or reset, automatically
103 // removes the higher pool if RemoveHigherLayeredPool has not been called.
104 void AddHigherLayeredPool(HigherLayeredPool* higher_pool);
106 // Removes a higher layered pool from the socket pool that |socket_| belongs
107 // to. |higher_pool| must have been added by the above function.
108 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool);
110 // Returns true when Init() has completed successfully.
111 bool is_initialized() const { return is_initialized_; }
113 // Returns the time tick when Init() was called.
114 base::TimeTicks init_time() const { return init_time_; }
116 // Returns the time between Init() and when is_initialized() becomes true.
117 base::TimeDelta setup_time() const { return setup_time_; }
119 // Sets the portion of LoadTimingInfo related to connection establishment, and
120 // the socket id. |is_reused| is needed because the handle may not have full
121 // reuse information. |load_timing_info| must have all default values when
122 // called. Returns false and makes no changes to |load_timing_info| when
123 // |socket_| is NULL.
124 bool GetLoadTimingInfo(bool is_reused,
125 LoadTimingInfo* load_timing_info) const;
127 // Used by ClientSocketPool to initialize the ClientSocketHandle.
129 // SetSocket() may also be used if this handle is used as simply for
130 // socket storage (e.g., http://crbug.com/37810).
131 void SetSocket(scoped_ptr<StreamSocket> s);
132 void set_reuse_type(SocketReuseType reuse_type) { reuse_type_ = reuse_type; }
133 void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; }
134 void set_pool_id(int id) { pool_id_ = id; }
135 void set_is_ssl_error(bool is_ssl_error) { is_ssl_error_ = is_ssl_error; }
136 void set_ssl_error_response_info(const HttpResponseInfo& ssl_error_state) {
137 ssl_error_response_info_ = ssl_error_state;
139 void set_ssl_failure_state(SSLFailureState ssl_failure_state) {
140 ssl_failure_state_ = ssl_failure_state;
142 void set_pending_http_proxy_connection(ClientSocketHandle* connection) {
143 pending_http_proxy_connection_.reset(connection);
145 void set_connection_attempts(const ConnectionAttempts& attempts) {
146 connection_attempts_ = attempts;
149 // Only valid if there is no |socket_|.
150 bool is_ssl_error() const {
151 DCHECK(socket_.get() == NULL);
152 return is_ssl_error_;
154 // On an ERR_PROXY_AUTH_REQUESTED error, the |headers| and |auth_challenge|
155 // fields are filled in. On an ERR_SSL_CLIENT_AUTH_CERT_NEEDED error,
156 // the |cert_request_info| field is set.
157 const HttpResponseInfo& ssl_error_response_info() const {
158 return ssl_error_response_info_;
160 SSLFailureState ssl_failure_state() const { return ssl_failure_state_; }
161 ClientSocketHandle* release_pending_http_proxy_connection() {
162 return pending_http_proxy_connection_.release();
164 // If the connection failed, returns the connection attempts made. (If it
165 // succeeded, they will be returned through the socket instead; see
166 // |StreamSocket::GetConnectionAttempts|.)
167 const ConnectionAttempts& connection_attempts() {
168 return connection_attempts_;
171 StreamSocket* socket() { return socket_.get(); }
173 // SetSocket() must be called with a new socket before this handle
174 // is destroyed if is_initialized() is true.
175 scoped_ptr<StreamSocket> PassSocket();
177 // These may only be used if is_initialized() is true.
178 const std::string& group_name() const { return group_name_; }
179 int id() const { return pool_id_; }
180 bool is_reused() const { return reuse_type_ == REUSED_IDLE; }
181 base::TimeDelta idle_time() const { return idle_time_; }
182 SocketReuseType reuse_type() const { return reuse_type_; }
183 const LoadTimingInfo::ConnectTiming& connect_timing() const {
184 return connect_timing_;
186 void set_connect_timing(const LoadTimingInfo::ConnectTiming& connect_timing) {
187 connect_timing_ = connect_timing;
190 private:
191 // Called on asynchronous completion of an Init() request.
192 void OnIOComplete(int result);
194 // Called on completion (both asynchronous & synchronous) of an Init()
195 // request.
196 void HandleInitCompletion(int result);
198 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or
199 // not to try to cancel the request with the ClientSocketPool. Does not
200 // reset the supplemental error state.
201 void ResetInternal(bool cancel);
203 // Resets the supplemental error state.
204 void ResetErrorState();
206 bool is_initialized_;
207 ClientSocketPool* pool_;
208 HigherLayeredPool* higher_pool_;
209 scoped_ptr<StreamSocket> socket_;
210 std::string group_name_;
211 SocketReuseType reuse_type_;
212 CompletionCallback callback_;
213 CompletionCallback user_callback_;
214 base::TimeDelta idle_time_;
215 int pool_id_; // See ClientSocketPool::ReleaseSocket() for an explanation.
216 bool is_ssl_error_;
217 HttpResponseInfo ssl_error_response_info_;
218 SSLFailureState ssl_failure_state_;
219 scoped_ptr<ClientSocketHandle> pending_http_proxy_connection_;
220 std::vector<ConnectionAttempt> connection_attempts_;
221 base::TimeTicks init_time_;
222 base::TimeDelta setup_time_;
224 NetLog::Source requesting_source_;
226 // Timing information is set when a connection is successfully established.
227 LoadTimingInfo::ConnectTiming connect_timing_;
229 DISALLOW_COPY_AND_ASSIGN(ClientSocketHandle);
232 // Template function implementation:
233 template <typename PoolType>
234 int ClientSocketHandle::Init(
235 const std::string& group_name,
236 const scoped_refptr<typename PoolType::SocketParams>& socket_params,
237 RequestPriority priority,
238 const CompletionCallback& callback,
239 PoolType* pool,
240 const BoundNetLog& net_log) {
241 requesting_source_ = net_log.source();
243 CHECK(!group_name.empty());
244 ResetInternal(true);
245 ResetErrorState();
246 pool_ = pool;
247 group_name_ = group_name;
248 init_time_ = base::TimeTicks::Now();
249 int rv = pool_->RequestSocket(
250 group_name, &socket_params, priority, this, callback_, net_log);
251 if (rv == ERR_IO_PENDING) {
252 user_callback_ = callback;
253 } else {
254 HandleInitCompletion(rv);
256 return rv;
259 } // namespace net
261 #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_