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