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.
5 #include "mojo/public/bindings/sample/generated/sample_service_proxy.h"
9 #include "mojo/public/bindings/lib/message.h"
10 #include "mojo/public/bindings/lib/message_builder.h"
11 #include "mojo/public/bindings/sample/generated/sample_service_serialization.h"
15 ServiceProxy::ServiceProxy(mojo::MessageReceiver* receiver)
16 : receiver_(receiver) {
19 void ServiceProxy::Frobinate(const Foo* foo, bool baz, mojo::Handle port) {
21 mojo::internal::Align(sizeof(internal::Service_Frobinate_Params));
22 payload_size += mojo::internal::ComputeSizeOf(foo);
24 mojo::MessageBuilder builder(internal::kService_Frobinate_Name, payload_size);
26 // We now go about allocating the anonymous Frobinate_Params struct. It
27 // holds the parameters to the Frobinate message.
29 // Notice how foo is cloned. This causes a copy of foo to be generated
30 // within the same buffer as the Frobinate_Params struct. That's what we
31 // need in order to generate a contiguous blob of message data.
33 internal::Service_Frobinate_Params* params =
34 internal::Service_Frobinate_Params::New(builder.buffer());
35 params->set_foo(mojo::internal::Clone(foo, builder.buffer()));
37 params->set_port(port);
39 // NOTE: If foo happened to be a graph with cycles, then Clone would not
42 // Next step is to encode pointers and handles so that messages become
43 // hermetic. Pointers become offsets and handles becomes indices into the
45 mojo::Message message;
46 mojo::internal::EncodePointersAndHandles(params, &message.handles);
48 // Finally, we get the generated message data, and forward it to the
50 message.data = builder.Finish();
52 receiver_->Accept(&message);