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

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

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

Powered by Google App Engine
This is Rietveld 408576698