OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 5 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/run_loop.h" | |
13 #include "mojo/public/cpp/bindings/lib/message_builder.h" | 14 #include "mojo/public/cpp/bindings/lib/message_builder.h" |
14 #include "mojo/public/cpp/bindings/lib/serialization.h" | 15 #include "mojo/public/cpp/bindings/lib/serialization.h" |
16 #include "mojo/public/cpp/bindings/lib/validation_util.h" | |
15 #include "mojo/public/cpp/bindings/message.h" | 17 #include "mojo/public/cpp/bindings/message.h" |
16 #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h" | 18 #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h" |
17 | 19 |
18 namespace mojo { | 20 namespace mojo { |
19 namespace internal { | 21 namespace internal { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
25 bool ValidateControlResponse(Message* message) { | |
26 ValidationContext validation_context( | |
27 message->data(), message->data_num_bytes(), message->handles()->size(), | |
28 message, "ControlResponseValidator"); | |
29 switch (message->header()->name) { | |
yzshen1
2016/08/31 17:06:47
we should also do ValidateMessageIsResponse
Sam McNally
2016/09/01 00:44:37
Done.
| |
30 case interface_control::kRunMessageId: | |
31 return ValidateMessagePayload< | |
32 interface_control::internal::RunResponseMessageParams_Data>( | |
33 message, &validation_context); | |
34 } | |
35 return false; | |
36 } | |
37 | |
23 using RunCallback = | 38 using RunCallback = |
24 base::Callback<void(interface_control::RunResponseMessageParamsPtr)>; | 39 base::Callback<void(interface_control::RunResponseMessageParamsPtr)>; |
25 | 40 |
26 class RunResponseForwardToCallback : public MessageReceiver { | 41 class RunResponseForwardToCallback : public MessageReceiver { |
27 public: | 42 public: |
28 explicit RunResponseForwardToCallback(const RunCallback& callback) | 43 explicit RunResponseForwardToCallback(const RunCallback& callback) |
29 : callback_(callback) {} | 44 : callback_(callback) {} |
30 bool Accept(Message* message) override; | 45 bool Accept(Message* message) override; |
31 | 46 |
32 private: | 47 private: |
33 RunCallback callback_; | 48 RunCallback callback_; |
34 DISALLOW_COPY_AND_ASSIGN(RunResponseForwardToCallback); | 49 DISALLOW_COPY_AND_ASSIGN(RunResponseForwardToCallback); |
35 }; | 50 }; |
36 | 51 |
37 bool RunResponseForwardToCallback::Accept(Message* message) { | 52 bool RunResponseForwardToCallback::Accept(Message* message) { |
53 if (!ValidateControlResponse(message)) | |
54 return false; | |
55 | |
38 interface_control::internal::RunResponseMessageParams_Data* params = | 56 interface_control::internal::RunResponseMessageParams_Data* params = |
39 reinterpret_cast< | 57 reinterpret_cast< |
40 interface_control::internal::RunResponseMessageParams_Data*>( | 58 interface_control::internal::RunResponseMessageParams_Data*>( |
41 message->mutable_payload()); | 59 message->mutable_payload()); |
42 interface_control::RunResponseMessageParamsPtr params_ptr; | 60 interface_control::RunResponseMessageParamsPtr params_ptr; |
43 SerializationContext context; | 61 SerializationContext context; |
44 Deserialize<interface_control::RunResponseMessageParamsDataView>( | 62 Deserialize<interface_control::RunResponseMessageParamsDataView>( |
45 params, ¶ms_ptr, &context); | 63 params, ¶ms_ptr, &context); |
46 | 64 |
47 callback_.Run(std::move(params_ptr)); | 65 callback_.Run(std::move(params_ptr)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 | 106 |
89 void RunVersionCallback( | 107 void RunVersionCallback( |
90 const base::Callback<void(uint32_t)>& callback, | 108 const base::Callback<void(uint32_t)>& callback, |
91 interface_control::RunResponseMessageParamsPtr run_response) { | 109 interface_control::RunResponseMessageParamsPtr run_response) { |
92 uint32_t version = 0u; | 110 uint32_t version = 0u; |
93 if (run_response->output && run_response->output->is_query_version_result()) | 111 if (run_response->output && run_response->output->is_query_version_result()) |
94 version = run_response->output->get_query_version_result()->version; | 112 version = run_response->output->get_query_version_result()->version; |
95 callback.Run(version); | 113 callback.Run(version); |
96 } | 114 } |
97 | 115 |
116 void RunClosure(const base::Closure& callback, | |
117 interface_control::RunResponseMessageParamsPtr run_response) { | |
118 callback.Run(); | |
119 } | |
120 | |
98 } // namespace | 121 } // namespace |
99 | 122 |
100 ControlMessageProxy::ControlMessageProxy(MessageReceiverWithResponder* receiver) | 123 ControlMessageProxy::ControlMessageProxy(MessageReceiverWithResponder* receiver) |
101 : receiver_(receiver) { | 124 : receiver_(receiver) { |
102 } | 125 } |
103 | 126 |
127 ControlMessageProxy::~ControlMessageProxy() = default; | |
128 | |
104 void ControlMessageProxy::QueryVersion( | 129 void ControlMessageProxy::QueryVersion( |
105 const base::Callback<void(uint32_t)>& callback) { | 130 const base::Callback<void(uint32_t)>& callback) { |
106 auto input_ptr = interface_control::RunInput::New(); | 131 auto input_ptr = interface_control::RunInput::New(); |
107 input_ptr->set_query_version(interface_control::QueryVersion::New()); | 132 input_ptr->set_query_version(interface_control::QueryVersion::New()); |
108 SendRunMessage(receiver_, std::move(input_ptr), | 133 SendRunMessage(receiver_, std::move(input_ptr), |
109 base::Bind(&RunVersionCallback, callback), &context_); | 134 base::Bind(&RunVersionCallback, callback), &context_); |
110 } | 135 } |
111 | 136 |
112 void ControlMessageProxy::RequireVersion(uint32_t version) { | 137 void ControlMessageProxy::RequireVersion(uint32_t version) { |
113 auto require_version = interface_control::RequireVersion::New(); | 138 auto require_version = interface_control::RequireVersion::New(); |
114 require_version->version = version; | 139 require_version->version = version; |
115 auto input_ptr = interface_control::RunOrClosePipeInput::New(); | 140 auto input_ptr = interface_control::RunOrClosePipeInput::New(); |
116 input_ptr->set_require_version(std::move(require_version)); | 141 input_ptr->set_require_version(std::move(require_version)); |
117 SendRunOrClosePipeMessage(receiver_, std::move(input_ptr), &context_); | 142 SendRunOrClosePipeMessage(receiver_, std::move(input_ptr), &context_); |
118 } | 143 } |
119 | 144 |
145 void ControlMessageProxy::FlushForTesting() { | |
146 if (encountered_error_) | |
147 return; | |
148 | |
149 auto input_ptr = interface_control::RunInput::New(); | |
150 input_ptr->set_flush_for_testing(interface_control::FlushForTesting::New()); | |
151 base::RunLoop run_loop; | |
152 run_loop_quit_closure_ = run_loop.QuitClosure(); | |
153 SendRunMessage( | |
154 receiver_, std::move(input_ptr), | |
155 base::Bind(&RunClosure, | |
156 base::Bind(&ControlMessageProxy::RunFlushForTestingClosure, | |
157 base::Unretained(this))), | |
158 &context_); | |
159 run_loop.Run(); | |
160 } | |
161 | |
162 void ControlMessageProxy::RunFlushForTestingClosure() { | |
163 DCHECK(!run_loop_quit_closure_.is_null()); | |
164 run_loop_quit_closure_.Run(); | |
165 run_loop_quit_closure_.Reset(); | |
166 } | |
167 | |
168 void ControlMessageProxy::OnConnectionError() { | |
169 encountered_error_ = true; | |
170 if (!run_loop_quit_closure_.is_null()) | |
171 RunFlushForTestingClosure(); | |
172 } | |
173 | |
120 } // namespace internal | 174 } // namespace internal |
121 } // namespace mojo | 175 } // namespace mojo |
OLD | NEW |