OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/public/bindings/sample/generated/sample_service.h" | 5 #include "mojo/public/bindings/sample/generated/sample_service.h" |
6 | 6 |
7 #include "mojo/public/bindings/lib/message_builder.h" | 7 #include "mojo/public/bindings/lib/message_builder.h" |
8 #include "mojo/public/bindings/sample/generated/sample_service_internal.h" | 8 #include "mojo/public/bindings/sample/generated/sample_service_internal.h" |
9 | 9 |
10 namespace sample { | 10 namespace sample { |
11 namespace internal { | 11 namespace { |
12 | |
13 const uint32_t kService_Frobinate_Name = 1; | 12 const uint32_t kService_Frobinate_Name = 1; |
14 | 13 |
15 #pragma pack(push, 1) | 14 #pragma pack(push, 1) |
16 | 15 #if defined(__clang__) |
| 16 #pragma clang diagnostic push |
| 17 #pragma clang diagnostic ignored "-Wunused-private-field" |
| 18 #endif |
17 class Service_Frobinate_Params { | 19 class Service_Frobinate_Params { |
18 public: | 20 public: |
19 static Service_Frobinate_Params* New(mojo::Buffer* buf); | 21 static Service_Frobinate_Params* New(mojo::Buffer* buf) { |
| 22 return new (buf->Allocate(sizeof(Service_Frobinate_Params))) |
| 23 Service_Frobinate_Params(); |
| 24 } |
20 | 25 |
21 void set_foo(Foo* foo) { foo_.ptr = foo; } | 26 void set_foo(Foo* foo) { foo_.ptr = foo; } |
22 void set_baz(bool baz) { baz_ = baz; } | 27 void set_baz(bool baz) { baz_ = baz; } |
23 void set_port(mojo::Handle port) { port_ = port; } | 28 void set_port(mojo::Handle port) { port_ = port; } |
24 | 29 |
25 const Foo* foo() const { return foo_.ptr; } | 30 const Foo* foo() const { return foo_.ptr; } |
26 bool baz() const { return baz_; } | 31 bool baz() const { return baz_; } |
27 mojo::Handle port() const { | 32 mojo::Handle port() const { |
28 // NOTE: port is an optional field! | 33 // NOTE: port is an optional field! |
29 return _header_.num_fields >= 3 ? port_ : mojo::kInvalidHandle; | 34 return _header_.num_fields >= 3 ? port_ : mojo::kInvalidHandle; |
30 } | 35 } |
31 | 36 |
32 private: | 37 private: |
33 friend class mojo::internal::ObjectTraits<Service_Frobinate_Params>; | 38 friend class mojo::internal::ObjectTraits<Service_Frobinate_Params>; |
34 | 39 |
35 Service_Frobinate_Params(); | 40 Service_Frobinate_Params() { |
36 ~Service_Frobinate_Params(); // NOT IMPLEMENTED | 41 _header_.num_bytes = sizeof(*this); |
| 42 _header_.num_fields = 3; |
| 43 } |
37 | 44 |
38 mojo::internal::StructHeader _header_; | 45 mojo::internal::StructHeader _header_; |
39 mojo::internal::StructPointer<Foo> foo_; | 46 mojo::internal::StructPointer<Foo> foo_; |
40 uint8_t baz_ : 1; | 47 uint8_t baz_ : 1; |
41 uint8_t _pad0_[3]; | 48 uint8_t _pad0_[3]; |
42 mojo::Handle port_; | 49 mojo::Handle port_; |
43 }; | 50 }; |
44 MOJO_COMPILE_ASSERT(sizeof(Service_Frobinate_Params) == 24, | 51 MOJO_COMPILE_ASSERT(sizeof(Service_Frobinate_Params) == 24, |
45 bad_sizeof_Service_Frobinate_Params); | 52 bad_sizeof_Service_Frobinate_Params); |
46 | 53 #if defined(__clang__) |
| 54 #pragma clang diagnostic pop |
| 55 #endif |
47 #pragma pack(pop) | 56 #pragma pack(pop) |
48 | 57 |
49 // static | 58 } // namespace |
50 Service_Frobinate_Params* Service_Frobinate_Params::New(mojo::Buffer* buf) { | |
51 return new (buf->Allocate(sizeof(Service_Frobinate_Params))) | |
52 Service_Frobinate_Params(); | |
53 } | |
54 | |
55 Service_Frobinate_Params::Service_Frobinate_Params() { | |
56 _header_.num_bytes = sizeof(*this); | |
57 _header_.num_fields = 3; | |
58 } | |
59 | |
60 } // namespace internal | |
61 | 59 |
62 // static | 60 // static |
63 Bar* Bar::New(mojo::Buffer* buf) { | 61 Bar* Bar::New(mojo::Buffer* buf) { |
64 return new (buf->Allocate(sizeof(Bar))) Bar(); | 62 return new (buf->Allocate(sizeof(Bar))) Bar(); |
65 } | 63 } |
66 | 64 |
67 Bar::Bar() { | 65 Bar::Bar() { |
68 _header_.num_bytes = sizeof(*this); | 66 _header_.num_bytes = sizeof(*this); |
69 _header_.num_fields = 3; | 67 _header_.num_fields = 3; |
70 } | 68 } |
71 | 69 |
72 // static | 70 // static |
73 Foo* Foo::New(mojo::Buffer* buf) { | 71 Foo* Foo::New(mojo::Buffer* buf) { |
74 return new (buf->Allocate(sizeof(Foo))) Foo(); | 72 return new (buf->Allocate(sizeof(Foo))) Foo(); |
75 } | 73 } |
76 | 74 |
77 Foo::Foo() { | 75 Foo::Foo() { |
78 _header_.num_bytes = sizeof(*this); | 76 _header_.num_bytes = sizeof(*this); |
79 _header_.num_fields = 10; | 77 _header_.num_fields = 10; |
80 } | 78 } |
81 | 79 |
82 ServiceProxy::ServiceProxy(mojo::MessageReceiver* receiver) | 80 ServiceProxy::ServiceProxy(mojo::MessageReceiver* receiver) |
83 : receiver_(receiver) { | 81 : receiver_(receiver) { |
84 } | 82 } |
85 | 83 |
86 void ServiceProxy::Frobinate(const Foo* foo, bool baz, mojo::Handle port) { | 84 void ServiceProxy::Frobinate(const Foo* foo, bool baz, mojo::Handle port) { |
87 size_t payload_size = | 85 size_t payload_size = |
88 mojo::internal::Align(sizeof(internal::Service_Frobinate_Params)); | 86 mojo::internal::Align(sizeof(Service_Frobinate_Params)); |
89 payload_size += mojo::internal::ComputeSizeOf(foo); | 87 payload_size += mojo::internal::ComputeSizeOf(foo); |
90 | 88 |
91 mojo::MessageBuilder builder(internal::kService_Frobinate_Name, payload_size); | 89 mojo::MessageBuilder builder(kService_Frobinate_Name, payload_size); |
92 | 90 |
93 // We now go about allocating the anonymous Frobinate_Params struct. It | 91 // We now go about allocating the anonymous Frobinate_Params struct. It |
94 // holds the parameters to the Frobinate message. | 92 // holds the parameters to the Frobinate message. |
95 // | 93 // |
96 // Notice how foo is cloned. This causes a copy of foo to be generated | 94 // Notice how foo is cloned. This causes a copy of foo to be generated |
97 // within the same buffer as the Frobinate_Params struct. That's what we | 95 // within the same buffer as the Frobinate_Params struct. That's what we |
98 // need in order to generate a contiguous blob of message data. | 96 // need in order to generate a contiguous blob of message data. |
99 | 97 |
100 internal::Service_Frobinate_Params* params = | 98 Service_Frobinate_Params* params = |
101 internal::Service_Frobinate_Params::New(builder.buffer()); | 99 Service_Frobinate_Params::New(builder.buffer()); |
102 params->set_foo(mojo::internal::Clone(foo, builder.buffer())); | 100 params->set_foo(mojo::internal::Clone(foo, builder.buffer())); |
103 params->set_baz(baz); | 101 params->set_baz(baz); |
104 params->set_port(port); | 102 params->set_port(port); |
105 | 103 |
106 // NOTE: If foo happened to be a graph with cycles, then Clone would not | 104 // NOTE: If foo happened to be a graph with cycles, then Clone would not |
107 // have returned. | 105 // have returned. |
108 | 106 |
109 // Next step is to encode pointers and handles so that messages become | 107 // Next step is to encode pointers and handles so that messages become |
110 // hermetic. Pointers become offsets and handles becomes indices into the | 108 // hermetic. Pointers become offsets and handles becomes indices into the |
111 // handles array. | 109 // handles array. |
112 mojo::Message message; | 110 mojo::Message message; |
113 mojo::internal::EncodePointersAndHandles(params, &message.handles); | 111 mojo::internal::EncodePointersAndHandles(params, &message.handles); |
114 | 112 |
115 // Finally, we get the generated message data, and forward it to the | 113 // Finally, we get the generated message data, and forward it to the |
116 // receiver. | 114 // receiver. |
117 message.data = builder.Finish(); | 115 message.data = builder.Finish(); |
118 | 116 |
119 receiver_->Accept(&message); | 117 receiver_->Accept(&message); |
120 } | 118 } |
121 | 119 |
122 bool ServiceStub::Accept(mojo::Message* message) { | 120 bool ServiceStub::Accept(mojo::Message* message) { |
123 switch (message->data->header.name) { | 121 switch (message->data->header.name) { |
124 case internal::kService_Frobinate_Name: { | 122 case kService_Frobinate_Name: { |
125 internal::Service_Frobinate_Params* params = | 123 Service_Frobinate_Params* params = |
126 reinterpret_cast<internal::Service_Frobinate_Params*>( | 124 reinterpret_cast<Service_Frobinate_Params*>( |
127 message->data->payload); | 125 message->data->payload); |
128 | 126 |
129 if (!mojo::internal::DecodePointersAndHandles(params, *message)) | 127 if (!mojo::internal::DecodePointersAndHandles(params, *message)) |
130 return false; | 128 return false; |
131 | 129 |
132 Frobinate(params->foo(), params->baz(), params->port()); | 130 Frobinate(params->foo(), params->baz(), params->port()); |
133 break; | 131 break; |
134 } | 132 } |
135 } | 133 } |
136 return true; | 134 return true; |
(...skipping 13 matching lines...) Expand all Loading... |
150 // static | 148 // static |
151 sample::Bar* ObjectTraits<sample::Bar>::Clone( | 149 sample::Bar* ObjectTraits<sample::Bar>::Clone( |
152 const sample::Bar* bar, Buffer* buf) { | 150 const sample::Bar* bar, Buffer* buf) { |
153 sample::Bar* clone = sample::Bar::New(buf); | 151 sample::Bar* clone = sample::Bar::New(buf); |
154 memcpy(clone, bar, sizeof(*bar)); | 152 memcpy(clone, bar, sizeof(*bar)); |
155 return clone; | 153 return clone; |
156 } | 154 } |
157 | 155 |
158 // static | 156 // static |
159 void ObjectTraits<sample::Bar>::EncodePointersAndHandles( | 157 void ObjectTraits<sample::Bar>::EncodePointersAndHandles( |
160 sample::Bar* bar, std::vector<mojo::Handle>* handles) { | 158 sample::Bar* bar, std::vector<Handle>* handles) { |
161 } | 159 } |
162 | 160 |
163 // static | 161 // static |
164 bool ObjectTraits<sample::Bar>::DecodePointersAndHandles( | 162 bool ObjectTraits<sample::Bar>::DecodePointersAndHandles( |
165 sample::Bar* bar, const mojo::Message& message) { | 163 sample::Bar* bar, const Message& message) { |
166 return true; | 164 return true; |
167 } | 165 } |
168 | 166 |
169 // static | 167 // static |
170 size_t ObjectTraits<sample::Foo>::ComputeSizeOf( | 168 size_t ObjectTraits<sample::Foo>::ComputeSizeOf( |
171 const sample::Foo* foo) { | 169 const sample::Foo* foo) { |
172 return sizeof(*foo) + | 170 return sizeof(*foo) + |
173 mojo::internal::ComputeSizeOf(foo->bar()) + | 171 mojo::internal::ComputeSizeOf(foo->bar()) + |
174 mojo::internal::ComputeSizeOf(foo->data()) + | 172 mojo::internal::ComputeSizeOf(foo->data()) + |
175 mojo::internal::ComputeSizeOf(foo->extra_bars()) + | 173 mojo::internal::ComputeSizeOf(foo->extra_bars()) + |
(...skipping 11 matching lines...) Expand all Loading... |
187 clone->set_data(mojo::internal::Clone(foo->data(), buf)); | 185 clone->set_data(mojo::internal::Clone(foo->data(), buf)); |
188 clone->set_extra_bars(mojo::internal::Clone(foo->extra_bars(), buf)); | 186 clone->set_extra_bars(mojo::internal::Clone(foo->extra_bars(), buf)); |
189 clone->set_name(mojo::internal::Clone(foo->name(), buf)); | 187 clone->set_name(mojo::internal::Clone(foo->name(), buf)); |
190 clone->set_files(mojo::internal::Clone(foo->files(), buf)); | 188 clone->set_files(mojo::internal::Clone(foo->files(), buf)); |
191 | 189 |
192 return clone; | 190 return clone; |
193 } | 191 } |
194 | 192 |
195 // static | 193 // static |
196 void ObjectTraits<sample::Foo>::EncodePointersAndHandles( | 194 void ObjectTraits<sample::Foo>::EncodePointersAndHandles( |
197 sample::Foo* foo, std::vector<mojo::Handle>* handles) { | 195 sample::Foo* foo, std::vector<Handle>* handles) { |
198 Encode(&foo->bar_, handles); | 196 Encode(&foo->bar_, handles); |
199 Encode(&foo->data_, handles); | 197 Encode(&foo->data_, handles); |
200 Encode(&foo->extra_bars_, handles); | 198 Encode(&foo->extra_bars_, handles); |
201 Encode(&foo->name_, handles); | 199 Encode(&foo->name_, handles); |
202 Encode(&foo->files_, handles); | 200 Encode(&foo->files_, handles); |
203 } | 201 } |
204 | 202 |
205 // static | 203 // static |
206 bool ObjectTraits<sample::Foo>::DecodePointersAndHandles( | 204 bool ObjectTraits<sample::Foo>::DecodePointersAndHandles( |
207 sample::Foo* foo, const mojo::Message& message) { | 205 sample::Foo* foo, const Message& message) { |
208 if (!Decode(&foo->bar_, message)) | 206 if (!Decode(&foo->bar_, message)) |
209 return false; | 207 return false; |
210 if (!Decode(&foo->data_, message)) | 208 if (!Decode(&foo->data_, message)) |
211 return false; | 209 return false; |
212 if (foo->_header_.num_fields >= 8) { | 210 if (foo->_header_.num_fields >= 8) { |
213 if (!Decode(&foo->extra_bars_, message)) | 211 if (!Decode(&foo->extra_bars_, message)) |
214 return false; | 212 return false; |
215 } | 213 } |
216 if (foo->_header_.num_fields >= 9) { | 214 if (foo->_header_.num_fields >= 9) { |
217 if (!Decode(&foo->name_, message)) | 215 if (!Decode(&foo->name_, message)) |
218 return false; | 216 return false; |
219 } | 217 } |
220 if (foo->_header_.num_fields >= 10) { | 218 if (foo->_header_.num_fields >= 10) { |
221 if (!Decode(&foo->files_, message)) | 219 if (!Decode(&foo->files_, message)) |
222 return false; | 220 return false; |
223 } | 221 } |
224 | 222 |
225 // TODO: validate | 223 // TODO: validate |
226 return true; | 224 return true; |
227 } | 225 } |
228 | 226 |
229 template <> | 227 template <> |
230 class ObjectTraits<sample::internal::Service_Frobinate_Params> { | 228 class ObjectTraits<sample::Service_Frobinate_Params> { |
231 public: | 229 public: |
232 static void EncodePointersAndHandles( | 230 static void EncodePointersAndHandles( |
233 sample::internal::Service_Frobinate_Params* params, | 231 sample::Service_Frobinate_Params* params, |
234 std::vector<mojo::Handle>* handles); | 232 std::vector<Handle>* handles) { |
235 static bool DecodePointersAndHandles( | 233 Encode(¶ms->foo_, handles); |
236 sample::internal::Service_Frobinate_Params* params, | 234 EncodeHandle(¶ms->port_, handles); |
237 const mojo::Message& message); | |
238 }; | |
239 | |
240 // static | |
241 void ObjectTraits<sample::internal::Service_Frobinate_Params>:: | |
242 EncodePointersAndHandles( | |
243 sample::internal::Service_Frobinate_Params* params, | |
244 std::vector<mojo::Handle>* handles) { | |
245 Encode(¶ms->foo_, handles); | |
246 EncodeHandle(¶ms->port_, handles); | |
247 } | |
248 | |
249 // static | |
250 bool ObjectTraits<sample::internal::Service_Frobinate_Params>:: | |
251 DecodePointersAndHandles( | |
252 sample::internal::Service_Frobinate_Params* params, | |
253 const mojo::Message& message) { | |
254 if (!Decode(¶ms->foo_, message)) | |
255 return false; | |
256 if (params->_header_.num_fields >= 3) { | |
257 if (!DecodeHandle(¶ms->port_, message.handles)) | |
258 return false; | |
259 } | 235 } |
260 | 236 |
261 // TODO: validate | 237 static bool DecodePointersAndHandles( |
262 return true; | 238 sample::Service_Frobinate_Params* params, |
263 } | 239 const Message& message){ |
| 240 if (!Decode(¶ms->foo_, message)) |
| 241 return false; |
| 242 if (params->_header_.num_fields >= 3) { |
| 243 if (!DecodeHandle(¶ms->port_, message.handles)) |
| 244 return false; |
| 245 } |
| 246 |
| 247 // TODO: validate |
| 248 return true; |
| 249 } |
| 250 }; |
264 | 251 |
265 } // namespace internal | 252 } // namespace internal |
266 } // namespace mojo | 253 } // namespace mojo |
OLD | NEW |