[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, |
benwells | 247f0e9a | 2016-08-01 05:36:47 | [diff] [blame] | 182 | mojo::Array<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) { |
| 256 | return base::WrapUnique(new 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) { |
| 264 | return base::WrapUnique(new 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 | e9453ea6 | 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 | |
| 295 | void ChannelMojo::Close() { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 296 | // NOTE: The MessagePipeReader's destructor may re-enter this function. Use |
| 297 | // caution when changing this method. |
| 298 | std::unique_ptr<internal::MessagePipeReader> reader = |
| 299 | std::move(message_reader_); |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 300 | reader.reset(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 301 | |
| 302 | base::AutoLock lock(associated_interface_lock_); |
| 303 | associated_interfaces_.clear(); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 304 | } |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 305 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 306 | // MojoBootstrap::Delegate implementation |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 307 | void ChannelMojo::OnPipesAvailable(mojom::ChannelAssociatedPtr sender, |
| 308 | mojom::ChannelAssociatedRequest receiver) { |
| 309 | sender->SetPeerPid(GetSelfPID()); |
| 310 | message_reader_.reset(new internal::MessagePipeReader( |
| 311 | pipe_, std::move(sender), std::move(receiver), this)); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 312 | } |
| 313 | |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 314 | void ChannelMojo::OnPipeError() { |
rockot | c18f64f | 2016-03-25 04:49:18 | [diff] [blame] | 315 | DCHECK(task_runner_); |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 316 | if (task_runner_->RunsTasksOnCurrentThread()) { |
| 317 | listener_->OnChannelError(); |
| 318 | } else { |
| 319 | task_runner_->PostTask( |
| 320 | FROM_HERE, |
| 321 | base::Bind(&ChannelMojo::OnPipeError, weak_factory_.GetWeakPtr())); |
| 322 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 323 | } |
| 324 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 325 | void ChannelMojo::OnAssociatedInterfaceRequest( |
| 326 | const std::string& name, |
| 327 | mojo::ScopedInterfaceEndpointHandle handle) { |
| 328 | GenericAssociatedInterfaceFactory factory; |
| 329 | { |
| 330 | base::AutoLock locker(associated_interface_lock_); |
| 331 | auto iter = associated_interfaces_.find(name); |
| 332 | if (iter != associated_interfaces_.end()) |
| 333 | factory = iter->second; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 334 | } |
rockot | 508da246 | 2016-07-22 03:53:59 | [diff] [blame] | 335 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 336 | if (!factory.is_null()) |
| 337 | factory.Run(std::move(handle)); |
| 338 | } |
| 339 | |
| 340 | bool ChannelMojo::Send(Message* message) { |
| 341 | std::unique_ptr<Message> scoped_message = base::WrapUnique(message); |
| 342 | if (!message_reader_) |
| 343 | return false; |
| 344 | |
amistry | 1e355dd1 | 2016-07-11 21:33:28 | [diff] [blame] | 345 | // Comment copied from ipc_channel_posix.cc: |
| 346 | // We can't close the pipe here, because calling OnChannelError may destroy |
| 347 | // this object, and that would be bad if we are called from Send(). Instead, |
| 348 | // we return false and hope the caller will close the pipe. If they do not, |
| 349 | // the pipe will still be closed next time OnFileCanReadWithoutBlocking is |
| 350 | // called. |
| 351 | // |
| 352 | // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the |
| 353 | // pipe's connection error handler will be invoked in its place. |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 354 | return message_reader_->Send(std::move(scoped_message)); |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | bool ChannelMojo::IsSendThreadSafe() const { |
rockot | f14a8ae | 2016-06-16 19:28:41 | [diff] [blame] | 358 | return false; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | base::ProcessId ChannelMojo::GetPeerPID() const { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 362 | if (!message_reader_) |
| 363 | return base::kNullProcessId; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 364 | return message_reader_->GetPeerPid(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | base::ProcessId ChannelMojo::GetSelfPID() const { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 368 | #if defined(OS_LINUX) |
| 369 | if (int global_pid = GetGlobalPid()) |
| 370 | return global_pid; |
| 371 | #endif // OS_LINUX |
| 372 | #if defined(OS_NACL) |
| 373 | return -1; |
| 374 | #else |
| 375 | return base::GetCurrentProcId(); |
| 376 | #endif // defined(OS_NACL) |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 377 | } |
| 378 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 379 | Channel::AssociatedInterfaceSupport* |
| 380 | ChannelMojo::GetAssociatedInterfaceSupport() { return this; } |
| 381 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 382 | void ChannelMojo::OnPeerPidReceived() { |
| 383 | listener_->OnChannelConnected(static_cast<int32_t>(GetPeerPID())); |
| 384 | } |
| 385 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 386 | void ChannelMojo::OnMessageReceived(const Message& message) { |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 387 | TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", |
| 388 | "class", IPC_MESSAGE_ID_CLASS(message.type()), |
| 389 | "line", IPC_MESSAGE_ID_LINE(message.type())); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 390 | if (AttachmentBroker* broker = AttachmentBroker::GetGlobal()) { |
| 391 | if (broker->OnMessageReceived(message)) |
| 392 | return; |
| 393 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 394 | listener_->OnMessageReceived(message); |
| 395 | if (message.dispatch_error()) |
| 396 | listener_->OnBadMessageReceived(message); |
| 397 | } |
| 398 | |
amistry | e309ea3 | 2016-06-06 03:20:49 | [diff] [blame] | 399 | #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 400 | int ChannelMojo::GetClientFileDescriptor() const { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 401 | return -1; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 402 | } |
| 403 | |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 404 | base::ScopedFD ChannelMojo::TakeClientFileDescriptor() { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 405 | return base::ScopedFD(GetClientFileDescriptor()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 406 | } |
amistry | e309ea3 | 2016-06-06 03:20:49 | [diff] [blame] | 407 | #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 408 | |
| 409 | // static |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 410 | MojoResult ChannelMojo::ReadFromMessageAttachmentSet( |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 411 | Message* message, |
benwells | 247f0e9a | 2016-08-01 05:36:47 | [diff] [blame] | 412 | mojo::Array<mojom::SerializedHandlePtr>* handles) { |
| 413 | if (message->HasAttachments()) { |
| 414 | MessageAttachmentSet* set = message->attachment_set(); |
| 415 | for (unsigned i = 0; i < set->num_non_brokerable_attachments(); ++i) { |
| 416 | MojoResult result = WrapAttachment( |
| 417 | set->GetNonBrokerableAttachmentAt(i).get(), handles); |
| 418 | if (result != MOJO_RESULT_OK) { |
| 419 | set->CommitAllDescriptors(); |
| 420 | return result; |
| 421 | } |
| 422 | } |
| 423 | for (unsigned i = 0; i < set->num_brokerable_attachments(); ++i) { |
| 424 | MojoResult result = |
| 425 | WrapAttachment(set->GetBrokerableAttachmentAt(i).get(), handles); |
| 426 | if (result != MOJO_RESULT_OK) { |
| 427 | set->CommitAllDescriptors(); |
| 428 | return result; |
| 429 | } |
| 430 | } |
| 431 | set->CommitAllDescriptors(); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 432 | } |
benwells | 247f0e9a | 2016-08-01 05:36:47 | [diff] [blame] | 433 | return MOJO_RESULT_OK; |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 434 | } |
| 435 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 436 | // static |
| 437 | MojoResult ChannelMojo::WriteToMessageAttachmentSet( |
benwells | 247f0e9a | 2016-08-01 05:36:47 | [diff] [blame] | 438 | mojo::Array<mojom::SerializedHandlePtr> handle_buffer, |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 439 | Message* message) { |
benwells | 247f0e9a | 2016-08-01 05:36:47 | [diff] [blame] | 440 | for (size_t i = 0; i < handle_buffer.size(); ++i) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 441 | scoped_refptr<MessageAttachment> unwrapped_attachment; |
benwells | 247f0e9a | 2016-08-01 05:36:47 | [diff] [blame] | 442 | MojoResult unwrap_result = UnwrapAttachment(std::move(handle_buffer[i]), |
| 443 | &unwrapped_attachment); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 444 | if (unwrap_result != MOJO_RESULT_OK) { |
| 445 | LOG(WARNING) << "Pipe failed to unwrap handles. Closing: " |
| 446 | << unwrap_result; |
| 447 | return unwrap_result; |
| 448 | } |
| 449 | DCHECK(unwrapped_attachment); |
| 450 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 451 | bool ok = message->attachment_set()->AddAttachment( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 452 | std::move(unwrapped_attachment)); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 453 | DCHECK(ok); |
| 454 | if (!ok) { |
morrita | a3889aa | 2015-03-16 22:40:51 | [diff] [blame] | 455 | LOG(ERROR) << "Failed to add new Mojo handle."; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 456 | return MOJO_RESULT_UNKNOWN; |
| 457 | } |
| 458 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 459 | return MOJO_RESULT_OK; |
| 460 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 461 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 462 | mojo::AssociatedGroup* ChannelMojo::GetAssociatedGroup() { |
| 463 | DCHECK(bootstrap_); |
| 464 | return bootstrap_->GetAssociatedGroup(); |
| 465 | } |
| 466 | |
| 467 | void ChannelMojo::AddGenericAssociatedInterface( |
| 468 | const std::string& name, |
| 469 | const GenericAssociatedInterfaceFactory& factory) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 470 | base::AutoLock locker(associated_interface_lock_); |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 471 | auto result = associated_interfaces_.insert({ name, factory }); |
| 472 | DCHECK(result.second); |
| 473 | } |
| 474 | |
| 475 | void ChannelMojo::GetGenericRemoteAssociatedInterface( |
| 476 | const std::string& name, |
| 477 | mojo::ScopedInterfaceEndpointHandle handle) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 478 | if (message_reader_) |
| 479 | message_reader_->GetRemoteInterface(name, std::move(handle)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 480 | } |
| 481 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 482 | } // namespace IPC |