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