Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_manager_client.cc
blobb8b312677af1479f051e16366d94b4180b45d5be
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/bluetooth_manager_client.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chromeos/dbus/bluetooth_property.h"
13 #include "dbus/bus.h"
14 #include "dbus/message.h"
15 #include "dbus/object_path.h"
16 #include "dbus/object_proxy.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
19 namespace chromeos {
21 BluetoothManagerClient::Properties::Properties(
22 dbus::ObjectProxy* object_proxy,
23 const PropertyChangedCallback& callback)
24 : BluetoothPropertySet(object_proxy,
25 bluetooth_manager::kBluetoothManagerInterface,
26 callback) {
27 RegisterProperty(bluetooth_manager::kAdaptersProperty, &adapters);
30 BluetoothManagerClient::Properties::~Properties() {
34 // The BluetoothManagerClient implementation used in production.
35 class BluetoothManagerClientImpl : public BluetoothManagerClient {
36 public:
37 explicit BluetoothManagerClientImpl(dbus::Bus* bus)
38 : object_proxy_(NULL),
39 weak_ptr_factory_(this) {
40 // Create the object proxy.
41 DCHECK(bus);
42 object_proxy_ = bus->GetObjectProxy(
43 bluetooth_manager::kBluetoothManagerServiceName,
44 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath));
46 object_proxy_->ConnectToSignal(
47 bluetooth_manager::kBluetoothManagerInterface,
48 bluetooth_manager::kAdapterAddedSignal,
49 base::Bind(&BluetoothManagerClientImpl::AdapterAddedReceived,
50 weak_ptr_factory_.GetWeakPtr()),
51 base::Bind(&BluetoothManagerClientImpl::AdapterAddedConnected,
52 weak_ptr_factory_.GetWeakPtr()));
54 object_proxy_->ConnectToSignal(
55 bluetooth_manager::kBluetoothManagerInterface,
56 bluetooth_manager::kAdapterRemovedSignal,
57 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedReceived,
58 weak_ptr_factory_.GetWeakPtr()),
59 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedConnected,
60 weak_ptr_factory_.GetWeakPtr()));
62 object_proxy_->ConnectToSignal(
63 bluetooth_manager::kBluetoothManagerInterface,
64 bluetooth_manager::kDefaultAdapterChangedSignal,
65 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedReceived,
66 weak_ptr_factory_.GetWeakPtr()),
67 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedConnected,
68 weak_ptr_factory_.GetWeakPtr()));
70 // Create the properties structure.
71 properties_ = new Properties(
72 object_proxy_,
73 base::Bind(&BluetoothManagerClientImpl::OnPropertyChanged,
74 weak_ptr_factory_.GetWeakPtr()));
76 properties_->ConnectSignals();
77 properties_->GetAll();
80 virtual ~BluetoothManagerClientImpl() {
81 // Clean up the Properties structure.
82 delete properties_;
85 // BluetoothManagerClient override.
86 virtual void AddObserver(Observer* observer) OVERRIDE {
87 DCHECK(observer);
88 observers_.AddObserver(observer);
91 // BluetoothManagerClient override.
92 virtual void RemoveObserver(Observer* observer) OVERRIDE {
93 DCHECK(observer);
94 observers_.RemoveObserver(observer);
97 // BluetoothManagerClient override.
98 virtual Properties* GetProperties() OVERRIDE {
99 return properties_;
102 // BluetoothManagerClient override.
103 virtual void DefaultAdapter(const AdapterCallback& callback) OVERRIDE {
104 dbus::MethodCall method_call(
105 bluetooth_manager::kBluetoothManagerInterface,
106 bluetooth_manager::kDefaultAdapter);
108 DCHECK(object_proxy_);
109 object_proxy_->CallMethod(
110 &method_call,
111 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
112 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter,
113 weak_ptr_factory_.GetWeakPtr(), callback));
116 // BluetoothManagerClient override.
117 virtual void FindAdapter(const std::string& address,
118 const AdapterCallback& callback) OVERRIDE {
119 dbus::MethodCall method_call(
120 bluetooth_manager::kBluetoothManagerInterface,
121 bluetooth_manager::kFindAdapter);
123 dbus::MessageWriter writer(&method_call);
124 writer.AppendString(address);
126 DCHECK(object_proxy_);
127 object_proxy_->CallMethod(
128 &method_call,
129 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
130 base::Bind(&BluetoothManagerClientImpl::OnFindAdapter,
131 weak_ptr_factory_.GetWeakPtr(), callback));
134 private:
135 // Called by BluetoothPropertySet when a property value is changed,
136 // either by result of a signal or response to a GetAll() or Get()
137 // call. Informs observers.
138 void OnPropertyChanged(const std::string& property_name) {
139 FOR_EACH_OBSERVER(BluetoothManagerClient::Observer, observers_,
140 ManagerPropertyChanged(property_name));
143 // Called by dbus:: when an AdapterAdded signal is received.
144 void AdapterAddedReceived(dbus::Signal* signal) {
145 DCHECK(signal);
146 dbus::MessageReader reader(signal);
147 dbus::ObjectPath object_path;
148 if (!reader.PopObjectPath(&object_path)) {
149 LOG(WARNING) << "AdapterAdded signal has incorrect parameters: "
150 << signal->ToString();
151 return;
154 VLOG(1) << "Adapter added: " << object_path.value();
155 FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path));
158 // Called by dbus:: when the AdapterAdded signal is initially connected.
159 void AdapterAddedConnected(const std::string& interface_name,
160 const std::string& signal_name,
161 bool success) {
162 LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal.";
165 // Called by dbus:: when an AdapterRemoved signal is received.
166 void AdapterRemovedReceived(dbus::Signal* signal) {
167 DCHECK(signal);
168 dbus::MessageReader reader(signal);
169 dbus::ObjectPath object_path;
170 if (!reader.PopObjectPath(&object_path)) {
171 LOG(WARNING) << "AdapterRemoved signal has incorrect parameters: "
172 << signal->ToString();
173 return;
176 VLOG(1) << "Adapter removed: " << object_path.value();
177 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path));
180 // Called by dbus:: when the AdapterRemoved signal is initially connected.
181 void AdapterRemovedConnected(const std::string& interface_name,
182 const std::string& signal_name,
183 bool success) {
184 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal.";
187 // Called by dbus:: when a DefaultAdapterChanged signal is received.
188 void DefaultAdapterChangedReceived(dbus::Signal* signal) {
189 DCHECK(signal);
190 dbus::MessageReader reader(signal);
191 dbus::ObjectPath object_path;
192 if (!reader.PopObjectPath(&object_path)) {
193 LOG(WARNING) << "DefaultAdapterChanged signal has incorrect parameters: "
194 << signal->ToString();
195 return;
198 VLOG(1) << "Default adapter changed: " << object_path.value();
199 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(object_path));
202 // Called by dbus:: when the DefaultAdapterChanged signal is initially
203 // connected.
204 void DefaultAdapterChangedConnected(const std::string& interface_name,
205 const std::string& signal_name,
206 bool success) {
207 LOG_IF(WARNING, !success)
208 << "Failed to connect to DefaultAdapterChanged signal.";
211 // Called when a response for DefaultAdapter() is received.
212 void OnDefaultAdapter(const AdapterCallback& callback,
213 dbus::Response* response) {
214 // Parse response.
215 bool success = false;
216 dbus::ObjectPath object_path;
217 if (response != NULL) {
218 dbus::MessageReader reader(response);
219 if (!reader.PopObjectPath(&object_path)) {
220 LOG(WARNING) << "DefaultAdapter response has incorrect parameters: "
221 << response->ToString();
222 } else {
223 success = true;
225 } else {
226 LOG(WARNING) << "Failed to get default adapter.";
229 // Notify client.
230 callback.Run(object_path, success);
233 // Called when a response for FindAdapter() is received.
234 void OnFindAdapter(const AdapterCallback& callback,
235 dbus::Response* response) {
236 // Parse response.
237 bool success = false;
238 dbus::ObjectPath object_path;
239 if (response != NULL) {
240 dbus::MessageReader reader(response);
241 if (!reader.PopObjectPath(&object_path)) {
242 LOG(WARNING) << "FindAdapter response has incorrect parameters: "
243 << response->ToString();
244 } else {
245 success = true;
247 } else {
248 LOG(WARNING) << "Failed to find adapter.";
251 // Notify client.
252 callback.Run(object_path, success);
255 // D-Bus proxy for BlueZ Manager interface.
256 dbus::ObjectProxy* object_proxy_;
258 // Properties for BlueZ Manager interface.
259 Properties* properties_;
261 // List of observers interested in event notifications from us.
262 ObserverList<Observer> observers_;
264 // Weak pointer factory for generating 'this' pointers that might live longer
265 // than we do.
266 // Note: This should remain the last member so it'll be destroyed and
267 // invalidate its weak pointers before any other members are destroyed.
268 base::WeakPtrFactory<BluetoothManagerClientImpl> weak_ptr_factory_;
270 DISALLOW_COPY_AND_ASSIGN(BluetoothManagerClientImpl);
273 // The BluetoothManagerClient implementation used on Linux desktop, which does
274 // nothing.
275 class BluetoothManagerClientStubImpl : public BluetoothManagerClient {
276 public:
277 struct Properties : public BluetoothManagerClient::Properties {
278 explicit Properties(const PropertyChangedCallback& callback)
279 : BluetoothManagerClient::Properties(NULL, callback) {
282 virtual ~Properties() {
285 virtual void Get(dbus::PropertyBase* property,
286 dbus::PropertySet::GetCallback callback) OVERRIDE {
287 VLOG(1) << "Get " << property->name();
288 callback.Run(false);
291 virtual void GetAll() OVERRIDE {
292 VLOG(1) << "GetAll";
295 virtual void Set(dbus::PropertyBase* property,
296 dbus::PropertySet::SetCallback callback) OVERRIDE {
297 VLOG(1) << "Set " << property->name();
298 callback.Run(false);
302 BluetoothManagerClientStubImpl() {
303 properties_.reset(new Properties(base::Bind(
304 &BluetoothManagerClientStubImpl::OnPropertyChanged,
305 base::Unretained(this))));
307 std::vector<dbus::ObjectPath> adapters;
308 adapters.push_back(dbus::ObjectPath("/fake/hci0"));
309 properties_->adapters.ReplaceValue(adapters);
312 // BluetoothManagerClient override.
313 virtual void AddObserver(Observer* observer) OVERRIDE {
314 observers_.AddObserver(observer);
317 // BluetoothManagerClient override.
318 virtual void RemoveObserver(Observer* observer) OVERRIDE {
319 observers_.RemoveObserver(observer);
322 // BluetoothManagerClient override.
323 virtual Properties* GetProperties() OVERRIDE {
324 VLOG(1) << "GetProperties";
325 return properties_.get();
328 // BluetoothManagerClient override.
329 virtual void DefaultAdapter(const AdapterCallback& callback) OVERRIDE {
330 VLOG(1) << "DefaultAdapter.";
331 callback.Run(dbus::ObjectPath("/fake/hci0"), true);
334 // BluetoothManagerClient override.
335 virtual void FindAdapter(const std::string& address,
336 const AdapterCallback& callback) OVERRIDE {
337 VLOG(1) << "FindAdapter: " << address;
338 if (address == "hci0")
339 callback.Run(dbus::ObjectPath("/fake/hci0"), true);
340 else
341 callback.Run(dbus::ObjectPath(), false);
344 private:
345 void OnPropertyChanged(const std::string& property_name) {
346 FOR_EACH_OBSERVER(BluetoothManagerClient::Observer, observers_,
347 ManagerPropertyChanged(property_name));
350 // List of observers interested in event notifications from us.
351 ObserverList<Observer> observers_;
353 // Static properties we return.
354 scoped_ptr<Properties> properties_;
357 BluetoothManagerClient::BluetoothManagerClient() {
360 BluetoothManagerClient::~BluetoothManagerClient() {
363 BluetoothManagerClient* BluetoothManagerClient::Create(
364 DBusClientImplementationType type,
365 dbus::Bus* bus) {
366 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
367 return new BluetoothManagerClientImpl(bus);
368 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
369 return new BluetoothManagerClientStubImpl();
372 } // namespace chromeos