[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 1 | // Copyright 2014 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 | |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 5 | #include "ipc/ipc_channel_mojo.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 10 | #include <memory> |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 11 | #include <utility> |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 12 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 13 | #include "base/bind.h" |
| 14 | #include "base/bind_helpers.h" |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 15 | #include "base/command_line.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 16 | #include "base/lazy_instance.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 17 | #include "base/macros.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 18 | #include "base/memory/ptr_util.h" |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 19 | #include "base/process/process_handle.h" |
gab | f08ccc0 | 2016-05-11 18:51:11 | [diff] [blame] | 20 | #include "base/threading/thread_task_runner_handle.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 22 | #include "ipc/ipc_listener.h" |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 23 | #include "ipc/ipc_logging.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 24 | #include "ipc/ipc_message_attachment_set.h" |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 25 | #include "ipc/ipc_message_macros.h" |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 26 | #include "ipc/ipc_mojo_bootstrap.h" |
| 27 | #include "ipc/ipc_mojo_handle_attachment.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 28 | #include "mojo/public/cpp/bindings/binding.h" |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 29 | #include "mojo/public/cpp/system/platform_handle.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 30 | |
amistry | e309ea3 | 2016-06-06 03:20:49 | [diff] [blame] | 31 | #if defined(OS_POSIX) |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 32 | #include "ipc/ipc_platform_file_attachment_posix.h" |
| 33 | #endif |
| 34 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 35 | #if defined(OS_MACOSX) |
| 36 | #include "ipc/mach_port_attachment_mac.h" |
| 37 | #endif |
| 38 | |
| 39 | #if defined(OS_WIN) |
| 40 | #include "ipc/handle_attachment_win.h" |
| 41 | #endif |
| 42 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 43 | namespace IPC { |
| 44 | |
| 45 | namespace { |
| 46 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 47 | class MojoChannelFactory : public ChannelFactory { |
| 48 | public: |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 49 | MojoChannelFactory( |
| 50 | mojo::ScopedMessagePipeHandle handle, |
| 51 | Channel::Mode mode, |
| 52 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
| 53 | : handle_(std::move(handle)), |
| 54 | mode_(mode), |
| 55 | ipc_task_runner_(ipc_task_runner) {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 56 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 57 | std::string GetName() const override { return ""; } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 58 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 59 | std::unique_ptr<Channel> BuildChannel(Listener* listener) override { |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 60 | return ChannelMojo::Create( |
| 61 | std::move(handle_), mode_, listener, ipc_task_runner_); |
| 62 | } |
| 63 | |
| 64 | scoped_refptr<base::SingleThreadTaskRunner> GetIPCTaskRunner() override { |
| 65 | return ipc_task_runner_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 69 | mojo::ScopedMessagePipeHandle handle_; |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 70 | const Channel::Mode mode_; |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 71 | scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 72 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(MojoChannelFactory); |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 74 | }; |
| 75 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 76 | mojom::SerializedHandlePtr CreateSerializedHandle( |
| 77 | mojo::ScopedHandle handle, |
| 78 | mojom::SerializedHandle::Type type) { |
| 79 | mojom::SerializedHandlePtr serialized_handle = mojom::SerializedHandle::New(); |
| 80 | serialized_handle->the_handle = std::move(handle); |
| 81 | serialized_handle->type = type; |
| 82 | return serialized_handle; |
| 83 | } |
| 84 | |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 85 | MojoResult WrapPlatformHandle(base::PlatformFile handle, |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 86 | mojom::SerializedHandle::Type type, |
| 87 | mojom::SerializedHandlePtr* serialized) { |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 88 | mojo::ScopedHandle wrapped_handle = mojo::WrapPlatformFile(handle); |
| 89 | if (!wrapped_handle.is_valid()) |
| 90 | return MOJO_RESULT_UNKNOWN; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 91 | |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 92 | *serialized = CreateSerializedHandle(std::move(wrapped_handle), type); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 93 | return MOJO_RESULT_OK; |
| 94 | } |
| 95 | |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 96 | #if defined(OS_MACOSX) |
| 97 | |
| 98 | MojoResult WrapMachPort(mach_port_t mach_port, |
| 99 | mojom::SerializedHandlePtr* serialized) { |
| 100 | MojoPlatformHandle platform_handle = { |
| 101 | sizeof(MojoPlatformHandle), MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT, |
| 102 | static_cast<uint64_t>(mach_port) |
| 103 | }; |
| 104 | |
| 105 | MojoHandle wrapped_handle; |
| 106 | MojoResult result = MojoWrapPlatformHandle(&platform_handle, &wrapped_handle); |
| 107 | if (result != MOJO_RESULT_OK) |
| 108 | return result; |
| 109 | |
| 110 | *serialized = CreateSerializedHandle( |
| 111 | mojo::MakeScopedHandle(mojo::Handle(wrapped_handle)), |
| 112 | mojom::SerializedHandle::Type::MACH_PORT); |
| 113 | return MOJO_RESULT_OK; |
| 114 | } |
| 115 | |
| 116 | #endif |
| 117 | |
amistry | e309ea3 | 2016-06-06 03:20:49 | [diff] [blame] | 118 | #if defined(OS_POSIX) |
morrita | 98ac98f | 2015-02-25 02:55:04 | [diff] [blame] | 119 | |
| 120 | base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) { |
| 121 | return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) |
| 122 | : base::ScopedFD(dup(attachment->file())); |
| 123 | } |
| 124 | |
| 125 | #endif |
| 126 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 127 | MojoResult WrapAttachmentImpl(MessageAttachment* attachment, |
| 128 | mojom::SerializedHandlePtr* serialized) { |
| 129 | if (attachment->GetType() == MessageAttachment::TYPE_MOJO_HANDLE) { |
| 130 | *serialized = CreateSerializedHandle( |
| 131 | static_cast<internal::MojoHandleAttachment&>(*attachment).TakeHandle(), |
| 132 | mojom::SerializedHandle::Type::MOJO_HANDLE); |
| 133 | return MOJO_RESULT_OK; |
| 134 | } |
amistry | e309ea3 | 2016-06-06 03:20:49 | [diff] [blame] | 135 | #if defined(OS_POSIX) |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 136 | if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE) { |
| 137 | // We dup() the handles in IPC::Message to transmit. |
| 138 | // IPC::MessageAttachmentSet has intricate lifecycle semantics |
| 139 | // of FDs, so just to dup()-and-own them is the safest option. |
| 140 | base::ScopedFD file = TakeOrDupFile( |
| 141 | static_cast<IPC::internal::PlatformFileAttachment*>(attachment)); |
| 142 | if (!file.is_valid()) { |
| 143 | DPLOG(WARNING) << "Failed to dup FD to transmit."; |
| 144 | return MOJO_RESULT_UNKNOWN; |
| 145 | } |
| 146 | |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 147 | return WrapPlatformHandle(file.release(), |
amistry | 980a61b | 2016-06-09 02:51:20 | [diff] [blame] | 148 | mojom::SerializedHandle::Type::PLATFORM_FILE, |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 149 | serialized); |
| 150 | } |
| 151 | #endif |
| 152 | #if defined(OS_MACOSX) |
| 153 | DCHECK_EQ(attachment->GetType(), |
| 154 | MessageAttachment::TYPE_BROKERABLE_ATTACHMENT); |
| 155 | DCHECK_EQ(static_cast<BrokerableAttachment&>(*attachment).GetBrokerableType(), |
| 156 | BrokerableAttachment::MACH_PORT); |
| 157 | internal::MachPortAttachmentMac& mach_port_attachment = |
| 158 | static_cast<internal::MachPortAttachmentMac&>(*attachment); |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 159 | MojoResult result = WrapMachPort(mach_port_attachment.get_mach_port(), |
| 160 | serialized); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 161 | mach_port_attachment.reset_mach_port_ownership(); |
| 162 | return result; |
| 163 | #elif defined(OS_WIN) |
| 164 | DCHECK_EQ(attachment->GetType(), |
| 165 | MessageAttachment::TYPE_BROKERABLE_ATTACHMENT); |
| 166 | DCHECK_EQ(static_cast<BrokerableAttachment&>(*attachment).GetBrokerableType(), |
| 167 | BrokerableAttachment::WIN_HANDLE); |
| 168 | internal::HandleAttachmentWin& handle_attachment = |
| 169 | static_cast<internal::HandleAttachmentWin&>(*attachment); |
| 170 | MojoResult result = WrapPlatformHandle( |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 171 | handle_attachment.get_handle(), |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 172 | mojom::SerializedHandle::Type::WIN_HANDLE, serialized); |
| 173 | handle_attachment.reset_handle_ownership(); |
| 174 | return result; |
| 175 | #else |
| 176 | NOTREACHED(); |
| 177 | return MOJO_RESULT_UNKNOWN; |
| 178 | #endif // defined(OS_MACOSX) |
| 179 | } |
| 180 | |
| 181 | MojoResult WrapAttachment(MessageAttachment* attachment, |
yzshen | 24b40a3 | 2016-08-24 01:10:13 | [diff] [blame] | 182 | std::vector<mojom::SerializedHandlePtr>* handles) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 183 | mojom::SerializedHandlePtr serialized_handle; |
| 184 | MojoResult wrap_result = WrapAttachmentImpl(attachment, &serialized_handle); |
| 185 | if (wrap_result != MOJO_RESULT_OK) { |
| 186 | LOG(WARNING) << "Pipe failed to wrap handles. Closing: " << wrap_result; |
| 187 | return wrap_result; |
| 188 | } |
| 189 | handles->push_back(std::move(serialized_handle)); |
| 190 | return MOJO_RESULT_OK; |
| 191 | } |
| 192 | |
| 193 | MojoResult UnwrapAttachment(mojom::SerializedHandlePtr handle, |
| 194 | scoped_refptr<MessageAttachment>* attachment) { |
| 195 | if (handle->type == mojom::SerializedHandle::Type::MOJO_HANDLE) { |
| 196 | *attachment = |
| 197 | new IPC::internal::MojoHandleAttachment(std::move(handle->the_handle)); |
| 198 | return MOJO_RESULT_OK; |
| 199 | } |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 200 | MojoPlatformHandle platform_handle = { sizeof(MojoPlatformHandle), 0, 0 }; |
| 201 | MojoResult unwrap_result = MojoUnwrapPlatformHandle( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 202 | handle->the_handle.release().value(), &platform_handle); |
| 203 | if (unwrap_result != MOJO_RESULT_OK) |
| 204 | return unwrap_result; |
amistry | 980a61b | 2016-06-09 02:51:20 | [diff] [blame] | 205 | #if defined(OS_POSIX) |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 206 | if (handle->type == mojom::SerializedHandle::Type::PLATFORM_FILE) { |
| 207 | base::PlatformFile file = base::kInvalidPlatformFile; |
| 208 | if (platform_handle.type == MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR) |
| 209 | file = static_cast<base::PlatformFile>(platform_handle.value); |
| 210 | *attachment = new internal::PlatformFileAttachment(file); |
amistry | 980a61b | 2016-06-09 02:51:20 | [diff] [blame] | 211 | return MOJO_RESULT_OK; |
| 212 | } |
| 213 | #endif // defined(OS_POSIX) |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 214 | #if defined(OS_MACOSX) |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 215 | if (handle->type == mojom::SerializedHandle::Type::MACH_PORT) { |
| 216 | mach_port_t mach_port = MACH_PORT_NULL; |
| 217 | if (platform_handle.type == MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT) |
| 218 | mach_port = static_cast<mach_port_t>(platform_handle.value); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 219 | *attachment = new internal::MachPortAttachmentMac( |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 220 | mach_port, internal::MachPortAttachmentMac::FROM_WIRE); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 221 | return MOJO_RESULT_OK; |
| 222 | } |
| 223 | #endif // defined(OS_MACOSX) |
| 224 | #if defined(OS_WIN) |
| 225 | if (handle->type == mojom::SerializedHandle::Type::WIN_HANDLE) { |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 226 | base::PlatformFile handle = base::kInvalidPlatformFile; |
| 227 | if (platform_handle.type == MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE) |
| 228 | handle = reinterpret_cast<base::PlatformFile>(platform_handle.value); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 229 | *attachment = new internal::HandleAttachmentWin( |
amistry | cbdbf18 | 2016-06-09 04:08:12 | [diff] [blame] | 230 | handle, internal::HandleAttachmentWin::FROM_WIRE); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 231 | return MOJO_RESULT_OK; |
| 232 | } |
| 233 | #endif // defined(OS_WIN) |
| 234 | NOTREACHED(); |
| 235 | return MOJO_RESULT_UNKNOWN; |
| 236 | } |
| 237 | |
rockot | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 238 | } // namespace |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 239 | |
| 240 | //------------------------------------------------------------------------------ |
| 241 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 242 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 243 | std::unique_ptr<ChannelMojo> ChannelMojo::Create( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 244 | mojo::ScopedMessagePipeHandle handle, |
| 245 | Mode mode, |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 246 | Listener* listener, |
| 247 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
| 248 | return base::WrapUnique( |
| 249 | new ChannelMojo(std::move(handle), mode, listener, ipc_task_runner)); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 253 | std::unique_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 254 | mojo::ScopedMessagePipeHandle handle, |
| 255 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
ricea | d4fe0370 | 2016-09-05 05:06:18 | [diff] [blame] | 256 | return base::MakeUnique<MojoChannelFactory>( |
| 257 | std::move(handle), Channel::MODE_SERVER, ipc_task_runner); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 258 | } |
| 259 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 260 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 261 | std::unique_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 262 | mojo::ScopedMessagePipeHandle handle, |
| 263 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
ricea | d4fe0370 | 2016-09-05 05:06:18 | [diff] [blame] | 264 | return base::MakeUnique<MojoChannelFactory>( |
| 265 | std::move(handle), Channel::MODE_CLIENT, ipc_task_runner); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 266 | } |
| 267 | |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 268 | ChannelMojo::ChannelMojo( |
| 269 | mojo::ScopedMessagePipeHandle handle, |
| 270 | Mode mode, |
| 271 | Listener* listener, |
| 272 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 273 | : pipe_(handle.get()), listener_(listener), weak_factory_(this) { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 274 | // Create MojoBootstrap after all members are set as it touches |
| 275 | // ChannelMojo from a different thread. |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 276 | bootstrap_ = |
| 277 | MojoBootstrap::Create(std::move(handle), mode, this, ipc_task_runner); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | ChannelMojo::~ChannelMojo() { |
| 281 | Close(); |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 282 | } |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 283 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 284 | bool ChannelMojo::Connect() { |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 285 | WillConnect(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 286 | |
| 287 | DCHECK(!task_runner_); |
| 288 | task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 289 | DCHECK(!message_reader_); |
| 290 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 291 | bootstrap_->Connect(); |
| 292 | return true; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 293 | } |
| 294 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame^] | 295 | void ChannelMojo::Pause() { |
| 296 | bootstrap_->Pause(); |
| 297 | } |
| 298 | |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 299 | void ChannelMojo::Unpause(bool flush) { |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame^] | 300 | bootstrap_->Unpause(); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 301 | if (flush) |
| 302 | Flush(); |
| 303 | } |
| 304 | |
| 305 | void ChannelMojo::Flush() { |
| 306 | bootstrap_->Flush(); |
| 307 | } |
| 308 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 309 | void ChannelMojo::Close() { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 310 | // NOTE: The MessagePipeReader's destructor may re-enter this function. Use |
| 311 | // caution when changing this method. |
| 312 | std::unique_ptr<internal::MessagePipeReader> reader = |
| 313 | std::move(message_reader_); |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 314 | reader.reset(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 315 | |
| 316 | base::AutoLock lock(associated_interface_lock_); |
| 317 | associated_interfaces_.clear(); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 318 | } |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 319 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 320 | // MojoBootstrap::Delegate implementation |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 321 | void ChannelMojo::OnPipesAvailable(mojom::ChannelAssociatedPtr sender, |
| 322 | mojom::ChannelAssociatedRequest receiver) { |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame^] | 323 | sender->SetPeerPid(GetSelfPID()); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 324 | message_reader_.reset(new internal::MessagePipeReader( |
| 325 | pipe_, std::move(sender), std::move(receiver), this)); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 326 | } |
| 327 | |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 328 | void ChannelMojo::OnPipeError() { |
rockot | c18f64f | 2016-03-25 04:49:18 | [diff] [blame] | 329 | DCHECK(task_runner_); |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 330 | if (task_runner_->RunsTasksOnCurrentThread()) { |
| 331 | listener_->OnChannelError(); |
| 332 | } else { |
| 333 | task_runner_->PostTask( |
| 334 | FROM_HERE, |
| 335 | base::Bind(&ChannelMojo::OnPipeError, weak_factory_.GetWeakPtr())); |
| 336 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 337 | } |
| 338 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 339 | void ChannelMojo::OnAssociatedInterfaceRequest( |
| 340 | const std::string& name, |
| 341 | mojo::ScopedInterfaceEndpointHandle handle) { |
| 342 | GenericAssociatedInterfaceFactory factory; |
| 343 | { |
| 344 | base::AutoLock locker(associated_interface_lock_); |
| 345 | auto iter = associated_interfaces_.find(name); |
| 346 | if (iter != associated_interfaces_.end()) |
| 347 | factory = iter->second; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 348 | } |
rockot | 508da246 | 2016-07-22 03:53:59 | [diff] [blame] | 349 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 350 | if (!factory.is_null()) |
| 351 | factory.Run(std::move(handle)); |
| 352 | } |
| 353 | |
| 354 | bool ChannelMojo::Send(Message* message) { |
| 355 | std::unique_ptr<Message> scoped_message = base::WrapUnique(message); |
| 356 | if (!message_reader_) |
| 357 | return false; |
| 358 | |
amistry | 1e355dd1 | 2016-07-11 21:33:28 | [diff] [blame] | 359 | // Comment copied from ipc_channel_posix.cc: |
| 360 | // We can't close the pipe here, because calling OnChannelError may destroy |
| 361 | // this object, and that would be bad if we are called from Send(). Instead, |
| 362 | // we return false and hope the caller will close the pipe. If they do not, |
| 363 | // the pipe will still be closed next time OnFileCanReadWithoutBlocking is |
| 364 | // called. |
| 365 | // |
| 366 | // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the |
| 367 | // pipe's connection error handler will be invoked in its place. |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 368 | return message_reader_->Send(std::move(scoped_message)); |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | bool ChannelMojo::IsSendThreadSafe() const { |
rockot | f14a8ae | 2016-06-16 19:28:41 | [diff] [blame] | 372 | return false; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | base::ProcessId ChannelMojo::GetPeerPID() const { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 376 | if (!message_reader_) |
| 377 | return base::kNullProcessId; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 378 | return message_reader_->GetPeerPid(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | base::ProcessId ChannelMojo::GetSelfPID() const { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 382 | #if defined(OS_LINUX) |
| 383 | if (int global_pid = GetGlobalPid()) |
| 384 | return global_pid; |
| 385 | #endif // OS_LINUX |
| 386 | #if defined(OS_NACL) |
| 387 | return -1; |
| 388 | #else |
| 389 | return base::GetCurrentProcId(); |
| 390 | #endif // defined(OS_NACL) |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 391 | } |
| 392 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 393 | Channel::AssociatedInterfaceSupport* |
| 394 | ChannelMojo::GetAssociatedInterfaceSupport() { return this; } |
| 395 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 396 | void ChannelMojo::OnPeerPidReceived() { |
| 397 | listener_->OnChannelConnected(static_cast<int32_t>(GetPeerPID())); |
| 398 | } |
| 399 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 400 | void ChannelMojo::OnMessageReceived(const Message& message) { |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 401 | TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", |
| 402 | "class", IPC_MESSAGE_ID_CLASS(message.type()), |
| 403 | "line", IPC_MESSAGE_ID_LINE(message.type())); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 404 | if (AttachmentBroker* broker = AttachmentBroker::GetGlobal()) { |
| 405 | if (broker->OnMessageReceived(message)) |
| 406 | return; |
| 407 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 408 | listener_->OnMessageReceived(message); |
| 409 | if (message.dispatch_error()) |
| 410 | listener_->OnBadMessageReceived(message); |
| 411 | } |
| 412 | |
amistry | e309ea3 | 2016-06-06 03:20:49 | [diff] [blame] | 413 | #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 414 | int ChannelMojo::GetClientFileDescriptor() const { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 415 | return -1; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 416 | } |
| 417 | |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 418 | base::ScopedFD ChannelMojo::TakeClientFileDescriptor() { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 419 | return base::ScopedFD(GetClientFileDescriptor()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 420 | } |
amistry | e309ea3 | 2016-06-06 03:20:49 | [diff] [blame] | 421 | #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 422 | |
| 423 | // static |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 424 | MojoResult ChannelMojo::ReadFromMessageAttachmentSet( |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 425 | Message* message, |
yzshen | 24b40a3 | 2016-08-24 01:10:13 | [diff] [blame] | 426 | base::Optional<std::vector<mojom::SerializedHandlePtr>>* handles) { |
| 427 | DCHECK(!*handles); |
| 428 | |
| 429 | MojoResult result = MOJO_RESULT_OK; |
| 430 | if (!message->HasAttachments()) |
| 431 | return result; |
| 432 | |
| 433 | std::vector<mojom::SerializedHandlePtr> output_handles; |
| 434 | MessageAttachmentSet* set = message->attachment_set(); |
| 435 | |
| 436 | for (unsigned i = 0; |
| 437 | result == MOJO_RESULT_OK && i < set->num_non_brokerable_attachments(); |
| 438 | ++i) { |
| 439 | result = WrapAttachment(set->GetNonBrokerableAttachmentAt(i).get(), |
| 440 | &output_handles); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 441 | } |
yzshen | 24b40a3 | 2016-08-24 01:10:13 | [diff] [blame] | 442 | for (unsigned i = 0; |
| 443 | result == MOJO_RESULT_OK && i < set->num_brokerable_attachments(); ++i) { |
| 444 | result = WrapAttachment(set->GetBrokerableAttachmentAt(i).get(), |
| 445 | &output_handles); |
| 446 | } |
| 447 | |
| 448 | set->CommitAllDescriptors(); |
| 449 | |
| 450 | if (!output_handles.empty()) |
| 451 | *handles = std::move(output_handles); |
| 452 | |
| 453 | return result; |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 454 | } |
| 455 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 456 | // static |
| 457 | MojoResult ChannelMojo::WriteToMessageAttachmentSet( |
yzshen | 24b40a3 | 2016-08-24 01:10:13 | [diff] [blame] | 458 | base::Optional<std::vector<mojom::SerializedHandlePtr>> handle_buffer, |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 459 | Message* message) { |
yzshen | 24b40a3 | 2016-08-24 01:10:13 | [diff] [blame] | 460 | if (!handle_buffer) |
| 461 | return MOJO_RESULT_OK; |
| 462 | for (size_t i = 0; i < handle_buffer->size(); ++i) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 463 | scoped_refptr<MessageAttachment> unwrapped_attachment; |
yzshen | 24b40a3 | 2016-08-24 01:10:13 | [diff] [blame] | 464 | MojoResult unwrap_result = |
| 465 | UnwrapAttachment(std::move((*handle_buffer)[i]), &unwrapped_attachment); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 466 | if (unwrap_result != MOJO_RESULT_OK) { |
| 467 | LOG(WARNING) << "Pipe failed to unwrap handles. Closing: " |
| 468 | << unwrap_result; |
| 469 | return unwrap_result; |
| 470 | } |
| 471 | DCHECK(unwrapped_attachment); |
| 472 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 473 | bool ok = message->attachment_set()->AddAttachment( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 474 | std::move(unwrapped_attachment)); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 475 | DCHECK(ok); |
| 476 | if (!ok) { |
morrita | a3889aa | 2015-03-16 22:40:51 | [diff] [blame] | 477 | LOG(ERROR) << "Failed to add new Mojo handle."; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 478 | return MOJO_RESULT_UNKNOWN; |
| 479 | } |
| 480 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 481 | return MOJO_RESULT_OK; |
| 482 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 483 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 484 | mojo::AssociatedGroup* ChannelMojo::GetAssociatedGroup() { |
| 485 | DCHECK(bootstrap_); |
| 486 | return bootstrap_->GetAssociatedGroup(); |
| 487 | } |
| 488 | |
| 489 | void ChannelMojo::AddGenericAssociatedInterface( |
| 490 | const std::string& name, |
| 491 | const GenericAssociatedInterfaceFactory& factory) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 492 | base::AutoLock locker(associated_interface_lock_); |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 493 | auto result = associated_interfaces_.insert({ name, factory }); |
| 494 | DCHECK(result.second); |
| 495 | } |
| 496 | |
| 497 | void ChannelMojo::GetGenericRemoteAssociatedInterface( |
| 498 | const std::string& name, |
| 499 | mojo::ScopedInterfaceEndpointHandle handle) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 500 | if (message_reader_) |
| 501 | message_reader_->GetRemoteInterface(name, std::move(handle)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 502 | } |
| 503 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 504 | } // namespace IPC |