[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 | |
| 5 | #include "ipc/mojo/ipc_channel_mojo.h" |
| 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 | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 19 | #include "base/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" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 25 | #include "ipc/mojo/ipc_mojo_bootstrap.h" |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 26 | #include "ipc/mojo/ipc_mojo_handle_attachment.h" |
rockot | c637caf9b | 2016-02-10 09:57:08 | [diff] [blame] | 27 | #include "mojo/edk/embedder/embedder.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 28 | #include "mojo/public/cpp/bindings/binding.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 29 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 30 | #if defined(OS_POSIX) && !defined(OS_NACL) |
| 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: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 48 | MojoChannelFactory(mojo::ScopedMessagePipeHandle handle, Channel::Mode mode) |
| 49 | : handle_(std::move(handle)), mode_(mode) {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 50 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 51 | std::string GetName() const override { return ""; } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 52 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 53 | std::unique_ptr<Channel> BuildChannel(Listener* listener) override { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 54 | return ChannelMojo::Create(std::move(handle_), mode_, listener); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 58 | mojo::ScopedMessagePipeHandle handle_; |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 59 | const Channel::Mode mode_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 60 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(MojoChannelFactory); |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 62 | }; |
| 63 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 64 | mojom::SerializedHandlePtr CreateSerializedHandle( |
| 65 | mojo::ScopedHandle handle, |
| 66 | mojom::SerializedHandle::Type type) { |
| 67 | mojom::SerializedHandlePtr serialized_handle = mojom::SerializedHandle::New(); |
| 68 | serialized_handle->the_handle = std::move(handle); |
| 69 | serialized_handle->type = type; |
| 70 | return serialized_handle; |
| 71 | } |
| 72 | |
| 73 | MojoResult WrapPlatformHandle(mojo::edk::ScopedPlatformHandle handle, |
| 74 | mojom::SerializedHandle::Type type, |
| 75 | mojom::SerializedHandlePtr* serialized) { |
| 76 | MojoHandle wrapped_handle; |
| 77 | MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper( |
| 78 | std::move(handle), &wrapped_handle); |
| 79 | if (wrap_result != MOJO_RESULT_OK) |
| 80 | return wrap_result; |
| 81 | |
| 82 | *serialized = CreateSerializedHandle( |
| 83 | mojo::MakeScopedHandle(mojo::Handle(wrapped_handle)), type); |
| 84 | return MOJO_RESULT_OK; |
| 85 | } |
| 86 | |
morrita | 98ac98f | 2015-02-25 02:55:04 | [diff] [blame] | 87 | #if defined(OS_POSIX) && !defined(OS_NACL) |
| 88 | |
| 89 | base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) { |
| 90 | return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) |
| 91 | : base::ScopedFD(dup(attachment->file())); |
| 92 | } |
| 93 | |
| 94 | #endif |
| 95 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 96 | MojoResult WrapAttachmentImpl(MessageAttachment* attachment, |
| 97 | mojom::SerializedHandlePtr* serialized) { |
| 98 | if (attachment->GetType() == MessageAttachment::TYPE_MOJO_HANDLE) { |
| 99 | *serialized = CreateSerializedHandle( |
| 100 | static_cast<internal::MojoHandleAttachment&>(*attachment).TakeHandle(), |
| 101 | mojom::SerializedHandle::Type::MOJO_HANDLE); |
| 102 | return MOJO_RESULT_OK; |
| 103 | } |
| 104 | #if defined(OS_POSIX) && !defined(OS_NACL) |
| 105 | if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE) { |
| 106 | // We dup() the handles in IPC::Message to transmit. |
| 107 | // IPC::MessageAttachmentSet has intricate lifecycle semantics |
| 108 | // of FDs, so just to dup()-and-own them is the safest option. |
| 109 | base::ScopedFD file = TakeOrDupFile( |
| 110 | static_cast<IPC::internal::PlatformFileAttachment*>(attachment)); |
| 111 | if (!file.is_valid()) { |
| 112 | DPLOG(WARNING) << "Failed to dup FD to transmit."; |
| 113 | return MOJO_RESULT_UNKNOWN; |
| 114 | } |
| 115 | |
| 116 | return WrapPlatformHandle(mojo::edk::ScopedPlatformHandle( |
| 117 | mojo::edk::PlatformHandle(file.release())), |
| 118 | mojom::SerializedHandle::Type::MOJO_HANDLE, |
| 119 | serialized); |
| 120 | } |
| 121 | #endif |
| 122 | #if defined(OS_MACOSX) |
| 123 | DCHECK_EQ(attachment->GetType(), |
| 124 | MessageAttachment::TYPE_BROKERABLE_ATTACHMENT); |
| 125 | DCHECK_EQ(static_cast<BrokerableAttachment&>(*attachment).GetBrokerableType(), |
| 126 | BrokerableAttachment::MACH_PORT); |
| 127 | internal::MachPortAttachmentMac& mach_port_attachment = |
| 128 | static_cast<internal::MachPortAttachmentMac&>(*attachment); |
| 129 | MojoResult result = WrapPlatformHandle( |
| 130 | mojo::edk::ScopedPlatformHandle( |
| 131 | mojo::edk::PlatformHandle(mach_port_attachment.get_mach_port())), |
| 132 | mojom::SerializedHandle::Type::MACH_PORT, serialized); |
| 133 | mach_port_attachment.reset_mach_port_ownership(); |
| 134 | return result; |
| 135 | #elif defined(OS_WIN) |
| 136 | DCHECK_EQ(attachment->GetType(), |
| 137 | MessageAttachment::TYPE_BROKERABLE_ATTACHMENT); |
| 138 | DCHECK_EQ(static_cast<BrokerableAttachment&>(*attachment).GetBrokerableType(), |
| 139 | BrokerableAttachment::WIN_HANDLE); |
| 140 | internal::HandleAttachmentWin& handle_attachment = |
| 141 | static_cast<internal::HandleAttachmentWin&>(*attachment); |
| 142 | MojoResult result = WrapPlatformHandle( |
| 143 | mojo::edk::ScopedPlatformHandle( |
| 144 | mojo::edk::PlatformHandle(handle_attachment.get_handle())), |
| 145 | mojom::SerializedHandle::Type::WIN_HANDLE, serialized); |
| 146 | handle_attachment.reset_handle_ownership(); |
| 147 | return result; |
| 148 | #else |
| 149 | NOTREACHED(); |
| 150 | return MOJO_RESULT_UNKNOWN; |
| 151 | #endif // defined(OS_MACOSX) |
| 152 | } |
| 153 | |
| 154 | MojoResult WrapAttachment(MessageAttachment* attachment, |
| 155 | mojo::Array<mojom::SerializedHandlePtr>* handles) { |
| 156 | mojom::SerializedHandlePtr serialized_handle; |
| 157 | MojoResult wrap_result = WrapAttachmentImpl(attachment, &serialized_handle); |
| 158 | if (wrap_result != MOJO_RESULT_OK) { |
| 159 | LOG(WARNING) << "Pipe failed to wrap handles. Closing: " << wrap_result; |
| 160 | return wrap_result; |
| 161 | } |
| 162 | handles->push_back(std::move(serialized_handle)); |
| 163 | return MOJO_RESULT_OK; |
| 164 | } |
| 165 | |
| 166 | MojoResult UnwrapAttachment(mojom::SerializedHandlePtr handle, |
| 167 | scoped_refptr<MessageAttachment>* attachment) { |
| 168 | if (handle->type == mojom::SerializedHandle::Type::MOJO_HANDLE) { |
| 169 | *attachment = |
| 170 | new IPC::internal::MojoHandleAttachment(std::move(handle->the_handle)); |
| 171 | return MOJO_RESULT_OK; |
| 172 | } |
| 173 | mojo::edk::ScopedPlatformHandle platform_handle; |
| 174 | MojoResult unwrap_result = mojo::edk::PassWrappedPlatformHandle( |
| 175 | handle->the_handle.release().value(), &platform_handle); |
| 176 | if (unwrap_result != MOJO_RESULT_OK) |
| 177 | return unwrap_result; |
| 178 | #if defined(OS_MACOSX) |
| 179 | if (handle->type == mojom::SerializedHandle::Type::MACH_PORT && |
| 180 | platform_handle.get().type == mojo::edk::PlatformHandle::Type::MACH) { |
| 181 | *attachment = new internal::MachPortAttachmentMac( |
| 182 | platform_handle.release().port, |
| 183 | internal::MachPortAttachmentMac::FROM_WIRE); |
| 184 | return MOJO_RESULT_OK; |
| 185 | } |
| 186 | #endif // defined(OS_MACOSX) |
| 187 | #if defined(OS_WIN) |
| 188 | if (handle->type == mojom::SerializedHandle::Type::WIN_HANDLE) { |
| 189 | *attachment = new internal::HandleAttachmentWin( |
| 190 | platform_handle.release().handle, |
| 191 | internal::HandleAttachmentWin::FROM_WIRE); |
| 192 | return MOJO_RESULT_OK; |
| 193 | } |
| 194 | #endif // defined(OS_WIN) |
| 195 | NOTREACHED(); |
| 196 | return MOJO_RESULT_UNKNOWN; |
| 197 | } |
| 198 | |
rockot | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 199 | } // namespace |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 200 | |
| 201 | //------------------------------------------------------------------------------ |
| 202 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 203 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 204 | std::unique_ptr<ChannelMojo> ChannelMojo::Create( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 205 | mojo::ScopedMessagePipeHandle handle, |
| 206 | Mode mode, |
| 207 | Listener* listener) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 208 | return base::WrapUnique(new ChannelMojo(std::move(handle), mode, listener)); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 212 | std::unique_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 213 | mojo::ScopedMessagePipeHandle handle) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 214 | return base::WrapUnique( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 215 | new MojoChannelFactory(std::move(handle), Channel::MODE_SERVER)); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 216 | } |
| 217 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 218 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 219 | std::unique_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 220 | mojo::ScopedMessagePipeHandle handle) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 221 | return base::WrapUnique( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 222 | new MojoChannelFactory(std::move(handle), Channel::MODE_CLIENT)); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 223 | } |
| 224 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 225 | ChannelMojo::ChannelMojo(mojo::ScopedMessagePipeHandle handle, |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 226 | Mode mode, |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 227 | Listener* listener) |
rockot | c18f64f | 2016-03-25 04:49:18 | [diff] [blame] | 228 | : pipe_(handle.get()), |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 229 | listener_(listener), |
| 230 | waiting_connect_(true), |
| 231 | weak_factory_(this) { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 232 | // Create MojoBootstrap after all members are set as it touches |
| 233 | // ChannelMojo from a different thread. |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 234 | bootstrap_ = MojoBootstrap::Create(std::move(handle), mode, this); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | ChannelMojo::~ChannelMojo() { |
| 238 | Close(); |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 239 | } |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 240 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 241 | bool ChannelMojo::Connect() { |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame^] | 242 | WillConnect(); |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 243 | base::AutoLock lock(lock_); |
rockot | c18f64f | 2016-03-25 04:49:18 | [diff] [blame] | 244 | DCHECK(!task_runner_); |
| 245 | task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 246 | DCHECK(!message_reader_); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 247 | bootstrap_->Connect(); |
| 248 | return true; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void ChannelMojo::Close() { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 252 | std::unique_ptr<internal::MessagePipeReader, ReaderDeleter> reader; |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 253 | { |
| 254 | base::AutoLock lock(lock_); |
| 255 | if (!message_reader_) |
| 256 | return; |
| 257 | // The reader's destructor may re-enter Close, so we swap it out first to |
| 258 | // avoid deadlock when freeing it below. |
| 259 | std::swap(message_reader_, reader); |
| 260 | |
| 261 | // We might Close() before we Connect(). |
| 262 | waiting_connect_ = false; |
| 263 | } |
| 264 | |
| 265 | reader.reset(); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 266 | } |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 267 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 268 | // MojoBootstrap::Delegate implementation |
| 269 | void ChannelMojo::OnPipesAvailable( |
| 270 | mojom::ChannelAssociatedPtrInfo send_channel, |
| 271 | mojom::ChannelAssociatedRequest receive_channel, |
| 272 | int32_t peer_pid) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 273 | InitMessageReader(std::move(send_channel), std::move(receive_channel), |
| 274 | peer_pid); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 275 | } |
| 276 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 277 | void ChannelMojo::OnBootstrapError() { |
| 278 | listener_->OnChannelError(); |
| 279 | } |
| 280 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 281 | void ChannelMojo::InitMessageReader(mojom::ChannelAssociatedPtrInfo sender, |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 282 | mojom::ChannelAssociatedRequest receiver, |
| 283 | base::ProcessId peer_pid) { |
| 284 | mojom::ChannelAssociatedPtr sender_ptr; |
| 285 | sender_ptr.Bind(std::move(sender)); |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 286 | std::unique_ptr<internal::MessagePipeReader, ChannelMojo::ReaderDeleter> |
| 287 | reader(new internal::MessagePipeReader( |
| 288 | pipe_, std::move(sender_ptr), std::move(receiver), peer_pid, this)); |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 289 | |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 290 | bool connected = true; |
| 291 | { |
| 292 | base::AutoLock lock(lock_); |
| 293 | for (size_t i = 0; i < pending_messages_.size(); ++i) { |
| 294 | if (!reader->Send(std::move(pending_messages_[i]))) { |
| 295 | LOG(ERROR) << "Failed to flush pending messages"; |
| 296 | pending_messages_.clear(); |
| 297 | connected = false; |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (connected) { |
| 303 | // We set |message_reader_| here and won't get any |pending_messages_| |
| 304 | // hereafter. Although we might have some if there is an error, we don't |
| 305 | // care. They cannot be sent anyway. |
| 306 | message_reader_ = std::move(reader); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 307 | pending_messages_.clear(); |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 308 | waiting_connect_ = false; |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 309 | } |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 310 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 311 | |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 312 | if (connected) |
| 313 | listener_->OnChannelConnected(static_cast<int32_t>(GetPeerPID())); |
| 314 | else |
| 315 | OnPipeError(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 316 | } |
| 317 | |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 318 | void ChannelMojo::OnPipeError() { |
rockot | c18f64f | 2016-03-25 04:49:18 | [diff] [blame] | 319 | DCHECK(task_runner_); |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 320 | if (task_runner_->RunsTasksOnCurrentThread()) { |
| 321 | listener_->OnChannelError(); |
| 322 | } else { |
| 323 | task_runner_->PostTask( |
| 324 | FROM_HERE, |
| 325 | base::Bind(&ChannelMojo::OnPipeError, weak_factory_.GetWeakPtr())); |
| 326 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 327 | } |
| 328 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 329 | bool ChannelMojo::Send(Message* message) { |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 330 | base::AutoLock lock(lock_); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 331 | if (!message_reader_) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 332 | pending_messages_.push_back(base::WrapUnique(message)); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 333 | // Counts as OK before the connection is established, but it's an |
| 334 | // error otherwise. |
| 335 | return waiting_connect_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 336 | } |
| 337 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 338 | if (!message_reader_->Send(base::WrapUnique(message))) { |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 339 | OnPipeError(); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | bool ChannelMojo::IsSendThreadSafe() const { |
| 347 | return true; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | base::ProcessId ChannelMojo::GetPeerPID() const { |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 351 | base::AutoLock lock(lock_); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 352 | if (!message_reader_) |
| 353 | return base::kNullProcessId; |
| 354 | |
| 355 | return message_reader_->GetPeerPid(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | base::ProcessId ChannelMojo::GetSelfPID() const { |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 359 | return bootstrap_->GetSelfPID(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 360 | } |
| 361 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 362 | void ChannelMojo::OnMessageReceived(const Message& message) { |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 363 | TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", |
| 364 | "class", IPC_MESSAGE_ID_CLASS(message.type()), |
| 365 | "line", IPC_MESSAGE_ID_LINE(message.type())); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 366 | if (AttachmentBroker* broker = AttachmentBroker::GetGlobal()) { |
| 367 | if (broker->OnMessageReceived(message)) |
| 368 | return; |
| 369 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 370 | listener_->OnMessageReceived(message); |
| 371 | if (message.dispatch_error()) |
| 372 | listener_->OnBadMessageReceived(message); |
| 373 | } |
| 374 | |
| 375 | #if defined(OS_POSIX) && !defined(OS_NACL) |
| 376 | int ChannelMojo::GetClientFileDescriptor() const { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 377 | return -1; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 378 | } |
| 379 | |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 380 | base::ScopedFD ChannelMojo::TakeClientFileDescriptor() { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 381 | return base::ScopedFD(GetClientFileDescriptor()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 382 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 383 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 384 | |
| 385 | // static |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 386 | MojoResult ChannelMojo::ReadFromMessageAttachmentSet( |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 387 | Message* message, |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 388 | mojo::Array<mojom::SerializedHandlePtr>* handles) { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 389 | if (message->HasAttachments()) { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 390 | MessageAttachmentSet* set = message->attachment_set(); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 391 | for (unsigned i = 0; i < set->num_non_brokerable_attachments(); ++i) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 392 | MojoResult result = WrapAttachment( |
| 393 | set->GetNonBrokerableAttachmentAt(i).get(), handles); |
| 394 | if (result != MOJO_RESULT_OK) { |
| 395 | set->CommitAllDescriptors(); |
| 396 | return result; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 397 | } |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 398 | } |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 399 | for (unsigned i = 0; i < set->num_brokerable_attachments(); ++i) { |
| 400 | MojoResult result = |
| 401 | WrapAttachment(set->GetBrokerableAttachmentAt(i).get(), handles); |
| 402 | if (result != MOJO_RESULT_OK) { |
| 403 | set->CommitAllDescriptors(); |
| 404 | return result; |
| 405 | } |
| 406 | } |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 407 | set->CommitAllDescriptors(); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 408 | } |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 409 | return MOJO_RESULT_OK; |
| 410 | } |
| 411 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 412 | // static |
| 413 | MojoResult ChannelMojo::WriteToMessageAttachmentSet( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 414 | mojo::Array<mojom::SerializedHandlePtr> handle_buffer, |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 415 | Message* message) { |
| 416 | for (size_t i = 0; i < handle_buffer.size(); ++i) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 417 | scoped_refptr<MessageAttachment> unwrapped_attachment; |
| 418 | MojoResult unwrap_result = UnwrapAttachment(std::move(handle_buffer[i]), |
| 419 | &unwrapped_attachment); |
| 420 | if (unwrap_result != MOJO_RESULT_OK) { |
| 421 | LOG(WARNING) << "Pipe failed to unwrap handles. Closing: " |
| 422 | << unwrap_result; |
| 423 | return unwrap_result; |
| 424 | } |
| 425 | DCHECK(unwrapped_attachment); |
| 426 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 427 | bool ok = message->attachment_set()->AddAttachment( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 428 | std::move(unwrapped_attachment)); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 429 | DCHECK(ok); |
| 430 | if (!ok) { |
morrita | a3889aa | 2015-03-16 22:40:51 | [diff] [blame] | 431 | LOG(ERROR) << "Failed to add new Mojo handle."; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 432 | return MOJO_RESULT_UNKNOWN; |
| 433 | } |
| 434 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 435 | return MOJO_RESULT_OK; |
| 436 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 437 | |
| 438 | } // namespace IPC |