blob: 798172a1eaf1e3f111aa2e0623d8f652e0002292 [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"
Julie Jeongeun Kim903b34b2019-09-25 11:11:5429#include "mojo/public/cpp/bindings/associated_remote.h"
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2030#include "mojo/public/cpp/bindings/lib/message_quota_checker.h"
Julie Jeongeun Kim903b34b2019-09-25 11:11:5431#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
amistrycbdbf182016-06-09 04:08:1232#include "mojo/public/cpp/system/platform_handle.h"
[email protected]64860882014-08-04 23:44:1733
[email protected]64860882014-08-04 23:44:1734namespace IPC {
35
36namespace {
37
[email protected]64860882014-08-04 23:44:1738class MojoChannelFactory : public ChannelFactory {
39 public:
rockota34707ca2016-07-20 04:28:3240 MojoChannelFactory(
41 mojo::ScopedMessagePipeHandle handle,
42 Channel::Mode mode,
Hajime Hoshia98f1102017-11-20 06:34:3543 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
44 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner)
rockota34707ca2016-07-20 04:28:3245 : handle_(std::move(handle)),
46 mode_(mode),
Hajime Hoshia98f1102017-11-20 06:34:3547 ipc_task_runner_(ipc_task_runner),
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2048 proxy_task_runner_(proxy_task_runner),
49 quota_checker_(mojo::internal::MessageQuotaChecker::MaybeCreate()) {}
[email protected]64860882014-08-04 23:44:1750
danakj03de39b22016-04-23 04:21:0951 std::unique_ptr<Channel> BuildChannel(Listener* listener) override {
Hajime Hoshia98f1102017-11-20 06:34:3552 return ChannelMojo::Create(std::move(handle_), mode_, listener,
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2053 ipc_task_runner_, proxy_task_runner_,
54 quota_checker_);
rockota34707ca2016-07-20 04:28:3255 }
56
57 scoped_refptr<base::SingleThreadTaskRunner> GetIPCTaskRunner() override {
58 return ipc_task_runner_;
[email protected]64860882014-08-04 23:44:1759 }
60
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2061 scoped_refptr<mojo::internal::MessageQuotaChecker> GetQuotaChecker()
62 override {
63 return quota_checker_;
64 }
65
[email protected]64860882014-08-04 23:44:1766 private:
sammc57ed9f982016-03-10 06:28:3567 mojo::ScopedMessagePipeHandle handle_;
sammce4d0abd2016-03-07 22:38:0468 const Channel::Mode mode_;
rockota34707ca2016-07-20 04:28:3269 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
Hajime Hoshia98f1102017-11-20 06:34:3570 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_;
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2071 scoped_refptr<mojo::internal::MessageQuotaChecker> quota_checker_;
[email protected]64860882014-08-04 23:44:1772
sammce4d0abd2016-03-07 22:38:0473 DISALLOW_COPY_AND_ASSIGN(MojoChannelFactory);
morritaf8f92dcd2014-10-27 20:10:2574};
75
sammcf810f07f2016-11-10 22:34:0776base::ProcessId GetSelfPID() {
Sean McAllister82700412020-08-19 20:10:3577#if defined(OS_LINUX) || defined(OS_CHROMEOS)
sammcf810f07f2016-11-10 22:34:0778 if (int global_pid = Channel::GetGlobalPid())
79 return global_pid;
Sean McAllister82700412020-08-19 20:10:3580#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
sammcf810f07f2016-11-10 22:34:0781#if defined(OS_NACL)
82 return -1;
83#else
84 return base::GetCurrentProcId();
85#endif // defined(OS_NACL)
86}
87
rockotdbb3bb6b2015-05-11 22:53:2288} // namespace
[email protected]64860882014-08-04 23:44:1789
90//------------------------------------------------------------------------------
91
[email protected]64860882014-08-04 23:44:1792// static
danakj03de39b22016-04-23 04:21:0993std::unique_ptr<ChannelMojo> ChannelMojo::Create(
sammc57ed9f982016-03-10 06:28:3594 mojo::ScopedMessagePipeHandle handle,
95 Mode mode,
rockota34707ca2016-07-20 04:28:3296 Listener* listener,
Hajime Hoshia98f1102017-11-20 06:34:3597 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2098 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner,
99 const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker) {
Hajime Hoshia98f1102017-11-20 06:34:35100 return base::WrapUnique(new ChannelMojo(std::move(handle), mode, listener,
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:20101 ipc_task_runner, proxy_task_runner,
102 quota_checker));
[email protected]64860882014-08-04 23:44:17103}
104
105// static
danakj03de39b22016-04-23 04:21:09106std::unique_ptr<ChannelFactory> ChannelMojo::CreateServerFactory(
rockota34707ca2016-07-20 04:28:32107 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshia98f1102017-11-20 06:34:35108 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
109 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) {
Jeremy Roman160eb922017-08-29 17:43:43110 return std::make_unique<MojoChannelFactory>(
Hajime Hoshia98f1102017-11-20 06:34:35111 std::move(handle), Channel::MODE_SERVER, ipc_task_runner,
112 proxy_task_runner);
[email protected]64860882014-08-04 23:44:17113}
114
morrita54f6f80c2014-09-23 21:16:00115// static
danakj03de39b22016-04-23 04:21:09116std::unique_ptr<ChannelFactory> ChannelMojo::CreateClientFactory(
rockota34707ca2016-07-20 04:28:32117 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshia98f1102017-11-20 06:34:35118 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
119 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) {
Jeremy Roman160eb922017-08-29 17:43:43120 return std::make_unique<MojoChannelFactory>(
Hajime Hoshia98f1102017-11-20 06:34:35121 std::move(handle), Channel::MODE_CLIENT, ipc_task_runner,
122 proxy_task_runner);
morrita54f6f80c2014-09-23 21:16:00123}
124
rockota34707ca2016-07-20 04:28:32125ChannelMojo::ChannelMojo(
126 mojo::ScopedMessagePipeHandle handle,
127 Mode mode,
128 Listener* listener,
Hajime Hoshia98f1102017-11-20 06:34:35129 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:20130 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner,
131 const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker)
Jeremy Roman7c5cfabd2019-08-12 15:45:27132 : task_runner_(ipc_task_runner), pipe_(handle.get()), listener_(listener) {
Wez888ef822017-10-30 16:46:06133 weak_ptr_ = weak_factory_.GetWeakPtr();
Hajime Hoshia98f1102017-11-20 06:34:35134 bootstrap_ = MojoBootstrap::Create(std::move(handle), mode, ipc_task_runner,
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:20135 proxy_task_runner, quota_checker);
rockota628d0b2017-02-09 08:40:15136}
137
138void ChannelMojo::ForwardMessageFromThreadSafePtr(mojo::Message message) {
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()->ForwardMessage(
143 std::move(message));
144}
145
146void ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr(
147 mojo::Message message,
148 std::unique_ptr<mojo::MessageReceiver> responder) {
peary231ef24642017-05-19 00:37:15149 DCHECK(task_runner_->RunsTasksInCurrentSequence());
rockot67b4b0c2017-02-09 21:25:20150 if (!message_reader_ || !message_reader_->sender().is_bound())
rockota628d0b2017-02-09 08:40:15151 return;
152 message_reader_->sender().internal_state()->ForwardMessageWithResponder(
153 std::move(message), std::move(responder));
[email protected]64860882014-08-04 23:44:17154}
155
156ChannelMojo::~ChannelMojo() {
peary231ef24642017-05-19 00:37:15157 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]64860882014-08-04 23:44:17158 Close();
morritae9453ea2014-09-26 03:20:48159}
morrita54f6f80c2014-09-23 21:16:00160
[email protected]64860882014-08-04 23:44:17161bool ChannelMojo::Connect() {
peary231ef24642017-05-19 00:37:15162 DCHECK(task_runner_->RunsTasksInCurrentSequence());
rockota628d0b2017-02-09 08:40:15163
erikchen90971902016-04-25 23:45:31164 WillConnect();
rockot0e4de5f2016-07-22 21:18:07165
Julie Jeongeun Kim903b34b2019-09-25 11:11:54166 mojo::AssociatedRemote<mojom::Channel> sender;
167 mojo::PendingAssociatedReceiver<mojom::Channel> receiver;
rockota628d0b2017-02-09 08:40:15168 bootstrap_->Connect(&sender, &receiver);
rockot0e4de5f2016-07-22 21:18:07169
rockota628d0b2017-02-09 08:40:15170 DCHECK(!message_reader_);
171 sender->SetPeerPid(GetSelfPID());
172 message_reader_.reset(new internal::MessagePipeReader(
173 pipe_, std::move(sender), std::move(receiver), this));
sammce4d0abd2016-03-07 22:38:04174 return true;
[email protected]64860882014-08-04 23:44:17175}
176
rockot10188752016-09-08 18:24:56177void ChannelMojo::Pause() {
178 bootstrap_->Pause();
179}
180
rockot401fb2c2016-09-06 18:35:57181void ChannelMojo::Unpause(bool flush) {
rockot10188752016-09-08 18:24:56182 bootstrap_->Unpause();
rockot401fb2c2016-09-06 18:35:57183 if (flush)
184 Flush();
185}
186
187void ChannelMojo::Flush() {
188 bootstrap_->Flush();
189}
190
[email protected]64860882014-08-04 23:44:17191void ChannelMojo::Close() {
rockot0e4de5f2016-07-22 21:18:07192 // NOTE: The MessagePipeReader's destructor may re-enter this function. Use
193 // caution when changing this method.
194 std::unique_ptr<internal::MessagePipeReader> reader =
195 std::move(message_reader_);
msramek5507fee2016-07-22 10:06:21196 reader.reset();
rockot0e4de5f2016-07-22 21:18:07197
198 base::AutoLock lock(associated_interface_lock_);
199 associated_interfaces_.clear();
sammce4d0abd2016-03-07 22:38:04200}
morritab4472142015-04-20 21:20:12201
rockot506f92fa22016-03-23 01:32:18202void ChannelMojo::OnPipeError() {
rockotc18f64f2016-03-25 04:49:18203 DCHECK(task_runner_);
peary231ef24642017-05-19 00:37:15204 if (task_runner_->RunsTasksInCurrentSequence()) {
rockot506f92fa22016-03-23 01:32:18205 listener_->OnChannelError();
206 } else {
kylecharf448cc92019-02-19 20:28:09207 task_runner_->PostTask(
208 FROM_HERE, base::BindOnce(&ChannelMojo::OnPipeError, weak_ptr_));
rockot506f92fa22016-03-23 01:32:18209 }
[email protected]64860882014-08-04 23:44:17210}
211
rockot0e4de5f2016-07-22 21:18:07212void ChannelMojo::OnAssociatedInterfaceRequest(
213 const std::string& name,
214 mojo::ScopedInterfaceEndpointHandle handle) {
215 GenericAssociatedInterfaceFactory factory;
216 {
217 base::AutoLock locker(associated_interface_lock_);
218 auto iter = associated_interfaces_.find(name);
219 if (iter != associated_interfaces_.end())
220 factory = iter->second;
msramek5507fee2016-07-22 10:06:21221 }
rockot508da2462016-07-22 03:53:59222
rockot0e4de5f2016-07-22 21:18:07223 if (!factory.is_null())
224 factory.Run(std::move(handle));
rockotf62002a2016-09-15 00:08:59225 else
226 listener_->OnAssociatedInterfaceRequest(name, std::move(handle));
rockot0e4de5f2016-07-22 21:18:07227}
228
229bool ChannelMojo::Send(Message* message) {
Wezc7fcdea2018-03-09 06:20:00230 DVLOG(2) << "sending message @" << message << " on channel @" << this
231 << " with type " << message->type();
232#if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
233 Logging::GetInstance()->OnSendMessage(message);
234#endif
235
rockot0e4de5f2016-07-22 21:18:07236 std::unique_ptr<Message> scoped_message = base::WrapUnique(message);
237 if (!message_reader_)
238 return false;
239
amistry1e355dd12016-07-11 21:33:28240 // Comment copied from ipc_channel_posix.cc:
241 // We can't close the pipe here, because calling OnChannelError may destroy
242 // this object, and that would be bad if we are called from Send(). Instead,
243 // we return false and hope the caller will close the pipe. If they do not,
244 // the pipe will still be closed next time OnFileCanReadWithoutBlocking is
245 // called.
246 //
247 // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the
248 // pipe's connection error handler will be invoked in its place.
rockot0e4de5f2016-07-22 21:18:07249 return message_reader_->Send(std::move(scoped_message));
rockot506f92fa22016-03-23 01:32:18250}
251
rockot7c6bf952016-07-14 00:34:11252Channel::AssociatedInterfaceSupport*
253ChannelMojo::GetAssociatedInterfaceSupport() { return this; }
254
rockota628d0b2017-02-09 08:40:15255std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
256ChannelMojo::CreateThreadSafeChannel() {
Jeremy Roman160eb922017-08-29 17:43:43257 return std::make_unique<mojo::ThreadSafeForwarder<mojom::Channel>>(
258 task_runner_,
Matt Falkenhagen6140bb12019-11-19 22:52:36259 base::BindRepeating(&ChannelMojo::ForwardMessageFromThreadSafePtr,
260 weak_ptr_),
261 base::BindRepeating(
262 &ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr,
263 weak_ptr_),
Ken Rockot5e23e5982020-07-08 06:06:34264 base::DoNothing(), *bootstrap_->GetAssociatedGroup());
rockota628d0b2017-02-09 08:40:15265}
266
sammcf810f07f2016-11-10 22:34:07267void ChannelMojo::OnPeerPidReceived(int32_t peer_pid) {
268 listener_->OnChannelConnected(peer_pid);
rockot0e4de5f2016-07-22 21:18:07269}
270
sammce4d0abd2016-03-07 22:38:04271void ChannelMojo::OnMessageReceived(const Message& message) {
morrita7126b7a2014-12-17 19:01:40272 TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived",
273 "class", IPC_MESSAGE_ID_CLASS(message.type()),
274 "line", IPC_MESSAGE_ID_LINE(message.type()));
[email protected]64860882014-08-04 23:44:17275 listener_->OnMessageReceived(message);
276 if (message.dispatch_error())
277 listener_->OnBadMessageReceived(message);
278}
279
Roman Karaseva43d5b4e2017-12-21 03:06:02280void ChannelMojo::OnBrokenDataReceived() {
281 listener_->OnBadMessageReceived(Message());
282}
283
morrita3b41d6c2014-09-11 19:06:29284// static
morrita4b5c28e22015-01-14 21:17:06285MojoResult ChannelMojo::ReadFromMessageAttachmentSet(
morrita96693852014-09-24 20:11:45286 Message* message,
Eve Martin-Jones475e7e62018-02-13 22:57:25287 base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles) {
yzshen24b40a32016-08-24 01:10:13288 DCHECK(!*handles);
289
290 MojoResult result = MOJO_RESULT_OK;
291 if (!message->HasAttachments())
292 return result;
293
Eve Martin-Jones475e7e62018-02-13 22:57:25294 std::vector<mojo::native::SerializedHandlePtr> output_handles;
yzshen24b40a32016-08-24 01:10:13295 MessageAttachmentSet* set = message->attachment_set();
296
sammc6ed3efb2016-11-23 03:17:35297 for (unsigned i = 0; result == MOJO_RESULT_OK && i < set->size(); ++i) {
Ken Rockotfd907632017-09-14 04:23:41298 auto attachment = set->GetAttachmentAt(i);
Eve Martin-Jones475e7e62018-02-13 22:57:25299 auto serialized_handle = mojo::native::SerializedHandle::New();
Ken Rockotfd907632017-09-14 04:23:41300 serialized_handle->the_handle = attachment->TakeMojoHandle();
301 serialized_handle->type =
Ken Rockot28580fd022019-03-07 00:18:15302 mojo::ConvertTo<mojo::native::SerializedHandleType>(
Eve Martin-Jones475e7e62018-02-13 22:57:25303 attachment->GetType());
Ken Rockotfd907632017-09-14 04:23:41304 output_handles.emplace_back(std::move(serialized_handle));
morrita3b41d6c2014-09-11 19:06:29305 }
yzshen24b40a32016-08-24 01:10:13306 set->CommitAllDescriptors();
307
308 if (!output_handles.empty())
309 *handles = std::move(output_handles);
310
311 return result;
morrita3b41d6c2014-09-11 19:06:29312}
313
morrita81b17e02015-02-06 00:58:30314// static
315MojoResult ChannelMojo::WriteToMessageAttachmentSet(
Eve Martin-Jones475e7e62018-02-13 22:57:25316 base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
morrita81b17e02015-02-06 00:58:30317 Message* message) {
Ken Rockotfd907632017-09-14 04:23:41318 if (!handles)
yzshen24b40a32016-08-24 01:10:13319 return MOJO_RESULT_OK;
Ken Rockotfd907632017-09-14 04:23:41320 for (size_t i = 0; i < handles->size(); ++i) {
321 auto& handle = handles->at(i);
322 scoped_refptr<MessageAttachment> unwrapped_attachment =
323 MessageAttachment::CreateFromMojoHandle(
324 std::move(handle->the_handle),
325 mojo::ConvertTo<MessageAttachment::Type>(handle->type));
326 if (!unwrapped_attachment) {
327 DLOG(WARNING) << "Pipe failed to unwrap handles.";
328 return MOJO_RESULT_UNKNOWN;
sammc57ed9f982016-03-10 06:28:35329 }
sammc57ed9f982016-03-10 06:28:35330
morrita81b17e02015-02-06 00:58:30331 bool ok = message->attachment_set()->AddAttachment(
sammc57ed9f982016-03-10 06:28:35332 std::move(unwrapped_attachment));
morrita81b17e02015-02-06 00:58:30333 DCHECK(ok);
334 if (!ok) {
morritaa3889aa2015-03-16 22:40:51335 LOG(ERROR) << "Failed to add new Mojo handle.";
morrita81b17e02015-02-06 00:58:30336 return MOJO_RESULT_UNKNOWN;
337 }
338 }
morrita81b17e02015-02-06 00:58:30339 return MOJO_RESULT_OK;
340}
[email protected]64860882014-08-04 23:44:17341
rockot7c6bf952016-07-14 00:34:11342void ChannelMojo::AddGenericAssociatedInterface(
343 const std::string& name,
344 const GenericAssociatedInterfaceFactory& factory) {
rockot0e4de5f2016-07-22 21:18:07345 base::AutoLock locker(associated_interface_lock_);
rockot7c6bf952016-07-14 00:34:11346 auto result = associated_interfaces_.insert({ name, factory });
347 DCHECK(result.second);
348}
349
350void ChannelMojo::GetGenericRemoteAssociatedInterface(
351 const std::string& name,
352 mojo::ScopedInterfaceEndpointHandle handle) {
yzshen34dcca732017-03-25 08:27:17353 if (message_reader_) {
rockot0e4de5f2016-07-22 21:18:07354 message_reader_->GetRemoteInterface(name, std::move(handle));
yzshen34dcca732017-03-25 08:27:17355 } else {
356 // Attach the associated interface to a disconnected pipe, so that the
357 // associated interface pointer can be used to make calls (which are
358 // dropped).
Balazs Engedyf0b497ed2017-11-04 00:50:21359 mojo::AssociateWithDisconnectedPipe(std::move(handle));
yzshen34dcca732017-03-25 08:27:17360 }
rockot8d890f62016-07-14 16:37:14361}
362
[email protected]64860882014-08-04 23:44:17363} // namespace IPC