Skip to content

Commit a6558a1

Browse files
author
Jun Choi
committed
Stop advertisement once after Cable discovery
The current BlueZ adapter implementation for BLE advertisement does not unregister advertisements when BluetoothAdapter scoped referenece pointer is destructed. This cause BluetoothAdapter::advertisements_ queue to increase indeterminately as BluetoothAdapter is a Chrome wide singleton object. Manually unregister any ongoing advertisement once CableDiscovery is destructed. Bug: 846535 Change-Id: If7782132f99e580bfa5f6e9a0da2a116375e6b7c Reviewed-on: https://chromium-review.googlesource.com/1072929 Commit-Queue: Jun Choi <[email protected]> Reviewed-by: Tim Song <[email protected]> Reviewed-by: Balazs Engedy <[email protected]> Cr-Original-Commit-Position: refs/heads/master@{#563003}(cherry picked from commit 7698eb3) Reviewed-on: https://chromium-review.googlesource.com/1081181 Reviewed-by: Jun Choi <[email protected]> Cr-Commit-Position: refs/branch-heads/3440@{#68} Cr-Branched-From: 010ddcf-refs/heads/master@{#561733}
1 parent 1408f3c commit a6558a1

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

device/fido/fido_cable_discovery.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <utility>
99

1010
#include "base/bind.h"
11+
#include "base/bind_helpers.h"
1112
#include "base/callback_helpers.h"
1213
#include "base/strings/stringprintf.h"
1314
#include "base/threading/sequenced_task_runner_handle.h"
@@ -128,9 +129,11 @@ FidoCableDiscovery::FidoCableDiscovery(
128129
std::vector<CableDiscoveryData> discovery_data)
129130
: discovery_data_(std::move(discovery_data)), weak_factory_(this) {}
130131

131-
// Destruction of FidoCableDiscovery will unregister |advertisements_| on
132-
// best-effort basis.
133-
FidoCableDiscovery::~FidoCableDiscovery() = default;
132+
// This is a workaround for https://crbug.com/846522
133+
FidoCableDiscovery::~FidoCableDiscovery() {
134+
for (auto advertisement : advertisements_)
135+
advertisement.second->Unregister(base::DoNothing(), base::DoNothing());
136+
}
134137

135138
void FidoCableDiscovery::DeviceAdded(BluetoothAdapter* adapter,
136139
BluetoothDevice* device) {

device/fido/fido_cable_discovery.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class COMPONENT_EXPORT(DEVICE_FIDO) FidoCableDiscovery
5656
~FidoCableDiscovery() override;
5757

5858
private:
59+
FRIEND_TEST_ALL_PREFIXES(FidoCableDiscoveryTest,
60+
TestUnregisterAdvertisementUponDestruction);
61+
5962
// BluetoothAdapter::Observer:
6063
void DeviceAdded(BluetoothAdapter* adapter, BluetoothDevice* device) override;
6164
void DeviceChanged(BluetoothAdapter* adapter,

device/fido/fido_cable_discovery_unittest.cc

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include "base/stl_util.h"
1212
#include "build/build_config.h"
1313
#include "device/bluetooth/bluetooth_adapter_factory.h"
14+
#include "device/bluetooth/bluetooth_advertisement.h"
1415
#include "device/bluetooth/test/bluetooth_test.h"
1516
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
16-
#include "device/bluetooth/test/mock_bluetooth_advertisement.h"
1717
#include "device/fido/fido_ble_device.h"
1818
#include "device/fido/fido_ble_uuids.h"
1919
#include "device/fido/fido_parsing_utils.h"
@@ -112,6 +112,16 @@ MATCHER_P2(IsAdvertisementContent,
112112
return true;
113113
}
114114

115+
class CableMockBluetoothAdvertisement : public BluetoothAdvertisement {
116+
public:
117+
MOCK_METHOD2(Unregister,
118+
void(const SuccessCallback& success_callback,
119+
const ErrorCallback& error_callback));
120+
121+
private:
122+
~CableMockBluetoothAdvertisement() override = default;
123+
};
124+
115125
// Mock BLE adapter that abstracts out authenticator logic with the following
116126
// logic:
117127
// - Responds to BluetoothAdapter::RegisterAdvertisement() by always invoking
@@ -150,18 +160,23 @@ class CableMockAdapter : public MockBluetoothAdapter {
150160
void ExpectRegisterAdvertisementWithResponse(
151161
bool simulate_success,
152162
base::span<const uint8_t> expected_client_eid,
153-
base::StringPiece expected_uuid_formatted_client_eid) {
163+
base::StringPiece expected_uuid_formatted_client_eid,
164+
scoped_refptr<CableMockBluetoothAdvertisement> advertisement_ptr =
165+
nullptr) {
166+
if (!advertisement_ptr)
167+
advertisement_ptr =
168+
base::MakeRefCounted<CableMockBluetoothAdvertisement>();
169+
154170
EXPECT_CALL(*this,
155171
RegisterAdvertisement(
156172
IsAdvertisementContent(expected_client_eid,
157173
expected_uuid_formatted_client_eid),
158174
_, _))
159175
.WillOnce(::testing::WithArgs<1, 2>(
160-
[simulate_success](const auto& success_callback,
161-
const auto& failure_callback) {
176+
[simulate_success, advertisement_ptr](
177+
const auto& success_callback, const auto& failure_callback) {
162178
simulate_success
163-
? success_callback.Run(
164-
base::MakeRefCounted<MockBluetoothAdvertisement>())
179+
? success_callback.Run(advertisement_ptr)
165180
: failure_callback.Run(BluetoothAdvertisement::ErrorCode::
166181
INVALID_ADVERTISEMENT_ERROR_CODE);
167182
}));
@@ -338,4 +353,26 @@ TEST_F(FidoCableDiscoveryTest, TestDiscoveryWithAdvertisementFailures) {
338353
scoped_task_environment_.RunUntilIdle();
339354
}
340355

356+
TEST_F(FidoCableDiscoveryTest, TestUnregisterAdvertisementUponDestruction) {
357+
auto cable_discovery = CreateDiscovery();
358+
CableMockBluetoothAdvertisement* advertisement =
359+
new CableMockBluetoothAdvertisement();
360+
EXPECT_CALL(*advertisement, Unregister(_, _)).Times(1);
361+
362+
::testing::InSequence testing_sequence;
363+
auto mock_adapter =
364+
base::MakeRefCounted<::testing::NiceMock<CableMockAdapter>>();
365+
mock_adapter->ExpectSuccessCallbackToSetPowered();
366+
mock_adapter->ExpectRegisterAdvertisementWithResponse(
367+
true /* simulate_success */, kClientEid, kUuidFormattedClientEid,
368+
base::WrapRefCounted(advertisement));
369+
370+
BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter);
371+
cable_discovery->Start();
372+
scoped_task_environment_.RunUntilIdle();
373+
374+
EXPECT_EQ(1u, cable_discovery->advertisements_.size());
375+
cable_discovery.reset();
376+
}
377+
341378
} // namespace device

0 commit comments

Comments
 (0)