Chromium Code Reviews| Index: device/bluetooth/bluetooth_device_win.cc |
| diff --git a/device/bluetooth/bluetooth_device_win.cc b/device/bluetooth/bluetooth_device_win.cc |
| index e628a25591b9507561c28266de92a845ca0b4408..96d1ab15ebd741cad18c2d0a5229b051a2b23660 100644 |
| --- a/device/bluetooth/bluetooth_device_win.cc |
| +++ b/device/bluetooth/bluetooth_device_win.cc |
| @@ -44,15 +44,6 @@ BluetoothDeviceWin::BluetoothDeviceWin( |
| } |
| BluetoothDeviceWin::~BluetoothDeviceWin() { |
| - // Explicitly take and erase GATT services one by one to ensure that calling |
| - // GetGattService on removed service in GattServiceRemoved returns null. |
| - std::vector<std::string> service_keys; |
| - for (const auto& gatt_service : gatt_services_) { |
| - service_keys.push_back(gatt_service.first); |
| - } |
| - for (const auto& key : service_keys) { |
| - gatt_services_.take_and_erase(key); |
| - } |
| } |
| uint32_t BluetoothDeviceWin::GetBluetoothClass() const { |
| @@ -213,13 +204,11 @@ void BluetoothDeviceWin::CreateGattConnection( |
| const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord( |
| const device::BluetoothUUID& uuid) const { |
| - for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
| - iter != service_record_list_.end(); |
| - ++iter) { |
| - if ((*iter)->uuid() == uuid) |
| - return *iter; |
| + for (auto& record : service_record_list_) { |
| + if (record->uuid() == uuid) |
| + return record.get(); |
| } |
| - return NULL; |
| + return nullptr; |
| } |
| bool BluetoothDeviceWin::IsEqual( |
| @@ -233,21 +222,18 @@ bool BluetoothDeviceWin::IsEqual( |
| } |
| // Checks service collection |
| - typedef base::ScopedPtrHashMap<std::string, |
| - std::unique_ptr<BluetoothServiceRecordWin>> |
| - ServiceRecordMap; |
| - |
| + using ServiceRecordMap = |
| + std::unordered_map<std::string, |
| + std::unique_ptr<BluetoothServiceRecordWin>>; |
| UUIDSet new_services; |
| ServiceRecordMap new_service_records; |
| - for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
| - iter = device_state.service_record_states.begin(); |
| - iter != device_state.service_record_states.end(); ++iter) { |
| - BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin( |
| - address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); |
| + for (auto& state : device_state.service_record_states) { |
| + std::unique_ptr<BluetoothServiceRecordWin> service_record( |
| + new BluetoothServiceRecordWin(address_, state->name, state->sdp_bytes, |
| + state->gatt_uuid)); |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:14
Use auto and base::MakeUnique.
dougt
2016/12/22 01:18:02
Done.
|
| new_services.insert(service_record->uuid()); |
| - new_service_records.set( |
| - service_record->uuid().canonical_value(), |
| - std::unique_ptr<BluetoothServiceRecordWin>(service_record)); |
| + new_service_records.insert(std::make_pair( |
| + service_record->uuid().canonical_value(), std::move(service_record))); |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:14
set() => subscript:
new_service_records[service_r
dougt
2016/12/22 01:18:02
Done.
|
| } |
| // Check that no new services have been added or removed. |
| @@ -255,12 +241,14 @@ bool BluetoothDeviceWin::IsEqual( |
| return false; |
| } |
| - for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
| - iter != service_record_list_.end(); ++iter) { |
| - BluetoothServiceRecordWin* service_record = (*iter); |
| - BluetoothServiceRecordWin* new_service_record = |
| - new_service_records.get((*iter)->uuid().canonical_value()); |
| - if (!service_record->IsEqual(*new_service_record)) |
| + for (auto& service_record : service_record_list_) { |
| + auto new_service_record = |
| + new_service_records.find(service_record->uuid().canonical_value()); |
| + |
| + if (new_service_record == new_service_records.end()) |
| + return false; |
| + |
| + if (!service_record->IsEqual(*new_service_record->second.get())) |
| return false; |
| } |
| return true; |
| @@ -300,14 +288,12 @@ void BluetoothDeviceWin::UpdateServices( |
| uuids_.clear(); |
| service_record_list_.clear(); |
| - for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
| - iter = device_state.service_record_states.begin(); |
| - iter != device_state.service_record_states.end(); ++iter) { |
| - BluetoothServiceRecordWin* service_record = |
| - new BluetoothServiceRecordWin(device_state.address, (*iter)->name, |
| - (*iter)->sdp_bytes, (*iter)->gatt_uuid); |
| - service_record_list_.push_back(service_record); |
| + for (auto& state : device_state.service_record_states) { |
| + std::unique_ptr<BluetoothServiceRecordWin> service_record( |
| + new BluetoothServiceRecordWin(device_state.address, state->name, |
| + state->sdp_bytes, state->gatt_uuid)); |
| uuids_.insert(service_record->uuid()); |
| + service_record_list_.push_back(std::move(service_record)); |
| } |
| if (!device_state.is_bluetooth_classic()) |
| @@ -316,12 +302,11 @@ void BluetoothDeviceWin::UpdateServices( |
| bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, |
| uint16_t attribute_handle) { |
| - GattServiceMap::iterator it = gatt_services_.begin(); |
| - for (; it != gatt_services_.end(); it++) { |
| + for (auto& service : gatt_services_) { |
| uint16_t it_att_handle = |
| - static_cast<BluetoothRemoteGattServiceWin*>(it->second) |
| + static_cast<BluetoothRemoteGattServiceWin*>(service.second.get()) |
| ->GetAttributeHandle(); |
| - BluetoothUUID it_uuid = it->second->GetUUID(); |
| + BluetoothUUID it_uuid = service.second->GetUUID(); |
| if (attribute_handle == it_att_handle && uuid == it_uuid) { |
| return true; |
| } |
| @@ -330,39 +315,39 @@ bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, |
| } |
| bool BluetoothDeviceWin::DoesGattServiceExist( |
| - const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| - service_state, |
| + const std::vector<std::unique_ptr< |
| + BluetoothTaskManagerWin::ServiceRecordState>>& service_state, |
| BluetoothRemoteGattService* service) { |
| uint16_t attribute_handle = |
| static_cast<BluetoothRemoteGattServiceWin*>(service) |
| ->GetAttributeHandle(); |
| BluetoothUUID uuid = service->GetUUID(); |
| - ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator it = |
| - service_state.begin(); |
| - for (; it != service_state.end(); ++it) { |
| - if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid) |
| + |
| + for (auto& state : service_state) { |
| + if (attribute_handle == state->attribute_handle && uuid == state->gatt_uuid) |
| return true; |
| } |
| return false; |
| } |
| void BluetoothDeviceWin::UpdateGattServices( |
| - const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| + const std::vector< |
| + std::unique_ptr<BluetoothTaskManagerWin::ServiceRecordState>>& |
| service_state) { |
| // First, remove no longer exist GATT service. |
| { |
| std::vector<std::string> to_be_removed_services; |
| - for (const auto& gatt_service : gatt_services_) { |
| - if (!DoesGattServiceExist(service_state, gatt_service.second)) { |
| - to_be_removed_services.push_back(gatt_service.first); |
| + for (const auto& service : gatt_services_) { |
| + if (!DoesGattServiceExist(service_state, service.second.get())) { |
| + to_be_removed_services.push_back(service.first); |
| } |
| } |
| for (const auto& service : to_be_removed_services) { |
| - gatt_services_.take_and_erase(service); |
| + gatt_services_.erase(service); |
| } |
| // Update previously discovered services. |
| - for (auto gatt_service : gatt_services_) { |
| - static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second) |
| + for (auto& service : gatt_services_) { |
| + static_cast<BluetoothRemoteGattServiceWin*>(service.second.get()) |
| ->Update(); |
| } |
| } |
| @@ -370,23 +355,22 @@ void BluetoothDeviceWin::UpdateGattServices( |
| // Return if no new services have been added. |
| if (gatt_services_.size() == service_state.size()) |
| return; |
| - |
| // Add new services. |
| - for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
| - it = service_state.begin(); |
| - it != service_state.end(); ++it) { |
| - if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) { |
| - BluetoothRemoteGattServiceWin* primary_service = |
| - new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, |
| - (*it)->attribute_handle, true, |
| - nullptr, ui_task_runner_); |
| - gatt_services_.add( |
| - primary_service->GetIdentifier(), |
| - std::unique_ptr<BluetoothRemoteGattService>(primary_service)); |
| - adapter_->NotifyGattServiceAdded(primary_service); |
| + for (auto& state : service_state) { |
| + if (!IsGattServiceDiscovered(state->gatt_uuid, state->attribute_handle)) { |
| + std::unique_ptr<BluetoothRemoteGattService> primary_service( |
| + new BluetoothRemoteGattServiceWin(this, state->path, state->gatt_uuid, |
| + state->attribute_handle, true, |
| + nullptr, ui_task_runner_)); |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:14
Use auto and base::MakeUnique.
dougt
2016/12/22 01:18:02
Done.
|
| + |
| + auto insertion = gatt_services_.insert(std::make_pair( |
| + primary_service->GetIdentifier(), std::move(primary_service))); |
| + |
| + if (insertion.second) { |
| + adapter_->NotifyGattServiceAdded(insertion.first->second.get()); |
| + } |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:14
nit: no braces around single-line ifs
dougt
2016/12/22 01:18:02
Done.
|
| } |
| } |
| - |
| adapter_->NotifyGattServicesDiscovered(this); |
| } |