OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ |
| 6 #define MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ |
| 7 |
| 8 #include <unordered_map> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/process/process_handle.h" |
| 13 #include "base/synchronization/lock.h" |
| 14 #include "base/task_runner.h" |
| 15 #include "build/build_config.h" |
| 16 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 17 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 18 #include "mojo/edk/system/channel.h" |
| 19 #include "mojo/edk/system/ports/name.h" |
| 20 |
| 21 namespace mojo { |
| 22 namespace edk { |
| 23 |
| 24 // Wraps a Channel to send and receive Node control messages. |
| 25 class NodeChannel : public base::RefCountedThreadSafe<NodeChannel>, |
| 26 public Channel::Delegate { |
| 27 public: |
| 28 class Delegate { |
| 29 public: |
| 30 virtual ~Delegate() {} |
| 31 virtual void OnAcceptChild(const ports::NodeName& from_node, |
| 32 const ports::NodeName& parent_name, |
| 33 const ports::NodeName& token) = 0; |
| 34 virtual void OnAcceptParent(const ports::NodeName& from_node, |
| 35 const ports::NodeName& token, |
| 36 const ports::NodeName& child_name) = 0; |
| 37 virtual void OnPortsMessage(Channel::MessagePtr message) = 0; |
| 38 virtual void OnRequestPortConnection( |
| 39 const ports::NodeName& from_node, |
| 40 const ports::PortName& connector_port_name, |
| 41 const std::string& token) = 0; |
| 42 virtual void OnConnectToPort( |
| 43 const ports::NodeName& from_node, |
| 44 const ports::PortName& connector_port_name, |
| 45 const ports::PortName& connectee_port_name) = 0; |
| 46 virtual void OnRequestIntroduction(const ports::NodeName& from_node, |
| 47 const ports::NodeName& name) = 0; |
| 48 virtual void OnIntroduce(const ports::NodeName& from_node, |
| 49 const ports::NodeName& name, |
| 50 ScopedPlatformHandle channel_handle) = 0; |
| 51 #if defined(OS_WIN) |
| 52 virtual void OnRelayPortsMessage(const ports::NodeName& from_node, |
| 53 base::ProcessHandle from_process, |
| 54 const ports::NodeName& destination, |
| 55 Channel::MessagePtr message) = 0; |
| 56 #endif |
| 57 |
| 58 virtual void OnChannelError(const ports::NodeName& node) = 0; |
| 59 }; |
| 60 |
| 61 static scoped_refptr<NodeChannel> Create( |
| 62 Delegate* delegate, |
| 63 ScopedPlatformHandle platform_handle, |
| 64 scoped_refptr<base::TaskRunner> io_task_runner); |
| 65 |
| 66 static Channel::MessagePtr CreatePortsMessage(size_t payload_size, |
| 67 void** payload, |
| 68 size_t num_handles); |
| 69 |
| 70 static void GetPortsMessageData(Channel::Message* message, void** data, |
| 71 size_t* num_data_bytes); |
| 72 |
| 73 // Start receiving messages. |
| 74 void Start(); |
| 75 |
| 76 // Permanently stop the channel from sending or receiving messages. |
| 77 void ShutDown(); |
| 78 |
| 79 void SetRemoteProcessHandle(base::ProcessHandle process_handle); |
| 80 |
| 81 // Used for context in Delegate calls (via |from_node| arguments.) |
| 82 void SetRemoteNodeName(const ports::NodeName& name); |
| 83 |
| 84 void AcceptChild(const ports::NodeName& parent_name, |
| 85 const ports::NodeName& token); |
| 86 void AcceptParent(const ports::NodeName& token, |
| 87 const ports::NodeName& child_name); |
| 88 void PortsMessage(Channel::MessagePtr message); |
| 89 void RequestPortConnection(const ports::PortName& connector_port_name, |
| 90 const std::string& token); |
| 91 void ConnectToPort(const ports::PortName& connector_port_name, |
| 92 const ports::PortName& connectee_port_name); |
| 93 void RequestIntroduction(const ports::NodeName& name); |
| 94 void Introduce(const ports::NodeName& name, ScopedPlatformHandle handle); |
| 95 |
| 96 #if defined(OS_WIN) |
| 97 // Relay the message to the specified node via this channel. This is used to |
| 98 // pass windows handles between two processes that do not have permission to |
| 99 // duplicate handles into the other's address space. The relay process is |
| 100 // assumed to have that permission. |
| 101 void RelayPortsMessage(const ports::NodeName& destination, |
| 102 Channel::MessagePtr message); |
| 103 #endif |
| 104 |
| 105 private: |
| 106 friend class base::RefCountedThreadSafe<NodeChannel>; |
| 107 |
| 108 NodeChannel(Delegate* delegate, |
| 109 ScopedPlatformHandle platform_handle, |
| 110 scoped_refptr<base::TaskRunner> io_task_runner); |
| 111 ~NodeChannel() override; |
| 112 |
| 113 // Channel::Delegate: |
| 114 void OnChannelMessage(const void* payload, |
| 115 size_t payload_size, |
| 116 ScopedPlatformHandleVectorPtr handles) override; |
| 117 void OnChannelError() override; |
| 118 |
| 119 void WriteChannelMessage(Channel::MessagePtr message); |
| 120 |
| 121 Delegate* const delegate_; |
| 122 const scoped_refptr<base::TaskRunner> io_task_runner_; |
| 123 |
| 124 base::Lock channel_lock_; |
| 125 scoped_refptr<Channel> channel_; |
| 126 |
| 127 // Must only be accessed from |io_task_runner_|'s thread. |
| 128 ports::NodeName remote_node_name_; |
| 129 |
| 130 #if defined(OS_WIN) |
| 131 base::Lock remote_process_handle_lock_; |
| 132 base::ProcessHandle remote_process_handle_ = base::kNullProcessHandle; |
| 133 #endif |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(NodeChannel); |
| 136 }; |
| 137 |
| 138 } // namespace edk |
| 139 } // namespace mojo |
| 140 |
| 141 #endif // MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ |
OLD | NEW |