1 // Copyright (c) 2012 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.
5 #include "dbus/test_service.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/threading/platform_thread.h"
11 #include "dbus/exported_object.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/property.h"
18 void EmptyCallback(bool /* success */) {
25 // Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set.
26 const int TestService::kNumMethodsToExport
= 7;
28 TestService::Options::Options() {
31 TestService::Options::~Options() {
34 TestService::TestService(const Options
& options
)
35 : base::Thread("TestService"),
36 dbus_thread_message_loop_proxy_(options
.dbus_thread_message_loop_proxy
),
37 on_all_methods_exported_(false, false),
38 num_exported_methods_(0) {
41 TestService::~TestService() {
45 bool TestService::StartService() {
46 base::Thread::Options thread_options
;
47 thread_options
.message_loop_type
= MessageLoop::TYPE_IO
;
48 return StartWithOptions(thread_options
);
51 bool TestService::WaitUntilServiceIsStarted() {
52 const base::TimeDelta
timeout(TestTimeouts::action_max_timeout());
53 // Wait until all methods are exported.
54 return on_all_methods_exported_
.TimedWait(timeout
);
57 void TestService::ShutdownAndBlock() {
58 message_loop()->PostTask(
60 base::Bind(&TestService::ShutdownAndBlockInternal
,
61 base::Unretained(this)));
64 bool TestService::HasDBusThread() {
65 return bus_
->HasDBusThread();
68 void TestService::ShutdownAndBlockInternal() {
70 bus_
->ShutdownOnDBusThreadAndBlock();
72 bus_
->ShutdownAndBlock();
75 void TestService::SendTestSignal(const std::string
& message
) {
76 message_loop()->PostTask(
78 base::Bind(&TestService::SendTestSignalInternal
,
79 base::Unretained(this),
83 void TestService::SendTestSignalFromRoot(const std::string
& message
) {
84 message_loop()->PostTask(
86 base::Bind(&TestService::SendTestSignalFromRootInternal
,
87 base::Unretained(this),
91 void TestService::SendTestSignalInternal(const std::string
& message
) {
92 dbus::Signal
signal("org.chromium.TestInterface", "Test");
93 dbus::MessageWriter
writer(&signal
);
94 writer
.AppendString(message
);
95 exported_object_
->SendSignal(&signal
);
98 void TestService::SendTestSignalFromRootInternal(const std::string
& message
) {
99 dbus::Signal
signal("org.chromium.TestInterface", "Test");
100 dbus::MessageWriter
writer(&signal
);
101 writer
.AppendString(message
);
103 bus_
->RequestOwnership("org.chromium.TestService",
104 base::Bind(&TestService::OnOwnership
,
105 base::Unretained(this),
106 base::Bind(&EmptyCallback
)));
108 // Use "/" just like dbus-send does.
109 ExportedObject
* root_object
=
110 bus_
->GetExportedObject(dbus::ObjectPath("/"));
111 root_object
->SendSignal(&signal
);
114 void TestService::RequestOwnership(base::Callback
<void(bool)> callback
) {
115 message_loop()->PostTask(
117 base::Bind(&TestService::RequestOwnershipInternal
,
118 base::Unretained(this),
122 void TestService::RequestOwnershipInternal(
123 base::Callback
<void(bool)> callback
) {
124 bus_
->RequestOwnership("org.chromium.TestService",
125 base::Bind(&TestService::OnOwnership
,
126 base::Unretained(this),
130 void TestService::OnOwnership(base::Callback
<void(bool)> callback
,
131 const std::string
& service_name
,
133 has_ownership_
= success
;
134 LOG_IF(ERROR
, !success
) << "Failed to own: " << service_name
;
135 callback
.Run(success
);
138 void TestService::OnExported(const std::string
& interface_name
,
139 const std::string
& method_name
,
142 LOG(ERROR
) << "Failed to export: " << interface_name
<< "."
144 // Returning here will make WaitUntilServiceIsStarted() to time out
149 ++num_exported_methods_
;
150 if (num_exported_methods_
== kNumMethodsToExport
)
151 on_all_methods_exported_
.Signal();
154 void TestService::Run(MessageLoop
* message_loop
) {
155 Bus::Options bus_options
;
156 bus_options
.bus_type
= Bus::SESSION
;
157 bus_options
.connection_type
= Bus::PRIVATE
;
158 bus_options
.dbus_thread_message_loop_proxy
= dbus_thread_message_loop_proxy_
;
159 bus_
= new Bus(bus_options
);
161 bus_
->RequestOwnership("org.chromium.TestService",
162 base::Bind(&TestService::OnOwnership
,
163 base::Unretained(this),
164 base::Bind(&EmptyCallback
)));
166 exported_object_
= bus_
->GetExportedObject(
167 dbus::ObjectPath("/org/chromium/TestObject"));
170 exported_object_
->ExportMethod(
171 "org.chromium.TestInterface",
173 base::Bind(&TestService::Echo
,
174 base::Unretained(this)),
175 base::Bind(&TestService::OnExported
,
176 base::Unretained(this)));
179 exported_object_
->ExportMethod(
180 "org.chromium.TestInterface",
182 base::Bind(&TestService::SlowEcho
,
183 base::Unretained(this)),
184 base::Bind(&TestService::OnExported
,
185 base::Unretained(this)));
188 exported_object_
->ExportMethod(
189 "org.chromium.TestInterface",
191 base::Bind(&TestService::AsyncEcho
,
192 base::Unretained(this)),
193 base::Bind(&TestService::OnExported
,
194 base::Unretained(this)));
197 exported_object_
->ExportMethod(
198 "org.chromium.TestInterface",
200 base::Bind(&TestService::BrokenMethod
,
201 base::Unretained(this)),
202 base::Bind(&TestService::OnExported
,
203 base::Unretained(this)));
206 exported_object_
->ExportMethod(
207 kPropertiesInterface
,
209 base::Bind(&TestService::GetAllProperties
,
210 base::Unretained(this)),
211 base::Bind(&TestService::OnExported
,
212 base::Unretained(this)));
215 exported_object_
->ExportMethod(
216 kPropertiesInterface
,
218 base::Bind(&TestService::GetProperty
,
219 base::Unretained(this)),
220 base::Bind(&TestService::OnExported
,
221 base::Unretained(this)));
224 exported_object_
->ExportMethod(
225 kPropertiesInterface
,
227 base::Bind(&TestService::SetProperty
,
228 base::Unretained(this)),
229 base::Bind(&TestService::OnExported
,
230 base::Unretained(this)));
233 // Just print an error message as we don't want to crash tests.
234 // Tests will fail at a call to WaitUntilServiceIsStarted().
235 if (num_methods
!= kNumMethodsToExport
) {
236 LOG(ERROR
) << "The number of methods does not match";
241 void TestService::Echo(MethodCall
* method_call
,
242 dbus::ExportedObject::ResponseSender response_sender
) {
243 MessageReader
reader(method_call
);
244 std::string text_message
;
245 if (!reader
.PopString(&text_message
)) {
246 response_sender
.Run(NULL
);
250 Response
* response
= Response::FromMethodCall(method_call
);
251 MessageWriter
writer(response
);
252 writer
.AppendString(text_message
);
253 response_sender
.Run(response
);
256 void TestService::SlowEcho(
257 MethodCall
* method_call
,
258 dbus::ExportedObject::ResponseSender response_sender
) {
259 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
260 Echo(method_call
, response_sender
);
263 void TestService::AsyncEcho(
264 MethodCall
* method_call
,
265 dbus::ExportedObject::ResponseSender response_sender
) {
266 // Schedule a call to Echo() to send an asynchronous response after we return.
267 message_loop()->PostDelayedTask(FROM_HERE
,
268 base::Bind(&TestService::Echo
,
269 base::Unretained(this),
272 TestTimeouts::tiny_timeout());
275 void TestService::BrokenMethod(
276 MethodCall
* method_call
,
277 dbus::ExportedObject::ResponseSender response_sender
) {
278 response_sender
.Run(NULL
);
282 void TestService::GetAllProperties(
283 MethodCall
* method_call
,
284 dbus::ExportedObject::ResponseSender response_sender
) {
285 MessageReader
reader(method_call
);
286 std::string interface
;
287 if (!reader
.PopString(&interface
)) {
288 response_sender
.Run(NULL
);
292 // The properties response is a dictionary of strings identifying the
293 // property and a variant containing the property value. We return all
294 // of the properties, thus the response is:
297 // "Name": Variant<"TestService">,
298 // "Version": Variant<10>,
299 // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
300 // "Objects": Variant<[objectpath:"/TestObjectPath"]>
303 Response
* response
= Response::FromMethodCall(method_call
);
304 MessageWriter
writer(response
);
306 MessageWriter
array_writer(NULL
);
307 MessageWriter
dict_entry_writer(NULL
);
308 MessageWriter
variant_writer(NULL
);
309 MessageWriter
variant_array_writer(NULL
);
311 writer
.OpenArray("{sv}", &array_writer
);
313 array_writer
.OpenDictEntry(&dict_entry_writer
);
314 dict_entry_writer
.AppendString("Name");
315 dict_entry_writer
.AppendVariantOfString("TestService");
316 array_writer
.CloseContainer(&dict_entry_writer
);
318 array_writer
.OpenDictEntry(&dict_entry_writer
);
319 dict_entry_writer
.AppendString("Version");
320 dict_entry_writer
.AppendVariantOfInt16(10);
321 array_writer
.CloseContainer(&dict_entry_writer
);
323 array_writer
.OpenDictEntry(&dict_entry_writer
);
324 dict_entry_writer
.AppendString("Methods");
325 dict_entry_writer
.OpenVariant("as", &variant_writer
);
326 variant_writer
.OpenArray("s", &variant_array_writer
);
327 variant_array_writer
.AppendString("Echo");
328 variant_array_writer
.AppendString("SlowEcho");
329 variant_array_writer
.AppendString("AsyncEcho");
330 variant_array_writer
.AppendString("BrokenMethod");
331 variant_writer
.CloseContainer(&variant_array_writer
);
332 dict_entry_writer
.CloseContainer(&variant_writer
);
333 array_writer
.CloseContainer(&dict_entry_writer
);
335 array_writer
.OpenDictEntry(&dict_entry_writer
);
336 dict_entry_writer
.AppendString("Objects");
337 dict_entry_writer
.OpenVariant("ao", &variant_writer
);
338 variant_writer
.OpenArray("o", &variant_array_writer
);
339 variant_array_writer
.AppendObjectPath(dbus::ObjectPath("/TestObjectPath"));
340 variant_writer
.CloseContainer(&variant_array_writer
);
341 dict_entry_writer
.CloseContainer(&variant_writer
);
342 array_writer
.CloseContainer(&dict_entry_writer
);
344 writer
.CloseContainer(&array_writer
);
346 response_sender
.Run(response
);
349 void TestService::GetProperty(
350 MethodCall
* method_call
,
351 dbus::ExportedObject::ResponseSender response_sender
) {
352 MessageReader
reader(method_call
);
353 std::string interface
;
354 if (!reader
.PopString(&interface
)) {
355 response_sender
.Run(NULL
);
360 if (!reader
.PopString(&name
)) {
361 response_sender
.Run(NULL
);
365 if (name
== "Name") {
366 // Return the previous value for the "Name" property:
367 // Variant<"TestService">
368 Response
* response
= Response::FromMethodCall(method_call
);
369 MessageWriter
writer(response
);
371 writer
.AppendVariantOfString("TestService");
373 response_sender
.Run(response
);
374 } else if (name
== "Version") {
375 // Return a new value for the "Version" property:
377 Response
* response
= Response::FromMethodCall(method_call
);
378 MessageWriter
writer(response
);
380 writer
.AppendVariantOfInt16(20);
382 response_sender
.Run(response
);
383 } else if (name
== "Methods") {
384 // Return the previous value for the "Methods" property:
385 // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
386 Response
* response
= Response::FromMethodCall(method_call
);
387 MessageWriter
writer(response
);
388 MessageWriter
variant_writer(NULL
);
389 MessageWriter
variant_array_writer(NULL
);
391 writer
.OpenVariant("as", &variant_writer
);
392 variant_writer
.OpenArray("s", &variant_array_writer
);
393 variant_array_writer
.AppendString("Echo");
394 variant_array_writer
.AppendString("SlowEcho");
395 variant_array_writer
.AppendString("AsyncEcho");
396 variant_array_writer
.AppendString("BrokenMethod");
397 variant_writer
.CloseContainer(&variant_array_writer
);
398 writer
.CloseContainer(&variant_writer
);
400 response_sender
.Run(response
);
401 } else if (name
== "Objects") {
402 // Return the previous value for the "Objects" property:
403 // Variant<[objectpath:"/TestObjectPath"]>
404 Response
* response
= Response::FromMethodCall(method_call
);
405 MessageWriter
writer(response
);
406 MessageWriter
variant_writer(NULL
);
407 MessageWriter
variant_array_writer(NULL
);
409 writer
.OpenVariant("ao", &variant_writer
);
410 variant_writer
.OpenArray("o", &variant_array_writer
);
411 variant_array_writer
.AppendObjectPath(dbus::ObjectPath("/TestObjectPath"));
412 variant_writer
.CloseContainer(&variant_array_writer
);
413 writer
.CloseContainer(&variant_writer
);
415 response_sender
.Run(response
);
418 response_sender
.Run(NULL
);
423 void TestService::SetProperty(
424 MethodCall
* method_call
,
425 dbus::ExportedObject::ResponseSender response_sender
) {
426 MessageReader
reader(method_call
);
427 std::string interface
;
428 if (!reader
.PopString(&interface
)) {
429 response_sender
.Run(NULL
);
434 if (!reader
.PopString(&name
)) {
435 response_sender
.Run(NULL
);
439 if (name
!= "Name") {
440 response_sender
.Run(NULL
);
445 if (!reader
.PopVariantOfString(&value
)) {
446 response_sender
.Run(NULL
);
450 SendPropertyChangedSignal(value
);
452 Response
* response
= Response::FromMethodCall(method_call
);
453 response_sender
.Run(response
);
456 void TestService::SendPropertyChangedSignal(const std::string
& name
) {
457 message_loop()->PostTask(
459 base::Bind(&TestService::SendPropertyChangedSignalInternal
,
460 base::Unretained(this),
464 void TestService::SendPropertyChangedSignalInternal(const std::string
& name
) {
465 dbus::Signal
signal(kPropertiesInterface
, kPropertiesChanged
);
466 dbus::MessageWriter
writer(&signal
);
467 writer
.AppendString("org.chromium.TestService");
469 MessageWriter
array_writer(NULL
);
470 MessageWriter
dict_entry_writer(NULL
);
472 writer
.OpenArray("{sv}", &array_writer
);
473 array_writer
.OpenDictEntry(&dict_entry_writer
);
474 dict_entry_writer
.AppendString("Name");
475 dict_entry_writer
.AppendVariantOfString(name
);
476 array_writer
.CloseContainer(&dict_entry_writer
);
477 writer
.CloseContainer(&array_writer
);
479 exported_object_
->SendSignal(&signal
);