Revert of Revert of Fix text clipping in CompositorFrameTime Display (patchset #1...
[chromium-blink-merge.git] / chromeos / dbus / introspectable_client.cc
blob71508f2c049f7e642e98ccc6f9da1cf9f3211485
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"
8 #include "base/bind.h"
9 #include "base/logging.h"
10 #include "dbus/bus.h"
11 #include "dbus/message.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/libxml/chromium/libxml_utils.h"
15 namespace {
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";
25 } // namespace
27 namespace chromeos {
29 // The IntrospectableClient implementation used in production.
30 class IntrospectableClientImpl : public IntrospectableClient {
31 public:
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,
43 object_path);
45 object_proxy->CallMethod(
46 &method_call,
47 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
48 base::Bind(&IntrospectableClientImpl::OnIntrospect,
49 weak_ptr_factory_.GetWeakPtr(),
50 service_name, object_path, callback));
53 protected:
54 void Init(dbus::Bus* bus) override { bus_ = bus; }
56 private:
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) {
62 // Parse response.
63 bool success = false;
64 std::string xml_data;
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();
70 } else {
71 success = true;
75 // Notify client.
76 callback.Run(service_name, object_path, xml_data, success);
79 dbus::Bus* bus_;
81 // Weak pointer factory for generating 'this' pointers that might live longer
82 // than we do.
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() {
96 // static
97 std::vector<std::string>
98 IntrospectableClient::GetInterfacesFromIntrospectResult(
99 const std::string& xml_data) {
100 std::vector<std::string> interfaces;
102 XmlReader reader;
103 if (!reader.Load(xml_data))
104 return interfaces;
106 do {
107 // Skip to the next open tag, exit when done.
108 while (!reader.SkipToElement()) {
109 if (!reader.Read()) {
110 return interfaces;
114 // Only look at interface nodes.
115 if (reader.NodeName() != kInterfaceNode)
116 continue;
118 // Skip if missing the interface name.
119 std::string interface_name;
120 if (!reader.NodeAttribute(kInterfaceNameAttribute, &interface_name))
121 continue;
123 interfaces.push_back(interface_name);
124 } while (reader.Read());
126 return interfaces;
129 // static
130 IntrospectableClient* IntrospectableClient::Create() {
131 return new IntrospectableClientImpl();
134 } // namespace chromeos