This source file includes following definitions.
- weak_ptr_factory_
- Introspect
- Init
- OnIntrospect
- GetInterfacesFromIntrospectResult
- Create
#include "chromeos/dbus/introspectable_client.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "third_party/libxml/chromium/libxml_utils.h"
namespace {
const char kIntrospectableInterface[] = "org.freedesktop.DBus.Introspectable";
const char kIntrospect[] = "Introspect";
const char kInterfaceNode[] = "interface";
const char kInterfaceNameAttribute[] = "name";
}
namespace chromeos {
class IntrospectableClientImpl : public IntrospectableClient {
public:
IntrospectableClientImpl() : bus_(NULL), weak_ptr_factory_(this) {}
virtual ~IntrospectableClientImpl() {
}
virtual void Introspect(const std::string& service_name,
const dbus::ObjectPath& object_path,
const IntrospectCallback& callback) OVERRIDE {
dbus::MethodCall method_call(kIntrospectableInterface, kIntrospect);
dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(service_name,
object_path);
object_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&IntrospectableClientImpl::OnIntrospect,
weak_ptr_factory_.GetWeakPtr(),
service_name, object_path, callback));
}
protected:
virtual void Init(dbus::Bus* bus) OVERRIDE { bus_ = bus; }
private:
void OnIntrospect(const std::string& service_name,
const dbus::ObjectPath& object_path,
const IntrospectCallback& callback,
dbus::Response* response) {
bool success = false;
std::string xml_data;
if (response != NULL) {
dbus::MessageReader reader(response);
if (!reader.PopString(&xml_data)) {
LOG(WARNING) << "Introspect response has incorrect paramters: "
<< response->ToString();
} else {
success = true;
}
}
callback.Run(service_name, object_path, xml_data, success);
}
dbus::Bus* bus_;
base::WeakPtrFactory<IntrospectableClientImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(IntrospectableClientImpl);
};
IntrospectableClient::IntrospectableClient() {
}
IntrospectableClient::~IntrospectableClient() {
}
std::vector<std::string>
IntrospectableClient::GetInterfacesFromIntrospectResult(
const std::string& xml_data) {
std::vector<std::string> interfaces;
XmlReader reader;
if (!reader.Load(xml_data))
return interfaces;
do {
while (!reader.SkipToElement()) {
if (!reader.Read()) {
return interfaces;
}
}
if (reader.NodeName() != kInterfaceNode)
continue;
std::string interface_name;
if (!reader.NodeAttribute(kInterfaceNameAttribute, &interface_name))
continue;
interfaces.push_back(interface_name);
} while (reader.Read());
return interfaces;
}
IntrospectableClient* IntrospectableClient::Create() {
return new IntrospectableClientImpl();
}
}