tls_connection_factory.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 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 COMPONENTS_OPENSCREEN_PLATFORM_TLS_CONNECTION_FACTORY_H_
  5. #define COMPONENTS_OPENSCREEN_PLATFORM_TLS_CONNECTION_FACTORY_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "mojo/public/cpp/bindings/pending_receiver.h"
  9. #include "mojo/public/cpp/bindings/receiver.h"
  10. #include "mojo/public/cpp/bindings/remote.h"
  11. #include "services/network/public/mojom/tcp_socket.mojom.h"
  12. #include "services/network/public/mojom/tls_socket.mojom.h"
  13. #include "third_party/openscreen/src/platform/api/tls_connection_factory.h"
  14. #include "third_party/openscreen/src/platform/base/ip_address.h"
  15. #include "third_party/openscreen/src/platform/base/tls_connect_options.h"
  16. namespace net {
  17. class IPEndPoint;
  18. }
  19. namespace openscreen {
  20. class TaskRunner;
  21. struct TlsCredentials;
  22. struct TlsListenOptions;
  23. } // namespace openscreen
  24. namespace openscreen_platform {
  25. class TlsConnectionFactory final : public openscreen::TlsConnectionFactory {
  26. public:
  27. TlsConnectionFactory(openscreen::TlsConnectionFactory::Client* client,
  28. openscreen::TaskRunner* task_runner);
  29. ~TlsConnectionFactory() final;
  30. // TlsConnectionFactory overrides
  31. void Connect(const openscreen::IPEndpoint& remote_address,
  32. const openscreen::TlsConnectOptions& options) final;
  33. // Since Chrome doesn't implement TLS server sockets, these methods are not
  34. // implemented.
  35. void SetListenCredentials(
  36. const openscreen::TlsCredentials& credentials) final;
  37. void Listen(const openscreen::IPEndpoint& local_address,
  38. const openscreen::TlsListenOptions& options) final;
  39. private:
  40. // Note on TcpConnectRequest and TlsUpgradeRequest:
  41. // These classes are used to manage connection state for creating TCP.
  42. // connections and upgrading them to TLS. They are movable, but not copyable,
  43. // due to unique ownership of the mojo::Remotes, and passed into the TCP/TLS
  44. // callbacks (OnTcpConnect and OnTlsUpgrade) using currying.
  45. struct TcpConnectRequest {
  46. TcpConnectRequest(
  47. openscreen::TlsConnectOptions options_in,
  48. openscreen::IPEndpoint remote_address_in,
  49. mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket_in);
  50. TcpConnectRequest(const TcpConnectRequest&) = delete;
  51. TcpConnectRequest(TcpConnectRequest&&);
  52. TcpConnectRequest& operator=(const TcpConnectRequest&) = delete;
  53. TcpConnectRequest& operator=(TcpConnectRequest&&);
  54. ~TcpConnectRequest();
  55. openscreen::TlsConnectOptions options;
  56. openscreen::IPEndpoint remote_address;
  57. mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket;
  58. };
  59. struct TlsUpgradeRequest {
  60. TlsUpgradeRequest(
  61. openscreen::IPEndpoint local_address_in,
  62. openscreen::IPEndpoint remote_address_in,
  63. mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket_in,
  64. mojo::Remote<network::mojom::TLSClientSocket> tls_socket_in);
  65. TlsUpgradeRequest(const TlsUpgradeRequest&) = delete;
  66. TlsUpgradeRequest(TlsUpgradeRequest&&);
  67. TlsUpgradeRequest& operator=(const TlsUpgradeRequest&) = delete;
  68. TlsUpgradeRequest& operator=(TlsUpgradeRequest&&);
  69. ~TlsUpgradeRequest();
  70. openscreen::IPEndpoint local_address;
  71. openscreen::IPEndpoint remote_address;
  72. mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket;
  73. mojo::Remote<network::mojom::TLSClientSocket> tls_socket;
  74. };
  75. void OnTcpConnect(TcpConnectRequest request,
  76. int32_t net_result,
  77. const absl::optional<net::IPEndPoint>& local_address,
  78. const absl::optional<net::IPEndPoint>& remote_address,
  79. mojo::ScopedDataPipeConsumerHandle receive_stream,
  80. mojo::ScopedDataPipeProducerHandle send_stream);
  81. void OnTlsUpgrade(TlsUpgradeRequest request,
  82. int32_t net_result,
  83. mojo::ScopedDataPipeConsumerHandle receive_stream,
  84. mojo::ScopedDataPipeProducerHandle send_stream,
  85. const absl::optional<net::SSLInfo>& ssl_info);
  86. raw_ptr<openscreen::TlsConnectionFactory::Client> client_;
  87. const raw_ptr<openscreen::TaskRunner> task_runner_;
  88. base::WeakPtrFactory<TlsConnectionFactory> weak_factory_{this};
  89. };
  90. } // namespace openscreen_platform
  91. #endif // COMPONENTS_OPENSCREEN_PLATFORM_TLS_CONNECTION_FACTORY_H_