| 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_win.h" | 5 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 14 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "device/bluetooth/bluetooth_device_win.h" | 18 #include "device/bluetooth/bluetooth_device_win.h" |
| 18 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 19 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" |
| 19 #include "device/bluetooth/bluetooth_socket_thread.h" | 20 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 20 #include "device/bluetooth/bluetooth_socket_win.h" | 21 #include "device/bluetooth/bluetooth_socket_win.h" |
| 21 #include "device/bluetooth/bluetooth_task_manager_win.h" | 22 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 22 #include "device/bluetooth/bluetooth_uuid.h" | 23 #include "device/bluetooth/bluetooth_uuid.h" |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 } | 228 } |
| 228 | 229 |
| 229 void BluetoothAdapterWin::DevicesPolled( | 230 void BluetoothAdapterWin::DevicesPolled( |
| 230 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) { | 231 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) { |
| 231 DCHECK(thread_checker_.CalledOnValidThread()); | 232 DCHECK(thread_checker_.CalledOnValidThread()); |
| 232 | 233 |
| 233 // We are receiving a new list of all devices known to the system. Merge this | 234 // We are receiving a new list of all devices known to the system. Merge this |
| 234 // new list with the list we know of (|devices_|) and raise corresponding | 235 // new list with the list we know of (|devices_|) and raise corresponding |
| 235 // DeviceAdded, DeviceRemoved and DeviceChanged events. | 236 // DeviceAdded, DeviceRemoved and DeviceChanged events. |
| 236 | 237 |
| 237 typedef std::set<std::string> DeviceAddressSet; | 238 using DeviceAddressSet = std::set<std::string>; |
| 238 DeviceAddressSet known_devices; | 239 DeviceAddressSet known_devices; |
| 239 for (DevicesMap::const_iterator iter = devices_.begin(); | 240 for (const auto& device : devices_) |
| 240 iter != devices_.end(); | 241 known_devices.insert(device.first); |
| 241 ++iter) { | |
| 242 known_devices.insert((*iter).first); | |
| 243 } | |
| 244 | 242 |
| 245 DeviceAddressSet new_devices; | 243 DeviceAddressSet new_devices; |
| 246 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = | 244 for (auto iter = devices.begin(); iter != devices.end(); ++iter) |
| 247 devices.begin(); | |
| 248 iter != devices.end(); | |
| 249 ++iter) { | |
| 250 new_devices.insert((*iter)->address); | 245 new_devices.insert((*iter)->address); |
| 251 } | |
| 252 | 246 |
| 253 // Process device removal first | 247 // Process device removal first. |
| 254 DeviceAddressSet removed_devices = | 248 DeviceAddressSet removed_devices = |
| 255 base::STLSetDifference<DeviceAddressSet>(known_devices, new_devices); | 249 base::STLSetDifference<DeviceAddressSet>(known_devices, new_devices); |
| 256 for (DeviceAddressSet::const_iterator iter = removed_devices.begin(); | 250 for (const auto& device : removed_devices) { |
| 257 iter != removed_devices.end(); | 251 auto it = devices_.find(device); |
| 258 ++iter) { | 252 std::unique_ptr<BluetoothDevice> device_win = std::move(it->second); |
| 259 std::unique_ptr<BluetoothDevice> device_win = | 253 devices_.erase(it); |
| 260 devices_.take_and_erase(*iter); | |
| 261 for (auto& observer : observers_) | 254 for (auto& observer : observers_) |
| 262 observer.DeviceRemoved(this, device_win.get()); | 255 observer.DeviceRemoved(this, device_win.get()); |
| 263 } | 256 } |
| 264 | 257 |
| 265 // Process added and (maybe) changed devices in one pass | 258 // Process added and (maybe) changed devices in one pass. |
| 266 DeviceAddressSet added_devices = | 259 DeviceAddressSet added_devices = |
| 267 base::STLSetDifference<DeviceAddressSet>(new_devices, known_devices); | 260 base::STLSetDifference<DeviceAddressSet>(new_devices, known_devices); |
| 268 DeviceAddressSet changed_devices = | 261 DeviceAddressSet changed_devices = |
| 269 base::STLSetIntersection<DeviceAddressSet>(known_devices, new_devices); | 262 base::STLSetIntersection<DeviceAddressSet>(known_devices, new_devices); |
| 270 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = | 263 for (auto iter = devices.begin(); iter != devices.end(); ++iter) { |
| 271 devices.begin(); | |
| 272 iter != devices.end(); | |
| 273 ++iter) { | |
| 274 BluetoothTaskManagerWin::DeviceState* device_state = (*iter); | 264 BluetoothTaskManagerWin::DeviceState* device_state = (*iter); |
| 275 if (added_devices.find(device_state->address) != added_devices.end()) { | 265 if (added_devices.find(device_state->address) != added_devices.end()) { |
| 276 BluetoothDeviceWin* device_win = | 266 BluetoothDeviceWin* device_win = |
| 277 new BluetoothDeviceWin(this, *device_state, ui_task_runner_, | 267 new BluetoothDeviceWin(this, *device_state, ui_task_runner_, |
| 278 socket_thread_, NULL, net::NetLogSource()); | 268 socket_thread_, NULL, net::NetLogSource()); |
| 279 devices_.set(device_state->address, | 269 devices_[device_state->address] = base::WrapUnique(device_win); |
| 280 std::unique_ptr<BluetoothDevice>(device_win)); | |
| 281 for (auto& observer : observers_) | 270 for (auto& observer : observers_) |
| 282 observer.DeviceAdded(this, device_win); | 271 observer.DeviceAdded(this, device_win); |
| 283 } else if (changed_devices.find(device_state->address) != | 272 } else if (changed_devices.find(device_state->address) != |
| 284 changed_devices.end()) { | 273 changed_devices.end()) { |
| 285 DevicesMap::const_iterator iter = devices_.find(device_state->address); | 274 auto iter = devices_.find(device_state->address); |
| 286 DCHECK(iter != devices_.end()); | 275 DCHECK(iter != devices_.end()); |
| 287 BluetoothDeviceWin* device_win = | 276 BluetoothDeviceWin* device_win = |
| 288 static_cast<BluetoothDeviceWin*>(iter->second); | 277 static_cast<BluetoothDeviceWin*>(iter->second.get()); |
| 289 if (!device_win->IsEqual(*device_state)) { | 278 if (!device_win->IsEqual(*device_state)) { |
| 290 device_win->Update(*device_state); | 279 device_win->Update(*device_state); |
| 291 for (auto& observer : observers_) | 280 for (auto& observer : observers_) |
| 292 observer.DeviceChanged(this, device_win); | 281 observer.DeviceChanged(this, device_win); |
| 293 } | 282 } |
| 294 // Above IsEqual returns true if device name, address, status and services | 283 // Above IsEqual returns true if device name, address, status and services |
| 295 // (primary services of BLE device) are the same. However, in BLE tests, | 284 // (primary services of BLE device) are the same. However, in BLE tests, |
| 296 // we may simulate characteristic, descriptor and secondary GATT service | 285 // we may simulate characteristic, descriptor and secondary GATT service |
| 297 // after device has been initialized. | 286 // after device has been initialized. |
| 298 if (force_update_device_for_test_) | 287 if (force_update_device_for_test_) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size(); | 369 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size(); |
| 381 on_stop_discovery_callbacks_.clear(); | 370 on_stop_discovery_callbacks_.clear(); |
| 382 return; | 371 return; |
| 383 } | 372 } |
| 384 | 373 |
| 385 discovery_status_ = DISCOVERY_STOPPING; | 374 discovery_status_ = DISCOVERY_STOPPING; |
| 386 task_manager_->PostStopDiscoveryTask(); | 375 task_manager_->PostStopDiscoveryTask(); |
| 387 } | 376 } |
| 388 | 377 |
| 389 } // namespace device | 378 } // namespace device |
| OLD | NEW |