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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_TESTS_BINDINGS_TEST_NATIVE_TYPES_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_BINDINGS_TEST_NATIVE_TYPES_H_
7 
8 #include <string>
9 
10 #include "base/macros.h"
11 #include "ipc/ipc_message.h"
12 #include "ipc/ipc_param_traits.h"
13 #include "mojo/public/cpp/system/message_pipe.h"
14 
15 namespace mojo {
16 namespace test {
17 
18 class TestNativeStruct {
19  public:
20   TestNativeStruct();
21   TestNativeStruct(const std::string& message, int x, int y);
22   ~TestNativeStruct();
23 
message()24   const std::string& message() const { return message_; }
set_message(const std::string & message)25   void set_message(const std::string& message) { message_ = message; }
26 
x()27   int x() const { return x_; }
set_x(int x)28   void set_x(int x) { x_ = x; }
29 
y()30   int y() const { return y_; }
set_y(int y)31   void set_y(int y) { y_ = y; }
32 
33  private:
34   std::string message_;
35   int x_, y_;
36 };
37 
38 class TestNativeStructWithAttachments {
39  public:
40   TestNativeStructWithAttachments();
41   TestNativeStructWithAttachments(TestNativeStructWithAttachments&& other);
42   TestNativeStructWithAttachments(const std::string& message,
43                                   ScopedMessagePipeHandle pipe);
44   ~TestNativeStructWithAttachments();
45 
46   TestNativeStructWithAttachments& operator=(
47       TestNativeStructWithAttachments&& other);
48 
message()49   const std::string& message() const { return message_; }
set_message(const std::string & message)50   void set_message(const std::string& message) { message_ = message; }
51 
set_pipe(mojo::ScopedMessagePipeHandle pipe)52   void set_pipe(mojo::ScopedMessagePipeHandle pipe) { pipe_ = std::move(pipe); }
PassPipe()53   mojo::ScopedMessagePipeHandle PassPipe() const { return std::move(pipe_); }
54 
55  private:
56   std::string message_;
57   mutable mojo::ScopedMessagePipeHandle pipe_;
58 
59   DISALLOW_COPY_AND_ASSIGN(TestNativeStructWithAttachments);
60 };
61 
62 }  // namespace test
63 }  // namespace mojo
64 
65 namespace IPC {
66 
67 template <>
68 struct ParamTraits<mojo::test::TestNativeStruct> {
69   using param_type = mojo::test::TestNativeStruct;
70 
71   static void Write(base::Pickle* m, const param_type& p);
72   static bool Read(const base::Pickle* m,
73                    base::PickleIterator* iter,
74                    param_type* r);
75   static void Log(const param_type& p, std::string* l);
76 };
77 
78 template <>
79 struct ParamTraits<mojo::test::TestNativeStructWithAttachments> {
80   using param_type = mojo::test::TestNativeStructWithAttachments;
81 
82   static void Write(Message* m, const param_type& p);
83   static bool Read(const Message* m, base::PickleIterator* iter, param_type* r);
84   static void Log(const param_type& p, std::string* l);
85 };
86 
87 }  // namespace IPC
88 
89 #endif  // MOJO_PUBLIC_CPP_BINDINGS_TESTS_BINDINGS_TEST_NATIVE_TYPES_H_
90