| 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 <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "components/cryptauth/bluetooth_throttler.h" |
| 19 #include "components/cryptauth/connection.h" |
| 18 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 20 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" |
| 19 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | 21 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" |
| 20 #include "components/proximity_auth/logging/logging.h" | 22 #include "components/proximity_auth/logging/logging.h" |
| 21 #include "device/bluetooth/bluetooth_adapter_factory.h" | 23 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 22 #include "device/bluetooth/bluetooth_common.h" | 24 #include "device/bluetooth/bluetooth_common.h" |
| 23 #include "device/bluetooth/bluetooth_device.h" | 25 #include "device/bluetooth/bluetooth_device.h" |
| 24 #include "device/bluetooth/bluetooth_discovery_session.h" | 26 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 25 #include "device/bluetooth/bluetooth_uuid.h" | 27 #include "device/bluetooth/bluetooth_uuid.h" |
| 26 | 28 |
| 27 using device::BluetoothAdapter; | 29 using device::BluetoothAdapter; |
| 28 using device::BluetoothDevice; | 30 using device::BluetoothDevice; |
| 29 using device::BluetoothGattConnection; | 31 using device::BluetoothGattConnection; |
| 30 using device::BluetoothDiscoveryFilter; | 32 using device::BluetoothDiscoveryFilter; |
| 31 | 33 |
| 32 namespace proximity_auth { | 34 namespace proximity_auth { |
| 33 namespace { | 35 namespace { |
| 34 const int kMinDiscoveryRSSI = -90; | 36 const int kMinDiscoveryRSSI = -90; |
| 35 } // namespace | 37 } // namespace |
| 36 | 38 |
| 37 class BluetoothThrottler; | |
| 38 | |
| 39 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( | 39 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
| 40 const cryptauth::RemoteDevice remote_device, | 40 const cryptauth::RemoteDevice remote_device, |
| 41 const std::string& remote_service_uuid, | 41 const std::string& remote_service_uuid, |
| 42 FinderStrategy finder_strategy, | 42 FinderStrategy finder_strategy, |
| 43 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, | 43 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, |
| 44 BluetoothThrottler* bluetooth_throttler, | 44 cryptauth::BluetoothThrottler* bluetooth_throttler, |
| 45 int max_number_of_tries) | 45 int max_number_of_tries) |
| 46 : remote_device_(remote_device), | 46 : remote_device_(remote_device), |
| 47 remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), | 47 remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), |
| 48 finder_strategy_(finder_strategy), | 48 finder_strategy_(finder_strategy), |
| 49 device_whitelist_(device_whitelist), | 49 device_whitelist_(device_whitelist), |
| 50 bluetooth_throttler_(bluetooth_throttler), | 50 bluetooth_throttler_(bluetooth_throttler), |
| 51 max_number_of_tries_(max_number_of_tries), | 51 max_number_of_tries_(max_number_of_tries), |
| 52 weak_ptr_factory_(this) { | 52 weak_ptr_factory_(this) { |
| 53 DCHECK(finder_strategy_ == FIND_ANY_DEVICE || | 53 DCHECK(finder_strategy_ == FIND_ANY_DEVICE || |
| 54 !remote_device.bluetooth_address.empty()); | 54 !remote_device.bluetooth_address.empty()); |
| 55 } | 55 } |
| 56 | 56 |
| 57 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { | 57 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
| 58 if (discovery_session_) { | 58 if (discovery_session_) { |
| 59 StopDiscoverySession(); | 59 StopDiscoverySession(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 if (connection_) { | 62 if (connection_) { |
| 63 connection_->RemoveObserver(this); | 63 connection_->RemoveObserver(this); |
| 64 connection_.reset(); | 64 connection_.reset(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 if (adapter_) { | 67 if (adapter_) { |
| 68 adapter_->RemoveObserver(this); | 68 adapter_->RemoveObserver(this); |
| 69 adapter_ = NULL; | 69 adapter_ = NULL; |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 void BluetoothLowEnergyConnectionFinder::Find( | 73 void BluetoothLowEnergyConnectionFinder::Find( |
| 74 const ConnectionCallback& connection_callback) { | 74 const cryptauth::ConnectionFinder::ConnectionCallback& |
| 75 connection_callback) { |
| 75 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 76 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 76 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting."; | 77 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting."; |
| 77 return; | 78 return; |
| 78 } | 79 } |
| 79 PA_LOG(INFO) << "Finding connection"; | 80 PA_LOG(INFO) << "Finding connection"; |
| 80 | 81 |
| 81 connection_callback_ = connection_callback; | 82 connection_callback_ = connection_callback; |
| 82 | 83 |
| 83 device::BluetoothAdapterFactory::GetAdapter( | 84 device::BluetoothAdapterFactory::GetAdapter( |
| 84 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, | 85 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 std::unique_ptr<Connection> | 249 std::unique_ptr<cryptauth::Connection> |
| 249 BluetoothLowEnergyConnectionFinder::CreateConnection( | 250 BluetoothLowEnergyConnectionFinder::CreateConnection( |
| 250 const std::string& device_address) { | 251 const std::string& device_address) { |
| 251 DCHECK(remote_device_.bluetooth_address.empty() || | 252 DCHECK(remote_device_.bluetooth_address.empty() || |
| 252 remote_device_.bluetooth_address == device_address); | 253 remote_device_.bluetooth_address == device_address); |
| 253 remote_device_.bluetooth_address = device_address; | 254 remote_device_.bluetooth_address = device_address; |
| 254 return base::MakeUnique<BluetoothLowEnergyConnection>( | 255 return base::MakeUnique<BluetoothLowEnergyConnection>( |
| 255 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_, | 256 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_, |
| 256 max_number_of_tries_); | 257 max_number_of_tries_); |
| 257 } | 258 } |
| 258 | 259 |
| 259 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( | 260 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( |
| 260 Connection* connection, | 261 cryptauth::Connection* connection, |
| 261 Connection::Status old_status, | 262 cryptauth::Connection::Status old_status, |
| 262 Connection::Status new_status) { | 263 cryptauth::Connection::Status new_status) { |
| 263 DCHECK_EQ(connection, connection_.get()); | 264 DCHECK_EQ(connection, connection_.get()); |
| 264 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " | 265 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " |
| 265 << new_status; | 266 << new_status; |
| 266 | 267 |
| 267 if (!connection_callback_.is_null() && connection_->IsConnected()) { | 268 if (!connection_callback_.is_null() && connection_->IsConnected()) { |
| 268 adapter_->RemoveObserver(this); | 269 adapter_->RemoveObserver(this); |
| 269 connection_->RemoveObserver(this); | 270 connection_->RemoveObserver(this); |
| 270 | 271 |
| 271 // If we invoke the callback now, the callback function may install its own | 272 // If we invoke the callback now, the callback function may install its own |
| 272 // observer to |connection_|. Because we are in the ConnectionObserver | 273 // observer to |connection_|. Because we are in the ConnectionObserver |
| 273 // callstack, this new observer will receive this connection event. | 274 // callstack, this new observer will receive this connection event. |
| 274 // Therefore, we need to invoke the callback or restart discovery | 275 // Therefore, we need to invoke the callback or restart discovery |
| 275 // asynchronously. | 276 // asynchronously. |
| 276 base::ThreadTaskRunnerHandle::Get()->PostTask( | 277 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 277 FROM_HERE, | 278 FROM_HERE, |
| 278 base::Bind(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync, | 279 base::Bind(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync, |
| 279 weak_ptr_factory_.GetWeakPtr())); | 280 weak_ptr_factory_.GetWeakPtr())); |
| 280 } else if (old_status == Connection::IN_PROGRESS) { | 281 } else if (old_status == cryptauth::Connection::IN_PROGRESS) { |
| 281 PA_LOG(WARNING) << "Connection failed. Retrying."; | 282 PA_LOG(WARNING) << "Connection failed. Retrying."; |
| 282 base::ThreadTaskRunnerHandle::Get()->PostTask( | 283 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 283 FROM_HERE, | 284 FROM_HERE, |
| 284 base::Bind( | 285 base::Bind( |
| 285 &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync, | 286 &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync, |
| 286 weak_ptr_factory_.GetWeakPtr())); | 287 weak_ptr_factory_.GetWeakPtr())); |
| 287 } | 288 } |
| 288 } | 289 } |
| 289 | 290 |
| 290 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() { | 291 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 309 return device; | 310 return device; |
| 310 } | 311 } |
| 311 return nullptr; | 312 return nullptr; |
| 312 } | 313 } |
| 313 | 314 |
| 314 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { | 315 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { |
| 315 connection_callback_.Run(std::move(connection_)); | 316 connection_callback_.Run(std::move(connection_)); |
| 316 } | 317 } |
| 317 | 318 |
| 318 } // namespace proximity_auth | 319 } // namespace proximity_auth |
| OLD | NEW |