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