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
= 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 dbus::Signal
signal("org.chromium.TestInterface", "Test");
95 dbus::MessageWriter
writer(&signal
);
96 writer
.AppendString(message
);
97 exported_object_
->SendSignal(&signal
);
100 void TestService::SendTestSignalFromRootInternal(const std::string
& message
) {
101 dbus::Signal
signal("org.chromium.TestInterface", "Test");
102 dbus::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
=
112 bus_
->GetExportedObject(dbus::ObjectPath("/"));
113 root_object
->SendSignal(&signal
);
116 void TestService::RequestOwnership(base::Callback
<void(bool)> callback
) {
117 message_loop()->PostTask(
119 base::Bind(&TestService::RequestOwnershipInternal
,
120 base::Unretained(this),
124 void TestService::RequestOwnershipInternal(
125 base::Callback
<void(bool)> callback
) {
126 bus_
->RequestOwnership("org.chromium.TestService",
127 base::Bind(&TestService::OnOwnership
,
128 base::Unretained(this),
132 void TestService::OnOwnership(base::Callback
<void(bool)> callback
,
133 const std::string
& service_name
,
135 has_ownership_
= success
;
136 LOG_IF(ERROR
, !success
) << "Failed to own: " << service_name
;
137 callback
.Run(success
);
140 void TestService::OnExported(const std::string
& interface_name
,
141 const std::string
& method_name
,
144 LOG(ERROR
) << "Failed to export: " << interface_name
<< "."
146 // Returning here will make WaitUntilServiceIsStarted() to time out
151 ++num_exported_methods_
;
152 if (num_exported_methods_
== kNumMethodsToExport
)
153 on_all_methods_exported_
.Signal();
156 void TestService::Run(MessageLoop
* message_loop
) {
157 Bus::Options bus_options
;
158 bus_options
.bus_type
= Bus::SESSION
;
159 bus_options
.connection_type
= Bus::PRIVATE
;
160 bus_options
.dbus_task_runner
= dbus_task_runner_
;
161 bus_
= new Bus(bus_options
);
163 bus_
->RequestOwnership("org.chromium.TestService",
164 base::Bind(&TestService::OnOwnership
,
165 base::Unretained(this),
166 base::Bind(&EmptyCallback
)));
168 exported_object_
= bus_
->GetExportedObject(
169 dbus::ObjectPath("/org/chromium/TestObject"));
172 exported_object_
->ExportMethod(
173 "org.chromium.TestInterface",
175 base::Bind(&TestService::Echo
,
176 base::Unretained(this)),
177 base::Bind(&TestService::OnExported
,
178 base::Unretained(this)));
181 exported_object_
->ExportMethod(
182 "org.chromium.TestInterface",
184 base::Bind(&TestService::SlowEcho
,
185 base::Unretained(this)),
186 base::Bind(&TestService::OnExported
,
187 base::Unretained(this)));
190 exported_object_
->ExportMethod(
191 "org.chromium.TestInterface",
193 base::Bind(&TestService::AsyncEcho
,
194 base::Unretained(this)),
195 base::Bind(&TestService::OnExported
,
196 base::Unretained(this)));
199 exported_object_
->ExportMethod(
200 "org.chromium.TestInterface",
202 base::Bind(&TestService::BrokenMethod
,
203 base::Unretained(this)),
204 base::Bind(&TestService::OnExported
,
205 base::Unretained(this)));
208 exported_object_
->ExportMethod(
209 "org.chromium.TestInterface",
211 base::Bind(&TestService::PerformAction
,
212 base::Unretained(this)),
213 base::Bind(&TestService::OnExported
,
214 base::Unretained(this)));
217 exported_object_
->ExportMethod(
218 kPropertiesInterface
,
220 base::Bind(&TestService::GetAllProperties
,
221 base::Unretained(this)),
222 base::Bind(&TestService::OnExported
,
223 base::Unretained(this)));
226 exported_object_
->ExportMethod(
227 kPropertiesInterface
,
229 base::Bind(&TestService::GetProperty
,
230 base::Unretained(this)),
231 base::Bind(&TestService::OnExported
,
232 base::Unretained(this)));
235 exported_object_
->ExportMethod(
236 kPropertiesInterface
,
238 base::Bind(&TestService::SetProperty
,
239 base::Unretained(this)),
240 base::Bind(&TestService::OnExported
,
241 base::Unretained(this)));
244 exported_object_manager_
= bus_
->GetExportedObject(
245 dbus::ObjectPath("/org/chromium/TestService"));
247 exported_object_manager_
->ExportMethod(
248 kObjectManagerInterface
,
249 kObjectManagerGetManagedObjects
,
250 base::Bind(&TestService::GetManagedObjects
,
251 base::Unretained(this)),
252 base::Bind(&TestService::OnExported
,
253 base::Unretained(this)));
256 // Just print an error message as we don't want to crash tests.
257 // Tests will fail at a call to WaitUntilServiceIsStarted().
258 if (num_methods
!= kNumMethodsToExport
) {
259 LOG(ERROR
) << "The number of methods does not match";
264 void TestService::Echo(MethodCall
* method_call
,
265 dbus::ExportedObject::ResponseSender response_sender
) {
266 MessageReader
reader(method_call
);
267 std::string text_message
;
268 if (!reader
.PopString(&text_message
)) {
269 response_sender
.Run(scoped_ptr
<dbus::Response
>());
273 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
274 MessageWriter
writer(response
.get());
275 writer
.AppendString(text_message
);
276 response_sender
.Run(response
.Pass());
279 void TestService::SlowEcho(
280 MethodCall
* method_call
,
281 dbus::ExportedObject::ResponseSender response_sender
) {
282 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
283 Echo(method_call
, response_sender
);
286 void TestService::AsyncEcho(
287 MethodCall
* method_call
,
288 dbus::ExportedObject::ResponseSender response_sender
) {
289 // Schedule a call to Echo() to send an asynchronous response after we return.
290 message_loop()->PostDelayedTask(FROM_HERE
,
291 base::Bind(&TestService::Echo
,
292 base::Unretained(this),
295 TestTimeouts::tiny_timeout());
298 void TestService::BrokenMethod(
299 MethodCall
* method_call
,
300 dbus::ExportedObject::ResponseSender response_sender
) {
301 response_sender
.Run(scoped_ptr
<dbus::Response
>());
305 void TestService::GetAllProperties(
306 MethodCall
* method_call
,
307 dbus::ExportedObject::ResponseSender response_sender
) {
308 MessageReader
reader(method_call
);
309 std::string interface
;
310 if (!reader
.PopString(&interface
)) {
311 response_sender
.Run(scoped_ptr
<dbus::Response
>());
315 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
316 MessageWriter
writer(response
.get());
318 AddPropertiesToWriter(&writer
);
320 response_sender
.Run(response
.Pass());
323 void TestService::GetProperty(
324 MethodCall
* method_call
,
325 dbus::ExportedObject::ResponseSender response_sender
) {
326 MessageReader
reader(method_call
);
327 std::string interface
;
328 if (!reader
.PopString(&interface
)) {
329 response_sender
.Run(scoped_ptr
<dbus::Response
>());
334 if (!reader
.PopString(&name
)) {
335 response_sender
.Run(scoped_ptr
<dbus::Response
>());
339 if (name
== "Name") {
340 // Return the previous value for the "Name" property:
341 // Variant<"TestService">
342 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
343 MessageWriter
writer(response
.get());
345 writer
.AppendVariantOfString("TestService");
347 response_sender
.Run(response
.Pass());
348 } else if (name
== "Version") {
349 // Return a new value for the "Version" property:
351 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
352 MessageWriter
writer(response
.get());
354 writer
.AppendVariantOfInt16(20);
356 response_sender
.Run(response
.Pass());
357 } else if (name
== "Methods") {
358 // Return the previous value for the "Methods" property:
359 // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
360 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
361 MessageWriter
writer(response
.get());
362 MessageWriter
variant_writer(NULL
);
363 MessageWriter
variant_array_writer(NULL
);
365 writer
.OpenVariant("as", &variant_writer
);
366 variant_writer
.OpenArray("s", &variant_array_writer
);
367 variant_array_writer
.AppendString("Echo");
368 variant_array_writer
.AppendString("SlowEcho");
369 variant_array_writer
.AppendString("AsyncEcho");
370 variant_array_writer
.AppendString("BrokenMethod");
371 variant_writer
.CloseContainer(&variant_array_writer
);
372 writer
.CloseContainer(&variant_writer
);
374 response_sender
.Run(response
.Pass());
375 } else if (name
== "Objects") {
376 // Return the previous value for the "Objects" property:
377 // Variant<[objectpath:"/TestObjectPath"]>
378 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
379 MessageWriter
writer(response
.get());
380 MessageWriter
variant_writer(NULL
);
381 MessageWriter
variant_array_writer(NULL
);
383 writer
.OpenVariant("ao", &variant_writer
);
384 variant_writer
.OpenArray("o", &variant_array_writer
);
385 variant_array_writer
.AppendObjectPath(dbus::ObjectPath("/TestObjectPath"));
386 variant_writer
.CloseContainer(&variant_array_writer
);
387 writer
.CloseContainer(&variant_writer
);
389 response_sender
.Run(response
.Pass());
392 response_sender
.Run(scoped_ptr
<dbus::Response
>());
397 void TestService::SetProperty(
398 MethodCall
* method_call
,
399 dbus::ExportedObject::ResponseSender response_sender
) {
400 MessageReader
reader(method_call
);
401 std::string interface
;
402 if (!reader
.PopString(&interface
)) {
403 response_sender
.Run(scoped_ptr
<dbus::Response
>());
408 if (!reader
.PopString(&name
)) {
409 response_sender
.Run(scoped_ptr
<dbus::Response
>());
413 if (name
!= "Name") {
414 response_sender
.Run(scoped_ptr
<dbus::Response
>());
419 if (!reader
.PopVariantOfString(&value
)) {
420 response_sender
.Run(scoped_ptr
<dbus::Response
>());
424 SendPropertyChangedSignal(value
);
426 response_sender
.Run(Response::FromMethodCall(method_call
));
429 void TestService::PerformAction(
430 MethodCall
* method_call
,
431 dbus::ExportedObject::ResponseSender response_sender
) {
432 MessageReader
reader(method_call
);
434 dbus::ObjectPath object_path
;
435 if (!reader
.PopString(&action
) || !reader
.PopObjectPath(&object_path
)) {
436 response_sender
.Run(scoped_ptr
<dbus::Response
>());
440 if (action
== "AddObject")
441 AddObject(object_path
);
442 else if (action
== "RemoveObject")
443 RemoveObject(object_path
);
445 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
446 response_sender
.Run(response
.Pass());
449 void TestService::GetManagedObjects(
450 MethodCall
* method_call
,
451 dbus::ExportedObject::ResponseSender response_sender
) {
452 scoped_ptr
<Response
> response
= Response::FromMethodCall(method_call
);
453 MessageWriter
writer(response
.get());
455 // The managed objects response is a dictionary of object paths identifying
456 // the object(s) with a dictionary of strings identifying the interface(s)
457 // they implement and then a dictionary of property values.
459 // Thus this looks something like:
462 // "/org/chromium/TestObject": {
463 // "org.chromium.TestInterface": { /* Properties */ }
468 MessageWriter
array_writer(NULL
);
469 MessageWriter
dict_entry_writer(NULL
);
470 MessageWriter
object_array_writer(NULL
);
471 MessageWriter
object_dict_entry_writer(NULL
);
473 writer
.OpenArray("{oa{sa{sv}}}", &array_writer
);
475 array_writer
.OpenDictEntry(&dict_entry_writer
);
476 dict_entry_writer
.AppendObjectPath(
477 dbus::ObjectPath("/org/chromium/TestObject"));
478 dict_entry_writer
.OpenArray("{sa{sv}}", &object_array_writer
);
480 object_array_writer
.OpenDictEntry(&object_dict_entry_writer
);
481 object_dict_entry_writer
.AppendString("org.chromium.TestInterface");
482 AddPropertiesToWriter(&object_dict_entry_writer
);
483 object_array_writer
.CloseContainer(&object_dict_entry_writer
);
485 dict_entry_writer
.CloseContainer(&object_array_writer
);
487 array_writer
.CloseContainer(&dict_entry_writer
);
488 writer
.CloseContainer(&array_writer
);
490 response_sender
.Run(response
.Pass());
493 void TestService::AddPropertiesToWriter(MessageWriter
* writer
)
495 // The properties response is a dictionary of strings identifying the
496 // property and a variant containing the property value. We return all
497 // of the properties, thus the response is:
500 // "Name": Variant<"TestService">,
501 // "Version": Variant<10>,
502 // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
503 // "Objects": Variant<[objectpath:"/TestObjectPath"]>
506 MessageWriter
array_writer(NULL
);
507 MessageWriter
dict_entry_writer(NULL
);
508 MessageWriter
variant_writer(NULL
);
509 MessageWriter
variant_array_writer(NULL
);
511 writer
->OpenArray("{sv}", &array_writer
);
513 array_writer
.OpenDictEntry(&dict_entry_writer
);
514 dict_entry_writer
.AppendString("Name");
515 dict_entry_writer
.AppendVariantOfString("TestService");
516 array_writer
.CloseContainer(&dict_entry_writer
);
518 array_writer
.OpenDictEntry(&dict_entry_writer
);
519 dict_entry_writer
.AppendString("Version");
520 dict_entry_writer
.AppendVariantOfInt16(10);
521 array_writer
.CloseContainer(&dict_entry_writer
);
523 array_writer
.OpenDictEntry(&dict_entry_writer
);
524 dict_entry_writer
.AppendString("Methods");
525 dict_entry_writer
.OpenVariant("as", &variant_writer
);
526 variant_writer
.OpenArray("s", &variant_array_writer
);
527 variant_array_writer
.AppendString("Echo");
528 variant_array_writer
.AppendString("SlowEcho");
529 variant_array_writer
.AppendString("AsyncEcho");
530 variant_array_writer
.AppendString("BrokenMethod");
531 variant_writer
.CloseContainer(&variant_array_writer
);
532 dict_entry_writer
.CloseContainer(&variant_writer
);
533 array_writer
.CloseContainer(&dict_entry_writer
);
535 array_writer
.OpenDictEntry(&dict_entry_writer
);
536 dict_entry_writer
.AppendString("Objects");
537 dict_entry_writer
.OpenVariant("ao", &variant_writer
);
538 variant_writer
.OpenArray("o", &variant_array_writer
);
539 variant_array_writer
.AppendObjectPath(dbus::ObjectPath("/TestObjectPath"));
540 variant_writer
.CloseContainer(&variant_array_writer
);
541 dict_entry_writer
.CloseContainer(&variant_writer
);
542 array_writer
.CloseContainer(&dict_entry_writer
);
544 writer
->CloseContainer(&array_writer
);
547 void TestService::AddObject(const dbus::ObjectPath
& object_path
) {
548 message_loop()->PostTask(
550 base::Bind(&TestService::AddObjectInternal
,
551 base::Unretained(this),
555 void TestService::AddObjectInternal(const dbus::ObjectPath
& object_path
) {
556 dbus::Signal
signal(kObjectManagerInterface
, kObjectManagerInterfacesAdded
);
557 dbus::MessageWriter
writer(&signal
);
558 writer
.AppendObjectPath(object_path
);
560 MessageWriter
array_writer(NULL
);
561 MessageWriter
dict_entry_writer(NULL
);
563 writer
.OpenArray("{sa{sv}}", &array_writer
);
564 array_writer
.OpenDictEntry(&dict_entry_writer
);
565 dict_entry_writer
.AppendString("org.chromium.TestInterface");
566 AddPropertiesToWriter(&dict_entry_writer
);
567 array_writer
.CloseContainer(&dict_entry_writer
);
568 writer
.CloseContainer(&array_writer
);
570 exported_object_manager_
->SendSignal(&signal
);
573 void TestService::RemoveObject(const dbus::ObjectPath
& object_path
) {
574 message_loop()->PostTask(
576 base::Bind(&TestService::RemoveObjectInternal
,
577 base::Unretained(this),
581 void TestService::RemoveObjectInternal(const dbus::ObjectPath
& object_path
) {
582 dbus::Signal
signal(kObjectManagerInterface
, kObjectManagerInterfacesRemoved
);
583 dbus::MessageWriter
writer(&signal
);
585 writer
.AppendObjectPath(object_path
);
587 std::vector
<std::string
> interfaces
;
588 interfaces
.push_back("org.chromium.TestInterface");
589 writer
.AppendArrayOfStrings(interfaces
);
591 exported_object_manager_
->SendSignal(&signal
);
594 void TestService::SendPropertyChangedSignal(const std::string
& name
) {
595 message_loop()->PostTask(
597 base::Bind(&TestService::SendPropertyChangedSignalInternal
,
598 base::Unretained(this),
602 void TestService::SendPropertyChangedSignalInternal(const std::string
& name
) {
603 dbus::Signal
signal(kPropertiesInterface
, kPropertiesChanged
);
604 dbus::MessageWriter
writer(&signal
);
605 writer
.AppendString("org.chromium.TestInterface");
607 MessageWriter
array_writer(NULL
);
608 MessageWriter
dict_entry_writer(NULL
);
610 writer
.OpenArray("{sv}", &array_writer
);
611 array_writer
.OpenDictEntry(&dict_entry_writer
);
612 dict_entry_writer
.AppendString("Name");
613 dict_entry_writer
.AppendVariantOfString(name
);
614 array_writer
.CloseContainer(&dict_entry_writer
);
615 writer
.CloseContainer(&array_writer
);
617 exported_object_
->SendSignal(&signal
);