| OLD | NEW |
| 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 "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 17 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 18 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" |
| 18 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | 19 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" |
| 19 #include "components/proximity_auth/logging/logging.h" | 20 #include "components/proximity_auth/logging/logging.h" |
| 20 #include "device/bluetooth/bluetooth_adapter_factory.h" | 21 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 21 #include "device/bluetooth/bluetooth_device.h" | 22 #include "device/bluetooth/bluetooth_device.h" |
| 22 #include "device/bluetooth/bluetooth_discovery_session.h" | 23 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 23 #include "device/bluetooth/bluetooth_uuid.h" | 24 #include "device/bluetooth/bluetooth_uuid.h" |
| 24 | 25 |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 203 } |
| 203 | 204 |
| 204 // Note: It's possible to connect to the paired directly, so when using | 205 // Note: It's possible to connect to the paired directly, so when using |
| 205 // FIND_PAIRED_DEVICE strategy this is not necessary. However, the discovery | 206 // FIND_PAIRED_DEVICE strategy this is not necessary. However, the discovery |
| 206 // doesn't add a lot of latency, and the makes the code path for both | 207 // doesn't add a lot of latency, and the makes the code path for both |
| 207 // strategies more similar. | 208 // strategies more similar. |
| 208 StartDiscoverySession(); | 209 StartDiscoverySession(); |
| 209 } | 210 } |
| 210 | 211 |
| 211 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( | 212 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( |
| 212 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 213 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 213 PA_LOG(INFO) << "Discovery session started"; | 214 PA_LOG(INFO) << "Discovery session started"; |
| 214 discovery_session_ = std::move(discovery_session); | 215 discovery_session_ = std::move(discovery_session); |
| 215 } | 216 } |
| 216 | 217 |
| 217 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { | 218 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { |
| 218 PA_LOG(WARNING) << "Error starting discovery session"; | 219 PA_LOG(WARNING) << "Error starting discovery session"; |
| 219 } | 220 } |
| 220 | 221 |
| 221 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { | 222 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { |
| 222 DCHECK(adapter_); | 223 DCHECK(adapter_); |
| 223 if (discovery_session_ && discovery_session_->IsActive()) { | 224 if (discovery_session_ && discovery_session_->IsActive()) { |
| 224 PA_LOG(INFO) << "Discovery session already active"; | 225 PA_LOG(INFO) << "Discovery session already active"; |
| 225 return; | 226 return; |
| 226 } | 227 } |
| 227 | 228 |
| 228 // Discover only low energy (LE) devices with strong enough signal. | 229 // Discover only low energy (LE) devices with strong enough signal. |
| 229 scoped_ptr<BluetoothDiscoveryFilter> filter(new BluetoothDiscoveryFilter( | 230 std::unique_ptr<BluetoothDiscoveryFilter> filter(new BluetoothDiscoveryFilter( |
| 230 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | 231 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| 231 filter->SetRSSI(kMinDiscoveryRSSI); | 232 filter->SetRSSI(kMinDiscoveryRSSI); |
| 232 | 233 |
| 233 adapter_->StartDiscoverySessionWithFilter( | 234 adapter_->StartDiscoverySessionWithFilter( |
| 234 std::move(filter), | 235 std::move(filter), |
| 235 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, | 236 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, |
| 236 weak_ptr_factory_.GetWeakPtr()), | 237 weak_ptr_factory_.GetWeakPtr()), |
| 237 base::Bind( | 238 base::Bind( |
| 238 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, | 239 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, |
| 239 weak_ptr_factory_.GetWeakPtr())); | 240 weak_ptr_factory_.GetWeakPtr())); |
| 240 } | 241 } |
| 241 | 242 |
| 242 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { | 243 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { |
| 243 PA_LOG(INFO) << "Stopping discovery session"; | 244 PA_LOG(INFO) << "Stopping discovery session"; |
| 244 // Destroying the discovery session also stops it. | 245 // Destroying the discovery session also stops it. |
| 245 discovery_session_.reset(); | 246 discovery_session_.reset(); |
| 246 } | 247 } |
| 247 | 248 |
| 248 scoped_ptr<Connection> BluetoothLowEnergyConnectionFinder::CreateConnection( | 249 std::unique_ptr<Connection> |
| 250 BluetoothLowEnergyConnectionFinder::CreateConnection( |
| 249 const std::string& device_address) { | 251 const std::string& device_address) { |
| 250 DCHECK(remote_device_.bluetooth_address.empty() || | 252 DCHECK(remote_device_.bluetooth_address.empty() || |
| 251 remote_device_.bluetooth_address == device_address); | 253 remote_device_.bluetooth_address == device_address); |
| 252 remote_device_.bluetooth_address = device_address; | 254 remote_device_.bluetooth_address = device_address; |
| 253 return make_scoped_ptr(new BluetoothLowEnergyConnection( | 255 return base::WrapUnique(new BluetoothLowEnergyConnection( |
| 254 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_, | 256 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_, |
| 255 max_number_of_tries_)); | 257 max_number_of_tries_)); |
| 256 } | 258 } |
| 257 | 259 |
| 258 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( | 260 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( |
| 259 Connection* connection, | 261 Connection* connection, |
| 260 Connection::Status old_status, | 262 Connection::Status old_status, |
| 261 Connection::Status new_status) { | 263 Connection::Status new_status) { |
| 262 DCHECK_EQ(connection, connection_.get()); | 264 DCHECK_EQ(connection, connection_.get()); |
| 263 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " | 265 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 return device; | 310 return device; |
| 309 } | 311 } |
| 310 return nullptr; | 312 return nullptr; |
| 311 } | 313 } |
| 312 | 314 |
| 313 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { | 315 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { |
| 314 connection_callback_.Run(std::move(connection_)); | 316 connection_callback_.Run(std::move(connection_)); |
| 315 } | 317 } |
| 316 | 318 |
| 317 } // namespace proximity_auth | 319 } // namespace proximity_auth |
| OLD | NEW |