Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: chrome/browser/chromeos/dbus/bluetooth_device_client.cc

Issue 9838085: Move files inside chrome/browser/chromeos/dbus to chromeos/dbus (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: _ Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/bluetooth_device_client.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/chromeos/chromeos_version.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h"
14 #include "chrome/browser/chromeos/dbus/bluetooth_property.h"
15 #include "dbus/bus.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
20
21 namespace chromeos {
22
23 BluetoothDeviceClient::Properties::Properties(dbus::ObjectProxy* object_proxy,
24 PropertyChangedCallback callback)
25 : BluetoothPropertySet(object_proxy,
26 bluetooth_device::kBluetoothDeviceInterface,
27 callback) {
28 RegisterProperty(bluetooth_device::kAddressProperty, &address);
29 RegisterProperty(bluetooth_device::kNameProperty, &name);
30 RegisterProperty(bluetooth_device::kVendorProperty, &vendor);
31 RegisterProperty(bluetooth_device::kProductProperty, &product);
32 RegisterProperty(bluetooth_device::kVersionProperty, &version);
33 RegisterProperty(bluetooth_device::kIconProperty, &icon);
34 RegisterProperty(bluetooth_device::kClassProperty, &bluetooth_class);
35 RegisterProperty(bluetooth_device::kUUIDsProperty, &uuids);
36 RegisterProperty(bluetooth_device::kServicesProperty, &services);
37 RegisterProperty(bluetooth_device::kPairedProperty, &paired);
38 RegisterProperty(bluetooth_device::kConnectedProperty, &connected);
39 RegisterProperty(bluetooth_device::kTrustedProperty, &trusted);
40 RegisterProperty(bluetooth_device::kBlockedProperty, &blocked);
41 RegisterProperty(bluetooth_device::kAliasProperty, &alias);
42 RegisterProperty(bluetooth_device::kNodesProperty, &nodes);
43 RegisterProperty(bluetooth_device::kAdapterProperty, &adapter);
44 RegisterProperty(bluetooth_device::kLegacyPairingProperty, &legacy_pairing);
45 }
46
47 BluetoothDeviceClient::Properties::~Properties() {
48 }
49
50
51 // The BluetoothDeviceClient implementation used in production.
52 class BluetoothDeviceClientImpl: public BluetoothDeviceClient,
53 private BluetoothAdapterClient::Observer {
54 public:
55 BluetoothDeviceClientImpl(dbus::Bus* bus,
56 BluetoothAdapterClient* adapter_client)
57 : weak_ptr_factory_(this),
58 bus_(bus) {
59 DVLOG(1) << "Creating BluetoothDeviceClientImpl";
60
61 DCHECK(adapter_client);
62 adapter_client->AddObserver(this);
63 }
64
65 virtual ~BluetoothDeviceClientImpl() {
66 // Clean up Properties structures
67 for (ObjectMap::iterator iter = object_map_.begin();
68 iter != object_map_.end(); ++iter) {
69 Object object = iter->second;
70 Properties* properties = object.second;
71 delete properties;
72 }
73 }
74
75 // BluetoothDeviceClient override.
76 virtual void AddObserver(BluetoothDeviceClient::Observer* observer)
77 OVERRIDE {
78 DCHECK(observer);
79 observers_.AddObserver(observer);
80 }
81
82 // BluetoothDeviceClient override.
83 virtual void RemoveObserver(BluetoothDeviceClient::Observer* observer)
84 OVERRIDE {
85 DCHECK(observer);
86 observers_.RemoveObserver(observer);
87 }
88
89 // BluetoothDeviceClient override.
90 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
91 OVERRIDE {
92 return GetObject(object_path).second;
93 }
94
95 // BluetoothDeviceClient override.
96 virtual void DiscoverServices(const dbus::ObjectPath& object_path,
97 const std::string& pattern,
98 const ServicesCallback& callback) OVERRIDE {
99 dbus::MethodCall method_call(
100 bluetooth_device::kBluetoothDeviceInterface,
101 bluetooth_device::kDiscoverServices);
102
103 dbus::MessageWriter writer(&method_call);
104 writer.AppendString(pattern);
105
106 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
107
108 object_proxy->CallMethod(
109 &method_call,
110 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
111 base::Bind(&BluetoothDeviceClientImpl::OnDiscoverServices,
112 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
113 }
114
115 // BluetoothDeviceClient override.
116 virtual void CancelDiscovery(const dbus::ObjectPath& object_path,
117 const DeviceCallback& callback) OVERRIDE {
118 dbus::MethodCall method_call(
119 bluetooth_device::kBluetoothDeviceInterface,
120 bluetooth_device::kCancelDiscovery);
121
122 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
123
124 object_proxy->CallMethod(
125 &method_call,
126 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
127 base::Bind(&BluetoothDeviceClientImpl::OnCancelDiscovery,
128 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
129 }
130
131 // BluetoothDeviceClient override.
132 virtual void Disconnect(const dbus::ObjectPath& object_path,
133 const DeviceCallback& callback) OVERRIDE {
134 dbus::MethodCall method_call(
135 bluetooth_device::kBluetoothDeviceInterface,
136 bluetooth_device::kDisconnect);
137
138 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
139
140 object_proxy->CallMethod(
141 &method_call,
142 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
143 base::Bind(&BluetoothDeviceClientImpl::OnDisconnect,
144 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
145 }
146
147 // BluetoothDeviceClient override.
148 virtual void CreateNode(const dbus::ObjectPath& object_path,
149 const std::string& uuid,
150 const NodeCallback& callback) OVERRIDE {
151 dbus::MethodCall method_call(
152 bluetooth_device::kBluetoothDeviceInterface,
153 bluetooth_device::kCreateNode);
154
155 dbus::MessageWriter writer(&method_call);
156 writer.AppendString(uuid);
157
158 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
159
160 object_proxy->CallMethod(
161 &method_call,
162 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
163 base::Bind(&BluetoothDeviceClientImpl::OnCreateNode,
164 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
165 }
166
167 // BluetoothDeviceClient override.
168 virtual void RemoveNode(const dbus::ObjectPath& object_path,
169 const dbus::ObjectPath& node_path,
170 const DeviceCallback& callback) OVERRIDE {
171 dbus::MethodCall method_call(
172 bluetooth_device::kBluetoothDeviceInterface,
173 bluetooth_device::kRemoveNode);
174
175 dbus::MessageWriter writer(&method_call);
176 writer.AppendObjectPath(node_path);
177
178 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
179
180 object_proxy->CallMethod(
181 &method_call,
182 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
183 base::Bind(&BluetoothDeviceClientImpl::OnRemoveNode,
184 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
185 }
186
187 private:
188 // We maintain a collection of dbus object proxies and properties structures
189 // for each device.
190 typedef std::pair<dbus::ObjectProxy*, Properties*> Object;
191 typedef std::map<const dbus::ObjectPath, Object> ObjectMap;
192 ObjectMap object_map_;
193
194 // BluetoothAdapterClient::Observer override.
195 virtual void DeviceCreated(const dbus::ObjectPath& adapter_path,
196 const dbus::ObjectPath& object_path) OVERRIDE {
197 }
198
199 // BluetoothAdapterClient::Observer override.
200 virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path,
201 const dbus::ObjectPath& object_path) OVERRIDE {
202 RemoveObject(object_path);
203 }
204
205 // Ensures that we have an object proxy and properties structure for
206 // a device with object path |object_path|, creating it if not and
207 // storing it in our |object_map_| map.
208 Object GetObject(const dbus::ObjectPath& object_path) {
209 ObjectMap::iterator iter = object_map_.find(object_path);
210 if (iter != object_map_.end())
211 return iter->second;
212
213 // Create the object proxy.
214 DCHECK(bus_);
215 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
216 bluetooth_device::kBluetoothDeviceServiceName, object_path);
217
218 object_proxy->ConnectToSignal(
219 bluetooth_device::kBluetoothDeviceInterface,
220 bluetooth_device::kDisconnectRequestedSignal,
221 base::Bind(&BluetoothDeviceClientImpl::DisconnectRequestedReceived,
222 weak_ptr_factory_.GetWeakPtr(), object_path),
223 base::Bind(&BluetoothDeviceClientImpl::DisconnectRequestedConnected,
224 weak_ptr_factory_.GetWeakPtr(), object_path));
225
226 object_proxy->ConnectToSignal(
227 bluetooth_device::kBluetoothDeviceInterface,
228 bluetooth_device::kNodeCreatedSignal,
229 base::Bind(&BluetoothDeviceClientImpl::NodeCreatedReceived,
230 weak_ptr_factory_.GetWeakPtr(), object_path),
231 base::Bind(&BluetoothDeviceClientImpl::NodeCreatedConnected,
232 weak_ptr_factory_.GetWeakPtr(), object_path));
233
234 object_proxy->ConnectToSignal(
235 bluetooth_device::kBluetoothDeviceInterface,
236 bluetooth_device::kNodeRemovedSignal,
237 base::Bind(&BluetoothDeviceClientImpl::NodeRemovedReceived,
238 weak_ptr_factory_.GetWeakPtr(), object_path),
239 base::Bind(&BluetoothDeviceClientImpl::NodeRemovedConnected,
240 weak_ptr_factory_.GetWeakPtr(), object_path));
241
242 // Create the properties structure.
243 Properties* properties = new Properties(
244 object_proxy,
245 base::Bind(&BluetoothDeviceClientImpl::OnPropertyChanged,
246 weak_ptr_factory_.GetWeakPtr(), object_path));
247
248 properties->ConnectSignals();
249 properties->GetAll();
250
251 Object object = std::make_pair(object_proxy, properties);
252 object_map_[object_path] = object;
253 return object;
254 }
255
256 // Removes the dbus object proxy and properties for the device with
257 // dbus object path |object_path| from our |object_map_| map.
258 void RemoveObject(const dbus::ObjectPath& object_path) {
259 ObjectMap::iterator iter = object_map_.find(object_path);
260 if (iter != object_map_.end()) {
261 // Clean up the Properties structure.
262 Object object = iter->second;
263 Properties* properties = object.second;
264 delete properties;
265
266 object_map_.erase(iter);
267 }
268 }
269
270 // Returns a pointer to the object proxy for |object_path|, creating
271 // it if necessary.
272 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
273 return GetObject(object_path).first;
274 }
275
276 // Called by BluetoothPropertySet when a property value is changed,
277 // either by result of a signal or response to a GetAll() or Get()
278 // call. Informs observers.
279 void OnPropertyChanged(const dbus::ObjectPath& object_path,
280 const std::string& property_name) {
281 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
282 DevicePropertyChanged(object_path, property_name));
283 }
284
285 // Called by dbus:: when a DisconnectRequested signal is received.
286 void DisconnectRequestedReceived(const dbus::ObjectPath& object_path,
287 dbus::Signal* signal) {
288 DCHECK(signal);
289
290 DVLOG(1) << object_path.value() << ": Disconnect requested.";
291 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
292 DisconnectRequested(object_path));
293 }
294
295 // Called by dbus:: when the DisconnectRequested signal is initially
296 // connected.
297 void DisconnectRequestedConnected(const dbus::ObjectPath& object_path,
298 const std::string& interface_name,
299 const std::string& signal_name,
300 bool success) {
301 LOG_IF(WARNING, !success) << object_path.value()
302 << ": Failed to connect to "
303 "DisconnectRequested signal.";
304 }
305
306 // Called by dbus:: when a NodeCreated signal is received.
307 void NodeCreatedReceived(const dbus::ObjectPath& object_path,
308 dbus::Signal* signal) {
309 DCHECK(signal);
310 dbus::MessageReader reader(signal);
311 dbus::ObjectPath node_path;
312 if (!reader.PopObjectPath(&node_path)) {
313 LOG(WARNING) << object_path.value()
314 << ": NodeCreated signal has incorrect parameters: "
315 << signal->ToString();
316 return;
317 }
318
319 DVLOG(1) << object_path.value() << ": Node created: "
320 << node_path.value();
321 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
322 NodeCreated(object_path, node_path));
323 }
324
325 // Called by dbus:: when the NodeCreated signal is initially connected.
326 void NodeCreatedConnected(const dbus::ObjectPath& object_path,
327 const std::string& interface_name,
328 const std::string& signal_name,
329 bool success) {
330 LOG_IF(WARNING, !success) << object_path.value()
331 << ": Failed to connect to NodeCreated signal.";
332 }
333
334 // Called by dbus:: when a NodeRemoved signal is received.
335 void NodeRemovedReceived(const dbus::ObjectPath& object_path,
336 dbus::Signal* signal) {
337 DCHECK(signal);
338 dbus::MessageReader reader(signal);
339 dbus::ObjectPath node_path;
340 if (!reader.PopObjectPath(&node_path)) {
341 LOG(WARNING) << object_path.value()
342 << ": NodeRemoved signal has incorrect parameters: "
343 << signal->ToString();
344 return;
345 }
346
347 DVLOG(1) << object_path.value() << ": Node removed: "
348 << node_path.value();
349 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
350 NodeRemoved(object_path, node_path));
351 }
352
353 // Called by dbus:: when the NodeRemoved signal is initially connected.
354 void NodeRemovedConnected(const dbus::ObjectPath& object_path,
355 const std::string& interface_name,
356 const std::string& signal_name,
357 bool success) {
358 LOG_IF(WARNING, !success) << object_path.value()
359 << ": Failed to connect to NodeRemoved signal.";
360 }
361
362 // Called when a response for DiscoverServices() is received.
363 void OnDiscoverServices(const dbus::ObjectPath& object_path,
364 const ServicesCallback& callback,
365 dbus::Response* response) {
366 // Parse response.
367 bool success = false;
368 ServiceMap services;
369 if (response != NULL) {
370 dbus::MessageReader reader(response);
371
372 dbus::MessageReader array_reader(NULL);
373 if (!reader.PopArray(&array_reader)) {
374 LOG(WARNING) << "DiscoverServices response has incorrect parameters: "
375 << response->ToString();
376 } else {
377 while (array_reader.HasMoreData()) {
378 dbus::MessageReader dict_entry_reader(NULL);
379 uint32 key = 0;
380 std::string value;
381 if (!array_reader.PopDictEntry(&dict_entry_reader)
382 || !dict_entry_reader.PopUint32(&key)
383 || !dict_entry_reader.PopString(&value)) {
384 LOG(WARNING) << "DiscoverServices response has "
385 "incorrect parameters: " << response->ToString();
386 } else {
387 services[key] = value;
388 }
389 }
390
391 success = true;
392 }
393 } else {
394 LOG(WARNING) << "Failed to discover services.";
395 }
396
397 // Notify client.
398 callback.Run(object_path, services, success);
399 }
400
401 // Called when a response for CancelDiscovery() is received.
402 void OnCancelDiscovery(const dbus::ObjectPath& object_path,
403 const DeviceCallback& callback,
404 dbus::Response* response) {
405 LOG_IF(WARNING, !response) << object_path.value()
406 << ": OnCancelDiscovery: failed.";
407 callback.Run(object_path, response);
408 }
409
410 // Called when a response for Disconnect() is received.
411 void OnDisconnect(const dbus::ObjectPath& object_path,
412 const DeviceCallback& callback,
413 dbus::Response* response) {
414 LOG_IF(WARNING, !response) << object_path.value()
415 << ": OnDisconnect: failed.";
416 callback.Run(object_path, response);
417 }
418
419 // Called when a response for CreateNode() is received.
420 void OnCreateNode(const dbus::ObjectPath& object_path,
421 const NodeCallback& callback,
422 dbus::Response* response) {
423 // Parse response.
424 bool success = false;
425 dbus::ObjectPath node_path;
426 if (response != NULL) {
427 dbus::MessageReader reader(response);
428 if (!reader.PopObjectPath(&node_path)) {
429 LOG(WARNING) << "CreateNode response has incorrect parameters: "
430 << response->ToString();
431 } else {
432 success = true;
433 }
434 } else {
435 LOG(WARNING) << "Failed to create node.";
436 }
437
438 // Notify client.
439 callback.Run(node_path, success);
440 }
441
442 // Called when a response for RemoveNode() is received.
443 void OnRemoveNode(const dbus::ObjectPath& object_path,
444 const DeviceCallback& callback,
445 dbus::Response* response) {
446 LOG_IF(WARNING, !response) << object_path.value()
447 << ": OnRemoveNode: failed.";
448 callback.Run(object_path, response);
449 }
450
451 // Weak pointer factory for generating 'this' pointers that might live longer
452 // than we do.
453 base::WeakPtrFactory<BluetoothDeviceClientImpl> weak_ptr_factory_;
454
455 dbus::Bus* bus_;
456
457 // List of observers interested in event notifications from us.
458 ObserverList<BluetoothDeviceClient::Observer> observers_;
459
460 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceClientImpl);
461 };
462
463 // The BluetoothDeviceClient implementation used on Linux desktop, which does
464 // nothing.
465 class BluetoothDeviceClientStubImpl : public BluetoothDeviceClient {
466 public:
467 // BluetoothDeviceClient override.
468 virtual void AddObserver(Observer* observer) OVERRIDE {
469 }
470
471 // BluetoothDeviceClient override.
472 virtual void RemoveObserver(Observer* observer) OVERRIDE {
473 }
474
475 // BluetoothDeviceClient override.
476 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
477 OVERRIDE {
478 VLOG(1) << "GetProperties: " << object_path.value();
479 return NULL;
480 }
481
482 // BluetoothDeviceClient override.
483 virtual void DiscoverServices(const dbus::ObjectPath& object_path,
484 const std::string& pattern,
485 const ServicesCallback& callback) OVERRIDE {
486 VLOG(1) << "DiscoverServices: " << object_path.value() << " " << pattern;
487
488 ServiceMap services;
489 callback.Run(object_path, services, false);
490 }
491
492 // BluetoothDeviceClient override.
493 virtual void CancelDiscovery(const dbus::ObjectPath& object_path,
494 const DeviceCallback& callback) OVERRIDE {
495 VLOG(1) << "CancelDiscovery: " << object_path.value();
496 callback.Run(object_path, false);
497 }
498
499 // BluetoothDeviceClient override.
500 virtual void Disconnect(const dbus::ObjectPath& object_path,
501 const DeviceCallback& callback) OVERRIDE {
502 VLOG(1) << "Disconnect: " << object_path.value();
503 callback.Run(object_path, false);
504 }
505
506 // BluetoothDeviceClient override.
507 virtual void CreateNode(const dbus::ObjectPath& object_path,
508 const std::string& uuid,
509 const NodeCallback& callback) OVERRIDE {
510 VLOG(1) << "CreateNode: " << object_path.value() << " " << uuid;
511 callback.Run(dbus::ObjectPath(), false);
512 }
513
514 // BluetoothDeviceClient override.
515 virtual void RemoveNode(const dbus::ObjectPath& object_path,
516 const dbus::ObjectPath& node_path,
517 const DeviceCallback& callback) OVERRIDE {
518 VLOG(1) << "RemoveNode: " << object_path.value()
519 << " " << node_path.value();
520 callback.Run(object_path, false);
521 }
522 };
523
524 BluetoothDeviceClient::BluetoothDeviceClient() {
525 }
526
527 BluetoothDeviceClient::~BluetoothDeviceClient() {
528 }
529
530 BluetoothDeviceClient* BluetoothDeviceClient::Create(
531 dbus::Bus* bus,
532 BluetoothAdapterClient* adapter_client) {
533 if (base::chromeos::IsRunningOnChromeOS()) {
534 return new BluetoothDeviceClientImpl(bus, adapter_client);
535 } else {
536 return new BluetoothDeviceClientStubImpl();
537 }
538 }
539
540 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698