1 // Copyright (c) 2011 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. 4 5 #ifndef NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ 6 #define NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "base/logging.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/time.h" 15 #include "net/base/completion_callback.h" 16 #include "net/base/load_states.h" 17 #include "net/base/net_errors.h" 18 #include "net/base/net_log.h" 19 #include "net/base/request_priority.h" 20 #include "net/http/http_response_info.h" 21 #include "net/socket/client_socket.h" 22 #include "net/socket/client_socket_pool.h" 23 24 namespace net { 25 26 // A container for a ClientSocket. 27 // 28 // The handle's |group_name| uniquely identifies the origin and type of the 29 // connection. It is used by the ClientSocketPool to group similar connected 30 // client socket objects. 31 // 32 class ClientSocketHandle { 33 public: 34 enum SocketReuseType { 35 UNUSED = 0, // unused socket that just finished connecting 36 UNUSED_IDLE, // unused socket that has been idle for awhile 37 REUSED_IDLE, // previously used socket 38 NUM_TYPES, 39 }; 40 41 ClientSocketHandle(); 42 ~ClientSocketHandle(); 43 44 // Initializes a ClientSocketHandle object, which involves talking to the 45 // ClientSocketPool to obtain a connected socket, possibly reusing one. This 46 // method returns either OK or ERR_IO_PENDING. On ERR_IO_PENDING, |priority| 47 // is used to determine the placement in ClientSocketPool's wait list. 48 // 49 // If this method succeeds, then the socket member will be set to an existing 50 // connected socket if an existing connected socket was available to reuse, 51 // otherwise it will be set to a new connected socket. Consumers can then 52 // call is_reused() to see if the socket was reused. If not reusing an 53 // existing socket, ClientSocketPool may need to establish a new 54 // connection using |socket_params|. 55 // 56 // This method returns ERR_IO_PENDING if it cannot complete synchronously, in 57 // which case the consumer will be notified of completion via |callback|. 58 // 59 // If the pool was not able to reuse an existing socket, the new socket 60 // may report a recoverable error. In this case, the return value will 61 // indicate an error and the socket member will be set. If it is determined 62 // that the error is not recoverable, the Disconnect method should be used 63 // on the socket, so that it does not get reused. 64 // 65 // A non-recoverable error may set additional state in the ClientSocketHandle 66 // to allow the caller to determine what went wrong. 67 // 68 // Init may be called multiple times. 69 // 70 // Profiling information for the request is saved to |net_log| if non-NULL. 71 // 72 template <typename SocketParams, typename PoolType> 73 int Init(const std::string& group_name, 74 const scoped_refptr<SocketParams>& socket_params, 75 RequestPriority priority, 76 CompletionCallback* callback, 77 PoolType* pool, 78 const BoundNetLog& net_log); 79 80 // An initialized handle can be reset, which causes it to return to the 81 // un-initialized state. This releases the underlying socket, which in the 82 // case of a socket that still has an established connection, indicates that 83 // the socket may be kept alive for use by a subsequent ClientSocketHandle. 84 // 85 // NOTE: To prevent the socket from being kept alive, be sure to call its 86 // Disconnect method. This will result in the ClientSocketPool deleting the 87 // ClientSocket. 88 void Reset(); 89 90 // Used after Init() is called, but before the ClientSocketPool has 91 // initialized the ClientSocketHandle. 92 LoadState GetLoadState() const; 93 94 // Returns true when Init() has completed successfully. 95 bool is_initialized() const { return is_initialized_; } 96 97 // Returns the time tick when Init() was called. 98 base::TimeTicks init_time() const { return init_time_; } 99 100 // Returns the time between Init() and when is_initialized() becomes true. 101 base::TimeDelta setup_time() const { return setup_time_; } 102 103 // Used by ClientSocketPool to initialize the ClientSocketHandle. 104 void set_is_reused(bool is_reused) { is_reused_ = is_reused; } 105 void set_socket(ClientSocket* s) { socket_.reset(s); } 106 void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; } 107 void set_pool_id(int id) { pool_id_ = id; } 108 void set_is_ssl_error(bool is_ssl_error) { is_ssl_error_ = is_ssl_error; } 109 void set_ssl_error_response_info(const HttpResponseInfo& ssl_error_state) { 110 ssl_error_response_info_ = ssl_error_state; 111 } 112 void set_pending_http_proxy_connection(ClientSocketHandle* connection) { 113 pending_http_proxy_connection_.reset(connection); 114 } 115 116 // Only valid if there is no |socket_|. 117 bool is_ssl_error() const { 118 DCHECK(socket_.get() == NULL); 119 return is_ssl_error_; 120 } 121 // On an ERR_PROXY_AUTH_REQUESTED error, the |headers| and |auth_challenge| 122 // fields are filled in. On an ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, 123 // the |cert_request_info| field is set. 124 const HttpResponseInfo& ssl_error_response_info() const { 125 return ssl_error_response_info_; 126 } 127 ClientSocketHandle* release_pending_http_proxy_connection() { 128 return pending_http_proxy_connection_.release(); 129 } 130 131 // These may only be used if is_initialized() is true. 132 const std::string& group_name() const { return group_name_; } 133 int id() const { return pool_id_; } 134 ClientSocket* socket() { return socket_.get(); } 135 ClientSocket* release_socket() { return socket_.release(); } 136 bool is_reused() const { return is_reused_; } 137 base::TimeDelta idle_time() const { return idle_time_; } 138 SocketReuseType reuse_type() const { 139 if (is_reused()) { 140 return REUSED_IDLE; 141 } else if (idle_time() == base::TimeDelta()) { 142 return UNUSED; 143 } else { 144 return UNUSED_IDLE; 145 } 146 } 147 148 private: 149 // Called on asynchronous completion of an Init() request. 150 void OnIOComplete(int result); 151 152 // Called on completion (both asynchronous & synchronous) of an Init() 153 // request. 154 void HandleInitCompletion(int result); 155 156 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or 157 // not to try to cancel the request with the ClientSocketPool. Does not 158 // reset the supplemental error state. 159 void ResetInternal(bool cancel); 160 161 // Resets the supplemental error state. 162 void ResetErrorState(); 163 164 bool is_initialized_; 165 ClientSocketPool* pool_; 166 scoped_ptr<ClientSocket> socket_; 167 std::string group_name_; 168 bool is_reused_; 169 CompletionCallbackImpl<ClientSocketHandle> callback_; 170 CompletionCallback* user_callback_; 171 base::TimeDelta idle_time_; 172 int pool_id_; // See ClientSocketPool::ReleaseSocket() for an explanation. 173 bool is_ssl_error_; 174 HttpResponseInfo ssl_error_response_info_; 175 scoped_ptr<ClientSocketHandle> pending_http_proxy_connection_; 176 base::TimeTicks init_time_; 177 base::TimeDelta setup_time_; 178 179 NetLog::Source requesting_source_; 180 181 DISALLOW_COPY_AND_ASSIGN(ClientSocketHandle); 182 }; 183 184 // Template function implementation: 185 template <typename SocketParams, typename PoolType> 186 int ClientSocketHandle::Init(const std::string& group_name, 187 const scoped_refptr<SocketParams>& socket_params, 188 RequestPriority priority, 189 CompletionCallback* callback, 190 PoolType* pool, 191 const BoundNetLog& net_log) { 192 requesting_source_ = net_log.source(); 193 194 CHECK(!group_name.empty()); 195 // Note that this will result in a compile error if the SocketParams has not 196 // been registered for the PoolType via REGISTER_SOCKET_PARAMS_FOR_POOL 197 // (defined in client_socket_pool.h). 198 CheckIsValidSocketParamsForPool<PoolType, SocketParams>(); 199 ResetInternal(true); 200 ResetErrorState(); 201 pool_ = pool; 202 group_name_ = group_name; 203 init_time_ = base::TimeTicks::Now(); 204 int rv = pool_->RequestSocket( 205 group_name, &socket_params, priority, this, &callback_, net_log); 206 if (rv == ERR_IO_PENDING) { 207 user_callback_ = callback; 208 } else { 209 HandleInitCompletion(rv); 210 } 211 return rv; 212 } 213 214 } // namespace net 215 216 #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ 217