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 
5 #include "ipc/ipc_mojo_param_traits.h"
6 
7 #include "ipc/ipc_message_utils.h"
8 #include "ipc/ipc_mojo_handle_attachment.h"
9 #include "ipc/ipc_mojo_message_helper.h"
10 
11 namespace IPC {
12 
Write(base::Pickle * m,const param_type & p)13 void ParamTraits<mojo::MessagePipeHandle>::Write(base::Pickle* m,
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 
Read(const base::Pickle * m,base::PickleIterator * iter,param_type * r)20 bool ParamTraits<mojo::MessagePipeHandle>::Read(const base::Pickle* m,
21                                                 base::PickleIterator* iter,
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 
Log(const param_type & p,std::string * l)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 
Write(base::Pickle * m,const param_type & p)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 
Read(const base::Pickle * m,base::PickleIterator * iter,param_type * r)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) {
72     DLOG(ERROR) << "Unexpected attachment type:" << static_cast<int>(type);
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 
Log(const param_type & p,std::string * l)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 
94 }  // namespace IPC
95