fast_pair_handshake_lookup.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_LOOKUP_H_
  5. #define ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_LOOKUP_H_
  6. #include <memory>
  7. #include "ash/quick_pair/common/pair_failure.h"
  8. #include "base/bind.h"
  9. #include "base/callback_forward.h"
  10. #include "base/containers/flat_map.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/memory/singleton.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace device {
  15. class BluetoothAdapter;
  16. } // namespace device
  17. namespace ash {
  18. namespace quick_pair {
  19. struct Device;
  20. class FastPairHandshake;
  21. // This class creates, deletes and exposes FastPairHandshake instances.
  22. class FastPairHandshakeLookup {
  23. public:
  24. using OnCompleteCallback =
  25. base::OnceCallback<void(scoped_refptr<Device>,
  26. absl::optional<PairFailure>)>;
  27. using CreateFunction =
  28. base::RepeatingCallback<std::unique_ptr<FastPairHandshake>(
  29. scoped_refptr<Device> device,
  30. OnCompleteCallback callback)>;
  31. static FastPairHandshakeLookup* GetInstance();
  32. static void SetCreateFunctionForTesting(CreateFunction create_function);
  33. FastPairHandshakeLookup(const FastPairHandshakeLookup&) = delete;
  34. FastPairHandshakeLookup& operator=(const FastPairHandshakeLookup&) = delete;
  35. // Get an existing instance for |device|.
  36. FastPairHandshake* Get(scoped_refptr<Device> device);
  37. // Get an existing instance for |address|.
  38. FastPairHandshake* Get(const std::string& address);
  39. // Erases the FastPairHandshake instance for |device| if exists.
  40. bool Erase(scoped_refptr<Device> device);
  41. // Erases the FastPairHandshake instance for |address| if exists.
  42. bool Erase(const std::string& address);
  43. // Deletes all existing FastPairHandshake instances.
  44. void Clear();
  45. // Creates and returns a new instance for |device| if no instance already
  46. // exists. Returns the existing instance if there is one.
  47. FastPairHandshake* Create(scoped_refptr<device::BluetoothAdapter> adapter,
  48. scoped_refptr<Device> device,
  49. OnCompleteCallback on_complete);
  50. protected:
  51. FastPairHandshakeLookup();
  52. virtual ~FastPairHandshakeLookup();
  53. private:
  54. friend struct base::DefaultSingletonTraits<FastPairHandshakeLookup>;
  55. base::flat_map<scoped_refptr<Device>, std::unique_ptr<FastPairHandshake>>
  56. fast_pair_handshakes_;
  57. };
  58. } // namespace quick_pair
  59. } // namespace ash
  60. #endif // ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_LOOKUP_H_