node_channel.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2016 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_NODE_CHANNEL_H_
  5. #define MOJO_CORE_NODE_CHANNEL_H_
  6. #include <utility>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted_delete_on_sequence.h"
  12. #include "base/process/process.h"
  13. #include "base/process/process_handle.h"
  14. #include "base/synchronization/lock.h"
  15. #include "base/task/single_thread_task_runner.h"
  16. #include "build/build_config.h"
  17. #include "mojo/core/channel.h"
  18. #include "mojo/core/connection_params.h"
  19. #include "mojo/core/embedder/process_error_callback.h"
  20. #include "mojo/core/ports/name.h"
  21. #include "mojo/core/system_impl_export.h"
  22. namespace mojo {
  23. namespace core {
  24. constexpr uint64_t kNodeCapabilityNone = 0;
  25. constexpr uint64_t kNodeCapabilitySupportsUpgrade = 1;
  26. // Wraps a Channel to send and receive Node control messages.
  27. class MOJO_SYSTEM_IMPL_EXPORT NodeChannel
  28. : public base::RefCountedDeleteOnSequence<NodeChannel>,
  29. public Channel::Delegate {
  30. public:
  31. class Delegate {
  32. public:
  33. virtual ~Delegate() = default;
  34. virtual void OnAcceptInvitee(const ports::NodeName& from_node,
  35. const ports::NodeName& inviter_name,
  36. const ports::NodeName& token) = 0;
  37. virtual void OnAcceptInvitation(const ports::NodeName& from_node,
  38. const ports::NodeName& token,
  39. const ports::NodeName& invitee_name) = 0;
  40. virtual void OnAddBrokerClient(const ports::NodeName& from_node,
  41. const ports::NodeName& client_name,
  42. base::ProcessHandle process_handle) = 0;
  43. virtual void OnBrokerClientAdded(const ports::NodeName& from_node,
  44. const ports::NodeName& client_name,
  45. PlatformHandle broker_channel) = 0;
  46. virtual void OnAcceptBrokerClient(const ports::NodeName& from_node,
  47. const ports::NodeName& broker_name,
  48. PlatformHandle broker_channel,
  49. const uint64_t broker_capabilities) = 0;
  50. virtual void OnEventMessage(const ports::NodeName& from_node,
  51. Channel::MessagePtr message) = 0;
  52. virtual void OnRequestPortMerge(const ports::NodeName& from_node,
  53. const ports::PortName& connector_port_name,
  54. const std::string& token) = 0;
  55. virtual void OnRequestIntroduction(const ports::NodeName& from_node,
  56. const ports::NodeName& name) = 0;
  57. virtual void OnIntroduce(const ports::NodeName& from_node,
  58. const ports::NodeName& name,
  59. PlatformHandle channel_handle,
  60. const uint64_t remote_capabilities) = 0;
  61. virtual void OnBroadcast(const ports::NodeName& from_node,
  62. Channel::MessagePtr message) = 0;
  63. #if BUILDFLAG(IS_WIN)
  64. virtual void OnRelayEventMessage(const ports::NodeName& from_node,
  65. base::ProcessHandle from_process,
  66. const ports::NodeName& destination,
  67. Channel::MessagePtr message) = 0;
  68. virtual void OnEventMessageFromRelay(const ports::NodeName& from_node,
  69. const ports::NodeName& source_node,
  70. Channel::MessagePtr message) = 0;
  71. #endif
  72. virtual void OnAcceptPeer(const ports::NodeName& from_node,
  73. const ports::NodeName& token,
  74. const ports::NodeName& peer_name,
  75. const ports::PortName& port_name) = 0;
  76. virtual void OnChannelError(const ports::NodeName& node,
  77. NodeChannel* channel) = 0;
  78. };
  79. static scoped_refptr<NodeChannel> Create(
  80. Delegate* delegate,
  81. ConnectionParams connection_params,
  82. Channel::HandlePolicy channel_handle_policy,
  83. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
  84. const ProcessErrorCallback& process_error_callback);
  85. NodeChannel(const NodeChannel&) = delete;
  86. NodeChannel& operator=(const NodeChannel&) = delete;
  87. static Channel::MessagePtr CreateEventMessage(size_t capacity,
  88. size_t payload_size,
  89. void** payload,
  90. size_t num_handles);
  91. // Retrieves address and size of an Event message's underlying message data.
  92. // Returns `false` if the message is not a valid Event message.
  93. static bool GetEventMessageData(Channel::Message& message,
  94. void** data,
  95. size_t* num_data_bytes);
  96. // Start receiving messages.
  97. void Start();
  98. // Permanently stop the channel from sending or receiving messages.
  99. void ShutDown();
  100. // Leaks the pipe handle instead of closing it on shutdown.
  101. void LeakHandleOnShutdown();
  102. // Invokes the bad message callback for this channel. To avoid losing error
  103. // reports the caller should ensure that the channel |HasBadMessageHandler|
  104. // before calling |NotifyBadMessage|.
  105. void NotifyBadMessage(const std::string& error);
  106. // Returns whether the channel has a bad message handler.
  107. bool HasBadMessageHandler() { return !process_error_callback_.is_null(); }
  108. void SetRemoteProcessHandle(base::Process process_handle);
  109. bool HasRemoteProcessHandle();
  110. base::Process CloneRemoteProcessHandle();
  111. // Used for context in Delegate calls (via |from_node| arguments.)
  112. void SetRemoteNodeName(const ports::NodeName& name);
  113. void AcceptInvitee(const ports::NodeName& inviter_name,
  114. const ports::NodeName& token);
  115. void AcceptInvitation(const ports::NodeName& token,
  116. const ports::NodeName& invitee_name);
  117. void AcceptPeer(const ports::NodeName& sender_name,
  118. const ports::NodeName& token,
  119. const ports::PortName& port_name);
  120. void AddBrokerClient(const ports::NodeName& client_name,
  121. base::Process process_handle);
  122. void BrokerClientAdded(const ports::NodeName& client_name,
  123. PlatformHandle broker_channel);
  124. void AcceptBrokerClient(const ports::NodeName& broker_name,
  125. PlatformHandle broker_channel,
  126. const uint64_t broker_capabilities);
  127. void RequestPortMerge(const ports::PortName& connector_port_name,
  128. const std::string& token);
  129. void RequestIntroduction(const ports::NodeName& name);
  130. void Introduce(const ports::NodeName& name,
  131. PlatformHandle channel_handle,
  132. uint64_t capabilities);
  133. void SendChannelMessage(Channel::MessagePtr message);
  134. void Broadcast(Channel::MessagePtr message);
  135. void BindBrokerHost(PlatformHandle broker_host_handle);
  136. uint64_t RemoteCapabilities() const;
  137. bool HasRemoteCapability(const uint64_t capability) const;
  138. void SetRemoteCapabilities(const uint64_t capability);
  139. uint64_t LocalCapabilities() const;
  140. bool HasLocalCapability(const uint64_t capability) const;
  141. void SetLocalCapabilities(const uint64_t capability);
  142. #if BUILDFLAG(IS_WIN)
  143. // Relay the message to the specified node via this channel. This is used to
  144. // pass windows handles between two processes that do not have permission to
  145. // duplicate handles into the other's address space. The relay process is
  146. // assumed to have that permission.
  147. void RelayEventMessage(const ports::NodeName& destination,
  148. Channel::MessagePtr message);
  149. // Sends a message to its destination from a relay. This is interpreted by the
  150. // receiver similarly to EventMessage, but the original source node is
  151. // provided as additional message metadata from the (trusted) relay node.
  152. void EventMessageFromRelay(const ports::NodeName& source,
  153. Channel::MessagePtr message);
  154. #endif
  155. void OfferChannelUpgrade();
  156. private:
  157. friend class base::RefCountedDeleteOnSequence<NodeChannel>;
  158. friend class base::DeleteHelper<NodeChannel>;
  159. using PendingMessageQueue = base::queue<Channel::MessagePtr>;
  160. using PendingRelayMessageQueue =
  161. base::queue<std::pair<ports::NodeName, Channel::MessagePtr>>;
  162. NodeChannel(Delegate* delegate,
  163. ConnectionParams connection_params,
  164. Channel::HandlePolicy channel_handle_policy,
  165. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
  166. const ProcessErrorCallback& process_error_callback);
  167. ~NodeChannel() override;
  168. // Creates a BrokerHost to satisfy a |BindBrokerHost()| request from the other
  169. // end of the channel.
  170. void CreateAndBindLocalBrokerHost(PlatformHandle broker_host_handle);
  171. // Channel::Delegate:
  172. void OnChannelMessage(const void* payload,
  173. size_t payload_size,
  174. std::vector<PlatformHandle> handles) override;
  175. void OnChannelError(Channel::Error error) override;
  176. void WriteChannelMessage(Channel::MessagePtr message);
  177. // This method is responsible for setting up the default set of capabilities
  178. // for this channel.
  179. void InitializeLocalCapabilities();
  180. const raw_ptr<Delegate> delegate_;
  181. const ProcessErrorCallback process_error_callback_;
  182. base::Lock channel_lock_;
  183. scoped_refptr<Channel> channel_ GUARDED_BY(channel_lock_);
  184. // Must only be accessed from the owning task runner's thread.
  185. ports::NodeName remote_node_name_;
  186. uint64_t remote_capabilities_ = kNodeCapabilityNone;
  187. uint64_t local_capabilities_ = kNodeCapabilityNone;
  188. base::Lock remote_process_handle_lock_;
  189. base::Process remote_process_handle_;
  190. };
  191. } // namespace core
  192. } // namespace mojo
  193. #endif // MOJO_CORE_NODE_CHANNEL_H_