forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FastPair] Delete association when device is unpaired
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
Showing
6 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
70 changes: 70 additions & 0 deletions
70
ash/quick_pair/pairing/fast_pair/fast_pair_unpair_handler_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters