blob: 460e04f547750feb9fc6ffbc5ee784e8d32ada2c [file] [log] [blame]
morrita81b17e02015-02-06 00:58:301// Copyright (c) 2015 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_mojo_message_helper.h"
morrita81b17e02015-02-06 00:58:306
dchenge48600452015-12-28 02:24:507#include <utility>
8
Hans Wennborg24397592020-06-17 16:58:509#include "base/logging.h"
amistryd4aa70d2016-06-23 07:52:3710#include "ipc/ipc_mojo_handle_attachment.h"
morrita81b17e02015-02-06 00:58:3011
12namespace IPC {
13
14// static
15bool MojoMessageHelper::WriteMessagePipeTo(
rockot502c94f2016-02-03 20:20:1616 base::Pickle* message,
morrita81b17e02015-02-06 00:58:3017 mojo::ScopedMessagePipeHandle handle) {
18 message->WriteAttachment(new internal::MojoHandleAttachment(
dchenge48600452015-12-28 02:24:5019 mojo::ScopedHandle::From(std::move(handle))));
morrita81b17e02015-02-06 00:58:3020 return true;
21}
22
23// static
24bool MojoMessageHelper::ReadMessagePipeFrom(
rockot502c94f2016-02-03 20:20:1625 const base::Pickle* message,
brettwbd4d7112015-06-03 04:29:2526 base::PickleIterator* iter,
morrita81b17e02015-02-06 00:58:3027 mojo::ScopedMessagePipeHandle* handle) {
rockot502c94f2016-02-03 20:20:1628 scoped_refptr<base::Pickle::Attachment> attachment;
morrita81b17e02015-02-06 00:58:3029 if (!message->ReadAttachment(iter, &attachment)) {
morritaa3889aa2015-03-16 22:40:5130 LOG(ERROR) << "Failed to read attachment for message pipe.";
morrita81b17e02015-02-06 00:58:3031 return false;
32 }
33
rockot502c94f2016-02-03 20:20:1634 MessageAttachment::Type type =
35 static_cast<MessageAttachment*>(attachment.get())->GetType();
sammc6ed3efb2016-11-23 03:17:3536 if (type != MessageAttachment::Type::MOJO_HANDLE) {
Ken Rockotfd907632017-09-14 04:23:4137 LOG(ERROR) << "Unxpected attachment type:" << static_cast<int>(type);
morrita81b17e02015-02-06 00:58:3038 return false;
39 }
40
41 handle->reset(mojo::MessagePipeHandle(
42 static_cast<internal::MojoHandleAttachment*>(attachment.get())
43 ->TakeHandle()
44 .release()
45 .value()));
46 return true;
47}
48
Chris Watkins2d879af2017-11-30 02:11:5949MojoMessageHelper::MojoMessageHelper() = default;
morrita81b17e02015-02-06 00:58:3050
51} // namespace IPC