blob: cb10b0915b95f9db6bb20d49defcb5f8e7fdf511 [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// 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
amistryd4aa70d2016-06-23 07:52:375#include "ipc/ipc_channel_mojo.h"
[email protected]64860882014-08-04 23:44:176
avi246998d82015-12-22 02:39:047#include <stddef.h>
8#include <stdint.h>
danakj03de39b22016-04-23 04:21:099
dcheng0917ec42015-11-19 07:00:2010#include <memory>
dchenge48600452015-12-28 02:24:5011#include <utility>
dcheng0917ec42015-11-19 07:00:2012
[email protected]64860882014-08-04 23:44:1713#include "base/bind.h"
14#include "base/bind_helpers.h"
jam76bcf0c2015-10-02 21:01:2815#include "base/command_line.h"
[email protected]64860882014-08-04 23:44:1716#include "base/lazy_instance.h"
avi246998d82015-12-22 02:39:0417#include "base/macros.h"
danakj03de39b22016-04-23 04:21:0918#include "base/memory/ptr_util.h"
rockot0e4de5f2016-07-22 21:18:0719#include "base/process/process_handle.h"
gabf08ccc02016-05-11 18:51:1120#include "base/threading/thread_task_runner_handle.h"
avi246998d82015-12-22 02:39:0421#include "build/build_config.h"
[email protected]64860882014-08-04 23:44:1722#include "ipc/ipc_listener.h"
morrita7126b7a2014-12-17 19:01:4023#include "ipc/ipc_logging.h"
morrita4b5c28e22015-01-14 21:17:0624#include "ipc/ipc_message_attachment_set.h"
morrita7126b7a2014-12-17 19:01:4025#include "ipc/ipc_message_macros.h"
amistryd4aa70d2016-06-23 07:52:3726#include "ipc/ipc_mojo_bootstrap.h"
27#include "ipc/ipc_mojo_handle_attachment.h"
Eve Martin-Jones475e7e62018-02-13 22:57:2528#include "ipc/native_handle_type_converters.h"
rockot85dce0862015-11-13 01:33:5929#include "mojo/public/cpp/bindings/binding.h"
amistrycbdbf182016-06-09 04:08:1230#include "mojo/public/cpp/system/platform_handle.h"
[email protected]64860882014-08-04 23:44:1731
[email protected]64860882014-08-04 23:44:1732namespace IPC {
33
34namespace {
35
[email protected]64860882014-08-04 23:44:1736class MojoChannelFactory : public ChannelFactory {
37 public:
rockota34707ca2016-07-20 04:28:3238 MojoChannelFactory(
39 mojo::ScopedMessagePipeHandle handle,
40 Channel::Mode mode,
Hajime Hoshia98f1102017-11-20 06:34:3541 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
42 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner)
rockota34707ca2016-07-20 04:28:3243 : handle_(std::move(handle)),
44 mode_(mode),
Hajime Hoshia98f1102017-11-20 06:34:3545 ipc_task_runner_(ipc_task_runner),
46 proxy_task_runner_(proxy_task_runner) {}
[email protected]64860882014-08-04 23:44:1747
danakj03de39b22016-04-23 04:21:0948 std::unique_ptr<Channel> BuildChannel(Listener* listener) override {
Hajime Hoshia98f1102017-11-20 06:34:3549 return ChannelMojo::Create(std::move(handle_), mode_, listener,
50 ipc_task_runner_, proxy_task_runner_);
rockota34707ca2016-07-20 04:28:3251 }
52
53 scoped_refptr<base::SingleThreadTaskRunner> GetIPCTaskRunner() override {
54 return ipc_task_runner_;
[email protected]64860882014-08-04 23:44:1755 }
56
57 private:
sammc57ed9f982016-03-10 06:28:3558 mojo::ScopedMessagePipeHandle handle_;
sammce4d0abd2016-03-07 22:38:0459 const Channel::Mode mode_;
rockota34707ca2016-07-20 04:28:3260 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
Hajime Hoshia98f1102017-11-20 06:34:3561 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_;
[email protected]64860882014-08-04 23:44:1762
sammce4d0abd2016-03-07 22:38:0463 DISALLOW_COPY_AND_ASSIGN(MojoChannelFactory);
morritaf8f92dcd2014-10-27 20:10:2564};
65
sammcf810f07f2016-11-10 22:34:0766base::ProcessId GetSelfPID() {
67#if defined(OS_LINUX)
68 if (int global_pid = Channel::GetGlobalPid())
69 return global_pid;
70#endif // OS_LINUX
71#if defined(OS_NACL)
72 return -1;
73#else
74 return base::GetCurrentProcId();
75#endif // defined(OS_NACL)
76}
77
rockotdbb3bb6b2015-05-11 22:53:2278} // namespace
[email protected]64860882014-08-04 23:44:1779
80//------------------------------------------------------------------------------
81
[email protected]64860882014-08-04 23:44:1782// static
danakj03de39b22016-04-23 04:21:0983std::unique_ptr<ChannelMojo> ChannelMojo::Create(
sammc57ed9f982016-03-10 06:28:3584 mojo::ScopedMessagePipeHandle handle,
85 Mode mode,
rockota34707ca2016-07-20 04:28:3286 Listener* listener,
Hajime Hoshia98f1102017-11-20 06:34:3587 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
88 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) {
89 return base::WrapUnique(new ChannelMojo(std::move(handle), mode, listener,
90 ipc_task_runner, proxy_task_runner));
[email protected]64860882014-08-04 23:44:1791}
92
93// static
danakj03de39b22016-04-23 04:21:0994std::unique_ptr<ChannelFactory> ChannelMojo::CreateServerFactory(
rockota34707ca2016-07-20 04:28:3295 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshia98f1102017-11-20 06:34:3596 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
97 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) {
Jeremy Roman160eb922017-08-29 17:43:4398 return std::make_unique<MojoChannelFactory>(
Hajime Hoshia98f1102017-11-20 06:34:3599 std::move(handle), Channel::MODE_SERVER, ipc_task_runner,
100 proxy_task_runner);
[email protected]64860882014-08-04 23:44:17101}
102
morrita54f6f80c2014-09-23 21:16:00103// static
danakj03de39b22016-04-23 04:21:09104std::unique_ptr<ChannelFactory> ChannelMojo::CreateClientFactory(
rockota34707ca2016-07-20 04:28:32105 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshia98f1102017-11-20 06:34:35106 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
107 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) {
Jeremy Roman160eb922017-08-29 17:43:43108 return std::make_unique<MojoChannelFactory>(
Hajime Hoshia98f1102017-11-20 06:34:35109 std::move(handle), Channel::MODE_CLIENT, ipc_task_runner,
110 proxy_task_runner);
morrita54f6f80c2014-09-23 21:16:00111}
112
rockota34707ca2016-07-20 04:28:32113ChannelMojo::ChannelMojo(
114 mojo::ScopedMessagePipeHandle handle,
115 Mode mode,
116 Listener* listener,
Hajime Hoshia98f1102017-11-20 06:34:35117 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
118 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner)
rockota628d0b2017-02-09 08:40:15119 : task_runner_(ipc_task_runner),
120 pipe_(handle.get()),
121 listener_(listener),
122 weak_factory_(this) {
Wez888ef822017-10-30 16:46:06123 weak_ptr_ = weak_factory_.GetWeakPtr();
Hajime Hoshia98f1102017-11-20 06:34:35124 bootstrap_ = MojoBootstrap::Create(std::move(handle), mode, ipc_task_runner,
125 proxy_task_runner);
rockota628d0b2017-02-09 08:40:15126}
127
128void ChannelMojo::ForwardMessageFromThreadSafePtr(mojo::Message message) {
peary231ef24642017-05-19 00:37:15129 DCHECK(task_runner_->RunsTasksInCurrentSequence());
rockot67b4b0c2017-02-09 21:25:20130 if (!message_reader_ || !message_reader_->sender().is_bound())
rockota628d0b2017-02-09 08:40:15131 return;
132 message_reader_->sender().internal_state()->ForwardMessage(
133 std::move(message));
134}
135
136void ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr(
137 mojo::Message message,
138 std::unique_ptr<mojo::MessageReceiver> responder) {
peary231ef24642017-05-19 00:37:15139 DCHECK(task_runner_->RunsTasksInCurrentSequence());
rockot67b4b0c2017-02-09 21:25:20140 if (!message_reader_ || !message_reader_->sender().is_bound())
rockota628d0b2017-02-09 08:40:15141 return;
142 message_reader_->sender().internal_state()->ForwardMessageWithResponder(
143 std::move(message), std::move(responder));
[email protected]64860882014-08-04 23:44:17144}
145
146ChannelMojo::~ChannelMojo() {
peary231ef24642017-05-19 00:37:15147 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]64860882014-08-04 23:44:17148 Close();
morritae9453ea2014-09-26 03:20:48149}
morrita54f6f80c2014-09-23 21:16:00150
[email protected]64860882014-08-04 23:44:17151bool ChannelMojo::Connect() {
peary231ef24642017-05-19 00:37:15152 DCHECK(task_runner_->RunsTasksInCurrentSequence());
rockota628d0b2017-02-09 08:40:15153
erikchen90971902016-04-25 23:45:31154 WillConnect();
rockot0e4de5f2016-07-22 21:18:07155
rockota628d0b2017-02-09 08:40:15156 mojom::ChannelAssociatedPtr sender;
157 mojom::ChannelAssociatedRequest receiver;
158 bootstrap_->Connect(&sender, &receiver);
rockot0e4de5f2016-07-22 21:18:07159
rockota628d0b2017-02-09 08:40:15160 DCHECK(!message_reader_);
161 sender->SetPeerPid(GetSelfPID());
162 message_reader_.reset(new internal::MessagePipeReader(
163 pipe_, std::move(sender), std::move(receiver), this));
sammce4d0abd2016-03-07 22:38:04164 return true;
[email protected]64860882014-08-04 23:44:17165}
166
rockot10188752016-09-08 18:24:56167void ChannelMojo::Pause() {
168 bootstrap_->Pause();
169}
170
rockot401fb2c2016-09-06 18:35:57171void ChannelMojo::Unpause(bool flush) {
rockot10188752016-09-08 18:24:56172 bootstrap_->Unpause();
rockot401fb2c2016-09-06 18:35:57173 if (flush)
174 Flush();
175}
176
177void ChannelMojo::Flush() {
178 bootstrap_->Flush();
179}
180
[email protected]64860882014-08-04 23:44:17181void ChannelMojo::Close() {
rockot0e4de5f2016-07-22 21:18:07182 // NOTE: The MessagePipeReader's destructor may re-enter this function. Use
183 // caution when changing this method.
184 std::unique_ptr<internal::MessagePipeReader> reader =
185 std::move(message_reader_);
msramek5507fee2016-07-22 10:06:21186 reader.reset();
rockot0e4de5f2016-07-22 21:18:07187
188 base::AutoLock lock(associated_interface_lock_);
189 associated_interfaces_.clear();
sammce4d0abd2016-03-07 22:38:04190}
morritab4472142015-04-20 21:20:12191
rockot506f92fa22016-03-23 01:32:18192void ChannelMojo::OnPipeError() {
rockotc18f64f2016-03-25 04:49:18193 DCHECK(task_runner_);
peary231ef24642017-05-19 00:37:15194 if (task_runner_->RunsTasksInCurrentSequence()) {
rockot506f92fa22016-03-23 01:32:18195 listener_->OnChannelError();
196 } else {
Wez888ef822017-10-30 16:46:06197 task_runner_->PostTask(FROM_HERE,
198 base::Bind(&ChannelMojo::OnPipeError, weak_ptr_));
rockot506f92fa22016-03-23 01:32:18199 }
[email protected]64860882014-08-04 23:44:17200}
201
rockot0e4de5f2016-07-22 21:18:07202void ChannelMojo::OnAssociatedInterfaceRequest(
203 const std::string& name,
204 mojo::ScopedInterfaceEndpointHandle handle) {
205 GenericAssociatedInterfaceFactory factory;
206 {
207 base::AutoLock locker(associated_interface_lock_);
208 auto iter = associated_interfaces_.find(name);
209 if (iter != associated_interfaces_.end())
210 factory = iter->second;
msramek5507fee2016-07-22 10:06:21211 }
rockot508da2462016-07-22 03:53:59212
rockot0e4de5f2016-07-22 21:18:07213 if (!factory.is_null())
214 factory.Run(std::move(handle));
rockotf62002a2016-09-15 00:08:59215 else
216 listener_->OnAssociatedInterfaceRequest(name, std::move(handle));
rockot0e4de5f2016-07-22 21:18:07217}
218
219bool ChannelMojo::Send(Message* message) {
Wezc7fcdea2018-03-09 06:20:00220 DVLOG(2) << "sending message @" << message << " on channel @" << this
221 << " with type " << message->type();
222#if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
223 Logging::GetInstance()->OnSendMessage(message);
224#endif
225
rockot0e4de5f2016-07-22 21:18:07226 std::unique_ptr<Message> scoped_message = base::WrapUnique(message);
227 if (!message_reader_)
228 return false;
229
amistry1e355dd12016-07-11 21:33:28230 // Comment copied from ipc_channel_posix.cc:
231 // We can't close the pipe here, because calling OnChannelError may destroy
232 // this object, and that would be bad if we are called from Send(). Instead,
233 // we return false and hope the caller will close the pipe. If they do not,
234 // the pipe will still be closed next time OnFileCanReadWithoutBlocking is
235 // called.
236 //
237 // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the
238 // pipe's connection error handler will be invoked in its place.
rockot0e4de5f2016-07-22 21:18:07239 return message_reader_->Send(std::move(scoped_message));
rockot506f92fa22016-03-23 01:32:18240}
241
rockot7c6bf952016-07-14 00:34:11242Channel::AssociatedInterfaceSupport*
243ChannelMojo::GetAssociatedInterfaceSupport() { return this; }
244
rockota628d0b2017-02-09 08:40:15245std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
246ChannelMojo::CreateThreadSafeChannel() {
Jeremy Roman160eb922017-08-29 17:43:43247 return std::make_unique<mojo::ThreadSafeForwarder<mojom::Channel>>(
248 task_runner_,
Wez888ef822017-10-30 16:46:06249 base::Bind(&ChannelMojo::ForwardMessageFromThreadSafePtr, weak_ptr_),
rockota628d0b2017-02-09 08:40:15250 base::Bind(&ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr,
Wez888ef822017-10-30 16:46:06251 weak_ptr_),
yzshen2859a2ac2017-02-14 22:24:25252 *bootstrap_->GetAssociatedGroup());
rockota628d0b2017-02-09 08:40:15253}
254
sammcf810f07f2016-11-10 22:34:07255void ChannelMojo::OnPeerPidReceived(int32_t peer_pid) {
256 listener_->OnChannelConnected(peer_pid);
rockot0e4de5f2016-07-22 21:18:07257}
258
sammce4d0abd2016-03-07 22:38:04259void ChannelMojo::OnMessageReceived(const Message& message) {
morrita7126b7a2014-12-17 19:01:40260 TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived",
261 "class", IPC_MESSAGE_ID_CLASS(message.type()),
262 "line", IPC_MESSAGE_ID_LINE(message.type()));
[email protected]64860882014-08-04 23:44:17263 listener_->OnMessageReceived(message);
264 if (message.dispatch_error())
265 listener_->OnBadMessageReceived(message);
266}
267
Roman Karaseva43d5b4e2017-12-21 03:06:02268void ChannelMojo::OnBrokenDataReceived() {
269 listener_->OnBadMessageReceived(Message());
270}
271
morrita3b41d6c2014-09-11 19:06:29272// static
morrita4b5c28e22015-01-14 21:17:06273MojoResult ChannelMojo::ReadFromMessageAttachmentSet(
morrita96693852014-09-24 20:11:45274 Message* message,
Eve Martin-Jones475e7e62018-02-13 22:57:25275 base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles) {
yzshen24b40a32016-08-24 01:10:13276 DCHECK(!*handles);
277
278 MojoResult result = MOJO_RESULT_OK;
279 if (!message->HasAttachments())
280 return result;
281
Eve Martin-Jones475e7e62018-02-13 22:57:25282 std::vector<mojo::native::SerializedHandlePtr> output_handles;
yzshen24b40a32016-08-24 01:10:13283 MessageAttachmentSet* set = message->attachment_set();
284
sammc6ed3efb2016-11-23 03:17:35285 for (unsigned i = 0; result == MOJO_RESULT_OK && i < set->size(); ++i) {
Ken Rockotfd907632017-09-14 04:23:41286 auto attachment = set->GetAttachmentAt(i);
Eve Martin-Jones475e7e62018-02-13 22:57:25287 auto serialized_handle = mojo::native::SerializedHandle::New();
Ken Rockotfd907632017-09-14 04:23:41288 serialized_handle->the_handle = attachment->TakeMojoHandle();
289 serialized_handle->type =
Eve Martin-Jones475e7e62018-02-13 22:57:25290 mojo::ConvertTo<mojo::native::SerializedHandle::Type>(
291 attachment->GetType());
Ken Rockotfd907632017-09-14 04:23:41292 output_handles.emplace_back(std::move(serialized_handle));
morrita3b41d6c2014-09-11 19:06:29293 }
yzshen24b40a32016-08-24 01:10:13294 set->CommitAllDescriptors();
295
296 if (!output_handles.empty())
297 *handles = std::move(output_handles);
298
299 return result;
morrita3b41d6c2014-09-11 19:06:29300}
301
morrita81b17e02015-02-06 00:58:30302// static
303MojoResult ChannelMojo::WriteToMessageAttachmentSet(
Eve Martin-Jones475e7e62018-02-13 22:57:25304 base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
morrita81b17e02015-02-06 00:58:30305 Message* message) {
Ken Rockotfd907632017-09-14 04:23:41306 if (!handles)
yzshen24b40a32016-08-24 01:10:13307 return MOJO_RESULT_OK;
Ken Rockotfd907632017-09-14 04:23:41308 for (size_t i = 0; i < handles->size(); ++i) {
309 auto& handle = handles->at(i);
310 scoped_refptr<MessageAttachment> unwrapped_attachment =
311 MessageAttachment::CreateFromMojoHandle(
312 std::move(handle->the_handle),
313 mojo::ConvertTo<MessageAttachment::Type>(handle->type));
314 if (!unwrapped_attachment) {
315 DLOG(WARNING) << "Pipe failed to unwrap handles.";
316 return MOJO_RESULT_UNKNOWN;
sammc57ed9f982016-03-10 06:28:35317 }
sammc57ed9f982016-03-10 06:28:35318
morrita81b17e02015-02-06 00:58:30319 bool ok = message->attachment_set()->AddAttachment(
sammc57ed9f982016-03-10 06:28:35320 std::move(unwrapped_attachment));
morrita81b17e02015-02-06 00:58:30321 DCHECK(ok);
322 if (!ok) {
morritaa3889aa2015-03-16 22:40:51323 LOG(ERROR) << "Failed to add new Mojo handle.";
morrita81b17e02015-02-06 00:58:30324 return MOJO_RESULT_UNKNOWN;
325 }
326 }
morrita81b17e02015-02-06 00:58:30327 return MOJO_RESULT_OK;
328}
[email protected]64860882014-08-04 23:44:17329
rockot7c6bf952016-07-14 00:34:11330void ChannelMojo::AddGenericAssociatedInterface(
331 const std::string& name,
332 const GenericAssociatedInterfaceFactory& factory) {
rockot0e4de5f2016-07-22 21:18:07333 base::AutoLock locker(associated_interface_lock_);
rockot7c6bf952016-07-14 00:34:11334 auto result = associated_interfaces_.insert({ name, factory });
335 DCHECK(result.second);
336}
337
338void ChannelMojo::GetGenericRemoteAssociatedInterface(
339 const std::string& name,
340 mojo::ScopedInterfaceEndpointHandle handle) {
yzshen34dcca732017-03-25 08:27:17341 if (message_reader_) {
rockot0e4de5f2016-07-22 21:18:07342 message_reader_->GetRemoteInterface(name, std::move(handle));
yzshen34dcca732017-03-25 08:27:17343 } else {
344 // Attach the associated interface to a disconnected pipe, so that the
345 // associated interface pointer can be used to make calls (which are
346 // dropped).
Balazs Engedyf0b497ed2017-11-04 00:50:21347 mojo::AssociateWithDisconnectedPipe(std::move(handle));
yzshen34dcca732017-03-25 08:27:17348 }
rockot8d890f62016-07-14 16:37:14349}
350
[email protected]64860882014-08-04 23:44:17351} // namespace IPC