[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / chromeos / dbus / introspectable_client.cc
blob9e56a9958107807115ac6e081b85e95c6489f7e9
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"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "dbus/bus.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"
18 namespace {
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";
28 } // namespace
30 namespace chromeos {
32 // The IntrospectableClient implementation used in production.
33 class IntrospectableClientImpl : public IntrospectableClient {
34 public:
35 IntrospectableClientImpl() : bus_(NULL), weak_ptr_factory_(this) {}
37 ~IntrospectableClientImpl() override {}
39 // IntrospectableClient override.
40 void Introspect(const std::string& service_name,
41 const dbus::ObjectPath& object_path,
42 const IntrospectCallback& callback) override {
43 dbus::MethodCall method_call(kIntrospectableInterface, kIntrospect);
45 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(service_name,
46 object_path);
48 object_proxy->CallMethod(
49 &method_call,
50 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
51 base::Bind(&IntrospectableClientImpl::OnIntrospect,
52 weak_ptr_factory_.GetWeakPtr(),
53 service_name, object_path, callback));
56 protected:
57 void Init(dbus::Bus* bus) override { bus_ = bus; }
59 private:
60 // Called by dbus:: when a response for Introspect() is recieved.
61 void OnIntrospect(const std::string& service_name,
62 const dbus::ObjectPath& object_path,
63 const IntrospectCallback& callback,
64 dbus::Response* response) {
65 // Parse response.
66 bool success = false;
67 std::string xml_data;
68 if (response != NULL) {
69 dbus::MessageReader reader(response);
70 if (!reader.PopString(&xml_data)) {
71 LOG(WARNING) << "Introspect response has incorrect paramters: "
72 << response->ToString();
73 } else {
74 success = true;
78 // Notify client.
79 callback.Run(service_name, object_path, xml_data, success);
82 dbus::Bus* bus_;
84 // Weak pointer factory for generating 'this' pointers that might live longer
85 // than we do.
86 // Note: This should remain the last member so it'll be destroyed and
87 // invalidate its weak pointers before any other members are destroyed.
88 base::WeakPtrFactory<IntrospectableClientImpl> weak_ptr_factory_;
90 DISALLOW_COPY_AND_ASSIGN(IntrospectableClientImpl);
93 IntrospectableClient::IntrospectableClient() {
96 IntrospectableClient::~IntrospectableClient() {
99 // static
100 std::vector<std::string>
101 IntrospectableClient::GetInterfacesFromIntrospectResult(
102 const std::string& xml_data) {
103 std::vector<std::string> interfaces;
105 XmlReader reader;
106 if (!reader.Load(xml_data))
107 return interfaces;
109 do {
110 // Skip to the next open tag, exit when done.
111 while (!reader.SkipToElement()) {
112 if (!reader.Read()) {
113 return interfaces;
117 // Only look at interface nodes.
118 if (reader.NodeName() != kInterfaceNode)
119 continue;
121 // Skip if missing the interface name.
122 std::string interface_name;
123 if (!reader.NodeAttribute(kInterfaceNameAttribute, &interface_name))
124 continue;
126 interfaces.push_back(interface_name);
127 } while (reader.Read());
129 return interfaces;
132 // static
133 IntrospectableClient* IntrospectableClient::Create() {
134 return new IntrospectableClientImpl();
137 } // namespace chromeos