-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fast Pair] Create abstract handshake lookup
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
Showing
5 changed files
with
155 additions
and
95 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
76 changes: 5 additions & 71 deletions
76
ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup.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 |
---|---|---|
@@ -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 |
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
87 changes: 87 additions & 0 deletions
87
ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup_impl.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,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 |
48 changes: 48 additions & 0 deletions
48
ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup_impl.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,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_ |