1 // Copyright (c) 2012 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.
5 #include "chromeos/dbus/introspectable_client.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
13 #include "dbus/message.h"
14 #include "dbus/object_path.h"
15 #include "dbus/object_proxy.h"
16 #include "third_party/libxml/chromium/libxml_utils.h"
20 // D-Bus specification constants.
21 const char kIntrospectableInterface
[] = "org.freedesktop.DBus.Introspectable";
22 const char kIntrospect
[] = "Introspect";
24 // String constants used for parsing D-Bus Introspection XML data.
25 const char kInterfaceNode
[] = "interface";
26 const char kInterfaceNameAttribute
[] = "name";
32 // The IntrospectableClient implementation used in production.
33 class IntrospectableClientImpl
: public IntrospectableClient
{
35 IntrospectableClientImpl() : bus_(NULL
), weak_ptr_factory_(this) {}
37 virtual ~IntrospectableClientImpl() {
40 // IntrospectableClient override.
41 virtual void Introspect(const std::string
& service_name
,
42 const dbus::ObjectPath
& object_path
,
43 const IntrospectCallback
& callback
) OVERRIDE
{
44 dbus::MethodCall
method_call(kIntrospectableInterface
, kIntrospect
);
46 dbus::ObjectProxy
* object_proxy
= bus_
->GetObjectProxy(service_name
,
49 object_proxy
->CallMethod(
51 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
52 base::Bind(&IntrospectableClientImpl::OnIntrospect
,
53 weak_ptr_factory_
.GetWeakPtr(),
54 service_name
, object_path
, callback
));
58 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{ bus_
= bus
; }
61 // Called by dbus:: when a response for Introspect() is recieved.
62 void OnIntrospect(const std::string
& service_name
,
63 const dbus::ObjectPath
& object_path
,
64 const IntrospectCallback
& callback
,
65 dbus::Response
* response
) {
69 if (response
!= NULL
) {
70 dbus::MessageReader
reader(response
);
71 if (!reader
.PopString(&xml_data
)) {
72 LOG(WARNING
) << "Introspect response has incorrect paramters: "
73 << response
->ToString();
80 callback
.Run(service_name
, object_path
, xml_data
, success
);
85 // Weak pointer factory for generating 'this' pointers that might live longer
87 // Note: This should remain the last member so it'll be destroyed and
88 // invalidate its weak pointers before any other members are destroyed.
89 base::WeakPtrFactory
<IntrospectableClientImpl
> weak_ptr_factory_
;
91 DISALLOW_COPY_AND_ASSIGN(IntrospectableClientImpl
);
94 IntrospectableClient::IntrospectableClient() {
97 IntrospectableClient::~IntrospectableClient() {
101 std::vector
<std::string
>
102 IntrospectableClient::GetInterfacesFromIntrospectResult(
103 const std::string
& xml_data
) {
104 std::vector
<std::string
> interfaces
;
107 if (!reader
.Load(xml_data
))
111 // Skip to the next open tag, exit when done.
112 while (!reader
.SkipToElement()) {
113 if (!reader
.Read()) {
118 // Only look at interface nodes.
119 if (reader
.NodeName() != kInterfaceNode
)
122 // Skip if missing the interface name.
123 std::string interface_name
;
124 if (!reader
.NodeAttribute(kInterfaceNameAttribute
, &interface_name
))
127 interfaces
.push_back(interface_name
);
128 } while (reader
.Read());
134 IntrospectableClient
* IntrospectableClient::Create() {
135 return new IntrospectableClientImpl();
138 } // namespace chromeos