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