[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 1 | // Copyright (c) 2011 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. |
| 4 | |
| 5 | #include "dbus/test_service.h" |
| 6 | |
| 7 | #include "base/bind.h" |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 8 | #include "base/test/test_timeouts.h" |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 9 | #include "base/threading/platform_thread.h" |
| 10 | #include "dbus/bus.h" |
| 11 | #include "dbus/exported_object.h" |
| 12 | #include "dbus/message.h" |
| 13 | |
| 14 | namespace dbus { |
| 15 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 16 | // Echo, SlowEcho, BrokenMethod. |
| 17 | const int TestService::kNumMethodsToExport = 3; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 18 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 19 | TestService::Options::Options() |
| 20 | : dbus_thread(NULL) { |
| 21 | } |
| 22 | |
| 23 | TestService::Options::~Options() { |
| 24 | } |
| 25 | |
| 26 | TestService::TestService(const Options& options) |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 27 | : base::Thread("TestService"), |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 28 | dbus_thread_(options.dbus_thread), |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 29 | on_all_methods_exported_(false, false), |
| 30 | num_exported_methods_(0) { |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TestService::~TestService() { |
| 34 | } |
| 35 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 36 | bool TestService::StartService() { |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 37 | base::Thread::Options thread_options; |
| 38 | thread_options.message_loop_type = MessageLoop::TYPE_IO; |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 39 | return StartWithOptions(thread_options); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 42 | bool TestService::WaitUntilServiceIsStarted() { |
| 43 | const base::TimeDelta timeout( |
| 44 | base::TimeDelta::FromMilliseconds( |
| 45 | TestTimeouts::action_max_timeout_ms())); |
| 46 | // Wait until all methods are exported. |
| 47 | return on_all_methods_exported_.TimedWait(timeout); |
| 48 | } |
| 49 | |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame^] | 50 | void TestService::ShutdownAndBlock() { |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 51 | message_loop()->PostTask( |
| 52 | FROM_HERE, |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame^] | 53 | base::Bind(&TestService::ShutdownAndBlockInternal, |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 54 | base::Unretained(this))); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 57 | bool TestService::HasDBusThread() { |
| 58 | return bus_->HasDBusThread(); |
| 59 | } |
| 60 | |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame^] | 61 | void TestService::ShutdownAndBlockInternal() { |
| 62 | if (HasDBusThread()) |
| 63 | bus_->ShutdownOnDBusThreadAndBlock(); |
| 64 | else |
| 65 | bus_->ShutdownAndBlock(); |
| 66 | } |
| 67 | |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 68 | void TestService::SendTestSignal(const std::string& message) { |
| 69 | message_loop()->PostTask( |
| 70 | FROM_HERE, |
| 71 | base::Bind(&TestService::SendTestSignalInternal, |
| 72 | base::Unretained(this), |
| 73 | message)); |
| 74 | } |
| 75 | |
| 76 | void TestService::SendTestSignalInternal(const std::string& message) { |
| 77 | dbus::Signal signal("org.chromium.TestInterface", "Test"); |
| 78 | dbus::MessageWriter writer(&signal); |
| 79 | writer.AppendString(message); |
| 80 | exported_object_->SendSignal(&signal); |
| 81 | } |
| 82 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 83 | void TestService::OnExported(const std::string& interface_name, |
| 84 | const std::string& method_name, |
| 85 | bool success) { |
| 86 | if (!success) { |
| 87 | LOG(ERROR) << "Failed to export: " << interface_name << "." |
| 88 | << method_name; |
| 89 | // Returning here will make WaitUntilServiceIsStarted() to time out |
| 90 | // and return false. |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | ++num_exported_methods_; |
| 95 | if (num_exported_methods_ == kNumMethodsToExport) |
| 96 | on_all_methods_exported_.Signal(); |
| 97 | } |
| 98 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 99 | void TestService::Run(MessageLoop* message_loop) { |
| 100 | Bus::Options bus_options; |
| 101 | bus_options.bus_type = Bus::SESSION; |
| 102 | bus_options.connection_type = Bus::PRIVATE; |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 103 | bus_options.dbus_thread = dbus_thread_; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 104 | bus_ = new Bus(bus_options); |
| 105 | |
| 106 | exported_object_ = bus_->GetExportedObject( |
| 107 | "org.chromium.TestService", |
| 108 | "/org/chromium/TestObject"); |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 109 | |
| 110 | int num_methods = 0; |
| 111 | exported_object_->ExportMethod( |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 112 | "org.chromium.TestInterface", |
| 113 | "Echo", |
| 114 | base::Bind(&TestService::Echo, |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 115 | base::Unretained(this)), |
| 116 | base::Bind(&TestService::OnExported, |
| 117 | base::Unretained(this))); |
| 118 | ++num_methods; |
| 119 | |
| 120 | exported_object_->ExportMethod( |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 121 | "org.chromium.TestInterface", |
| 122 | "SlowEcho", |
| 123 | base::Bind(&TestService::SlowEcho, |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 124 | base::Unretained(this)), |
| 125 | base::Bind(&TestService::OnExported, |
| 126 | base::Unretained(this))); |
| 127 | ++num_methods; |
| 128 | |
| 129 | exported_object_->ExportMethod( |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 130 | "org.chromium.TestInterface", |
| 131 | "BrokenMethod", |
| 132 | base::Bind(&TestService::BrokenMethod, |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 133 | base::Unretained(this)), |
| 134 | base::Bind(&TestService::OnExported, |
| 135 | base::Unretained(this))); |
| 136 | ++num_methods; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 137 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 138 | // Just print an error message as we don't want to crash tests. |
| 139 | // Tests will fail at a call to WaitUntilServiceIsStarted(). |
| 140 | if (num_methods != kNumMethodsToExport) { |
| 141 | LOG(ERROR) << "The number of methods does not match"; |
| 142 | } |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 143 | message_loop->Run(); |
| 144 | } |
| 145 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 146 | Response* TestService::Echo(MethodCall* method_call) { |
| 147 | MessageReader reader(method_call); |
| 148 | std::string text_message; |
| 149 | if (!reader.PopString(&text_message)) |
| 150 | return NULL; |
| 151 | |
| 152 | Response* response = Response::FromMethodCall(method_call); |
| 153 | MessageWriter writer(response); |
| 154 | writer.AppendString(text_message); |
| 155 | return response; |
| 156 | } |
| 157 | |
| 158 | Response* TestService::SlowEcho(MethodCall* method_call) { |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 159 | base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 160 | return Echo(method_call); |
| 161 | } |
| 162 | |
| 163 | Response* TestService::BrokenMethod(MethodCall* method_call) { |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | } // namespace dbus |