Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: device/bluetooth/bluetooth_device_win.cc

Issue 2567903004: Replace ScopedVector/ScopedPtrHashMap with std::vector and std::unordered_map (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_win.h" 5 #include "device/bluetooth/bluetooth_device_win.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/containers/scoped_ptr_hash_map.h" 10 #include "base/containers/scoped_ptr_hash_map.h"
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 return false; 232 return false;
233 } 233 }
234 234
235 // Checks service collection 235 // Checks service collection
236 typedef base::ScopedPtrHashMap<std::string, 236 typedef base::ScopedPtrHashMap<std::string,
237 std::unique_ptr<BluetoothServiceRecordWin>> 237 std::unique_ptr<BluetoothServiceRecordWin>>
238 ServiceRecordMap; 238 ServiceRecordMap;
239 239
240 UUIDSet new_services; 240 UUIDSet new_services;
241 ServiceRecordMap new_service_records; 241 ServiceRecordMap new_service_records;
242 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator 242 for (auto& state : device_state.service_record_states) {
243 iter = device_state.service_record_states.begin();
244 iter != device_state.service_record_states.end(); ++iter) {
245 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin( 243 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin(
246 address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); 244 address_, state->name, state->sdp_bytes, state->gatt_uuid);
247 new_services.insert(service_record->uuid()); 245 new_services.insert(service_record->uuid());
248 new_service_records.set( 246 new_service_records.set(
249 service_record->uuid().canonical_value(), 247 service_record->uuid().canonical_value(),
250 std::unique_ptr<BluetoothServiceRecordWin>(service_record)); 248 std::unique_ptr<BluetoothServiceRecordWin>(service_record));
251 } 249 }
252 250
253 // Check that no new services have been added or removed. 251 // Check that no new services have been added or removed.
254 if (uuids_ != new_services) { 252 if (uuids_ != new_services) {
255 return false; 253 return false;
256 } 254 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 291
294 void BluetoothDeviceWin::SetVisible(bool visible) { 292 void BluetoothDeviceWin::SetVisible(bool visible) {
295 visible_ = visible; 293 visible_ = visible;
296 } 294 }
297 295
298 void BluetoothDeviceWin::UpdateServices( 296 void BluetoothDeviceWin::UpdateServices(
299 const BluetoothTaskManagerWin::DeviceState& device_state) { 297 const BluetoothTaskManagerWin::DeviceState& device_state) {
300 uuids_.clear(); 298 uuids_.clear();
301 service_record_list_.clear(); 299 service_record_list_.clear();
302 300
303 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator 301 for (auto& state : device_state.service_record_states) {
304 iter = device_state.service_record_states.begin(); 302 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin(
305 iter != device_state.service_record_states.end(); ++iter) { 303 device_state.address, state->name, state->sdp_bytes, state->gatt_uuid);
306 BluetoothServiceRecordWin* service_record =
307 new BluetoothServiceRecordWin(device_state.address, (*iter)->name,
308 (*iter)->sdp_bytes, (*iter)->gatt_uuid);
309 service_record_list_.push_back(service_record); 304 service_record_list_.push_back(service_record);
310 uuids_.insert(service_record->uuid()); 305 uuids_.insert(service_record->uuid());
311 } 306 }
312 307
313 if (!device_state.is_bluetooth_classic()) 308 if (!device_state.is_bluetooth_classic())
314 UpdateGattServices(device_state.service_record_states); 309 UpdateGattServices(device_state.service_record_states);
315 } 310 }
316 311
317 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, 312 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid,
318 uint16_t attribute_handle) { 313 uint16_t attribute_handle) {
319 GattServiceMap::iterator it = gatt_services_.begin(); 314 GattServiceMap::iterator it = gatt_services_.begin();
320 for (; it != gatt_services_.end(); it++) { 315 for (; it != gatt_services_.end(); it++) {
321 uint16_t it_att_handle = 316 uint16_t it_att_handle =
322 static_cast<BluetoothRemoteGattServiceWin*>(it->second) 317 static_cast<BluetoothRemoteGattServiceWin*>(it->second)
323 ->GetAttributeHandle(); 318 ->GetAttributeHandle();
324 BluetoothUUID it_uuid = it->second->GetUUID(); 319 BluetoothUUID it_uuid = it->second->GetUUID();
325 if (attribute_handle == it_att_handle && uuid == it_uuid) { 320 if (attribute_handle == it_att_handle && uuid == it_uuid) {
326 return true; 321 return true;
327 } 322 }
328 } 323 }
329 return false; 324 return false;
330 } 325 }
331 326
332 bool BluetoothDeviceWin::DoesGattServiceExist( 327 bool BluetoothDeviceWin::DoesGattServiceExist(
333 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& 328 const std::vector<std::unique_ptr<
334 service_state, 329 BluetoothTaskManagerWin::ServiceRecordState>>& service_state,
335 BluetoothRemoteGattService* service) { 330 BluetoothRemoteGattService* service) {
336 uint16_t attribute_handle = 331 uint16_t attribute_handle =
337 static_cast<BluetoothRemoteGattServiceWin*>(service) 332 static_cast<BluetoothRemoteGattServiceWin*>(service)
338 ->GetAttributeHandle(); 333 ->GetAttributeHandle();
339 BluetoothUUID uuid = service->GetUUID(); 334 BluetoothUUID uuid = service->GetUUID();
340 ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator it = 335 for (auto& state : service_state) {
341 service_state.begin(); 336 if (attribute_handle == state->attribute_handle && uuid == state->gatt_uuid)
342 for (; it != service_state.end(); ++it) {
343 if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid)
344 return true; 337 return true;
345 } 338 }
346 return false; 339 return false;
347 } 340 }
348 341
349 void BluetoothDeviceWin::UpdateGattServices( 342 void BluetoothDeviceWin::UpdateGattServices(
350 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& 343 const std::vector<
344 std::unique_ptr<BluetoothTaskManagerWin::ServiceRecordState>>&
351 service_state) { 345 service_state) {
352 // First, remove no longer exist GATT service. 346 // First, remove no longer exist GATT service.
353 { 347 {
354 std::vector<std::string> to_be_removed_services; 348 std::vector<std::string> to_be_removed_services;
355 for (const auto& gatt_service : gatt_services_) { 349 for (const auto& gatt_service : gatt_services_) {
356 if (!DoesGattServiceExist(service_state, gatt_service.second)) { 350 if (!DoesGattServiceExist(service_state, gatt_service.second)) {
357 to_be_removed_services.push_back(gatt_service.first); 351 to_be_removed_services.push_back(gatt_service.first);
358 } 352 }
359 } 353 }
360 for (const auto& service : to_be_removed_services) { 354 for (const auto& service : to_be_removed_services) {
361 gatt_services_.take_and_erase(service); 355 gatt_services_.take_and_erase(service);
362 } 356 }
363 // Update previously discovered services. 357 // Update previously discovered services.
364 for (auto gatt_service : gatt_services_) { 358 for (auto gatt_service : gatt_services_) {
365 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second) 359 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second)
366 ->Update(); 360 ->Update();
367 } 361 }
368 } 362 }
369 363
370 // Return if no new services have been added. 364 // Return if no new services have been added.
371 if (gatt_services_.size() == service_state.size()) 365 if (gatt_services_.size() == service_state.size())
372 return; 366 return;
373 367
374 // Add new services. 368 // Add new services.
375 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator 369 for (auto& state : service_state) {
376 it = service_state.begin(); 370 if (!IsGattServiceDiscovered(state->gatt_uuid, state->attribute_handle)) {
377 it != service_state.end(); ++it) {
378 if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) {
379 BluetoothRemoteGattServiceWin* primary_service = 371 BluetoothRemoteGattServiceWin* primary_service =
380 new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, 372 new BluetoothRemoteGattServiceWin(this, state->path, state->gatt_uuid,
381 (*it)->attribute_handle, true, 373 state->attribute_handle, true,
382 nullptr, ui_task_runner_); 374 nullptr, ui_task_runner_);
383 gatt_services_.add( 375 gatt_services_.add(
384 primary_service->GetIdentifier(), 376 primary_service->GetIdentifier(),
385 std::unique_ptr<BluetoothRemoteGattService>(primary_service)); 377 std::unique_ptr<BluetoothRemoteGattService>(primary_service));
386 adapter_->NotifyGattServiceAdded(primary_service); 378 adapter_->NotifyGattServiceAdded(primary_service);
387 } 379 }
388 } 380 }
389 381
390 adapter_->NotifyGattServicesDiscovered(this); 382 adapter_->NotifyGattServicesDiscovered(this);
391 } 383 }
392 384
393 } // namespace device 385 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698