OLD | NEW |
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_adapter.h" | 5 #include "device/bluetooth/bluetooth_adapter.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/stl_util.h" | |
13 #include "build/build_config.h" | 12 #include "build/build_config.h" |
14 #include "device/bluetooth/bluetooth_common.h" | 13 #include "device/bluetooth/bluetooth_common.h" |
15 #include "device/bluetooth/bluetooth_device.h" | 14 #include "device/bluetooth/bluetooth_device.h" |
16 #include "device/bluetooth/bluetooth_discovery_session.h" | 15 #include "device/bluetooth/bluetooth_discovery_session.h" |
17 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 16 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" |
18 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 17 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
19 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" | 18 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" |
20 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 19 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
21 | 20 |
22 namespace device { | 21 namespace device { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 DeviceList devices; | 102 DeviceList devices; |
104 for (ConstDeviceList::const_iterator i = const_devices.begin(); | 103 for (ConstDeviceList::const_iterator i = const_devices.begin(); |
105 i != const_devices.end(); ++i) | 104 i != const_devices.end(); ++i) |
106 devices.push_back(const_cast<BluetoothDevice *>(*i)); | 105 devices.push_back(const_cast<BluetoothDevice *>(*i)); |
107 | 106 |
108 return devices; | 107 return devices; |
109 } | 108 } |
110 | 109 |
111 BluetoothAdapter::ConstDeviceList BluetoothAdapter::GetDevices() const { | 110 BluetoothAdapter::ConstDeviceList BluetoothAdapter::GetDevices() const { |
112 ConstDeviceList devices; | 111 ConstDeviceList devices; |
113 for (DevicesMap::const_iterator iter = devices_.begin(); | 112 for (const auto& device : devices_) |
114 iter != devices_.end(); | 113 devices.push_back(device.second.get()); |
115 ++iter) | |
116 devices.push_back(iter->second); | |
117 | 114 |
118 return devices; | 115 return devices; |
119 } | 116 } |
120 | 117 |
121 BluetoothDevice* BluetoothAdapter::GetDevice(const std::string& address) { | 118 BluetoothDevice* BluetoothAdapter::GetDevice(const std::string& address) { |
122 return const_cast<BluetoothDevice *>( | 119 return const_cast<BluetoothDevice *>( |
123 const_cast<const BluetoothAdapter *>(this)->GetDevice(address)); | 120 const_cast<const BluetoothAdapter *>(this)->GetDevice(address)); |
124 } | 121 } |
125 | 122 |
126 const BluetoothDevice* BluetoothAdapter::GetDevice( | 123 const BluetoothDevice* BluetoothAdapter::GetDevice( |
127 const std::string& address) const { | 124 const std::string& address) const { |
128 std::string canonicalized_address = | 125 std::string canonicalized_address = |
129 BluetoothDevice::CanonicalizeAddress(address); | 126 BluetoothDevice::CanonicalizeAddress(address); |
130 if (canonicalized_address.empty()) | 127 if (canonicalized_address.empty()) |
131 return nullptr; | 128 return nullptr; |
132 | 129 |
133 DevicesMap::const_iterator iter = devices_.find(canonicalized_address); | 130 auto iter = devices_.find(canonicalized_address); |
134 if (iter != devices_.end()) | 131 if (iter != devices_.end()) |
135 return iter->second; | 132 return iter->second.get(); |
136 | 133 |
137 return nullptr; | 134 return nullptr; |
138 } | 135 } |
139 | 136 |
140 void BluetoothAdapter::AddPairingDelegate( | 137 void BluetoothAdapter::AddPairingDelegate( |
141 BluetoothDevice::PairingDelegate* pairing_delegate, | 138 BluetoothDevice::PairingDelegate* pairing_delegate, |
142 PairingDelegatePriority priority) { | 139 PairingDelegatePriority priority) { |
143 // Remove the delegate, if it already exists, before inserting to allow a | 140 // Remove the delegate, if it already exists, before inserting to allow a |
144 // change of priority. | 141 // change of priority. |
145 RemovePairingDelegate(pairing_delegate); | 142 RemovePairingDelegate(pairing_delegate); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 continue; | 366 continue; |
370 } | 367 } |
371 | 368 |
372 result = BluetoothDiscoveryFilter::Merge(result.get(), curr_filter); | 369 result = BluetoothDiscoveryFilter::Merge(result.get(), curr_filter); |
373 } | 370 } |
374 | 371 |
375 return result; | 372 return result; |
376 } | 373 } |
377 | 374 |
378 void BluetoothAdapter::RemoveTimedOutDevices() { | 375 void BluetoothAdapter::RemoveTimedOutDevices() { |
379 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end();) { | 376 for (auto it = devices_.begin(); it != devices_.end();) { |
380 BluetoothDevice* device = it->second; | 377 BluetoothDevice* device = it->second.get(); |
381 if (device->IsPaired() || device->IsConnected() || | 378 if (device->IsPaired() || device->IsConnected() || |
382 device->IsGattConnected()) { | 379 device->IsGattConnected()) { |
383 ++it; | 380 ++it; |
384 continue; | 381 continue; |
385 } | 382 } |
386 | 383 |
387 base::Time last_update_time = device->GetLastUpdateTime(); | 384 base::Time last_update_time = device->GetLastUpdateTime(); |
388 | 385 |
389 bool device_expired = | 386 bool device_expired = |
390 (base::Time::NowFromSystemTime() - last_update_time) > timeoutSec; | 387 (base::Time::NowFromSystemTime() - last_update_time) > timeoutSec; |
391 VLOG(3) << "device: " << device->GetAddress() | 388 VLOG(3) << "device: " << device->GetAddress() |
392 << ", last_update: " << last_update_time | 389 << ", last_update: " << last_update_time |
393 << ", exp: " << device_expired; | 390 << ", exp: " << device_expired; |
394 | 391 |
395 if (!device_expired) { | 392 if (!device_expired) { |
396 ++it; | 393 ++it; |
397 continue; | 394 continue; |
398 } | 395 } |
399 | 396 |
400 VLOG(1) << "Removing device: " << device->GetAddress(); | 397 VLOG(1) << "Removing device: " << device->GetAddress(); |
401 DevicesMap::iterator next = it; | 398 auto next = it; |
402 next++; | 399 next++; |
403 std::unique_ptr<BluetoothDevice> removed_device = | 400 std::unique_ptr<BluetoothDevice> removed_device = std::move(it->second); |
404 devices_.take_and_erase(it); | 401 devices_.erase(it); |
405 it = next; | 402 it = next; |
406 | 403 |
407 for (auto& observer : observers_) | 404 for (auto& observer : observers_) |
408 observer.DeviceRemoved(this, removed_device.get()); | 405 observer.DeviceRemoved(this, removed_device.get()); |
409 } | 406 } |
410 } | 407 } |
411 | 408 |
412 // static | 409 // static |
413 void BluetoothAdapter::RecordBluetoothDiscoverySessionStartOutcome( | 410 void BluetoothAdapter::RecordBluetoothDiscoverySessionStartOutcome( |
414 UMABluetoothDiscoverySessionOutcome outcome) { | 411 UMABluetoothDiscoverySessionOutcome outcome) { |
415 UMA_HISTOGRAM_ENUMERATION( | 412 UMA_HISTOGRAM_ENUMERATION( |
416 "Bluetooth.DiscoverySession.Start.Outcome", static_cast<int>(outcome), | 413 "Bluetooth.DiscoverySession.Start.Outcome", static_cast<int>(outcome), |
417 static_cast<int>(UMABluetoothDiscoverySessionOutcome::COUNT)); | 414 static_cast<int>(UMABluetoothDiscoverySessionOutcome::COUNT)); |
418 } | 415 } |
419 | 416 |
420 // static | 417 // static |
421 void BluetoothAdapter::RecordBluetoothDiscoverySessionStopOutcome( | 418 void BluetoothAdapter::RecordBluetoothDiscoverySessionStopOutcome( |
422 UMABluetoothDiscoverySessionOutcome outcome) { | 419 UMABluetoothDiscoverySessionOutcome outcome) { |
423 UMA_HISTOGRAM_ENUMERATION( | 420 UMA_HISTOGRAM_ENUMERATION( |
424 "Bluetooth.DiscoverySession.Stop.Outcome", static_cast<int>(outcome), | 421 "Bluetooth.DiscoverySession.Stop.Outcome", static_cast<int>(outcome), |
425 static_cast<int>(UMABluetoothDiscoverySessionOutcome::COUNT)); | 422 static_cast<int>(UMABluetoothDiscoverySessionOutcome::COUNT)); |
426 } | 423 } |
427 | 424 |
428 // static | 425 // static |
429 const base::TimeDelta BluetoothAdapter::timeoutSec = | 426 const base::TimeDelta BluetoothAdapter::timeoutSec = |
430 base::TimeDelta::FromSeconds(180); | 427 base::TimeDelta::FromSeconds(180); |
431 | 428 |
432 } // namespace device | 429 } // namespace device |
OLD | NEW |