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"
9 #include "base/logging.h"
11 #include "dbus/message.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/libxml/chromium/libxml_utils.h"
17 // D-Bus specification constants.
18 const char kIntrospectableInterface
[] = "org.freedesktop.DBus.Introspectable";
19 const char kIntrospect
[] = "Introspect";
21 // String constants used for parsing D-Bus Introspection XML data.
22 const char kInterfaceNode
[] = "interface";
23 const char kInterfaceNameAttribute
[] = "name";
29 // The IntrospectableClient implementation used in production.
30 class IntrospectableClientImpl
: public IntrospectableClient
{
32 IntrospectableClientImpl() : bus_(NULL
), weak_ptr_factory_(this) {}
34 ~IntrospectableClientImpl() override
{}
36 // IntrospectableClient override.
37 void Introspect(const std::string
& service_name
,
38 const dbus::ObjectPath
& object_path
,
39 const IntrospectCallback
& callback
) override
{
40 dbus::MethodCall
method_call(kIntrospectableInterface
, kIntrospect
);
42 dbus::ObjectProxy
* object_proxy
= bus_
->GetObjectProxy(service_name
,
45 object_proxy
->CallMethod(
47 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
48 base::Bind(&IntrospectableClientImpl::OnIntrospect
,
49 weak_ptr_factory_
.GetWeakPtr(),
50 service_name
, object_path
, callback
));
54 void Init(dbus::Bus
* bus
) override
{ bus_
= bus
; }
57 // Called by dbus:: when a response for Introspect() is recieved.
58 void OnIntrospect(const std::string
& service_name
,
59 const dbus::ObjectPath
& object_path
,
60 const IntrospectCallback
& callback
,
61 dbus::Response
* response
) {
65 if (response
!= NULL
) {
66 dbus::MessageReader
reader(response
);
67 if (!reader
.PopString(&xml_data
)) {
68 LOG(WARNING
) << "Introspect response has incorrect paramters: "
69 << response
->ToString();
76 callback
.Run(service_name
, object_path
, xml_data
, success
);
81 // Weak pointer factory for generating 'this' pointers that might live longer
83 // Note: This should remain the last member so it'll be destroyed and
84 // invalidate its weak pointers before any other members are destroyed.
85 base::WeakPtrFactory
<IntrospectableClientImpl
> weak_ptr_factory_
;
87 DISALLOW_COPY_AND_ASSIGN(IntrospectableClientImpl
);
90 IntrospectableClient::IntrospectableClient() {
93 IntrospectableClient::~IntrospectableClient() {
97 std::vector
<std::string
>
98 IntrospectableClient::GetInterfacesFromIntrospectResult(
99 const std::string
& xml_data
) {
100 std::vector
<std::string
> interfaces
;
103 if (!reader
.Load(xml_data
))
107 // Skip to the next open tag, exit when done.
108 while (!reader
.SkipToElement()) {
109 if (!reader
.Read()) {
114 // Only look at interface nodes.
115 if (reader
.NodeName() != kInterfaceNode
)
118 // Skip if missing the interface name.
119 std::string interface_name
;
120 if (!reader
.NodeAttribute(kInterfaceNameAttribute
, &interface_name
))
123 interfaces
.push_back(interface_name
);
124 } while (reader
.Read());
130 IntrospectableClient
* IntrospectableClient::Create() {
131 return new IntrospectableClientImpl();
134 } // namespace chromeos