Index: device/bluetooth/bluez/bluetooth_adapter_bluez.cc |
diff --git a/device/bluetooth/bluez/bluetooth_adapter_bluez.cc b/device/bluetooth/bluez/bluetooth_adapter_bluez.cc |
index e1ed672fff233aa616cd97d843953299ad373e1d..9c5639e1c965dbd68f639c2721cb7cfd7cca3ea5 100644 |
--- a/device/bluetooth/bluez/bluetooth_adapter_bluez.cc |
+++ b/device/bluetooth/bluez/bluetooth_adapter_bluez.cc |
@@ -509,10 +509,9 @@ void BluetoothAdapterBlueZ::RemovePairingDelegateInternal( |
BluetoothDevice::PairingDelegate* pairing_delegate) { |
// Check if any device is using the pairing delegate. |
// If so, clear the pairing context which will make any responses no-ops. |
- for (DevicesMap::const_iterator iter = devices_.begin(); |
- iter != devices_.end(); ++iter) { |
+ for (auto& item : devices_) { |
BluetoothDeviceBlueZ* device_bluez = |
- static_cast<BluetoothDeviceBlueZ*>(iter->second); |
+ static_cast<BluetoothDeviceBlueZ*>(item.second.get()); |
BluetoothPairingBlueZ* pairing = device_bluez->GetPairing(); |
if (pairing && pairing->GetPairingDelegate() == pairing_delegate) |
@@ -562,28 +561,32 @@ void BluetoothAdapterBlueZ::DeviceAdded(const dbus::ObjectPath& object_path) { |
return; |
DCHECK(IsPresent()); |
- BluetoothDeviceBlueZ* device_bluez = new BluetoothDeviceBlueZ( |
- this, object_path, ui_task_runner_, socket_thread_); |
+ std::unique_ptr<BluetoothDeviceBlueZ> device_bluez(new BluetoothDeviceBlueZ( |
+ this, object_path, ui_task_runner_, socket_thread_)); |
DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); |
- devices_.set(device_bluez->GetAddress(), |
- std::unique_ptr<BluetoothDevice>(device_bluez)); |
+ auto insertion = devices_.insert( |
+ std::make_pair(device_bluez->GetAddress(), std::move(device_bluez))); |
+ if (!insertion.second) { |
+ return; |
+ } |
for (auto& observer : observers_) |
- observer.DeviceAdded(this, device_bluez); |
+ observer.DeviceAdded(this, insertion.first->second.get()); |
} |
void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) { |
for (DevicesMap::const_iterator iter = devices_.begin(); |
iter != devices_.end(); ++iter) { |
BluetoothDeviceBlueZ* device_bluez = |
- static_cast<BluetoothDeviceBlueZ*>(iter->second); |
+ static_cast<BluetoothDeviceBlueZ*>(iter->second.get()); |
if (device_bluez->object_path() == object_path) { |
- std::unique_ptr<BluetoothDevice> scoped_device = |
- devices_.take_and_erase(iter->first); |
for (auto& observer : observers_) |
observer.DeviceRemoved(this, device_bluez); |
+ |
+ devices_.erase(iter); |
+ |
return; |
} |
} |
@@ -608,13 +611,18 @@ void BluetoothAdapterBlueZ::DevicePropertyChanged( |
VLOG(1) << "Device changed address, old: " << old_address |
<< " new: " << device_bluez->GetAddress(); |
std::unique_ptr<BluetoothDevice> scoped_device = |
- devices_.take_and_erase(iter); |
- ignore_result(scoped_device.release()); |
- |
- DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); |
- devices_.set(device_bluez->GetAddress(), |
- std::unique_ptr<BluetoothDevice>(device_bluez)); |
- NotifyDeviceAddressChanged(device_bluez, old_address); |
+ std::move(iter->second); |
+ devices_.erase(iter); |
+ |
+ DCHECK(devices_.find(scoped_device->GetAddress()) == devices_.end()); |
+ auto insertion = devices_.insert(std::make_pair( |
+ scoped_device->GetAddress(), std::move(scoped_device))); |
+ if (!insertion.second) { |
+ return; |
+ } |
+ BluetoothDeviceBlueZ* bluez = |
+ static_cast<BluetoothDeviceBlueZ*>(insertion.first->second.get()); |
+ NotifyDeviceAddressChanged(bluez, old_address); |
break; |
} |
} |
@@ -906,7 +914,7 @@ BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath( |
for (DevicesMap::const_iterator iter = devices_.begin(); |
iter != devices_.end(); ++iter) { |
BluetoothDeviceBlueZ* device_bluez = |
- static_cast<BluetoothDeviceBlueZ*>(iter->second); |
+ static_cast<BluetoothDeviceBlueZ*>(iter->second.get()); |
if (device_bluez->object_path() == object_path) |
return device_bluez; |
} |
@@ -1041,7 +1049,7 @@ void BluetoothAdapterBlueZ::RemoveAdapter() { |
for (auto& iter : devices_swapped) { |
for (auto& observer : observers_) |
- observer.DeviceRemoved(this, iter.second); |
+ observer.DeviceRemoved(this, iter.second.get()); |
} |
PresentChanged(false); |