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