From 67f4c5db3b5bbe50e051e0364441e06ee0d45202 Mon Sep 17 00:00:00 2001 From: "davemoore@chromium.org" Date: Mon, 11 Nov 2013 17:30:35 +0000 Subject: Add mojo_idl_generator.gypi NOT FOR REVIEW BUG= R=darin@chromium.org Review URL: https://codereview.chromium.org/53173008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@234233 0039d316-1c4b-4281-b951-d872f2087c98 --- mojo/DEPS | 1 + mojo/mojo.gyp | 23 +- mojo/public/bindings/mojo_idl.py | 43 ---- mojo/public/bindings/mojom_bindings_generator.gypi | 46 ++++ mojo/public/bindings/mojom_bindings_generator.py | 46 ++++ .../bindings/sample/generated/sample_service.cc | 253 --------------------- .../bindings/sample/generated/sample_service.h | 132 ----------- .../sample/generated/sample_service_internal.h | 41 ---- .../public/bindings/sample/mojom/sample_service.cc | 253 +++++++++++++++++++++ mojo/public/bindings/sample/mojom/sample_service.h | 132 +++++++++++ .../sample/mojom/sample_service_internal.h | 41 ++++ mojo/public/bindings/sample/sample_service.idl | 33 --- mojo/public/bindings/sample/sample_service.mojom | 62 ++--- mojo/public/bindings/sample/sample_test.cc | 2 +- 14 files changed, 571 insertions(+), 537 deletions(-) delete mode 100755 mojo/public/bindings/mojo_idl.py create mode 100644 mojo/public/bindings/mojom_bindings_generator.gypi create mode 100755 mojo/public/bindings/mojom_bindings_generator.py delete mode 100644 mojo/public/bindings/sample/generated/sample_service.cc delete mode 100644 mojo/public/bindings/sample/generated/sample_service.h delete mode 100644 mojo/public/bindings/sample/generated/sample_service_internal.h create mode 100644 mojo/public/bindings/sample/mojom/sample_service.cc create mode 100644 mojo/public/bindings/sample/mojom/sample_service.h create mode 100644 mojo/public/bindings/sample/mojom/sample_service_internal.h delete mode 100644 mojo/public/bindings/sample/sample_service.idl (limited to 'mojo') diff --git a/mojo/DEPS b/mojo/DEPS index 411bead..1bdd152 100644 --- a/mojo/DEPS +++ b/mojo/DEPS @@ -2,5 +2,6 @@ include_rules = [ "+base", "+build", "+mojo", + "+mojom", "+testing", ] diff --git a/mojo/mojo.gyp b/mojo/mojo.gyp index 969669f..7f07480 100644 --- a/mojo/mojo.gyp +++ b/mojo/mojo.gyp @@ -24,6 +24,7 @@ 'mojo_js', 'sample_app', 'mojo_bindings', + 'mojom_test', 'mojo_bindings_test', 'native_viewport', ], @@ -323,19 +324,33 @@ ], }, { + 'target_name': 'mojom_test', + 'type': 'executable', + 'sources': [ + 'public/bindings/sample/sample_test.cc', + 'public/bindings/sample/sample_service.mojom', + ], + 'includes': [ 'public/bindings/mojom_bindings_generator.gypi' ], + 'dependencies': [ + 'mojo_bindings', + 'mojo_system', + ], + }, + { 'target_name': 'mojo_bindings_test', 'type': 'executable', 'include_dirs': [ - '..' + '..', + '<(DEPTH)/mojo/public/bindings/sample', ], 'dependencies': [ 'mojo_bindings', 'mojo_system', ], 'sources': [ - 'public/bindings/sample/generated/sample_service.cc', - 'public/bindings/sample/generated/sample_service.h', - 'public/bindings/sample/generated/sample_service_internal.h', + 'public/bindings/sample/mojom/sample_service.cc', + 'public/bindings/sample/mojom/sample_service.h', + 'public/bindings/sample/mojom/sample_service_internal.h', 'public/bindings/sample/sample_test.cc', ], }, diff --git a/mojo/public/bindings/mojo_idl.py b/mojo/public/bindings/mojo_idl.py deleted file mode 100755 index 567adf7..0000000 --- a/mojo/public/bindings/mojo_idl.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python -# 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. - -"""The frontend for the Mojo bindings system.""" - - -import os -import sys -from optparse import OptionParser -from parser import mojo_parser -from parser import mojo_translate -from generators import mojom_data -from generators import mojom_cpp_generator - - -def Main(): - parser = OptionParser(usage="usage: %prog [options] filename1 [filename2...]") - parser.add_option("-i", "--include_dir", dest="include_dir", default=".", - help="specify directory for #includes") - parser.add_option("-o", "--output_dir", dest="output_dir", default=".", - help="specify output directory") - (options, args) = parser.parse_args() - - if len(args) < 1: - parser.print_help() - sys.exit(1) - - for filename in args: - name = os.path.splitext(os.path.basename(filename))[0] - # TODO(darin): There's clearly too many layers of translation here! We can - # at least avoid generating the serialized Mojom IR. - tree = mojo_parser.Parse(filename) - mojom = mojo_translate.Translate(tree, name) - module = mojom_data.ModuleFromData(mojom) - cpp = mojom_cpp_generator.CPPGenerator( - module, options.include_dir, options.output_dir) - cpp.GenerateFiles() - - -if __name__ == '__main__': - Main() diff --git a/mojo/public/bindings/mojom_bindings_generator.gypi b/mojo/public/bindings/mojom_bindings_generator.gypi new file mode 100644 index 0000000..47abf00 --- /dev/null +++ b/mojo/public/bindings/mojom_bindings_generator.gypi @@ -0,0 +1,46 @@ +# Copyright (c) 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. + +{ + 'variables': { + 'output_dir': '<(SHARED_INTERMEDIATE_DIR)/mojom', + }, + 'rules': [ + { + 'rule_name': 'Generate C++ source files from mojom files', + 'extension': 'mojom', + 'variables': { + 'mojom_bindings_generator': + '<(DEPTH)/mojo/public/bindings/mojom_bindings_generator.py', + }, + 'inputs': [ + '<(mojom_bindings_generator)', + '<(DEPTH)/mojo/public/bindings/parser/mojo_parser.py', + '<(DEPTH)/mojo/public/bindings/parser/mojo_translate.py', + '<(DEPTH)/mojo/public/bindings/generators/mojom.py', + '<(DEPTH)/mojo/public/bindings/generators/mojom_data.py', + '<(DEPTH)/mojo/public/bindings/generators/mojom_pack.py', + '<(DEPTH)/mojo/public/bindings/generators/mojom_cpp_generator.py', + ], + 'outputs': [ + '<(output_dir)/<(RULE_INPUT_ROOT).h', + '<(output_dir)/<(RULE_INPUT_ROOT)_internal.h', + '<(output_dir)/<(RULE_INPUT_ROOT).cc', + ], + 'action': [ + 'python', '<@(mojom_bindings_generator)', + '<(RULE_INPUT_PATH)', + '-i', 'mojom', + '-o', '<(output_dir)', + ], + 'message': 'Generating C++ from mojom <(RULE_INPUT_PATH)', + 'process_outputs_as_sources': 1, + } + ], + 'include_dirs': [ + '<(DEPTH)', + '<(SHARED_INTERMEDIATE_DIR)', + ], + 'hard_dependency': 1, +} diff --git a/mojo/public/bindings/mojom_bindings_generator.py b/mojo/public/bindings/mojom_bindings_generator.py new file mode 100755 index 0000000..4039972 --- /dev/null +++ b/mojo/public/bindings/mojom_bindings_generator.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# 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. + +"""The frontend for the Mojo bindings system.""" + + +import os +import sys +from optparse import OptionParser +from parser import mojo_parser +from parser import mojo_translate +from generators import mojom_data +from generators import mojom_cpp_generator + + +def Main(): + parser = OptionParser(usage="usage: %prog [options] filename1 [filename2...]") + parser.add_option("-i", "--include_dir", dest="include_dir", default=".", + help="specify directory for #includes") + parser.add_option("-o", "--output_dir", dest="output_dir", default=".", + help="specify output directory") + (options, args) = parser.parse_args() + + if len(args) < 1: + parser.print_help() + sys.exit(1) + + if not os.path.exists(options.output_dir): + os.makedirs(options.output_dir) + + for filename in args: + name = os.path.splitext(os.path.basename(filename))[0] + # TODO(darin): There's clearly too many layers of translation here! We can + # at least avoid generating the serialized Mojom IR. + tree = mojo_parser.Parse(filename) + mojom = mojo_translate.Translate(tree, name) + module = mojom_data.ModuleFromData(mojom) + cpp = mojom_cpp_generator.CPPGenerator( + module, options.include_dir, options.output_dir) + cpp.GenerateFiles() + + +if __name__ == '__main__': + Main() diff --git a/mojo/public/bindings/sample/generated/sample_service.cc b/mojo/public/bindings/sample/generated/sample_service.cc deleted file mode 100644 index 9209cb9..0000000 --- a/mojo/public/bindings/sample/generated/sample_service.cc +++ /dev/null @@ -1,253 +0,0 @@ -// 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 "mojo/public/bindings/sample/generated/sample_service.h" - -#include "mojo/public/bindings/lib/message_builder.h" -#include "mojo/public/bindings/sample/generated/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() { - _header_.num_bytes = sizeof(*this); - _header_.num_fields = 3; - } - - mojo::internal::StructHeader _header_; - mojo::internal::StructPointer 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( - 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::ComputeSizeOf( - const sample::Bar* bar) { - return sizeof(*bar); -} - -// static -sample::Bar* ObjectTraits::Clone( - const sample::Bar* bar, Buffer* buf) { - sample::Bar* clone = sample::Bar::New(buf); - memcpy(clone, bar, sizeof(*bar)); - return clone; -} - -// static -void ObjectTraits::EncodePointersAndHandles( - sample::Bar* bar, std::vector* handles) { -} - -// static -bool ObjectTraits::DecodePointersAndHandles( - sample::Bar* bar, const Message& message) { - return true; -} - -// static -size_t ObjectTraits::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::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::EncodePointersAndHandles( - sample::Foo* foo, std::vector* 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::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 { - public: - static void EncodePointersAndHandles( - sample::Service_Frobinate_Params* params, - std::vector* 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/generated/sample_service.h b/mojo/public/bindings/sample/generated/sample_service.h deleted file mode 100644 index 9a77aa0..0000000 --- a/mojo/public/bindings/sample/generated/sample_service.h +++ /dev/null @@ -1,132 +0,0 @@ -// 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(); // 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* data) { data_.ptr = data; } - void set_extra_bars(mojo::Array* extra_bars) { - extra_bars_.ptr = extra_bars; - } - void set_name(mojo::String* name) { - name_.ptr = name; - } - void set_files(mojo::Array* 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* data() const { return data_.ptr; } - const mojo::Array* 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* 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(); // 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_; - mojo::internal::ArrayPointer data_; - mojo::internal::ArrayPointer extra_bars_; - mojo::internal::StringPointer name_; - mojo::internal::ArrayPointer 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/generated/sample_service_internal.h b/mojo/public/bindings/sample/generated/sample_service_internal.h deleted file mode 100644 index fde2b3d..0000000 --- a/mojo/public/bindings/sample/generated/sample_service_internal.h +++ /dev/null @@ -1,41 +0,0 @@ -// 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 - -#include "mojo/public/bindings/lib/bindings_serialization.h" -#include "mojo/public/bindings/sample/generated/sample_service.h" - -namespace mojo { -namespace internal { - -template <> -class ObjectTraits { - 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* handles); - static bool DecodePointersAndHandles(sample::Bar* bar, - const mojo::Message& message); -}; - -template <> -class ObjectTraits { - 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* handles); - static bool DecodePointersAndHandles(sample::Foo* foo, - const mojo::Message& message); -}; - -} // namespace internal -} // namespace mojo - -#endif // MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_SERIALIZATION_H_ 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() { + _header_.num_bytes = sizeof(*this); + _header_.num_fields = 3; + } + + mojo::internal::StructHeader _header_; + mojo::internal::StructPointer 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( + 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::ComputeSizeOf( + const sample::Bar* bar) { + return sizeof(*bar); +} + +// static +sample::Bar* ObjectTraits::Clone( + const sample::Bar* bar, Buffer* buf) { + sample::Bar* clone = sample::Bar::New(buf); + memcpy(clone, bar, sizeof(*bar)); + return clone; +} + +// static +void ObjectTraits::EncodePointersAndHandles( + sample::Bar* bar, std::vector* handles) { +} + +// static +bool ObjectTraits::DecodePointersAndHandles( + sample::Bar* bar, const Message& message) { + return true; +} + +// static +size_t ObjectTraits::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::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::EncodePointersAndHandles( + sample::Foo* foo, std::vector* 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::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 { + public: + static void EncodePointersAndHandles( + sample::Service_Frobinate_Params* params, + std::vector* 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(); // 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* data) { data_.ptr = data; } + void set_extra_bars(mojo::Array* extra_bars) { + extra_bars_.ptr = extra_bars; + } + void set_name(mojo::String* name) { + name_.ptr = name; + } + void set_files(mojo::Array* 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* data() const { return data_.ptr; } + const mojo::Array* 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* 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(); // 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_; + mojo::internal::ArrayPointer data_; + mojo::internal::ArrayPointer extra_bars_; + mojo::internal::StringPointer name_; + mojo::internal::ArrayPointer 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 + +#include "mojo/public/bindings/lib/bindings_serialization.h" +#include "mojom/sample_service.h" + +namespace mojo { +namespace internal { + +template <> +class ObjectTraits { + 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* handles); + static bool DecodePointersAndHandles(sample::Bar* bar, + const mojo::Message& message); +}; + +template <> +class ObjectTraits { + 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* handles); + static bool DecodePointersAndHandles(sample::Foo* foo, + const mojo::Message& message); +}; + +} // namespace internal +} // namespace mojo + +#endif // MOJO_GENERATED_BINDINGS_SAMPLE_SERVICE_SERIALIZATION_H_ diff --git a/mojo/public/bindings/sample/sample_service.idl b/mojo/public/bindings/sample/sample_service.idl deleted file mode 100644 index 3358a58..0000000 --- a/mojo/public/bindings/sample/sample_service.idl +++ /dev/null @@ -1,33 +0,0 @@ -// 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. - -// HYPOTHETICAL IDL: - -module sample { - -struct Bar { - uint8 alpha @0; - uint8 beta @1; - uint8 gamma @2; -}; - -[RequiredFields=7] -struct Foo { - string name @8; - int32 x @0; - int32 y @1; - bool a @2; - bool b @3; - bool c @4; - Bar bar @5; - Bar[] extra_bars @7; - uint8[] data @6; - handle[] files @9; -}; - -interface Service { - void Frobinate(Foo foo @0, bool baz @1, handle port @2) @0; -}; - -} diff --git a/mojo/public/bindings/sample/sample_service.mojom b/mojo/public/bindings/sample/sample_service.mojom index a2903f3..3358a58 100644 --- a/mojo/public/bindings/sample/sample_service.mojom +++ b/mojo/public/bindings/sample/sample_service.mojom @@ -1,31 +1,33 @@ -{ - 'name': 'sample_service', - 'namespace': 'sample', - 'structs': [{ - 'name': 'Bar', - 'fields': [ - {'name': 'alpha', 'kind': 'u8', 'ordinal': 0}, - {'name': 'beta', 'kind': 'u8', 'ordinal': 1}, - {'name': 'gamma', 'kind': 'u8', 'ordinal': 2}]}, { - 'name': 'Foo', - 'fields': [ - {'name': 'name', 'kind': 's', 'ordinal': 8}, - {'name': 'x', 'kind': 'i32', 'ordinal': 0}, - {'name': 'y', 'kind': 'i32', 'ordinal': 1}, - {'name': 'a', 'kind': 'b', 'ordinal': 2}, - {'name': 'b', 'kind': 'b', 'ordinal': 3}, - {'name': 'c', 'kind': 'b', 'ordinal': 4}, - {'name': 'bar', 'kind': 'x:Bar', 'ordinal': 5}, - {'name': 'extra_bars', 'kind': 'a:x:Bar', 'ordinal': 7}, - {'name': 'data', 'kind': 'a:u8', 'ordinal': 6}, - {'name': 'files', 'kind': 'a:h', 'ordinal': 9}]}], - 'interfaces': [{ - 'name': 'Service', - 'methods': [{ - 'name': 'Frobinate', - 'ordinal': 0, - 'parameters': [ - {'name': 'foo', 'kind': 'x:Foo', 'ordinal': 0}, - {'name': 'baz', 'kind': 'b', 'ordinal': 1}, - {'name': 'port', 'kind': 'h', 'ordinal': 2}]}]}] +// 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. + +// HYPOTHETICAL IDL: + +module sample { + +struct Bar { + uint8 alpha @0; + uint8 beta @1; + uint8 gamma @2; +}; + +[RequiredFields=7] +struct Foo { + string name @8; + int32 x @0; + int32 y @1; + bool a @2; + bool b @3; + bool c @4; + Bar bar @5; + Bar[] extra_bars @7; + uint8[] data @6; + handle[] files @9; +}; + +interface Service { + void Frobinate(Foo foo @0, bool baz @1, handle port @2) @0; +}; + } diff --git a/mojo/public/bindings/sample/sample_test.cc b/mojo/public/bindings/sample/sample_test.cc index 7fb999e..1ca95dc 100644 --- a/mojo/public/bindings/sample/sample_test.cc +++ b/mojo/public/bindings/sample/sample_test.cc @@ -6,7 +6,7 @@ #include -#include "mojo/public/bindings/sample/generated/sample_service.h" +#include "mojom/sample_service.h" namespace sample { -- cgit v1.1