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()); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, | 238 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, |
239 weak_ptr_factory_.GetWeakPtr())); | 239 weak_ptr_factory_.GetWeakPtr())); |
240 } | 240 } |
241 | 241 |
242 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { | 242 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { |
243 PA_LOG(INFO) << "Stopping discovery session"; | 243 PA_LOG(INFO) << "Stopping discovery session"; |
244 // Destroying the discovery session also stops it. | 244 // Destroying the discovery session also stops it. |
245 discovery_session_.reset(); | 245 discovery_session_.reset(); |
246 } | 246 } |
247 | 247 |
248 std::unique_ptr<Connection> | 248 std::unique_ptr<cryptauth::Connection> |
249 BluetoothLowEnergyConnectionFinder::CreateConnection( | 249 BluetoothLowEnergyConnectionFinder::CreateConnection( |
250 const std::string& device_address) { | 250 const std::string& device_address) { |
251 DCHECK(remote_device_.bluetooth_address.empty() || | 251 DCHECK(remote_device_.bluetooth_address.empty() || |
252 remote_device_.bluetooth_address == device_address); | 252 remote_device_.bluetooth_address == device_address); |
253 remote_device_.bluetooth_address = device_address; | 253 remote_device_.bluetooth_address = device_address; |
254 return base::MakeUnique<BluetoothLowEnergyConnection>( | 254 return base::MakeUnique<BluetoothLowEnergyConnection>( |
255 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_, | 255 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_, |
256 max_number_of_tries_); | 256 max_number_of_tries_); |
257 } | 257 } |
258 | 258 |
259 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( | 259 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( |
260 Connection* connection, | 260 cryptauth::Connection* connection, |
261 Connection::Status old_status, | 261 cryptauth::Connection::Status old_status, |
262 Connection::Status new_status) { | 262 cryptauth::Connection::Status new_status) { |
263 DCHECK_EQ(connection, connection_.get()); | 263 DCHECK_EQ(connection, connection_.get()); |
264 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " | 264 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " |
265 << new_status; | 265 << new_status; |
266 | 266 |
267 if (!connection_callback_.is_null() && connection_->IsConnected()) { | 267 if (!connection_callback_.is_null() && connection_->IsConnected()) { |
268 adapter_->RemoveObserver(this); | 268 adapter_->RemoveObserver(this); |
269 connection_->RemoveObserver(this); | 269 connection_->RemoveObserver(this); |
270 | 270 |
271 // If we invoke the callback now, the callback function may install its own | 271 // If we invoke the callback now, the callback function may install its own |
272 // observer to |connection_|. Because we are in the ConnectionObserver | 272 // observer to |connection_|. Because we are in the ConnectionObserver |
273 // callstack, this new observer will receive this connection event. | 273 // callstack, this new observer will receive this connection event. |
274 // Therefore, we need to invoke the callback or restart discovery | 274 // Therefore, we need to invoke the callback or restart discovery |
275 // asynchronously. | 275 // asynchronously. |
276 base::ThreadTaskRunnerHandle::Get()->PostTask( | 276 base::ThreadTaskRunnerHandle::Get()->PostTask( |
277 FROM_HERE, | 277 FROM_HERE, |
278 base::Bind(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync, | 278 base::Bind(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync, |
279 weak_ptr_factory_.GetWeakPtr())); | 279 weak_ptr_factory_.GetWeakPtr())); |
280 } else if (old_status == Connection::IN_PROGRESS) { | 280 } else if (old_status == cryptauth::Connection::IN_PROGRESS) { |
281 PA_LOG(WARNING) << "Connection failed. Retrying."; | 281 PA_LOG(WARNING) << "Connection failed. Retrying."; |
282 base::ThreadTaskRunnerHandle::Get()->PostTask( | 282 base::ThreadTaskRunnerHandle::Get()->PostTask( |
283 FROM_HERE, | 283 FROM_HERE, |
284 base::Bind( | 284 base::Bind( |
285 &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync, | 285 &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync, |
286 weak_ptr_factory_.GetWeakPtr())); | 286 weak_ptr_factory_.GetWeakPtr())); |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() { | 290 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() { |
(...skipping 18 matching lines...) Expand all Loading... |
309 return device; | 309 return device; |
310 } | 310 } |
311 return nullptr; | 311 return nullptr; |
312 } | 312 } |
313 | 313 |
314 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { | 314 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { |
315 connection_callback_.Run(std::move(connection_)); | 315 connection_callback_.Run(std::move(connection_)); |
316 } | 316 } |
317 | 317 |
318 } // namespace proximity_auth | 318 } // namespace proximity_auth |
OLD | NEW |