diff options
Diffstat (limited to 'mojo/public/bindings/sample/mojom')
-rw-r--r-- | mojo/public/bindings/sample/mojom/sample_service.cc | 253 | ||||
-rw-r--r-- | mojo/public/bindings/sample/mojom/sample_service.h | 132 | ||||
-rw-r--r-- | mojo/public/bindings/sample/mojom/sample_service_internal.h | 41 |
3 files changed, 426 insertions, 0 deletions
diff --git a/mojo/public/bindings/sample/mojom/sample_service.cc b/mojo/public/bindings/sample/mojom/sample_service.cc new file mode 100644 index 0000000..877b532 --- /dev/null +++ b/mojo/public/bindings/sample/mojom/sample_service.cc @@ -0,0 +1,253 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "mojom/sample_service.h" + +#include "mojo/public/bindings/lib/message_builder.h" +#include "mojom/sample_service_internal.h" + +namespace sample { +namespace { +const uint32_t kService_Frobinate_Name = 1; + +#pragma pack(push, 1) +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-private-field" +#endif +class Service_Frobinate_Params { + public: + static Service_Frobinate_Params* New(mojo::Buffer* buf) { + return new (buf->Allocate(sizeof(Service_Frobinate_Params))) + Service_Frobinate_Params(); + } + + void set_foo(Foo* foo) { foo_.ptr = foo; } + void set_baz(bool baz) { baz_ = baz; } + void set_port(mojo::Handle port) { port_ = port; } + + const Foo* foo() const { return foo_.ptr; } + bool baz() const { return baz_; } + mojo::Handle port() const { + // NOTE: port is an optional field! + return _header_.num_fields >= 3 ? port_ : mojo::kInvalidHandle; + } + + private: + friend class mojo::internal::ObjectTraits<Service_Frobinate_Params>; + + Service_Frobinate_Params() { + _header_.num_bytes = sizeof(*this); + _header_.num_fields = 3; + } + + mojo::internal::StructHeader _header_; + mojo::internal::StructPointer<Foo> foo_; + uint8_t baz_ : 1; + uint8_t _pad0_[3]; + mojo::Handle port_; +}; +MOJO_COMPILE_ASSERT(sizeof(Service_Frobinate_Params) == 24, + bad_sizeof_Service_Frobinate_Params); +#if defined(__clang__) +#pragma clang diagnostic pop +#endif +#pragma pack(pop) + +} // namespace + +// static +Bar* Bar::New(mojo::Buffer* buf) { + return new (buf->Allocate(sizeof(Bar))) Bar(); +} + +Bar::Bar() { + _header_.num_bytes = sizeof(*this); + _header_.num_fields = 3; +} + +// static +Foo* Foo::New(mojo::Buffer* buf) { + return new (buf->Allocate(sizeof(Foo))) Foo(); +} + +Foo::Foo() { + _header_.num_bytes = sizeof(*this); + _header_.num_fields = 10; +} + +ServiceProxy::ServiceProxy(mojo::MessageReceiver* receiver) + : receiver_(receiver) { +} + +void ServiceProxy::Frobinate(const Foo* foo, bool baz, mojo::Handle port) { + size_t payload_size = + mojo::internal::Align(sizeof(Service_Frobinate_Params)); + payload_size += mojo::internal::ComputeSizeOf(foo); + + mojo::MessageBuilder builder(kService_Frobinate_Name, payload_size); + + // We now go about allocating the anonymous Frobinate_Params struct. It + // holds the parameters to the Frobinate message. + // + // Notice how foo is cloned. This causes a copy of foo to be generated + // within the same buffer as the Frobinate_Params struct. That's what we + // need in order to generate a contiguous blob of message data. + + Service_Frobinate_Params* params = + Service_Frobinate_Params::New(builder.buffer()); + params->set_foo(mojo::internal::Clone(foo, builder.buffer())); + params->set_baz(baz); + params->set_port(port); + + // NOTE: If foo happened to be a graph with cycles, then Clone would not + // have returned. + + // Next step is to encode pointers and handles so that messages become + // hermetic. Pointers become offsets and handles becomes indices into the + // handles array. + mojo::Message message; + mojo::internal::EncodePointersAndHandles(params, &message.handles); + + // Finally, we get the generated message data, and forward it to the + // receiver. + message.data = builder.Finish(); + + receiver_->Accept(&message); +} + +bool ServiceStub::Accept(mojo::Message* message) { + switch (message->data->header.name) { + case kService_Frobinate_Name: { + Service_Frobinate_Params* params = + reinterpret_cast<Service_Frobinate_Params*>( + message->data->payload); + + if (!mojo::internal::DecodePointersAndHandles(params, *message)) + return false; + + Frobinate(params->foo(), params->baz(), params->port()); + break; + } + } + return true; +} + +} // namespace sample + +namespace mojo { +namespace internal { + +// static +size_t ObjectTraits<sample::Bar>::ComputeSizeOf( + const sample::Bar* bar) { + return sizeof(*bar); +} + +// static +sample::Bar* ObjectTraits<sample::Bar>::Clone( + const sample::Bar* bar, Buffer* buf) { + sample::Bar* clone = sample::Bar::New(buf); + memcpy(clone, bar, sizeof(*bar)); + return clone; +} + +// static +void ObjectTraits<sample::Bar>::EncodePointersAndHandles( + sample::Bar* bar, std::vector<Handle>* handles) { +} + +// static +bool ObjectTraits<sample::Bar>::DecodePointersAndHandles( + sample::Bar* bar, const Message& message) { + return true; +} + +// static +size_t ObjectTraits<sample::Foo>::ComputeSizeOf( + const sample::Foo* foo) { + return sizeof(*foo) + + mojo::internal::ComputeSizeOf(foo->bar()) + + mojo::internal::ComputeSizeOf(foo->data()) + + mojo::internal::ComputeSizeOf(foo->extra_bars()) + + mojo::internal::ComputeSizeOf(foo->name()) + + mojo::internal::ComputeSizeOf(foo->files()); +} + +// static +sample::Foo* ObjectTraits<sample::Foo>::Clone( + const sample::Foo* foo, Buffer* buf) { + sample::Foo* clone = sample::Foo::New(buf); + memcpy(clone, foo, sizeof(*foo)); + + clone->set_bar(mojo::internal::Clone(foo->bar(), buf)); + clone->set_data(mojo::internal::Clone(foo->data(), buf)); + clone->set_extra_bars(mojo::internal::Clone(foo->extra_bars(), buf)); + clone->set_name(mojo::internal::Clone(foo->name(), buf)); + clone->set_files(mojo::internal::Clone(foo->files(), buf)); + + return clone; +} + +// static +void ObjectTraits<sample::Foo>::EncodePointersAndHandles( + sample::Foo* foo, std::vector<Handle>* handles) { + Encode(&foo->bar_, handles); + Encode(&foo->data_, handles); + Encode(&foo->extra_bars_, handles); + Encode(&foo->name_, handles); + Encode(&foo->files_, handles); +} + +// static +bool ObjectTraits<sample::Foo>::DecodePointersAndHandles( + sample::Foo* foo, const Message& message) { + if (!Decode(&foo->bar_, message)) + return false; + if (!Decode(&foo->data_, message)) + return false; + if (foo->_header_.num_fields >= 8) { + if (!Decode(&foo->extra_bars_, message)) + return false; + } + if (foo->_header_.num_fields >= 9) { + if (!Decode(&foo->name_, message)) + return false; + } + if (foo->_header_.num_fields >= 10) { + if (!Decode(&foo->files_, message)) + return false; + } + + // TODO: validate + return true; +} + +template <> +class ObjectTraits<sample::Service_Frobinate_Params> { + public: + static void EncodePointersAndHandles( + sample::Service_Frobinate_Params* params, + std::vector<Handle>* handles) { + Encode(¶ms->foo_, handles); + EncodeHandle(¶ms->port_, handles); + } + + static bool DecodePointersAndHandles( + sample::Service_Frobinate_Params* params, + const Message& message){ + if (!Decode(¶ms->foo_, message)) + return false; + if (params->_header_.num_fields >= 3) { + if (!DecodeHandle(¶ms->port_, message.handles)) + return false; + } + + // TODO: validate + return true; + } +}; + +} // namespace internal +} // namespace mojo diff --git a/mojo/public/bindings/sample/mojom/sample_service.h b/mojo/public/bindings/sample/mojom/sample_service.h new file mode 100644 index 0000000..9a77aa0 --- /dev/null +++ b/mojo/public/bindings/sample/mojom/sample_service.h @@ -0,0 +1,132 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_H_ +#define MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_H_ + +#include "mojo/public/bindings/lib/bindings.h" +#include "mojo/public/bindings/lib/message.h" + +namespace sample { + +#pragma pack(push, 1) + +class Bar { + public: + static Bar* New(mojo::Buffer* buf); + + void set_alpha(uint8_t alpha) { alpha_ = alpha; } + void set_beta(uint8_t beta) { beta_ = beta; } + void set_gamma(uint8_t gamma) { gamma_ = gamma; } + + uint8_t alpha() const { return alpha_; } + uint8_t beta() const { return beta_; } + uint8_t gamma() const { return gamma_; } + + private: + friend class mojo::internal::ObjectTraits<Bar>; + + Bar(); + ~Bar(); // NOT IMPLEMENTED + + mojo::internal::StructHeader _header_; + uint8_t alpha_; + uint8_t beta_; + uint8_t gamma_; + uint8_t _pad0_[5]; +}; +MOJO_COMPILE_ASSERT(sizeof(Bar) == 16, bad_sizeof_Bar); + +#pragma pack(pop) + +#pragma pack(push, 1) + +class Foo { + public: + static Foo* New(mojo::Buffer* buf); + + void set_x(int32_t x) { x_ = x; } + void set_y(int32_t y) { y_ = y; } + void set_a(bool a) { a_ = a; } + void set_b(bool b) { b_ = b; } + void set_c(bool c) { c_ = c; } + void set_bar(Bar* bar) { bar_.ptr = bar; } + void set_data(mojo::Array<uint8_t>* data) { data_.ptr = data; } + void set_extra_bars(mojo::Array<Bar*>* extra_bars) { + extra_bars_.ptr = extra_bars; + } + void set_name(mojo::String* name) { + name_.ptr = name; + } + void set_files(mojo::Array<mojo::Handle>* files) { + files_.ptr = files; + } + + int32_t x() const { return x_; } + int32_t y() const { return y_; } + bool a() const { return a_; } + bool b() const { return b_; } + bool c() const { return c_; } + const Bar* bar() const { return bar_.ptr; } + const mojo::Array<uint8_t>* data() const { return data_.ptr; } + const mojo::Array<Bar*>* extra_bars() const { + // NOTE: extra_bars is an optional field! + return _header_.num_fields >= 8 ? extra_bars_.ptr : NULL; + } + const mojo::String* name() const { + // NOTE: name is also an optional field! + return _header_.num_fields >= 9 ? name_.ptr : NULL; + } + const mojo::Array<mojo::Handle>* files() const { + // NOTE: files is also an optional field! + return _header_.num_fields >= 10 ? files_.ptr : NULL; + } + + private: + friend class mojo::internal::ObjectTraits<Foo>; + + Foo(); + ~Foo(); // NOT IMPLEMENTED + + mojo::internal::StructHeader _header_; + int32_t x_; + int32_t y_; + uint8_t a_ : 1; + uint8_t b_ : 1; + uint8_t c_ : 1; + uint8_t _pad0_[7]; + mojo::internal::StructPointer<Bar> bar_; + mojo::internal::ArrayPointer<uint8_t> data_; + mojo::internal::ArrayPointer<Bar*> extra_bars_; + mojo::internal::StringPointer name_; + mojo::internal::ArrayPointer<mojo::Handle> files_; +}; +MOJO_COMPILE_ASSERT(sizeof(Foo) == 64, bad_sizeof_Foo); + +#pragma pack(pop) + +class Service { + public: + virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port) = 0; +}; + +class ServiceProxy : public Service { + public: + explicit ServiceProxy(mojo::MessageReceiver* receiver); + + virtual void Frobinate(const Foo* Foo, bool baz, mojo::Handle port) + MOJO_OVERRIDE; + + private: + mojo::MessageReceiver* receiver_; +}; + +class ServiceStub : public Service, public mojo::MessageReceiver { + public: + virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE; +}; + +} // namespace sample + +#endif // MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_H_ diff --git a/mojo/public/bindings/sample/mojom/sample_service_internal.h b/mojo/public/bindings/sample/mojom/sample_service_internal.h new file mode 100644 index 0000000..93bb78d --- /dev/null +++ b/mojo/public/bindings/sample/mojom/sample_service_internal.h @@ -0,0 +1,41 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_SERIALIZATION_H_ +#define MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_SERIALIZATION_H_ + +#include <string.h> + +#include "mojo/public/bindings/lib/bindings_serialization.h" +#include "mojom/sample_service.h" + +namespace mojo { +namespace internal { + +template <> +class ObjectTraits<sample::Bar> { + public: + static size_t ComputeSizeOf(const sample::Bar* bar); + static sample::Bar* Clone(const sample::Bar* bar, Buffer* buf); + static void EncodePointersAndHandles(sample::Bar* bar, + std::vector<mojo::Handle>* handles); + static bool DecodePointersAndHandles(sample::Bar* bar, + const mojo::Message& message); +}; + +template <> +class ObjectTraits<sample::Foo> { + public: + static size_t ComputeSizeOf(const sample::Foo* foo); + static sample::Foo* Clone(const sample::Foo* foo, Buffer* buf); + static void EncodePointersAndHandles(sample::Foo* foo, + std::vector<mojo::Handle>* handles); + static bool DecodePointersAndHandles(sample::Foo* foo, + const mojo::Message& message); +}; + +} // namespace internal +} // namespace mojo + +#endif // MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_SERIALIZATION_H_ |