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 "chrome/browser/chromeos/dbus/introspectable_client.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
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 | |
17 namespace { | |
18 | |
19 // D-Bus specification constants. | |
20 const char kIntrospectableInterface[] = "org.freedesktop.DBus.Introspectable"; | |
21 const char kIntrospect[] = "Introspect"; | |
22 | |
23 } // namespace | |
24 | |
25 namespace chromeos { | |
26 | |
27 // The IntrospectableClient implementation used in production. | |
28 class IntrospectableClientImpl : public IntrospectableClient { | |
29 public: | |
30 explicit IntrospectableClientImpl(dbus::Bus* bus) | |
31 : weak_ptr_factory_(this), | |
32 bus_(bus) { | |
33 DVLOG(1) << "Creating IntrospectableClientImpl"; | |
34 } | |
35 | |
36 virtual ~IntrospectableClientImpl() { | |
37 } | |
38 | |
39 // IntrospectableClient override. | |
40 virtual 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); | |
44 | |
45 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(service_name, | |
46 object_path); | |
47 | |
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)); | |
54 } | |
55 | |
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; | |
72 } | |
73 } | |
74 | |
75 // Notify client. | |
76 callback.Run(service_name, object_path, xml_data, success); | |
77 } | |
78 | |
79 // Weak pointer factory for generating 'this' pointers that might live longer | |
80 // than we do. | |
81 base::WeakPtrFactory<IntrospectableClientImpl> weak_ptr_factory_; | |
82 | |
83 dbus::Bus* bus_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(IntrospectableClientImpl); | |
86 }; | |
87 | |
88 // The IntrospectableClient implementation used on Linux desktop, which does | |
89 // nothing. | |
90 class IntrospectableClientStubImpl : public IntrospectableClient { | |
91 public: | |
92 // IntrospectableClient override. | |
93 virtual void Introspect(const std::string& service_name, | |
94 const dbus::ObjectPath& object_path, | |
95 const IntrospectCallback& callback) OVERRIDE { | |
96 VLOG(1) << "Introspect: " << service_name << " " << object_path.value(); | |
97 callback.Run(service_name, object_path, "", false); | |
98 } | |
99 }; | |
100 | |
101 IntrospectableClient::IntrospectableClient() { | |
102 } | |
103 | |
104 IntrospectableClient::~IntrospectableClient() { | |
105 } | |
106 | |
107 // static | |
108 IntrospectableClient* IntrospectableClient::Create( | |
109 DBusClientImplementationType type, | |
110 dbus::Bus* bus) { | |
111 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
112 return new IntrospectableClientImpl(bus); | |
113 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
114 return new IntrospectableClientStubImpl(); | |
115 } | |
116 | |
117 } // namespace chromeos | |
OLD | NEW |