fast_pair_handshake_lookup.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup.h"
  5. #include <memory>
  6. #include "ash/quick_pair/common/device.h"
  7. #include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_impl.h"
  8. #include "base/callback.h"
  9. #include "base/memory/singleton.h"
  10. #include "device/bluetooth/bluetooth_adapter.h"
  11. namespace ash {
  12. namespace quick_pair {
  13. // Create function override which can be set by tests.
  14. absl::optional<FastPairHandshakeLookup::CreateFunction> g_test_create_function =
  15. absl::nullopt;
  16. // static
  17. FastPairHandshakeLookup* FastPairHandshakeLookup::GetInstance() {
  18. return base::Singleton<FastPairHandshakeLookup>::get();
  19. }
  20. // static
  21. void FastPairHandshakeLookup::SetCreateFunctionForTesting(
  22. CreateFunction create_function) {
  23. g_test_create_function = std::move(create_function);
  24. }
  25. FastPairHandshakeLookup::FastPairHandshakeLookup() = default;
  26. FastPairHandshakeLookup::~FastPairHandshakeLookup() = default;
  27. FastPairHandshake* FastPairHandshakeLookup::Get(scoped_refptr<Device> device) {
  28. auto it = fast_pair_handshakes_.find(device);
  29. return it != fast_pair_handshakes_.end() ? it->second.get() : nullptr;
  30. }
  31. FastPairHandshake* FastPairHandshakeLookup::Get(const std::string& address) {
  32. for (const auto& pair : fast_pair_handshakes_) {
  33. if (pair.first->classic_address() == address ||
  34. pair.first->ble_address == address) {
  35. return pair.second.get();
  36. }
  37. }
  38. return nullptr;
  39. }
  40. bool FastPairHandshakeLookup::Erase(scoped_refptr<Device> device) {
  41. return fast_pair_handshakes_.erase(device) == 1;
  42. }
  43. bool FastPairHandshakeLookup::Erase(const std::string& address) {
  44. for (const auto& pair : fast_pair_handshakes_) {
  45. if (pair.first->classic_address() == address ||
  46. pair.first->ble_address == address) {
  47. fast_pair_handshakes_.erase(pair);
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. void FastPairHandshakeLookup::Clear() {
  54. fast_pair_handshakes_.clear();
  55. }
  56. FastPairHandshake* FastPairHandshakeLookup::Create(
  57. scoped_refptr<device::BluetoothAdapter> adapter,
  58. scoped_refptr<Device> device,
  59. OnCompleteCallback on_complete) {
  60. auto it = fast_pair_handshakes_.emplace(
  61. device, g_test_create_function.has_value()
  62. ? g_test_create_function->Run(device, std::move(on_complete))
  63. : std::make_unique<FastPairHandshakeImpl>(
  64. std::move(adapter), device, std::move(on_complete)));
  65. DCHECK(it.second) << "An existing item shouldn't exist.";
  66. return it.first->second.get();
  67. }
  68. } // namespace quick_pair
  69. } // namespace ash