transport.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2022 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 MOJO_CORE_IPCZ_DRIVER_TRANSPORT_H_
  5. #define MOJO_CORE_IPCZ_DRIVER_TRANSPORT_H_
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <utility>
  9. #include "base/containers/span.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/synchronization/lock.h"
  12. #include "base/task/single_thread_task_runner.h"
  13. #include "mojo/core/channel.h"
  14. #include "mojo/core/ipcz_driver/object.h"
  15. #include "mojo/core/system_impl_export.h"
  16. #include "mojo/public/cpp/platform/platform_channel_endpoint.h"
  17. #include "mojo/public/cpp/platform/platform_handle.h"
  18. #include "third_party/ipcz/include/ipcz/ipcz.h"
  19. namespace mojo::core::ipcz_driver {
  20. // An ipcz driver transport implementation backed by a Channel object.
  21. class MOJO_SYSTEM_IMPL_EXPORT Transport : public Object<Transport>,
  22. public Channel::Delegate {
  23. public:
  24. // Tracks what type of remote process is on the other end of this transport.
  25. // This is used for handle brokering decisions on Windows.
  26. enum Destination : uint32_t {
  27. kToNonBroker,
  28. kToBroker,
  29. };
  30. Transport(Destination destination,
  31. PlatformChannelEndpoint endpoint,
  32. base::Process remote_process = base::Process());
  33. static std::pair<scoped_refptr<Transport>, scoped_refptr<Transport>>
  34. CreatePair(Destination first_destination, Destination second_destination);
  35. static constexpr Type object_type() { return kTransport; }
  36. Destination destination() const { return destination_; }
  37. const base::Process& remote_process() const { return remote_process_; }
  38. // Activates this transport by creating and starting the underlying Channel
  39. // instance.
  40. bool Activate(IpczHandle transport,
  41. IpczTransportActivityHandler activity_handler);
  42. // Deactives this transport, release and calling ShutDown() on the underlying
  43. // Channel. Channel shutdown is asynchronous and will conclude with an
  44. // OnChannelDestroyed() invocation on this Transport.
  45. bool Deactivate();
  46. // Transmits `data` and `handles` over the underlying Channel. All handles in
  47. // `handles` must reference TransmissibleHandle instances with an underlying
  48. // handle the Channel can transmit out-of-band from `data`.
  49. bool Transmit(base::span<const uint8_t> data,
  50. base::span<const IpczDriverHandle> handles);
  51. // Attempts to serialize `object` for eventual transmission over this
  52. // Transport. This essentially implements the mojo-ipcz driver's Serialize()
  53. // API and behaves according to its specification. Upon success, `object` may
  54. // be invalidated.
  55. IpczResult SerializeObject(ObjectBase& object,
  56. void* data,
  57. size_t* num_bytes,
  58. IpczDriverHandle* handles,
  59. size_t* num_handles);
  60. // Deserializes a new driver object from `bytes` and `handles` received over
  61. // this Transport.
  62. IpczResult DeserializeObject(base::span<const uint8_t> bytes,
  63. base::span<const IpczDriverHandle> handles,
  64. scoped_refptr<ObjectBase>& object);
  65. // Object:
  66. void Close() override;
  67. bool IsSerializable() const override;
  68. bool GetSerializedDimensions(Transport& transmitter,
  69. size_t& num_bytes,
  70. size_t& num_handles) override;
  71. bool Serialize(Transport& transmitter,
  72. base::span<uint8_t> data,
  73. base::span<PlatformHandle> handles) override;
  74. static scoped_refptr<Transport> Deserialize(
  75. base::span<const uint8_t> data,
  76. base::span<PlatformHandle> handles);
  77. // Channel::Delegate:
  78. bool IsIpczTransport() const override;
  79. void OnChannelMessage(const void* payload,
  80. size_t payload_size,
  81. std::vector<PlatformHandle> handles) override;
  82. void OnChannelError(Channel::Error error) override;
  83. void OnChannelDestroyed() override;
  84. private:
  85. struct PendingTransmission {
  86. PendingTransmission();
  87. PendingTransmission(PendingTransmission&&);
  88. PendingTransmission& operator=(PendingTransmission&&);
  89. ~PendingTransmission();
  90. std::vector<uint8_t> bytes;
  91. std::vector<PlatformHandle> handles;
  92. };
  93. ~Transport() override;
  94. bool CanTransmitHandles() const;
  95. const Destination destination_;
  96. const base::Process remote_process_;
  97. // The channel endpoint which will be used by this Transport to construct and
  98. // start its underlying Channel instance once activated. Not guarded by a lock
  99. // since it must not accessed beyond activation, where thread safety becomes a
  100. // factor.
  101. PlatformChannelEndpoint inactive_endpoint_;
  102. base::Lock lock_;
  103. scoped_refptr<Channel> channel_ GUARDED_BY(lock_);
  104. // Transmissions prior to activation must be queued, as the Channel is not
  105. // created until then. Queued messages are stored here. Once the Transport has
  106. // been activated, this is no longer used.
  107. std::vector<PendingTransmission> pending_transmissions_ GUARDED_BY(lock_);
  108. // NOTE: Channel does not retain a reference to its Delegate (this Transport,
  109. // in our case) and it may call back into us from any thread as long as it's
  110. // still alive. So we retain a self-reference on behalf of the Channel and
  111. // release it only once notified of the Channel's destruction.
  112. //
  113. // TODO(https://crbug.com/1299283): Refactor Channel so that this is
  114. // unnecessary, once the non-ipcz Mojo implementation is phased out.
  115. scoped_refptr<Transport> self_reference_for_channel_ GUARDED_BY(lock_);
  116. // These fields are not guarded by locks, since they're only set prior to
  117. // activation and remain constant throughout the remainder of this object's
  118. // lifetime.
  119. IpczHandle ipcz_transport_ = IPCZ_INVALID_HANDLE;
  120. IpczTransportActivityHandler activity_handler_ = nullptr;
  121. };
  122. } // namespace mojo::core::ipcz_driver
  123. #endif // MOJO_CORE_IPCZ_DRIVER_TRANSPORT_H_