tcp_client_socket_brokered.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2022 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. #ifndef SERVICES_NETWORK_TCP_CLIENT_SOCKET_BROKERED_H_
  5. #define SERVICES_NETWORK_TCP_CLIENT_SOCKET_BROKERED_H_
  6. #include "base/component_export.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "build/build_config.h"
  10. #include "mojo/public/cpp/platform/platform_handle.h"
  11. #include "net/base/address_list.h"
  12. #include "net/base/completion_once_callback.h"
  13. #include "net/nqe/network_quality_estimator.h"
  14. #include "net/socket/socket_tag.h"
  15. #include "net/socket/tcp_socket.h"
  16. #include "net/socket/transport_client_socket.h"
  17. namespace net {
  18. class NetLog;
  19. struct NetLogSource;
  20. class SocketPerformanceWatcher;
  21. class NetworkQualityEstimator;
  22. class SocketTag;
  23. } // namespace net
  24. namespace network {
  25. class BrokeredClientSocketFactory;
  26. // A client socket used exclusively with a socket broker. Currently intended for
  27. // Windows and Android only. Not intended to be used by non-brokered
  28. // connections. Generally, all calls pass through to an underlying
  29. // TCPClientSocket API, but Bind and Connect are the sent to a privileged
  30. // process using the net:SocketBroker interface. This is because socket creation
  31. // needs to be brokered, and TCPClientSocket only creates and opens a socket
  32. // within Bind and Connect.
  33. class COMPONENT_EXPORT(NETWORK_SERVICE) TCPClientSocketBrokered
  34. : public net::TransportClientSocket {
  35. public:
  36. TCPClientSocketBrokered(
  37. const net::AddressList& addresses,
  38. std::unique_ptr<net::SocketPerformanceWatcher>
  39. brokered_socket_performance_watcher,
  40. net::NetworkQualityEstimator* network_quality_estimator,
  41. net::NetLog* net_log,
  42. const net::NetLogSource& source,
  43. BrokeredClientSocketFactory* client_socket_factory);
  44. ~TCPClientSocketBrokered() override;
  45. TCPClientSocketBrokered(const TCPClientSocketBrokered&) = delete;
  46. TCPClientSocketBrokered& operator=(const TCPClientSocketBrokered&) = delete;
  47. // TransportClientSocket implementation.
  48. int Bind(const net::IPEndPoint& address) override;
  49. bool SetKeepAlive(bool enable, int delay) override;
  50. bool SetNoDelay(bool no_delay) override;
  51. // StreamSocket implementation.
  52. void SetBeforeConnectCallback(
  53. const BeforeConnectCallback& before_connect_callback) override;
  54. int Connect(net::CompletionOnceCallback callback) override;
  55. void Disconnect() override;
  56. bool IsConnected() const override;
  57. bool IsConnectedAndIdle() const override;
  58. int GetPeerAddress(net::IPEndPoint* address) const override;
  59. int GetLocalAddress(net::IPEndPoint* address) const override;
  60. const net::NetLogWithSource& NetLog() const override;
  61. bool WasEverUsed() const override;
  62. bool WasAlpnNegotiated() const override;
  63. net::NextProto GetNegotiatedProtocol() const override;
  64. bool GetSSLInfo(net::SSLInfo* ssl_info) override;
  65. int64_t GetTotalReceivedBytes() const override;
  66. void ApplySocketTag(const net::SocketTag& tag) override;
  67. // Socket implementation.
  68. // Multiple outstanding requests are not supported.
  69. // Full duplex mode (reading and writing at the same time) is supported.
  70. int Read(net::IOBuffer* buf,
  71. int buf_len,
  72. net::CompletionOnceCallback callback) override;
  73. int ReadIfReady(net::IOBuffer* buf,
  74. int buf_len,
  75. net::CompletionOnceCallback callback) override;
  76. int CancelReadIfReady() override;
  77. int Write(
  78. net::IOBuffer* buf,
  79. int buf_len,
  80. net::CompletionOnceCallback callback,
  81. const net::NetworkTrafficAnnotationTag& traffic_annotation) override;
  82. int SetReceiveBufferSize(int32_t size) override;
  83. int SetSendBufferSize(int32_t size) override;
  84. private:
  85. int OpenSocketForBind(const net::IPEndPoint& address);
  86. void DidCompleteOpenForBind(const net::IPEndPoint& address,
  87. std::unique_ptr<net::TCPSocket> new_socket,
  88. net::Error result);
  89. void DidCompleteConnect(net::CompletionOnceCallback callback, int result);
  90. void DidCompleteCreate(net::CompletionOnceCallback callback,
  91. mojo::PlatformHandle fd,
  92. int result);
  93. // The list of addresses we should try in order to establish a connection.
  94. net::AddressList addresses_;
  95. // Arguments for creating a new TCPClientSocket
  96. std::unique_ptr<net::SocketPerformanceWatcher> socket_performance_watcher_;
  97. raw_ptr<net::NetworkQualityEstimator> network_quality_estimator_;
  98. raw_ptr<net::NetLog> net_log_;
  99. const net::NetLogSource source_;
  100. // State to track whether socket is currently attempting to connect.
  101. bool is_connect_in_progress_ = false;
  102. // Need to store the tag in case ApplySocketTag() is called before Connect().
  103. net::SocketTag tag_;
  104. // The underlying brokered socket. Created when the socket is created for
  105. // Connect().
  106. std::unique_ptr<net::TransportClientSocket> brokered_socket_;
  107. // The ClientSocketFactory that created this socket. Used to send IPCs to the
  108. // remote SocketBroker.
  109. const raw_ptr<BrokeredClientSocketFactory> client_socket_factory_;
  110. base::WeakPtrFactory<TCPClientSocketBrokered> brokered_weak_ptr_factory_{
  111. this};
  112. };
  113. } // namespace network
  114. #endif // SERVICES_NETWORK_TCP_CLIENT_SOCKET_BROKERED_H_