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_manager.h"
14 #include "dbus/object_path.h"
15 #include "dbus/property.h"
19 void EmptyCallback(bool /* success */) {
26 // Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set, PerformAction,
28 const int TestService::kNumMethodsToExport
= 9;
30 TestService::Options::Options() {
33 TestService::Options::~Options() {
36 TestService::TestService(const Options
& options
)
37 : base::Thread("TestService"),
38 dbus_task_runner_(options
.dbus_task_runner
),
39 on_all_methods_exported_(false, false),
40 num_exported_methods_(0) {
43 TestService::~TestService() {
47 bool TestService::StartService() {
48 base::Thread::Options thread_options
;
49 thread_options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
50 return StartWithOptions(thread_options
);
53 bool TestService::WaitUntilServiceIsStarted() {
54 const base::TimeDelta
timeout(TestTimeouts::action_max_timeout());
55 // Wait until all methods are exported.
56 return on_all_methods_exported_
.TimedWait(timeout
);
59 void TestService::ShutdownAndBlock() {
60 message_loop()->PostTask(
62 base::Bind(&TestService::ShutdownAndBlockInternal
,
63 base::Unretained(this)));
66 bool TestService::HasDBusThread() {
67 return bus_
->HasDBusThread();
70 void TestService::ShutdownAndBlockInternal() {
72 bus_
->ShutdownOnDBusThreadAndBlock();
74 bus_
->ShutdownAndBlock();
77 void TestService::SendTestSignal(const std::string
& message
) {
78 message_loop()->PostTask(
80 base::Bind(&TestService::SendTestSignalInternal
,
81 base::Unretained(this),
85 void TestService::SendTestSignalFromRoot(const std::string
& message
) {
86 message_loop()->PostTask(
88 base::Bind(&TestService::SendTestSignalFromRootInternal
,
89 base::Unretained(this),
93 void TestService::SendTestSignalInternal(const std::string
& message
) {
94 Signal
signal("org.chromium.TestInterface", "Test");
95 MessageWriter
writer(&signal
);
96 writer
.AppendString(message
);
97 exported_object_
->SendSignal(&signal
);
100 void TestService::SendTestSignalFromRootInternal(const std::string
& message
) {
101 Signal
signal("org.chromium.TestInterface", "Test");
102 MessageWriter
writer(&signal
);
103 writer
.AppendString(message
);
105 bus_
->RequestOwnership("org.chromium.TestService",
106 base::Bind(&TestService::OnOwnership
,
107 base::Unretained(this),
108 base::Bind(&EmptyCallback
)));
110 // Use "/" just like dbus-send does.
111 ExportedObject
* root_object
= bus_
->GetExportedObject(ObjectPath("/"));
112 root_object
->SendSignal(&signal
);
115 void TestService::RequestOwnership(base::Callback
<void(bool)> callback
) {
116 message_loop()->PostTask(
118 base::Bind(&TestService::RequestOwnershipInternal
,
119 base::Unretained(this),
123 void TestService::RequestOwnershipInternal(
124 base::Callback
<void(bool)> callback
) {
125 bus_
->RequestOwnership("org.chromium.TestService",
126 base::Bind(&TestService::OnOwnership
,
127 base::Unretained(this),
131 void TestService::OnOwnership(base::Callback
<void(bool)> callback
,
132 const std::string
& service_name
,
134 has_ownership_
= success
;
135 LOG_IF(ERROR
, !success
) << "Failed to own: " << service_name
;
136 callback
.Run(success
);
139 void TestService::OnExported(const std::string
& interface_name
,
140 const std::string
& method_name
,
143 LOG(ERROR
) << "Failed to export: " << interface_name
<< "."
145 // Returning here will make WaitUntilServiceIsStarted() to time out
150 ++num_exported_methods_
;
151 if (num_exported_methods_
== kNumMethodsToExport
)
152 on_all_methods_exported_
.Signal();
155 void TestService::Run(base::MessageLoop
* message_loop
) {
156 Bus::Options bus_options
;
157 bus_options
.bus_type
= Bus::SESSION
;
158 bus_options
.connection_type
= Bus::PRIVATE
;
159 bus_options
.dbus_task_runner
= dbus_task_runner_
;
160 bus_
= new Bus(bus_options
);
162 bus_
->RequestOwnership("org.chromium.TestService",
163 base::Bind(&TestService::OnOwnership
,
164 base::Unretained(this),
165 base::Bind(&EmptyCallback
)));
167 exported_object_
= bus_
->GetExportedObject(
168 ObjectPath("/org/chromium/TestObject"));
171 exported_object_
->ExportMethod(
172 "org.chromium.TestInterface",
174 base::Bind(&TestService::Echo
,
175 base::Unretained(this)),
176 base::Bind(&TestService::OnExported
,
177 base::Unretained(this)));
180 exported_object_
->ExportMethod(
181 "org.chromium.TestInterface",
183 base::Bind(&TestService::SlowEcho
,
184 base::Unretained(this)),
185 base::Bind(&TestService::OnExported
,
186 base::Unretained(this)));
189 exported_object_
->ExportMethod(
190 "org.chromium.TestInterface",
192 base::Bind(&TestService::AsyncEcho
,
193 base::Unretained(this)),
194 base::Bind(&TestService::OnExported
,
195 base::Unretained(this)));
198 exported_object_
->ExportMethod(
199 "org.chromium.TestInterface",
201 base::Bind(&TestService::BrokenMethod
,
202 base::Unretained(this)),
203 base::Bind(&TestService::OnExported
,
204 base::Unretained(this)));
207 exported_object_
->ExportMethod(
208 "org.chromium.TestInterface",
210 base::Bind(&TestService::PerformAction
,
211 base::Unretained(this)),
212 base::Bind(&TestService::OnExported
,
213 base::Unretained(this)));
216 exported_object_
->ExportMethod(
217 kPropertiesInterface
,
219 base::Bind(&TestService::GetAllProperties
,
220 base::Unretained(this)),
221 base::Bind(&TestService::OnExported
,
222 base::Unretained(this)));
225 exported_object_
->ExportMethod(
226 kPropertiesInterface
,
228 base::Bind(&TestService::GetProperty
,
229 base::Unretained(this)),
230 base::Bind(&TestService::OnExported
,
231 base::Unretained(this)));
234 exported_object_
->ExportMethod(
235 kPropertiesInterface
,
237 base::Bind(&TestService::SetProperty
,
238 base::Unretained(this)),
239 base::Bind(&TestService::OnExported
,
240 base::Unretained(this)));
243 exported_object_manager_
= bus_
->GetExportedObject(
244 ObjectPath("/org/chromium/TestService"));
246 exported_object_manager_
->ExportMethod(
247 kObjectManagerInterface
,
248 kObjectManagerGetManagedObjects
,
249 base::Bind(&TestService::GetManagedObjects
,
250 base::Unretained(this)),
251 base::Bind(&TestService::OnExported
,
252 base::Unretained(this)));
255 // Just print an error message as we don't want to crash tests.
256 // Tests will fail at a call to WaitUntilServiceIsStarted().
257 if (num_methods
!= kNumMethodsToExport
) {
258 LOG(ERROR
) << "The number of methods does not match";
263 void TestService::Echo(MethodCall
* method_call
,
264 ExportedObject::ResponseSender response_sender
) {
265 MessageReader
reader(method_call
);
266 std::string text_message
;
267 if (!reader
.PopString(&text_message
)) {
268 response_sender
.Run(scoped_ptr
<Response
>());
272 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
273 MessageWriter
writer(response
.get());
274 writer
.AppendString(text_message
);
275 response_sender
.Run(response
.Pass());
278 void TestService::SlowEcho(MethodCall
* method_call
,
279 ExportedObject::ResponseSender response_sender
) {
280 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
281 Echo(method_call
, response_sender
);
284 void TestService::AsyncEcho(MethodCall
* method_call
,
285 ExportedObject::ResponseSender response_sender
) {
286 // Schedule a call to Echo() to send an asynchronous response after we return.
287 message_loop()->PostDelayedTask(FROM_HERE
,
288 base::Bind(&TestService::Echo
,
289 base::Unretained(this),
292 TestTimeouts::tiny_timeout());
295 void TestService::BrokenMethod(MethodCall
* method_call
,
296 ExportedObject::ResponseSender response_sender
) {
297 response_sender
.Run(scoped_ptr
<Response
>());
301 void TestService::GetAllProperties(
302 MethodCall
* method_call
,
303 ExportedObject::ResponseSender response_sender
) {
304 MessageReader
reader(method_call
);
305 std::string interface
;
306 if (!reader
.PopString(&interface
)) {
307 response_sender
.Run(scoped_ptr
<Response
>());
311 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
312 MessageWriter
writer(response
.get());
314 AddPropertiesToWriter(&writer
);
316 response_sender
.Run(response
.Pass());
319 void TestService::GetProperty(MethodCall
* method_call
,
320 ExportedObject::ResponseSender response_sender
) {
321 MessageReader
reader(method_call
);
322 std::string interface
;
323 if (!reader
.PopString(&interface
)) {
324 response_sender
.Run(scoped_ptr
<Response
>());
329 if (!reader
.PopString(&name
)) {
330 response_sender
.Run(scoped_ptr
<Response
>());
334 if (name
== "Name") {
335 // Return the previous value for the "Name" property:
336 // Variant<"TestService">
337 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
338 MessageWriter
writer(response
.get());
340 writer
.AppendVariantOfString("TestService");
342 response_sender
.Run(response
.Pass());
343 } else if (name
== "Version") {
344 // Return a new value for the "Version" property:
346 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
347 MessageWriter
writer(response
.get());
349 writer
.AppendVariantOfInt16(20);
351 response_sender
.Run(response
.Pass());
352 } else if (name
== "Methods") {
353 // Return the previous value for the "Methods" property:
354 // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
355 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
356 MessageWriter
writer(response
.get());
357 MessageWriter
variant_writer(NULL
);
358 MessageWriter
variant_array_writer(NULL
);
360 writer
.OpenVariant("as", &variant_writer
);
361 variant_writer
.OpenArray("s", &variant_array_writer
);
362 variant_array_writer
.AppendString("Echo");
363 variant_array_writer
.AppendString("SlowEcho");
364 variant_array_writer
.AppendString("AsyncEcho");
365 variant_array_writer
.AppendString("BrokenMethod");
366 variant_writer
.CloseContainer(&variant_array_writer
);
367 writer
.CloseContainer(&variant_writer
);
369 response_sender
.Run(response
.Pass());
370 } else if (name
== "Objects") {
371 // Return the previous value for the "Objects" property:
372 // Variant<[objectpath:"/TestObjectPath"]>
373 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
374 MessageWriter
writer(response
.get());
375 MessageWriter
variant_writer(NULL
);
376 MessageWriter
variant_array_writer(NULL
);
378 writer
.OpenVariant("ao", &variant_writer
);
379 variant_writer
.OpenArray("o", &variant_array_writer
);
380 variant_array_writer
.AppendObjectPath(ObjectPath("/TestObjectPath"));
381 variant_writer
.CloseContainer(&variant_array_writer
);
382 writer
.CloseContainer(&variant_writer
);
384 response_sender
.Run(response
.Pass());
387 response_sender
.Run(scoped_ptr
<Response
>());
392 void TestService::SetProperty(MethodCall
* method_call
,
393 ExportedObject::ResponseSender response_sender
) {
394 MessageReader
reader(method_call
);
395 std::string interface
;
396 if (!reader
.PopString(&interface
)) {
397 response_sender
.Run(scoped_ptr
<Response
>());
402 if (!reader
.PopString(&name
)) {
403 response_sender
.Run(scoped_ptr
<Response
>());
407 if (name
!= "Name") {
408 response_sender
.Run(scoped_ptr
<Response
>());
413 if (!reader
.PopVariantOfString(&value
)) {
414 response_sender
.Run(scoped_ptr
<Response
>());
418 SendPropertyChangedSignal(value
);
420 response_sender
.Run(Response::FromMethodCall(method_call
));
423 void TestService::PerformAction(
424 MethodCall
* method_call
,
425 ExportedObject::ResponseSender response_sender
) {
426 MessageReader
reader(method_call
);
428 ObjectPath object_path
;
429 if (!reader
.PopString(&action
) || !reader
.PopObjectPath(&object_path
)) {
430 response_sender
.Run(scoped_ptr
<Response
>());
434 if (action
== "AddObject")
435 AddObject(object_path
);
436 else if (action
== "RemoveObject")
437 RemoveObject(object_path
);
439 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
440 response_sender
.Run(response
.Pass());
443 void TestService::GetManagedObjects(
444 MethodCall
* method_call
,
445 ExportedObject::ResponseSender response_sender
) {
446 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
447 MessageWriter
writer(response
.get());
449 // The managed objects response is a dictionary of object paths identifying
450 // the object(s) with a dictionary of strings identifying the interface(s)
451 // they implement and then a dictionary of property values.
453 // Thus this looks something like:
456 // "/org/chromium/TestObject": {
457 // "org.chromium.TestInterface": { /* Properties */ }
462 MessageWriter
array_writer(NULL
);
463 MessageWriter
dict_entry_writer(NULL
);
464 MessageWriter
object_array_writer(NULL
);
465 MessageWriter
object_dict_entry_writer(NULL
);
467 writer
.OpenArray("{oa{sa{sv}}}", &array_writer
);
469 array_writer
.OpenDictEntry(&dict_entry_writer
);
470 dict_entry_writer
.AppendObjectPath(ObjectPath("/org/chromium/TestObject"));
471 dict_entry_writer
.OpenArray("{sa{sv}}", &object_array_writer
);
473 object_array_writer
.OpenDictEntry(&object_dict_entry_writer
);
474 object_dict_entry_writer
.AppendString("org.chromium.TestInterface");
475 AddPropertiesToWriter(&object_dict_entry_writer
);
476 object_array_writer
.CloseContainer(&object_dict_entry_writer
);
478 dict_entry_writer
.CloseContainer(&object_array_writer
);
480 array_writer
.CloseContainer(&dict_entry_writer
);
481 writer
.CloseContainer(&array_writer
);
483 response_sender
.Run(response
.Pass());
486 void TestService::AddPropertiesToWriter(MessageWriter
* writer
) {
487 // The properties response is a dictionary of strings identifying the
488 // property and a variant containing the property value. We return all
489 // of the properties, thus the response is:
492 // "Name": Variant<"TestService">,
493 // "Version": Variant<10>,
494 // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
495 // "Objects": Variant<[objectpath:"/TestObjectPath"]>
498 MessageWriter
array_writer(NULL
);
499 MessageWriter
dict_entry_writer(NULL
);
500 MessageWriter
variant_writer(NULL
);
501 MessageWriter
variant_array_writer(NULL
);
503 writer
->OpenArray("{sv}", &array_writer
);
505 array_writer
.OpenDictEntry(&dict_entry_writer
);
506 dict_entry_writer
.AppendString("Name");
507 dict_entry_writer
.AppendVariantOfString("TestService");
508 array_writer
.CloseContainer(&dict_entry_writer
);
510 array_writer
.OpenDictEntry(&dict_entry_writer
);
511 dict_entry_writer
.AppendString("Version");
512 dict_entry_writer
.AppendVariantOfInt16(10);
513 array_writer
.CloseContainer(&dict_entry_writer
);
515 array_writer
.OpenDictEntry(&dict_entry_writer
);
516 dict_entry_writer
.AppendString("Methods");
517 dict_entry_writer
.OpenVariant("as", &variant_writer
);
518 variant_writer
.OpenArray("s", &variant_array_writer
);
519 variant_array_writer
.AppendString("Echo");
520 variant_array_writer
.AppendString("SlowEcho");
521 variant_array_writer
.AppendString("AsyncEcho");
522 variant_array_writer
.AppendString("BrokenMethod");
523 variant_writer
.CloseContainer(&variant_array_writer
);
524 dict_entry_writer
.CloseContainer(&variant_writer
);
525 array_writer
.CloseContainer(&dict_entry_writer
);
527 array_writer
.OpenDictEntry(&dict_entry_writer
);
528 dict_entry_writer
.AppendString("Objects");
529 dict_entry_writer
.OpenVariant("ao", &variant_writer
);
530 variant_writer
.OpenArray("o", &variant_array_writer
);
531 variant_array_writer
.AppendObjectPath(ObjectPath("/TestObjectPath"));
532 variant_writer
.CloseContainer(&variant_array_writer
);
533 dict_entry_writer
.CloseContainer(&variant_writer
);
534 array_writer
.CloseContainer(&dict_entry_writer
);
536 writer
->CloseContainer(&array_writer
);
539 void TestService::AddObject(const ObjectPath
& object_path
) {
540 message_loop()->PostTask(
542 base::Bind(&TestService::AddObjectInternal
,
543 base::Unretained(this),
547 void TestService::AddObjectInternal(const ObjectPath
& object_path
) {
548 Signal
signal(kObjectManagerInterface
, kObjectManagerInterfacesAdded
);
549 MessageWriter
writer(&signal
);
550 writer
.AppendObjectPath(object_path
);
552 MessageWriter
array_writer(NULL
);
553 MessageWriter
dict_entry_writer(NULL
);
555 writer
.OpenArray("{sa{sv}}", &array_writer
);
556 array_writer
.OpenDictEntry(&dict_entry_writer
);
557 dict_entry_writer
.AppendString("org.chromium.TestInterface");
558 AddPropertiesToWriter(&dict_entry_writer
);
559 array_writer
.CloseContainer(&dict_entry_writer
);
560 writer
.CloseContainer(&array_writer
);
562 exported_object_manager_
->SendSignal(&signal
);
565 void TestService::RemoveObject(const ObjectPath
& object_path
) {
566 message_loop()->PostTask(FROM_HERE
,
567 base::Bind(&TestService::RemoveObjectInternal
,
568 base::Unretained(this),
572 void TestService::RemoveObjectInternal(const ObjectPath
& object_path
) {
573 Signal
signal(kObjectManagerInterface
, kObjectManagerInterfacesRemoved
);
574 MessageWriter
writer(&signal
);
576 writer
.AppendObjectPath(object_path
);
578 std::vector
<std::string
> interfaces
;
579 interfaces
.push_back("org.chromium.TestInterface");
580 writer
.AppendArrayOfStrings(interfaces
);
582 exported_object_manager_
->SendSignal(&signal
);
585 void TestService::SendPropertyChangedSignal(const std::string
& name
) {
586 message_loop()->PostTask(
588 base::Bind(&TestService::SendPropertyChangedSignalInternal
,
589 base::Unretained(this),
593 void TestService::SendPropertyChangedSignalInternal(const std::string
& name
) {
594 Signal
signal(kPropertiesInterface
, kPropertiesChanged
);
595 MessageWriter
writer(&signal
);
596 writer
.AppendString("org.chromium.TestInterface");
598 MessageWriter
array_writer(NULL
);
599 MessageWriter
dict_entry_writer(NULL
);
601 writer
.OpenArray("{sv}", &array_writer
);
602 array_writer
.OpenDictEntry(&dict_entry_writer
);
603 dict_entry_writer
.AppendString("Name");
604 dict_entry_writer
.AppendVariantOfString(name
);
605 array_writer
.CloseContainer(&dict_entry_writer
);
606 writer
.CloseContainer(&array_writer
);
608 exported_object_
->SendSignal(&signal
);