| 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 <queue> |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/process/process_handle.h" | 15 #include "base/process/process_handle.h" |
| 15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 16 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "mojo/edk/embedder/embedder.h" |
| 18 #include "mojo/edk/embedder/platform_handle_vector.h" | 20 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 19 #include "mojo/edk/embedder/scoped_platform_handle.h" | 21 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 20 #include "mojo/edk/system/channel.h" | 22 #include "mojo/edk/system/channel.h" |
| 21 #include "mojo/edk/system/ports/name.h" | 23 #include "mojo/edk/system/ports/name.h" |
| 22 | 24 |
| 23 #if defined(OS_MACOSX) && !defined(OS_IOS) | 25 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 24 #include "mojo/edk/system/mach_port_relay.h" | 26 #include "mojo/edk/system/mach_port_relay.h" |
| 25 #endif | 27 #endif |
| 26 | 28 |
| 27 namespace mojo { | 29 namespace mojo { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 virtual void OnRequestIntroduction(const ports::NodeName& from_node, | 63 virtual void OnRequestIntroduction(const ports::NodeName& from_node, |
| 62 const ports::NodeName& name) = 0; | 64 const ports::NodeName& name) = 0; |
| 63 virtual void OnIntroduce(const ports::NodeName& from_node, | 65 virtual void OnIntroduce(const ports::NodeName& from_node, |
| 64 const ports::NodeName& name, | 66 const ports::NodeName& name, |
| 65 ScopedPlatformHandle channel_handle) = 0; | 67 ScopedPlatformHandle channel_handle) = 0; |
| 66 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) | 68 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 67 virtual void OnRelayPortsMessage(const ports::NodeName& from_node, | 69 virtual void OnRelayPortsMessage(const ports::NodeName& from_node, |
| 68 base::ProcessHandle from_process, | 70 base::ProcessHandle from_process, |
| 69 const ports::NodeName& destination, | 71 const ports::NodeName& destination, |
| 70 Channel::MessagePtr message) = 0; | 72 Channel::MessagePtr message) = 0; |
| 73 virtual void OnPortsMessageFromRelay(const ports::NodeName& from_node, |
| 74 const ports::NodeName& source_node, |
| 75 Channel::MessagePtr message) = 0; |
| 71 #endif | 76 #endif |
| 72 | 77 |
| 73 virtual void OnChannelError(const ports::NodeName& node) = 0; | 78 virtual void OnChannelError(const ports::NodeName& node) = 0; |
| 74 | 79 |
| 75 #if defined(OS_MACOSX) && !defined(OS_IOS) | 80 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 76 virtual MachPortRelay* GetMachPortRelay() = 0; | 81 virtual MachPortRelay* GetMachPortRelay() = 0; |
| 77 #endif | 82 #endif |
| 78 }; | 83 }; |
| 79 | 84 |
| 80 static scoped_refptr<NodeChannel> Create( | 85 static scoped_refptr<NodeChannel> Create( |
| 81 Delegate* delegate, | 86 Delegate* delegate, |
| 82 ScopedPlatformHandle platform_handle, | 87 ScopedPlatformHandle platform_handle, |
| 83 scoped_refptr<base::TaskRunner> io_task_runner); | 88 scoped_refptr<base::TaskRunner> io_task_runner, |
| 89 const ProcessErrorCallback& process_error_callback); |
| 84 | 90 |
| 85 static Channel::MessagePtr CreatePortsMessage(size_t payload_size, | 91 static Channel::MessagePtr CreatePortsMessage(size_t payload_size, |
| 86 void** payload, | 92 void** payload, |
| 87 size_t num_handles); | 93 size_t num_handles); |
| 88 | 94 |
| 89 static void GetPortsMessageData(Channel::Message* message, void** data, | 95 static void GetPortsMessageData(Channel::Message* message, void** data, |
| 90 size_t* num_data_bytes); | 96 size_t* num_data_bytes); |
| 91 | 97 |
| 92 // Start receiving messages. | 98 // Start receiving messages. |
| 93 void Start(); | 99 void Start(); |
| 94 | 100 |
| 95 // Permanently stop the channel from sending or receiving messages. | 101 // Permanently stop the channel from sending or receiving messages. |
| 96 void ShutDown(); | 102 void ShutDown(); |
| 97 | 103 |
| 104 // Invokes the bad message callback for this channel, if any. |
| 105 void NotifyBadMessage(const std::string& error); |
| 106 |
| 98 void SetRemoteProcessHandle(base::ProcessHandle process_handle); | 107 void SetRemoteProcessHandle(base::ProcessHandle process_handle); |
| 99 bool HasRemoteProcessHandle(); | 108 bool HasRemoteProcessHandle(); |
| 100 // Note: The returned |ProcessHandle| is owned by the caller and should be | 109 // Note: The returned |ProcessHandle| is owned by the caller and should be |
| 101 // freed if necessary. | 110 // freed if necessary. |
| 102 base::ProcessHandle CopyRemoteProcessHandle(); | 111 base::ProcessHandle CopyRemoteProcessHandle(); |
| 103 | 112 |
| 104 // Used for context in Delegate calls (via |from_node| arguments.) | 113 // Used for context in Delegate calls (via |from_node| arguments.) |
| 105 void SetRemoteNodeName(const ports::NodeName& name); | 114 void SetRemoteNodeName(const ports::NodeName& name); |
| 106 | 115 |
| 107 void AcceptChild(const ports::NodeName& parent_name, | 116 void AcceptChild(const ports::NodeName& parent_name, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 121 void Introduce(const ports::NodeName& name, | 130 void Introduce(const ports::NodeName& name, |
| 122 ScopedPlatformHandle channel_handle); | 131 ScopedPlatformHandle channel_handle); |
| 123 | 132 |
| 124 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) | 133 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 125 // Relay the message to the specified node via this channel. This is used to | 134 // Relay the message to the specified node via this channel. This is used to |
| 126 // pass windows handles between two processes that do not have permission to | 135 // pass windows handles between two processes that do not have permission to |
| 127 // duplicate handles into the other's address space. The relay process is | 136 // duplicate handles into the other's address space. The relay process is |
| 128 // assumed to have that permission. | 137 // assumed to have that permission. |
| 129 void RelayPortsMessage(const ports::NodeName& destination, | 138 void RelayPortsMessage(const ports::NodeName& destination, |
| 130 Channel::MessagePtr message); | 139 Channel::MessagePtr message); |
| 140 |
| 141 // Sends a message to its destination from a relay. This is interpreted by the |
| 142 // receiver similarly to PortsMessage, but the original source node is |
| 143 // provided as additional message metadata from the (trusted) relay node. |
| 144 void PortsMessageFromRelay(const ports::NodeName& source, |
| 145 Channel::MessagePtr message); |
| 131 #endif | 146 #endif |
| 132 | 147 |
| 133 private: | 148 private: |
| 134 friend class base::RefCountedThreadSafe<NodeChannel>; | 149 friend class base::RefCountedThreadSafe<NodeChannel>; |
| 135 | 150 |
| 136 using PendingMessageQueue = std::queue<Channel::MessagePtr>; | 151 using PendingMessageQueue = std::queue<Channel::MessagePtr>; |
| 137 using PendingRelayMessageQueue = | 152 using PendingRelayMessageQueue = |
| 138 std::queue<std::pair<ports::NodeName, Channel::MessagePtr>>; | 153 std::queue<std::pair<ports::NodeName, Channel::MessagePtr>>; |
| 139 | 154 |
| 140 NodeChannel(Delegate* delegate, | 155 NodeChannel(Delegate* delegate, |
| 141 ScopedPlatformHandle platform_handle, | 156 ScopedPlatformHandle platform_handle, |
| 142 scoped_refptr<base::TaskRunner> io_task_runner); | 157 scoped_refptr<base::TaskRunner> io_task_runner, |
| 158 const ProcessErrorCallback& process_error_callback); |
| 143 ~NodeChannel() override; | 159 ~NodeChannel() override; |
| 144 | 160 |
| 145 // Channel::Delegate: | 161 // Channel::Delegate: |
| 146 void OnChannelMessage(const void* payload, | 162 void OnChannelMessage(const void* payload, |
| 147 size_t payload_size, | 163 size_t payload_size, |
| 148 ScopedPlatformHandleVectorPtr handles) override; | 164 ScopedPlatformHandleVectorPtr handles) override; |
| 149 void OnChannelError() override; | 165 void OnChannelError() override; |
| 150 | 166 |
| 151 #if defined(OS_MACOSX) && !defined(OS_IOS) | 167 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 152 // MachPortRelay::Observer: | 168 // MachPortRelay::Observer: |
| 153 void OnProcessReady(base::ProcessHandle process) override; | 169 void OnProcessReady(base::ProcessHandle process) override; |
| 154 | 170 |
| 155 void ProcessPendingMessagesWithMachPorts(); | 171 void ProcessPendingMessagesWithMachPorts(); |
| 156 #endif | 172 #endif |
| 157 | 173 |
| 158 void WriteChannelMessage(Channel::MessagePtr message); | 174 void WriteChannelMessage(Channel::MessagePtr message); |
| 159 | 175 |
| 160 Delegate* const delegate_; | 176 Delegate* const delegate_; |
| 161 const scoped_refptr<base::TaskRunner> io_task_runner_; | 177 const scoped_refptr<base::TaskRunner> io_task_runner_; |
| 178 const ProcessErrorCallback process_error_callback_; |
| 162 | 179 |
| 163 base::Lock channel_lock_; | 180 base::Lock channel_lock_; |
| 164 scoped_refptr<Channel> channel_; | 181 scoped_refptr<Channel> channel_; |
| 165 | 182 |
| 166 // Must only be accessed from |io_task_runner_|'s thread. | 183 // Must only be accessed from |io_task_runner_|'s thread. |
| 167 ports::NodeName remote_node_name_; | 184 ports::NodeName remote_node_name_; |
| 168 | 185 |
| 169 base::Lock remote_process_handle_lock_; | 186 base::Lock remote_process_handle_lock_; |
| 170 base::ProcessHandle remote_process_handle_ = base::kNullProcessHandle; | 187 base::ProcessHandle remote_process_handle_ = base::kNullProcessHandle; |
| 171 | 188 |
| 172 #if defined(OS_MACOSX) && !defined(OS_IOS) | 189 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 173 base::Lock pending_mach_messages_lock_; | 190 base::Lock pending_mach_messages_lock_; |
| 174 PendingMessageQueue pending_write_messages_; | 191 PendingMessageQueue pending_write_messages_; |
| 175 PendingRelayMessageQueue pending_relay_messages_; | 192 PendingRelayMessageQueue pending_relay_messages_; |
| 176 #endif | 193 #endif |
| 177 | 194 |
| 178 DISALLOW_COPY_AND_ASSIGN(NodeChannel); | 195 DISALLOW_COPY_AND_ASSIGN(NodeChannel); |
| 179 }; | 196 }; |
| 180 | 197 |
| 181 } // namespace edk | 198 } // namespace edk |
| 182 } // namespace mojo | 199 } // namespace mojo |
| 183 | 200 |
| 184 #endif // MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ | 201 #endif // MOJO_EDK_SYSTEM_NODE_CHANNEL_H_ |
| OLD | NEW |