| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/bluez/bluetooth_adapter_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstdint> | 8 #include <cstdint> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 19 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
| 20 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 25 #include "device/bluetooth/bluetooth_common.h" | 26 #include "device/bluetooth/bluetooth_common.h" |
| 26 #include "device/bluetooth/bluetooth_device.h" | 27 #include "device/bluetooth/bluetooth_device.h" |
| 27 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 28 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 const std::string& identifier) const { | 503 const std::string& identifier) const { |
| 503 const auto& service = owned_gatt_services_.find(dbus::ObjectPath(identifier)); | 504 const auto& service = owned_gatt_services_.find(dbus::ObjectPath(identifier)); |
| 504 return service == owned_gatt_services_.end() ? nullptr | 505 return service == owned_gatt_services_.end() ? nullptr |
| 505 : service->second.get(); | 506 : service->second.get(); |
| 506 } | 507 } |
| 507 | 508 |
| 508 void BluetoothAdapterBlueZ::RemovePairingDelegateInternal( | 509 void BluetoothAdapterBlueZ::RemovePairingDelegateInternal( |
| 509 BluetoothDevice::PairingDelegate* pairing_delegate) { | 510 BluetoothDevice::PairingDelegate* pairing_delegate) { |
| 510 // Check if any device is using the pairing delegate. | 511 // Check if any device is using the pairing delegate. |
| 511 // If so, clear the pairing context which will make any responses no-ops. | 512 // If so, clear the pairing context which will make any responses no-ops. |
| 512 for (DevicesMap::const_iterator iter = devices_.begin(); | 513 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) { |
| 513 iter != devices_.end(); ++iter) { | |
| 514 BluetoothDeviceBlueZ* device_bluez = | 514 BluetoothDeviceBlueZ* device_bluez = |
| 515 static_cast<BluetoothDeviceBlueZ*>(iter->second); | 515 static_cast<BluetoothDeviceBlueZ*>(iter->second.get()); |
| 516 | 516 |
| 517 BluetoothPairingBlueZ* pairing = device_bluez->GetPairing(); | 517 BluetoothPairingBlueZ* pairing = device_bluez->GetPairing(); |
| 518 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) | 518 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) |
| 519 device_bluez->EndPairing(); | 519 device_bluez->EndPairing(); |
| 520 } | 520 } |
| 521 } | 521 } |
| 522 | 522 |
| 523 void BluetoothAdapterBlueZ::AdapterAdded(const dbus::ObjectPath& object_path) { | 523 void BluetoothAdapterBlueZ::AdapterAdded(const dbus::ObjectPath& object_path) { |
| 524 // Set the adapter to the newly added adapter only if no adapter is present. | 524 // Set the adapter to the newly added adapter only if no adapter is present. |
| 525 if (!IsPresent()) | 525 if (!IsPresent()) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 559 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
| 560 object_path); | 560 object_path); |
| 561 if (!properties || properties->adapter.value() != object_path_) | 561 if (!properties || properties->adapter.value() != object_path_) |
| 562 return; | 562 return; |
| 563 DCHECK(IsPresent()); | 563 DCHECK(IsPresent()); |
| 564 | 564 |
| 565 BluetoothDeviceBlueZ* device_bluez = new BluetoothDeviceBlueZ( | 565 BluetoothDeviceBlueZ* device_bluez = new BluetoothDeviceBlueZ( |
| 566 this, object_path, ui_task_runner_, socket_thread_); | 566 this, object_path, ui_task_runner_, socket_thread_); |
| 567 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); | 567 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); |
| 568 | 568 |
| 569 devices_.set(device_bluez->GetAddress(), | 569 devices_[device_bluez->GetAddress()] = base::WrapUnique(device_bluez); |
| 570 std::unique_ptr<BluetoothDevice>(device_bluez)); | |
| 571 | 570 |
| 572 for (auto& observer : observers_) | 571 for (auto& observer : observers_) |
| 573 observer.DeviceAdded(this, device_bluez); | 572 observer.DeviceAdded(this, device_bluez); |
| 574 } | 573 } |
| 575 | 574 |
| 576 void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) { | 575 void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) { |
| 577 for (DevicesMap::const_iterator iter = devices_.begin(); | 576 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) { |
| 578 iter != devices_.end(); ++iter) { | |
| 579 BluetoothDeviceBlueZ* device_bluez = | 577 BluetoothDeviceBlueZ* device_bluez = |
| 580 static_cast<BluetoothDeviceBlueZ*>(iter->second); | 578 static_cast<BluetoothDeviceBlueZ*>(iter->second.get()); |
| 581 if (device_bluez->object_path() == object_path) { | 579 if (device_bluez->object_path() == object_path) { |
| 582 std::unique_ptr<BluetoothDevice> scoped_device = | 580 std::unique_ptr<BluetoothDevice> scoped_device = std::move(iter->second); |
| 583 devices_.take_and_erase(iter->first); | 581 devices_.erase(iter); |
| 584 | 582 |
| 585 for (auto& observer : observers_) | 583 for (auto& observer : observers_) |
| 586 observer.DeviceRemoved(this, device_bluez); | 584 observer.DeviceRemoved(this, device_bluez); |
| 587 return; | 585 return; |
| 588 } | 586 } |
| 589 } | 587 } |
| 590 } | 588 } |
| 591 | 589 |
| 592 void BluetoothAdapterBlueZ::DevicePropertyChanged( | 590 void BluetoothAdapterBlueZ::DevicePropertyChanged( |
| 593 const dbus::ObjectPath& object_path, | 591 const dbus::ObjectPath& object_path, |
| 594 const std::string& property_name) { | 592 const std::string& property_name) { |
| 595 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path); | 593 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path); |
| 596 if (!device_bluez) | 594 if (!device_bluez) |
| 597 return; | 595 return; |
| 598 | 596 |
| 599 bluez::BluetoothDeviceClient::Properties* properties = | 597 bluez::BluetoothDeviceClient::Properties* properties = |
| 600 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 598 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
| 601 object_path); | 599 object_path); |
| 602 | 600 |
| 603 if (property_name == properties->address.name()) { | 601 if (property_name == properties->address.name()) { |
| 604 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); | 602 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) { |
| 605 ++iter) { | |
| 606 if (iter->second->GetAddress() == device_bluez->GetAddress()) { | 603 if (iter->second->GetAddress() == device_bluez->GetAddress()) { |
| 607 std::string old_address = iter->first; | 604 std::string old_address = iter->first; |
| 608 VLOG(1) << "Device changed address, old: " << old_address | 605 VLOG(1) << "Device changed address, old: " << old_address |
| 609 << " new: " << device_bluez->GetAddress(); | 606 << " new: " << device_bluez->GetAddress(); |
| 610 std::unique_ptr<BluetoothDevice> scoped_device = | 607 std::unique_ptr<BluetoothDevice> scoped_device = |
| 611 devices_.take_and_erase(iter); | 608 std::move(iter->second); |
| 612 ignore_result(scoped_device.release()); | 609 devices_.erase(iter); |
| 613 | 610 |
| 614 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); | 611 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); |
| 615 devices_.set(device_bluez->GetAddress(), | 612 devices_[device_bluez->GetAddress()] = std::move(scoped_device); |
| 616 std::unique_ptr<BluetoothDevice>(device_bluez)); | |
| 617 NotifyDeviceAddressChanged(device_bluez, old_address); | 613 NotifyDeviceAddressChanged(device_bluez, old_address); |
| 618 break; | 614 break; |
| 619 } | 615 } |
| 620 } | 616 } |
| 621 } | 617 } |
| 622 | 618 |
| 623 if (property_name == properties->service_data.name()) | 619 if (property_name == properties->service_data.name()) |
| 624 device_bluez->UpdateServiceData(); | 620 device_bluez->UpdateServiceData(); |
| 625 else if (property_name == properties->manufacturer_data.name()) | 621 else if (property_name == properties->manufacturer_data.name()) |
| 626 device_bluez->UpdateManufacturerData(); | 622 device_bluez->UpdateManufacturerData(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 // PlayStation joystick tries to reconnect after disconnection from USB. | 659 // PlayStation joystick tries to reconnect after disconnection from USB. |
| 664 // If it is still not trusted, set it, so it becomes available on the | 660 // If it is still not trusted, set it, so it becomes available on the |
| 665 // list of known devices. | 661 // list of known devices. |
| 666 if (properties->connected.value()) { | 662 if (properties->connected.value()) { |
| 667 if (device_bluez->IsTrustable() && !properties->trusted.value()) | 663 if (device_bluez->IsTrustable() && !properties->trusted.value()) |
| 668 device_bluez->SetTrusted(); | 664 device_bluez->SetTrusted(); |
| 669 } | 665 } |
| 670 | 666 |
| 671 int count = 0; | 667 int count = 0; |
| 672 | 668 |
| 673 for (DevicesMap::const_iterator iter = devices_.begin(); | 669 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) { |
| 674 iter != devices_.end(); ++iter) { | |
| 675 if (iter->second->IsPaired() && iter->second->IsConnected()) | 670 if (iter->second->IsPaired() && iter->second->IsConnected()) |
| 676 ++count; | 671 ++count; |
| 677 } | 672 } |
| 678 | 673 |
| 679 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); | 674 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); |
| 680 } | 675 } |
| 681 } | 676 } |
| 682 | 677 |
| 683 void BluetoothAdapterBlueZ::InputPropertyChanged( | 678 void BluetoothAdapterBlueZ::InputPropertyChanged( |
| 684 const dbus::ObjectPath& object_path, | 679 const dbus::ObjectPath& object_path, |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 object_path_, handle, callback, | 891 object_path_, handle, callback, |
| 897 base::Bind(&BluetoothAdapterBlueZ::ServiceRecordErrorConnector, | 892 base::Bind(&BluetoothAdapterBlueZ::ServiceRecordErrorConnector, |
| 898 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 893 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 899 } | 894 } |
| 900 | 895 |
| 901 BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath( | 896 BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath( |
| 902 const dbus::ObjectPath& object_path) { | 897 const dbus::ObjectPath& object_path) { |
| 903 if (!IsPresent()) | 898 if (!IsPresent()) |
| 904 return nullptr; | 899 return nullptr; |
| 905 | 900 |
| 906 for (DevicesMap::const_iterator iter = devices_.begin(); | 901 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) { |
| 907 iter != devices_.end(); ++iter) { | |
| 908 BluetoothDeviceBlueZ* device_bluez = | 902 BluetoothDeviceBlueZ* device_bluez = |
| 909 static_cast<BluetoothDeviceBlueZ*>(iter->second); | 903 static_cast<BluetoothDeviceBlueZ*>(iter->second.get()); |
| 910 if (device_bluez->object_path() == object_path) | 904 if (device_bluez->object_path() == object_path) |
| 911 return device_bluez; | 905 return device_bluez; |
| 912 } | 906 } |
| 913 | 907 |
| 914 return nullptr; | 908 return nullptr; |
| 915 } | 909 } |
| 916 | 910 |
| 917 BluetoothPairingBlueZ* BluetoothAdapterBlueZ::GetPairing( | 911 BluetoothPairingBlueZ* BluetoothAdapterBlueZ::GetPairing( |
| 918 const dbus::ObjectPath& object_path) { | 912 const dbus::ObjectPath& object_path) { |
| 919 DCHECK(IsPresent()); | 913 DCHECK(IsPresent()); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1034 DiscoveringChanged(false); | 1028 DiscoveringChanged(false); |
| 1035 | 1029 |
| 1036 // Move all elements of the original devices list to a new list here, | 1030 // Move all elements of the original devices list to a new list here, |
| 1037 // leaving the original list empty so that when we send DeviceRemoved(), | 1031 // leaving the original list empty so that when we send DeviceRemoved(), |
| 1038 // GetDevices() returns no devices. | 1032 // GetDevices() returns no devices. |
| 1039 DevicesMap devices_swapped; | 1033 DevicesMap devices_swapped; |
| 1040 devices_swapped.swap(devices_); | 1034 devices_swapped.swap(devices_); |
| 1041 | 1035 |
| 1042 for (auto& iter : devices_swapped) { | 1036 for (auto& iter : devices_swapped) { |
| 1043 for (auto& observer : observers_) | 1037 for (auto& observer : observers_) |
| 1044 observer.DeviceRemoved(this, iter.second); | 1038 observer.DeviceRemoved(this, iter.second.get()); |
| 1045 } | 1039 } |
| 1046 | 1040 |
| 1047 PresentChanged(false); | 1041 PresentChanged(false); |
| 1048 } | 1042 } |
| 1049 | 1043 |
| 1050 void BluetoothAdapterBlueZ::DiscoverableChanged(bool discoverable) { | 1044 void BluetoothAdapterBlueZ::DiscoverableChanged(bool discoverable) { |
| 1051 for (auto& observer : observers_) | 1045 for (auto& observer : observers_) |
| 1052 observer.AdapterDiscoverableChanged(this, discoverable); | 1046 observer.AdapterDiscoverableChanged(this, discoverable); |
| 1053 } | 1047 } |
| 1054 | 1048 |
| (...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1734 } else if (error_name == bluetooth_adapter::kErrorAlreadyExists) { | 1728 } else if (error_name == bluetooth_adapter::kErrorAlreadyExists) { |
| 1735 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_RECORD_ALREADY_EXISTS; | 1729 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_RECORD_ALREADY_EXISTS; |
| 1736 } else if (error_name == bluetooth_adapter::kErrorNotReady) { | 1730 } else if (error_name == bluetooth_adapter::kErrorNotReady) { |
| 1737 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY; | 1731 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY; |
| 1738 } | 1732 } |
| 1739 | 1733 |
| 1740 error_callback.Run(code); | 1734 error_callback.Run(code); |
| 1741 } | 1735 } |
| 1742 | 1736 |
| 1743 } // namespace bluez | 1737 } // namespace bluez |
| OLD | NEW |