Index: mojo/public/bindings/sample_test.cc |
diff --git a/mojo/public/bindings/sample_test.cc b/mojo/public/bindings/sample_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..735fe5ed14bd054e871a30353c494ccf4ddc8ee1 |
--- /dev/null |
+++ b/mojo/public/bindings/sample_test.cc |
@@ -0,0 +1,192 @@ |
+// 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 <assert.h> |
+#include <stddef.h> |
+#include <stdint.h> |
+#include <stdio.h> |
+#include <stdlib.h> |
+#include <string.h> |
+ |
+#include <new> |
+#include <vector> |
+ |
+#include "mojo/public/bindings/generated/sample_service.h" |
+#include "mojo/public/bindings/generated/sample_service_proxy.h" |
+#include "mojo/public/bindings/generated/sample_service_stub.h" |
+ |
+namespace sample { |
+ |
+static void PrintSpacer(int depth) { |
+ for (int i = 0; i < depth; ++i) |
+ printf(" "); |
+} |
+ |
+static void Print(int depth, const char* name, bool value) { |
+ PrintSpacer(depth); |
+ printf("%s: %s\n", name, value ? "true" : "false"); |
+} |
+ |
+static void Print(int depth, const char* name, int32_t value) { |
+ PrintSpacer(depth); |
+ printf("%s: %d\n", name, value); |
+} |
+ |
+static void Print(int depth, const char* name, uint8_t value) { |
+ PrintSpacer(depth); |
+ printf("%s: %u\n", name, value); |
+} |
+ |
+static void Print(int depth, const char* name, mojo::Handle value) { |
+ PrintSpacer(depth); |
+ printf("%s: 0x%x\n", name, value.value); |
+} |
+ |
+static void Print(int depth, const char* name, const mojo::String* str) { |
+ PrintSpacer(depth); |
+ printf("%s: \"%*s\"\n", name, static_cast<int>(str->size()), str->data()); |
+} |
+ |
+static void Print(int depth, const char* name, const Bar* bar) { |
+ PrintSpacer(depth); |
+ printf("%s: %p\n", name, bar); |
+ if (bar) { |
+ ++depth; |
+ Print(depth, "alpha", bar->alpha()); |
+ Print(depth, "beta", bar->beta()); |
+ Print(depth, "gamma", bar->gamma()); |
+ --depth; |
+ } |
+} |
+ |
+template <typename T> |
+static void Print(int depth, const char* name, const mojo::Array<T>* array) { |
+ PrintSpacer(depth); |
+ printf("%s: %p\n", name, array); |
+ if (array) { |
+ ++depth; |
+ for (size_t i = 0; i < array->size(); ++i) { |
+ char buf[32]; |
+ sprintf(buf, "%lu", i); |
+ Print(depth, buf, array->at(i)); |
+ } |
+ --depth; |
+ } |
+} |
+ |
+static void Print(int depth, const char* name, const Foo* foo) { |
+ PrintSpacer(depth); |
+ printf("%s: %p\n", name, foo); |
+ if (foo) { |
+ ++depth; |
+ Print(depth, "name", foo->name()); |
+ Print(depth, "x", foo->x()); |
+ Print(depth, "y", foo->y()); |
+ Print(depth, "a", foo->a()); |
+ Print(depth, "b", foo->b()); |
+ Print(depth, "c", foo->c()); |
+ Print(depth, "bar", foo->bar()); |
+ Print(depth, "extra_bars", foo->extra_bars()); |
+ Print(depth, "data", foo->data()); |
+ Print(depth, "files", foo->files()); |
+ --depth; |
+ } |
+} |
+ |
+class ServiceImpl : public ServiceStub { |
+ public: |
+ virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port) { |
+ // Users code goes here to handle the incoming Frobinate message. |
+ // We'll just dump the Foo structure and all of its members. |
+ |
+ printf("Frobinate:\n"); |
+ |
+ int depth = 1; |
+ Print(depth, "foo", foo); |
+ Print(depth, "baz", baz); |
+ Print(depth, "port", port); |
+ } |
+}; |
+ |
+class SimpleMessageSender : public mojo::MessageSender { |
+ public: |
+ virtual void Send(const mojo::Message& message) { |
+ // Imagine some IPC happened here. |
+ |
+ // In the receiving process, an implementation of ServiceStub is known to |
+ // the system. It receives the incoming message. |
+ ServiceImpl impl; |
+ |
+ ServiceStub* stub = &impl; |
+ stub->OnMessageReceived(message); |
+ } |
+}; |
+ |
+void Exercise() { |
+ SimpleMessageSender sender; |
+ |
+ // User has a proxy to a Service somehow. |
+ Service* service = new ServiceProxy(&sender); |
+ |
+ // User constructs a message to send. |
+ |
+ // Notice that it doesn't matter in what order the structs / arrays are |
+ // allocated. Here, the various members of Foo are allocated before Foo is |
+ // allocated. |
+ |
+ mojo::MessageBuffer buf; |
+ |
+ Bar* bar = buf.New<Bar>(); |
+ bar->set_alpha(20); |
+ bar->set_beta(40); |
+ bar->set_gamma(60); |
+ |
+ const char kName[] = "foopy"; |
+ const size_t kNameLen = sizeof(kName) - 1; |
+ mojo::String* name = buf.NewString(kNameLen); |
+ memcpy(name->data(), kName, kNameLen); |
+ |
+ const size_t kExtraBarsLen = 3; |
+ mojo::Array<Bar*>* extra_bars = buf.NewArray<Bar*>(kExtraBarsLen); |
+ for (size_t i = 0; i < kExtraBarsLen; ++i) { |
+ Bar* bar = buf.New<Bar>(); |
+ bar->set_alpha(i * 100); |
+ bar->set_beta(i * 100 + 20); |
+ bar->set_gamma(i * 100 + 40); |
+ (*extra_bars)[i] = bar; |
+ } |
+ |
+ const size_t kDataLen = 10; |
+ mojo::Array<uint8_t>* data = buf.NewArray<uint8_t>(kDataLen); |
+ for (size_t i = 0; i < kDataLen; ++i) |
+ (*data)[i] = static_cast<uint8_t>(kDataLen - i); |
+ |
+ const size_t kFilesLen = 4; |
+ mojo::Array<mojo::Handle>* files = buf.NewArray<mojo::Handle>(kFilesLen); |
+ for (size_t i = 0; i < kFilesLen; ++i) |
+ (*files)[i].value = 0xFFFF - i; |
+ |
+ Foo* foo = buf.New<Foo>(); |
+ foo->set_name(name); |
+ foo->set_x(1); |
+ foo->set_y(2); |
+ foo->set_a(false); |
+ foo->set_b(true); |
+ foo->set_c(false); |
+ foo->set_bar(bar); |
+ foo->set_extra_bars(extra_bars); |
+ foo->set_data(data); |
+ foo->set_files(files); |
+ |
+ mojo::Handle port = { 10 }; |
+ |
+ service->Frobinate(foo, true, port); |
+} |
+ |
+} // namespace sample |
+ |
+int main() { |
+ sample::Exercise(); |
+ return 0; |
+} |