Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: mojo/public/bindings/sample/generated/sample_service.cc

Issue 60803002: Simpler bindings, fewer files! (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move more to the .cc file Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/public/bindings/sample/generated/sample_service.h"
6
7 #include "mojo/public/bindings/lib/message_builder.h"
8 #include "mojo/public/bindings/sample/generated/sample_service_internal.h"
9
10 namespace sample {
11 namespace internal {
12
13 const uint32_t kService_Frobinate_Name = 1;
14
15 #pragma pack(push, 1)
16
17 class Service_Frobinate_Params {
18 public:
19 static Service_Frobinate_Params* New(mojo::Buffer* buf);
20
21 void set_foo(Foo* foo) { foo_.ptr = foo; }
22 void set_baz(bool baz) { baz_ = baz; }
23 void set_port(mojo::Handle port) { port_ = port; }
24
25 const Foo* foo() const { return foo_.ptr; }
26 bool baz() const { return baz_; }
27 mojo::Handle port() const {
28 // NOTE: port is an optional field!
29 return _header_.num_fields >= 3 ? port_ : mojo::kInvalidHandle;
30 }
31
32 private:
33 friend class mojo::internal::ObjectTraits<Service_Frobinate_Params>;
34
35 Service_Frobinate_Params();
36 ~Service_Frobinate_Params(); // NOT IMPLEMENTED
37
38 mojo::internal::StructHeader _header_;
39 mojo::internal::StructPointer<Foo> foo_;
40 uint8_t baz_ : 1;
41 uint8_t _pad0_[3];
42 mojo::Handle port_;
43 };
44 MOJO_COMPILE_ASSERT(sizeof(Service_Frobinate_Params) == 24,
45 bad_sizeof_Service_Frobinate_Params);
46
47 #pragma pack(pop)
48
49 // static
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
62 // static
63 Bar* Bar::New(mojo::Buffer* buf) {
64 return new (buf->Allocate(sizeof(Bar))) Bar();
65 }
66
67 Bar::Bar() {
68 _header_.num_bytes = sizeof(*this);
69 _header_.num_fields = 3;
70 }
71
72 // static
73 Foo* Foo::New(mojo::Buffer* buf) {
74 return new (buf->Allocate(sizeof(Foo))) Foo();
75 }
76
77 Foo::Foo() {
78 _header_.num_bytes = sizeof(*this);
79 _header_.num_fields = 10;
80 }
81
82 ServiceProxy::ServiceProxy(mojo::MessageReceiver* receiver)
83 : receiver_(receiver) {
84 }
85
86 void ServiceProxy::Frobinate(const Foo* foo, bool baz, mojo::Handle port) {
87 size_t payload_size =
88 mojo::internal::Align(sizeof(internal::Service_Frobinate_Params));
89 payload_size += mojo::internal::ComputeSizeOf(foo);
90
91 mojo::MessageBuilder builder(internal::kService_Frobinate_Name, payload_size);
92
93 // We now go about allocating the anonymous Frobinate_Params struct. It
94 // holds the parameters to the Frobinate message.
95 //
96 // 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
98 // need in order to generate a contiguous blob of message data.
99
100 internal::Service_Frobinate_Params* params =
101 internal::Service_Frobinate_Params::New(builder.buffer());
102 params->set_foo(mojo::internal::Clone(foo, builder.buffer()));
103 params->set_baz(baz);
104 params->set_port(port);
105
106 // NOTE: If foo happened to be a graph with cycles, then Clone would not
107 // have returned.
108
109 // Next step is to encode pointers and handles so that messages become
110 // hermetic. Pointers become offsets and handles becomes indices into the
111 // handles array.
112 mojo::Message message;
113 mojo::internal::EncodePointersAndHandles(params, &message.handles);
114
115 // Finally, we get the generated message data, and forward it to the
116 // receiver.
117 message.data = builder.Finish();
118
119 receiver_->Accept(&message);
120 }
121
122 bool ServiceStub::Accept(mojo::Message* message) {
123 switch (message->data->header.name) {
124 case internal::kService_Frobinate_Name: {
125 internal::Service_Frobinate_Params* params =
126 reinterpret_cast<internal::Service_Frobinate_Params*>(
127 message->data->payload);
128
129 if (!mojo::internal::DecodePointersAndHandles(params, *message))
130 return false;
131
132 Frobinate(params->foo(), params->baz(), params->port());
133 break;
134 }
135 }
136 return true;
137 }
138
139 } // namespace sample
140
141 namespace mojo {
142 namespace internal {
143
144 // static
145 size_t ObjectTraits<sample::Bar>::ComputeSizeOf(
146 const sample::Bar* bar) {
147 return sizeof(*bar);
148 }
149
150 // static
151 sample::Bar* ObjectTraits<sample::Bar>::Clone(
152 const sample::Bar* bar, Buffer* buf) {
153 sample::Bar* clone = sample::Bar::New(buf);
154 memcpy(clone, bar, sizeof(*bar));
155 return clone;
156 }
157
158 // static
159 void ObjectTraits<sample::Bar>::EncodePointersAndHandles(
160 sample::Bar* bar, std::vector<mojo::Handle>* handles) {
161 }
162
163 // static
164 bool ObjectTraits<sample::Bar>::DecodePointersAndHandles(
165 sample::Bar* bar, const mojo::Message& message) {
166 return true;
167 }
168
169 // static
170 size_t ObjectTraits<sample::Foo>::ComputeSizeOf(
171 const sample::Foo* foo) {
172 return sizeof(*foo) +
173 mojo::internal::ComputeSizeOf(foo->bar()) +
174 mojo::internal::ComputeSizeOf(foo->data()) +
175 mojo::internal::ComputeSizeOf(foo->extra_bars()) +
176 mojo::internal::ComputeSizeOf(foo->name()) +
177 mojo::internal::ComputeSizeOf(foo->files());
178 }
179
180 // static
181 sample::Foo* ObjectTraits<sample::Foo>::Clone(
182 const sample::Foo* foo, Buffer* buf) {
183 sample::Foo* clone = sample::Foo::New(buf);
184 memcpy(clone, foo, sizeof(*foo));
185
186 clone->set_bar(mojo::internal::Clone(foo->bar(), buf));
187 clone->set_data(mojo::internal::Clone(foo->data(), buf));
188 clone->set_extra_bars(mojo::internal::Clone(foo->extra_bars(), buf));
189 clone->set_name(mojo::internal::Clone(foo->name(), buf));
190 clone->set_files(mojo::internal::Clone(foo->files(), buf));
191
192 return clone;
193 }
194
195 // static
196 void ObjectTraits<sample::Foo>::EncodePointersAndHandles(
197 sample::Foo* foo, std::vector<mojo::Handle>* handles) {
198 Encode(&foo->bar_, handles);
199 Encode(&foo->data_, handles);
200 Encode(&foo->extra_bars_, handles);
201 Encode(&foo->name_, handles);
202 Encode(&foo->files_, handles);
203 }
204
205 // static
206 bool ObjectTraits<sample::Foo>::DecodePointersAndHandles(
207 sample::Foo* foo, const mojo::Message& message) {
208 if (!Decode(&foo->bar_, message))
209 return false;
210 if (!Decode(&foo->data_, message))
211 return false;
212 if (foo->_header_.num_fields >= 8) {
213 if (!Decode(&foo->extra_bars_, message))
214 return false;
215 }
216 if (foo->_header_.num_fields >= 9) {
217 if (!Decode(&foo->name_, message))
218 return false;
219 }
220 if (foo->_header_.num_fields >= 10) {
221 if (!Decode(&foo->files_, message))
222 return false;
223 }
224
225 // TODO: validate
226 return true;
227 }
228
229 template <>
230 class ObjectTraits<sample::internal::Service_Frobinate_Params> {
231 public:
232 static void EncodePointersAndHandles(
233 sample::internal::Service_Frobinate_Params* params,
234 std::vector<mojo::Handle>* handles);
235 static bool DecodePointersAndHandles(
236 sample::internal::Service_Frobinate_Params* params,
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(&params->foo_, handles);
246 EncodeHandle(&params->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(&params->foo_, message))
255 return false;
256 if (params->_header_.num_fields >= 3) {
257 if (!DecodeHandle(&params->port_, message.handles))
258 return false;
259 }
260
261 // TODO: validate
262 return true;
263 }
264
265 } // namespace internal
266 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698