morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 1 | // 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/mojo/ipc_mojo_message_helper.h" |
| 6 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame^] | 7 | #include <utility> |
| 8 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 9 | #include "ipc/mojo/ipc_mojo_handle_attachment.h" |
| 10 | |
| 11 | namespace IPC { |
| 12 | |
| 13 | // static |
| 14 | bool MojoMessageHelper::WriteMessagePipeTo( |
| 15 | Message* message, |
| 16 | mojo::ScopedMessagePipeHandle handle) { |
| 17 | message->WriteAttachment(new internal::MojoHandleAttachment( |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame^] | 18 | mojo::ScopedHandle::From(std::move(handle)))); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 19 | return true; |
| 20 | } |
| 21 | |
| 22 | // static |
| 23 | bool MojoMessageHelper::ReadMessagePipeFrom( |
| 24 | const Message* message, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 25 | base::PickleIterator* iter, |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 26 | mojo::ScopedMessagePipeHandle* handle) { |
| 27 | scoped_refptr<MessageAttachment> attachment; |
| 28 | if (!message->ReadAttachment(iter, &attachment)) { |
morrita | a3889aa | 2015-03-16 22:40:51 | [diff] [blame] | 29 | LOG(ERROR) << "Failed to read attachment for message pipe."; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 30 | return false; |
| 31 | } |
| 32 | |
| 33 | if (attachment->GetType() != MessageAttachment::TYPE_MOJO_HANDLE) { |
morrita | a3889aa | 2015-03-16 22:40:51 | [diff] [blame] | 34 | LOG(ERROR) << "Unxpected attachment type:" << attachment->GetType(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 35 | return false; |
| 36 | } |
| 37 | |
| 38 | handle->reset(mojo::MessagePipeHandle( |
| 39 | static_cast<internal::MojoHandleAttachment*>(attachment.get()) |
| 40 | ->TakeHandle() |
| 41 | .release() |
| 42 | .value())); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | MojoMessageHelper::MojoMessageHelper() { |
| 47 | } |
| 48 | |
| 49 | } // namespace IPC |