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