1 // Copyright 2024 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_STREAM_SOCKET_HANDLE_H_ 6 #define NET_SOCKET_STREAM_SOCKET_HANDLE_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 12 #include "net/base/load_timing_info.h" 13 #include "net/base/net_export.h" 14 15 namespace net { 16 17 class StreamSocket; 18 class HigherLayeredPool; 19 20 // A base class for handles that contain a StreamSocket. A subclass may have a 21 // concept of initialization, where a handle needs to be initialized before it 22 // can be used. A handle can be deinitialized by calling Reset(). 23 class NET_EXPORT_PRIVATE StreamSocketHandle { 24 public: 25 enum class SocketReuseType { 26 kUnused = 0, // unused socket that just finished connecting 27 kUnusedIdle, // unused socket that has been idle for awhile 28 kReusedIdle, // previously used socket 29 kNumTypes, 30 }; 31 32 StreamSocketHandle(); 33 34 StreamSocketHandle(const StreamSocketHandle&) = delete; 35 StreamSocketHandle& operator=(const StreamSocketHandle&) = delete; 36 37 virtual ~StreamSocketHandle(); 38 39 // Returns true when `this` is initialized successfully. is_initialized()40 bool is_initialized() const { return is_initialized_; } 41 socket()42 StreamSocket* socket() { return socket_.get(); } socket()43 StreamSocket* socket() const { return socket_.get(); } 44 45 // Sets `socket` to `this`. 46 void SetSocket(std::unique_ptr<StreamSocket> socket); 47 48 // Releases the ownership of `socket`. 49 std::unique_ptr<StreamSocket> PassSocket(); 50 51 // Sets the portion of LoadTimingInfo related to connection establishment, and 52 // the socket id. `is_reused` is needed because the handle may not have full 53 // reuse information. `load_timing_info` must have all default values when 54 // called. Returns false and makes no changes to `load_timing_info` when 55 // `socket_` is nullptr. 56 bool GetLoadTimingInfo(bool is_reused, 57 LoadTimingInfo* load_timing_info) const; 58 reuse_type()59 SocketReuseType reuse_type() const { return reuse_type_; } set_reuse_type(SocketReuseType reuse_type)60 void set_reuse_type(SocketReuseType reuse_type) { reuse_type_ = reuse_type; } 61 connect_timing()62 const LoadTimingInfo::ConnectTiming& connect_timing() const { 63 return connect_timing_; 64 } set_connect_timing(const LoadTimingInfo::ConnectTiming & connect_timing)65 void set_connect_timing(const LoadTimingInfo::ConnectTiming& connect_timing) { 66 connect_timing_ = connect_timing; 67 } 68 69 // If this handle is associated with a pool that has the concept of higher 70 // layered pools, adds/removes a higher layered pool to the pool. Otherwise, 71 // does nothing. 72 virtual void AddHigherLayeredPool(HigherLayeredPool* higher_pool); 73 virtual void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); 74 75 // Releases the underlying socket and uninitializes `this`. 76 virtual void Reset() = 0; 77 78 // Returns true if the pool that is associated with this handle is stalled. 79 virtual bool IsPoolStalled() const = 0; 80 81 protected: set_is_initialized(bool is_initialized)82 void set_is_initialized(bool is_initialized) { 83 is_initialized_ = is_initialized; 84 } 85 86 private: 87 bool is_initialized_ = false; 88 std::unique_ptr<StreamSocket> socket_; 89 SocketReuseType reuse_type_ = SocketReuseType::kUnused; 90 91 // Timing information is set when a connection is successfully established. 92 LoadTimingInfo::ConnectTiming connect_timing_; 93 }; 94 95 } // namespace net 96 97 #endif // NET_SOCKET_STREAM_SOCKET_HANDLE_H_ 98