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

Unified Diff: components/proximity_auth/bluetooth_low_energy_connection_finder.cc

Issue 2844743002: Revert of [EasyUnlock] Update BluetoothLowEnergyConnectionFinder to look for EIDs. (Closed)
Patch Set: Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: components/proximity_auth/bluetooth_low_energy_connection_finder.cc
diff --git a/components/proximity_auth/bluetooth_low_energy_connection_finder.cc b/components/proximity_auth/bluetooth_low_energy_connection_finder.cc
deleted file mode 100644
index 0edf2a391d9590ea5ccb4c608e55c6ffa01766a5..0000000000000000000000000000000000000000
--- a/components/proximity_auth/bluetooth_low_energy_connection_finder.cc
+++ /dev/null
@@ -1,280 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "components/proximity_auth/bluetooth_low_energy_connection_finder.h"
-
-#include <memory>
-#include <string>
-#include <utility>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/location.h"
-#include "base/logging.h"
-#include "base/memory/ptr_util.h"
-#include "base/strings/utf_string_conversions.h"
-#include "base/threading/thread_task_runner_handle.h"
-#include "components/cryptauth/ble/bluetooth_low_energy_weave_client_connection.h"
-#include "components/cryptauth/bluetooth_throttler.h"
-#include "components/cryptauth/connection.h"
-#include "components/proximity_auth/logging/logging.h"
-#include "device/bluetooth/bluetooth_adapter_factory.h"
-#include "device/bluetooth/bluetooth_common.h"
-#include "device/bluetooth/bluetooth_device.h"
-#include "device/bluetooth/bluetooth_discovery_session.h"
-#include "device/bluetooth/bluetooth_uuid.h"
-
-using device::BluetoothAdapter;
-using device::BluetoothDevice;
-using device::BluetoothGattConnection;
-using device::BluetoothDiscoveryFilter;
-
-namespace proximity_auth {
-namespace {
-const char kAdvertisementUUID[] = "0000fe50-0000-1000-8000-00805f9b34fb";
-const char kBLEGattServiceUUID[] = "b3b7e28e-a000-3e17-bd86-6e97b9e28c11";
-const int kRestartDiscoveryOnErrorDelaySeconds = 2;
-} // namespace
-
-BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder(
- const cryptauth::RemoteDevice remote_device,
- const std::vector<cryptauth::BeaconSeed>& beacon_seeds,
- cryptauth::BluetoothThrottler* bluetooth_throttler)
- : BluetoothLowEnergyConnectionFinder(
- remote_device,
- beacon_seeds,
- base::MakeUnique<cryptauth::BackgroundEidGenerator>(),
- bluetooth_throttler) {}
-
-BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder(
- const cryptauth::RemoteDevice remote_device,
- const std::vector<cryptauth::BeaconSeed>& beacon_seeds,
- std::unique_ptr<cryptauth::BackgroundEidGenerator> eid_generator,
- cryptauth::BluetoothThrottler* bluetooth_throttler)
- : remote_device_(remote_device),
- beacon_seeds_(beacon_seeds),
- eid_generator_(std::move(eid_generator)),
- bluetooth_throttler_(bluetooth_throttler),
- weak_ptr_factory_(this) {}
-
-BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() {
- if (discovery_session_) {
- StopDiscoverySession();
- }
-
- if (connection_) {
- connection_->RemoveObserver(this);
- connection_.reset();
- }
-
- if (adapter_) {
- adapter_->RemoveObserver(this);
- adapter_ = NULL;
- }
-}
-
-void BluetoothLowEnergyConnectionFinder::Find(
- const cryptauth::ConnectionFinder::ConnectionCallback&
- connection_callback) {
- if (!device::BluetoothAdapterFactory::IsBluetoothSupported()) {
- PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting.";
- return;
- }
- PA_LOG(INFO) << "Finding connection";
-
- connection_callback_ = connection_callback;
-
- device::BluetoothAdapterFactory::GetAdapter(
- base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized,
- weak_ptr_factory_.GetWeakPtr()));
-}
-
-// It's not necessary to observe |AdapterPresentChanged| too. When |adapter_| is
-// present, but not powered, it's not possible to scan for new devices.
-void BluetoothLowEnergyConnectionFinder::AdapterPoweredChanged(
- BluetoothAdapter* adapter,
- bool powered) {
- DCHECK_EQ(adapter_.get(), adapter);
- PA_LOG(INFO) << "Adapter powered: " << powered;
-
- // Important: do not rely on |adapter->IsDiscoverying()| to verify if there is
- // an active discovery session. We need to create our own with an specific
- // filter.
- if (powered && (!discovery_session_ || !discovery_session_->IsActive()))
- StartDiscoverySession();
-}
-
-void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter,
- BluetoothDevice* device) {
- DCHECK_EQ(adapter_.get(), adapter);
- DCHECK(device);
-
- // Note: Only consider |device| when it was actually added/updated during a
- // scanning, otherwise the device is stale and the GATT connection will fail.
- // For instance, when |adapter_| change status from unpowered to powered,
- // |DeviceAdded| is called for each paired |device|.
- if (adapter_->IsPowered() && discovery_session_ &&
- discovery_session_->IsActive())
- HandleDeviceUpdated(device);
-}
-
-void BluetoothLowEnergyConnectionFinder::DeviceChanged(
- BluetoothAdapter* adapter,
- BluetoothDevice* device) {
- DCHECK_EQ(adapter_.get(), adapter);
- DCHECK(device);
-
- // Note: Only consider |device| when it was actually added/updated during a
- // scanning, otherwise the device is stale and the GATT connection will fail.
- // For instance, when |adapter_| change status from unpowered to powered,
- // |DeviceAdded| is called for each paired |device|.
- if (adapter_->IsPowered() && discovery_session_ &&
- discovery_session_->IsActive())
- HandleDeviceUpdated(device);
-}
-
-void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated(
- BluetoothDevice* device) {
- // Ensuring only one call to |CreateConnection()| is made. A new |connection_|
- // can be created only when the previous one disconnects, triggering a call to
- // |OnConnectionStatusChanged|.
- if (connection_)
- return;
-
- if (IsRightDevice(device)) {
- PA_LOG(INFO) << "Connecting to device " << device->GetAddress();
- connection_ = CreateConnection(device->GetAddress());
- connection_->AddObserver(this);
- connection_->Connect();
-
- StopDiscoverySession();
- }
-}
-
-bool BluetoothLowEnergyConnectionFinder::IsRightDevice(
- BluetoothDevice* device) {
- if (!device)
- return false;
-
- device::BluetoothUUID advertisement_uuid(kAdvertisementUUID);
- const std::vector<uint8_t>* service_data =
- device->GetServiceDataForUUID(advertisement_uuid);
- if (!service_data)
- return false;
-
- std::string service_data_string(service_data->begin(), service_data->end());
- std::vector<std::string> nearest_eids =
- eid_generator_->GenerateNearestEids(beacon_seeds_);
- for (const std::string& eid : nearest_eids) {
- if (eid == service_data_string) {
- PA_LOG(INFO) << "Found a matching EID: " << eid;
- return true;
- }
- }
- return false;
-}
-
-void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized(
- scoped_refptr<BluetoothAdapter> adapter) {
- PA_LOG(INFO) << "Adapter ready";
- adapter_ = adapter;
- adapter_->AddObserver(this);
- StartDiscoverySession();
-}
-
-void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted(
- std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) {
- PA_LOG(INFO) << "Discovery session started";
- discovery_session_ = std::move(discovery_session);
-}
-
-void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() {
- PA_LOG(WARNING) << "Error starting discovery session, restarting in "
- << kRestartDiscoveryOnErrorDelaySeconds << " seconds.";
- base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
- FROM_HERE,
- base::Bind(
- &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync,
- weak_ptr_factory_.GetWeakPtr()),
- base::TimeDelta::FromSeconds(kRestartDiscoveryOnErrorDelaySeconds));
-}
-
-void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() {
- DCHECK(adapter_);
- if (discovery_session_ && discovery_session_->IsActive()) {
- PA_LOG(INFO) << "Discovery session already active";
- return;
- }
-
- // Discover only low energy (LE) devices.
- std::unique_ptr<BluetoothDiscoveryFilter> filter(
- new BluetoothDiscoveryFilter(device::BLUETOOTH_TRANSPORT_LE));
-
- adapter_->StartDiscoverySessionWithFilter(
- std::move(filter),
- base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted,
- weak_ptr_factory_.GetWeakPtr()),
- base::Bind(
- &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError,
- weak_ptr_factory_.GetWeakPtr()));
-}
-
-void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() {
- PA_LOG(INFO) << "Stopping discovery session";
- // Destroying the discovery session also stops it.
- discovery_session_.reset();
-}
-
-std::unique_ptr<cryptauth::Connection>
-BluetoothLowEnergyConnectionFinder::CreateConnection(
- const std::string& device_address) {
- return cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::Factory::
- NewInstance(remote_device_, device_address, adapter_,
- device::BluetoothUUID(kBLEGattServiceUUID),
- bluetooth_throttler_);
-}
-
-void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged(
- cryptauth::Connection* connection,
- cryptauth::Connection::Status old_status,
- cryptauth::Connection::Status new_status) {
- DCHECK_EQ(connection, connection_.get());
- PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> "
- << new_status;
-
- if (!connection_callback_.is_null() && connection_->IsConnected()) {
- adapter_->RemoveObserver(this);
- connection_->RemoveObserver(this);
-
- // If we invoke the callback now, the callback function may install its own
- // observer to |connection_|. Because we are in the ConnectionObserver
- // callstack, this new observer will receive this connection event.
- // Therefore, we need to invoke the callback or restart discovery
- // asynchronously.
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE,
- base::Bind(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync,
- weak_ptr_factory_.GetWeakPtr()));
- } else if (old_status == cryptauth::Connection::IN_PROGRESS) {
- PA_LOG(WARNING) << "Connection failed. Retrying.";
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE,
- base::Bind(
- &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync,
- weak_ptr_factory_.GetWeakPtr()));
- }
-}
-
-void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() {
- PA_LOG(INFO) << "Restarting discovery session.";
- connection_.reset();
- if (!discovery_session_ || !discovery_session_->IsActive())
- StartDiscoverySession();
-}
-
-void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() {
- connection_callback_.Run(std::move(connection_));
-}
-
-} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698