Commit 15b1f530 authored by avi's avatar avi Committed by Commit bot

Remove base::ScopedPtrHashMap from device/.

BUG=579229

Review-Url: https://codereview.chromium.org/2606823002
Cr-Commit-Position: refs/heads/master@{#440902}
parent 58a87433
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/stl_util.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "device/bluetooth/bluetooth_common.h" #include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_device.h"
...@@ -110,10 +109,8 @@ BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() { ...@@ -110,10 +109,8 @@ BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() {
BluetoothAdapter::ConstDeviceList BluetoothAdapter::GetDevices() const { BluetoothAdapter::ConstDeviceList BluetoothAdapter::GetDevices() const {
ConstDeviceList devices; ConstDeviceList devices;
for (DevicesMap::const_iterator iter = devices_.begin(); for (const auto& device : devices_)
iter != devices_.end(); devices.push_back(device.second.get());
++iter)
devices.push_back(iter->second);
return devices; return devices;
} }
...@@ -130,9 +127,9 @@ const BluetoothDevice* BluetoothAdapter::GetDevice( ...@@ -130,9 +127,9 @@ const BluetoothDevice* BluetoothAdapter::GetDevice(
if (canonicalized_address.empty()) if (canonicalized_address.empty())
return nullptr; return nullptr;
DevicesMap::const_iterator iter = devices_.find(canonicalized_address); auto iter = devices_.find(canonicalized_address);
if (iter != devices_.end()) if (iter != devices_.end())
return iter->second; return iter->second.get();
return nullptr; return nullptr;
} }
...@@ -376,8 +373,8 @@ BluetoothAdapter::GetMergedDiscoveryFilterHelper( ...@@ -376,8 +373,8 @@ BluetoothAdapter::GetMergedDiscoveryFilterHelper(
} }
void BluetoothAdapter::RemoveTimedOutDevices() { void BluetoothAdapter::RemoveTimedOutDevices() {
for (DevicesMap::iterator it = devices_.begin(); it != devices_.end();) { for (auto it = devices_.begin(); it != devices_.end();) {
BluetoothDevice* device = it->second; BluetoothDevice* device = it->second.get();
if (device->IsPaired() || device->IsConnected() || if (device->IsPaired() || device->IsConnected() ||
device->IsGattConnected()) { device->IsGattConnected()) {
++it; ++it;
...@@ -398,10 +395,10 @@ void BluetoothAdapter::RemoveTimedOutDevices() { ...@@ -398,10 +395,10 @@ void BluetoothAdapter::RemoveTimedOutDevices() {
} }
VLOG(1) << "Removing device: " << device->GetAddress(); VLOG(1) << "Removing device: " << device->GetAddress();
DevicesMap::iterator next = it; auto next = it;
next++; next++;
std::unique_ptr<BluetoothDevice> removed_device = std::unique_ptr<BluetoothDevice> removed_device = std::move(it->second);
devices_.take_and_erase(it); devices_.erase(it);
it = next; it = next;
for (auto& observer : observers_) for (auto& observer : observers_)
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -501,12 +500,12 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter ...@@ -501,12 +500,12 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter
friend class BluetoothDiscoverySession; friend class BluetoothDiscoverySession;
friend class BluetoothTestBase; friend class BluetoothTestBase;
typedef base::ScopedPtrHashMap<std::string, std::unique_ptr<BluetoothDevice>> using DevicesMap =
DevicesMap; std::unordered_map<std::string, std::unique_ptr<BluetoothDevice>>;
typedef std::pair<BluetoothDevice::PairingDelegate*, PairingDelegatePriority> using PairingDelegatePair =
PairingDelegatePair; std::pair<BluetoothDevice::PairingDelegate*, PairingDelegatePriority>;
typedef base::Callback<void(UMABluetoothDiscoverySessionOutcome)> using DiscoverySessionErrorCallback =
DiscoverySessionErrorCallback; base::Callback<void(UMABluetoothDiscoverySessionOutcome)>;
BluetoothAdapter(); BluetoothAdapter();
virtual ~BluetoothAdapter(); virtual ~BluetoothAdapter();
......
...@@ -183,7 +183,7 @@ void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan( ...@@ -183,7 +183,7 @@ void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan(
const JavaParamRef<jobjectArray>& advertised_uuids, // Java Type: String[] const JavaParamRef<jobjectArray>& advertised_uuids, // Java Type: String[]
int32_t tx_power) { int32_t tx_power) {
std::string device_address = ConvertJavaStringToUTF8(env, address); std::string device_address = ConvertJavaStringToUTF8(env, address);
DevicesMap::const_iterator iter = devices_.find(device_address); auto iter = devices_.find(device_address);
bool is_new_device = false; bool is_new_device = false;
std::unique_ptr<BluetoothDeviceAndroid> device_android_owner; std::unique_ptr<BluetoothDeviceAndroid> device_android_owner;
...@@ -197,7 +197,7 @@ void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan( ...@@ -197,7 +197,7 @@ void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan(
device_android = device_android_owner.get(); device_android = device_android_owner.get();
} else { } else {
// Existing device. // Existing device.
device_android = static_cast<BluetoothDeviceAndroid*>(iter->second); device_android = static_cast<BluetoothDeviceAndroid*>(iter->second.get());
} }
DCHECK(device_android); DCHECK(device_android);
...@@ -219,7 +219,7 @@ void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan( ...@@ -219,7 +219,7 @@ void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan(
tx_power == INT32_MIN ? nullptr : &clamped_tx_power); tx_power == INT32_MIN ? nullptr : &clamped_tx_power);
if (is_new_device) { if (is_new_device) {
devices_.add(device_address, std::move(device_android_owner)); devices_[device_address] = std::move(device_android_owner);
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DeviceAdded(this, device_android); observer.DeviceAdded(this, device_android);
} else { } else {
......
...@@ -502,7 +502,7 @@ void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { ...@@ -502,7 +502,7 @@ void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) {
} }
device_classic = new BluetoothClassicDeviceMac(this, device); device_classic = new BluetoothClassicDeviceMac(this, device);
devices_.set(device_address, base::WrapUnique(device_classic)); devices_[device_address] = base::WrapUnique(device_classic);
VLOG(1) << "Adding new classic device: " << device_classic->GetAddress(); VLOG(1) << "Adding new classic device: " << device_classic->GetAddress();
for (auto& observer : observers_) for (auto& observer : observers_)
...@@ -566,7 +566,7 @@ void BluetoothAdapterMac::LowEnergyDeviceUpdated( ...@@ -566,7 +566,7 @@ void BluetoothAdapterMac::LowEnergyDeviceUpdated(
if (is_new_device) { if (is_new_device) {
std::string device_address = std::string device_address =
BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac)); devices_[device_address] = base::WrapUnique(device_mac);
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DeviceAdded(this, device_mac); observer.DeviceAdded(this, device_mac);
} else { } else {
...@@ -620,8 +620,7 @@ BluetoothAdapterMac::RetrieveGattConnectedDevicesWithService( ...@@ -620,8 +620,7 @@ BluetoothAdapterMac::RetrieveGattConnectedDevicesWithService(
device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral);
std::string device_address = std::string device_address =
BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
devices_.add(device_address, devices_[device_address] = base::WrapUnique(device_mac);
std::unique_ptr<BluetoothDevice>(device_mac));
for (auto& observer : observers_) { for (auto& observer : observers_) {
observer.DeviceAdded(this, device_mac); observer.DeviceAdded(this, device_mac);
} }
...@@ -694,11 +693,11 @@ BluetoothLowEnergyDeviceMac* ...@@ -694,11 +693,11 @@ BluetoothLowEnergyDeviceMac*
BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) {
std::string device_address = std::string device_address =
BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
DevicesMap::const_iterator iter = devices_.find(device_address); auto iter = devices_.find(device_address);
if (iter == devices_.end()) { if (iter == devices_.end()) {
return nil; return nil;
} }
return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second.get());
} }
bool BluetoothAdapterMac::DoesCollideWithKnownDevice( bool BluetoothAdapterMac::DoesCollideWithKnownDevice(
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -83,11 +84,6 @@ class BluetoothAdapterMacTest : public testing::Test { ...@@ -83,11 +84,6 @@ class BluetoothAdapterMacTest : public testing::Test {
return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
} }
void AddLowEnergyDevice(BluetoothLowEnergyDeviceMac* device) {
adapter_mac_->devices_.set(device->GetAddress(),
std::unique_ptr<BluetoothDevice>(device));
}
int NumDevices() { return adapter_mac_->devices_.size(); } int NumDevices() { return adapter_mac_->devices_.size(); }
bool DevicePresent(CBPeripheral* peripheral) { bool DevicePresent(CBPeripheral* peripheral) {
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/stl_util.h" #include "base/stl_util.h"
...@@ -234,58 +235,46 @@ void BluetoothAdapterWin::DevicesPolled( ...@@ -234,58 +235,46 @@ void BluetoothAdapterWin::DevicesPolled(
// new list with the list we know of (|devices_|) and raise corresponding // new list with the list we know of (|devices_|) and raise corresponding
// DeviceAdded, DeviceRemoved and DeviceChanged events. // DeviceAdded, DeviceRemoved and DeviceChanged events.
typedef std::set<std::string> DeviceAddressSet; using DeviceAddressSet = std::set<std::string>;
DeviceAddressSet known_devices; DeviceAddressSet known_devices;
for (DevicesMap::const_iterator iter = devices_.begin(); for (const auto& device : devices_)
iter != devices_.end(); known_devices.insert(device.first);
++iter) {
known_devices.insert((*iter).first);
}
DeviceAddressSet new_devices; DeviceAddressSet new_devices;
for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = for (auto iter = devices.begin(); iter != devices.end(); ++iter)
devices.begin();
iter != devices.end();
++iter) {
new_devices.insert((*iter)->address); new_devices.insert((*iter)->address);
}
// Process device removal first // Process device removal first.
DeviceAddressSet removed_devices = DeviceAddressSet removed_devices =
base::STLSetDifference<DeviceAddressSet>(known_devices, new_devices); base::STLSetDifference<DeviceAddressSet>(known_devices, new_devices);
for (DeviceAddressSet::const_iterator iter = removed_devices.begin(); for (const auto& device : removed_devices) {
iter != removed_devices.end(); auto it = devices_.find(device);
++iter) { std::unique_ptr<BluetoothDevice> device_win = std::move(it->second);
std::unique_ptr<BluetoothDevice> device_win = devices_.erase(it);
devices_.take_and_erase(*iter);
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DeviceRemoved(this, device_win.get()); observer.DeviceRemoved(this, device_win.get());
} }
// Process added and (maybe) changed devices in one pass // Process added and (maybe) changed devices in one pass.
DeviceAddressSet added_devices = DeviceAddressSet added_devices =
base::STLSetDifference<DeviceAddressSet>(new_devices, known_devices); base::STLSetDifference<DeviceAddressSet>(new_devices, known_devices);
DeviceAddressSet changed_devices = DeviceAddressSet changed_devices =
base::STLSetIntersection<DeviceAddressSet>(known_devices, new_devices); base::STLSetIntersection<DeviceAddressSet>(known_devices, new_devices);
for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = for (auto iter = devices.begin(); iter != devices.end(); ++iter) {
devices.begin();
iter != devices.end();
++iter) {
BluetoothTaskManagerWin::DeviceState* device_state = (*iter); BluetoothTaskManagerWin::DeviceState* device_state = (*iter);
if (added_devices.find(device_state->address) != added_devices.end()) { if (added_devices.find(device_state->address) != added_devices.end()) {
BluetoothDeviceWin* device_win = BluetoothDeviceWin* device_win =
new BluetoothDeviceWin(this, *device_state, ui_task_runner_, new BluetoothDeviceWin(this, *device_state, ui_task_runner_,
socket_thread_, NULL, net::NetLogSource()); socket_thread_, NULL, net::NetLogSource());
devices_.set(device_state->address, devices_[device_state->address] = base::WrapUnique(device_win);
std::unique_ptr<BluetoothDevice>(device_win));
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DeviceAdded(this, device_win); observer.DeviceAdded(this, device_win);
} else if (changed_devices.find(device_state->address) != } else if (changed_devices.find(device_state->address) !=
changed_devices.end()) { changed_devices.end()) {
DevicesMap::const_iterator iter = devices_.find(device_state->address); auto iter = devices_.find(device_state->address);
DCHECK(iter != devices_.end()); DCHECK(iter != devices_.end());
BluetoothDeviceWin* device_win = BluetoothDeviceWin* device_win =
static_cast<BluetoothDeviceWin*>(iter->second); static_cast<BluetoothDeviceWin*>(iter->second.get());
if (!device_win->IsEqual(*device_state)) { if (!device_win->IsEqual(*device_state)) {
device_win->Update(*device_state); device_win->Update(*device_state);
for (auto& observer : observers_) for (auto& observer : observers_)
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include <string> #include <string>
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -23,9 +22,14 @@ ...@@ -23,9 +22,14 @@
namespace device { namespace device {
BluetoothDevice::DeviceUUIDs::DeviceUUIDs() {} BluetoothDevice::DeviceUUIDs::DeviceUUIDs() = default;
BluetoothDevice::DeviceUUIDs::~DeviceUUIDs() {} BluetoothDevice::DeviceUUIDs::~DeviceUUIDs() = default;
BluetoothDevice::DeviceUUIDs::DeviceUUIDs(const DeviceUUIDs& other) = default;
BluetoothDevice::DeviceUUIDs& BluetoothDevice::DeviceUUIDs::operator=(
const DeviceUUIDs& other) = default;
void BluetoothDevice::DeviceUUIDs::ReplaceAdvertisedUUIDs( void BluetoothDevice::DeviceUUIDs::ReplaceAdvertisedUUIDs(
UUIDList new_advertised_uuids) { UUIDList new_advertised_uuids) {
...@@ -44,9 +48,8 @@ void BluetoothDevice::DeviceUUIDs::ClearAdvertisedUUIDs() { ...@@ -44,9 +48,8 @@ void BluetoothDevice::DeviceUUIDs::ClearAdvertisedUUIDs() {
void BluetoothDevice::DeviceUUIDs::ReplaceServiceUUIDs( void BluetoothDevice::DeviceUUIDs::ReplaceServiceUUIDs(
const GattServiceMap& gatt_services) { const GattServiceMap& gatt_services) {
service_uuids_.clear(); service_uuids_.clear();
for (const auto& gatt_service_pair : gatt_services) { for (const auto& gatt_service_pair : gatt_services)
service_uuids_.insert(gatt_service_pair.second->GetUUID()); service_uuids_.insert(gatt_service_pair.second->GetUUID());
}
UpdateDeviceUUIDs(); UpdateDeviceUUIDs();
} }
...@@ -371,13 +374,16 @@ std::vector<BluetoothRemoteGattService*> BluetoothDevice::GetGattServices() ...@@ -371,13 +374,16 @@ std::vector<BluetoothRemoteGattService*> BluetoothDevice::GetGattServices()
const { const {
std::vector<BluetoothRemoteGattService*> services; std::vector<BluetoothRemoteGattService*> services;
for (const auto& iter : gatt_services_) for (const auto& iter : gatt_services_)
services.push_back(iter.second); services.push_back(iter.second.get());
return services; return services;
} }
BluetoothRemoteGattService* BluetoothDevice::GetGattService( BluetoothRemoteGattService* BluetoothDevice::GetGattService(
const std::string& identifier) const { const std::string& identifier) const {
return gatt_services_.get(identifier); auto it = gatt_services_.find(identifier);
if (it == gatt_services_.end())
return nullptr;
return it->second.get();
} }
// static // static
......
...@@ -16,21 +16,21 @@ ...@@ -16,21 +16,21 @@
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "device/bluetooth/bluetooth_common.h" #include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_export.h" #include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h"
#include "device/bluetooth/bluetooth_uuid.h" #include "device/bluetooth/bluetooth_uuid.h"
namespace device { namespace device {
class BluetoothAdapter; class BluetoothAdapter;
class BluetoothGattConnection; class BluetoothGattConnection;
class BluetoothRemoteGattService;
class BluetoothSocket; class BluetoothSocket;
class BluetoothUUID; class BluetoothUUID;
...@@ -106,8 +106,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice { ...@@ -106,8 +106,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
// Mapping from the platform-specific GATT service identifiers to // Mapping from the platform-specific GATT service identifiers to
// BluetoothRemoteGattService objects. // BluetoothRemoteGattService objects.
typedef base::ScopedPtrHashMap<std::string, typedef std::unordered_map<std::string,
std::unique_ptr<BluetoothRemoteGattService>> std::unique_ptr<BluetoothRemoteGattService>>
GattServiceMap; GattServiceMap;
// Interface for negotiating pairing of bluetooth devices. // Interface for negotiating pairing of bluetooth devices.
...@@ -579,6 +579,9 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice { ...@@ -579,6 +579,9 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
DeviceUUIDs(); DeviceUUIDs();
~DeviceUUIDs(); ~DeviceUUIDs();
DeviceUUIDs(const DeviceUUIDs& other);
DeviceUUIDs& operator=(const DeviceUUIDs& other);
// Advertised Service UUIDs functions // Advertised Service UUIDs functions
void ReplaceAdvertisedUUIDs(UUIDList new_advertised_uuids); void ReplaceAdvertisedUUIDs(UUIDList new_advertised_uuids);
...@@ -601,7 +604,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice { ...@@ -601,7 +604,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
BluetoothDevice::UUIDSet device_uuids_; BluetoothDevice::UUIDSet device_uuids_;
}; };
BluetoothDevice(BluetoothAdapter* adapter); explicit BluetoothDevice(BluetoothAdapter* adapter);
// Implements platform specific operations to initiate a GATT connection. // Implements platform specific operations to initiate a GATT connection.
// Subclasses must also call DidConnectGatt, DidFailToConnectGatt, or // Subclasses must also call DidConnectGatt, DidFailToConnectGatt, or
...@@ -673,6 +676,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice { ...@@ -673,6 +676,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
// Returns a localized string containing the device's bluetooth address and // Returns a localized string containing the device's bluetooth address and
// a device type for display when |name_| is empty. // a device type for display when |name_| is empty.
base::string16 GetAddressWithLocalizedDeviceTypeName() const; base::string16 GetAddressWithLocalizedDeviceTypeName() const;
DISALLOW_COPY_AND_ASSIGN(BluetoothDevice);
}; };
} // namespace device } // namespace device
......
...@@ -246,17 +246,17 @@ void BluetoothDeviceAndroid::CreateGattRemoteService( ...@@ -246,17 +246,17 @@ void BluetoothDeviceAndroid::CreateGattRemoteService(
std::string instance_id_string = std::string instance_id_string =
base::android::ConvertJavaStringToUTF8(env, instance_id); base::android::ConvertJavaStringToUTF8(env, instance_id);
if (gatt_services_.contains(instance_id_string)) if (gatt_services_.find(instance_id_string) != gatt_services_.end())
return; return;
BluetoothDevice::GattServiceMap::iterator service_iterator = std::unique_ptr<BluetoothRemoteGattServiceAndroid> service =
gatt_services_.set( BluetoothRemoteGattServiceAndroid::Create(GetAndroidAdapter(), this,
instance_id_string, bluetooth_gatt_service_wrapper,
BluetoothRemoteGattServiceAndroid::Create( instance_id_string, j_device_);
GetAndroidAdapter(), this, bluetooth_gatt_service_wrapper, BluetoothRemoteGattServiceAndroid* service_ptr = service.get();
instance_id_string, j_device_)); gatt_services_[instance_id_string] = std::move(service);
adapter_->NotifyGattServiceAdded(service_iterator->second); adapter_->NotifyGattServiceAdded(service_ptr);
} }
BluetoothDeviceAndroid::BluetoothDeviceAndroid(BluetoothAdapterAndroid* adapter) BluetoothDeviceAndroid::BluetoothDeviceAndroid(BluetoothAdapterAndroid* adapter)
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_vector.h" #include "base/memory/scoped_vector.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
...@@ -51,7 +51,9 @@ BluetoothDeviceWin::~BluetoothDeviceWin() { ...@@ -51,7 +51,9 @@ BluetoothDeviceWin::~BluetoothDeviceWin() {
service_keys.push_back(gatt_service.first); service_keys.push_back(gatt_service.first);
} }
for (const auto& key : service_keys) { for (const auto& key : service_keys) {
gatt_services_.take_and_erase(key); std::unique_ptr<BluetoothRemoteGattService> service =
std::move(gatt_services_[key]);
gatt_services_.erase(key);
} }
} }
...@@ -213,9 +215,8 @@ void BluetoothDeviceWin::CreateGattConnection( ...@@ -213,9 +215,8 @@ void BluetoothDeviceWin::CreateGattConnection(
const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord( const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord(
const device::BluetoothUUID& uuid) const { const device::BluetoothUUID& uuid) const {
for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); for (auto iter = service_record_list_.begin();
iter != service_record_list_.end(); iter != service_record_list_.end(); ++iter) {
++iter) {
if ((*iter)->uuid() == uuid) if ((*iter)->uuid() == uuid)
return *iter; return *iter;
} }
...@@ -233,21 +234,17 @@ bool BluetoothDeviceWin::IsEqual( ...@@ -233,21 +234,17 @@ bool BluetoothDeviceWin::IsEqual(
} }
// Checks service collection // Checks service collection
typedef base::ScopedPtrHashMap<std::string,
std::unique_ptr<BluetoothServiceRecordWin>>
ServiceRecordMap;
UUIDSet new_services; UUIDSet new_services;
ServiceRecordMap new_service_records; std::unordered_map<std::string, std::unique_ptr<BluetoothServiceRecordWin>>
for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator new_service_records;
iter = device_state.service_record_states.begin(); for (auto iter = device_state.service_record_states.begin();
iter != device_state.service_record_states.end(); ++iter) { iter != device_state.service_record_states.end(); ++iter) {
BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin( std::unique_ptr<BluetoothServiceRecordWin> service_record =
address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); base::MakeUnique<BluetoothServiceRecordWin>(
address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid);
new_services.insert(service_record->uuid()); new_services.insert(service_record->uuid());
new_service_records.set( new_service_records[service_record->uuid().canonical_value()] =
service_record->uuid().canonical_value(), std::move(service_record);
std::unique_ptr<BluetoothServiceRecordWin>(service_record));
} }
// Check that no new services have been added or removed. // Check that no new services have been added or removed.
...@@ -255,11 +252,11 @@ bool BluetoothDeviceWin::IsEqual( ...@@ -255,11 +252,11 @@ bool BluetoothDeviceWin::IsEqual(
return false; return false;
} }
for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); for (auto iter = service_record_list_.begin();
iter != service_record_list_.end(); ++iter) { iter != service_record_list_.end(); ++iter) {
BluetoothServiceRecordWin* service_record = (*iter); BluetoothServiceRecordWin* service_record = (*iter);
BluetoothServiceRecordWin* new_service_record = BluetoothServiceRecordWin* new_service_record =
new_service_records.get((*iter)->uuid().canonical_value()); new_service_records[(*iter)->uuid().canonical_value()].get();
if (!service_record->IsEqual(*new_service_record)) if (!service_record->IsEqual(*new_service_record))
return false; return false;
} }
...@@ -300,8 +297,7 @@ void BluetoothDeviceWin::UpdateServices( ...@@ -300,8 +297,7 @@ void BluetoothDeviceWin::UpdateServices(
uuids_.clear(); uuids_.clear();
service_record_list_.clear(); service_record_list_.clear();
for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator for (auto iter = device_state.service_record_states.begin();
iter = device_state.service_record_states.begin();
iter != device_state.service_record_states.end(); ++iter) { iter != device_state.service_record_states.end(); ++iter) {
BluetoothServiceRecordWin* service_record = BluetoothServiceRecordWin* service_record =
new BluetoothServiceRecordWin(device_state.address, (*iter)->name, new BluetoothServiceRecordWin(device_state.address, (*iter)->name,
...@@ -316,12 +312,11 @@ void BluetoothDeviceWin::UpdateServices( ...@@ -316,12 +312,11 @@ void BluetoothDeviceWin::UpdateServices(
bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid,
uint16_t attribute_handle) { uint16_t attribute_handle) {
GattServiceMap::iterator it = gatt_services_.begin(); for (const auto& gatt_service : gatt_services_) {
for (; it != gatt_services_.end(); it++) {
uint16_t it_att_handle = uint16_t it_att_handle =
static_cast<BluetoothRemoteGattServiceWin*>(it->second) static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get())
->GetAttributeHandle(); ->GetAttributeHandle();
BluetoothUUID it_uuid = it->second->GetUUID(); BluetoothUUID it_uuid = gatt_service.second->GetUUID();
if (attribute_handle == it_att_handle && uuid == it_uuid) { if (attribute_handle == it_att_handle && uuid == it_uuid) {
return true; return true;
} }
...@@ -337,8 +332,7 @@ bool BluetoothDeviceWin::DoesGattServiceExist( ...@@ -337,8 +332,7 @@ bool BluetoothDeviceWin::DoesGattServiceExist(
static_cast<BluetoothRemoteGattServiceWin*>(service) static_cast<BluetoothRemoteGattServiceWin*>(service)
->GetAttributeHandle(); ->GetAttributeHandle();
BluetoothUUID uuid = service->GetUUID(); BluetoothUUID uuid = service->GetUUID();
ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator it = auto it = service_state.begin();
service_state.begin();
for (; it != service_state.end(); ++it) { for (; it != service_state.end(); ++it) {
if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid) if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid)
return true; return true;
...@@ -353,16 +347,18 @@ void BluetoothDeviceWin::UpdateGattServices( ...@@ -353,16 +347,18 @@ void BluetoothDeviceWin::UpdateGattServices(
{ {
std::vector<std::string> to_be_removed_services; std::vector<std::string> to_be_removed_services;
for (const auto& gatt_service : gatt_services_) { for (const auto& gatt_service : gatt_services_) {
if (!DoesGattServiceExist(service_state, gatt_service.second)) { if (!DoesGattServiceExist(service_state, gatt_service.second.get())) {
to_be_removed_services.push_back(gatt_service.first); to_be_removed_services.push_back(gatt_service.first);
} }
} }
for (const auto& service : to_be_removed_services) { for (const auto& service : to_be_removed_services) {
gatt_services_.take_and_erase(service); std::unique_ptr<BluetoothRemoteGattService> service_ptr =
std::move(gatt_services_[service]);
gatt_services_.erase(service);
} }
// Update previously discovered services. // Update previously discovered services.
for (auto gatt_service : gatt_services_) { for (const auto& gatt_service : gatt_services_) {
static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second) static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get())
->Update(); ->Update();
} }
} }
...@@ -372,17 +368,14 @@ void BluetoothDeviceWin::UpdateGattServices( ...@@ -372,17 +368,14 @@ void BluetoothDeviceWin::UpdateGattServices(
return; return;
// Add new services. // Add new services.
for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator for (auto it = service_state.begin(); it != service_state.end(); ++it) {
it = service_state.begin();
it != service_state.end(); ++it) {
if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) { if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) {
BluetoothRemoteGattServiceWin* primary_service = BluetoothRemoteGattServiceWin* primary_service =
new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid,
(*it)->attribute_handle, true, (*it)->attribute_handle, true,
nullptr, ui_task_runner_); nullptr, ui_task_runner_);
gatt_services_.add( gatt_services_[primary_service->GetIdentifier()] =
primary_service->GetIdentifier(), base::WrapUnique(primary_service);
std::unique_ptr<BluetoothRemoteGattService>(primary_service));
adapter_->NotifyGattServiceAdded(primary_service); adapter_->NotifyGattServiceAdded(primary_service);
} }
} }
......
...@@ -206,15 +206,14 @@ void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { ...@@ -206,15 +206,14 @@ void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) {
if (!gatt_service) { if (!gatt_service) {
gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service,
true /* is_primary */); true /* is_primary */);
auto result_iter = gatt_services_.add(gatt_service->GetIdentifier(), auto result_iter = gatt_services_.insert(std::make_pair(
base::WrapUnique(gatt_service)); gatt_service->GetIdentifier(), base::WrapUnique(gatt_service)));
DCHECK(result_iter.second); DCHECK(result_iter.second);
adapter_->NotifyGattServiceAdded(gatt_service); adapter_->NotifyGattServiceAdded(gatt_service);
} }
} }
for (GattServiceMap::const_iterator it = gatt_services_.begin(); for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) {
it != gatt_services_.end(); ++it) { device::BluetoothRemoteGattService* gatt_service = it->second.get();
device::BluetoothRemoteGattService* gatt_service = it->second;
device::BluetoothRemoteGattServiceMac* gatt_service_mac = device::BluetoothRemoteGattServiceMac* gatt_service_mac =
static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); static_cast<BluetoothRemoteGattServiceMac*>(gatt_service);
gatt_service_mac->DiscoverCharacteristics(); gatt_service_mac->DiscoverCharacteristics();
...@@ -248,8 +247,8 @@ void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( ...@@ -248,8 +247,8 @@ void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics(
bool discovery_complete = bool discovery_complete =
std::find_if_not( std::find_if_not(
gatt_services_.begin(), gatt_services_.end(), gatt_services_.begin(), gatt_services_.end(),
[](std::pair<std::string, BluetoothRemoteGattService*> pair) { [](GattServiceMap::value_type& pair) {
BluetoothRemoteGattService* gatt_service = pair.second; BluetoothRemoteGattService* gatt_service = pair.second.get();
return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service)
->IsDiscoveryComplete(); ->IsDiscoveryComplete();
}) == gatt_services_.end(); }) == gatt_services_.end();
...@@ -270,7 +269,8 @@ void BluetoothLowEnergyDeviceMac::DidModifyServices( ...@@ -270,7 +269,8 @@ void BluetoothLowEnergyDeviceMac::DidModifyServices(
DCHECK(gatt_service); DCHECK(gatt_service);
VLOG(1) << gatt_service->GetUUID().canonical_value(); VLOG(1) << gatt_service->GetUUID().canonical_value();
std::unique_ptr<BluetoothRemoteGattService> scoped_service = std::unique_ptr<BluetoothRemoteGattService> scoped_service =
gatt_services_.take_and_erase(gatt_service->GetIdentifier()); std::move(gatt_services_[gatt_service->GetIdentifier()]);
gatt_services_.erase(gatt_service->GetIdentifier());
adapter_->NotifyGattServiceRemoved(scoped_service.get()); adapter_->NotifyGattServiceRemoved(scoped_service.get());
} }
device_uuids_.ClearServiceUUIDs(); device_uuids_.ClearServiceUUIDs();
...@@ -340,9 +340,8 @@ CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { ...@@ -340,9 +340,8 @@ CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() {
device::BluetoothRemoteGattServiceMac* device::BluetoothRemoteGattServiceMac*
BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService(
CBService* cb_service) const { CBService* cb_service) const {
for (GattServiceMap::const_iterator it = gatt_services_.begin(); for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) {
it != gatt_services_.end(); ++it) { device::BluetoothRemoteGattService* gatt_service = it->second.get();
device::BluetoothRemoteGattService* gatt_service = it->second;
device::BluetoothRemoteGattServiceMac* gatt_service_mac = device::BluetoothRemoteGattServiceMac* gatt_service_mac =
static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); static_cast<BluetoothRemoteGattServiceMac*>(gatt_service);
if (gatt_service_mac->GetService() == cb_service) if (gatt_service_mac->GetService() == cb_service)
......
...@@ -113,7 +113,7 @@ BluetoothRemoteGattCharacteristicAndroid::GetDescriptors() const { ...@@ -113,7 +113,7 @@ BluetoothRemoteGattCharacteristicAndroid::GetDescriptors() const {
EnsureDescriptorsCreated(); EnsureDescriptorsCreated();
std::vector<BluetoothRemoteGattDescriptor*> descriptors; std::vector<BluetoothRemoteGattDescriptor*> descriptors;
for (const auto& map_iter : descriptors_) for (const auto& map_iter : descriptors_)
descriptors.push_back(map_iter.second); descriptors.push_back(map_iter.second.get());
return descriptors; return descriptors;
} }
...@@ -124,7 +124,7 @@ BluetoothRemoteGattCharacteristicAndroid::GetDescriptor( ...@@ -124,7 +124,7 @@ BluetoothRemoteGattCharacteristicAndroid::GetDescriptor(
const auto& iter = descriptors_.find(identifier); const auto& iter = descriptors_.find(identifier);
if (iter == descriptors_.end()) if (iter == descriptors_.end())
return nullptr; return nullptr;
return iter->second; return iter->second.get();
} }
void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic(
...@@ -241,12 +241,11 @@ void BluetoothRemoteGattCharacteristicAndroid::CreateGattRemoteDescriptor( ...@@ -241,12 +241,11 @@ void BluetoothRemoteGattCharacteristicAndroid::CreateGattRemoteDescriptor(
std::string instanceIdString = std::string instanceIdString =
base::android::ConvertJavaStringToUTF8(env, instanceId); base::android::ConvertJavaStringToUTF8(env, instanceId);
DCHECK(!descriptors_.contains(instanceIdString)); DCHECK(descriptors_.find(instanceIdString) == descriptors_.end());
descriptors_.set(instanceIdString, descriptors_[instanceIdString] = BluetoothRemoteGattDescriptorAndroid::Create(
BluetoothRemoteGattDescriptorAndroid::Create( instanceIdString, bluetooth_gatt_descriptor_wrapper,
instanceIdString, bluetooth_gatt_descriptor_wrapper, chrome_bluetooth_device);
chrome_bluetooth_device));
} }
void BluetoothRemoteGattCharacteristicAndroid::SubscribeToNotifications( void BluetoothRemoteGattCharacteristicAndroid::SubscribeToNotifications(
......
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
#include <stdint.h> #include <stdint.h>
#include <memory> #include <memory>
#include <unordered_map>
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/macros.h" #include "base/macros.h"
#include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h" #include "device/bluetooth/bluetooth_remote_gatt_service.h"
...@@ -142,8 +142,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid ...@@ -142,8 +142,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid
std::vector<uint8_t> value_; std::vector<uint8_t> value_;
// Map of descriptors, keyed by descriptor identifier. // Map of descriptors, keyed by descriptor identifier.
base::ScopedPtrHashMap<std::string, std::unordered_map<std::string,
std::unique_ptr<BluetoothRemoteGattDescriptorAndroid>> std::unique_ptr<BluetoothRemoteGattDescriptorAndroid>>
descriptors_; descriptors_;
DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicAndroid); DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicAndroid);
......
...@@ -135,7 +135,7 @@ BluetoothRemoteGattServiceAndroid::GetCharacteristics() const { ...@@ -135,7 +135,7 @@ BluetoothRemoteGattServiceAndroid::GetCharacteristics() const {
EnsureCharacteristicsCreated(); EnsureCharacteristicsCreated();
std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics; std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics;
for (const auto& map_iter : characteristics_) for (const auto& map_iter : characteristics_)
characteristics.push_back(map_iter.second); characteristics.push_back(map_iter.second.get());
return characteristics; return characteristics;
} }
...@@ -152,7 +152,7 @@ BluetoothRemoteGattServiceAndroid::GetCharacteristic( ...@@ -152,7 +152,7 @@ BluetoothRemoteGattServiceAndroid::GetCharacteristic(
const auto& iter = characteristics_.find(identifier); const auto& iter = characteristics_.find(identifier);
if (iter == characteristics_.end()) if (iter == characteristics_.end())
return nullptr; return nullptr;
return iter->second; return iter->second.get();
} }
void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic( void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic(
...@@ -166,13 +166,12 @@ void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic( ...@@ -166,13 +166,12 @@ void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic(
std::string instance_id_string = std::string instance_id_string =
base::android::ConvertJavaStringToUTF8(env, instance_id); base::android::ConvertJavaStringToUTF8(env, instance_id);
DCHECK(!characteristics_.contains(instance_id_string)); DCHECK(characteristics_.find(instance_id_string) == characteristics_.end());
characteristics_.set( characteristics_[instance_id_string] =
instance_id_string,
BluetoothRemoteGattCharacteristicAndroid::Create( BluetoothRemoteGattCharacteristicAndroid::Create(
adapter_, this, instance_id_string, adapter_, this, instance_id_string,
bluetooth_gatt_characteristic_wrapper, chrome_bluetooth_device)); bluetooth_gatt_characteristic_wrapper, chrome_bluetooth_device);
} }
BluetoothRemoteGattServiceAndroid::BluetoothRemoteGattServiceAndroid( BluetoothRemoteGattServiceAndroid::BluetoothRemoteGattServiceAndroid(
......
...@@ -8,11 +8,11 @@ ...@@ -8,11 +8,11 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/macros.h" #include "base/macros.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h" #include "device/bluetooth/bluetooth_remote_gatt_service.h"
#include "device/bluetooth/bluetooth_uuid.h" #include "device/bluetooth/bluetooth_uuid.h"
...@@ -109,9 +109,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceAndroid ...@@ -109,9 +109,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceAndroid
std::string instance_id_; std::string instance_id_;
// Map of characteristics, keyed by characteristic identifier. // Map of characteristics, keyed by characteristic identifier.
base::ScopedPtrHashMap< std::unordered_map<std::string,
std::string, std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid>>
std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid>>
characteristics_; characteristics_;
DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattServiceAndroid); DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattServiceAndroid);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
...@@ -509,10 +510,9 @@ void BluetoothAdapterBlueZ::RemovePairingDelegateInternal( ...@@ -509,10 +510,9 @@ void BluetoothAdapterBlueZ::RemovePairingDelegateInternal(
BluetoothDevice::PairingDelegate* pairing_delegate) { BluetoothDevice::PairingDelegate* pairing_delegate) {
// Check if any device is using the pairing delegate. // Check if any device is using the pairing delegate.
// If so, clear the pairing context which will make any responses no-ops. // If so, clear the pairing context which will make any responses no-ops.
for (DevicesMap::const_iterator iter = devices_.begin(); for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) {
iter != devices_.end(); ++iter) {
BluetoothDeviceBlueZ* device_bluez = BluetoothDeviceBlueZ* device_bluez =
static_cast<BluetoothDeviceBlueZ*>(iter->second); static_cast<BluetoothDeviceBlueZ*>(iter->second.get());
BluetoothPairingBlueZ* pairing = device_bluez->GetPairing(); BluetoothPairingBlueZ* pairing = device_bluez->GetPairing();
if (pairing && pairing->GetPairingDelegate() == pairing_delegate) if (pairing && pairing->GetPairingDelegate() == pairing_delegate)
...@@ -566,21 +566,19 @@ void BluetoothAdapterBlueZ::DeviceAdded(const dbus::ObjectPath& object_path) { ...@@ -566,21 +566,19 @@ void BluetoothAdapterBlueZ::DeviceAdded(const dbus::ObjectPath& object_path) {
this, object_path, ui_task_runner_, socket_thread_); this, object_path, ui_task_runner_, socket_thread_);
DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end());
devices_.set(device_bluez->GetAddress(), devices_[device_bluez->GetAddress()] = base::WrapUnique(device_bluez);
std::unique_ptr<BluetoothDevice>(device_bluez));
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DeviceAdded(this, device_bluez); observer.DeviceAdded(this, device_bluez);
} }
void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) { void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) {
for (DevicesMap::const_iterator iter = devices_.begin(); for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) {
iter != devices_.end(); ++iter) {
BluetoothDeviceBlueZ* device_bluez = BluetoothDeviceBlueZ* device_bluez =
static_cast<BluetoothDeviceBlueZ*>(iter->second); static_cast<BluetoothDeviceBlueZ*>(iter->second.get());
if (device_bluez->object_path() == object_path) { if (device_bluez->object_path() == object_path) {
std::unique_ptr<BluetoothDevice> scoped_device = std::unique_ptr<BluetoothDevice> scoped_device = std::move(iter->second);
devices_.take_and_erase(iter->first); devices_.erase(iter);
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DeviceRemoved(this, device_bluez); observer.DeviceRemoved(this, device_bluez);
...@@ -601,19 +599,17 @@ void BluetoothAdapterBlueZ::DevicePropertyChanged( ...@@ -601,19 +599,17 @@ void BluetoothAdapterBlueZ::DevicePropertyChanged(
object_path); object_path);
if (property_name == properties->address.name()) { if (property_name == properties->address.name()) {
for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) {
++iter) {
if (iter->second->GetAddress() == device_bluez->GetAddress()) { if (iter->second->GetAddress() == device_bluez->GetAddress()) {
std::string old_address = iter->first; std::string old_address = iter->first;
VLOG(1) << "Device changed address, old: " << old_address VLOG(1) << "Device changed address, old: " << old_address
<< " new: " << device_bluez->GetAddress(); << " new: " << device_bluez->GetAddress();
std::unique_ptr<BluetoothDevice> scoped_device = std::unique_ptr<BluetoothDevice> scoped_device =
devices_.take_and_erase(iter); std::move(iter->second);
ignore_result(scoped_device.release()); devices_.erase(iter);
DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end());
devices_.set(device_bluez->GetAddress(), devices_[device_bluez->GetAddress()] = std::move(scoped_device);
std::unique_ptr<BluetoothDevice>(device_bluez));
NotifyDeviceAddressChanged(device_bluez, old_address); NotifyDeviceAddressChanged(device_bluez, old_address);
break; break;
} }
...@@ -670,8 +666,7 @@ void BluetoothAdapterBlueZ::DevicePropertyChanged( ...@@ -670,8 +666,7 @@ void BluetoothAdapterBlueZ::DevicePropertyChanged(
int count = 0; int count = 0;
for (DevicesMap::const_iterator iter = devices_.begin(); for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) {
iter != devices_.end(); ++iter) {
if (iter->second->IsPaired() && iter->second->IsConnected()) if (iter->second->IsPaired() && iter->second->IsConnected())
++count; ++count;
} }
...@@ -903,10 +898,9 @@ BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath( ...@@ -903,10 +898,9 @@ BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath(
if (!IsPresent()) if (!IsPresent())
return nullptr; return nullptr;
for (DevicesMap::const_iterator iter = devices_.begin(); for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) {
iter != devices_.end(); ++iter) {
BluetoothDeviceBlueZ* device_bluez = BluetoothDeviceBlueZ* device_bluez =
static_cast<BluetoothDeviceBlueZ*>(iter->second); static_cast<BluetoothDeviceBlueZ*>(iter->second.get());
if (device_bluez->object_path() == object_path) if (device_bluez->object_path() == object_path)
return device_bluez; return device_bluez;
} }
...@@ -1041,7 +1035,7 @@ void BluetoothAdapterBlueZ::RemoveAdapter() { ...@@ -1041,7 +1035,7 @@ void BluetoothAdapterBlueZ::RemoveAdapter() {
for (auto& iter : devices_swapped) { for (auto& iter : devices_swapped) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DeviceRemoved(this, iter.second); observer.DeviceRemoved(this, iter.second.get());
} }
PresentChanged(false); PresentChanged(false);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
...@@ -180,7 +181,7 @@ BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() { ...@@ -180,7 +181,7 @@ BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() {
for (const auto& iter : gatt_services_swapped) { for (const auto& iter : gatt_services_swapped) {
DCHECK(adapter()); DCHECK(adapter());
adapter()->NotifyGattServiceRemoved( adapter()->NotifyGattServiceRemoved(
static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second.get()));
} }
} }
...@@ -678,8 +679,7 @@ void BluetoothDeviceBlueZ::GattServiceAdded( ...@@ -678,8 +679,7 @@ void BluetoothDeviceBlueZ::GattServiceAdded(
BluetoothRemoteGattServiceBlueZ* service = BluetoothRemoteGattServiceBlueZ* service =
new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path);
gatt_services_.set(service->GetIdentifier(), gatt_services_[service->GetIdentifier()] = base::WrapUnique(service);
std::unique_ptr<BluetoothRemoteGattService>(service));
DCHECK(service->object_path() == object_path); DCHECK(service->object_path() == object_path);
DCHECK(service->GetUUID().IsValid()); DCHECK(service->GetUUID().IsValid());
...@@ -689,15 +689,14 @@ void BluetoothDeviceBlueZ::GattServiceAdded( ...@@ -689,15 +689,14 @@ void BluetoothDeviceBlueZ::GattServiceAdded(
void BluetoothDeviceBlueZ::GattServiceRemoved( void BluetoothDeviceBlueZ::GattServiceRemoved(
const dbus::ObjectPath& object_path) { const dbus::ObjectPath& object_path) {
GattServiceMap::const_iterator iter = auto iter = gatt_services_.find(object_path.value());
gatt_services_.find(object_path.value());
if (iter == gatt_services_.end()) { if (iter == gatt_services_.end()) {
VLOG(3) << "Unknown GATT service removed: " << object_path.value(); VLOG(3) << "Unknown GATT service removed: " << object_path.value();
return; return;
} }
BluetoothRemoteGattServiceBlueZ* service = BluetoothRemoteGattServiceBlueZ* service =
static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second); static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second.get());
VLOG(1) << "Removing remote GATT service with UUID: '" VLOG(1) << "Removing remote GATT service with UUID: '"
<< service->GetUUID().canonical_value() << service->GetUUID().canonical_value()
...@@ -705,7 +704,8 @@ void BluetoothDeviceBlueZ::GattServiceRemoved( ...@@ -705,7 +704,8 @@ void BluetoothDeviceBlueZ::GattServiceRemoved(
DCHECK(service->object_path() == object_path); DCHECK(service->object_path() == object_path);
std::unique_ptr<BluetoothRemoteGattService> scoped_service = std::unique_ptr<BluetoothRemoteGattService> scoped_service =
gatt_services_.take_and_erase(iter->first); std::move(gatt_services_[object_path.value()]);
gatt_services_.erase(iter);
DCHECK(adapter()); DCHECK(adapter());
discovery_complete_notified_.erase(service); discovery_complete_notified_.erase(service);
......
...@@ -49,6 +49,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceBlueZ ...@@ -49,6 +49,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceBlueZ
using GetServiceRecordsErrorCallback = using GetServiceRecordsErrorCallback =
base::Callback<void(BluetoothServiceRecordBlueZ::ErrorCode)>; base::Callback<void(BluetoothServiceRecordBlueZ::ErrorCode)>;
~BluetoothDeviceBlueZ() override;
// BluetoothDevice override // BluetoothDevice override
uint32_t GetBluetoothClass() const override; uint32_t GetBluetoothClass() const override;
device::BluetoothTransport GetType() const override; device::BluetoothTransport GetType() const override;
...@@ -164,7 +166,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceBlueZ ...@@ -164,7 +166,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceBlueZ
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
scoped_refptr<base::SequencedTaskRunner> ui_task_runner, scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
scoped_refptr<device::BluetoothSocketThread> socket_thread); scoped_refptr<device::BluetoothSocketThread> socket_thread);
~BluetoothDeviceBlueZ() override;
// bluez::BluetoothGattServiceClient::Observer overrides // bluez::BluetoothGattServiceClient::Observer overrides
void GattServiceAdded(const dbus::ObjectPath& object_path) override; void GattServiceAdded(const dbus::ObjectPath& object_path) override;
......
...@@ -42,6 +42,8 @@ class BluetoothRemoteGattServiceBlueZ ...@@ -42,6 +42,8 @@ class BluetoothRemoteGattServiceBlueZ
public BluetoothGattCharacteristicClient::Observer, public BluetoothGattCharacteristicClient::Observer,
public device::BluetoothRemoteGattService { public device::BluetoothRemoteGattService {
public: public:
~BluetoothRemoteGattServiceBlueZ() override;
// device::BluetoothRemoteGattService overrides. // device::BluetoothRemoteGattService overrides.
device::BluetoothUUID GetUUID() const override; device::BluetoothUUID GetUUID() const override;
device::BluetoothDevice* GetDevice() const override; device::BluetoothDevice* GetDevice() const override;
...@@ -83,7 +85,6 @@ class BluetoothRemoteGattServiceBlueZ ...@@ -83,7 +85,6 @@ class BluetoothRemoteGattServiceBlueZ
BluetoothRemoteGattServiceBlueZ(BluetoothAdapterBlueZ* adapter, BluetoothRemoteGattServiceBlueZ(BluetoothAdapterBlueZ* adapter,
BluetoothDeviceBlueZ* device, BluetoothDeviceBlueZ* device,
const dbus::ObjectPath& object_path); const dbus::ObjectPath& object_path);
~BluetoothRemoteGattServiceBlueZ() override;
// bluez::BluetoothGattServiceClient::Observer override. // bluez::BluetoothGattServiceClient::Observer override.
void GattServicePropertyChanged(const dbus::ObjectPath& object_path, void GattServicePropertyChanged(const dbus::ObjectPath& object_path,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment