| OLD | NEW |
| (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 | |
| 54 const uint32_t kServiceClient_DidFrobinate_Name = 0; | |
| 55 | |
| 56 class ServiceClient_DidFrobinate_Params { | |
| 57 public: | |
| 58 static ServiceClient_DidFrobinate_Params* New(mojo::Buffer* buf) { | |
| 59 return new (buf->Allocate(sizeof(ServiceClient_DidFrobinate_Params))) | |
| 60 ServiceClient_DidFrobinate_Params(); | |
| 61 } | |
| 62 | |
| 63 void set_result(int32_t result) { result_ = result; } | |
| 64 | |
| 65 int32_t result() const { return result_; } | |
| 66 | |
| 67 private: | |
| 68 friend class mojo::internal::ObjectTraits<ServiceClient_DidFrobinate_Params>; | |
| 69 | |
| 70 ServiceClient_DidFrobinate_Params() { | |
| 71 _header_.num_bytes = sizeof(*this); | |
| 72 _header_.num_fields = 3; | |
| 73 } | |
| 74 | |
| 75 mojo::internal::StructHeader _header_; | |
| 76 int32_t result_; | |
| 77 uint8_t _pad0_[4]; | |
| 78 }; | |
| 79 MOJO_COMPILE_ASSERT(sizeof(ServiceClient_DidFrobinate_Params) == 16, | |
| 80 bad_sizeof_ServiceClient_DidFrobinate_Params); | |
| 81 | |
| 82 #if defined(__clang__) | |
| 83 #pragma clang diagnostic pop | |
| 84 #endif | |
| 85 #pragma pack(pop) | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 // static | |
| 90 Bar* Bar::New(mojo::Buffer* buf) { | |
| 91 return new (buf->Allocate(sizeof(Bar))) Bar(); | |
| 92 } | |
| 93 | |
| 94 Bar::Bar() { | |
| 95 _header_.num_bytes = sizeof(*this); | |
| 96 _header_.num_fields = 3; | |
| 97 } | |
| 98 | |
| 99 // static | |
| 100 Foo* Foo::New(mojo::Buffer* buf) { | |
| 101 return new (buf->Allocate(sizeof(Foo))) Foo(); | |
| 102 } | |
| 103 | |
| 104 Foo::Foo() { | |
| 105 _header_.num_bytes = sizeof(*this); | |
| 106 _header_.num_fields = 10; | |
| 107 } | |
| 108 | |
| 109 ServiceProxy::ServiceProxy(mojo::MessageReceiver* receiver) | |
| 110 : receiver_(receiver) { | |
| 111 } | |
| 112 | |
| 113 void ServiceProxy::Frobinate(const Foo* foo, bool baz, mojo::Handle port) { | |
| 114 size_t payload_size = | |
| 115 mojo::internal::Align(sizeof(Service_Frobinate_Params)); | |
| 116 payload_size += mojo::internal::ComputeSizeOf(foo); | |
| 117 | |
| 118 mojo::MessageBuilder builder(kService_Frobinate_Name, payload_size); | |
| 119 | |
| 120 // We now go about allocating the anonymous Frobinate_Params struct. It | |
| 121 // holds the parameters to the Frobinate message. | |
| 122 // | |
| 123 // Notice how foo is cloned. This causes a copy of foo to be generated | |
| 124 // within the same buffer as the Frobinate_Params struct. That's what we | |
| 125 // need in order to generate a contiguous blob of message data. | |
| 126 | |
| 127 Service_Frobinate_Params* params = | |
| 128 Service_Frobinate_Params::New(builder.buffer()); | |
| 129 params->set_foo(mojo::internal::Clone(foo, builder.buffer())); | |
| 130 params->set_baz(baz); | |
| 131 params->set_port(port); | |
| 132 | |
| 133 // NOTE: If foo happened to be a graph with cycles, then Clone would not | |
| 134 // have returned. | |
| 135 | |
| 136 // Next step is to encode pointers and handles so that messages become | |
| 137 // hermetic. Pointers become offsets and handles becomes indices into the | |
| 138 // handles array. | |
| 139 mojo::Message message; | |
| 140 mojo::internal::EncodePointersAndHandles(params, &message.handles); | |
| 141 | |
| 142 // Finally, we get the generated message data, and forward it to the | |
| 143 // receiver. | |
| 144 message.data = builder.Finish(); | |
| 145 | |
| 146 receiver_->Accept(&message); | |
| 147 } | |
| 148 | |
| 149 bool ServiceStub::Accept(mojo::Message* message) { | |
| 150 switch (message->data->header.name) { | |
| 151 case kService_Frobinate_Name: { | |
| 152 Service_Frobinate_Params* params = | |
| 153 reinterpret_cast<Service_Frobinate_Params*>( | |
| 154 message->data->payload); | |
| 155 | |
| 156 if (!mojo::internal::DecodePointersAndHandles(params, *message)) | |
| 157 return false; | |
| 158 | |
| 159 Frobinate(params->foo(), params->baz(), params->port()); | |
| 160 break; | |
| 161 } | |
| 162 } | |
| 163 return true; | |
| 164 } | |
| 165 | |
| 166 ServiceClientProxy::ServiceClientProxy(mojo::MessageReceiver* receiver) | |
| 167 : receiver_(receiver) { | |
| 168 } | |
| 169 | |
| 170 void ServiceClientProxy::DidFrobinate(int32_t result) { | |
| 171 size_t payload_size = | |
| 172 mojo::internal::Align(sizeof(ServiceClient_DidFrobinate_Params)); | |
| 173 | |
| 174 | |
| 175 mojo::MessageBuilder builder(kServiceClient_DidFrobinate_Name, payload_size); | |
| 176 | |
| 177 ServiceClient_DidFrobinate_Params* params = | |
| 178 ServiceClient_DidFrobinate_Params::New(builder.buffer()); | |
| 179 | |
| 180 params->set_result(result); | |
| 181 | |
| 182 mojo::Message message; | |
| 183 mojo::internal::EncodePointersAndHandles(params, &message.handles); | |
| 184 | |
| 185 message.data = builder.Finish(); | |
| 186 | |
| 187 receiver_->Accept(&message); | |
| 188 } | |
| 189 | |
| 190 | |
| 191 bool ServiceClientStub::Accept(mojo::Message* message) { | |
| 192 switch (message->data->header.name) { | |
| 193 case kServiceClient_DidFrobinate_Name: { | |
| 194 ServiceClient_DidFrobinate_Params* params = | |
| 195 reinterpret_cast<ServiceClient_DidFrobinate_Params*>( | |
| 196 message->data->payload); | |
| 197 | |
| 198 if (!mojo::internal::DecodePointersAndHandles(params, *message)) | |
| 199 return false; | |
| 200 DidFrobinate(params->result()); | |
| 201 break; | |
| 202 } | |
| 203 | |
| 204 } | |
| 205 return true; | |
| 206 } | |
| 207 | |
| 208 } // namespace sample | |
| 209 | |
| 210 namespace mojo { | |
| 211 namespace internal { | |
| 212 | |
| 213 // static | |
| 214 size_t ObjectTraits<sample::Bar>::ComputeSizeOf( | |
| 215 const sample::Bar* bar) { | |
| 216 return sizeof(*bar); | |
| 217 } | |
| 218 | |
| 219 // static | |
| 220 sample::Bar* ObjectTraits<sample::Bar>::Clone( | |
| 221 const sample::Bar* bar, Buffer* buf) { | |
| 222 sample::Bar* clone = sample::Bar::New(buf); | |
| 223 memcpy(clone, bar, sizeof(*bar)); | |
| 224 return clone; | |
| 225 } | |
| 226 | |
| 227 // static | |
| 228 void ObjectTraits<sample::Bar>::EncodePointersAndHandles( | |
| 229 sample::Bar* bar, std::vector<Handle>* handles) { | |
| 230 } | |
| 231 | |
| 232 // static | |
| 233 bool ObjectTraits<sample::Bar>::DecodePointersAndHandles( | |
| 234 sample::Bar* bar, const Message& message) { | |
| 235 return true; | |
| 236 } | |
| 237 | |
| 238 // static | |
| 239 size_t ObjectTraits<sample::Foo>::ComputeSizeOf( | |
| 240 const sample::Foo* foo) { | |
| 241 return sizeof(*foo) + | |
| 242 mojo::internal::ComputeSizeOf(foo->bar()) + | |
| 243 mojo::internal::ComputeSizeOf(foo->data()) + | |
| 244 mojo::internal::ComputeSizeOf(foo->extra_bars()) + | |
| 245 mojo::internal::ComputeSizeOf(foo->name()) + | |
| 246 mojo::internal::ComputeSizeOf(foo->files()); | |
| 247 } | |
| 248 | |
| 249 // static | |
| 250 sample::Foo* ObjectTraits<sample::Foo>::Clone( | |
| 251 const sample::Foo* foo, Buffer* buf) { | |
| 252 sample::Foo* clone = sample::Foo::New(buf); | |
| 253 memcpy(clone, foo, sizeof(*foo)); | |
| 254 | |
| 255 clone->set_bar(mojo::internal::Clone(foo->bar(), buf)); | |
| 256 clone->set_data(mojo::internal::Clone(foo->data(), buf)); | |
| 257 clone->set_extra_bars(mojo::internal::Clone(foo->extra_bars(), buf)); | |
| 258 clone->set_name(mojo::internal::Clone(foo->name(), buf)); | |
| 259 clone->set_files(mojo::internal::Clone(foo->files(), buf)); | |
| 260 | |
| 261 return clone; | |
| 262 } | |
| 263 | |
| 264 // static | |
| 265 void ObjectTraits<sample::Foo>::EncodePointersAndHandles( | |
| 266 sample::Foo* foo, std::vector<Handle>* handles) { | |
| 267 Encode(&foo->bar_, handles); | |
| 268 Encode(&foo->data_, handles); | |
| 269 Encode(&foo->extra_bars_, handles); | |
| 270 Encode(&foo->name_, handles); | |
| 271 Encode(&foo->files_, handles); | |
| 272 } | |
| 273 | |
| 274 // static | |
| 275 bool ObjectTraits<sample::Foo>::DecodePointersAndHandles( | |
| 276 sample::Foo* foo, const Message& message) { | |
| 277 if (!Decode(&foo->bar_, message)) | |
| 278 return false; | |
| 279 if (!Decode(&foo->data_, message)) | |
| 280 return false; | |
| 281 if (foo->_header_.num_fields >= 8) { | |
| 282 if (!Decode(&foo->extra_bars_, message)) | |
| 283 return false; | |
| 284 } | |
| 285 if (foo->_header_.num_fields >= 9) { | |
| 286 if (!Decode(&foo->name_, message)) | |
| 287 return false; | |
| 288 } | |
| 289 if (foo->_header_.num_fields >= 10) { | |
| 290 if (!Decode(&foo->files_, message)) | |
| 291 return false; | |
| 292 } | |
| 293 | |
| 294 // TODO: validate | |
| 295 return true; | |
| 296 } | |
| 297 | |
| 298 template <> | |
| 299 class ObjectTraits<sample::Service_Frobinate_Params> { | |
| 300 public: | |
| 301 static void EncodePointersAndHandles( | |
| 302 sample::Service_Frobinate_Params* params, | |
| 303 std::vector<Handle>* handles) { | |
| 304 Encode(¶ms->foo_, handles); | |
| 305 EncodeHandle(¶ms->port_, handles); | |
| 306 } | |
| 307 | |
| 308 static bool DecodePointersAndHandles( | |
| 309 sample::Service_Frobinate_Params* params, | |
| 310 const Message& message){ | |
| 311 if (!Decode(¶ms->foo_, message)) | |
| 312 return false; | |
| 313 if (params->_header_.num_fields >= 3) { | |
| 314 if (!DecodeHandle(¶ms->port_, message.handles)) | |
| 315 return false; | |
| 316 } | |
| 317 | |
| 318 // TODO: validate | |
| 319 return true; | |
| 320 } | |
| 321 }; | |
| 322 | |
| 323 template <> | |
| 324 class ObjectTraits<sample::ServiceClient_DidFrobinate_Params> { | |
| 325 public: | |
| 326 static void EncodePointersAndHandles( | |
| 327 sample::ServiceClient_DidFrobinate_Params* params, | |
| 328 std::vector<Handle>* handles) { | |
| 329 } | |
| 330 | |
| 331 static bool DecodePointersAndHandles( | |
| 332 sample::ServiceClient_DidFrobinate_Params* params, | |
| 333 const Message& message) { | |
| 334 return true; | |
| 335 } | |
| 336 }; | |
| 337 | |
| 338 } // namespace internal | |
| 339 } // namespace mojo | |
| OLD | NEW |