Port Chromium's blink_tests target to GN.
[chromium-blink-merge.git] / net / socket / client_socket_handle.h
blob58cf4ca6709b8d72c9fb86193142cd25d8409fc2
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"
27 namespace net {
29 // A container for a StreamSocket.
31 // The handle's |group_name| uniquely identifies the origin and type of the
32 // connection. It is used by the ClientSocketPool to group similar connected
33 // client socket objects.
35 class NET_EXPORT ClientSocketHandle {
36 public:
37 enum SocketReuseType {
38 UNUSED = 0, // unused socket that just finished connecting
39 UNUSED_IDLE, // unused socket that has been idle for awhile
40 REUSED_IDLE, // previously used socket
41 NUM_TYPES,
44 ClientSocketHandle();
45 ~ClientSocketHandle();
47 // Initializes a ClientSocketHandle object, which involves talking to the
48 // ClientSocketPool to obtain a connected socket, possibly reusing one. This
49 // method returns either OK or ERR_IO_PENDING. On ERR_IO_PENDING, |priority|
50 // is used to determine the placement in ClientSocketPool's wait list.
52 // If this method succeeds, then the socket member will be set to an existing
53 // connected socket if an existing connected socket was available to reuse,
54 // otherwise it will be set to a new connected socket. Consumers can then
55 // call is_reused() to see if the socket was reused. If not reusing an
56 // existing socket, ClientSocketPool may need to establish a new
57 // connection using |socket_params|.
59 // This method returns ERR_IO_PENDING if it cannot complete synchronously, in
60 // which case the consumer will be notified of completion via |callback|.
62 // If the pool was not able to reuse an existing socket, the new socket
63 // may report a recoverable error. In this case, the return value will
64 // indicate an error and the socket member will be set. If it is determined
65 // that the error is not recoverable, the Disconnect method should be used
66 // on the socket, so that it does not get reused.
68 // A non-recoverable error may set additional state in the ClientSocketHandle
69 // to allow the caller to determine what went wrong.
71 // Init may be called multiple times.
73 // Profiling information for the request is saved to |net_log| if non-NULL.
75 template <typename PoolType>
76 int Init(const std::string& group_name,
77 const scoped_refptr<typename PoolType::SocketParams>& socket_params,
78 RequestPriority priority,
79 const CompletionCallback& callback,
80 PoolType* pool,
81 const BoundNetLog& net_log);
83 // An initialized handle can be reset, which causes it to return to the
84 // un-initialized state. This releases the underlying socket, which in the
85 // case of a socket that still has an established connection, indicates that
86 // the socket may be kept alive for use by a subsequent ClientSocketHandle.
88 // NOTE: To prevent the socket from being kept alive, be sure to call its
89 // Disconnect method. This will result in the ClientSocketPool deleting the
90 // StreamSocket.
91 void Reset();
93 // Used after Init() is called, but before the ClientSocketPool has
94 // initialized the ClientSocketHandle.
95 LoadState GetLoadState() const;
97 bool IsPoolStalled() const;
99 // Adds a higher layered pool on top of the socket pool that |socket_| belongs
100 // to. At most one higher layered pool can be added to a
101 // ClientSocketHandle at a time. On destruction or reset, automatically
102 // removes the higher pool if RemoveHigherLayeredPool has not been called.
103 void AddHigherLayeredPool(HigherLayeredPool* higher_pool);
105 // Removes a higher layered pool from the socket pool that |socket_| belongs
106 // to. |higher_pool| must have been added by the above function.
107 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool);
109 // Returns true when Init() has completed successfully.
110 bool is_initialized() const { return is_initialized_; }
112 // Returns the time tick when Init() was called.
113 base::TimeTicks init_time() const { return init_time_; }
115 // Returns the time between Init() and when is_initialized() becomes true.
116 base::TimeDelta setup_time() const { return setup_time_; }
118 // Sets the portion of LoadTimingInfo related to connection establishment, and
119 // the socket id. |is_reused| is needed because the handle may not have full
120 // reuse information. |load_timing_info| must have all default values when
121 // called. Returns false and makes no changes to |load_timing_info| when
122 // |socket_| is NULL.
123 bool GetLoadTimingInfo(bool is_reused,
124 LoadTimingInfo* load_timing_info) const;
126 // Used by ClientSocketPool to initialize the ClientSocketHandle.
128 // SetSocket() may also be used if this handle is used as simply for
129 // socket storage (e.g., http://crbug.com/37810).
130 void SetSocket(scoped_ptr<StreamSocket> s);
131 void set_reuse_type(SocketReuseType reuse_type) { reuse_type_ = reuse_type; }
132 void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; }
133 void set_pool_id(int id) { pool_id_ = id; }
134 void set_is_ssl_error(bool is_ssl_error) { is_ssl_error_ = is_ssl_error; }
135 void set_ssl_error_response_info(const HttpResponseInfo& ssl_error_state) {
136 ssl_error_response_info_ = ssl_error_state;
138 void set_pending_http_proxy_connection(ClientSocketHandle* connection) {
139 pending_http_proxy_connection_.reset(connection);
141 void set_connection_attempts(const ConnectionAttempts& attempts) {
142 connection_attempts_ = attempts;
145 // Only valid if there is no |socket_|.
146 bool is_ssl_error() const {
147 DCHECK(socket_.get() == NULL);
148 return is_ssl_error_;
150 // On an ERR_PROXY_AUTH_REQUESTED error, the |headers| and |auth_challenge|
151 // fields are filled in. On an ERR_SSL_CLIENT_AUTH_CERT_NEEDED error,
152 // the |cert_request_info| field is set.
153 const HttpResponseInfo& ssl_error_response_info() const {
154 return ssl_error_response_info_;
156 ClientSocketHandle* release_pending_http_proxy_connection() {
157 return pending_http_proxy_connection_.release();
159 const ConnectionAttempts& connection_attempts() {
160 return connection_attempts_;
163 StreamSocket* socket() { return socket_.get(); }
165 // SetSocket() must be called with a new socket before this handle
166 // is destroyed if is_initialized() is true.
167 scoped_ptr<StreamSocket> PassSocket();
169 // These may only be used if is_initialized() is true.
170 const std::string& group_name() const { return group_name_; }
171 int id() const { return pool_id_; }
172 bool is_reused() const { return reuse_type_ == REUSED_IDLE; }
173 base::TimeDelta idle_time() const { return idle_time_; }
174 SocketReuseType reuse_type() const { return reuse_type_; }
175 const LoadTimingInfo::ConnectTiming& connect_timing() const {
176 return connect_timing_;
178 void set_connect_timing(const LoadTimingInfo::ConnectTiming& connect_timing) {
179 connect_timing_ = connect_timing;
182 private:
183 // Called on asynchronous completion of an Init() request.
184 void OnIOComplete(int result);
186 // Called on completion (both asynchronous & synchronous) of an Init()
187 // request.
188 void HandleInitCompletion(int result);
190 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or
191 // not to try to cancel the request with the ClientSocketPool. Does not
192 // reset the supplemental error state.
193 void ResetInternal(bool cancel);
195 // Resets the supplemental error state.
196 void ResetErrorState();
198 bool is_initialized_;
199 ClientSocketPool* pool_;
200 HigherLayeredPool* higher_pool_;
201 scoped_ptr<StreamSocket> socket_;
202 std::string group_name_;
203 SocketReuseType reuse_type_;
204 CompletionCallback callback_;
205 CompletionCallback user_callback_;
206 base::TimeDelta idle_time_;
207 int pool_id_; // See ClientSocketPool::ReleaseSocket() for an explanation.
208 bool is_ssl_error_;
209 HttpResponseInfo ssl_error_response_info_;
210 scoped_ptr<ClientSocketHandle> pending_http_proxy_connection_;
211 std::vector<ConnectionAttempt> connection_attempts_;
212 base::TimeTicks init_time_;
213 base::TimeDelta setup_time_;
215 NetLog::Source requesting_source_;
217 // Timing information is set when a connection is successfully established.
218 LoadTimingInfo::ConnectTiming connect_timing_;
220 DISALLOW_COPY_AND_ASSIGN(ClientSocketHandle);
223 // Template function implementation:
224 template <typename PoolType>
225 int ClientSocketHandle::Init(
226 const std::string& group_name,
227 const scoped_refptr<typename PoolType::SocketParams>& socket_params,
228 RequestPriority priority,
229 const CompletionCallback& callback,
230 PoolType* pool,
231 const BoundNetLog& net_log) {
232 requesting_source_ = net_log.source();
234 CHECK(!group_name.empty());
235 ResetInternal(true);
236 ResetErrorState();
237 pool_ = pool;
238 group_name_ = group_name;
239 init_time_ = base::TimeTicks::Now();
240 int rv = pool_->RequestSocket(
241 group_name, &socket_params, priority, this, callback_, net_log);
242 if (rv == ERR_IO_PENDING) {
243 user_callback_ = callback;
244 } else {
245 HandleInitCompletion(rv);
247 return rv;
250 } // namespace net
252 #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_