| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chromeos/dbus/introspectable_client.h" | |
| 6 | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "dbus/bus.h" | |
| 12 #include "dbus/message.h" | |
| 13 #include "dbus/object_proxy.h" | |
| 14 #include "third_party/libxml/chromium/libxml_utils.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // D-Bus specification constants. | |
| 19 const char kIntrospectableInterface[] = "org.freedesktop.DBus.Introspectable"; | |
| 20 const char kIntrospect[] = "Introspect"; | |
| 21 | |
| 22 // String constants used for parsing D-Bus Introspection XML data. | |
| 23 const char kInterfaceNode[] = "interface"; | |
| 24 const char kInterfaceNameAttribute[] = "name"; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 namespace chromeos { | |
| 29 | |
| 30 // The IntrospectableClient implementation used in production. | |
| 31 class IntrospectableClientImpl : public IntrospectableClient { | |
| 32 public: | |
| 33 IntrospectableClientImpl() : bus_(NULL), weak_ptr_factory_(this) {} | |
| 34 | |
| 35 ~IntrospectableClientImpl() override {} | |
| 36 | |
| 37 // IntrospectableClient override. | |
| 38 void Introspect(const std::string& service_name, | |
| 39 const dbus::ObjectPath& object_path, | |
| 40 const IntrospectCallback& callback) override { | |
| 41 dbus::MethodCall method_call(kIntrospectableInterface, kIntrospect); | |
| 42 | |
| 43 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(service_name, | |
| 44 object_path); | |
| 45 | |
| 46 object_proxy->CallMethod( | |
| 47 &method_call, | |
| 48 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 49 base::Bind(&IntrospectableClientImpl::OnIntrospect, | |
| 50 weak_ptr_factory_.GetWeakPtr(), | |
| 51 service_name, object_path, callback)); | |
| 52 } | |
| 53 | |
| 54 protected: | |
| 55 void Init(dbus::Bus* bus) override { bus_ = bus; } | |
| 56 | |
| 57 private: | |
| 58 // Called by dbus:: when a response for Introspect() is recieved. | |
| 59 void OnIntrospect(const std::string& service_name, | |
| 60 const dbus::ObjectPath& object_path, | |
| 61 const IntrospectCallback& callback, | |
| 62 dbus::Response* response) { | |
| 63 // Parse response. | |
| 64 bool success = false; | |
| 65 std::string xml_data; | |
| 66 if (response != NULL) { | |
| 67 dbus::MessageReader reader(response); | |
| 68 if (!reader.PopString(&xml_data)) { | |
| 69 LOG(WARNING) << "Introspect response has incorrect paramters: " | |
| 70 << response->ToString(); | |
| 71 } else { | |
| 72 success = true; | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 // Notify client. | |
| 77 callback.Run(service_name, object_path, xml_data, success); | |
| 78 } | |
| 79 | |
| 80 dbus::Bus* bus_; | |
| 81 | |
| 82 // Weak pointer factory for generating 'this' pointers that might live longer | |
| 83 // than we do. | |
| 84 // Note: This should remain the last member so it'll be destroyed and | |
| 85 // invalidate its weak pointers before any other members are destroyed. | |
| 86 base::WeakPtrFactory<IntrospectableClientImpl> weak_ptr_factory_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(IntrospectableClientImpl); | |
| 89 }; | |
| 90 | |
| 91 IntrospectableClient::IntrospectableClient() { | |
| 92 } | |
| 93 | |
| 94 IntrospectableClient::~IntrospectableClient() { | |
| 95 } | |
| 96 | |
| 97 // static | |
| 98 std::vector<std::string> | |
| 99 IntrospectableClient::GetInterfacesFromIntrospectResult( | |
| 100 const std::string& xml_data) { | |
| 101 std::vector<std::string> interfaces; | |
| 102 | |
| 103 XmlReader reader; | |
| 104 if (!reader.Load(xml_data)) | |
| 105 return interfaces; | |
| 106 | |
| 107 do { | |
| 108 // Skip to the next open tag, exit when done. | |
| 109 while (!reader.SkipToElement()) { | |
| 110 if (!reader.Read()) { | |
| 111 return interfaces; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 // Only look at interface nodes. | |
| 116 if (reader.NodeName() != kInterfaceNode) | |
| 117 continue; | |
| 118 | |
| 119 // Skip if missing the interface name. | |
| 120 std::string interface_name; | |
| 121 if (!reader.NodeAttribute(kInterfaceNameAttribute, &interface_name)) | |
| 122 continue; | |
| 123 | |
| 124 interfaces.push_back(interface_name); | |
| 125 } while (reader.Read()); | |
| 126 | |
| 127 return interfaces; | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 IntrospectableClient* IntrospectableClient::Create() { | |
| 132 return new IntrospectableClientImpl(); | |
| 133 } | |
| 134 | |
| 135 } // namespace chromeos | |
| OLD | NEW |