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

Side by Side Diff: chrome/browser/chromeos/dbus/bluetooth_adapter_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_adapter_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_device_client.h"
14 #include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h"
15 #include "chrome/browser/chromeos/dbus/bluetooth_property.h"
16 #include "dbus/bus.h"
17 #include "dbus/message.h"
18 #include "dbus/object_path.h"
19 #include "dbus/object_proxy.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
21
22 namespace chromeos {
23
24 BluetoothAdapterClient::Properties::Properties(dbus::ObjectProxy* object_proxy,
25 PropertyChangedCallback callback)
26 : BluetoothPropertySet(object_proxy,
27 bluetooth_adapter::kBluetoothAdapterInterface,
28 callback) {
29 RegisterProperty(bluetooth_adapter::kAddressProperty, &address);
30 RegisterProperty(bluetooth_adapter::kNameProperty, &name);
31 RegisterProperty(bluetooth_adapter::kClassProperty, &bluetooth_class);
32 RegisterProperty(bluetooth_adapter::kPoweredProperty, &powered);
33 RegisterProperty(bluetooth_adapter::kDiscoverableProperty, &discoverable);
34 RegisterProperty(bluetooth_adapter::kPairableProperty, &pairable);
35 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty,
36 &pairable_timeout);
37 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty,
38 &discoverable_timeout);
39 RegisterProperty(bluetooth_adapter::kDiscoveringProperty, &discovering);
40 RegisterProperty(bluetooth_adapter::kDevicesProperty, &devices);
41 RegisterProperty(bluetooth_adapter::kUUIDsProperty, &uuids);
42 }
43
44 BluetoothAdapterClient::Properties::~Properties() {
45 }
46
47
48 // The BluetoothAdapterClient implementation used in production.
49 class BluetoothAdapterClientImpl: public BluetoothAdapterClient,
50 private BluetoothManagerClient::Observer {
51 public:
52 explicit BluetoothAdapterClientImpl(dbus::Bus* bus,
53 BluetoothManagerClient* manager_client)
54 : weak_ptr_factory_(this),
55 bus_(bus) {
56 DVLOG(1) << "Creating BluetoothAdapterClientImpl";
57
58 DCHECK(manager_client);
59 manager_client->AddObserver(this);
60 }
61
62 virtual ~BluetoothAdapterClientImpl() {
63 // Clean up Properties structures
64 for (ObjectMap::iterator iter = object_map_.begin();
65 iter != object_map_.end(); ++iter) {
66 Object object = iter->second;
67 Properties* properties = object.second;
68 delete properties;
69 }
70 }
71
72 // BluetoothAdapterClient override.
73 virtual void AddObserver(BluetoothAdapterClient::Observer* observer)
74 OVERRIDE {
75 DCHECK(observer);
76 observers_.AddObserver(observer);
77 }
78
79 // BluetoothAdapterClient override.
80 virtual void RemoveObserver(BluetoothAdapterClient::Observer* observer)
81 OVERRIDE {
82 DCHECK(observer);
83 observers_.RemoveObserver(observer);
84 }
85
86 // BluetoothAdapterClient override.
87 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
88 OVERRIDE {
89 return GetObject(object_path).second;
90 }
91
92 // BluetoothAdapterClient override.
93 virtual void RequestSession(const dbus::ObjectPath& object_path,
94 const AdapterCallback& callback) OVERRIDE {
95 dbus::MethodCall method_call(
96 bluetooth_adapter::kBluetoothAdapterInterface,
97 bluetooth_adapter::kRequestSession);
98
99 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
100
101 object_proxy->CallMethod(
102 &method_call,
103 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
104 base::Bind(&BluetoothAdapterClientImpl::OnRequestSession,
105 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
106 }
107
108 // BluetoothAdapterClient override.
109 virtual void ReleaseSession(const dbus::ObjectPath& object_path,
110 const AdapterCallback& callback) OVERRIDE {
111 dbus::MethodCall method_call(
112 bluetooth_adapter::kBluetoothAdapterInterface,
113 bluetooth_adapter::kReleaseSession);
114
115 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
116
117 object_proxy->CallMethod(
118 &method_call,
119 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
120 base::Bind(&BluetoothAdapterClientImpl::OnReleaseSession,
121 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
122 }
123
124 // BluetoothAdapterClient override.
125 virtual void StartDiscovery(const dbus::ObjectPath& object_path,
126 const AdapterCallback& callback) OVERRIDE {
127 dbus::MethodCall method_call(
128 bluetooth_adapter::kBluetoothAdapterInterface,
129 bluetooth_adapter::kStartDiscovery);
130
131 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
132
133 object_proxy->CallMethod(
134 &method_call,
135 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
136 base::Bind(&BluetoothAdapterClientImpl::OnStartDiscovery,
137 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
138 }
139
140 // BluetoothAdapterClient override.
141 virtual void StopDiscovery(const dbus::ObjectPath& object_path,
142 const AdapterCallback& callback) OVERRIDE {
143 dbus::MethodCall method_call(
144 bluetooth_adapter::kBluetoothAdapterInterface,
145 bluetooth_adapter::kStopDiscovery);
146
147 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
148
149 object_proxy->CallMethod(
150 &method_call,
151 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
152 base::Bind(&BluetoothAdapterClientImpl::OnStopDiscovery,
153 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
154 }
155
156 // BluetoothAdapterClient override.
157 virtual void FindDevice(const dbus::ObjectPath& object_path,
158 const std::string& address,
159 const DeviceCallback& callback) OVERRIDE {
160 dbus::MethodCall method_call(
161 bluetooth_adapter::kBluetoothAdapterInterface,
162 bluetooth_adapter::kFindDevice);
163
164 dbus::MessageWriter writer(&method_call);
165 writer.AppendString(address);
166
167 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
168
169 object_proxy->CallMethod(
170 &method_call,
171 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
172 base::Bind(&BluetoothAdapterClientImpl::OnFindDevice,
173 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
174 }
175
176 // BluetoothAdapterClient override.
177 virtual void CreateDevice(const dbus::ObjectPath& object_path,
178 const std::string& address,
179 const DeviceCallback& callback) OVERRIDE {
180 dbus::MethodCall method_call(
181 bluetooth_adapter::kBluetoothAdapterInterface,
182 bluetooth_adapter::kCreateDevice);
183
184 dbus::MessageWriter writer(&method_call);
185 writer.AppendString(address);
186
187 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
188
189 object_proxy->CallMethod(
190 &method_call,
191 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
192 base::Bind(&BluetoothAdapterClientImpl::OnCreateDevice,
193 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
194 }
195
196 // BluetoothAdapterClient override.
197 virtual void CreatePairedDevice(const dbus::ObjectPath& object_path,
198 const std::string& address,
199 const dbus::ObjectPath& agent_path,
200 const std::string& capability,
201 const DeviceCallback& callback) OVERRIDE {
202 dbus::MethodCall method_call(
203 bluetooth_adapter::kBluetoothAdapterInterface,
204 bluetooth_adapter::kCreatePairedDevice);
205
206 dbus::MessageWriter writer(&method_call);
207 writer.AppendString(address);
208 writer.AppendObjectPath(agent_path);
209 writer.AppendString(capability);
210
211 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
212
213 object_proxy->CallMethod(
214 &method_call,
215 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
216 base::Bind(&BluetoothAdapterClientImpl::OnCreatePairedDevice,
217 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
218 }
219
220 // BluetoothAdapterClient override.
221 virtual void CancelDeviceCreation(const dbus::ObjectPath& object_path,
222 const std::string& address,
223 const AdapterCallback& callback) OVERRIDE {
224 dbus::MethodCall method_call(
225 bluetooth_adapter::kBluetoothAdapterInterface,
226 bluetooth_adapter::kCancelDeviceCreation);
227
228 dbus::MessageWriter writer(&method_call);
229 writer.AppendString(address);
230
231 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
232
233 object_proxy->CallMethod(
234 &method_call,
235 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
236 base::Bind(&BluetoothAdapterClientImpl::OnCancelDeviceCreation,
237 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
238 }
239
240 // BluetoothAdapterClient override.
241 virtual void RemoveDevice(const dbus::ObjectPath& object_path,
242 const dbus::ObjectPath& device_path,
243 const AdapterCallback& callback) OVERRIDE {
244 dbus::MethodCall method_call(
245 bluetooth_adapter::kBluetoothAdapterInterface,
246 bluetooth_adapter::kRemoveDevice);
247
248 dbus::MessageWriter writer(&method_call);
249 writer.AppendObjectPath(device_path);
250
251 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
252
253 object_proxy->CallMethod(
254 &method_call,
255 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
256 base::Bind(&BluetoothAdapterClientImpl::OnRemoveDevice,
257 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
258 }
259
260 // BluetoothAdapterClient override.
261 virtual void RegisterAgent(const dbus::ObjectPath& object_path,
262 const dbus::ObjectPath& agent_path,
263 const std::string& capability,
264 const AdapterCallback& callback) OVERRIDE {
265 dbus::MethodCall method_call(
266 bluetooth_adapter::kBluetoothAdapterInterface,
267 bluetooth_adapter::kRegisterAgent);
268
269 dbus::MessageWriter writer(&method_call);
270 writer.AppendObjectPath(agent_path);
271 writer.AppendString(capability);
272
273 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
274
275 object_proxy->CallMethod(
276 &method_call,
277 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
278 base::Bind(&BluetoothAdapterClientImpl::OnRegisterAgent,
279 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
280 }
281
282 // BluetoothAdapterClient override.
283 virtual void UnregisterAgent(const dbus::ObjectPath& object_path,
284 const dbus::ObjectPath& agent_path,
285 const AdapterCallback& callback) OVERRIDE {
286 dbus::MethodCall method_call(
287 bluetooth_adapter::kBluetoothAdapterInterface,
288 bluetooth_adapter::kUnregisterAgent);
289
290 dbus::MessageWriter writer(&method_call);
291 writer.AppendObjectPath(agent_path);
292
293 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
294
295 object_proxy->CallMethod(
296 &method_call,
297 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
298 base::Bind(&BluetoothAdapterClientImpl::OnCreateDevice,
299 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
300 }
301
302 private:
303 // We maintain a collection of dbus object proxies and properties structures
304 // for each adapter.
305 typedef std::pair<dbus::ObjectProxy*, Properties*> Object;
306 typedef std::map<const dbus::ObjectPath, Object> ObjectMap;
307 ObjectMap object_map_;
308
309 // BluetoothManagerClient::Observer override.
310 virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE {
311 }
312
313 // BluetoothManagerClient::Observer override.
314 virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
315 RemoveObject(object_path);
316 }
317
318 // Ensures that we have an object proxy and properties structure for
319 // an adapter with object path |object_path|, creating it if not and
320 // storing in our |object_map_| map.
321 Object GetObject(const dbus::ObjectPath& object_path) {
322 ObjectMap::iterator iter = object_map_.find(object_path);
323 if (iter != object_map_.end())
324 return iter->second;
325
326 // Create the object proxy.
327 DCHECK(bus_);
328 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
329 bluetooth_adapter::kBluetoothAdapterServiceName, object_path);
330
331 object_proxy->ConnectToSignal(
332 bluetooth_adapter::kBluetoothAdapterInterface,
333 bluetooth_adapter::kDeviceCreatedSignal,
334 base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedReceived,
335 weak_ptr_factory_.GetWeakPtr(), object_path),
336 base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedConnected,
337 weak_ptr_factory_.GetWeakPtr(), object_path));
338
339 object_proxy->ConnectToSignal(
340 bluetooth_adapter::kBluetoothAdapterInterface,
341 bluetooth_adapter::kDeviceRemovedSignal,
342 base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedReceived,
343 weak_ptr_factory_.GetWeakPtr(), object_path),
344 base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedConnected,
345 weak_ptr_factory_.GetWeakPtr(), object_path));
346
347 object_proxy->ConnectToSignal(
348 bluetooth_adapter::kBluetoothAdapterInterface,
349 bluetooth_adapter::kDeviceFoundSignal,
350 base::Bind(&BluetoothAdapterClientImpl::DeviceFoundReceived,
351 weak_ptr_factory_.GetWeakPtr(), object_path),
352 base::Bind(&BluetoothAdapterClientImpl::DeviceFoundConnected,
353 weak_ptr_factory_.GetWeakPtr(), object_path));
354
355 object_proxy->ConnectToSignal(
356 bluetooth_adapter::kBluetoothAdapterInterface,
357 bluetooth_adapter::kDeviceDisappearedSignal,
358 base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedReceived,
359 weak_ptr_factory_.GetWeakPtr(), object_path),
360 base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedConnected,
361 weak_ptr_factory_.GetWeakPtr(), object_path));
362
363 // Create the properties structure.
364 Properties* properties = new Properties(
365 object_proxy,
366 base::Bind(&BluetoothAdapterClientImpl::OnPropertyChanged,
367 weak_ptr_factory_.GetWeakPtr(), object_path));
368
369 properties->ConnectSignals();
370 properties->GetAll();
371
372 Object object = std::make_pair(object_proxy, properties);
373 object_map_[object_path] = object;
374 return object;
375 }
376
377 // Removes the dbus object proxy and properties for the adapter with
378 // dbus object path |object_path| from our |object_map_| map.
379 void RemoveObject(const dbus::ObjectPath& object_path) {
380 ObjectMap::iterator iter = object_map_.find(object_path);
381 if (iter != object_map_.end()) {
382 // Clean up the Properties structure.
383 Object object = iter->second;
384 Properties* properties = object.second;
385 delete properties;
386
387 object_map_.erase(iter);
388 }
389 }
390
391 // Returns a pointer to the object proxy for |object_path|, creating
392 // it if necessary.
393 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
394 return GetObject(object_path).first;
395 }
396
397 // Called by BluetoothPropertySet when a property value is changed,
398 // either by result of a signal or response to a GetAll() or Get()
399 // call. Informs observers.
400 void OnPropertyChanged(const dbus::ObjectPath& object_path,
401 const std::string& property_name) {
402 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
403 AdapterPropertyChanged(object_path, property_name));
404 }
405
406 // Called by dbus:: when a DeviceCreated signal is received.
407 void DeviceCreatedReceived(const dbus::ObjectPath& object_path,
408 dbus::Signal* signal) {
409 DCHECK(signal);
410 dbus::MessageReader reader(signal);
411 dbus::ObjectPath device_path;
412 if (!reader.PopObjectPath(&device_path)) {
413 LOG(WARNING) << object_path.value()
414 << ": DeviceCreated signal has incorrect parameters: "
415 << signal->ToString();
416 return;
417 }
418
419 DVLOG(1) << object_path.value() << ": Device created: "
420 << device_path.value();
421 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
422 DeviceCreated(object_path, device_path));
423 }
424
425 // Called by dbus:: when the DeviceCreated signal is initially connected.
426 void DeviceCreatedConnected(const dbus::ObjectPath& object_path,
427 const std::string& interface_name,
428 const std::string& signal_name,
429 bool success) {
430 LOG_IF(WARNING, !success) << object_path.value()
431 << ": Failed to connect to DeviceCreated signal.";
432 }
433
434 // Called by dbus:: when a DeviceRemoved signal is received.
435 void DeviceRemovedReceived(const dbus::ObjectPath& object_path,
436 dbus::Signal* signal) {
437 DCHECK(signal);
438 dbus::MessageReader reader(signal);
439 dbus::ObjectPath device_path;
440 if (!reader.PopObjectPath(&device_path)) {
441 LOG(WARNING) << object_path.value()
442 << ": DeviceRemoved signal has incorrect parameters: "
443 << signal->ToString();
444 return;
445 }
446
447 DVLOG(1) << object_path.value() << ": Device removed: "
448 << device_path.value();
449 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
450 DeviceRemoved(object_path, device_path));
451 }
452
453 // Called by dbus:: when the DeviceRemoved signal is initially connected.
454 void DeviceRemovedConnected(const dbus::ObjectPath& object_path,
455 const std::string& interface_name,
456 const std::string& signal_name,
457 bool success) {
458 LOG_IF(WARNING, !success) << object_path.value()
459 << ": Failed to connect to DeviceRemoved signal.";
460 }
461
462 // Called by dbus:: when a DeviceFound signal is received.
463 void DeviceFoundReceived(const dbus::ObjectPath& object_path,
464 dbus::Signal* signal) {
465 DCHECK(signal);
466 dbus::MessageReader reader(signal);
467 std::string address;
468 if (!reader.PopString(&address)) {
469 LOG(WARNING) << object_path.value()
470 << ": DeviceFound signal has incorrect parameters: "
471 << signal->ToString();
472 return;
473 }
474
475 // Create device properties structure without an attached object_proxy
476 // and a NULL callback; value() functions will work on this, but not
477 // Get() or Set() calls.
478 BluetoothDeviceClient::Properties device_properties(
479 NULL, BluetoothDeviceClient::Properties::PropertyChangedCallback());
480 if (!device_properties.UpdatePropertiesFromReader(&reader)) {
481 LOG(WARNING) << object_path.value()
482 << ": DeviceFound signal has incorrect parameters: "
483 << signal->ToString();
484 return;
485 }
486
487 DVLOG(1) << object_path.value() << ": Device found: " << address;
488 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
489 DeviceFound(object_path, address, device_properties));
490 }
491
492 // Called by dbus:: when the DeviceFound signal is initially connected.
493 void DeviceFoundConnected(const dbus::ObjectPath& object_path,
494 const std::string& interface_name,
495 const std::string& signal_name,
496 bool success) {
497 LOG_IF(WARNING, !success) << object_path.value()
498 << ": Failed to connect to DeviceFound signal.";
499 }
500
501 // Called by dbus:: when a DeviceDisappeared signal is received.
502 void DeviceDisappearedReceived(const dbus::ObjectPath& object_path,
503 dbus::Signal* signal) {
504 DCHECK(signal);
505 dbus::MessageReader reader(signal);
506 std::string address;
507 if (!reader.PopString(&address)) {
508 LOG(WARNING) << object_path.value()
509 << ": DeviceDisappeared signal has incorrect parameters: "
510 << signal->ToString();
511 return;
512 }
513
514 DVLOG(1) << object_path.value() << ": Device disappeared: " << address;
515 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
516 DeviceDisappeared(object_path, address));
517 }
518
519 // Called by dbus:: when the DeviceDisappeared signal is initially connected.
520 void DeviceDisappearedConnected(const dbus::ObjectPath& object_path,
521 const std::string& interface_name,
522 const std::string& signal_name,
523 bool success) {
524 LOG_IF(WARNING, !success)
525 << object_path.value()
526 << ": Failed to connect to DeviceDisappeared signal.";
527 }
528
529 // Called when a response for RequestSession() is received.
530 void OnRequestSession(const dbus::ObjectPath& object_path,
531 const AdapterCallback& callback,
532 dbus::Response* response) {
533 LOG_IF(WARNING, !response) << object_path.value()
534 << ": OnRequestSession: failed.";
535 callback.Run(object_path, response);
536 }
537
538 // Called when a response for ReleaseSession() is received.
539 void OnReleaseSession(const dbus::ObjectPath& object_path,
540 const AdapterCallback& callback,
541 dbus::Response* response) {
542 LOG_IF(WARNING, !response) << object_path.value()
543 << ": OnReleaseSession: failed.";
544 callback.Run(object_path, response);
545 }
546
547 // Called when a response for StartDiscovery() is received.
548 void OnStartDiscovery(const dbus::ObjectPath& object_path,
549 const AdapterCallback& callback,
550 dbus::Response* response) {
551 LOG_IF(WARNING, !response) << object_path.value()
552 << ": OnStartDiscovery: failed.";
553 callback.Run(object_path, response);
554 }
555
556 // Called when a response for StopDiscovery() is received.
557 void OnStopDiscovery(const dbus::ObjectPath& object_path,
558 const AdapterCallback& callback,
559 dbus::Response* response) {
560 LOG_IF(WARNING, !response) << object_path.value()
561 << ": OnStopDiscovery: failed.";
562 callback.Run(object_path, response);
563 }
564
565 // Called when a response for FindDevice() is received.
566 void OnFindDevice(const dbus::ObjectPath& object_path,
567 const DeviceCallback& callback,
568 dbus::Response* response) {
569 // Parse response.
570 bool success = false;
571 dbus::ObjectPath device_path;
572 if (response != NULL) {
573 dbus::MessageReader reader(response);
574 if (!reader.PopObjectPath(&device_path)) {
575 LOG(WARNING) << "FindDevice response has incorrect parameters: "
576 << response->ToString();
577 } else {
578 success = true;
579 }
580 } else {
581 LOG(WARNING) << "Failed to find device.";
582 }
583
584 // Notify client.
585 callback.Run(device_path, success);
586 }
587
588 // Called when a response for CreateDevice() is received.
589 void OnCreateDevice(const dbus::ObjectPath& object_path,
590 const DeviceCallback& callback,
591 dbus::Response* response) {
592 // Parse response.
593 bool success = false;
594 dbus::ObjectPath device_path;
595 if (response != NULL) {
596 dbus::MessageReader reader(response);
597 if (!reader.PopObjectPath(&device_path)) {
598 LOG(WARNING) << "CreateDevice response has incorrect parameters: "
599 << response->ToString();
600 } else {
601 success = true;
602 }
603 } else {
604 LOG(WARNING) << "Failed to create device.";
605 }
606
607 // Notify client.
608 callback.Run(device_path, success);
609 }
610
611 // Called when a response for CreatePairedDevice() is received.
612 void OnCreatePairedDevice(const dbus::ObjectPath& object_path,
613 const DeviceCallback& callback,
614 dbus::Response* response) {
615 // Parse response.
616 bool success = false;
617 dbus::ObjectPath device_path;
618 if (response != NULL) {
619 dbus::MessageReader reader(response);
620 if (!reader.PopObjectPath(&device_path)) {
621 LOG(WARNING) << "CreatePairedDevice response has incorrect parameters: "
622 << response->ToString();
623 } else {
624 success = true;
625 }
626 } else {
627 LOG(WARNING) << "Failed to create paired device.";
628 }
629
630 // Notify client.
631 callback.Run(device_path, success);
632 }
633
634 // Called when a response for CancelDeviceCreation() is received.
635 void OnCancelDeviceCreation(const dbus::ObjectPath& object_path,
636 const AdapterCallback& callback,
637 dbus::Response* response) {
638 LOG_IF(WARNING, !response) << object_path.value()
639 << ": OnCancelDeviceCreation: failed.";
640 callback.Run(object_path, response);
641 }
642
643 // Called when a response for RemoveDevice() is received.
644 void OnRemoveDevice(const dbus::ObjectPath& object_path,
645 const AdapterCallback& callback,
646 dbus::Response* response) {
647 LOG_IF(WARNING, !response) << object_path.value()
648 << ": OnRemoveDevice: failed.";
649 callback.Run(object_path, response);
650 }
651
652 // Called when a response for RegisterAgent() is received.
653 void OnRegisterAgent(const dbus::ObjectPath& object_path,
654 const AdapterCallback& callback,
655 dbus::Response* response) {
656 LOG_IF(WARNING, !response) << object_path.value()
657 << ": OnRegisterAgent: failed.";
658 callback.Run(object_path, response);
659 }
660
661 // Called when a response for UnregisterAgent() is received.
662 void OnUnregisterAgent(const dbus::ObjectPath& object_path,
663 const AdapterCallback& callback,
664 dbus::Response* response) {
665 LOG_IF(WARNING, !response) << object_path.value()
666 << ": OnUnregisterAgent: failed.";
667 callback.Run(object_path, response);
668 }
669
670 // Weak pointer factory for generating 'this' pointers that might live longer
671 // than we do.
672 base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_;
673
674 dbus::Bus* bus_;
675
676 // List of observers interested in event notifications from us.
677 ObserverList<BluetoothAdapterClient::Observer> observers_;
678
679 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl);
680 };
681
682 // The BluetoothAdapterClient implementation used on Linux desktop, which does
683 // nothing.
684 class BluetoothAdapterClientStubImpl : public BluetoothAdapterClient {
685 public:
686 // BluetoothAdapterClient override.
687 virtual void AddObserver(Observer* observer) OVERRIDE {
688 }
689
690 // BluetoothAdapterClient override.
691 virtual void RemoveObserver(Observer* observer) OVERRIDE {
692 }
693
694 // BluetoothAdapterClient override.
695 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
696 OVERRIDE {
697 VLOG(1) << "GetProperties: " << object_path.value();
698 return NULL;
699 }
700
701 // BluetoothAdapterClient override.
702 virtual void RequestSession(const dbus::ObjectPath& object_path,
703 const AdapterCallback& callback) OVERRIDE {
704 VLOG(1) << "RequestSession: " << object_path.value();
705 callback.Run(object_path, false);
706 }
707
708 // BluetoothAdapterClient override.
709 virtual void ReleaseSession(const dbus::ObjectPath& object_path,
710 const AdapterCallback& callback) OVERRIDE {
711 VLOG(1) << "ReleaseSession: " << object_path.value();
712 callback.Run(object_path, false);
713 }
714
715 // BluetoothAdapterClient override.
716 virtual void StartDiscovery(const dbus::ObjectPath& object_path,
717 const AdapterCallback& callback) OVERRIDE {
718 VLOG(1) << "StartDiscovery: " << object_path.value();
719 callback.Run(object_path, false);
720 }
721
722 // BluetoothAdapterClient override.
723 virtual void StopDiscovery(const dbus::ObjectPath& object_path,
724 const AdapterCallback& callback) OVERRIDE {
725 VLOG(1) << "StopDiscovery: " << object_path.value();
726 callback.Run(object_path, false);
727 }
728
729 // BluetoothAdapterClient override.
730 virtual void FindDevice(const dbus::ObjectPath& object_path,
731 const std::string& address,
732 const DeviceCallback& callback) OVERRIDE {
733 VLOG(1) << "FindDevice: " << object_path.value() << " " << address;
734 callback.Run(dbus::ObjectPath(), false);
735 }
736
737 // BluetoothAdapterClient override.
738 virtual void CreateDevice(const dbus::ObjectPath& object_path,
739 const std::string& address,
740 const DeviceCallback& callback) OVERRIDE {
741 VLOG(1) << "CreateDevice: " << object_path.value() << " " << address;
742 callback.Run(dbus::ObjectPath(), false);
743 }
744
745 // BluetoothAdapterClient override.
746 virtual void CreatePairedDevice(const dbus::ObjectPath& object_path,
747 const std::string& address,
748 const dbus::ObjectPath& agent_path,
749 const std::string& capability,
750 const DeviceCallback& callback) OVERRIDE {
751 VLOG(1) << "CreatePairedDevice: " << object_path.value() << " " << address
752 << " " << agent_path.value() << " " << capability;
753 callback.Run(dbus::ObjectPath(), false);
754 }
755
756 // BluetoothAdapterClient override.
757 virtual void CancelDeviceCreation(const dbus::ObjectPath& object_path,
758 const std::string& address,
759 const AdapterCallback& callback) OVERRIDE {
760 VLOG(1) << "CancelDeviceCreation: " << object_path.value()
761 << " " << address;
762 callback.Run(object_path, false);
763 }
764
765 // BluetoothAdapterClient override.
766 virtual void RemoveDevice(const dbus::ObjectPath& object_path,
767 const dbus::ObjectPath& device_path,
768 const AdapterCallback& callback) OVERRIDE {
769 VLOG(1) << "RemoveDevice: " << object_path.value()
770 << " " << device_path.value();
771 callback.Run(object_path, false);
772 }
773
774 // BluetoothAdapterClient override.
775 virtual void RegisterAgent(const dbus::ObjectPath& object_path,
776 const dbus::ObjectPath& agent_path,
777 const std::string& capability,
778 const AdapterCallback& callback) OVERRIDE {
779 VLOG(1) << "RegisterAgent: " << object_path.value()
780 << " " << agent_path.value();
781 callback.Run(object_path, false);
782 }
783
784 // BluetoothAdapterClient override.
785 virtual void UnregisterAgent(const dbus::ObjectPath& object_path,
786 const dbus::ObjectPath& agent_path,
787 const AdapterCallback& callback) OVERRIDE {
788 VLOG(1) << "UnregisterAgent: " << object_path.value()
789 << " " << agent_path.value();
790 callback.Run(object_path, false);
791 }
792 };
793
794 BluetoothAdapterClient::BluetoothAdapterClient() {
795 }
796
797 BluetoothAdapterClient::~BluetoothAdapterClient() {
798 }
799
800 BluetoothAdapterClient* BluetoothAdapterClient::Create(
801 dbus::Bus* bus,
802 BluetoothManagerClient* manager_client) {
803 if (base::chromeos::IsRunningOnChromeOS()) {
804 return new BluetoothAdapterClientImpl(bus, manager_client);
805 } else {
806 return new BluetoothAdapterClientStubImpl();
807 }
808 }
809
810 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698