[go: nahoru, domu]

blob: 46b9a9b0d8c7203567df86ba08a32075455a4dbc [file] [log] [blame]
Helen Lid8672462018-03-15 16:10:121// 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 Lid8672462018-03-15 16:10:1211#include "base/macros.h"
12#include "base/memory/ref_counted.h"
13#include "mojo/public/cpp/system/data_pipe.h"
Helen Lid8672462018-03-15 16:10:1214#include "net/base/address_family.h"
Helen Lid8672462018-03-15 16:10:1215#include "net/base/ip_endpoint.h"
Helen Lid8672462018-03-15 16:10:1216#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 Menke5caf50e2018-11-13 23:57:2919#include "services/network/public/mojom/address_family.mojom.h"
20#include "services/network/public/mojom/ip_endpoint.mojom.h"
Helen Li68147c82018-05-31 23:34:5321#include "services/network/public/mojom/network_context.mojom.h"
Helen Lid8672462018-03-15 16:10:1222#include "services/network/public/mojom/tcp_socket.mojom.h"
Helen Li17c77ccb2018-04-19 21:29:1823#include "services/network/socket_data_pump.h"
Maks Orlovich38ff9682018-09-05 18:25:5724#include "services/network/tls_socket_factory.h"
Helen Lid8672462018-03-15 16:10:1225
26namespace net {
27class NetLog;
Helen Li17c77ccb2018-04-19 21:29:1828class ClientSocketFactory;
Helen Li48f117e2018-05-29 20:38:4029class TransportClientSocket;
Helen Lid8672462018-03-15 16:10:1230} // namespace net
31
32namespace network {
33
34class COMPONENT_EXPORT(NETWORK_SERVICE) TCPConnectedSocket
Helen Lia6d3b2c2018-05-08 16:09:0735 : public mojom::TCPConnectedSocket,
Maks Orlovich38ff9682018-09-05 18:25:5736 public SocketDataPump::Delegate,
37 public TLSSocketFactory::Delegate {
Helen Lid8672462018-03-15 16:10:1238 public:
Matt Menkeb52e5772018-09-19 19:25:3839 // Max send/receive buffer size the consumer is allowed to set. Exposed for
40 // testing.
41 static const int kMaxBufferSize;
42
Matt Menkef8fb81b2018-09-22 00:57:0543 // If |client_socket_factory| is nullptr, consumers must use
44 // ConnectWithSocket() instead of Connect().
Helen Lid8672462018-03-15 16:10:1245 TCPConnectedSocket(
Helen Lia6d3b2c2018-05-08 16:09:0746 mojom::SocketObserverPtr observer,
Helen Lid8672462018-03-15 16:10:1247 net::NetLog* net_log,
Maks Orlovich38ff9682018-09-05 18:25:5748 TLSSocketFactory* tls_socket_factory,
Helen Lid5bb9222018-04-12 15:33:0949 net::ClientSocketFactory* client_socket_factory,
Helen Lid8672462018-03-15 16:10:1250 const net::NetworkTrafficAnnotationTag& traffic_annotation);
51 TCPConnectedSocket(
Helen Lia6d3b2c2018-05-08 16:09:0752 mojom::SocketObserverPtr observer,
Helen Li48f117e2018-05-29 20:38:4053 std::unique_ptr<net::TransportClientSocket> socket,
Helen Lid8672462018-03-15 16:10:1254 mojo::ScopedDataPipeProducerHandle receive_pipe_handle,
55 mojo::ScopedDataPipeConsumerHandle send_pipe_handle,
56 const net::NetworkTrafficAnnotationTag& traffic_annotation);
57 ~TCPConnectedSocket() override;
Matt Menkeb52e5772018-09-19 19:25:3858
Helen Lid8672462018-03-15 16:10:1259 void Connect(
60 const base::Optional<net::IPEndPoint>& local_addr,
61 const net::AddressList& remote_addr_list,
Matt Menkeb52e5772018-09-19 19:25:3862 mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
Helen Lid8672462018-03-15 16:10:1263 mojom::NetworkContext::CreateTCPConnectedSocketCallback callback);
64
Matt Menkef8fb81b2018-09-22 00:57:0565 // 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 Lid8672462018-03-15 16:10:1273 // mojom::TCPConnectedSocket implementation.
Helen Lia6d3b2c2018-05-08 16:09:0774 void UpgradeToTLS(
75 const net::HostPortPair& host_port_pair,
Helen Li48f117e2018-05-29 20:38:4076 mojom::TLSClientSocketOptionsPtr socket_options,
Helen Lia6d3b2c2018-05-08 16:09:0777 const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
78 mojom::TLSClientSocketRequest request,
79 mojom::SocketObserverPtr observer,
Maks Orlovich38ff9682018-09-05 18:25:5780 mojom::TCPConnectedSocket::UpgradeToTLSCallback callback) override;
Matt Menkeb52e5772018-09-19 19:25:3881 void SetSendBufferSize(int send_buffer_size,
82 SetSendBufferSizeCallback callback) override;
83 void SetReceiveBufferSize(int send_buffer_size,
84 SetSendBufferSizeCallback callback) override;
Helen Li48f117e2018-05-29 20:38:4085 void SetNoDelay(bool no_delay, SetNoDelayCallback callback) override;
86 void SetKeepAlive(bool enable,
87 int32_t delay_secs,
88 SetKeepAliveCallback callback) override;
Helen Lia6d3b2c2018-05-08 16:09:0789
Helen Lid8672462018-03-15 16:10:1290 private:
91 // Invoked when net::TCPClientSocket::Connect() completes.
92 void OnConnectCompleted(int net_result);
93
Helen Lia6d3b2c2018-05-08 16:09:0794 // SocketDataPump::Delegate implementation.
95 void OnNetworkReadError(int net_error) override;
96 void OnNetworkWriteError(int net_error) override;
97 void OnShutdown() override;
Helen Lid8672462018-03-15 16:10:1298
Maks Orlovich38ff9682018-09-05 18:25:5799 // TLSSocketFactory::Delegate implementation.
100 const net::StreamSocket* BorrowSocket() override;
101 std::unique_ptr<net::StreamSocket> TakeSocket() override;
102
Helen Lia6d3b2c2018-05-08 16:09:07103 const mojom::SocketObserverPtr observer_;
104
105 net::NetLog* const net_log_;
Helen Lia6d3b2c2018-05-08 16:09:07106 net::ClientSocketFactory* const client_socket_factory_;
Maks Orlovich38ff9682018-09-05 18:25:57107 TLSSocketFactory* tls_socket_factory_;
Helen Lid8672462018-03-15 16:10:12108
Helen Li48f117e2018-05-29 20:38:40109 std::unique_ptr<net::TransportClientSocket> socket_;
Helen Lid8672462018-03-15 16:10:12110
111 mojom::NetworkContext::CreateTCPConnectedSocketCallback connect_callback_;
112
Helen Lia6d3b2c2018-05-08 16:09:07113 base::OnceClosure pending_upgrade_to_tls_callback_;
114
Helen Li17c77ccb2018-04-19 21:29:18115 std::unique_ptr<SocketDataPump> socket_data_pump_;
Helen Lid8672462018-03-15 16:10:12116
Helen Lia6d3b2c2018-05-08 16:09:07117 const net::NetworkTrafficAnnotationTag traffic_annotation_;
Helen Lid8672462018-03-15 16:10:12118
119 DISALLOW_COPY_AND_ASSIGN(TCPConnectedSocket);
120};
121
122} // namespace network
123
124#endif // SERVICES_NETWORK_TCP_CONNECTED_SOCKET_H_