OLD | NEW |
(Empty) | |
| 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" |
| 8 #include "base/threading/platform_thread.h" |
| 9 #include "dbus/bus.h" |
| 10 #include "dbus/exported_object.h" |
| 11 #include "dbus/message.h" |
| 12 |
| 13 namespace dbus { |
| 14 |
| 15 const int TestService::kSlowEchoSleepMs = 100; // In milliseconds. |
| 16 |
| 17 TestService::TestService() |
| 18 : base::Thread("TestService"), |
| 19 service_started_(false), |
| 20 on_service_started_(&service_started_lock_) { |
| 21 } |
| 22 |
| 23 TestService::~TestService() { |
| 24 } |
| 25 |
| 26 void TestService::StartService() { |
| 27 base::Thread::Options thread_options; |
| 28 thread_options.message_loop_type = MessageLoop::TYPE_IO; |
| 29 StartWithOptions(thread_options); |
| 30 } |
| 31 |
| 32 void TestService::WaitUntilServiceIsStarted() { |
| 33 message_loop()->PostTask( |
| 34 FROM_HERE, |
| 35 base::Bind(&TestService::OnServiceStarted, |
| 36 base::Unretained(this))); |
| 37 base::AutoLock auto_lock(service_started_lock_); |
| 38 while (!service_started_) |
| 39 on_service_started_.Wait(); |
| 40 } |
| 41 |
| 42 void TestService::OnServiceStarted() { |
| 43 base::AutoLock auto_lock(service_started_lock_); |
| 44 service_started_ = true; |
| 45 on_service_started_.Signal(); |
| 46 } |
| 47 |
| 48 void TestService::Run(MessageLoop* message_loop) { |
| 49 Bus::Options bus_options; |
| 50 bus_options.bus_type = Bus::SESSION; |
| 51 bus_options.connection_type = Bus::PRIVATE; |
| 52 bus_ = new Bus(bus_options); |
| 53 |
| 54 exported_object_ = bus_->GetExportedObject( |
| 55 "org.chromium.TestService", |
| 56 "/org/chromium/TestObject"); |
| 57 CHECK(exported_object_->ExportMethodAndBlock( |
| 58 "org.chromium.TestInterface", |
| 59 "Echo", |
| 60 base::Bind(&TestService::Echo, |
| 61 base::Unretained(this)))); |
| 62 CHECK(exported_object_->ExportMethodAndBlock( |
| 63 "org.chromium.TestInterface", |
| 64 "SlowEcho", |
| 65 base::Bind(&TestService::SlowEcho, |
| 66 base::Unretained(this)))); |
| 67 CHECK(exported_object_->ExportMethodAndBlock( |
| 68 "org.chromium.TestInterface", |
| 69 "BrokenMethod", |
| 70 base::Bind(&TestService::BrokenMethod, |
| 71 base::Unretained(this)))); |
| 72 |
| 73 message_loop->Run(); |
| 74 } |
| 75 |
| 76 void TestService::CleanUp() { |
| 77 bus_->ShutdownAndBlock(); |
| 78 } |
| 79 |
| 80 Response* TestService::Echo(MethodCall* method_call) { |
| 81 MessageReader reader(method_call); |
| 82 std::string text_message; |
| 83 if (!reader.PopString(&text_message)) |
| 84 return NULL; |
| 85 |
| 86 Response* response = Response::FromMethodCall(method_call); |
| 87 MessageWriter writer(response); |
| 88 writer.AppendString(text_message); |
| 89 return response; |
| 90 } |
| 91 |
| 92 Response* TestService::SlowEcho(MethodCall* method_call) { |
| 93 base::PlatformThread::Sleep(kSlowEchoSleepMs); |
| 94 return Echo(method_call); |
| 95 } |
| 96 |
| 97 Response* TestService::BrokenMethod(MethodCall* method_call) { |
| 98 return NULL; |
| 99 } |
| 100 |
| 101 } // namespace dbus |
OLD | NEW |