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

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/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 if (!ValidateMessageIsResponse(message, &validation_context))
30 return false;
31
32 switch (message->header()->name) {
33 case interface_control::kRunMessageId:
34 return ValidateMessagePayload<
35 interface_control::internal::RunResponseMessageParams_Data>(
36 message, &validation_context);
37 }
38 return false;
39 }
40
23 using RunCallback = 41 using RunCallback =
24 base::Callback<void(interface_control::RunResponseMessageParamsPtr)>; 42 base::Callback<void(interface_control::RunResponseMessageParamsPtr)>;
25 43
26 class RunResponseForwardToCallback : public MessageReceiver { 44 class RunResponseForwardToCallback : public MessageReceiver {
27 public: 45 public:
28 explicit RunResponseForwardToCallback(const RunCallback& callback) 46 explicit RunResponseForwardToCallback(const RunCallback& callback)
29 : callback_(callback) {} 47 : callback_(callback) {}
30 bool Accept(Message* message) override; 48 bool Accept(Message* message) override;
31 49
32 private: 50 private:
33 RunCallback callback_; 51 RunCallback callback_;
34 DISALLOW_COPY_AND_ASSIGN(RunResponseForwardToCallback); 52 DISALLOW_COPY_AND_ASSIGN(RunResponseForwardToCallback);
35 }; 53 };
36 54
37 bool RunResponseForwardToCallback::Accept(Message* message) { 55 bool RunResponseForwardToCallback::Accept(Message* message) {
56 if (!ValidateControlResponse(message))
57 return false;
58
38 interface_control::internal::RunResponseMessageParams_Data* params = 59 interface_control::internal::RunResponseMessageParams_Data* params =
39 reinterpret_cast< 60 reinterpret_cast<
40 interface_control::internal::RunResponseMessageParams_Data*>( 61 interface_control::internal::RunResponseMessageParams_Data*>(
41 message->mutable_payload()); 62 message->mutable_payload());
42 interface_control::RunResponseMessageParamsPtr params_ptr; 63 interface_control::RunResponseMessageParamsPtr params_ptr;
43 SerializationContext context; 64 SerializationContext context;
44 Deserialize<interface_control::RunResponseMessageParamsDataView>( 65 Deserialize<interface_control::RunResponseMessageParamsDataView>(
45 params, &params_ptr, &context); 66 params, &params_ptr, &context);
46 67
47 callback_.Run(std::move(params_ptr)); 68 callback_.Run(std::move(params_ptr));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 109
89 void RunVersionCallback( 110 void RunVersionCallback(
90 const base::Callback<void(uint32_t)>& callback, 111 const base::Callback<void(uint32_t)>& callback,
91 interface_control::RunResponseMessageParamsPtr run_response) { 112 interface_control::RunResponseMessageParamsPtr run_response) {
92 uint32_t version = 0u; 113 uint32_t version = 0u;
93 if (run_response->output && run_response->output->is_query_version_result()) 114 if (run_response->output && run_response->output->is_query_version_result())
94 version = run_response->output->get_query_version_result()->version; 115 version = run_response->output->get_query_version_result()->version;
95 callback.Run(version); 116 callback.Run(version);
96 } 117 }
97 118
119 void RunClosure(const base::Closure& callback,
120 interface_control::RunResponseMessageParamsPtr run_response) {
121 callback.Run();
122 }
123
98 } // namespace 124 } // namespace
99 125
100 ControlMessageProxy::ControlMessageProxy(MessageReceiverWithResponder* receiver) 126 ControlMessageProxy::ControlMessageProxy(MessageReceiverWithResponder* receiver)
101 : receiver_(receiver) { 127 : receiver_(receiver) {
102 } 128 }
103 129
130 ControlMessageProxy::~ControlMessageProxy() = default;
131
104 void ControlMessageProxy::QueryVersion( 132 void ControlMessageProxy::QueryVersion(
105 const base::Callback<void(uint32_t)>& callback) { 133 const base::Callback<void(uint32_t)>& callback) {
106 auto input_ptr = interface_control::RunInput::New(); 134 auto input_ptr = interface_control::RunInput::New();
107 input_ptr->set_query_version(interface_control::QueryVersion::New()); 135 input_ptr->set_query_version(interface_control::QueryVersion::New());
108 SendRunMessage(receiver_, std::move(input_ptr), 136 SendRunMessage(receiver_, std::move(input_ptr),
109 base::Bind(&RunVersionCallback, callback), &context_); 137 base::Bind(&RunVersionCallback, callback), &context_);
110 } 138 }
111 139
112 void ControlMessageProxy::RequireVersion(uint32_t version) { 140 void ControlMessageProxy::RequireVersion(uint32_t version) {
113 auto require_version = interface_control::RequireVersion::New(); 141 auto require_version = interface_control::RequireVersion::New();
114 require_version->version = version; 142 require_version->version = version;
115 auto input_ptr = interface_control::RunOrClosePipeInput::New(); 143 auto input_ptr = interface_control::RunOrClosePipeInput::New();
116 input_ptr->set_require_version(std::move(require_version)); 144 input_ptr->set_require_version(std::move(require_version));
117 SendRunOrClosePipeMessage(receiver_, std::move(input_ptr), &context_); 145 SendRunOrClosePipeMessage(receiver_, std::move(input_ptr), &context_);
118 } 146 }
119 147
148 void ControlMessageProxy::FlushForTesting() {
149 if (encountered_error_)
150 return;
151
152 auto input_ptr = interface_control::RunInput::New();
153 input_ptr->set_flush_for_testing(interface_control::FlushForTesting::New());
154 base::RunLoop run_loop;
155 run_loop_quit_closure_ = run_loop.QuitClosure();
156 SendRunMessage(
157 receiver_, std::move(input_ptr),
158 base::Bind(&RunClosure,
159 base::Bind(&ControlMessageProxy::RunFlushForTestingClosure,
160 base::Unretained(this))),
161 &context_);
162 run_loop.Run();
163 }
164
165 void ControlMessageProxy::RunFlushForTestingClosure() {
166 DCHECK(!run_loop_quit_closure_.is_null());
167 run_loop_quit_closure_.Run();
dcheng 2016/09/01 08:19:00 This could use base::ResetAndReturn().Run()
Sam McNally 2016/09/01 08:35:34 Done.
168 run_loop_quit_closure_.Reset();
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