OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
11 #include "dbus/exported_object.h" | 11 #include "dbus/exported_object.h" |
12 #include "dbus/message.h" | 12 #include "dbus/message.h" |
13 | 13 |
14 namespace dbus { | 14 namespace dbus { |
15 | 15 |
16 // Echo, SlowEcho, BrokenMethod. | 16 // Echo, SlowEcho, BrokenMethod. |
17 const int TestService::kNumMethodsToExport = 3; | 17 const int TestService::kNumMethodsToExport = 3; |
18 | 18 |
19 TestService::Options::Options() | 19 TestService::Options::Options() |
20 : dbus_thread(NULL) { | 20 : dbus_thread(NULL) { |
21 } | 21 } |
22 | 22 |
23 TestService::Options::~Options() { | 23 TestService::Options::~Options() { |
24 } | 24 } |
25 | 25 |
26 TestService::TestService(const Options& options) | 26 TestService::TestService(const Options& options) |
27 : base::Thread("TestService"), | 27 : base::Thread("TestService"), |
28 dbus_thread_(options.dbus_thread), | 28 dbus_thread_(options.dbus_thread), |
29 on_shutdown_(false /* manual_reset */, false /* initially_signaled */), | |
30 on_all_methods_exported_(false, false), | 29 on_all_methods_exported_(false, false), |
31 num_exported_methods_(0) { | 30 num_exported_methods_(0) { |
32 } | 31 } |
33 | 32 |
34 TestService::~TestService() { | 33 TestService::~TestService() { |
35 } | 34 } |
36 | 35 |
37 bool TestService::StartService() { | 36 bool TestService::StartService() { |
38 base::Thread::Options thread_options; | 37 base::Thread::Options thread_options; |
39 thread_options.message_loop_type = MessageLoop::TYPE_IO; | 38 thread_options.message_loop_type = MessageLoop::TYPE_IO; |
40 return StartWithOptions(thread_options); | 39 return StartWithOptions(thread_options); |
41 } | 40 } |
42 | 41 |
43 bool TestService::WaitUntilServiceIsStarted() { | 42 bool TestService::WaitUntilServiceIsStarted() { |
44 const base::TimeDelta timeout( | 43 const base::TimeDelta timeout( |
45 base::TimeDelta::FromMilliseconds( | 44 base::TimeDelta::FromMilliseconds( |
46 TestTimeouts::action_max_timeout_ms())); | 45 TestTimeouts::action_max_timeout_ms())); |
47 // Wait until all methods are exported. | 46 // Wait until all methods are exported. |
48 return on_all_methods_exported_.TimedWait(timeout); | 47 return on_all_methods_exported_.TimedWait(timeout); |
49 } | 48 } |
50 | 49 |
51 void TestService::Shutdown() { | 50 void TestService::ShutdownAndBlock() { |
52 message_loop()->PostTask( | 51 message_loop()->PostTask( |
53 FROM_HERE, | 52 FROM_HERE, |
54 base::Bind(&TestService::ShutdownInternal, | 53 base::Bind(&TestService::ShutdownAndBlockInternal, |
55 base::Unretained(this))); | 54 base::Unretained(this))); |
56 } | 55 } |
57 | 56 |
58 bool TestService::WaitUntilServiceIsShutdown() { | |
59 const base::TimeDelta timeout( | |
60 base::TimeDelta::FromMilliseconds( | |
61 TestTimeouts::action_max_timeout_ms())); | |
62 return on_shutdown_.TimedWait(timeout); | |
63 } | |
64 | |
65 bool TestService::HasDBusThread() { | 57 bool TestService::HasDBusThread() { |
66 return bus_->HasDBusThread(); | 58 return bus_->HasDBusThread(); |
67 } | 59 } |
68 | 60 |
| 61 void TestService::ShutdownAndBlockInternal() { |
| 62 if (HasDBusThread()) |
| 63 bus_->ShutdownOnDBusThreadAndBlock(); |
| 64 else |
| 65 bus_->ShutdownAndBlock(); |
| 66 } |
| 67 |
69 void TestService::SendTestSignal(const std::string& message) { | 68 void TestService::SendTestSignal(const std::string& message) { |
70 message_loop()->PostTask( | 69 message_loop()->PostTask( |
71 FROM_HERE, | 70 FROM_HERE, |
72 base::Bind(&TestService::SendTestSignalInternal, | 71 base::Bind(&TestService::SendTestSignalInternal, |
73 base::Unretained(this), | 72 base::Unretained(this), |
74 message)); | 73 message)); |
75 } | 74 } |
76 | 75 |
77 void TestService::SendTestSignalInternal(const std::string& message) { | 76 void TestService::SendTestSignalInternal(const std::string& message) { |
78 dbus::Signal signal("org.chromium.TestInterface", "Test"); | 77 dbus::Signal signal("org.chromium.TestInterface", "Test"); |
79 dbus::MessageWriter writer(&signal); | 78 dbus::MessageWriter writer(&signal); |
80 writer.AppendString(message); | 79 writer.AppendString(message); |
81 exported_object_->SendSignal(&signal); | 80 exported_object_->SendSignal(&signal); |
82 } | 81 } |
83 | 82 |
84 void TestService::ShutdownInternal() { | |
85 bus_->Shutdown(base::Bind(&TestService::OnShutdown, | |
86 base::Unretained(this))); | |
87 } | |
88 | |
89 void TestService::OnExported(const std::string& interface_name, | 83 void TestService::OnExported(const std::string& interface_name, |
90 const std::string& method_name, | 84 const std::string& method_name, |
91 bool success) { | 85 bool success) { |
92 if (!success) { | 86 if (!success) { |
93 LOG(ERROR) << "Failed to export: " << interface_name << "." | 87 LOG(ERROR) << "Failed to export: " << interface_name << "." |
94 << method_name; | 88 << method_name; |
95 // Returning here will make WaitUntilServiceIsStarted() to time out | 89 // Returning here will make WaitUntilServiceIsStarted() to time out |
96 // and return false. | 90 // and return false. |
97 return; | 91 return; |
98 } | 92 } |
99 | 93 |
100 ++num_exported_methods_; | 94 ++num_exported_methods_; |
101 if (num_exported_methods_ == kNumMethodsToExport) | 95 if (num_exported_methods_ == kNumMethodsToExport) |
102 on_all_methods_exported_.Signal(); | 96 on_all_methods_exported_.Signal(); |
103 } | 97 } |
104 | 98 |
105 void TestService::OnShutdown() { | |
106 on_shutdown_.Signal(); | |
107 } | |
108 | |
109 void TestService::Run(MessageLoop* message_loop) { | 99 void TestService::Run(MessageLoop* message_loop) { |
110 Bus::Options bus_options; | 100 Bus::Options bus_options; |
111 bus_options.bus_type = Bus::SESSION; | 101 bus_options.bus_type = Bus::SESSION; |
112 bus_options.connection_type = Bus::PRIVATE; | 102 bus_options.connection_type = Bus::PRIVATE; |
113 bus_options.dbus_thread = dbus_thread_; | 103 bus_options.dbus_thread = dbus_thread_; |
114 bus_ = new Bus(bus_options); | 104 bus_ = new Bus(bus_options); |
115 | 105 |
116 exported_object_ = bus_->GetExportedObject( | 106 exported_object_ = bus_->GetExportedObject( |
117 "org.chromium.TestService", | 107 "org.chromium.TestService", |
118 "/org/chromium/TestObject"); | 108 "/org/chromium/TestObject"); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 Response* TestService::SlowEcho(MethodCall* method_call) { | 158 Response* TestService::SlowEcho(MethodCall* method_call) { |
169 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); | 159 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); |
170 return Echo(method_call); | 160 return Echo(method_call); |
171 } | 161 } |
172 | 162 |
173 Response* TestService::BrokenMethod(MethodCall* method_call) { | 163 Response* TestService::BrokenMethod(MethodCall* method_call) { |
174 return NULL; | 164 return NULL; |
175 } | 165 } |
176 | 166 |
177 } // namespace dbus | 167 } // namespace dbus |
OLD | NEW |