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