1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ 6*6777b538SAndroid Build Coastguard Worker #define NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include <memory> 9*6777b538SAndroid Build Coastguard Worker #include <optional> 10*6777b538SAndroid Build Coastguard Worker #include <utility> 11*6777b538SAndroid Build Coastguard Worker 12*6777b538SAndroid Build Coastguard Worker #include "base/check.h" 13*6777b538SAndroid Build Coastguard Worker #include "base/functional/bind.h" 14*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h" 15*6777b538SAndroid Build Coastguard Worker #include "base/memory/scoped_refptr.h" 16*6777b538SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h" 17*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h" 18*6777b538SAndroid Build Coastguard Worker #include "net/base/ip_endpoint.h" 19*6777b538SAndroid Build Coastguard Worker #include "net/base/load_states.h" 20*6777b538SAndroid Build Coastguard Worker #include "net/base/load_timing_info.h" 21*6777b538SAndroid Build Coastguard Worker #include "net/base/net_errors.h" 22*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h" 23*6777b538SAndroid Build Coastguard Worker #include "net/base/request_priority.h" 24*6777b538SAndroid Build Coastguard Worker #include "net/dns/public/resolve_error_info.h" 25*6777b538SAndroid Build Coastguard Worker #include "net/log/net_log_source.h" 26*6777b538SAndroid Build Coastguard Worker #include "net/log/net_log_with_source.h" 27*6777b538SAndroid Build Coastguard Worker #include "net/socket/client_socket_pool.h" 28*6777b538SAndroid Build Coastguard Worker #include "net/socket/connection_attempts.h" 29*6777b538SAndroid Build Coastguard Worker #include "net/socket/stream_socket.h" 30*6777b538SAndroid Build Coastguard Worker #include "net/ssl/ssl_cert_request_info.h" 31*6777b538SAndroid Build Coastguard Worker 32*6777b538SAndroid Build Coastguard Worker namespace net { 33*6777b538SAndroid Build Coastguard Worker 34*6777b538SAndroid Build Coastguard Worker class ConnectJob; 35*6777b538SAndroid Build Coastguard Worker struct NetworkTrafficAnnotationTag; 36*6777b538SAndroid Build Coastguard Worker class SocketTag; 37*6777b538SAndroid Build Coastguard Worker 38*6777b538SAndroid Build Coastguard Worker // A container for a StreamSocket. 39*6777b538SAndroid Build Coastguard Worker // 40*6777b538SAndroid Build Coastguard Worker // The handle's |group_id| uniquely identifies the origin and type of the 41*6777b538SAndroid Build Coastguard Worker // connection. It is used by the ClientSocketPool to group similar connected 42*6777b538SAndroid Build Coastguard Worker // client socket objects. 43*6777b538SAndroid Build Coastguard Worker // 44*6777b538SAndroid Build Coastguard Worker class NET_EXPORT ClientSocketHandle { 45*6777b538SAndroid Build Coastguard Worker public: 46*6777b538SAndroid Build Coastguard Worker enum SocketReuseType { 47*6777b538SAndroid Build Coastguard Worker UNUSED = 0, // unused socket that just finished connecting 48*6777b538SAndroid Build Coastguard Worker UNUSED_IDLE, // unused socket that has been idle for awhile 49*6777b538SAndroid Build Coastguard Worker REUSED_IDLE, // previously used socket 50*6777b538SAndroid Build Coastguard Worker NUM_TYPES, 51*6777b538SAndroid Build Coastguard Worker }; 52*6777b538SAndroid Build Coastguard Worker 53*6777b538SAndroid Build Coastguard Worker ClientSocketHandle(); 54*6777b538SAndroid Build Coastguard Worker 55*6777b538SAndroid Build Coastguard Worker ClientSocketHandle(const ClientSocketHandle&) = delete; 56*6777b538SAndroid Build Coastguard Worker ClientSocketHandle& operator=(const ClientSocketHandle&) = delete; 57*6777b538SAndroid Build Coastguard Worker 58*6777b538SAndroid Build Coastguard Worker ~ClientSocketHandle(); 59*6777b538SAndroid Build Coastguard Worker 60*6777b538SAndroid Build Coastguard Worker // Initializes a ClientSocketHandle object, which involves talking to the 61*6777b538SAndroid Build Coastguard Worker // ClientSocketPool to obtain a connected socket, possibly reusing one. This 62*6777b538SAndroid Build Coastguard Worker // method returns either OK or ERR_IO_PENDING. On ERR_IO_PENDING, |priority| 63*6777b538SAndroid Build Coastguard Worker // is used to determine the placement in ClientSocketPool's wait list. 64*6777b538SAndroid Build Coastguard Worker // If |respect_limits| is DISABLED, will bypass the wait list, but |priority| 65*6777b538SAndroid Build Coastguard Worker // must also be HIGHEST, if set. 66*6777b538SAndroid Build Coastguard Worker // 67*6777b538SAndroid Build Coastguard Worker // If this method succeeds, then the socket member will be set to an existing 68*6777b538SAndroid Build Coastguard Worker // connected socket if an existing connected socket was available to reuse, 69*6777b538SAndroid Build Coastguard Worker // otherwise it will be set to a new connected socket. Consumers can then 70*6777b538SAndroid Build Coastguard Worker // call is_reused() to see if the socket was reused. If not reusing an 71*6777b538SAndroid Build Coastguard Worker // existing socket, ClientSocketPool may need to establish a new 72*6777b538SAndroid Build Coastguard Worker // connection using |socket_params|. 73*6777b538SAndroid Build Coastguard Worker // 74*6777b538SAndroid Build Coastguard Worker // This method returns ERR_IO_PENDING if it cannot complete synchronously, in 75*6777b538SAndroid Build Coastguard Worker // which case the consumer will be notified of completion via |callback|. 76*6777b538SAndroid Build Coastguard Worker // 77*6777b538SAndroid Build Coastguard Worker // If the pool was not able to reuse an existing socket, the new socket 78*6777b538SAndroid Build Coastguard Worker // may report a recoverable error. In this case, the return value will 79*6777b538SAndroid Build Coastguard Worker // indicate an error and the socket member will be set. If it is determined 80*6777b538SAndroid Build Coastguard Worker // that the error is not recoverable, the Disconnect method should be used 81*6777b538SAndroid Build Coastguard Worker // on the socket, so that it does not get reused. 82*6777b538SAndroid Build Coastguard Worker // 83*6777b538SAndroid Build Coastguard Worker // A non-recoverable error may set additional state in the ClientSocketHandle 84*6777b538SAndroid Build Coastguard Worker // to allow the caller to determine what went wrong. 85*6777b538SAndroid Build Coastguard Worker // 86*6777b538SAndroid Build Coastguard Worker // Init may be called multiple times. 87*6777b538SAndroid Build Coastguard Worker // 88*6777b538SAndroid Build Coastguard Worker // Profiling information for the request is saved to |net_log| if non-NULL. 89*6777b538SAndroid Build Coastguard Worker int Init( 90*6777b538SAndroid Build Coastguard Worker const ClientSocketPool::GroupId& group_id, 91*6777b538SAndroid Build Coastguard Worker scoped_refptr<ClientSocketPool::SocketParams> socket_params, 92*6777b538SAndroid Build Coastguard Worker const std::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, 93*6777b538SAndroid Build Coastguard Worker RequestPriority priority, 94*6777b538SAndroid Build Coastguard Worker const SocketTag& socket_tag, 95*6777b538SAndroid Build Coastguard Worker ClientSocketPool::RespectLimits respect_limits, 96*6777b538SAndroid Build Coastguard Worker CompletionOnceCallback callback, 97*6777b538SAndroid Build Coastguard Worker const ClientSocketPool::ProxyAuthCallback& proxy_auth_callback, 98*6777b538SAndroid Build Coastguard Worker ClientSocketPool* pool, 99*6777b538SAndroid Build Coastguard Worker const NetLogWithSource& net_log); 100*6777b538SAndroid Build Coastguard Worker 101*6777b538SAndroid Build Coastguard Worker // Changes the priority of the ClientSocketHandle to the passed value. 102*6777b538SAndroid Build Coastguard Worker // This function is a no-op if |priority| is the same as the current 103*6777b538SAndroid Build Coastguard Worker // priority, of if Init() has not been called since the last time 104*6777b538SAndroid Build Coastguard Worker // the ClientSocketHandle was reset. 105*6777b538SAndroid Build Coastguard Worker void SetPriority(RequestPriority priority); 106*6777b538SAndroid Build Coastguard Worker 107*6777b538SAndroid Build Coastguard Worker // An initialized handle can be reset, which causes it to return to the 108*6777b538SAndroid Build Coastguard Worker // un-initialized state. This releases the underlying socket, which in the 109*6777b538SAndroid Build Coastguard Worker // case of a socket that still has an established connection, indicates that 110*6777b538SAndroid Build Coastguard Worker // the socket may be kept alive for use by a subsequent ClientSocketHandle. 111*6777b538SAndroid Build Coastguard Worker // 112*6777b538SAndroid Build Coastguard Worker // NOTE: To prevent the socket from being kept alive, be sure to call its 113*6777b538SAndroid Build Coastguard Worker // Disconnect method. This will result in the ClientSocketPool deleting the 114*6777b538SAndroid Build Coastguard Worker // StreamSocket. 115*6777b538SAndroid Build Coastguard Worker void Reset(); 116*6777b538SAndroid Build Coastguard Worker 117*6777b538SAndroid Build Coastguard Worker // Like Reset(), but also closes the socket (if there is one) and cancels any 118*6777b538SAndroid Build Coastguard Worker // pending attempt to establish a connection, if the connection attempt is 119*6777b538SAndroid Build Coastguard Worker // still ongoing. 120*6777b538SAndroid Build Coastguard Worker void ResetAndCloseSocket(); 121*6777b538SAndroid Build Coastguard Worker 122*6777b538SAndroid Build Coastguard Worker // Used after Init() is called, but before the ClientSocketPool has 123*6777b538SAndroid Build Coastguard Worker // initialized the ClientSocketHandle. 124*6777b538SAndroid Build Coastguard Worker LoadState GetLoadState() const; 125*6777b538SAndroid Build Coastguard Worker 126*6777b538SAndroid Build Coastguard Worker bool IsPoolStalled() const; 127*6777b538SAndroid Build Coastguard Worker 128*6777b538SAndroid Build Coastguard Worker // Adds a higher layered pool on top of the socket pool that |socket_| belongs 129*6777b538SAndroid Build Coastguard Worker // to. At most one higher layered pool can be added to a 130*6777b538SAndroid Build Coastguard Worker // ClientSocketHandle at a time. On destruction or reset, automatically 131*6777b538SAndroid Build Coastguard Worker // removes the higher pool if RemoveHigherLayeredPool has not been called. 132*6777b538SAndroid Build Coastguard Worker void AddHigherLayeredPool(HigherLayeredPool* higher_pool); 133*6777b538SAndroid Build Coastguard Worker 134*6777b538SAndroid Build Coastguard Worker // Removes a higher layered pool from the socket pool that |socket_| belongs 135*6777b538SAndroid Build Coastguard Worker // to. |higher_pool| must have been added by the above function. 136*6777b538SAndroid Build Coastguard Worker void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); 137*6777b538SAndroid Build Coastguard Worker 138*6777b538SAndroid Build Coastguard Worker // Closes idle sockets that are in the same group with |this|. 139*6777b538SAndroid Build Coastguard Worker void CloseIdleSocketsInGroup(const char* net_log_reason_utf8); 140*6777b538SAndroid Build Coastguard Worker 141*6777b538SAndroid Build Coastguard Worker // Returns true when Init() has completed successfully. is_initialized()142*6777b538SAndroid Build Coastguard Worker bool is_initialized() const { return is_initialized_; } 143*6777b538SAndroid Build Coastguard Worker 144*6777b538SAndroid Build Coastguard Worker // Sets the portion of LoadTimingInfo related to connection establishment, and 145*6777b538SAndroid Build Coastguard Worker // the socket id. |is_reused| is needed because the handle may not have full 146*6777b538SAndroid Build Coastguard Worker // reuse information. |load_timing_info| must have all default values when 147*6777b538SAndroid Build Coastguard Worker // called. Returns false and makes no changes to |load_timing_info| when 148*6777b538SAndroid Build Coastguard Worker // |socket_| is NULL. 149*6777b538SAndroid Build Coastguard Worker bool GetLoadTimingInfo(bool is_reused, 150*6777b538SAndroid Build Coastguard Worker LoadTimingInfo* load_timing_info) const; 151*6777b538SAndroid Build Coastguard Worker 152*6777b538SAndroid Build Coastguard Worker // Used by ClientSocketPool to initialize the ClientSocketHandle. 153*6777b538SAndroid Build Coastguard Worker // 154*6777b538SAndroid Build Coastguard Worker // SetSocket() may also be used if this handle is used as simply for 155*6777b538SAndroid Build Coastguard Worker // socket storage (e.g., http://crbug.com/37810). 156*6777b538SAndroid Build Coastguard Worker void SetSocket(std::unique_ptr<StreamSocket> s); 157*6777b538SAndroid Build Coastguard Worker 158*6777b538SAndroid Build Coastguard Worker // Populates several fields of |this| with error-related information from the 159*6777b538SAndroid Build Coastguard Worker // provided completed ConnectJob. Should only be called on ConnectJob failure. 160*6777b538SAndroid Build Coastguard Worker void SetAdditionalErrorState(ConnectJob* connect_job); 161*6777b538SAndroid Build Coastguard Worker set_reuse_type(SocketReuseType reuse_type)162*6777b538SAndroid Build Coastguard Worker void set_reuse_type(SocketReuseType reuse_type) { reuse_type_ = reuse_type; } set_idle_time(base::TimeDelta idle_time)163*6777b538SAndroid Build Coastguard Worker void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; } set_group_generation(int64_t group_generation)164*6777b538SAndroid Build Coastguard Worker void set_group_generation(int64_t group_generation) { 165*6777b538SAndroid Build Coastguard Worker group_generation_ = group_generation; 166*6777b538SAndroid Build Coastguard Worker } set_is_ssl_error(bool is_ssl_error)167*6777b538SAndroid Build Coastguard Worker void set_is_ssl_error(bool is_ssl_error) { is_ssl_error_ = is_ssl_error; } set_ssl_cert_request_info(scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info)168*6777b538SAndroid Build Coastguard Worker void set_ssl_cert_request_info( 169*6777b538SAndroid Build Coastguard Worker scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info) { 170*6777b538SAndroid Build Coastguard Worker ssl_cert_request_info_ = std::move(ssl_cert_request_info); 171*6777b538SAndroid Build Coastguard Worker } set_connection_attempts(const ConnectionAttempts & attempts)172*6777b538SAndroid Build Coastguard Worker void set_connection_attempts(const ConnectionAttempts& attempts) { 173*6777b538SAndroid Build Coastguard Worker connection_attempts_ = attempts; 174*6777b538SAndroid Build Coastguard Worker } resolve_error_info()175*6777b538SAndroid Build Coastguard Worker ResolveErrorInfo resolve_error_info() const { return resolve_error_info_; } 176*6777b538SAndroid Build Coastguard Worker 177*6777b538SAndroid Build Coastguard Worker // Only valid if there is no |socket_|. is_ssl_error()178*6777b538SAndroid Build Coastguard Worker bool is_ssl_error() const { 179*6777b538SAndroid Build Coastguard Worker DCHECK(!socket_); 180*6777b538SAndroid Build Coastguard Worker return is_ssl_error_; 181*6777b538SAndroid Build Coastguard Worker } 182*6777b538SAndroid Build Coastguard Worker 183*6777b538SAndroid Build Coastguard Worker // On an ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, the |cert_request_info| field 184*6777b538SAndroid Build Coastguard Worker // is set. ssl_cert_request_info()185*6777b538SAndroid Build Coastguard Worker scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info() const { 186*6777b538SAndroid Build Coastguard Worker return ssl_cert_request_info_; 187*6777b538SAndroid Build Coastguard Worker } 188*6777b538SAndroid Build Coastguard Worker 189*6777b538SAndroid Build Coastguard Worker // If the connection failed, returns the connection attempts made. connection_attempts()190*6777b538SAndroid Build Coastguard Worker const ConnectionAttempts& connection_attempts() { 191*6777b538SAndroid Build Coastguard Worker return connection_attempts_; 192*6777b538SAndroid Build Coastguard Worker } 193*6777b538SAndroid Build Coastguard Worker socket()194*6777b538SAndroid Build Coastguard Worker StreamSocket* socket() { return socket_.get(); } 195*6777b538SAndroid Build Coastguard Worker 196*6777b538SAndroid Build Coastguard Worker // SetSocket() must be called with a new socket before this handle 197*6777b538SAndroid Build Coastguard Worker // is destroyed if is_initialized() is true. 198*6777b538SAndroid Build Coastguard Worker std::unique_ptr<StreamSocket> PassSocket(); 199*6777b538SAndroid Build Coastguard Worker 200*6777b538SAndroid Build Coastguard Worker // These may only be used if is_initialized() is true. group_id()201*6777b538SAndroid Build Coastguard Worker const ClientSocketPool::GroupId& group_id() const { return group_id_; } group_generation()202*6777b538SAndroid Build Coastguard Worker int64_t group_generation() const { return group_generation_; } is_reused()203*6777b538SAndroid Build Coastguard Worker bool is_reused() const { return reuse_type_ == REUSED_IDLE; } idle_time()204*6777b538SAndroid Build Coastguard Worker base::TimeDelta idle_time() const { return idle_time_; } reuse_type()205*6777b538SAndroid Build Coastguard Worker SocketReuseType reuse_type() const { return reuse_type_; } connect_timing()206*6777b538SAndroid Build Coastguard Worker const LoadTimingInfo::ConnectTiming& connect_timing() const { 207*6777b538SAndroid Build Coastguard Worker return connect_timing_; 208*6777b538SAndroid Build Coastguard Worker } set_connect_timing(const LoadTimingInfo::ConnectTiming & connect_timing)209*6777b538SAndroid Build Coastguard Worker void set_connect_timing(const LoadTimingInfo::ConnectTiming& connect_timing) { 210*6777b538SAndroid Build Coastguard Worker connect_timing_ = connect_timing; 211*6777b538SAndroid Build Coastguard Worker } 212*6777b538SAndroid Build Coastguard Worker GetWeakPtr()213*6777b538SAndroid Build Coastguard Worker base::WeakPtr<ClientSocketHandle> GetWeakPtr() { 214*6777b538SAndroid Build Coastguard Worker return weak_factory_.GetWeakPtr(); 215*6777b538SAndroid Build Coastguard Worker } 216*6777b538SAndroid Build Coastguard Worker 217*6777b538SAndroid Build Coastguard Worker private: 218*6777b538SAndroid Build Coastguard Worker // Called on asynchronous completion of an Init() request. 219*6777b538SAndroid Build Coastguard Worker void OnIOComplete(int result); 220*6777b538SAndroid Build Coastguard Worker 221*6777b538SAndroid Build Coastguard Worker // Called on completion (both asynchronous & synchronous) of an Init() 222*6777b538SAndroid Build Coastguard Worker // request. 223*6777b538SAndroid Build Coastguard Worker void HandleInitCompletion(int result); 224*6777b538SAndroid Build Coastguard Worker 225*6777b538SAndroid Build Coastguard Worker // Resets the state of the ClientSocketHandle. |cancel| indicates whether or 226*6777b538SAndroid Build Coastguard Worker // not to try to cancel the request with the ClientSocketPool. Does not 227*6777b538SAndroid Build Coastguard Worker // reset the supplemental error state. |cancel_connect_job| indicates whether 228*6777b538SAndroid Build Coastguard Worker // a pending ConnectJob, if there is one in the SocketPool, should be 229*6777b538SAndroid Build Coastguard Worker // cancelled in addition to cancelling the request. It may only be true if 230*6777b538SAndroid Build Coastguard Worker // |cancel| is also true. 231*6777b538SAndroid Build Coastguard Worker void ResetInternal(bool cancel, bool cancel_connect_job); 232*6777b538SAndroid Build Coastguard Worker 233*6777b538SAndroid Build Coastguard Worker // Resets the supplemental error state. 234*6777b538SAndroid Build Coastguard Worker void ResetErrorState(); 235*6777b538SAndroid Build Coastguard Worker 236*6777b538SAndroid Build Coastguard Worker bool is_initialized_ = false; 237*6777b538SAndroid Build Coastguard Worker raw_ptr<ClientSocketPool> pool_ = nullptr; 238*6777b538SAndroid Build Coastguard Worker raw_ptr<HigherLayeredPool> higher_pool_ = nullptr; 239*6777b538SAndroid Build Coastguard Worker std::unique_ptr<StreamSocket> socket_; 240*6777b538SAndroid Build Coastguard Worker ClientSocketPool::GroupId group_id_; 241*6777b538SAndroid Build Coastguard Worker SocketReuseType reuse_type_ = ClientSocketHandle::UNUSED; 242*6777b538SAndroid Build Coastguard Worker CompletionOnceCallback callback_; 243*6777b538SAndroid Build Coastguard Worker base::TimeDelta idle_time_; 244*6777b538SAndroid Build Coastguard Worker // See ClientSocketPool::ReleaseSocket() for an explanation. 245*6777b538SAndroid Build Coastguard Worker int64_t group_generation_ = -1; 246*6777b538SAndroid Build Coastguard Worker ResolveErrorInfo resolve_error_info_; 247*6777b538SAndroid Build Coastguard Worker bool is_ssl_error_ = false; 248*6777b538SAndroid Build Coastguard Worker scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_; 249*6777b538SAndroid Build Coastguard Worker std::vector<ConnectionAttempt> connection_attempts_; 250*6777b538SAndroid Build Coastguard Worker 251*6777b538SAndroid Build Coastguard Worker NetLogSource requesting_source_; 252*6777b538SAndroid Build Coastguard Worker 253*6777b538SAndroid Build Coastguard Worker // Timing information is set when a connection is successfully established. 254*6777b538SAndroid Build Coastguard Worker LoadTimingInfo::ConnectTiming connect_timing_; 255*6777b538SAndroid Build Coastguard Worker 256*6777b538SAndroid Build Coastguard Worker base::WeakPtrFactory<ClientSocketHandle> weak_factory_{this}; 257*6777b538SAndroid Build Coastguard Worker }; 258*6777b538SAndroid Build Coastguard Worker 259*6777b538SAndroid Build Coastguard Worker } // namespace net 260*6777b538SAndroid Build Coastguard Worker 261*6777b538SAndroid Build Coastguard Worker #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ 262