| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ | 5 #ifndef MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ |
| 6 #define MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ | 6 #define MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ |
| 7 | 7 |
| 8 #include <queue> |
| 8 #include <unordered_map> | 9 #include <unordered_map> |
| 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 12 #include "base/process/process_handle.h" | 14 #include "base/process/process_handle.h" |
| 13 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 14 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
| 15 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 16 #include "mojo/edk/embedder/platform_handle_vector.h" | 18 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 17 #include "mojo/edk/embedder/scoped_platform_handle.h" | 19 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 18 #include "mojo/edk/system/channel.h" | 20 #include "mojo/edk/system/channel.h" |
| 19 #include "mojo/edk/system/ports/name.h" | 21 #include "mojo/edk/system/ports/name.h" |
| 20 | 22 |
| 23 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 24 #include "mojo/edk/system/mach_port_relay.h" |
| 25 #endif |
| 26 |
| 21 namespace mojo { | 27 namespace mojo { |
| 22 namespace edk { | 28 namespace edk { |
| 23 | 29 |
| 24 // Wraps a Channel to send and receive Node control messages. | 30 // Wraps a Channel to send and receive Node control messages. |
| 25 class NodeChannel : public base::RefCountedThreadSafe<NodeChannel>, | 31 class NodeChannel : public base::RefCountedThreadSafe<NodeChannel>, |
| 26 public Channel::Delegate { | 32 public Channel::Delegate |
| 33 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 34 , public MachPortRelay::Observer |
| 35 #endif |
| 36 { |
| 27 public: | 37 public: |
| 28 class Delegate { | 38 class Delegate { |
| 29 public: | 39 public: |
| 30 virtual ~Delegate() {} | 40 virtual ~Delegate() {} |
| 31 virtual void OnAcceptChild(const ports::NodeName& from_node, | 41 virtual void OnAcceptChild(const ports::NodeName& from_node, |
| 32 const ports::NodeName& parent_name, | 42 const ports::NodeName& parent_name, |
| 33 const ports::NodeName& token) = 0; | 43 const ports::NodeName& token) = 0; |
| 34 virtual void OnAcceptParent(const ports::NodeName& from_node, | 44 virtual void OnAcceptParent(const ports::NodeName& from_node, |
| 35 const ports::NodeName& token, | 45 const ports::NodeName& token, |
| 36 const ports::NodeName& child_name) = 0; | 46 const ports::NodeName& child_name) = 0; |
| 37 virtual void OnAddBrokerClient(const ports::NodeName& from_node, | 47 virtual void OnAddBrokerClient(const ports::NodeName& from_node, |
| 38 const ports::NodeName& client_name, | 48 const ports::NodeName& client_name, |
| 39 base::ProcessHandle process_handle) = 0; | 49 base::ProcessHandle process_handle) = 0; |
| 40 virtual void OnBrokerClientAdded(const ports::NodeName& from_node, | 50 virtual void OnBrokerClientAdded(const ports::NodeName& from_node, |
| 41 const ports::NodeName& client_name, | 51 const ports::NodeName& client_name, |
| 42 ScopedPlatformHandle broker_channel) = 0; | 52 ScopedPlatformHandle broker_channel) = 0; |
| 43 virtual void OnAcceptBrokerClient(const ports::NodeName& from_node, | 53 virtual void OnAcceptBrokerClient(const ports::NodeName& from_node, |
| 44 const ports::NodeName& broker_name, | 54 const ports::NodeName& broker_name, |
| 45 ScopedPlatformHandle broker_channel) = 0; | 55 ScopedPlatformHandle broker_channel) = 0; |
| 46 virtual void OnPortsMessage(Channel::MessagePtr message) = 0; | 56 virtual void OnPortsMessage(Channel::MessagePtr message) = 0; |
| 47 virtual void OnRequestPortMerge(const ports::NodeName& from_node, | 57 virtual void OnRequestPortMerge(const ports::NodeName& from_node, |
| 48 const ports::PortName& connector_port_name, | 58 const ports::PortName& connector_port_name, |
| 49 const std::string& token) = 0; | 59 const std::string& token) = 0; |
| 50 virtual void OnRequestIntroduction(const ports::NodeName& from_node, | 60 virtual void OnRequestIntroduction(const ports::NodeName& from_node, |
| 51 const ports::NodeName& name) = 0; | 61 const ports::NodeName& name) = 0; |
| 52 virtual void OnIntroduce(const ports::NodeName& from_node, | 62 virtual void OnIntroduce(const ports::NodeName& from_node, |
| 53 const ports::NodeName& name, | 63 const ports::NodeName& name, |
| 54 ScopedPlatformHandle channel_handle) = 0; | 64 ScopedPlatformHandle channel_handle) = 0; |
| 55 #if defined(OS_WIN) | 65 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 56 virtual void OnRelayPortsMessage(const ports::NodeName& from_node, | 66 virtual void OnRelayPortsMessage(const ports::NodeName& from_node, |
| 57 base::ProcessHandle from_process, | 67 base::ProcessHandle from_process, |
| 58 const ports::NodeName& destination, | 68 const ports::NodeName& destination, |
| 59 Channel::MessagePtr message) = 0; | 69 Channel::MessagePtr message) = 0; |
| 60 #endif | 70 #endif |
| 61 | 71 |
| 62 virtual void OnChannelError(const ports::NodeName& node) = 0; | 72 virtual void OnChannelError(const ports::NodeName& node) = 0; |
| 73 |
| 74 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 75 virtual MachPortRelay* GetMachPortRelay() = 0; |
| 76 #endif |
| 63 }; | 77 }; |
| 64 | 78 |
| 65 static scoped_refptr<NodeChannel> Create( | 79 static scoped_refptr<NodeChannel> Create( |
| 66 Delegate* delegate, | 80 Delegate* delegate, |
| 67 ScopedPlatformHandle platform_handle, | 81 ScopedPlatformHandle platform_handle, |
| 68 scoped_refptr<base::TaskRunner> io_task_runner); | 82 scoped_refptr<base::TaskRunner> io_task_runner); |
| 69 | 83 |
| 70 static Channel::MessagePtr CreatePortsMessage(size_t payload_size, | 84 static Channel::MessagePtr CreatePortsMessage(size_t payload_size, |
| 71 void** payload, | 85 void** payload, |
| 72 size_t num_handles); | 86 size_t num_handles); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 99 ScopedPlatformHandle broker_channel); | 113 ScopedPlatformHandle broker_channel); |
| 100 void AcceptBrokerClient(const ports::NodeName& broker_name, | 114 void AcceptBrokerClient(const ports::NodeName& broker_name, |
| 101 ScopedPlatformHandle broker_channel); | 115 ScopedPlatformHandle broker_channel); |
| 102 void PortsMessage(Channel::MessagePtr message); | 116 void PortsMessage(Channel::MessagePtr message); |
| 103 void RequestPortMerge(const ports::PortName& connector_port_name, | 117 void RequestPortMerge(const ports::PortName& connector_port_name, |
| 104 const std::string& token); | 118 const std::string& token); |
| 105 void RequestIntroduction(const ports::NodeName& name); | 119 void RequestIntroduction(const ports::NodeName& name); |
| 106 void Introduce(const ports::NodeName& name, | 120 void Introduce(const ports::NodeName& name, |
| 107 ScopedPlatformHandle channel_handle); | 121 ScopedPlatformHandle channel_handle); |
| 108 | 122 |
| 109 #if defined(OS_WIN) | 123 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 110 // Relay the message to the specified node via this channel. This is used to | 124 // Relay the message to the specified node via this channel. This is used to |
| 111 // pass windows handles between two processes that do not have permission to | 125 // pass windows handles between two processes that do not have permission to |
| 112 // duplicate handles into the other's address space. The relay process is | 126 // duplicate handles into the other's address space. The relay process is |
| 113 // assumed to have that permission. | 127 // assumed to have that permission. |
| 114 void RelayPortsMessage(const ports::NodeName& destination, | 128 void RelayPortsMessage(const ports::NodeName& destination, |
| 115 Channel::MessagePtr message); | 129 Channel::MessagePtr message); |
| 116 #endif | 130 #endif |
| 117 | 131 |
| 118 private: | 132 private: |
| 119 friend class base::RefCountedThreadSafe<NodeChannel>; | 133 friend class base::RefCountedThreadSafe<NodeChannel>; |
| 120 | 134 |
| 135 using PendingMessageQueue = std::queue<Channel::MessagePtr>; |
| 136 using PendingRelayMessageQueue = |
| 137 std::queue<std::pair<ports::NodeName, Channel::MessagePtr>>; |
| 138 |
| 121 NodeChannel(Delegate* delegate, | 139 NodeChannel(Delegate* delegate, |
| 122 ScopedPlatformHandle platform_handle, | 140 ScopedPlatformHandle platform_handle, |
| 123 scoped_refptr<base::TaskRunner> io_task_runner); | 141 scoped_refptr<base::TaskRunner> io_task_runner); |
| 124 ~NodeChannel() override; | 142 ~NodeChannel() override; |
| 125 | 143 |
| 126 // Channel::Delegate: | 144 // Channel::Delegate: |
| 127 void OnChannelMessage(const void* payload, | 145 void OnChannelMessage(const void* payload, |
| 128 size_t payload_size, | 146 size_t payload_size, |
| 129 ScopedPlatformHandleVectorPtr handles) override; | 147 ScopedPlatformHandleVectorPtr handles) override; |
| 130 void OnChannelError() override; | 148 void OnChannelError() override; |
| 131 | 149 |
| 150 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 151 // MachPortRelay::Observer: |
| 152 void OnProcessReady(base::ProcessHandle process) override; |
| 153 |
| 154 void ProcessPendingMessagesWithMachPorts(); |
| 155 #endif |
| 156 |
| 132 void WriteChannelMessage(Channel::MessagePtr message); | 157 void WriteChannelMessage(Channel::MessagePtr message); |
| 133 | 158 |
| 134 Delegate* const delegate_; | 159 Delegate* const delegate_; |
| 135 const scoped_refptr<base::TaskRunner> io_task_runner_; | 160 const scoped_refptr<base::TaskRunner> io_task_runner_; |
| 136 | 161 |
| 137 base::Lock channel_lock_; | 162 base::Lock channel_lock_; |
| 138 scoped_refptr<Channel> channel_; | 163 scoped_refptr<Channel> channel_; |
| 139 | 164 |
| 140 // Must only be accessed from |io_task_runner_|'s thread. | 165 // Must only be accessed from |io_task_runner_|'s thread. |
| 141 ports::NodeName remote_node_name_; | 166 ports::NodeName remote_node_name_; |
| 142 | 167 |
| 143 base::Lock remote_process_handle_lock_; | 168 base::Lock remote_process_handle_lock_; |
| 144 base::ProcessHandle remote_process_handle_ = base::kNullProcessHandle; | 169 base::ProcessHandle remote_process_handle_ = base::kNullProcessHandle; |
| 145 | 170 |
| 171 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 172 base::Lock pending_mach_messages_lock_; |
| 173 PendingMessageQueue pending_write_messages_; |
| 174 PendingRelayMessageQueue pending_relay_messages_; |
| 175 #endif |
| 176 |
| 146 DISALLOW_COPY_AND_ASSIGN(NodeChannel); | 177 DISALLOW_COPY_AND_ASSIGN(NodeChannel); |
| 147 }; | 178 }; |
| 148 | 179 |
| 149 } // namespace edk | 180 } // namespace edk |
| 150 } // namespace mojo | 181 } // namespace mojo |
| 151 | 182 |
| 152 #endif // MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ | 183 #endif // MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ |
| OLD | NEW |