morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [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 | 6555e69 | 2016-06-23 16:52:37 +0900 | [diff] [blame] | 5 | #include "ipc/ipc_channel_mojo.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 6 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 9 | |
dcheng | 0bdf377 | 2015-11-19 16:00:20 +0900 | [diff] [blame] | 10 | #include <memory> |
dcheng | 6c1ffcf | 2015-12-28 11:24:50 +0900 | [diff] [blame] | 11 | #include <utility> |
dcheng | 0bdf377 | 2015-11-19 16:00:20 +0900 | [diff] [blame] | 12 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 13 | #include "base/bind.h" |
| 14 | #include "base/bind_helpers.h" |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 15 | #include "base/command_line.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 16 | #include "base/lazy_instance.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 17 | #include "base/macros.h" |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 18 | #include "base/memory/ptr_util.h" |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 19 | #include "base/process/process_handle.h" |
gab | 0867a90 | 2016-05-12 03:51:11 +0900 | [diff] [blame] | 20 | #include "base/threading/thread_task_runner_handle.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 21 | #include "build/build_config.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 22 | #include "ipc/ipc_listener.h" |
morrita | 24f34cd | 2014-12-18 04:01:40 +0900 | [diff] [blame] | 23 | #include "ipc/ipc_logging.h" |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 24 | #include "ipc/ipc_message_attachment_set.h" |
morrita | 24f34cd | 2014-12-18 04:01:40 +0900 | [diff] [blame] | 25 | #include "ipc/ipc_message_macros.h" |
amistry | 6555e69 | 2016-06-23 16:52:37 +0900 | [diff] [blame] | 26 | #include "ipc/ipc_mojo_bootstrap.h" |
| 27 | #include "ipc/ipc_mojo_handle_attachment.h" |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 28 | #include "ipc/native_handle_type_converters.h" |
rockot | af32acb | 2015-11-13 10:33:59 +0900 | [diff] [blame] | 29 | #include "mojo/public/cpp/bindings/binding.h" |
amistry | fe787b3 | 2016-06-09 13:08:12 +0900 | [diff] [blame] | 30 | #include "mojo/public/cpp/system/platform_handle.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 31 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 32 | namespace IPC { |
| 33 | |
| 34 | namespace { |
| 35 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 36 | class MojoChannelFactory : public ChannelFactory { |
| 37 | public: |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 38 | MojoChannelFactory( |
| 39 | mojo::ScopedMessagePipeHandle handle, |
| 40 | Channel::Mode mode, |
| 41 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
| 42 | : handle_(std::move(handle)), |
| 43 | mode_(mode), |
| 44 | ipc_task_runner_(ipc_task_runner) {} |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 45 | |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 46 | std::unique_ptr<Channel> BuildChannel(Listener* listener) override { |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 47 | return ChannelMojo::Create( |
| 48 | std::move(handle_), mode_, listener, ipc_task_runner_); |
| 49 | } |
| 50 | |
| 51 | scoped_refptr<base::SingleThreadTaskRunner> GetIPCTaskRunner() override { |
| 52 | return ipc_task_runner_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private: |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 56 | mojo::ScopedMessagePipeHandle handle_; |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 57 | const Channel::Mode mode_; |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 58 | scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 59 | |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 60 | DISALLOW_COPY_AND_ASSIGN(MojoChannelFactory); |
morrita | 88543a2 | 2014-10-28 05:10:25 +0900 | [diff] [blame] | 61 | }; |
| 62 | |
sammc | b83485d | 2016-11-11 07:34:07 +0900 | [diff] [blame] | 63 | base::ProcessId GetSelfPID() { |
| 64 | #if defined(OS_LINUX) |
| 65 | if (int global_pid = Channel::GetGlobalPid()) |
| 66 | return global_pid; |
| 67 | #endif // OS_LINUX |
| 68 | #if defined(OS_NACL) |
| 69 | return -1; |
| 70 | #else |
| 71 | return base::GetCurrentProcId(); |
| 72 | #endif // defined(OS_NACL) |
| 73 | } |
| 74 | |
rockot | ab99227 | 2015-05-12 07:53:22 +0900 | [diff] [blame] | 75 | } // namespace |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 76 | |
| 77 | //------------------------------------------------------------------------------ |
| 78 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 79 | // static |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 80 | std::unique_ptr<ChannelMojo> ChannelMojo::Create( |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 81 | mojo::ScopedMessagePipeHandle handle, |
| 82 | Mode mode, |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 83 | Listener* listener, |
| 84 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
| 85 | return base::WrapUnique( |
| 86 | new ChannelMojo(std::move(handle), mode, listener, ipc_task_runner)); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // static |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 90 | std::unique_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 91 | mojo::ScopedMessagePipeHandle handle, |
| 92 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
Jeremy Roman | bd20ea9 | 2017-08-30 02:43:43 +0900 | [diff] [blame] | 93 | return std::make_unique<MojoChannelFactory>( |
ricea | cc524fe | 2016-09-05 14:06:18 +0900 | [diff] [blame] | 94 | std::move(handle), Channel::MODE_SERVER, ipc_task_runner); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 95 | } |
| 96 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 97 | // static |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 98 | std::unique_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 99 | mojo::ScopedMessagePipeHandle handle, |
| 100 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
Jeremy Roman | bd20ea9 | 2017-08-30 02:43:43 +0900 | [diff] [blame] | 101 | return std::make_unique<MojoChannelFactory>( |
ricea | cc524fe | 2016-09-05 14:06:18 +0900 | [diff] [blame] | 102 | std::move(handle), Channel::MODE_CLIENT, ipc_task_runner); |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 103 | } |
| 104 | |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 105 | ChannelMojo::ChannelMojo( |
| 106 | mojo::ScopedMessagePipeHandle handle, |
| 107 | Mode mode, |
| 108 | Listener* listener, |
| 109 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 110 | : task_runner_(ipc_task_runner), |
| 111 | pipe_(handle.get()), |
| 112 | listener_(listener), |
| 113 | weak_factory_(this) { |
Wez | d7f09fa | 2017-10-31 01:46:06 +0900 | [diff] [blame] | 114 | weak_ptr_ = weak_factory_.GetWeakPtr(); |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 115 | bootstrap_ = MojoBootstrap::Create(std::move(handle), mode, ipc_task_runner); |
| 116 | } |
| 117 | |
| 118 | void ChannelMojo::ForwardMessageFromThreadSafePtr(mojo::Message message) { |
peary2 | 0b3a3c4 | 2017-05-19 09:37:15 +0900 | [diff] [blame] | 119 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 35d8b86 | 2017-02-10 06:25:20 +0900 | [diff] [blame] | 120 | if (!message_reader_ || !message_reader_->sender().is_bound()) |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 121 | return; |
| 122 | message_reader_->sender().internal_state()->ForwardMessage( |
| 123 | std::move(message)); |
| 124 | } |
| 125 | |
| 126 | void ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr( |
| 127 | mojo::Message message, |
| 128 | std::unique_ptr<mojo::MessageReceiver> responder) { |
peary2 | 0b3a3c4 | 2017-05-19 09:37:15 +0900 | [diff] [blame] | 129 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 35d8b86 | 2017-02-10 06:25:20 +0900 | [diff] [blame] | 130 | if (!message_reader_ || !message_reader_->sender().is_bound()) |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 131 | return; |
| 132 | message_reader_->sender().internal_state()->ForwardMessageWithResponder( |
| 133 | std::move(message), std::move(responder)); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | ChannelMojo::~ChannelMojo() { |
peary2 | 0b3a3c4 | 2017-05-19 09:37:15 +0900 | [diff] [blame] | 137 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 138 | Close(); |
morrita | 98b6e4a | 2014-09-26 12:20:48 +0900 | [diff] [blame] | 139 | } |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 140 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 141 | bool ChannelMojo::Connect() { |
peary2 | 0b3a3c4 | 2017-05-19 09:37:15 +0900 | [diff] [blame] | 142 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 143 | |
erikchen | 6b47767 | 2016-04-26 08:45:31 +0900 | [diff] [blame] | 144 | WillConnect(); |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 145 | |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 146 | mojom::ChannelAssociatedPtr sender; |
| 147 | mojom::ChannelAssociatedRequest receiver; |
| 148 | bootstrap_->Connect(&sender, &receiver); |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 149 | |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 150 | DCHECK(!message_reader_); |
| 151 | sender->SetPeerPid(GetSelfPID()); |
| 152 | message_reader_.reset(new internal::MessagePipeReader( |
| 153 | pipe_, std::move(sender), std::move(receiver), this)); |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 154 | return true; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 155 | } |
| 156 | |
rockot | e7c4790 | 2016-09-09 03:24:56 +0900 | [diff] [blame] | 157 | void ChannelMojo::Pause() { |
| 158 | bootstrap_->Pause(); |
| 159 | } |
| 160 | |
rockot | 685505b | 2016-09-07 03:35:57 +0900 | [diff] [blame] | 161 | void ChannelMojo::Unpause(bool flush) { |
rockot | e7c4790 | 2016-09-09 03:24:56 +0900 | [diff] [blame] | 162 | bootstrap_->Unpause(); |
rockot | 685505b | 2016-09-07 03:35:57 +0900 | [diff] [blame] | 163 | if (flush) |
| 164 | Flush(); |
| 165 | } |
| 166 | |
| 167 | void ChannelMojo::Flush() { |
| 168 | bootstrap_->Flush(); |
| 169 | } |
| 170 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 171 | void ChannelMojo::Close() { |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 172 | // NOTE: The MessagePipeReader's destructor may re-enter this function. Use |
| 173 | // caution when changing this method. |
| 174 | std::unique_ptr<internal::MessagePipeReader> reader = |
| 175 | std::move(message_reader_); |
msramek | 12185ef | 2016-07-22 19:06:21 +0900 | [diff] [blame] | 176 | reader.reset(); |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 177 | |
| 178 | base::AutoLock lock(associated_interface_lock_); |
| 179 | associated_interfaces_.clear(); |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 180 | } |
morrita | 5138bf5 | 2015-04-21 06:20:12 +0900 | [diff] [blame] | 181 | |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 182 | void ChannelMojo::OnPipeError() { |
rockot | 121a467 | 2016-03-25 13:49:18 +0900 | [diff] [blame] | 183 | DCHECK(task_runner_); |
peary2 | 0b3a3c4 | 2017-05-19 09:37:15 +0900 | [diff] [blame] | 184 | if (task_runner_->RunsTasksInCurrentSequence()) { |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 185 | listener_->OnChannelError(); |
| 186 | } else { |
Wez | d7f09fa | 2017-10-31 01:46:06 +0900 | [diff] [blame] | 187 | task_runner_->PostTask(FROM_HERE, |
| 188 | base::Bind(&ChannelMojo::OnPipeError, weak_ptr_)); |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 189 | } |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 190 | } |
| 191 | |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 192 | void ChannelMojo::OnAssociatedInterfaceRequest( |
| 193 | const std::string& name, |
| 194 | mojo::ScopedInterfaceEndpointHandle handle) { |
| 195 | GenericAssociatedInterfaceFactory factory; |
| 196 | { |
| 197 | base::AutoLock locker(associated_interface_lock_); |
| 198 | auto iter = associated_interfaces_.find(name); |
| 199 | if (iter != associated_interfaces_.end()) |
| 200 | factory = iter->second; |
msramek | 12185ef | 2016-07-22 19:06:21 +0900 | [diff] [blame] | 201 | } |
rockot | 262d553 | 2016-07-22 12:53:59 +0900 | [diff] [blame] | 202 | |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 203 | if (!factory.is_null()) |
| 204 | factory.Run(std::move(handle)); |
rockot | 065bc8f | 2016-09-15 09:08:59 +0900 | [diff] [blame] | 205 | else |
| 206 | listener_->OnAssociatedInterfaceRequest(name, std::move(handle)); |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bool ChannelMojo::Send(Message* message) { |
| 210 | std::unique_ptr<Message> scoped_message = base::WrapUnique(message); |
| 211 | if (!message_reader_) |
| 212 | return false; |
| 213 | |
amistry | 6dcfcf7 | 2016-07-12 06:33:28 +0900 | [diff] [blame] | 214 | // Comment copied from ipc_channel_posix.cc: |
| 215 | // We can't close the pipe here, because calling OnChannelError may destroy |
| 216 | // this object, and that would be bad if we are called from Send(). Instead, |
| 217 | // we return false and hope the caller will close the pipe. If they do not, |
| 218 | // the pipe will still be closed next time OnFileCanReadWithoutBlocking is |
| 219 | // called. |
| 220 | // |
| 221 | // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the |
| 222 | // pipe's connection error handler will be invoked in its place. |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 223 | return message_reader_->Send(std::move(scoped_message)); |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 224 | } |
| 225 | |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 226 | Channel::AssociatedInterfaceSupport* |
| 227 | ChannelMojo::GetAssociatedInterfaceSupport() { return this; } |
| 228 | |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 229 | std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> |
| 230 | ChannelMojo::CreateThreadSafeChannel() { |
Jeremy Roman | bd20ea9 | 2017-08-30 02:43:43 +0900 | [diff] [blame] | 231 | return std::make_unique<mojo::ThreadSafeForwarder<mojom::Channel>>( |
| 232 | task_runner_, |
Wez | d7f09fa | 2017-10-31 01:46:06 +0900 | [diff] [blame] | 233 | base::Bind(&ChannelMojo::ForwardMessageFromThreadSafePtr, weak_ptr_), |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 234 | base::Bind(&ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr, |
Wez | d7f09fa | 2017-10-31 01:46:06 +0900 | [diff] [blame] | 235 | weak_ptr_), |
yzshen | 8dc7bd7 | 2017-02-15 07:24:25 +0900 | [diff] [blame] | 236 | *bootstrap_->GetAssociatedGroup()); |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 237 | } |
| 238 | |
sammc | b83485d | 2016-11-11 07:34:07 +0900 | [diff] [blame] | 239 | void ChannelMojo::OnPeerPidReceived(int32_t peer_pid) { |
| 240 | listener_->OnChannelConnected(peer_pid); |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 241 | } |
| 242 | |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 243 | void ChannelMojo::OnMessageReceived(const Message& message) { |
morrita | 24f34cd | 2014-12-18 04:01:40 +0900 | [diff] [blame] | 244 | TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", |
| 245 | "class", IPC_MESSAGE_ID_CLASS(message.type()), |
| 246 | "line", IPC_MESSAGE_ID_LINE(message.type())); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 247 | listener_->OnMessageReceived(message); |
| 248 | if (message.dispatch_error()) |
| 249 | listener_->OnBadMessageReceived(message); |
| 250 | } |
| 251 | |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 252 | // static |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 253 | MojoResult ChannelMojo::ReadFromMessageAttachmentSet( |
morrita | ab20725 | 2014-09-25 05:11:45 +0900 | [diff] [blame] | 254 | Message* message, |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 255 | base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles) { |
yzshen | 8af798c | 2016-08-24 10:10:13 +0900 | [diff] [blame] | 256 | DCHECK(!*handles); |
| 257 | |
| 258 | MojoResult result = MOJO_RESULT_OK; |
| 259 | if (!message->HasAttachments()) |
| 260 | return result; |
| 261 | |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 262 | std::vector<mojo::native::SerializedHandlePtr> output_handles; |
yzshen | 8af798c | 2016-08-24 10:10:13 +0900 | [diff] [blame] | 263 | MessageAttachmentSet* set = message->attachment_set(); |
| 264 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 265 | for (unsigned i = 0; result == MOJO_RESULT_OK && i < set->size(); ++i) { |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 266 | auto attachment = set->GetAttachmentAt(i); |
| 267 | auto serialized_handle = mojo::native::SerializedHandle::New(); |
| 268 | serialized_handle->the_handle = attachment->TakeMojoHandle(); |
| 269 | serialized_handle->type = |
| 270 | mojo::ConvertTo<mojo::native::SerializedHandle::Type>( |
| 271 | attachment->GetType()); |
| 272 | output_handles.emplace_back(std::move(serialized_handle)); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 273 | } |
yzshen | 8af798c | 2016-08-24 10:10:13 +0900 | [diff] [blame] | 274 | set->CommitAllDescriptors(); |
| 275 | |
| 276 | if (!output_handles.empty()) |
| 277 | *handles = std::move(output_handles); |
| 278 | |
| 279 | return result; |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 280 | } |
| 281 | |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 282 | // static |
| 283 | MojoResult ChannelMojo::WriteToMessageAttachmentSet( |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 284 | base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles, |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 285 | Message* message) { |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 286 | if (!handles) |
yzshen | 8af798c | 2016-08-24 10:10:13 +0900 | [diff] [blame] | 287 | return MOJO_RESULT_OK; |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 288 | for (size_t i = 0; i < handles->size(); ++i) { |
| 289 | auto& handle = handles->at(i); |
| 290 | scoped_refptr<MessageAttachment> unwrapped_attachment = |
| 291 | MessageAttachment::CreateFromMojoHandle( |
| 292 | std::move(handle->the_handle), |
| 293 | mojo::ConvertTo<MessageAttachment::Type>(handle->type)); |
| 294 | if (!unwrapped_attachment) { |
| 295 | DLOG(WARNING) << "Pipe failed to unwrap handles."; |
| 296 | return MOJO_RESULT_UNKNOWN; |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 297 | } |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 298 | |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 299 | bool ok = message->attachment_set()->AddAttachment( |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 300 | std::move(unwrapped_attachment)); |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 301 | DCHECK(ok); |
| 302 | if (!ok) { |
morrita | da23c5d | 2015-03-17 07:40:51 +0900 | [diff] [blame] | 303 | LOG(ERROR) << "Failed to add new Mojo handle."; |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 304 | return MOJO_RESULT_UNKNOWN; |
| 305 | } |
| 306 | } |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 307 | return MOJO_RESULT_OK; |
| 308 | } |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 309 | |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 310 | void ChannelMojo::AddGenericAssociatedInterface( |
| 311 | const std::string& name, |
| 312 | const GenericAssociatedInterfaceFactory& factory) { |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 313 | base::AutoLock locker(associated_interface_lock_); |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 314 | auto result = associated_interfaces_.insert({ name, factory }); |
| 315 | DCHECK(result.second); |
| 316 | } |
| 317 | |
| 318 | void ChannelMojo::GetGenericRemoteAssociatedInterface( |
| 319 | const std::string& name, |
| 320 | mojo::ScopedInterfaceEndpointHandle handle) { |
yzshen | 91dd464 | 2017-03-25 17:27:17 +0900 | [diff] [blame] | 321 | if (message_reader_) { |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 322 | message_reader_->GetRemoteInterface(name, std::move(handle)); |
yzshen | 91dd464 | 2017-03-25 17:27:17 +0900 | [diff] [blame] | 323 | } else { |
| 324 | // Attach the associated interface to a disconnected pipe, so that the |
| 325 | // associated interface pointer can be used to make calls (which are |
| 326 | // dropped). |
Balazs Engedy | 4522c12 | 2017-11-04 09:50:21 +0900 | [diff] [blame] | 327 | mojo::AssociateWithDisconnectedPipe(std::move(handle)); |
yzshen | 91dd464 | 2017-03-25 17:27:17 +0900 | [diff] [blame] | 328 | } |
rockot | a289bcc | 2016-07-15 01:37:14 +0900 | [diff] [blame] | 329 | } |
| 330 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 331 | } // namespace IPC |