Mac Video Capture: Avoid enumerating Blackmagic devices via AVFoundation
[chromium-blink-merge.git] / dbus / test_service.cc
blob1ec207024670be30c001fcf853d0f97efdc08f6a
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"
7 #include "base/bind.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/threading/platform_thread.h"
10 #include "dbus/bus.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"
17 namespace {
19 void EmptyCallback(bool /* success */) {
22 } // namespace
24 namespace dbus {
26 // Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set, PerformAction,
27 // GetManagedObjects
28 const int TestService::kNumMethodsToExport = 9;
30 TestService::Options::Options()
31 : request_ownership_options(Bus::REQUIRE_PRIMARY) {
34 TestService::Options::~Options() {
37 TestService::TestService(const Options& options)
38 : base::Thread("TestService"),
39 request_ownership_options_(options.request_ownership_options),
40 dbus_task_runner_(options.dbus_task_runner),
41 on_name_obtained_(false, false),
42 num_exported_methods_(0),
43 send_immediate_properties_changed_(false),
44 has_ownership_(false),
45 exported_object_(NULL),
46 exported_object_manager_(NULL) {
49 TestService::~TestService() {
50 Stop();
53 bool TestService::StartService() {
54 base::Thread::Options thread_options;
55 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
56 return StartWithOptions(thread_options);
59 bool TestService::WaitUntilServiceIsStarted() {
60 const base::TimeDelta timeout(TestTimeouts::action_max_timeout());
61 // Wait until the ownership of the service name is obtained.
62 return on_name_obtained_.TimedWait(timeout);
65 void TestService::ShutdownAndBlock() {
66 message_loop()->PostTask(
67 FROM_HERE,
68 base::Bind(&TestService::ShutdownAndBlockInternal,
69 base::Unretained(this)));
72 bool TestService::HasDBusThread() {
73 return bus_->HasDBusThread();
76 void TestService::ShutdownAndBlockInternal() {
77 if (HasDBusThread())
78 bus_->ShutdownOnDBusThreadAndBlock();
79 else
80 bus_->ShutdownAndBlock();
83 void TestService::SendTestSignal(const std::string& message) {
84 message_loop()->PostTask(
85 FROM_HERE,
86 base::Bind(&TestService::SendTestSignalInternal,
87 base::Unretained(this),
88 message));
91 void TestService::SendTestSignalFromRoot(const std::string& message) {
92 message_loop()->PostTask(
93 FROM_HERE,
94 base::Bind(&TestService::SendTestSignalFromRootInternal,
95 base::Unretained(this),
96 message));
99 void TestService::SendTestSignalInternal(const std::string& message) {
100 Signal signal("org.chromium.TestInterface", "Test");
101 MessageWriter writer(&signal);
102 writer.AppendString(message);
103 exported_object_->SendSignal(&signal);
106 void TestService::SendTestSignalFromRootInternal(const std::string& message) {
107 Signal signal("org.chromium.TestInterface", "Test");
108 MessageWriter writer(&signal);
109 writer.AppendString(message);
111 bus_->RequestOwnership("org.chromium.TestService",
112 request_ownership_options_,
113 base::Bind(&TestService::OnOwnership,
114 base::Unretained(this),
115 base::Bind(&EmptyCallback)));
117 // Use "/" just like dbus-send does.
118 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/"));
119 root_object->SendSignal(&signal);
122 void TestService::RequestOwnership(base::Callback<void(bool)> callback) {
123 message_loop()->PostTask(
124 FROM_HERE,
125 base::Bind(&TestService::RequestOwnershipInternal,
126 base::Unretained(this),
127 callback));
130 void TestService::RequestOwnershipInternal(
131 base::Callback<void(bool)> callback) {
132 bus_->RequestOwnership("org.chromium.TestService",
133 request_ownership_options_,
134 base::Bind(&TestService::OnOwnership,
135 base::Unretained(this),
136 callback));
139 void TestService::OnOwnership(base::Callback<void(bool)> callback,
140 const std::string& service_name,
141 bool success) {
142 has_ownership_ = success;
143 LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
144 callback.Run(success);
146 on_name_obtained_.Signal();
149 void TestService::ReleaseOwnership(base::Closure callback) {
150 bus_->GetDBusTaskRunner()->PostTask(
151 FROM_HERE,
152 base::Bind(&TestService::ReleaseOwnershipInternal,
153 base::Unretained(this),
154 callback));
157 void TestService::ReleaseOwnershipInternal(
158 base::Closure callback) {
159 bus_->ReleaseOwnership("org.chromium.TestService");
160 has_ownership_ = false;
162 bus_->GetOriginTaskRunner()->PostTask(
163 FROM_HERE,
164 callback);
167 void TestService::SetSendImmediatePropertiesChanged() {
168 send_immediate_properties_changed_ = true;
171 void TestService::OnExported(const std::string& interface_name,
172 const std::string& method_name,
173 bool success) {
174 if (!success) {
175 LOG(ERROR) << "Failed to export: " << interface_name << "."
176 << method_name;
177 // Returning here will make WaitUntilServiceIsStarted() to time out
178 // and return false.
179 return;
182 ++num_exported_methods_;
183 if (num_exported_methods_ == kNumMethodsToExport) {
184 // As documented in exported_object.h, the service name should be
185 // requested after all methods are exposed.
186 bus_->RequestOwnership("org.chromium.TestService",
187 request_ownership_options_,
188 base::Bind(&TestService::OnOwnership,
189 base::Unretained(this),
190 base::Bind(&EmptyCallback)));
194 void TestService::Run(base::MessageLoop* message_loop) {
195 Bus::Options bus_options;
196 bus_options.bus_type = Bus::SESSION;
197 bus_options.connection_type = Bus::PRIVATE;
198 bus_options.dbus_task_runner = dbus_task_runner_;
199 bus_ = new Bus(bus_options);
201 exported_object_ = bus_->GetExportedObject(
202 ObjectPath("/org/chromium/TestObject"));
204 int num_methods = 0;
205 exported_object_->ExportMethod(
206 "org.chromium.TestInterface",
207 "Echo",
208 base::Bind(&TestService::Echo,
209 base::Unretained(this)),
210 base::Bind(&TestService::OnExported,
211 base::Unretained(this)));
212 ++num_methods;
214 exported_object_->ExportMethod(
215 "org.chromium.TestInterface",
216 "SlowEcho",
217 base::Bind(&TestService::SlowEcho,
218 base::Unretained(this)),
219 base::Bind(&TestService::OnExported,
220 base::Unretained(this)));
221 ++num_methods;
223 exported_object_->ExportMethod(
224 "org.chromium.TestInterface",
225 "AsyncEcho",
226 base::Bind(&TestService::AsyncEcho,
227 base::Unretained(this)),
228 base::Bind(&TestService::OnExported,
229 base::Unretained(this)));
230 ++num_methods;
232 exported_object_->ExportMethod(
233 "org.chromium.TestInterface",
234 "BrokenMethod",
235 base::Bind(&TestService::BrokenMethod,
236 base::Unretained(this)),
237 base::Bind(&TestService::OnExported,
238 base::Unretained(this)));
239 ++num_methods;
241 exported_object_->ExportMethod(
242 "org.chromium.TestInterface",
243 "PerformAction",
244 base::Bind(&TestService::PerformAction,
245 base::Unretained(this)),
246 base::Bind(&TestService::OnExported,
247 base::Unretained(this)));
248 ++num_methods;
250 exported_object_->ExportMethod(
251 kPropertiesInterface,
252 kPropertiesGetAll,
253 base::Bind(&TestService::GetAllProperties,
254 base::Unretained(this)),
255 base::Bind(&TestService::OnExported,
256 base::Unretained(this)));
257 ++num_methods;
259 exported_object_->ExportMethod(
260 kPropertiesInterface,
261 kPropertiesGet,
262 base::Bind(&TestService::GetProperty,
263 base::Unretained(this)),
264 base::Bind(&TestService::OnExported,
265 base::Unretained(this)));
266 ++num_methods;
268 exported_object_->ExportMethod(
269 kPropertiesInterface,
270 kPropertiesSet,
271 base::Bind(&TestService::SetProperty,
272 base::Unretained(this)),
273 base::Bind(&TestService::OnExported,
274 base::Unretained(this)));
275 ++num_methods;
277 exported_object_manager_ = bus_->GetExportedObject(
278 ObjectPath("/org/chromium/TestService"));
280 exported_object_manager_->ExportMethod(
281 kObjectManagerInterface,
282 kObjectManagerGetManagedObjects,
283 base::Bind(&TestService::GetManagedObjects,
284 base::Unretained(this)),
285 base::Bind(&TestService::OnExported,
286 base::Unretained(this)));
287 ++num_methods;
289 // Just print an error message as we don't want to crash tests.
290 // Tests will fail at a call to WaitUntilServiceIsStarted().
291 if (num_methods != kNumMethodsToExport) {
292 LOG(ERROR) << "The number of methods does not match";
294 message_loop->Run();
297 void TestService::Echo(MethodCall* method_call,
298 ExportedObject::ResponseSender response_sender) {
299 MessageReader reader(method_call);
300 std::string text_message;
301 if (!reader.PopString(&text_message)) {
302 response_sender.Run(scoped_ptr<Response>());
303 return;
306 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
307 MessageWriter writer(response.get());
308 writer.AppendString(text_message);
309 response_sender.Run(response.Pass());
312 void TestService::SlowEcho(MethodCall* method_call,
313 ExportedObject::ResponseSender response_sender) {
314 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
315 Echo(method_call, response_sender);
318 void TestService::AsyncEcho(MethodCall* method_call,
319 ExportedObject::ResponseSender response_sender) {
320 // Schedule a call to Echo() to send an asynchronous response after we return.
321 message_loop()->PostDelayedTask(FROM_HERE,
322 base::Bind(&TestService::Echo,
323 base::Unretained(this),
324 method_call,
325 response_sender),
326 TestTimeouts::tiny_timeout());
329 void TestService::BrokenMethod(MethodCall* method_call,
330 ExportedObject::ResponseSender response_sender) {
331 response_sender.Run(scoped_ptr<Response>());
335 void TestService::GetAllProperties(
336 MethodCall* method_call,
337 ExportedObject::ResponseSender response_sender) {
338 MessageReader reader(method_call);
339 std::string interface;
340 if (!reader.PopString(&interface)) {
341 response_sender.Run(scoped_ptr<Response>());
342 return;
345 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
346 MessageWriter writer(response.get());
348 AddPropertiesToWriter(&writer);
350 response_sender.Run(response.Pass());
353 void TestService::GetProperty(MethodCall* method_call,
354 ExportedObject::ResponseSender response_sender) {
355 MessageReader reader(method_call);
356 std::string interface;
357 if (!reader.PopString(&interface)) {
358 response_sender.Run(scoped_ptr<Response>());
359 return;
362 std::string name;
363 if (!reader.PopString(&name)) {
364 response_sender.Run(scoped_ptr<Response>());
365 return;
368 if (name == "Name") {
369 // Return the previous value for the "Name" property:
370 // Variant<"TestService">
371 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
372 MessageWriter writer(response.get());
374 writer.AppendVariantOfString("TestService");
376 response_sender.Run(response.Pass());
377 } else if (name == "Version") {
378 // Return a new value for the "Version" property:
379 // Variant<20>
380 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
381 MessageWriter writer(response.get());
383 writer.AppendVariantOfInt16(20);
385 response_sender.Run(response.Pass());
386 } else if (name == "Methods") {
387 // Return the previous value for the "Methods" property:
388 // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
389 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
390 MessageWriter writer(response.get());
391 MessageWriter variant_writer(NULL);
392 MessageWriter variant_array_writer(NULL);
394 writer.OpenVariant("as", &variant_writer);
395 variant_writer.OpenArray("s", &variant_array_writer);
396 variant_array_writer.AppendString("Echo");
397 variant_array_writer.AppendString("SlowEcho");
398 variant_array_writer.AppendString("AsyncEcho");
399 variant_array_writer.AppendString("BrokenMethod");
400 variant_writer.CloseContainer(&variant_array_writer);
401 writer.CloseContainer(&variant_writer);
403 response_sender.Run(response.Pass());
404 } else if (name == "Objects") {
405 // Return the previous value for the "Objects" property:
406 // Variant<[objectpath:"/TestObjectPath"]>
407 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
408 MessageWriter writer(response.get());
409 MessageWriter variant_writer(NULL);
410 MessageWriter variant_array_writer(NULL);
412 writer.OpenVariant("ao", &variant_writer);
413 variant_writer.OpenArray("o", &variant_array_writer);
414 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
415 variant_writer.CloseContainer(&variant_array_writer);
416 writer.CloseContainer(&variant_writer);
418 response_sender.Run(response.Pass());
419 } else if (name == "Bytes") {
420 // Return the previous value for the "Bytes" property:
421 // Variant<[0x54, 0x65, 0x73, 0x74]>
422 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
423 MessageWriter writer(response.get());
424 MessageWriter variant_writer(NULL);
425 MessageWriter variant_array_writer(NULL);
427 writer.OpenVariant("ay", &variant_writer);
428 const uint8 bytes[] = { 0x54, 0x65, 0x73, 0x74 };
429 variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
430 writer.CloseContainer(&variant_writer);
432 response_sender.Run(response.Pass());
433 } else {
434 // Return error.
435 response_sender.Run(scoped_ptr<Response>());
436 return;
440 void TestService::SetProperty(MethodCall* method_call,
441 ExportedObject::ResponseSender response_sender) {
442 MessageReader reader(method_call);
443 std::string interface;
444 if (!reader.PopString(&interface)) {
445 response_sender.Run(scoped_ptr<Response>());
446 return;
449 std::string name;
450 if (!reader.PopString(&name)) {
451 response_sender.Run(scoped_ptr<Response>());
452 return;
455 if (name != "Name") {
456 response_sender.Run(scoped_ptr<Response>());
457 return;
460 std::string value;
461 if (!reader.PopVariantOfString(&value)) {
462 response_sender.Run(scoped_ptr<Response>());
463 return;
466 SendPropertyChangedSignal(value);
468 response_sender.Run(Response::FromMethodCall(method_call));
471 void TestService::PerformAction(
472 MethodCall* method_call,
473 ExportedObject::ResponseSender response_sender) {
474 MessageReader reader(method_call);
475 std::string action;
476 ObjectPath object_path;
477 if (!reader.PopString(&action) || !reader.PopObjectPath(&object_path)) {
478 response_sender.Run(scoped_ptr<Response>());
479 return;
482 if (action == "AddObject") {
483 AddObject(object_path);
484 } else if (action == "RemoveObject") {
485 RemoveObject(object_path);
486 } else if (action == "SetSendImmediatePropertiesChanged") {
487 SetSendImmediatePropertiesChanged();
488 } if (action == "ReleaseOwnership") {
489 ReleaseOwnership(base::Bind(&TestService::PerformActionResponse,
490 base::Unretained(this),
491 method_call, response_sender));
492 return;
493 } else if (action == "Ownership") {
494 ReleaseOwnership(base::Bind(&TestService::OwnershipReleased,
495 base::Unretained(this),
496 method_call, response_sender));
497 return;
500 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
501 response_sender.Run(response.Pass());
504 void TestService::PerformActionResponse(
505 MethodCall* method_call,
506 ExportedObject::ResponseSender response_sender) {
507 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
508 response_sender.Run(response.Pass());
511 void TestService::OwnershipReleased(
512 MethodCall* method_call,
513 ExportedObject::ResponseSender response_sender) {
514 RequestOwnership(base::Bind(&TestService::OwnershipRegained,
515 base::Unretained(this),
516 method_call, response_sender));
520 void TestService::OwnershipRegained(
521 MethodCall* method_call,
522 ExportedObject::ResponseSender response_sender,
523 bool success) {
524 PerformActionResponse(method_call, response_sender);
528 void TestService::GetManagedObjects(
529 MethodCall* method_call,
530 ExportedObject::ResponseSender response_sender) {
531 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
532 MessageWriter writer(response.get());
534 // The managed objects response is a dictionary of object paths identifying
535 // the object(s) with a dictionary of strings identifying the interface(s)
536 // they implement and then a dictionary of property values.
538 // Thus this looks something like:
540 // {
541 // "/org/chromium/TestObject": {
542 // "org.chromium.TestInterface": { /* Properties */ }
543 // }
544 // }
547 MessageWriter array_writer(NULL);
548 MessageWriter dict_entry_writer(NULL);
549 MessageWriter object_array_writer(NULL);
550 MessageWriter object_dict_entry_writer(NULL);
552 writer.OpenArray("{oa{sa{sv}}}", &array_writer);
554 array_writer.OpenDictEntry(&dict_entry_writer);
555 dict_entry_writer.AppendObjectPath(ObjectPath("/org/chromium/TestObject"));
556 dict_entry_writer.OpenArray("{sa{sv}}", &object_array_writer);
558 object_array_writer.OpenDictEntry(&object_dict_entry_writer);
559 object_dict_entry_writer.AppendString("org.chromium.TestInterface");
560 AddPropertiesToWriter(&object_dict_entry_writer);
561 object_array_writer.CloseContainer(&object_dict_entry_writer);
563 dict_entry_writer.CloseContainer(&object_array_writer);
565 array_writer.CloseContainer(&dict_entry_writer);
566 writer.CloseContainer(&array_writer);
568 response_sender.Run(response.Pass());
570 if (send_immediate_properties_changed_)
571 SendPropertyChangedSignal("ChangedTestServiceName");
574 void TestService::AddPropertiesToWriter(MessageWriter* writer) {
575 // The properties response is a dictionary of strings identifying the
576 // property and a variant containing the property value. We return all
577 // of the properties, thus the response is:
579 // {
580 // "Name": Variant<"TestService">,
581 // "Version": Variant<10>,
582 // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
583 // "Objects": Variant<[objectpath:"/TestObjectPath"]>
584 // "Bytes": Variant<[0x54, 0x65, 0x73, 0x74]>
585 // }
587 MessageWriter array_writer(NULL);
588 MessageWriter dict_entry_writer(NULL);
589 MessageWriter variant_writer(NULL);
590 MessageWriter variant_array_writer(NULL);
592 writer->OpenArray("{sv}", &array_writer);
594 array_writer.OpenDictEntry(&dict_entry_writer);
595 dict_entry_writer.AppendString("Name");
596 dict_entry_writer.AppendVariantOfString("TestService");
597 array_writer.CloseContainer(&dict_entry_writer);
599 array_writer.OpenDictEntry(&dict_entry_writer);
600 dict_entry_writer.AppendString("Version");
601 dict_entry_writer.AppendVariantOfInt16(10);
602 array_writer.CloseContainer(&dict_entry_writer);
604 array_writer.OpenDictEntry(&dict_entry_writer);
605 dict_entry_writer.AppendString("Methods");
606 dict_entry_writer.OpenVariant("as", &variant_writer);
607 variant_writer.OpenArray("s", &variant_array_writer);
608 variant_array_writer.AppendString("Echo");
609 variant_array_writer.AppendString("SlowEcho");
610 variant_array_writer.AppendString("AsyncEcho");
611 variant_array_writer.AppendString("BrokenMethod");
612 variant_writer.CloseContainer(&variant_array_writer);
613 dict_entry_writer.CloseContainer(&variant_writer);
614 array_writer.CloseContainer(&dict_entry_writer);
616 array_writer.OpenDictEntry(&dict_entry_writer);
617 dict_entry_writer.AppendString("Objects");
618 dict_entry_writer.OpenVariant("ao", &variant_writer);
619 variant_writer.OpenArray("o", &variant_array_writer);
620 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
621 variant_writer.CloseContainer(&variant_array_writer);
622 dict_entry_writer.CloseContainer(&variant_writer);
623 array_writer.CloseContainer(&dict_entry_writer);
625 array_writer.OpenDictEntry(&dict_entry_writer);
626 dict_entry_writer.AppendString("Bytes");
627 dict_entry_writer.OpenVariant("ay", &variant_writer);
628 const uint8 bytes[] = { 0x54, 0x65, 0x73, 0x74 };
629 variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
630 dict_entry_writer.CloseContainer(&variant_writer);
631 array_writer.CloseContainer(&dict_entry_writer);
633 writer->CloseContainer(&array_writer);
636 void TestService::AddObject(const ObjectPath& object_path) {
637 message_loop()->PostTask(
638 FROM_HERE,
639 base::Bind(&TestService::AddObjectInternal,
640 base::Unretained(this),
641 object_path));
644 void TestService::AddObjectInternal(const ObjectPath& object_path) {
645 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded);
646 MessageWriter writer(&signal);
647 writer.AppendObjectPath(object_path);
649 MessageWriter array_writer(NULL);
650 MessageWriter dict_entry_writer(NULL);
652 writer.OpenArray("{sa{sv}}", &array_writer);
653 array_writer.OpenDictEntry(&dict_entry_writer);
654 dict_entry_writer.AppendString("org.chromium.TestInterface");
655 AddPropertiesToWriter(&dict_entry_writer);
656 array_writer.CloseContainer(&dict_entry_writer);
657 writer.CloseContainer(&array_writer);
659 exported_object_manager_->SendSignal(&signal);
662 void TestService::RemoveObject(const ObjectPath& object_path) {
663 message_loop()->PostTask(FROM_HERE,
664 base::Bind(&TestService::RemoveObjectInternal,
665 base::Unretained(this),
666 object_path));
669 void TestService::RemoveObjectInternal(const ObjectPath& object_path) {
670 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved);
671 MessageWriter writer(&signal);
673 writer.AppendObjectPath(object_path);
675 std::vector<std::string> interfaces;
676 interfaces.push_back("org.chromium.TestInterface");
677 writer.AppendArrayOfStrings(interfaces);
679 exported_object_manager_->SendSignal(&signal);
682 void TestService::SendPropertyChangedSignal(const std::string& name) {
683 message_loop()->PostTask(
684 FROM_HERE,
685 base::Bind(&TestService::SendPropertyChangedSignalInternal,
686 base::Unretained(this),
687 name));
690 void TestService::SendPropertyChangedSignalInternal(const std::string& name) {
691 Signal signal(kPropertiesInterface, kPropertiesChanged);
692 MessageWriter writer(&signal);
693 writer.AppendString("org.chromium.TestInterface");
695 MessageWriter array_writer(NULL);
696 MessageWriter dict_entry_writer(NULL);
698 writer.OpenArray("{sv}", &array_writer);
699 array_writer.OpenDictEntry(&dict_entry_writer);
700 dict_entry_writer.AppendString("Name");
701 dict_entry_writer.AppendVariantOfString(name);
702 array_writer.CloseContainer(&dict_entry_writer);
703 writer.CloseContainer(&array_writer);
705 exported_object_->SendSignal(&signal);
708 } // namespace dbus