Chromium Code Reviews| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 if (adapter_->IsPowered() && discovery_session_ && | 107 if (adapter_->IsPowered() && discovery_session_ && |
| 108 discovery_session_->IsActive()) | 108 discovery_session_->IsActive()) |
| 109 HandleDeviceUpdated(device); | 109 HandleDeviceUpdated(device); |
| 110 } | 110 } |
| 111 | 111 |
| 112 void BluetoothLowEnergyConnectionFinder::DeviceChanged( | 112 void BluetoothLowEnergyConnectionFinder::DeviceChanged( |
| 113 BluetoothAdapter* adapter, | 113 BluetoothAdapter* adapter, |
| 114 BluetoothDevice* device) { | 114 BluetoothDevice* device) { |
| 115 DCHECK_EQ(adapter_.get(), adapter); | 115 DCHECK_EQ(adapter_.get(), adapter); |
| 116 DCHECK(device); | 116 DCHECK(device); |
| 117 PA_LOG(INFO) << "Device changed: " << device->GetAddress(); | |
| 118 | 117 |
|
sacomoto
2015/08/10 19:40:28
nit: move it inside the if instead of removing it.
Tim Song
2015/08/10 22:17:55
Done.
| |
| 119 // Note: Only consider |device| when it was actually added/updated during a | 118 // Note: Only consider |device| when it was actually added/updated during a |
| 120 // scanning, otherwise the device is stale and the GATT connection will fail. | 119 // scanning, otherwise the device is stale and the GATT connection will fail. |
| 121 // For instance, when |adapter_| change status from unpowered to powered, | 120 // For instance, when |adapter_| change status from unpowered to powered, |
| 122 // |DeviceAdded| is called for each paired |device|. | 121 // |DeviceAdded| is called for each paired |device|. |
| 123 if (adapter_->IsPowered() && discovery_session_ && | 122 if (adapter_->IsPowered() && discovery_session_ && |
| 124 discovery_session_->IsActive()) | 123 discovery_session_->IsActive()) |
| 125 HandleDeviceUpdated(device); | 124 HandleDeviceUpdated(device); |
| 126 } | 125 } |
| 127 | 126 |
| 128 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( | 127 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 Connection::Status old_status, | 258 Connection::Status old_status, |
| 260 Connection::Status new_status) { | 259 Connection::Status new_status) { |
| 261 DCHECK_EQ(connection, connection_.get()); | 260 DCHECK_EQ(connection, connection_.get()); |
| 262 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " | 261 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " |
| 263 << new_status; | 262 << new_status; |
| 264 | 263 |
| 265 if (!connection_callback_.is_null() && connection_->IsConnected()) { | 264 if (!connection_callback_.is_null() && connection_->IsConnected()) { |
| 266 adapter_->RemoveObserver(this); | 265 adapter_->RemoveObserver(this); |
| 267 connection_->RemoveObserver(this); | 266 connection_->RemoveObserver(this); |
| 268 | 267 |
| 269 // Note: any observer of |connection_| added in |connection_callback_| will | 268 // If we invoke the callback now, the callback function may install its own |
| 270 // also receive this |OnConnectionStatusChanged| notification (IN_PROGRESS | 269 // observer to |connection_|. Because we are in the ConnectionObserver |
| 271 // -> CONNECTED). | 270 // callstack, this new observer will receive this connection event. |
| 272 connection_callback_.Run(connection_.Pass()); | 271 // Therefore, we need to invoke the callback asynchronously. |
| 273 connection_callback_.Reset(); | 272 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 273 FROM_HERE, | |
| 274 base::Bind(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync, | |
| 275 weak_ptr_factory_.GetWeakPtr())); | |
| 274 } else if (old_status == Connection::IN_PROGRESS) { | 276 } else if (old_status == Connection::IN_PROGRESS) { |
| 275 PA_LOG(WARNING) << "Connection failed. Retrying."; | 277 PA_LOG(WARNING) << "Connection failed. Retrying."; |
| 276 RestartDiscoverySessionWhenReady(); | 278 RestartDiscoverySessionWhenReady(); |
| 277 } | 279 } |
| 278 } | 280 } |
| 279 | 281 |
| 280 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionWhenReady() { | 282 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionWhenReady() { |
| 281 PA_LOG(INFO) << "Trying to restart discovery."; | 283 PA_LOG(INFO) << "Trying to restart discovery."; |
| 282 | 284 |
| 283 // To restart scanning for devices, it's necessary to ensure that: | 285 // To restart scanning for devices, it's necessary to ensure that: |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 308 // This is a bug in the way device::BluetoothAdapter is storing the devices | 310 // This is a bug in the way device::BluetoothAdapter is storing the devices |
| 309 // (see crbug.com/497841). | 311 // (see crbug.com/497841). |
| 310 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); | 312 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); |
| 311 for (const auto& device : devices) { | 313 for (const auto& device : devices) { |
| 312 if (device->GetAddress() == device_address) | 314 if (device->GetAddress() == device_address) |
| 313 return device; | 315 return device; |
| 314 } | 316 } |
| 315 return nullptr; | 317 return nullptr; |
| 316 } | 318 } |
| 317 | 319 |
| 320 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { | |
| 321 connection_callback_.Run(connection_.Pass()); | |
| 322 } | |
| 323 | |
| 318 } // namespace proximity_auth | 324 } // namespace proximity_auth |
| OLD | NEW |