Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc

Issue 1277483007: Implement finding BLE connections in chrome://proximity-auth. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refactor unit test Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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()) {
124 if (device_whitelist_->HasDeviceWithAddress(device->GetAddress()))
125 PA_LOG(INFO) << "Whitelisted device changed: " << device->GetAddress();
125 HandleDeviceUpdated(device); 126 HandleDeviceUpdated(device);
127 }
126 } 128 }
127 129
128 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( 130 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated(
129 BluetoothDevice* device) { 131 BluetoothDevice* device) {
130 // Ensuring only one call to |CreateConnection()| is made. A new |connection_| 132 // Ensuring only one call to |CreateConnection()| is made. A new |connection_|
131 // can be created only when the previous one disconnects, triggering a call to 133 // can be created only when the previous one disconnects, triggering a call to
132 // |OnConnectionStatusChanged|. 134 // |OnConnectionStatusChanged|.
133 if (connection_ || !device->IsPaired()) 135 if (connection_ || !device->IsPaired())
134 return; 136 return;
135 137
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 Connection::Status old_status, 261 Connection::Status old_status,
260 Connection::Status new_status) { 262 Connection::Status new_status) {
261 DCHECK_EQ(connection, connection_.get()); 263 DCHECK_EQ(connection, connection_.get());
262 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " 264 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> "
263 << new_status; 265 << new_status;
264 266
265 if (!connection_callback_.is_null() && connection_->IsConnected()) { 267 if (!connection_callback_.is_null() && connection_->IsConnected()) {
266 adapter_->RemoveObserver(this); 268 adapter_->RemoveObserver(this);
267 connection_->RemoveObserver(this); 269 connection_->RemoveObserver(this);
268 270
269 // Note: any observer of |connection_| added in |connection_callback_| will 271 // If we invoke the callback now, the callback function may install its own
270 // also receive this |OnConnectionStatusChanged| notification (IN_PROGRESS 272 // observer to |connection_|. Because we are in the ConnectionObserver
271 // -> CONNECTED). 273 // callstack, this new observer will receive this connection event.
272 connection_callback_.Run(connection_.Pass()); 274 // Therefore, we need to invoke the callback asynchronously.
273 connection_callback_.Reset(); 275 base::ThreadTaskRunnerHandle::Get()->PostTask(
276 FROM_HERE,
277 base::Bind(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync,
278 weak_ptr_factory_.GetWeakPtr()));
274 } else if (old_status == Connection::IN_PROGRESS) { 279 } else if (old_status == Connection::IN_PROGRESS) {
275 PA_LOG(WARNING) << "Connection failed. Retrying."; 280 PA_LOG(WARNING) << "Connection failed. Retrying.";
276 RestartDiscoverySessionWhenReady(); 281 RestartDiscoverySessionWhenReady();
277 } 282 }
278 } 283 }
279 284
280 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionWhenReady() { 285 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionWhenReady() {
281 PA_LOG(INFO) << "Trying to restart discovery."; 286 PA_LOG(INFO) << "Trying to restart discovery.";
282 287
283 // To restart scanning for devices, it's necessary to ensure that: 288 // To restart scanning for devices, it's necessary to ensure that:
(...skipping 24 matching lines...) Expand all
308 // This is a bug in the way device::BluetoothAdapter is storing the devices 313 // This is a bug in the way device::BluetoothAdapter is storing the devices
309 // (see crbug.com/497841). 314 // (see crbug.com/497841).
310 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); 315 std::vector<BluetoothDevice*> devices = adapter_->GetDevices();
311 for (const auto& device : devices) { 316 for (const auto& device : devices) {
312 if (device->GetAddress() == device_address) 317 if (device->GetAddress() == device_address)
313 return device; 318 return device;
314 } 319 }
315 return nullptr; 320 return nullptr;
316 } 321 }
317 322
323 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() {
324 connection_callback_.Run(connection_.Pass());
325 }
326
318 } // namespace proximity_auth 327 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698