This source file includes following definitions.
- EmptyCallback
- num_exported_methods_
- StartService
- WaitUntilServiceIsStarted
- ShutdownAndBlock
- HasDBusThread
- ShutdownAndBlockInternal
- SendTestSignal
- SendTestSignalFromRoot
- SendTestSignalInternal
- SendTestSignalFromRootInternal
- RequestOwnership
- RequestOwnershipInternal
- OnOwnership
- ReleaseOwnership
- ReleaseOwnershipInternal
- OnExported
- Run
- Echo
- SlowEcho
- AsyncEcho
- BrokenMethod
- GetAllProperties
- GetProperty
- SetProperty
- PerformAction
- PerformActionResponse
- OwnershipReleased
- OwnershipRegained
- GetManagedObjects
- AddPropertiesToWriter
- AddObject
- AddObjectInternal
- RemoveObject
- RemoveObjectInternal
- SendPropertyChangedSignal
- SendPropertyChangedSignalInternal
#include "dbus/test_service.h"
#include "base/bind.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "dbus/bus.h"
#include "dbus/exported_object.h"
#include "dbus/message.h"
#include "dbus/object_manager.h"
#include "dbus/object_path.h"
#include "dbus/property.h"
namespace {
void EmptyCallback(bool ) {
}
}
namespace dbus {
const int TestService::kNumMethodsToExport = 9;
TestService::Options::Options()
: request_ownership_options(Bus::REQUIRE_PRIMARY) {
}
TestService::Options::~Options() {
}
TestService::TestService(const Options& options)
: base::Thread("TestService"),
request_ownership_options_(options.request_ownership_options),
dbus_task_runner_(options.dbus_task_runner),
on_name_obtained_(false, false),
num_exported_methods_(0) {
}
TestService::~TestService() {
Stop();
}
bool TestService::StartService() {
base::Thread::Options thread_options;
thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
return StartWithOptions(thread_options);
}
bool TestService::WaitUntilServiceIsStarted() {
const base::TimeDelta timeout(TestTimeouts::action_max_timeout());
return on_name_obtained_.TimedWait(timeout);
}
void TestService::ShutdownAndBlock() {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::ShutdownAndBlockInternal,
base::Unretained(this)));
}
bool TestService::HasDBusThread() {
return bus_->HasDBusThread();
}
void TestService::ShutdownAndBlockInternal() {
if (HasDBusThread())
bus_->ShutdownOnDBusThreadAndBlock();
else
bus_->ShutdownAndBlock();
}
void TestService::SendTestSignal(const std::string& message) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::SendTestSignalInternal,
base::Unretained(this),
message));
}
void TestService::SendTestSignalFromRoot(const std::string& message) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::SendTestSignalFromRootInternal,
base::Unretained(this),
message));
}
void TestService::SendTestSignalInternal(const std::string& message) {
Signal signal("org.chromium.TestInterface", "Test");
MessageWriter writer(&signal);
writer.AppendString(message);
exported_object_->SendSignal(&signal);
}
void TestService::SendTestSignalFromRootInternal(const std::string& message) {
Signal signal("org.chromium.TestInterface", "Test");
MessageWriter writer(&signal);
writer.AppendString(message);
bus_->RequestOwnership("org.chromium.TestService",
request_ownership_options_,
base::Bind(&TestService::OnOwnership,
base::Unretained(this),
base::Bind(&EmptyCallback)));
ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/"));
root_object->SendSignal(&signal);
}
void TestService::RequestOwnership(base::Callback<void(bool)> callback) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::RequestOwnershipInternal,
base::Unretained(this),
callback));
}
void TestService::RequestOwnershipInternal(
base::Callback<void(bool)> callback) {
bus_->RequestOwnership("org.chromium.TestService",
request_ownership_options_,
base::Bind(&TestService::OnOwnership,
base::Unretained(this),
callback));
}
void TestService::OnOwnership(base::Callback<void(bool)> callback,
const std::string& service_name,
bool success) {
has_ownership_ = success;
LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
callback.Run(success);
on_name_obtained_.Signal();
}
void TestService::ReleaseOwnership(base::Closure callback) {
bus_->GetDBusTaskRunner()->PostTask(
FROM_HERE,
base::Bind(&TestService::ReleaseOwnershipInternal,
base::Unretained(this),
callback));
}
void TestService::ReleaseOwnershipInternal(
base::Closure callback) {
bus_->ReleaseOwnership("org.chromium.TestService");
has_ownership_ = false;
bus_->GetOriginTaskRunner()->PostTask(
FROM_HERE,
callback);
}
void TestService::OnExported(const std::string& interface_name,
const std::string& method_name,
bool success) {
if (!success) {
LOG(ERROR) << "Failed to export: " << interface_name << "."
<< method_name;
return;
}
++num_exported_methods_;
if (num_exported_methods_ == kNumMethodsToExport) {
bus_->RequestOwnership("org.chromium.TestService",
request_ownership_options_,
base::Bind(&TestService::OnOwnership,
base::Unretained(this),
base::Bind(&EmptyCallback)));
}
}
void TestService::Run(base::MessageLoop* message_loop) {
Bus::Options bus_options;
bus_options.bus_type = Bus::SESSION;
bus_options.connection_type = Bus::PRIVATE;
bus_options.dbus_task_runner = dbus_task_runner_;
bus_ = new Bus(bus_options);
exported_object_ = bus_->GetExportedObject(
ObjectPath("/org/chromium/TestObject"));
int num_methods = 0;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"Echo",
base::Bind(&TestService::Echo,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"SlowEcho",
base::Bind(&TestService::SlowEcho,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"AsyncEcho",
base::Bind(&TestService::AsyncEcho,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"BrokenMethod",
base::Bind(&TestService::BrokenMethod,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"PerformAction",
base::Bind(&TestService::PerformAction,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
kPropertiesInterface,
kPropertiesGetAll,
base::Bind(&TestService::GetAllProperties,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
kPropertiesInterface,
kPropertiesGet,
base::Bind(&TestService::GetProperty,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
kPropertiesInterface,
kPropertiesSet,
base::Bind(&TestService::SetProperty,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_manager_ = bus_->GetExportedObject(
ObjectPath("/org/chromium/TestService"));
exported_object_manager_->ExportMethod(
kObjectManagerInterface,
kObjectManagerGetManagedObjects,
base::Bind(&TestService::GetManagedObjects,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
if (num_methods != kNumMethodsToExport) {
LOG(ERROR) << "The number of methods does not match";
}
message_loop->Run();
}
void TestService::Echo(MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string text_message;
if (!reader.PopString(&text_message)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
writer.AppendString(text_message);
response_sender.Run(response.Pass());
}
void TestService::SlowEcho(MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
Echo(method_call, response_sender);
}
void TestService::AsyncEcho(MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
message_loop()->PostDelayedTask(FROM_HERE,
base::Bind(&TestService::Echo,
base::Unretained(this),
method_call,
response_sender),
TestTimeouts::tiny_timeout());
}
void TestService::BrokenMethod(MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
response_sender.Run(scoped_ptr<Response>());
}
void TestService::GetAllProperties(
MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string interface;
if (!reader.PopString(&interface)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
AddPropertiesToWriter(&writer);
response_sender.Run(response.Pass());
}
void TestService::GetProperty(MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string interface;
if (!reader.PopString(&interface)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
std::string name;
if (!reader.PopString(&name)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
if (name == "Name") {
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
writer.AppendVariantOfString("TestService");
response_sender.Run(response.Pass());
} else if (name == "Version") {
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
writer.AppendVariantOfInt16(20);
response_sender.Run(response.Pass());
} else if (name == "Methods") {
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
MessageWriter variant_writer(NULL);
MessageWriter variant_array_writer(NULL);
writer.OpenVariant("as", &variant_writer);
variant_writer.OpenArray("s", &variant_array_writer);
variant_array_writer.AppendString("Echo");
variant_array_writer.AppendString("SlowEcho");
variant_array_writer.AppendString("AsyncEcho");
variant_array_writer.AppendString("BrokenMethod");
variant_writer.CloseContainer(&variant_array_writer);
writer.CloseContainer(&variant_writer);
response_sender.Run(response.Pass());
} else if (name == "Objects") {
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
MessageWriter variant_writer(NULL);
MessageWriter variant_array_writer(NULL);
writer.OpenVariant("ao", &variant_writer);
variant_writer.OpenArray("o", &variant_array_writer);
variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
variant_writer.CloseContainer(&variant_array_writer);
writer.CloseContainer(&variant_writer);
response_sender.Run(response.Pass());
} else if (name == "Bytes") {
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
MessageWriter variant_writer(NULL);
MessageWriter variant_array_writer(NULL);
writer.OpenVariant("ay", &variant_writer);
const uint8 bytes[] = { 0x54, 0x65, 0x73, 0x74 };
variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
writer.CloseContainer(&variant_writer);
response_sender.Run(response.Pass());
} else {
response_sender.Run(scoped_ptr<Response>());
return;
}
}
void TestService::SetProperty(MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string interface;
if (!reader.PopString(&interface)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
std::string name;
if (!reader.PopString(&name)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
if (name != "Name") {
response_sender.Run(scoped_ptr<Response>());
return;
}
std::string value;
if (!reader.PopVariantOfString(&value)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
SendPropertyChangedSignal(value);
response_sender.Run(Response::FromMethodCall(method_call));
}
void TestService::PerformAction(
MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string action;
ObjectPath object_path;
if (!reader.PopString(&action) || !reader.PopObjectPath(&object_path)) {
response_sender.Run(scoped_ptr<Response>());
return;
}
if (action == "AddObject")
AddObject(object_path);
else if (action == "RemoveObject")
RemoveObject(object_path);
else if (action == "ReleaseOwnership") {
ReleaseOwnership(base::Bind(&TestService::PerformActionResponse,
base::Unretained(this),
method_call, response_sender));
return;
} else if (action == "Ownership") {
ReleaseOwnership(base::Bind(&TestService::OwnershipReleased,
base::Unretained(this),
method_call, response_sender));
return;
}
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
response_sender.Run(response.Pass());
}
void TestService::PerformActionResponse(
MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
response_sender.Run(response.Pass());
}
void TestService::OwnershipReleased(
MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
RequestOwnership(base::Bind(&TestService::OwnershipRegained,
base::Unretained(this),
method_call, response_sender));
}
void TestService::OwnershipRegained(
MethodCall* method_call,
ExportedObject::ResponseSender response_sender,
bool success) {
PerformActionResponse(method_call, response_sender);
}
void TestService::GetManagedObjects(
MethodCall* method_call,
ExportedObject::ResponseSender response_sender) {
scoped_ptr<Response> response = Response::FromMethodCall(method_call);
MessageWriter writer(response.get());
MessageWriter array_writer(NULL);
MessageWriter dict_entry_writer(NULL);
MessageWriter object_array_writer(NULL);
MessageWriter object_dict_entry_writer(NULL);
writer.OpenArray("{oa{sa{sv}}}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendObjectPath(ObjectPath("/org/chromium/TestObject"));
dict_entry_writer.OpenArray("{sa{sv}}", &object_array_writer);
object_array_writer.OpenDictEntry(&object_dict_entry_writer);
object_dict_entry_writer.AppendString("org.chromium.TestInterface");
AddPropertiesToWriter(&object_dict_entry_writer);
object_array_writer.CloseContainer(&object_dict_entry_writer);
dict_entry_writer.CloseContainer(&object_array_writer);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
response_sender.Run(response.Pass());
}
void TestService::AddPropertiesToWriter(MessageWriter* writer) {
MessageWriter array_writer(NULL);
MessageWriter dict_entry_writer(NULL);
MessageWriter variant_writer(NULL);
MessageWriter variant_array_writer(NULL);
writer->OpenArray("{sv}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Name");
dict_entry_writer.AppendVariantOfString("TestService");
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Version");
dict_entry_writer.AppendVariantOfInt16(10);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Methods");
dict_entry_writer.OpenVariant("as", &variant_writer);
variant_writer.OpenArray("s", &variant_array_writer);
variant_array_writer.AppendString("Echo");
variant_array_writer.AppendString("SlowEcho");
variant_array_writer.AppendString("AsyncEcho");
variant_array_writer.AppendString("BrokenMethod");
variant_writer.CloseContainer(&variant_array_writer);
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Objects");
dict_entry_writer.OpenVariant("ao", &variant_writer);
variant_writer.OpenArray("o", &variant_array_writer);
variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
variant_writer.CloseContainer(&variant_array_writer);
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Bytes");
dict_entry_writer.OpenVariant("ay", &variant_writer);
const uint8 bytes[] = { 0x54, 0x65, 0x73, 0x74 };
variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
writer->CloseContainer(&array_writer);
}
void TestService::AddObject(const ObjectPath& object_path) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::AddObjectInternal,
base::Unretained(this),
object_path));
}
void TestService::AddObjectInternal(const ObjectPath& object_path) {
Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded);
MessageWriter writer(&signal);
writer.AppendObjectPath(object_path);
MessageWriter array_writer(NULL);
MessageWriter dict_entry_writer(NULL);
writer.OpenArray("{sa{sv}}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("org.chromium.TestInterface");
AddPropertiesToWriter(&dict_entry_writer);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
exported_object_manager_->SendSignal(&signal);
}
void TestService::RemoveObject(const ObjectPath& object_path) {
message_loop()->PostTask(FROM_HERE,
base::Bind(&TestService::RemoveObjectInternal,
base::Unretained(this),
object_path));
}
void TestService::RemoveObjectInternal(const ObjectPath& object_path) {
Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved);
MessageWriter writer(&signal);
writer.AppendObjectPath(object_path);
std::vector<std::string> interfaces;
interfaces.push_back("org.chromium.TestInterface");
writer.AppendArrayOfStrings(interfaces);
exported_object_manager_->SendSignal(&signal);
}
void TestService::SendPropertyChangedSignal(const std::string& name) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::SendPropertyChangedSignalInternal,
base::Unretained(this),
name));
}
void TestService::SendPropertyChangedSignalInternal(const std::string& name) {
Signal signal(kPropertiesInterface, kPropertiesChanged);
MessageWriter writer(&signal);
writer.AppendString("org.chromium.TestInterface");
MessageWriter array_writer(NULL);
MessageWriter dict_entry_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Name");
dict_entry_writer.AppendVariantOfString(name);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
exported_object_->SendSignal(&signal);
}
}