| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/test/test_timeouts.h" | 16 #include "base/test/test_timeouts.h" |
| 16 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 17 #include "dbus/bus.h" | 18 #include "dbus/bus.h" |
| 18 #include "dbus/exported_object.h" | 19 #include "dbus/exported_object.h" |
| 19 #include "dbus/message.h" | 20 #include "dbus/message.h" |
| 20 #include "dbus/object_manager.h" | 21 #include "dbus/object_manager.h" |
| 21 #include "dbus/object_path.h" | 22 #include "dbus/object_path.h" |
| 22 #include "dbus/property.h" | 23 #include "dbus/property.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 return StartWithOptions(thread_options); | 69 return StartWithOptions(thread_options); |
| 69 } | 70 } |
| 70 | 71 |
| 71 bool TestService::WaitUntilServiceIsStarted() { | 72 bool TestService::WaitUntilServiceIsStarted() { |
| 72 const base::TimeDelta timeout(TestTimeouts::action_max_timeout()); | 73 const base::TimeDelta timeout(TestTimeouts::action_max_timeout()); |
| 73 // Wait until the ownership of the service name is obtained. | 74 // Wait until the ownership of the service name is obtained. |
| 74 return on_name_obtained_.TimedWait(timeout); | 75 return on_name_obtained_.TimedWait(timeout); |
| 75 } | 76 } |
| 76 | 77 |
| 77 void TestService::ShutdownAndBlock() { | 78 void TestService::ShutdownAndBlock() { |
| 78 message_loop()->PostTask( | 79 message_loop()->task_runner()->PostTask( |
| 79 FROM_HERE, | 80 FROM_HERE, base::Bind(&TestService::ShutdownAndBlockInternal, |
| 80 base::Bind(&TestService::ShutdownAndBlockInternal, | 81 base::Unretained(this))); |
| 81 base::Unretained(this))); | |
| 82 } | 82 } |
| 83 | 83 |
| 84 bool TestService::HasDBusThread() { | 84 bool TestService::HasDBusThread() { |
| 85 return bus_->HasDBusThread(); | 85 return bus_->HasDBusThread(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void TestService::ShutdownAndBlockInternal() { | 88 void TestService::ShutdownAndBlockInternal() { |
| 89 if (HasDBusThread()) | 89 if (HasDBusThread()) |
| 90 bus_->ShutdownOnDBusThreadAndBlock(); | 90 bus_->ShutdownOnDBusThreadAndBlock(); |
| 91 else | 91 else |
| 92 bus_->ShutdownAndBlock(); | 92 bus_->ShutdownAndBlock(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void TestService::SendTestSignal(const std::string& message) { | 95 void TestService::SendTestSignal(const std::string& message) { |
| 96 message_loop()->PostTask( | 96 message_loop()->task_runner()->PostTask( |
| 97 FROM_HERE, | 97 FROM_HERE, base::Bind(&TestService::SendTestSignalInternal, |
| 98 base::Bind(&TestService::SendTestSignalInternal, | 98 base::Unretained(this), message)); |
| 99 base::Unretained(this), | |
| 100 message)); | |
| 101 } | 99 } |
| 102 | 100 |
| 103 void TestService::SendTestSignalFromRoot(const std::string& message) { | 101 void TestService::SendTestSignalFromRoot(const std::string& message) { |
| 104 message_loop()->PostTask( | 102 message_loop()->task_runner()->PostTask( |
| 105 FROM_HERE, | 103 FROM_HERE, base::Bind(&TestService::SendTestSignalFromRootInternal, |
| 106 base::Bind(&TestService::SendTestSignalFromRootInternal, | 104 base::Unretained(this), message)); |
| 107 base::Unretained(this), | |
| 108 message)); | |
| 109 } | 105 } |
| 110 | 106 |
| 111 void TestService::SendTestSignalInternal(const std::string& message) { | 107 void TestService::SendTestSignalInternal(const std::string& message) { |
| 112 Signal signal("org.chromium.TestInterface", "Test"); | 108 Signal signal("org.chromium.TestInterface", "Test"); |
| 113 MessageWriter writer(&signal); | 109 MessageWriter writer(&signal); |
| 114 writer.AppendString(message); | 110 writer.AppendString(message); |
| 115 exported_object_->SendSignal(&signal); | 111 exported_object_->SendSignal(&signal); |
| 116 } | 112 } |
| 117 | 113 |
| 118 void TestService::SendTestSignalFromRootInternal(const std::string& message) { | 114 void TestService::SendTestSignalFromRootInternal(const std::string& message) { |
| 119 Signal signal("org.chromium.TestInterface", "Test"); | 115 Signal signal("org.chromium.TestInterface", "Test"); |
| 120 MessageWriter writer(&signal); | 116 MessageWriter writer(&signal); |
| 121 writer.AppendString(message); | 117 writer.AppendString(message); |
| 122 | 118 |
| 123 bus_->RequestOwnership(service_name_, | 119 bus_->RequestOwnership(service_name_, |
| 124 request_ownership_options_, | 120 request_ownership_options_, |
| 125 base::Bind(&TestService::OnOwnership, | 121 base::Bind(&TestService::OnOwnership, |
| 126 base::Unretained(this), | 122 base::Unretained(this), |
| 127 base::Bind(&EmptyCallback))); | 123 base::Bind(&EmptyCallback))); |
| 128 | 124 |
| 129 // Use "/" just like dbus-send does. | 125 // Use "/" just like dbus-send does. |
| 130 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/")); | 126 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/")); |
| 131 root_object->SendSignal(&signal); | 127 root_object->SendSignal(&signal); |
| 132 } | 128 } |
| 133 | 129 |
| 134 void TestService::RequestOwnership(base::Callback<void(bool)> callback) { | 130 void TestService::RequestOwnership(base::Callback<void(bool)> callback) { |
| 135 message_loop()->PostTask( | 131 message_loop()->task_runner()->PostTask( |
| 136 FROM_HERE, | 132 FROM_HERE, base::Bind(&TestService::RequestOwnershipInternal, |
| 137 base::Bind(&TestService::RequestOwnershipInternal, | 133 base::Unretained(this), callback)); |
| 138 base::Unretained(this), | |
| 139 callback)); | |
| 140 } | 134 } |
| 141 | 135 |
| 142 void TestService::RequestOwnershipInternal( | 136 void TestService::RequestOwnershipInternal( |
| 143 base::Callback<void(bool)> callback) { | 137 base::Callback<void(bool)> callback) { |
| 144 bus_->RequestOwnership(service_name_, | 138 bus_->RequestOwnership(service_name_, |
| 145 request_ownership_options_, | 139 request_ownership_options_, |
| 146 base::Bind(&TestService::OnOwnership, | 140 base::Bind(&TestService::OnOwnership, |
| 147 base::Unretained(this), | 141 base::Unretained(this), |
| 148 callback)); | 142 callback)); |
| 149 } | 143 } |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 | 317 |
| 324 void TestService::SlowEcho(MethodCall* method_call, | 318 void TestService::SlowEcho(MethodCall* method_call, |
| 325 ExportedObject::ResponseSender response_sender) { | 319 ExportedObject::ResponseSender response_sender) { |
| 326 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 320 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 327 Echo(method_call, response_sender); | 321 Echo(method_call, response_sender); |
| 328 } | 322 } |
| 329 | 323 |
| 330 void TestService::AsyncEcho(MethodCall* method_call, | 324 void TestService::AsyncEcho(MethodCall* method_call, |
| 331 ExportedObject::ResponseSender response_sender) { | 325 ExportedObject::ResponseSender response_sender) { |
| 332 // Schedule a call to Echo() to send an asynchronous response after we return. | 326 // Schedule a call to Echo() to send an asynchronous response after we return. |
| 333 message_loop()->PostDelayedTask(FROM_HERE, | 327 message_loop()->task_runner()->PostDelayedTask( |
| 334 base::Bind(&TestService::Echo, | 328 FROM_HERE, base::Bind(&TestService::Echo, base::Unretained(this), |
| 335 base::Unretained(this), | 329 method_call, response_sender), |
| 336 method_call, | 330 TestTimeouts::tiny_timeout()); |
| 337 response_sender), | |
| 338 TestTimeouts::tiny_timeout()); | |
| 339 } | 331 } |
| 340 | 332 |
| 341 void TestService::BrokenMethod(MethodCall* method_call, | 333 void TestService::BrokenMethod(MethodCall* method_call, |
| 342 ExportedObject::ResponseSender response_sender) { | 334 ExportedObject::ResponseSender response_sender) { |
| 343 response_sender.Run(std::unique_ptr<Response>()); | 335 response_sender.Run(std::unique_ptr<Response>()); |
| 344 } | 336 } |
| 345 | 337 |
| 346 | 338 |
| 347 void TestService::GetAllProperties( | 339 void TestService::GetAllProperties( |
| 348 MethodCall* method_call, | 340 MethodCall* method_call, |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 dict_entry_writer.OpenVariant("ay", &variant_writer); | 633 dict_entry_writer.OpenVariant("ay", &variant_writer); |
| 642 const uint8_t bytes[] = {0x54, 0x65, 0x73, 0x74}; | 634 const uint8_t bytes[] = {0x54, 0x65, 0x73, 0x74}; |
| 643 variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes)); | 635 variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes)); |
| 644 dict_entry_writer.CloseContainer(&variant_writer); | 636 dict_entry_writer.CloseContainer(&variant_writer); |
| 645 array_writer.CloseContainer(&dict_entry_writer); | 637 array_writer.CloseContainer(&dict_entry_writer); |
| 646 | 638 |
| 647 writer->CloseContainer(&array_writer); | 639 writer->CloseContainer(&array_writer); |
| 648 } | 640 } |
| 649 | 641 |
| 650 void TestService::AddObject(const ObjectPath& object_path) { | 642 void TestService::AddObject(const ObjectPath& object_path) { |
| 651 message_loop()->PostTask( | 643 message_loop()->task_runner()->PostTask( |
| 652 FROM_HERE, | 644 FROM_HERE, base::Bind(&TestService::AddObjectInternal, |
| 653 base::Bind(&TestService::AddObjectInternal, | 645 base::Unretained(this), object_path)); |
| 654 base::Unretained(this), | |
| 655 object_path)); | |
| 656 } | 646 } |
| 657 | 647 |
| 658 void TestService::AddObjectInternal(const ObjectPath& object_path) { | 648 void TestService::AddObjectInternal(const ObjectPath& object_path) { |
| 659 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded); | 649 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded); |
| 660 MessageWriter writer(&signal); | 650 MessageWriter writer(&signal); |
| 661 writer.AppendObjectPath(object_path); | 651 writer.AppendObjectPath(object_path); |
| 662 | 652 |
| 663 MessageWriter array_writer(NULL); | 653 MessageWriter array_writer(NULL); |
| 664 MessageWriter dict_entry_writer(NULL); | 654 MessageWriter dict_entry_writer(NULL); |
| 665 | 655 |
| 666 writer.OpenArray("{sa{sv}}", &array_writer); | 656 writer.OpenArray("{sa{sv}}", &array_writer); |
| 667 array_writer.OpenDictEntry(&dict_entry_writer); | 657 array_writer.OpenDictEntry(&dict_entry_writer); |
| 668 dict_entry_writer.AppendString("org.chromium.TestInterface"); | 658 dict_entry_writer.AppendString("org.chromium.TestInterface"); |
| 669 AddPropertiesToWriter(&dict_entry_writer); | 659 AddPropertiesToWriter(&dict_entry_writer); |
| 670 array_writer.CloseContainer(&dict_entry_writer); | 660 array_writer.CloseContainer(&dict_entry_writer); |
| 671 writer.CloseContainer(&array_writer); | 661 writer.CloseContainer(&array_writer); |
| 672 | 662 |
| 673 exported_object_manager_->SendSignal(&signal); | 663 exported_object_manager_->SendSignal(&signal); |
| 674 } | 664 } |
| 675 | 665 |
| 676 void TestService::RemoveObject(const ObjectPath& object_path) { | 666 void TestService::RemoveObject(const ObjectPath& object_path) { |
| 677 message_loop()->PostTask(FROM_HERE, | 667 message_loop()->task_runner()->PostTask( |
| 678 base::Bind(&TestService::RemoveObjectInternal, | 668 FROM_HERE, base::Bind(&TestService::RemoveObjectInternal, |
| 679 base::Unretained(this), | 669 base::Unretained(this), object_path)); |
| 680 object_path)); | |
| 681 } | 670 } |
| 682 | 671 |
| 683 void TestService::RemoveObjectInternal(const ObjectPath& object_path) { | 672 void TestService::RemoveObjectInternal(const ObjectPath& object_path) { |
| 684 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved); | 673 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved); |
| 685 MessageWriter writer(&signal); | 674 MessageWriter writer(&signal); |
| 686 | 675 |
| 687 writer.AppendObjectPath(object_path); | 676 writer.AppendObjectPath(object_path); |
| 688 | 677 |
| 689 std::vector<std::string> interfaces; | 678 std::vector<std::string> interfaces; |
| 690 interfaces.push_back("org.chromium.TestInterface"); | 679 interfaces.push_back("org.chromium.TestInterface"); |
| 691 writer.AppendArrayOfStrings(interfaces); | 680 writer.AppendArrayOfStrings(interfaces); |
| 692 | 681 |
| 693 exported_object_manager_->SendSignal(&signal); | 682 exported_object_manager_->SendSignal(&signal); |
| 694 } | 683 } |
| 695 | 684 |
| 696 void TestService::SendPropertyChangedSignal(const std::string& name) { | 685 void TestService::SendPropertyChangedSignal(const std::string& name) { |
| 697 message_loop()->PostTask( | 686 message_loop()->task_runner()->PostTask( |
| 698 FROM_HERE, | 687 FROM_HERE, base::Bind(&TestService::SendPropertyChangedSignalInternal, |
| 699 base::Bind(&TestService::SendPropertyChangedSignalInternal, | 688 base::Unretained(this), name)); |
| 700 base::Unretained(this), | |
| 701 name)); | |
| 702 } | 689 } |
| 703 | 690 |
| 704 void TestService::SendPropertyChangedSignalInternal(const std::string& name) { | 691 void TestService::SendPropertyChangedSignalInternal(const std::string& name) { |
| 705 Signal signal(kPropertiesInterface, kPropertiesChanged); | 692 Signal signal(kPropertiesInterface, kPropertiesChanged); |
| 706 MessageWriter writer(&signal); | 693 MessageWriter writer(&signal); |
| 707 writer.AppendString("org.chromium.TestInterface"); | 694 writer.AppendString("org.chromium.TestInterface"); |
| 708 | 695 |
| 709 MessageWriter array_writer(NULL); | 696 MessageWriter array_writer(NULL); |
| 710 MessageWriter dict_entry_writer(NULL); | 697 MessageWriter dict_entry_writer(NULL); |
| 711 | 698 |
| 712 writer.OpenArray("{sv}", &array_writer); | 699 writer.OpenArray("{sv}", &array_writer); |
| 713 array_writer.OpenDictEntry(&dict_entry_writer); | 700 array_writer.OpenDictEntry(&dict_entry_writer); |
| 714 dict_entry_writer.AppendString("Name"); | 701 dict_entry_writer.AppendString("Name"); |
| 715 dict_entry_writer.AppendVariantOfString(name); | 702 dict_entry_writer.AppendVariantOfString(name); |
| 716 array_writer.CloseContainer(&dict_entry_writer); | 703 array_writer.CloseContainer(&dict_entry_writer); |
| 717 writer.CloseContainer(&array_writer); | 704 writer.CloseContainer(&array_writer); |
| 718 | 705 |
| 719 MessageWriter invalidated_array_writer(NULL); | 706 MessageWriter invalidated_array_writer(NULL); |
| 720 | 707 |
| 721 writer.OpenArray("s", &invalidated_array_writer); | 708 writer.OpenArray("s", &invalidated_array_writer); |
| 722 writer.CloseContainer(&invalidated_array_writer); | 709 writer.CloseContainer(&invalidated_array_writer); |
| 723 | 710 |
| 724 exported_object_->SendSignal(&signal); | 711 exported_object_->SendSignal(&signal); |
| 725 } | 712 } |
| 726 | 713 |
| 727 void TestService::SendPropertyInvalidatedSignal() { | 714 void TestService::SendPropertyInvalidatedSignal() { |
| 728 message_loop()->PostTask( | 715 message_loop()->task_runner()->PostTask( |
| 729 FROM_HERE, base::Bind(&TestService::SendPropertyInvalidatedSignalInternal, | 716 FROM_HERE, base::Bind(&TestService::SendPropertyInvalidatedSignalInternal, |
| 730 base::Unretained(this))); | 717 base::Unretained(this))); |
| 731 } | 718 } |
| 732 | 719 |
| 733 void TestService::SendPropertyInvalidatedSignalInternal() { | 720 void TestService::SendPropertyInvalidatedSignalInternal() { |
| 734 Signal signal(kPropertiesInterface, kPropertiesChanged); | 721 Signal signal(kPropertiesInterface, kPropertiesChanged); |
| 735 MessageWriter writer(&signal); | 722 MessageWriter writer(&signal); |
| 736 writer.AppendString("org.chromium.TestInterface"); | 723 writer.AppendString("org.chromium.TestInterface"); |
| 737 | 724 |
| 738 MessageWriter array_writer(NULL); | 725 MessageWriter array_writer(NULL); |
| 739 MessageWriter dict_entry_writer(NULL); | 726 MessageWriter dict_entry_writer(NULL); |
| 740 | 727 |
| 741 writer.OpenArray("{sv}", &array_writer); | 728 writer.OpenArray("{sv}", &array_writer); |
| 742 writer.CloseContainer(&array_writer); | 729 writer.CloseContainer(&array_writer); |
| 743 | 730 |
| 744 MessageWriter invalidated_array_writer(NULL); | 731 MessageWriter invalidated_array_writer(NULL); |
| 745 | 732 |
| 746 writer.OpenArray("s", &invalidated_array_writer); | 733 writer.OpenArray("s", &invalidated_array_writer); |
| 747 invalidated_array_writer.AppendString("Name"); | 734 invalidated_array_writer.AppendString("Name"); |
| 748 writer.CloseContainer(&invalidated_array_writer); | 735 writer.CloseContainer(&invalidated_array_writer); |
| 749 | 736 |
| 750 exported_object_->SendSignal(&signal); | 737 exported_object_->SendSignal(&signal); |
| 751 } | 738 } |
| 752 | 739 |
| 753 } // namespace dbus | 740 } // namespace dbus |
| OLD | NEW |