tcp_connected_socket.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2018 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_CONNECTED_SOCKET_H_
  5. #define SERVICES_NETWORK_TCP_CONNECTED_SOCKET_H_
  6. #include <memory>
  7. #include "base/component_export.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "mojo/public/cpp/bindings/pending_remote.h"
  11. #include "mojo/public/cpp/bindings/remote.h"
  12. #include "mojo/public/cpp/system/data_pipe.h"
  13. #include "net/base/address_family.h"
  14. #include "net/base/ip_endpoint.h"
  15. #include "net/socket/tcp_client_socket.h"
  16. #include "net/traffic_annotation/network_traffic_annotation.h"
  17. #include "services/network/public/cpp/net_adapters.h"
  18. #include "services/network/public/mojom/address_family.mojom.h"
  19. #include "services/network/public/mojom/ip_endpoint.mojom.h"
  20. #include "services/network/public/mojom/network_context.mojom.h"
  21. #include "services/network/public/mojom/tcp_socket.mojom.h"
  22. #include "services/network/socket_data_pump.h"
  23. #include "services/network/tls_socket_factory.h"
  24. namespace net {
  25. class NetLog;
  26. class ClientSocketFactory;
  27. class TransportClientSocket;
  28. } // namespace net
  29. namespace network {
  30. class COMPONENT_EXPORT(NETWORK_SERVICE) TCPConnectedSocket
  31. : public mojom::TCPConnectedSocket,
  32. public SocketDataPump::Delegate,
  33. public TLSSocketFactory::Delegate {
  34. public:
  35. // Max send/receive buffer size the consumer is allowed to set. Exposed for
  36. // testing.
  37. static const int kMaxBufferSize;
  38. // If |client_socket_factory| is nullptr, consumers must use
  39. // ConnectWithSocket() instead of Connect().
  40. TCPConnectedSocket(
  41. mojo::PendingRemote<mojom::SocketObserver> observer,
  42. net::NetLog* net_log,
  43. TLSSocketFactory* tls_socket_factory,
  44. net::ClientSocketFactory* client_socket_factory,
  45. const net::NetworkTrafficAnnotationTag& traffic_annotation);
  46. TCPConnectedSocket(
  47. mojo::PendingRemote<mojom::SocketObserver> observer,
  48. std::unique_ptr<net::TransportClientSocket> socket,
  49. mojo::ScopedDataPipeProducerHandle receive_pipe_handle,
  50. mojo::ScopedDataPipeConsumerHandle send_pipe_handle,
  51. const net::NetworkTrafficAnnotationTag& traffic_annotation);
  52. TCPConnectedSocket(const TCPConnectedSocket&) = delete;
  53. TCPConnectedSocket& operator=(const TCPConnectedSocket&) = delete;
  54. ~TCPConnectedSocket() override;
  55. void Connect(
  56. const absl::optional<net::IPEndPoint>& local_addr,
  57. const net::AddressList& remote_addr_list,
  58. mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
  59. mojom::NetworkContext::CreateTCPConnectedSocketCallback callback);
  60. // Tries to connects using the provided TCPClientSocket. |socket| owns the
  61. // list of addresses to try to connect to, so this method doesn't need any
  62. // addresses as input.
  63. void ConnectWithSocket(
  64. std::unique_ptr<net::TransportClientSocket> socket,
  65. mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
  66. mojom::NetworkContext::CreateTCPConnectedSocketCallback callback);
  67. // mojom::TCPConnectedSocket implementation.
  68. void UpgradeToTLS(
  69. const net::HostPortPair& host_port_pair,
  70. mojom::TLSClientSocketOptionsPtr socket_options,
  71. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
  72. mojo::PendingReceiver<mojom::TLSClientSocket> receiver,
  73. mojo::PendingRemote<mojom::SocketObserver> observer,
  74. mojom::TCPConnectedSocket::UpgradeToTLSCallback callback) override;
  75. void SetSendBufferSize(int send_buffer_size,
  76. SetSendBufferSizeCallback callback) override;
  77. void SetReceiveBufferSize(int send_buffer_size,
  78. SetSendBufferSizeCallback callback) override;
  79. void SetNoDelay(bool no_delay, SetNoDelayCallback callback) override;
  80. void SetKeepAlive(bool enable,
  81. int32_t delay_secs,
  82. SetKeepAliveCallback callback) override;
  83. private:
  84. // Invoked when net::TCPClientSocket::Connect() completes.
  85. void OnConnectCompleted(int net_result);
  86. // SocketDataPump::Delegate implementation.
  87. void OnNetworkReadError(int net_error) override;
  88. void OnNetworkWriteError(int net_error) override;
  89. void OnShutdown() override;
  90. // TLSSocketFactory::Delegate implementation.
  91. const net::StreamSocket* BorrowSocket() override;
  92. std::unique_ptr<net::StreamSocket> TakeSocket() override;
  93. const mojo::Remote<mojom::SocketObserver> observer_;
  94. const raw_ptr<net::NetLog> net_log_;
  95. const raw_ptr<net::ClientSocketFactory> client_socket_factory_;
  96. raw_ptr<TLSSocketFactory> tls_socket_factory_;
  97. std::unique_ptr<net::TransportClientSocket> socket_;
  98. mojom::NetworkContext::CreateTCPConnectedSocketCallback connect_callback_;
  99. mojom::TCPConnectedSocketOptionsPtr socket_options_;
  100. base::OnceClosure pending_upgrade_to_tls_callback_;
  101. std::unique_ptr<SocketDataPump> socket_data_pump_;
  102. const net::NetworkTrafficAnnotationTag traffic_annotation_;
  103. };
  104. } // namespace network
  105. #endif // SERVICES_NETWORK_TCP_CONNECTED_SOCKET_H_