Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: mojo/public/cpp/bindings/lib/control_message_proxy.cc

Issue 2280483002: Add FlushForTesting to InterfacePtr and Binding. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/callback_helpers.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/run_loop.h"
13 #include "mojo/public/cpp/bindings/lib/message_builder.h" 15 #include "mojo/public/cpp/bindings/lib/message_builder.h"
14 #include "mojo/public/cpp/bindings/lib/serialization.h" 16 #include "mojo/public/cpp/bindings/lib/serialization.h"
17 #include "mojo/public/cpp/bindings/lib/validation_util.h"
15 #include "mojo/public/cpp/bindings/message.h" 18 #include "mojo/public/cpp/bindings/message.h"
16 #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h" 19 #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h"
17 20
18 namespace mojo { 21 namespace mojo {
19 namespace internal { 22 namespace internal {
20 23
21 namespace { 24 namespace {
22 25
26 bool ValidateControlResponse(Message* message) {
27 ValidationContext validation_context(
28 message->data(), message->data_num_bytes(), message->handles()->size(),
29 message, "ControlResponseValidator");
30 if (!ValidateMessageIsResponse(message, &validation_context))
31 return false;
32
33 switch (message->header()->name) {
34 case interface_control::kRunMessageId:
35 return ValidateMessagePayload<
36 interface_control::internal::RunResponseMessageParams_Data>(
37 message, &validation_context);
38 }
39 return false;
40 }
41
23 using RunCallback = 42 using RunCallback =
24 base::Callback<void(interface_control::RunResponseMessageParamsPtr)>; 43 base::Callback<void(interface_control::RunResponseMessageParamsPtr)>;
25 44
26 class RunResponseForwardToCallback : public MessageReceiver { 45 class RunResponseForwardToCallback : public MessageReceiver {
27 public: 46 public:
28 explicit RunResponseForwardToCallback(const RunCallback& callback) 47 explicit RunResponseForwardToCallback(const RunCallback& callback)
29 : callback_(callback) {} 48 : callback_(callback) {}
30 bool Accept(Message* message) override; 49 bool Accept(Message* message) override;
31 50
32 private: 51 private:
33 RunCallback callback_; 52 RunCallback callback_;
34 DISALLOW_COPY_AND_ASSIGN(RunResponseForwardToCallback); 53 DISALLOW_COPY_AND_ASSIGN(RunResponseForwardToCallback);
35 }; 54 };
36 55
37 bool RunResponseForwardToCallback::Accept(Message* message) { 56 bool RunResponseForwardToCallback::Accept(Message* message) {
57 if (!ValidateControlResponse(message))
58 return false;
59
38 interface_control::internal::RunResponseMessageParams_Data* params = 60 interface_control::internal::RunResponseMessageParams_Data* params =
39 reinterpret_cast< 61 reinterpret_cast<
40 interface_control::internal::RunResponseMessageParams_Data*>( 62 interface_control::internal::RunResponseMessageParams_Data*>(
41 message->mutable_payload()); 63 message->mutable_payload());
42 interface_control::RunResponseMessageParamsPtr params_ptr; 64 interface_control::RunResponseMessageParamsPtr params_ptr;
43 SerializationContext context; 65 SerializationContext context;
44 Deserialize<interface_control::RunResponseMessageParamsDataView>( 66 Deserialize<interface_control::RunResponseMessageParamsDataView>(
45 params, &params_ptr, &context); 67 params, &params_ptr, &context);
46 68
47 callback_.Run(std::move(params_ptr)); 69 callback_.Run(std::move(params_ptr));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 110
89 void RunVersionCallback( 111 void RunVersionCallback(
90 const base::Callback<void(uint32_t)>& callback, 112 const base::Callback<void(uint32_t)>& callback,
91 interface_control::RunResponseMessageParamsPtr run_response) { 113 interface_control::RunResponseMessageParamsPtr run_response) {
92 uint32_t version = 0u; 114 uint32_t version = 0u;
93 if (run_response->output && run_response->output->is_query_version_result()) 115 if (run_response->output && run_response->output->is_query_version_result())
94 version = run_response->output->get_query_version_result()->version; 116 version = run_response->output->get_query_version_result()->version;
95 callback.Run(version); 117 callback.Run(version);
96 } 118 }
97 119
120 void RunClosure(const base::Closure& callback,
121 interface_control::RunResponseMessageParamsPtr run_response) {
122 callback.Run();
123 }
124
98 } // namespace 125 } // namespace
99 126
100 ControlMessageProxy::ControlMessageProxy(MessageReceiverWithResponder* receiver) 127 ControlMessageProxy::ControlMessageProxy(MessageReceiverWithResponder* receiver)
101 : receiver_(receiver) { 128 : receiver_(receiver) {
102 } 129 }
103 130
131 ControlMessageProxy::~ControlMessageProxy() = default;
132
104 void ControlMessageProxy::QueryVersion( 133 void ControlMessageProxy::QueryVersion(
105 const base::Callback<void(uint32_t)>& callback) { 134 const base::Callback<void(uint32_t)>& callback) {
106 auto input_ptr = interface_control::RunInput::New(); 135 auto input_ptr = interface_control::RunInput::New();
107 input_ptr->set_query_version(interface_control::QueryVersion::New()); 136 input_ptr->set_query_version(interface_control::QueryVersion::New());
108 SendRunMessage(receiver_, std::move(input_ptr), 137 SendRunMessage(receiver_, std::move(input_ptr),
109 base::Bind(&RunVersionCallback, callback), &context_); 138 base::Bind(&RunVersionCallback, callback), &context_);
110 } 139 }
111 140
112 void ControlMessageProxy::RequireVersion(uint32_t version) { 141 void ControlMessageProxy::RequireVersion(uint32_t version) {
113 auto require_version = interface_control::RequireVersion::New(); 142 auto require_version = interface_control::RequireVersion::New();
114 require_version->version = version; 143 require_version->version = version;
115 auto input_ptr = interface_control::RunOrClosePipeInput::New(); 144 auto input_ptr = interface_control::RunOrClosePipeInput::New();
116 input_ptr->set_require_version(std::move(require_version)); 145 input_ptr->set_require_version(std::move(require_version));
117 SendRunOrClosePipeMessage(receiver_, std::move(input_ptr), &context_); 146 SendRunOrClosePipeMessage(receiver_, std::move(input_ptr), &context_);
118 } 147 }
119 148
149 void ControlMessageProxy::FlushForTesting() {
150 if (encountered_error_)
151 return;
152
153 auto input_ptr = interface_control::RunInput::New();
154 input_ptr->set_flush_for_testing(interface_control::FlushForTesting::New());
155 base::RunLoop run_loop;
156 run_loop_quit_closure_ = run_loop.QuitClosure();
157 SendRunMessage(
158 receiver_, std::move(input_ptr),
159 base::Bind(&RunClosure,
160 base::Bind(&ControlMessageProxy::RunFlushForTestingClosure,
161 base::Unretained(this))),
162 &context_);
163 run_loop.Run();
164 }
165
166 void ControlMessageProxy::RunFlushForTestingClosure() {
167 DCHECK(!run_loop_quit_closure_.is_null());
168 base::ResetAndReturn(&run_loop_quit_closure_).Run();
169 }
170
171 void ControlMessageProxy::OnConnectionError() {
172 encountered_error_ = true;
173 if (!run_loop_quit_closure_.is_null())
174 RunFlushForTestingClosure();
175 }
176
120 } // namespace internal 177 } // namespace internal
121 } // namespace mojo 178 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/control_message_proxy.h ('k') | mojo/public/cpp/bindings/lib/interface_endpoint_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698