blob: a87a2d694d5f974f10461f0eeedea771dc159567 [file] [log] [blame]
Luis Hector Chavez645501c2016-12-28 10:56:26 -08001// 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
5#include "ipc/ipc_mojo_message_helper.h"
6
7#include <utility>
8
9#include "ipc/ipc_mojo_handle_attachment.h"
10
11namespace IPC {
12
13// static
14bool MojoMessageHelper::WriteMessagePipeTo(
15 base::Pickle* message,
16 mojo::ScopedMessagePipeHandle handle) {
17 message->WriteAttachment(new internal::MojoHandleAttachment(
18 mojo::ScopedHandle::From(std::move(handle))));
19 return true;
20}
21
22// static
23bool MojoMessageHelper::ReadMessagePipeFrom(
24 const base::Pickle* message,
25 base::PickleIterator* iter,
26 mojo::ScopedMessagePipeHandle* handle) {
27 scoped_refptr<base::Pickle::Attachment> attachment;
28 if (!message->ReadAttachment(iter, &attachment)) {
29 LOG(ERROR) << "Failed to read attachment for message pipe.";
30 return false;
31 }
32
33 MessageAttachment::Type type =
34 static_cast<MessageAttachment*>(attachment.get())->GetType();
Jay Civellicfc1eaa2017-08-21 17:18:10 -070035 if (type != MessageAttachment::Type::MOJO_HANDLE) {
Luis Hector Chavez645501c2016-12-28 10:56:26 -080036 LOG(ERROR) << "Unxpected attachment type:" << type;
37 return false;
38 }
39
40 handle->reset(mojo::MessagePipeHandle(
41 static_cast<internal::MojoHandleAttachment*>(attachment.get())
42 ->TakeHandle()
43 .release()
44 .value()));
45 return true;
46}
47
48MojoMessageHelper::MojoMessageHelper() {
49}
50
51} // namespace IPC