blob: 1b8b9bff24951b358490e18577129fcdcfe9017d [file] [log] [blame]
[email protected]a51076112011-08-17 20:58:121// 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]12f97662011-08-20 01:07:178#include "base/test/test_timeouts.h"
[email protected]a51076112011-08-17 20:58:129#include "base/threading/platform_thread.h"
10#include "dbus/bus.h"
11#include "dbus/exported_object.h"
12#include "dbus/message.h"
13
14namespace dbus {
15
[email protected]12f97662011-08-20 01:07:1716// Echo, SlowEcho, BrokenMethod.
17const int TestService::kNumMethodsToExport = 3;
[email protected]a51076112011-08-17 20:58:1218
[email protected]12f97662011-08-20 01:07:1719TestService::Options::Options()
20 : dbus_thread(NULL) {
21}
22
23TestService::Options::~Options() {
24}
25
26TestService::TestService(const Options& options)
[email protected]a51076112011-08-17 20:58:1227 : base::Thread("TestService"),
[email protected]12f97662011-08-20 01:07:1728 dbus_thread_(options.dbus_thread),
[email protected]12f97662011-08-20 01:07:1729 on_all_methods_exported_(false, false),
30 num_exported_methods_(0) {
[email protected]a51076112011-08-17 20:58:1231}
32
33TestService::~TestService() {
34}
35
[email protected]12f97662011-08-20 01:07:1736bool TestService::StartService() {
[email protected]a51076112011-08-17 20:58:1237 base::Thread::Options thread_options;
38 thread_options.message_loop_type = MessageLoop::TYPE_IO;
[email protected]12f97662011-08-20 01:07:1739 return StartWithOptions(thread_options);
[email protected]a51076112011-08-17 20:58:1240}
41
[email protected]12f97662011-08-20 01:07:1742bool 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]e20d39fe2011-09-02 06:56:2350void TestService::ShutdownAndBlock() {
[email protected]a51076112011-08-17 20:58:1251 message_loop()->PostTask(
52 FROM_HERE,
[email protected]e20d39fe2011-09-02 06:56:2353 base::Bind(&TestService::ShutdownAndBlockInternal,
[email protected]a51076112011-08-17 20:58:1254 base::Unretained(this)));
[email protected]a51076112011-08-17 20:58:1255}
56
[email protected]12f97662011-08-20 01:07:1757bool TestService::HasDBusThread() {
58 return bus_->HasDBusThread();
59}
60
[email protected]e20d39fe2011-09-02 06:56:2361void TestService::ShutdownAndBlockInternal() {
62 if (HasDBusThread())
63 bus_->ShutdownOnDBusThreadAndBlock();
64 else
65 bus_->ShutdownAndBlock();
66}
67
[email protected]3beaaa4e2011-08-23 07:29:2168void 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
76void 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]12f97662011-08-20 01:07:1783void 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]a51076112011-08-17 20:58:1299void 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]12f97662011-08-20 01:07:17103 bus_options.dbus_thread = dbus_thread_;
[email protected]a51076112011-08-17 20:58:12104 bus_ = new Bus(bus_options);
105
106 exported_object_ = bus_->GetExportedObject(
107 "org.chromium.TestService",
108 "/org/chromium/TestObject");
[email protected]12f97662011-08-20 01:07:17109
110 int num_methods = 0;
111 exported_object_->ExportMethod(
[email protected]a51076112011-08-17 20:58:12112 "org.chromium.TestInterface",
113 "Echo",
114 base::Bind(&TestService::Echo,
[email protected]12f97662011-08-20 01:07:17115 base::Unretained(this)),
116 base::Bind(&TestService::OnExported,
117 base::Unretained(this)));
118 ++num_methods;
119
120 exported_object_->ExportMethod(
[email protected]a51076112011-08-17 20:58:12121 "org.chromium.TestInterface",
122 "SlowEcho",
123 base::Bind(&TestService::SlowEcho,
[email protected]12f97662011-08-20 01:07:17124 base::Unretained(this)),
125 base::Bind(&TestService::OnExported,
126 base::Unretained(this)));
127 ++num_methods;
128
129 exported_object_->ExportMethod(
[email protected]a51076112011-08-17 20:58:12130 "org.chromium.TestInterface",
131 "BrokenMethod",
132 base::Bind(&TestService::BrokenMethod,
[email protected]12f97662011-08-20 01:07:17133 base::Unretained(this)),
134 base::Bind(&TestService::OnExported,
135 base::Unretained(this)));
136 ++num_methods;
[email protected]a51076112011-08-17 20:58:12137
[email protected]12f97662011-08-20 01:07:17138 // 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]a51076112011-08-17 20:58:12143 message_loop->Run();
144}
145
[email protected]a51076112011-08-17 20:58:12146Response* 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
158Response* TestService::SlowEcho(MethodCall* method_call) {
[email protected]12f97662011-08-20 01:07:17159 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms());
[email protected]a51076112011-08-17 20:58:12160 return Echo(method_call);
161}
162
163Response* TestService::BrokenMethod(MethodCall* method_call) {
164 return NULL;
165}
166
167} // namespace dbus