OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "dbus/test_service.h" | 5 #include "dbus/test_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
11 #include "dbus/exported_object.h" | 11 #include "dbus/exported_object.h" |
12 #include "dbus/message.h" | 12 #include "dbus/message.h" |
13 | 13 |
14 namespace dbus { | 14 namespace dbus { |
15 | 15 |
16 // Echo, SlowEcho, BrokenMethod. | 16 // Echo, SlowEcho, AsyncEcho, BrokenMethod. |
17 const int TestService::kNumMethodsToExport = 3; | 17 const int TestService::kNumMethodsToExport = 4; |
18 | 18 |
19 TestService::Options::Options() { | 19 TestService::Options::Options() { |
20 } | 20 } |
21 | 21 |
22 TestService::Options::~Options() { | 22 TestService::Options::~Options() { |
23 } | 23 } |
24 | 24 |
25 TestService::TestService(const Options& options) | 25 TestService::TestService(const Options& options) |
26 : base::Thread("TestService"), | 26 : base::Thread("TestService"), |
27 dbus_thread_message_loop_proxy_(options.dbus_thread_message_loop_proxy), | 27 dbus_thread_message_loop_proxy_(options.dbus_thread_message_loop_proxy), |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 "org.chromium.TestInterface", | 141 "org.chromium.TestInterface", |
142 "SlowEcho", | 142 "SlowEcho", |
143 base::Bind(&TestService::SlowEcho, | 143 base::Bind(&TestService::SlowEcho, |
144 base::Unretained(this)), | 144 base::Unretained(this)), |
145 base::Bind(&TestService::OnExported, | 145 base::Bind(&TestService::OnExported, |
146 base::Unretained(this))); | 146 base::Unretained(this))); |
147 ++num_methods; | 147 ++num_methods; |
148 | 148 |
149 exported_object_->ExportMethod( | 149 exported_object_->ExportMethod( |
150 "org.chromium.TestInterface", | 150 "org.chromium.TestInterface", |
151 "AsyncEcho", | |
152 base::Bind(&TestService::AsyncEcho, | |
153 base::Unretained(this)), | |
154 base::Bind(&TestService::OnExported, | |
155 base::Unretained(this))); | |
156 ++num_methods; | |
157 | |
158 exported_object_->ExportMethod( | |
159 "org.chromium.TestInterface", | |
151 "BrokenMethod", | 160 "BrokenMethod", |
152 base::Bind(&TestService::BrokenMethod, | 161 base::Bind(&TestService::BrokenMethod, |
153 base::Unretained(this)), | 162 base::Unretained(this)), |
154 base::Bind(&TestService::OnExported, | 163 base::Bind(&TestService::OnExported, |
155 base::Unretained(this))); | 164 base::Unretained(this))); |
156 ++num_methods; | 165 ++num_methods; |
157 | 166 |
158 // Just print an error message as we don't want to crash tests. | 167 // Just print an error message as we don't want to crash tests. |
159 // Tests will fail at a call to WaitUntilServiceIsStarted(). | 168 // Tests will fail at a call to WaitUntilServiceIsStarted(). |
160 if (num_methods != kNumMethodsToExport) { | 169 if (num_methods != kNumMethodsToExport) { |
161 LOG(ERROR) << "The number of methods does not match"; | 170 LOG(ERROR) << "The number of methods does not match"; |
162 } | 171 } |
163 message_loop->Run(); | 172 message_loop->Run(); |
164 } | 173 } |
165 | 174 |
166 Response* TestService::Echo(MethodCall* method_call) { | 175 void TestService::Echo(MethodCall* method_call, |
176 dbus::ExportedObject::ResponseSender response_sender) { | |
167 MessageReader reader(method_call); | 177 MessageReader reader(method_call); |
168 std::string text_message; | 178 std::string text_message; |
169 if (!reader.PopString(&text_message)) | 179 if (!reader.PopString(&text_message)) { |
170 return NULL; | 180 response_sender.Run(NULL); |
181 return; | |
182 } | |
171 | 183 |
172 Response* response = Response::FromMethodCall(method_call); | 184 Response* response = Response::FromMethodCall(method_call); |
173 MessageWriter writer(response); | 185 MessageWriter writer(response); |
174 writer.AppendString(text_message); | 186 writer.AppendString(text_message); |
175 return response; | 187 response_sender.Run(response); |
176 } | 188 } |
177 | 189 |
178 Response* TestService::SlowEcho(MethodCall* method_call) { | 190 void TestService::SlowEcho( |
191 MethodCall* method_call, | |
192 dbus::ExportedObject::ResponseSender response_sender) { | |
179 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); | 193 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); |
180 return Echo(method_call); | 194 Echo(method_call, response_sender); |
181 } | 195 } |
182 | 196 |
183 Response* TestService::BrokenMethod(MethodCall* method_call) { | 197 void TestService::AsyncEcho( |
184 return NULL; | 198 MethodCall* method_call, |
199 dbus::ExportedObject::ResponseSender response_sender) { | |
200 // Schedule a call to Echo() to send an asynchronous response after we return. | |
201 message_loop()->PostDelayedTask(FROM_HERE, | |
202 base::Bind(&TestService::Echo, | |
satorux1
2011/11/24 00:17:41
Oh nice, you could reuse this. :)
| |
203 base::Unretained(this), | |
204 method_call, | |
205 response_sender), | |
206 TestTimeouts::tiny_timeout_ms()); | |
207 } | |
208 | |
209 void TestService::BrokenMethod( | |
210 MethodCall* method_call, | |
211 dbus::ExportedObject::ResponseSender response_sender) { | |
212 response_sender.Run(NULL); | |
185 } | 213 } |
186 | 214 |
187 } // namespace dbus | 215 } // namespace dbus |
OLD | NEW |