1 // Copyright 2017 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 "mojo/public/cpp/bindings/tests/test_native_types.h"
6 
7 #include "base/macros.h"
8 #include "ipc/ipc_mojo_message_helper.h"
9 
10 namespace mojo {
11 namespace test {
12 
13 TestNativeStruct::TestNativeStruct() = default;
14 
TestNativeStruct(const std::string & message,int x,int y)15 TestNativeStruct::TestNativeStruct(const std::string& message, int x, int y)
16     : message_(message), x_(x), y_(y) {}
17 
18 TestNativeStruct::~TestNativeStruct() = default;
19 
20 TestNativeStructWithAttachments::TestNativeStructWithAttachments() = default;
21 
22 TestNativeStructWithAttachments::TestNativeStructWithAttachments(
23     TestNativeStructWithAttachments&& other) = default;
24 
TestNativeStructWithAttachments(const std::string & message,mojo::ScopedMessagePipeHandle pipe)25 TestNativeStructWithAttachments::TestNativeStructWithAttachments(
26     const std::string& message,
27     mojo::ScopedMessagePipeHandle pipe)
28     : message_(message), pipe_(std::move(pipe)) {}
29 
30 TestNativeStructWithAttachments::~TestNativeStructWithAttachments() = default;
31 
32 TestNativeStructWithAttachments& TestNativeStructWithAttachments::operator=(
33     TestNativeStructWithAttachments&& other) = default;
34 
35 }  // namespace test
36 }  // namespace mojo
37 
38 namespace IPC {
39 
40 // static
Write(base::Pickle * m,const param_type & p)41 void ParamTraits<mojo::test::TestNativeStruct>::Write(base::Pickle* m,
42                                                       const param_type& p) {
43   m->WriteString(p.message());
44   m->WriteInt(p.x());
45   m->WriteInt(p.y());
46 }
47 
48 // static
Read(const base::Pickle * m,base::PickleIterator * iter,param_type * r)49 bool ParamTraits<mojo::test::TestNativeStruct>::Read(const base::Pickle* m,
50                                                      base::PickleIterator* iter,
51                                                      param_type* r) {
52   std::string message;
53   if (!iter->ReadString(&message))
54     return false;
55   int x, y;
56   if (!iter->ReadInt(&x) || !iter->ReadInt(&y))
57     return false;
58   r->set_message(message);
59   r->set_x(x);
60   r->set_y(y);
61   return true;
62 }
63 
64 // static
Log(const param_type & p,std::string * l)65 void ParamTraits<mojo::test::TestNativeStruct>::Log(const param_type& p,
66                                                     std::string* l) {}
67 
68 // static
Write(Message * m,const param_type & p)69 void ParamTraits<mojo::test::TestNativeStructWithAttachments>::Write(
70     Message* m,
71     const param_type& p) {
72   m->WriteString(p.message());
73   IPC::MojoMessageHelper::WriteMessagePipeTo(m, p.PassPipe());
74 }
75 
76 // static
Read(const Message * m,base::PickleIterator * iter,param_type * r)77 bool ParamTraits<mojo::test::TestNativeStructWithAttachments>::Read(
78     const Message* m,
79     base::PickleIterator* iter,
80     param_type* r) {
81   std::string message;
82   if (!iter->ReadString(&message))
83     return false;
84   r->set_message(message);
85 
86   mojo::ScopedMessagePipeHandle pipe;
87   if (!IPC::MojoMessageHelper::ReadMessagePipeFrom(m, iter, &pipe))
88     return false;
89 
90   r->set_pipe(std::move(pipe));
91   return true;
92 }
93 
94 // static
Log(const param_type & p,std::string * l)95 void ParamTraits<mojo::test::TestNativeStructWithAttachments>::Log(
96     const param_type& p,
97     std::string* l) {}
98 
99 }  // namespace IPC
100