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

Side by Side Diff: device/bluetooth/bluetooth_low_energy_device_mac.mm

Issue 2567903004: Replace ScopedVector/ScopedPtrHashMap with std::vector and std::unordered_map (Closed)
Patch Set: Mac bustage 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_low_energy_device_mac.h" 5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
6 6
7 #import <CoreFoundation/CoreFoundation.h> 7 #import <CoreFoundation/CoreFoundation.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include "base/mac/mac_util.h" 10 #include "base/mac/mac_util.h"
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 // Don't create services if the device disconnected. 199 // Don't create services if the device disconnected.
200 return; 200 return;
201 } 201 }
202 202
203 for (CBService* cb_service in GetPeripheral().services) { 203 for (CBService* cb_service in GetPeripheral().services) {
204 BluetoothRemoteGattServiceMac* gatt_service = 204 BluetoothRemoteGattServiceMac* gatt_service =
205 GetBluetoothRemoteGattService(cb_service); 205 GetBluetoothRemoteGattService(cb_service);
206 if (!gatt_service) { 206 if (!gatt_service) {
207 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, 207 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service,
208 true /* is_primary */); 208 true /* is_primary */);
209 auto result_iter = gatt_services_.add(gatt_service->GetIdentifier(), 209 auto insertion = gatt_services_.insert(std::make_pair(
210 base::WrapUnique(gatt_service)); 210 gatt_service->GetIdentifier(), std::move(gatt_service)));
211 DCHECK(result_iter.second); 211 if (!insertion.second) {
212 adapter_->NotifyGattServiceAdded(gatt_service); 212 VLOG(1) << "Insertion of service failed.";
213 continue;
214 }
215 adapter_->NotifyGattServiceAdded(insertion.first->second.get());
213 } 216 }
214 } 217 }
215 for (GattServiceMap::const_iterator it = gatt_services_.begin(); 218 for (auto& gatt_service : gatt_services_) {
216 it != gatt_services_.end(); ++it) {
217 device::BluetoothRemoteGattService* gatt_service = it->second;
218 device::BluetoothRemoteGattServiceMac* gatt_service_mac = 219 device::BluetoothRemoteGattServiceMac* gatt_service_mac =
219 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); 220 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service.second.get());
220 gatt_service_mac->DiscoverCharacteristics(); 221 gatt_service_mac->DiscoverCharacteristics();
221 } 222 }
222 } 223 }
223 224
224 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( 225 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics(
225 CBService* cb_service, 226 CBService* cb_service,
226 NSError* error) { 227 NSError* error) {
227 if (error) { 228 if (error) {
228 // TODO(http://crbug.com/609320): Need to pass the error. 229 // TODO(http://crbug.com/609320): Need to pass the error.
229 // TODO(http://crbug.com/609844): Decide what to do if discover failed 230 // TODO(http://crbug.com/609844): Decide what to do if discover failed
(...skipping 11 matching lines...) Expand all
241 242
242 BluetoothRemoteGattServiceMac* gatt_service = 243 BluetoothRemoteGattServiceMac* gatt_service =
243 GetBluetoothRemoteGattService(cb_service); 244 GetBluetoothRemoteGattService(cb_service);
244 DCHECK(gatt_service); 245 DCHECK(gatt_service);
245 gatt_service->DidDiscoverCharacteristics(); 246 gatt_service->DidDiscoverCharacteristics();
246 247
247 // Notify when all services have been discovered. 248 // Notify when all services have been discovered.
248 bool discovery_complete = 249 bool discovery_complete =
249 std::find_if_not( 250 std::find_if_not(
250 gatt_services_.begin(), gatt_services_.end(), 251 gatt_services_.begin(), gatt_services_.end(),
251 [](std::pair<std::string, BluetoothRemoteGattService*> pair) { 252 [](std::pair<const std::string,
252 BluetoothRemoteGattService* gatt_service = pair.second; 253 std::unique_ptr<device::BluetoothRemoteGattService>>&
254 pair) {
255 BluetoothRemoteGattService* gatt_service = pair.second.get();
253 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) 256 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service)
254 ->IsDiscoveryComplete(); 257 ->IsDiscoveryComplete();
255 }) == gatt_services_.end(); 258 }) == gatt_services_.end();
256 if (discovery_complete) { 259 if (discovery_complete) {
257 device_uuids_.ReplaceServiceUUIDs(gatt_services_); 260 device_uuids_.ReplaceServiceUUIDs(gatt_services_);
258 SetGattServicesDiscoveryComplete(true); 261 SetGattServicesDiscoveryComplete(true);
259 adapter_->NotifyGattServicesDiscovered(this); 262 adapter_->NotifyGattServicesDiscovered(this);
260 adapter_->NotifyDeviceChanged(this); 263 adapter_->NotifyDeviceChanged(this);
261 } 264 }
262 } 265 }
263 266
264 void BluetoothLowEnergyDeviceMac::DidModifyServices( 267 void BluetoothLowEnergyDeviceMac::DidModifyServices(
265 NSArray* invalidatedServices) { 268 NSArray* invalidatedServices) {
266 VLOG(1) << "DidModifyServices: "; 269 VLOG(1) << "DidModifyServices: ";
267 for (CBService* cb_service in invalidatedServices) { 270 for (CBService* cb_service in invalidatedServices) {
268 BluetoothRemoteGattServiceMac* gatt_service = 271 BluetoothRemoteGattServiceMac* gatt_service =
269 GetBluetoothRemoteGattService(cb_service); 272 GetBluetoothRemoteGattService(cb_service);
270 DCHECK(gatt_service); 273 DCHECK(gatt_service);
271 VLOG(1) << gatt_service->GetUUID().canonical_value(); 274 VLOG(1) << gatt_service->GetUUID().canonical_value();
272 std::unique_ptr<BluetoothRemoteGattService> scoped_service = 275
273 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); 276 auto iter = gatt_services_.find(gatt_service->GetIdentifier());
277 if (iter == gatt_services_.end())
278 return;
279
280 auto scoped_service = std::move(iter->second);
281 gatt_services_.erase(iter);
282
274 adapter_->NotifyGattServiceRemoved(scoped_service.get()); 283 adapter_->NotifyGattServiceRemoved(scoped_service.get());
275 } 284 }
276 device_uuids_.ClearServiceUUIDs(); 285 device_uuids_.ClearServiceUUIDs();
277 SetGattServicesDiscoveryComplete(false); 286 SetGattServicesDiscoveryComplete(false);
278 adapter_->NotifyDeviceChanged(this); 287 adapter_->NotifyDeviceChanged(this);
279 [GetPeripheral() discoverServices:nil]; 288 [GetPeripheral() discoverServices:nil];
280 } 289 }
281 290
282 void BluetoothLowEnergyDeviceMac::DidUpdateValue( 291 void BluetoothLowEnergyDeviceMac::DidUpdateValue(
283 CBCharacteristic* characteristic, 292 CBCharacteristic* characteristic,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 344
336 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { 345 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() {
337 return peripheral_; 346 return peripheral_;
338 } 347 }
339 348
340 device::BluetoothRemoteGattServiceMac* 349 device::BluetoothRemoteGattServiceMac*
341 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( 350 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService(
342 CBService* cb_service) const { 351 CBService* cb_service) const {
343 for (GattServiceMap::const_iterator it = gatt_services_.begin(); 352 for (GattServiceMap::const_iterator it = gatt_services_.begin();
344 it != gatt_services_.end(); ++it) { 353 it != gatt_services_.end(); ++it) {
345 device::BluetoothRemoteGattService* gatt_service = it->second; 354 device::BluetoothRemoteGattService* gatt_service = it->second.get();
346 device::BluetoothRemoteGattServiceMac* gatt_service_mac = 355 device::BluetoothRemoteGattServiceMac* gatt_service_mac =
347 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); 356 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service);
348 if (gatt_service_mac->GetService() == cb_service) 357 if (gatt_service_mac->GetService() == cb_service)
349 return gatt_service_mac; 358 return gatt_service_mac;
350 } 359 }
351 return nullptr; 360 return nullptr;
352 } 361 }
353 362
354 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { 363 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) {
355 SetGattServicesDiscoveryComplete(false); 364 SetGattServicesDiscoveryComplete(false);
356 // Removing all services at once to ensure that calling GetGattService on 365 // Removing all services at once to ensure that calling GetGattService on
357 // removed service in GattServiceRemoved returns null. 366 // removed service in GattServiceRemoved returns null.
358 GattServiceMap gatt_services_swapped; 367 GattServiceMap gatt_services_swapped;
359 gatt_services_swapped.swap(gatt_services_); 368 gatt_services_swapped.swap(gatt_services_);
360 gatt_services_swapped.clear(); 369 gatt_services_swapped.clear();
361 device_uuids_.ClearServiceUUIDs(); 370 device_uuids_.ClearServiceUUIDs();
362 // There are two cases in which this function will be called: 371 // There are two cases in which this function will be called:
363 // 1. When the connection to the device breaks (either because 372 // 1. When the connection to the device breaks (either because
364 // we closed it or the device closed it). 373 // we closed it or the device closed it).
365 // 2. When we cancel a pending connection request. 374 // 2. When we cancel a pending connection request.
366 if (create_gatt_connection_error_callbacks_.empty()) { 375 if (create_gatt_connection_error_callbacks_.empty()) {
367 // If there are no pending callbacks then the connection broke (#1). 376 // If there are no pending callbacks then the connection broke (#1).
368 DidDisconnectGatt(); 377 DidDisconnectGatt();
369 return; 378 return;
370 } 379 }
371 // Else we canceled the connection request (#2). 380 // Else we canceled the connection request (#2).
372 // TODO(http://crbug.com/585897): Need to pass the error. 381 // TODO(http://crbug.com/585897): Need to pass the error.
373 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); 382 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED);
374 } 383 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698