blob: 5eeb9f0fbb7b44c6fcbeef3abd66d1325738b1a8 [file] [log] [blame]
morrita438a2ee2015-04-03 05:28:211// 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
amistryd4aa70d2016-06-23 07:52:375#include "ipc/ipc_mojo_param_traits.h"
morrita438a2ee2015-04-03 05:28:216
7#include "ipc/ipc_message_utils.h"
scottmgefb697302017-04-12 22:37:308#include "ipc/ipc_mojo_handle_attachment.h"
amistryd4aa70d2016-06-23 07:52:379#include "ipc/ipc_mojo_message_helper.h"
morrita438a2ee2015-04-03 05:28:2110
11namespace IPC {
12
amistry36182522016-06-27 06:34:4213void ParamTraits<mojo::MessagePipeHandle>::Write(base::Pickle* m,
morrita438a2ee2015-04-03 05:28:2114 const param_type& p) {
15 WriteParam(m, p.is_valid());
16 if (p.is_valid())
17 MojoMessageHelper::WriteMessagePipeTo(m, mojo::ScopedMessagePipeHandle(p));
18}
19
amistry36182522016-06-27 06:34:4220bool ParamTraits<mojo::MessagePipeHandle>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:2521 base::PickleIterator* iter,
morrita438a2ee2015-04-03 05:28:2122 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
37void 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
scottmgefb697302017-04-12 22:37:3044void 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
54bool 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 Rockotfd907632017-09-14 04:23:4172 DLOG(ERROR) << "Unexpected attachment type:" << static_cast<int>(type);
scottmgefb697302017-04-12 22:37:3073 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
87void 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
morrita438a2ee2015-04-03 05:28:2194} // namespace IPC