1 // Copyright 2015 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.
4
5 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/macros.h"
13 #include "mojo/public/cpp/bindings/lib/message_builder.h"
14 #include "mojo/public/cpp/bindings/lib/serialization.h"
15 #include "mojo/public/cpp/bindings/message.h"
16 #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h"
17
18 namespace mojo {
19 namespace internal {
20
21 namespace {
22
23 using RunCallback = base::Callback<void(QueryVersionResultPtr)>;
24
25 class RunResponseForwardToCallback : public MessageReceiver {
26 public:
RunResponseForwardToCallback(const RunCallback & callback)27 RunResponseForwardToCallback(const RunCallback& callback)
28 : callback_(callback) {}
29 bool Accept(Message* message) override;
30
31 private:
32 RunCallback callback_;
33 DISALLOW_COPY_AND_ASSIGN(RunResponseForwardToCallback);
34 };
35
Accept(Message * message)36 bool RunResponseForwardToCallback::Accept(Message* message) {
37 RunResponseMessageParams_Data* params =
38 reinterpret_cast<RunResponseMessageParams_Data*>(
39 message->mutable_payload());
40 RunResponseMessageParamsPtr params_ptr;
41 SerializationContext context;
42 Deserialize<RunResponseMessageParamsPtr>(params, ¶ms_ptr, &context);
43
44 callback_.Run(std::move(params_ptr->query_version_result));
45 return true;
46 }
47
SendRunMessage(MessageReceiverWithResponder * receiver,QueryVersionPtr query_version,const RunCallback & callback,SerializationContext * context)48 void SendRunMessage(MessageReceiverWithResponder* receiver,
49 QueryVersionPtr query_version,
50 const RunCallback& callback,
51 SerializationContext* context) {
52 RunMessageParamsPtr params_ptr(RunMessageParams::New());
53 params_ptr->reserved0 = 16u;
54 params_ptr->reserved1 = 0u;
55 params_ptr->query_version = std::move(query_version);
56
57 size_t size = PrepareToSerialize<RunMessageParamsPtr>(params_ptr, context);
58 RequestMessageBuilder builder(kRunMessageId, size);
59
60 RunMessageParams_Data* params = nullptr;
61 Serialize<RunMessageParamsPtr>(params_ptr, builder.buffer(), ¶ms,
62 context);
63 MessageReceiver* responder = new RunResponseForwardToCallback(callback);
64 if (!receiver->AcceptWithResponder(builder.message(), responder))
65 delete responder;
66 }
67
SendRunOrClosePipeMessage(MessageReceiverWithResponder * receiver,RequireVersionPtr require_version,SerializationContext * context)68 void SendRunOrClosePipeMessage(MessageReceiverWithResponder* receiver,
69 RequireVersionPtr require_version,
70 SerializationContext* context) {
71 RunOrClosePipeMessageParamsPtr params_ptr(RunOrClosePipeMessageParams::New());
72 params_ptr->reserved0 = 16u;
73 params_ptr->reserved1 = 0u;
74 params_ptr->require_version = std::move(require_version);
75
76 size_t size =
77 PrepareToSerialize<RunOrClosePipeMessageParamsPtr>(params_ptr, context);
78 MessageBuilder builder(kRunOrClosePipeMessageId, size);
79
80 RunOrClosePipeMessageParams_Data* params = nullptr;
81 Serialize<RunOrClosePipeMessageParamsPtr>(params_ptr, builder.buffer(),
82 ¶ms, context);
83 bool ok = receiver->Accept(builder.message());
84 ALLOW_UNUSED_LOCAL(ok);
85 }
86
RunVersionCallback(const base::Callback<void (uint32_t)> & callback,QueryVersionResultPtr query_version_result)87 void RunVersionCallback(const base::Callback<void(uint32_t)>& callback,
88 QueryVersionResultPtr query_version_result) {
89 callback.Run(query_version_result->version);
90 }
91
92 } // namespace
93
ControlMessageProxy(MessageReceiverWithResponder * receiver)94 ControlMessageProxy::ControlMessageProxy(MessageReceiverWithResponder* receiver)
95 : receiver_(receiver) {
96 }
97
QueryVersion(const base::Callback<void (uint32_t)> & callback)98 void ControlMessageProxy::QueryVersion(
99 const base::Callback<void(uint32_t)>& callback) {
100 SendRunMessage(receiver_, QueryVersion::New(),
101 base::Bind(&RunVersionCallback, callback), &context_);
102 }
103
RequireVersion(uint32_t version)104 void ControlMessageProxy::RequireVersion(uint32_t version) {
105 RequireVersionPtr require_version(RequireVersion::New());
106 require_version->version = version;
107 SendRunOrClosePipeMessage(receiver_, std::move(require_version), &context_);
108 }
109
110 } // namespace internal
111 } // namespace mojo
112