morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 1 | // Copyright 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 | |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 5 | #include "ipc/ipc_mojo_param_traits.h" |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 6 | |
| 7 | #include "ipc/ipc_message_utils.h" |
scottmg | efb69730 | 2017-04-12 22:37:30 | [diff] [blame] | 8 | #include "ipc/ipc_mojo_handle_attachment.h" |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 9 | #include "ipc/ipc_mojo_message_helper.h" |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 10 | |
| 11 | namespace IPC { |
| 12 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 13 | void ParamTraits<mojo::MessagePipeHandle>::Write(base::Pickle* m, |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 14 | const param_type& p) { |
| 15 | WriteParam(m, p.is_valid()); |
| 16 | if (p.is_valid()) |
| 17 | MojoMessageHelper::WriteMessagePipeTo(m, mojo::ScopedMessagePipeHandle(p)); |
| 18 | } |
| 19 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 20 | bool ParamTraits<mojo::MessagePipeHandle>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 21 | base::PickleIterator* iter, |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 22 | param_type* r) { |
| 23 | bool is_valid; |
| 24 | if (!ReadParam(m, iter, &is_valid)) |
| 25 | return false; |
| 26 | if (!is_valid) |
| 27 | return true; |
| 28 | |
| 29 | mojo::ScopedMessagePipeHandle handle; |
| 30 | if (!MojoMessageHelper::ReadMessagePipeFrom(m, iter, &handle)) |
| 31 | return false; |
| 32 | DCHECK(handle.is_valid()); |
| 33 | *r = handle.release(); |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | void ParamTraits<mojo::MessagePipeHandle>::Log(const param_type& p, |
| 38 | std::string* l) { |
| 39 | l->append("mojo::MessagePipeHandle("); |
| 40 | LogParam(p.value(), l); |
| 41 | l->append(")"); |
| 42 | } |
| 43 | |
scottmg | efb69730 | 2017-04-12 22:37:30 | [diff] [blame] | 44 | void ParamTraits<mojo::DataPipeConsumerHandle>::Write(base::Pickle* m, |
| 45 | const param_type& p) { |
| 46 | WriteParam(m, p.is_valid()); |
| 47 | if (!p.is_valid()) |
| 48 | return; |
| 49 | |
| 50 | m->WriteAttachment(new internal::MojoHandleAttachment( |
| 51 | mojo::ScopedHandle::From(mojo::ScopedDataPipeConsumerHandle(p)))); |
| 52 | } |
| 53 | |
| 54 | bool ParamTraits<mojo::DataPipeConsumerHandle>::Read(const base::Pickle* m, |
| 55 | base::PickleIterator* iter, |
| 56 | param_type* r) { |
| 57 | bool is_valid; |
| 58 | if (!ReadParam(m, iter, &is_valid)) |
| 59 | return false; |
| 60 | if (!is_valid) |
| 61 | return true; |
| 62 | |
| 63 | scoped_refptr<base::Pickle::Attachment> attachment; |
| 64 | if (!m->ReadAttachment(iter, &attachment)) { |
| 65 | DLOG(ERROR) << "Failed to read attachment for message pipe."; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | MessageAttachment::Type type = |
| 70 | static_cast<MessageAttachment*>(attachment.get())->GetType(); |
| 71 | if (type != MessageAttachment::Type::MOJO_HANDLE) { |
Ken Rockot | fd90763 | 2017-09-14 04:23:41 | [diff] [blame] | 72 | DLOG(ERROR) << "Unexpected attachment type:" << static_cast<int>(type); |
scottmg | efb69730 | 2017-04-12 22:37:30 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
| 76 | mojo::ScopedDataPipeConsumerHandle handle; |
| 77 | handle.reset(mojo::DataPipeConsumerHandle( |
| 78 | static_cast<internal::MojoHandleAttachment*>(attachment.get()) |
| 79 | ->TakeHandle() |
| 80 | .release() |
| 81 | .value())); |
| 82 | DCHECK(handle.is_valid()); |
| 83 | *r = handle.release(); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | void ParamTraits<mojo::DataPipeConsumerHandle>::Log(const param_type& p, |
| 88 | std::string* l) { |
| 89 | l->append("mojo::DataPipeConsumerHandle("); |
| 90 | LogParam(p.value(), l); |
| 91 | l->append(")"); |
| 92 | } |
| 93 | |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 94 | } // namespace IPC |