summaryrefslogtreecommitdiff
path: root/chromium/components/openscreen_platform/tls_connection_factory.h
blob: 379aec3c06af610fd6e8fb276eee6a8b6439479b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_OPENSCREEN_PLATFORM_TLS_CONNECTION_FACTORY_H_
#define COMPONENTS_OPENSCREEN_PLATFORM_TLS_CONNECTION_FACTORY_H_

#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/public/mojom/tcp_socket.mojom.h"
#include "services/network/public/mojom/tls_socket.mojom.h"
#include "third_party/openscreen/src/platform/api/tls_connection_factory.h"
#include "third_party/openscreen/src/platform/base/ip_address.h"
#include "third_party/openscreen/src/platform/base/tls_connect_options.h"

namespace net {
class IPEndPoint;
}

namespace openscreen {

class TaskRunner;
struct TlsCredentials;
struct TlsListenOptions;

}  // namespace openscreen

namespace openscreen_platform {

class TlsConnectionFactory final : public openscreen::TlsConnectionFactory {
 public:
  TlsConnectionFactory(openscreen::TlsConnectionFactory::Client* client,
                       openscreen::TaskRunner* task_runner);

  ~TlsConnectionFactory() final;

  // TlsConnectionFactory overrides
  void Connect(const openscreen::IPEndpoint& remote_address,
               const openscreen::TlsConnectOptions& options) final;

  // Since Chrome doesn't implement TLS server sockets, these methods are not
  // implemented.
  void SetListenCredentials(
      const openscreen::TlsCredentials& credentials) final;
  void Listen(const openscreen::IPEndpoint& local_address,
              const openscreen::TlsListenOptions& options) final;

 private:
  // Note on TcpConnectRequest and TlsUpgradeRequest:
  // These classes are used to manage connection state for creating TCP.
  // connections and upgrading them to TLS. They are movable, but not copyable,
  // due to unique ownership of the mojo::Remotes, and passed into the TCP/TLS
  // callbacks (OnTcpConnect and OnTlsUpgrade) using currying.
  struct TcpConnectRequest {
    TcpConnectRequest(
        openscreen::TlsConnectOptions options_in,
        openscreen::IPEndpoint remote_address_in,
        mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket_in);
    TcpConnectRequest(const TcpConnectRequest&) = delete;
    TcpConnectRequest(TcpConnectRequest&&);
    TcpConnectRequest& operator=(const TcpConnectRequest&) = delete;
    TcpConnectRequest& operator=(TcpConnectRequest&&);
    ~TcpConnectRequest();

    openscreen::TlsConnectOptions options;
    openscreen::IPEndpoint remote_address;
    mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket;
  };

  struct TlsUpgradeRequest {
    TlsUpgradeRequest(
        openscreen::IPEndpoint local_address_in,
        openscreen::IPEndpoint remote_address_in,
        mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket_in,
        mojo::Remote<network::mojom::TLSClientSocket> tls_socket_in);
    TlsUpgradeRequest(const TlsUpgradeRequest&) = delete;
    TlsUpgradeRequest(TlsUpgradeRequest&&);
    TlsUpgradeRequest& operator=(const TlsUpgradeRequest&) = delete;
    TlsUpgradeRequest& operator=(TlsUpgradeRequest&&);
    ~TlsUpgradeRequest();

    openscreen::IPEndpoint local_address;
    openscreen::IPEndpoint remote_address;
    mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket;
    mojo::Remote<network::mojom::TLSClientSocket> tls_socket;
  };

  void OnTcpConnect(TcpConnectRequest request,
                    int32_t net_result,
                    const absl::optional<net::IPEndPoint>& local_address,
                    const absl::optional<net::IPEndPoint>& remote_address,
                    mojo::ScopedDataPipeConsumerHandle receive_stream,
                    mojo::ScopedDataPipeProducerHandle send_stream);

  void OnTlsUpgrade(TlsUpgradeRequest request,
                    int32_t net_result,
                    mojo::ScopedDataPipeConsumerHandle receive_stream,
                    mojo::ScopedDataPipeProducerHandle send_stream,
                    const absl::optional<net::SSLInfo>& ssl_info);

  openscreen::TlsConnectionFactory::Client* client_;
  openscreen::TaskRunner* const task_runner_;
  base::WeakPtrFactory<TlsConnectionFactory> weak_factory_{this};
};

}  // namespace openscreen_platform

#endif  // COMPONENTS_OPENSCREEN_PLATFORM_TLS_CONNECTION_FACTORY_H_