Skip to content

Commit

Permalink
[FastPair] Delete association when device is unpaired
Browse files Browse the repository at this point in the history
This changes adds the logic to observe for device unpair events and
delete the Fast Pair Footprints association if applicable.

Change-Id: I14a4ffd9d4acb207ab12069f938f03c8a82d5e21
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3223501
Commit-Queue: Shane Fitzpatrick <shanefitz@google.com>
Reviewed-by: Jon Mann <jonmann@chromium.org>
Cr-Commit-Position: refs/heads/main@{#931796}
  • Loading branch information
Shane Fitzpatrick authored and Chromium LUCI CQ committed Oct 15, 2021
1 parent 0cdc607 commit bf66b0a
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ash/quick_pair/pairing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ source_set("pairing") {
"fast_pair/fast_pair_key_pair.h",
"fast_pair/fast_pair_pairer.cc",
"fast_pair/fast_pair_pairer.h",
"fast_pair/fast_pair_unpair_handler.cc",
"fast_pair/fast_pair_unpair_handler.h",
"pairer_broker.h",
"pairer_broker_impl.cc",
"pairer_broker_impl.h",
Expand Down Expand Up @@ -69,6 +71,7 @@ source_set("unit_tests") {
"fast_pair/fast_pair_encryption_unittest.cc",
"fast_pair/fast_pair_gatt_service_client_unittest.cc",
"fast_pair/fast_pair_pairer_unittest.cc",
"fast_pair/fast_pair_unpair_handler_unittest.cc",
"retroactive_pairing_detector_unittest.cc",
]

Expand Down
42 changes: 42 additions & 0 deletions ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2021 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 "ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler.h"

#include "ash/quick_pair/common/logging.h"
#include "ash/quick_pair/repository/fast_pair_repository.h"
#include "base/memory/scoped_refptr.h"
#include "device/bluetooth/bluetooth_device.h"

namespace ash {
namespace quick_pair {

FastPairUnpairHandler::FastPairUnpairHandler(
scoped_refptr<device::BluetoothAdapter> adapter)
: adapter_(std::move(adapter)) {
observation_.Observe(adapter_.get());
}

FastPairUnpairHandler::~FastPairUnpairHandler() = default;

void FastPairUnpairHandler::DevicePairedChanged(
device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
bool new_paired_status) {
QP_LOG(VERBOSE) << __func__ << ": " << device->GetAddress()
<< " new_paired_status="
<< (new_paired_status ? "true" : "false");

if (new_paired_status)
return;

if (FastPairRepository::Get()->DeleteAssociatedDevice(device)) {
QP_LOG(INFO) << __func__ << ": Repository is processing the delete";
} else {
QP_LOG(VERBOSE) << __func__ << ": No device found by repository";
}
}

} // namespace quick_pair
} // namespace ash
44 changes: 44 additions & 0 deletions ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021 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.

#ifndef ASH_QUICK_PAIR_PAIRING_FAST_PAIR_FAST_PAIR_UNPAIR_HANDLER_H_
#define ASH_QUICK_PAIR_PAIRING_FAST_PAIR_FAST_PAIR_UNPAIR_HANDLER_H_

#include "base/memory/scoped_refptr.h"
#include "base/scoped_observation.h"
#include "device/bluetooth/bluetooth_adapter.h"

namespace device {
class BluetoothDevice;
} // namespace device

namespace ash {
namespace quick_pair {

// Class which observes bluetooth device pair state changes and invokes the
// Fast Pair delete flow on devices that have been unpaired.
class FastPairUnpairHandler : public device::BluetoothAdapter::Observer {
public:
explicit FastPairUnpairHandler(
scoped_refptr<device::BluetoothAdapter> adapter);
FastPairUnpairHandler(const FastPairUnpairHandler&) = delete;
FastPairUnpairHandler& operator=(const FastPairUnpairHandler&) = delete;
~FastPairUnpairHandler() override;

// BluetoothAdapter::Observer
void DevicePairedChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
bool new_paired_status) override;

private:
scoped_refptr<device::BluetoothAdapter> adapter_;
base::ScopedObservation<device::BluetoothAdapter,
device::BluetoothAdapter::Observer>
observation_{this};
};

} // namespace quick_pair
} // namespace ash

#endif // ASH_QUICK_PAIR_PAIRING_FAST_PAIR_FAST_PAIR_UNPAIR_HANDLER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021 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 "ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler.h"

#include <memory>

#include "ash/quick_pair/repository/mock_fast_pair_repository.h"
#include "base/memory/scoped_refptr.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace quick_pair {

class FakeBluetoothAdapter : public device::MockBluetoothAdapter {
public:
void NotifyPairedChanged(device::BluetoothDevice* device,
bool new_pair_state) {
device::BluetoothAdapter::NotifyDevicePairedChanged(device, new_pair_state);
}

private:
~FakeBluetoothAdapter() override = default;
};

class FastPairUnpairHandlerTest : public testing::Test {
public:
void SetUp() override {
adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);

device_ = std::make_unique<device::MockBluetoothDevice>(
adapter_.get(), 0, "test_name", "test_address", /*paired=*/false,
/*connected=*/false);

unpair_handler_ = std::make_unique<FastPairUnpairHandler>(adapter_);
mock_repository_ = std::make_unique<MockFastPairRepository>();
}

protected:
void NotifyPairChanged(bool new_pair_state) {
device_->SetPaired(!new_pair_state);
adapter_->NotifyPairedChanged(device_.get(), new_pair_state);
}

scoped_refptr<FakeBluetoothAdapter> adapter_;
std::unique_ptr<device::MockBluetoothDevice> device_;
std::unique_ptr<FastPairUnpairHandler> unpair_handler_;
std::unique_ptr<MockFastPairRepository> mock_repository_;
};

TEST_F(FastPairUnpairHandlerTest, DoesntDeleteIfDevicePaired) {
EXPECT_CALL(*(mock_repository_.get()), DeleteAssociatedDevice).Times(0);
NotifyPairChanged(/*new_pair_state=*/true);
}

TEST_F(FastPairUnpairHandlerTest, DeletesExpectedDevice) {
EXPECT_CALL(*(mock_repository_.get()), DeleteAssociatedDevice(device_.get()))
.Times(1);
NotifyPairChanged(/*new_pair_state=*/false);
}

} // namespace quick_pair
} // namespace ash
2 changes: 2 additions & 0 deletions ash/quick_pair/pairing/pairer_broker_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ash/quick_pair/common/pair_failure.h"
#include "ash/quick_pair/common/protocol.h"
#include "ash/quick_pair/pairing/fast_pair/fast_pair_pairer.h"
#include "ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/containers/contains.h"
Expand All @@ -29,6 +30,7 @@ PairerBrokerImpl::PairerBrokerImpl() {
void PairerBrokerImpl::OnGetAdapter(
scoped_refptr<device::BluetoothAdapter> adapter) {
adapter_ = adapter;
fast_pair_unpair_handler_ = std::make_unique<FastPairUnpairHandler>(adapter_);
}

PairerBrokerImpl::~PairerBrokerImpl() = default;
Expand Down
2 changes: 2 additions & 0 deletions ash/quick_pair/pairing/pairer_broker_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace quick_pair {

struct Device;
class FastPairPairer;
class FastPairUnpairHandler;

class PairerBrokerImpl final : public PairerBroker {
public:
Expand Down Expand Up @@ -59,6 +60,7 @@ class PairerBrokerImpl final : public PairerBroker {
base::flat_map<std::string, std::unique_ptr<FastPairPairer>>
fast_pair_pairers_;
scoped_refptr<device::BluetoothAdapter> adapter_;
std::unique_ptr<FastPairUnpairHandler> fast_pair_unpair_handler_;
base::ObserverList<Observer> observers_;
base::WeakPtrFactory<PairerBrokerImpl> weak_pointer_factory_{this};
};
Expand Down

0 comments on commit bf66b0a

Please sign in to comment.