Skip to content

Commit

Permalink
[Fast Pair] Create abstract handshake lookup
Browse files Browse the repository at this point in the history
Refactor the handshake lookup class to be a base class and an
implementation class.

Test: Manually tested on DUT and updated unittests
Change-Id: Ia3a55aab2f5cdf4bc2c5144a5975175e46f78ec5
Bug: b/265853116
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4706611
Reviewed-by: Daniel Classon <dclasson@google.com>
Reviewed-by: Jack Shira <jackshira@google.com>
Commit-Queue: Alex Kingsborough <akingsb@google.com>
Cr-Commit-Position: refs/heads/main@{#1188169}
  • Loading branch information
Alex Kingsborough authored and Chromium LUCI CQ committed Aug 25, 2023
1 parent ff07eb8 commit acecefc
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 95 deletions.
2 changes: 2 additions & 0 deletions ash/quick_pair/fast_pair_handshake/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ source_set("fast_pair_handshake") {
"fast_pair_handshake_impl_new.h",
"fast_pair_handshake_lookup.cc",
"fast_pair_handshake_lookup.h",
"fast_pair_handshake_lookup_impl.cc",
"fast_pair_handshake_lookup_impl.h",
"fast_pair_key_pair.cc",
"fast_pair_key_pair.h",
]
Expand Down
76 changes: 5 additions & 71 deletions ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup.cc
Original file line number Diff line number Diff line change
@@ -1,89 +1,23 @@
// Copyright 2021 The Chromium Authors
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup.h"

#include <memory>

#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_impl.h"
#include "base/functional/callback.h"
#include "base/memory/singleton.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup_impl.h"

namespace ash {
namespace quick_pair {

// Create function override which can be set by tests.
absl::optional<FastPairHandshakeLookup::CreateFunction> g_test_create_function =
absl::nullopt;

// static
FastPairHandshakeLookup* FastPairHandshakeLookup::GetInstance() {
return base::Singleton<FastPairHandshakeLookup>::get();
return FastPairHandshakeLookupImpl::GetImplInstance();
}

// static
void FastPairHandshakeLookup::SetCreateFunctionForTesting(
CreateFunction create_function) {
g_test_create_function = std::move(create_function);
}

FastPairHandshakeLookup::FastPairHandshakeLookup() = default;

FastPairHandshakeLookup::~FastPairHandshakeLookup() = default;

FastPairHandshake* FastPairHandshakeLookup::Get(scoped_refptr<Device> device) {
auto it = fast_pair_handshakes_.find(device);
return it != fast_pair_handshakes_.end() ? it->second.get() : nullptr;
}

FastPairHandshake* FastPairHandshakeLookup::Get(const std::string& address) {
for (const auto& pair : fast_pair_handshakes_) {
if (pair.first->classic_address() == address ||
pair.first->ble_address() == address) {
return pair.second.get();
}
}

return nullptr;
}

bool FastPairHandshakeLookup::Erase(scoped_refptr<Device> device) {
return fast_pair_handshakes_.erase(device) == 1;
}

bool FastPairHandshakeLookup::Erase(const std::string& address) {
for (const auto& pair : fast_pair_handshakes_) {
if (pair.first->classic_address() == address ||
pair.first->ble_address() == address) {
fast_pair_handshakes_.erase(pair);
return true;
}
}

return false;
}

void FastPairHandshakeLookup::Clear() {
fast_pair_handshakes_.clear();
FastPairHandshakeLookupImpl::SetImplCreateFunctionForTesting(
std::move(create_function));
}

FastPairHandshake* FastPairHandshakeLookup::Create(
scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete) {
auto it = fast_pair_handshakes_.emplace(
device, g_test_create_function.has_value()
? g_test_create_function->Run(device, std::move(on_complete))
: std::make_unique<FastPairHandshakeImpl>(
std::move(adapter), device, std::move(on_complete)));

DCHECK(it.second) << "An existing item shouldn't exist.";

return it.first->second.get();
}

} // namespace quick_pair
} // namespace ash
37 changes: 13 additions & 24 deletions ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include <memory>

#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/common/pair_failure.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake.h"
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
Expand All @@ -22,9 +24,6 @@ class BluetoothAdapter;
namespace ash {
namespace quick_pair {

class Device;
class FastPairHandshake;

// This class creates, deletes and exposes FastPairHandshake instances.
class FastPairHandshakeLookup {
public:
Expand All @@ -38,41 +37,31 @@ class FastPairHandshakeLookup {
OnCompleteCallback callback)>;

static FastPairHandshakeLookup* GetInstance();
static void SetCreateFunctionForTesting(CreateFunction create_function);

FastPairHandshakeLookup(const FastPairHandshakeLookup&) = delete;
FastPairHandshakeLookup& operator=(const FastPairHandshakeLookup&) = delete;
// TODO(b/265853116): Move this function to the impl as it will not be used
// in the refactored code.
static void SetCreateFunctionForTesting(CreateFunction create_function);

// Get an existing instance for |device|.
FastPairHandshake* Get(scoped_refptr<Device> device);
virtual FastPairHandshake* Get(scoped_refptr<Device> device) = 0;

// Get an existing instance for |address|.
FastPairHandshake* Get(const std::string& address);
virtual FastPairHandshake* Get(const std::string& address) = 0;

// Erases the FastPairHandshake instance for |device| if exists.
bool Erase(scoped_refptr<Device> device);
virtual bool Erase(scoped_refptr<Device> device) = 0;

// Erases the FastPairHandshake instance for |address| if exists.
bool Erase(const std::string& address);
virtual bool Erase(const std::string& address) = 0;

// Deletes all existing FastPairHandshake instances.
void Clear();
virtual void Clear() = 0;

// Creates and returns a new instance for |device| if no instance already
// exists. Returns the existing instance if there is one.
FastPairHandshake* Create(scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete);

protected:
FastPairHandshakeLookup();
virtual ~FastPairHandshakeLookup();

private:
friend struct base::DefaultSingletonTraits<FastPairHandshakeLookup>;

base::flat_map<scoped_refptr<Device>, std::unique_ptr<FastPairHandshake>>
fast_pair_handshakes_;
virtual void Create(scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete) = 0;
};

} // namespace quick_pair
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup_impl.h"

#include <memory>

#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_impl.h"
#include "base/functional/callback.h"
#include "base/memory/singleton.h"
#include "device/bluetooth/bluetooth_adapter.h"

namespace ash {
namespace quick_pair {

// Create function override which can be set by tests.
absl::optional<FastPairHandshakeLookup::CreateFunction> g_test_create_function =
absl::nullopt;

// static
FastPairHandshakeLookupImpl* FastPairHandshakeLookupImpl::GetImplInstance() {
return base::Singleton<FastPairHandshakeLookupImpl>::get();
}

// static
void FastPairHandshakeLookupImpl::SetImplCreateFunctionForTesting(
CreateFunction create_function) {
g_test_create_function = std::move(create_function);
}

FastPairHandshakeLookupImpl::FastPairHandshakeLookupImpl() {}
FastPairHandshakeLookupImpl::~FastPairHandshakeLookupImpl() {}

FastPairHandshake* FastPairHandshakeLookupImpl::Get(
scoped_refptr<Device> device) {
auto it = fast_pair_handshakes_.find(device);
return it != fast_pair_handshakes_.end() ? it->second.get() : nullptr;
}

FastPairHandshake* FastPairHandshakeLookupImpl::Get(
const std::string& address) {
for (const auto& pair : fast_pair_handshakes_) {
if (pair.first->classic_address() == address ||
pair.first->ble_address() == address) {
return pair.second.get();
}
}

return nullptr;
}

bool FastPairHandshakeLookupImpl::Erase(scoped_refptr<Device> device) {
return fast_pair_handshakes_.erase(device) == 1;
}

bool FastPairHandshakeLookupImpl::Erase(const std::string& address) {
for (const auto& pair : fast_pair_handshakes_) {
if (pair.first->classic_address() == address ||
pair.first->ble_address() == address) {
fast_pair_handshakes_.erase(pair);
return true;
}
}

return false;
}

void FastPairHandshakeLookupImpl::Clear() {
fast_pair_handshakes_.clear();
}

void FastPairHandshakeLookupImpl::Create(
scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete) {
auto it = fast_pair_handshakes_.emplace(
device, g_test_create_function.has_value()
? g_test_create_function->Run(device, std::move(on_complete))
: std::make_unique<FastPairHandshakeImpl>(
std::move(adapter), device, std::move(on_complete)));

DCHECK(it.second) << "An existing item shouldn't exist.";
}

} // namespace quick_pair
} // namespace ash
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_LOOKUP_IMPL_H_
#define ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_LOOKUP_IMPL_H_

#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup.h"

namespace ash {
namespace quick_pair {

// This class creates, deletes and exposes FastPairHandshake instances.
class FastPairHandshakeLookupImpl : public FastPairHandshakeLookup {
public:
static FastPairHandshakeLookupImpl* GetImplInstance();

static void SetImplCreateFunctionForTesting(CreateFunction create_function);

FastPairHandshakeLookupImpl(const FastPairHandshakeLookupImpl&) = delete;
FastPairHandshakeLookupImpl& operator=(const FastPairHandshakeLookupImpl&) =
delete;

// FastPairHandshakeLookup:
FastPairHandshake* Get(scoped_refptr<Device> device) override;
FastPairHandshake* Get(const std::string& address) override;
bool Erase(scoped_refptr<Device> device) override;
bool Erase(const std::string& address) override;
void Clear() override;
void Create(scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete) override;

protected:
FastPairHandshakeLookupImpl();
virtual ~FastPairHandshakeLookupImpl();

private:
friend struct base::DefaultSingletonTraits<FastPairHandshakeLookupImpl>;

base::flat_map<scoped_refptr<Device>, std::unique_ptr<FastPairHandshake>>
fast_pair_handshakes_;
};

} // namespace quick_pair
} // namespace ash

#endif // ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_LOOKUP_H_

0 comments on commit acecefc

Please sign in to comment.