OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_device_chromeos.h" | 5 #include "device/bluetooth/bluetooth_device_chromeos.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... | |
39 using device::BluetoothSocket; | 39 using device::BluetoothSocket; |
40 | 40 |
41 namespace chromeos { | 41 namespace chromeos { |
42 | 42 |
43 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS( | 43 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS( |
44 BluetoothAdapterChromeOS* adapter) | 44 BluetoothAdapterChromeOS* adapter) |
45 : BluetoothDevice(), | 45 : BluetoothDevice(), |
46 adapter_(adapter), | 46 adapter_(adapter), |
47 pairing_delegate_(NULL), | 47 pairing_delegate_(NULL), |
48 connecting_applications_counter_(0), | 48 connecting_applications_counter_(0), |
49 service_records_loaded_(false), | |
49 weak_ptr_factory_(this) { | 50 weak_ptr_factory_(this) { |
50 } | 51 } |
51 | 52 |
52 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() { | 53 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() { |
53 } | 54 } |
54 | 55 |
55 bool BluetoothDeviceChromeOS::IsPaired() const { | 56 bool BluetoothDeviceChromeOS::IsPaired() const { |
56 return !object_path_.value().empty(); | 57 return !object_path_.value().empty(); |
57 } | 58 } |
58 | 59 |
59 const BluetoothDevice::ServiceList& | 60 const BluetoothDevice::ServiceList& |
60 BluetoothDeviceChromeOS::GetServices() const { | 61 BluetoothDeviceChromeOS::GetServices() const { |
61 return service_uuids_; | 62 return service_uuids_; |
62 } | 63 } |
63 | 64 |
64 void BluetoothDeviceChromeOS::GetServiceRecords( | 65 void BluetoothDeviceChromeOS::GetServiceRecords( |
65 const ServiceRecordsCallback& callback, | 66 const ServiceRecordsCallback& callback, |
66 const ErrorCallback& error_callback) { | 67 const ErrorCallback& error_callback) { |
68 if (service_records_loaded_) { | |
69 callback.Run(service_records_); | |
70 } else { | |
71 UpdateServiceRecords(callback, error_callback); | |
72 } | |
keybuk
2013/02/21 21:08:58
One of the reasons that GetServiceRecords() always
deymo
2013/02/22 23:09:09
Done.
| |
73 } | |
74 | |
75 void BluetoothDeviceChromeOS::UpdateServiceRecords( | |
76 const ServiceRecordsCallback& callback, | |
77 const ErrorCallback& error_callback) { | |
67 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 78 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
68 DiscoverServices( | 79 DiscoverServices( |
69 object_path_, | 80 object_path_, |
70 "", // empty pattern to browse all services | 81 "", // empty pattern to browse all services |
71 base::Bind(&BluetoothDeviceChromeOS::CollectServiceRecordsCallback, | 82 base::Bind(&BluetoothDeviceChromeOS::CollectServiceRecordsCallback, |
72 weak_ptr_factory_.GetWeakPtr(), | 83 weak_ptr_factory_.GetWeakPtr(), |
73 callback, | 84 callback, |
74 error_callback)); | 85 error_callback)); |
75 } | 86 } |
76 | 87 |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
364 // we can connect after rebooting. This information is part of the | 375 // we can connect after rebooting. This information is part of the |
365 // pairing information of the device, and is unique to the combination | 376 // pairing information of the device, and is unique to the combination |
366 // of our bluetooth address and the device's bluetooth address. A | 377 // of our bluetooth address and the device's bluetooth address. A |
367 // different host needs a new pairing, so it's not useful to sync. | 378 // different host needs a new pairing, so it's not useful to sync. |
368 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 379 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
369 GetProperties(object_path_)->trusted.Set( | 380 GetProperties(object_path_)->trusted.Set( |
370 true, | 381 true, |
371 base::Bind(&BluetoothDeviceChromeOS::OnSetTrusted, | 382 base::Bind(&BluetoothDeviceChromeOS::OnSetTrusted, |
372 weak_ptr_factory_.GetWeakPtr(), | 383 weak_ptr_factory_.GetWeakPtr(), |
373 callback, | 384 callback, |
374 base::Bind(error_callback, | 385 error_callback)); |
375 ERROR_UNKNOWN))); | |
376 // TODO(deymo): Replace ERROR_UNKNOWN with an appropriate new constant. | |
377 | 386 |
378 // Connect application-layer protocols. | 387 // Connect application-layer protocols. |
379 ConnectApplications(callback, error_callback); | 388 ConnectApplications(callback, error_callback); |
380 } | 389 } |
381 | 390 |
382 void BluetoothDeviceChromeOS::OnCreateDeviceError( | 391 void BluetoothDeviceChromeOS::OnCreateDeviceError( |
383 const ConnectErrorCallback& error_callback, | 392 const ConnectErrorCallback& error_callback, |
384 const std::string& error_name, | 393 const std::string& error_name, |
385 const std::string& error_message) { | 394 const std::string& error_message) { |
386 // The default |error_code| is an unknown error. | 395 // The default |error_code| is an unknown error. |
(...skipping 22 matching lines...) Expand all Loading... | |
409 const ServiceRecordsCallback& callback, | 418 const ServiceRecordsCallback& callback, |
410 const ErrorCallback& error_callback, | 419 const ErrorCallback& error_callback, |
411 const dbus::ObjectPath& device_path, | 420 const dbus::ObjectPath& device_path, |
412 const BluetoothDeviceClient::ServiceMap& service_map, | 421 const BluetoothDeviceClient::ServiceMap& service_map, |
413 bool success) { | 422 bool success) { |
414 if (!success) { | 423 if (!success) { |
415 error_callback.Run(); | 424 error_callback.Run(); |
416 return; | 425 return; |
417 } | 426 } |
418 | 427 |
419 ScopedVector<BluetoothServiceRecord> records; | 428 // Update the cache. |
429 service_records_loaded_ = true; | |
430 service_records_.clear(); | |
keybuk
2013/02/21 21:08:58
This gives a time period where another thread woul
deymo
2013/02/22 23:09:09
As discussed, this is ok.
keybuk
2013/02/22 23:50:03
after discussion, this seems okay - since all BDCO
| |
431 // TODO(deymo): Perhaps don't update the cache if the new SDP information is | |
432 // empty and we had something before. Some devices only answer this | |
433 // information while paired. This requires more investigation. | |
420 for (BluetoothDeviceClient::ServiceMap::const_iterator i = | 434 for (BluetoothDeviceClient::ServiceMap::const_iterator i = |
421 service_map.begin(); i != service_map.end(); ++i) { | 435 service_map.begin(); i != service_map.end(); ++i) { |
422 records.push_back( | 436 service_records_.push_back( |
423 new BluetoothServiceRecordChromeOS(address(), i->second)); | 437 new BluetoothServiceRecordChromeOS(address(), i->second)); |
424 } | 438 } |
425 callback.Run(records); | 439 callback.Run(service_records_); |
426 } | 440 } |
427 | 441 |
428 void BluetoothDeviceChromeOS::OnSetTrusted(const base::Closure& callback, | 442 void BluetoothDeviceChromeOS::OnSetTrusted( |
429 const ErrorCallback& error_callback, | 443 const base::Closure& callback, |
430 bool success) { | 444 const ConnectErrorCallback& error_callback, |
445 bool success) { | |
431 if (success) { | 446 if (success) { |
432 callback.Run(); | 447 UpdateServiceRecords( |
448 base::Bind(&BluetoothDeviceChromeOS::OnServicesUpdatedAfterPair, | |
449 weak_ptr_factory_.GetWeakPtr(), | |
450 callback), | |
451 base::Bind(error_callback, ERROR_SDP_FAILED)); | |
keybuk
2013/02/21 21:08:58
Could failure be handled better here? This adds an
deymo
2013/02/22 23:09:09
Not having the SDP information is not a "fatal" co
| |
433 } else { | 452 } else { |
434 LOG(WARNING) << "Failed to set device as trusted: " << address_; | 453 LOG(WARNING) << "Failed to set device as trusted: " << address_; |
435 error_callback.Run(); | 454 error_callback.Run(ERROR_UNKNOWN); |
455 // TODO(deymo): Replace ERROR_UNKNOWN with an appropriate new constant. | |
436 } | 456 } |
437 } | 457 } |
438 | 458 |
459 void BluetoothDeviceChromeOS::OnServicesUpdatedAfterPair( | |
460 const base::Closure& callback, | |
461 const ServiceRecordList& list) { | |
462 // Update the BluetoothDevice::connectable_ property. | |
keybuk
2013/02/21 21:08:58
nit: a comment explaining the boolean logic would
deymo
2013/02/22 23:09:09
We have a simplification of the device here. We ha
| |
463 bool hid_normally_connectable = true; | |
464 bool hid_reconnect_initiate = true; | |
465 for (ServiceRecordList::const_iterator it = list.begin(); | |
466 it != list.end(); ++it) { | |
467 hid_normally_connectable = | |
468 hid_normally_connectable && (*it)->hid_normally_connectable(); | |
469 hid_reconnect_initiate = | |
470 hid_reconnect_initiate && (*it)->hid_reconnect_initiate(); | |
471 } | |
472 connectable_ = hid_normally_connectable || !hid_reconnect_initiate; | |
473 | |
474 callback.Run(); | |
475 } | |
476 | |
439 void BluetoothDeviceChromeOS::ConnectApplications( | 477 void BluetoothDeviceChromeOS::ConnectApplications( |
440 const base::Closure& callback, | 478 const base::Closure& callback, |
441 const ConnectErrorCallback& error_callback) { | 479 const ConnectErrorCallback& error_callback) { |
442 // Introspect the device object to determine supported applications. | 480 // Introspect the device object to determine supported applications. |
443 DBusThreadManager::Get()->GetIntrospectableClient()-> | 481 DBusThreadManager::Get()->GetIntrospectableClient()-> |
444 Introspect(bluetooth_device::kBluetoothDeviceServiceName, | 482 Introspect(bluetooth_device::kBluetoothDeviceServiceName, |
445 object_path_, | 483 object_path_, |
446 base::Bind(&BluetoothDeviceChromeOS::OnIntrospect, | 484 base::Bind(&BluetoothDeviceChromeOS::OnIntrospect, |
447 weak_ptr_factory_.GetWeakPtr(), | 485 weak_ptr_factory_.GetWeakPtr(), |
448 callback, | 486 callback, |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
742 } | 780 } |
743 | 781 |
744 | 782 |
745 // static | 783 // static |
746 BluetoothDeviceChromeOS* BluetoothDeviceChromeOS::Create( | 784 BluetoothDeviceChromeOS* BluetoothDeviceChromeOS::Create( |
747 BluetoothAdapterChromeOS* adapter) { | 785 BluetoothAdapterChromeOS* adapter) { |
748 return new BluetoothDeviceChromeOS(adapter); | 786 return new BluetoothDeviceChromeOS(adapter); |
749 } | 787 } |
750 | 788 |
751 } // namespace chromeos | 789 } // namespace chromeos |
OLD | NEW |