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.
9 #include "mojo/public/bindings/sample/generated/sample_bar.h"
10 #include "mojo/public/bindings/sample/generated/sample_foo.h"
11 #include "mojo/public/bindings/sample/generated/sample_service.h"
12 #include "mojo/public/bindings/sample/generated/sample_service_proxy.h"
13 #include "mojo/public/bindings/sample/generated/sample_service_stub.h"
17 static void PrintSpacer(int depth) {
18 for (int i = 0; i < depth; ++i)
22 static void Print(int depth, const char* name, bool value) {
24 printf("%s: %s\n", name, value ? "true" : "false");
27 static void Print(int depth, const char* name, int32_t value) {
29 printf("%s: %d\n", name, value);
32 static void Print(int depth, const char* name, uint8_t value) {
34 printf("%s: %u\n", name, value);
37 static void Print(int depth, const char* name, mojo::Handle value) {
39 printf("%s: 0x%x\n", name, value.value);
42 static void Print(int depth, const char* name, const mojo::String* str) {
44 printf("%s: \"%*s\"\n", name, static_cast<int>(str->size()), &str->at(0));
47 static void Print(int depth, const char* name, const Bar* bar) {
49 printf("%s: %p\n", name, bar);
52 Print(depth, "alpha", bar->alpha());
53 Print(depth, "beta", bar->beta());
54 Print(depth, "gamma", bar->gamma());
60 static void Print(int depth, const char* name, const mojo::Array<T>* array) {
62 printf("%s: %p\n", name, array);
65 for (size_t i = 0; i < array->size(); ++i) {
67 sprintf(buf, "%lu", static_cast<unsigned long>(i));
68 Print(depth, buf, array->at(i));
74 static void Print(int depth, const char* name, const Foo* foo) {
76 printf("%s: %p\n", name, foo);
79 Print(depth, "name", foo->name());
80 Print(depth, "x", foo->x());
81 Print(depth, "y", foo->y());
82 Print(depth, "a", foo->a());
83 Print(depth, "b", foo->b());
84 Print(depth, "c", foo->c());
85 Print(depth, "bar", foo->bar());
86 Print(depth, "extra_bars", foo->extra_bars());
87 Print(depth, "data", foo->data());
88 Print(depth, "files", foo->files());
93 class ServiceImpl : public ServiceStub {
95 virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port)
97 // Users code goes here to handle the incoming Frobinate message.
98 // We'll just dump the Foo structure and all of its members.
100 printf("Frobinate:\n");
103 Print(depth, "foo", foo);
104 Print(depth, "baz", baz);
105 Print(depth, "port", port);
109 class SimpleMessageReceiver : public mojo::MessageReceiver {
111 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE {
112 // Imagine some IPC happened here.
114 // In the receiving process, an implementation of ServiceStub is known to
115 // the system. It receives the incoming message.
118 ServiceStub* stub = &impl;
119 return stub->Accept(message);
124 SimpleMessageReceiver receiver;
126 // User has a proxy to a Service somehow.
127 Service* service = new ServiceProxy(&receiver);
129 // User constructs a message to send.
131 // Notice that it doesn't matter in what order the structs / arrays are
132 // allocated. Here, the various members of Foo are allocated before Foo is
135 mojo::ScratchBuffer buf;
137 Bar* bar = Bar::New(&buf);
142 const char kName[] = "foopy";
143 mojo::String* name = mojo::String::NewCopyOf(&buf, std::string(kName));
145 mojo::Array<Bar*>* extra_bars = mojo::Array<Bar*>::New(&buf, 3);
146 for (size_t i = 0; i < extra_bars->size(); ++i) {
147 Bar* bar = Bar::New(&buf);
148 uint8_t base = static_cast<uint8_t>(i * 100);
149 bar->set_alpha(base);
150 bar->set_beta(base + 20);
151 bar->set_gamma(base + 40);
152 (*extra_bars)[i] = bar;
155 mojo::Array<uint8_t>* data = mojo::Array<uint8_t>::New(&buf, 10);
156 for (size_t i = 0; i < data->size(); ++i)
157 (*data)[i] = static_cast<uint8_t>(data->size() - i);
159 mojo::Array<mojo::Handle>* files = mojo::Array<mojo::Handle>::New(&buf, 4);
160 for (size_t i = 0; i < files->size(); ++i)
161 (*files)[i].value = static_cast<MojoHandle>(0xFFFF - i);
163 Foo* foo = Foo::New(&buf);
171 foo->set_extra_bars(extra_bars);
173 foo->set_files(files);
175 mojo::Handle port = { 10 };
177 service->Frobinate(foo, true, port);
180 } // namespace sample