Avi Drissman | 0987565 | 2022-09-15 20:03:19 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Liza Burakova | e948bde | 2022-04-22 19:19:50 | [diff] [blame] | 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_BROKERED_CLIENT_SOCKET_FACTORY_H_ |
| 6 | #define SERVICES_NETWORK_BROKERED_CLIENT_SOCKET_FACTORY_H_ |
| 7 | |
Liza Burakova | e948bde | 2022-04-22 19:19:50 | [diff] [blame] | 8 | #include "base/component_export.h" |
Will Harris | c96b918 | 2022-09-09 22:35:28 | [diff] [blame] | 9 | #include "build/build_config.h" |
Liza Burakova | f76ff82 | 2022-06-17 23:02:32 | [diff] [blame] | 10 | #include "mojo/public/cpp/bindings/remote.h" |
Liza Burakova | e948bde | 2022-04-22 19:19:50 | [diff] [blame] | 11 | #include "net/socket/client_socket_factory.h" |
| 12 | #include "net/socket/datagram_socket.h" |
| 13 | #include "net/socket/socket_performance_watcher.h" |
| 14 | #include "net/socket/transport_client_socket.h" |
| 15 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Liza Burakova | f76ff82 | 2022-06-17 23:02:32 | [diff] [blame] | 16 | #include "services/network/public/mojom/socket_broker.mojom.h" |
Liza Burakova | e948bde | 2022-04-22 19:19:50 | [diff] [blame] | 17 | |
Will Harris | c96b918 | 2022-09-09 22:35:28 | [diff] [blame] | 18 | #if BUILDFLAG(IS_WIN) |
| 19 | #include "services/network/broker_helper_win.h" |
| 20 | #endif |
| 21 | |
Liza Burakova | e948bde | 2022-04-22 19:19:50 | [diff] [blame] | 22 | namespace net { |
| 23 | |
| 24 | class AddressList; |
| 25 | class DatagramClientSocket; |
| 26 | class HostPortPair; |
| 27 | class NetLog; |
| 28 | struct NetLogSource; |
| 29 | class SSLClientContext; |
| 30 | class SSLClientSocket; |
| 31 | struct SSLConfig; |
| 32 | class NetworkQualityEstimator; |
| 33 | |
| 34 | } // namespace net |
| 35 | |
| 36 | namespace network { |
| 37 | |
| 38 | // A ClientSocketFactory to create brokered sockets. |
| 39 | class COMPONENT_EXPORT(NETWORK_SERVICE) BrokeredClientSocketFactory |
| 40 | : public net::ClientSocketFactory { |
| 41 | public: |
Liza Burakova | f76ff82 | 2022-06-17 23:02:32 | [diff] [blame] | 42 | explicit BrokeredClientSocketFactory( |
| 43 | mojo::PendingRemote<mojom::SocketBroker> pending_remote); |
Liza Burakova | e948bde | 2022-04-22 19:19:50 | [diff] [blame] | 44 | ~BrokeredClientSocketFactory() override; |
| 45 | |
| 46 | BrokeredClientSocketFactory(const BrokeredClientSocketFactory&) = delete; |
| 47 | BrokeredClientSocketFactory& operator=(const BrokeredClientSocketFactory&) = |
| 48 | delete; |
| 49 | |
| 50 | // ClientSocketFactory: |
| 51 | std::unique_ptr<net::DatagramClientSocket> CreateDatagramClientSocket( |
| 52 | net::DatagramSocket::BindType bind_type, |
| 53 | net::NetLog* net_log, |
| 54 | const net::NetLogSource& source) override; |
| 55 | std::unique_ptr<net::TransportClientSocket> CreateTransportClientSocket( |
| 56 | const net::AddressList& addresses, |
| 57 | std::unique_ptr<net::SocketPerformanceWatcher> socket_performance_watcher, |
| 58 | net::NetworkQualityEstimator* network_quality_estimator, |
| 59 | net::NetLog* net_log, |
| 60 | const net::NetLogSource& source) override; |
| 61 | std::unique_ptr<net::SSLClientSocket> CreateSSLClientSocket( |
| 62 | net::SSLClientContext* context, |
| 63 | std::unique_ptr<net::StreamSocket> stream_socket, |
| 64 | const net::HostPortPair& host_and_port, |
| 65 | const net::SSLConfig& ssl_config) override; |
Liza Burakova | f76ff82 | 2022-06-17 23:02:32 | [diff] [blame] | 66 | |
| 67 | // Sends an IPC to the SocketBroker to create a new TCP socket. |
| 68 | void BrokerCreateTcpSocket( |
| 69 | net::AddressFamily address_family, |
| 70 | mojom::SocketBroker::CreateTcpSocketCallback callback); |
| 71 | |
Liza Burakova | 39fb9af3 | 2023-02-23 18:42:04 | [diff] [blame] | 72 | // Sends an IPC to the SocketBroker to create a new UDP socket. |
| 73 | void BrokerCreateUdpSocket( |
| 74 | net::AddressFamily address_family, |
| 75 | mojom::SocketBroker::CreateUdpSocketCallback callback); |
| 76 | |
Liza Burakova | 96f30bbc | 2023-05-16 15:32:33 | [diff] [blame] | 77 | // Whether or not a socket for `addresses` should be brokered or not. Virtual |
| 78 | // for testing. |
| 79 | virtual bool ShouldBroker(const net::AddressList& addresses) const; |
Will Harris | c96b918 | 2022-09-09 22:35:28 | [diff] [blame] | 80 | |
Liza Burakova | 96f30bbc | 2023-05-16 15:32:33 | [diff] [blame] | 81 | private: |
Liza Burakova | f76ff82 | 2022-06-17 23:02:32 | [diff] [blame] | 82 | mojo::Remote<mojom::SocketBroker> socket_broker_; |
Will Harris | c96b918 | 2022-09-09 22:35:28 | [diff] [blame] | 83 | #if BUILDFLAG(IS_WIN) |
| 84 | BrokerHelperWin broker_helper_; |
| 85 | #endif |
Liza Burakova | e948bde | 2022-04-22 19:19:50 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace network |
| 89 | |
| 90 | #endif // SERVICES_NETWORK_BROKERED_CLIENT_SOCKET_FACTORY_H_ |