remote_device_cache.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 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_COMPONENTS_MULTIDEVICE_REMOTE_DEVICE_CACHE_H_
  5. #define ASH_COMPONENTS_MULTIDEVICE_REMOTE_DEVICE_CACHE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/components/multidevice/remote_device.h"
  10. #include "ash/components/multidevice/remote_device_ref.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace ash::multidevice {
  13. // A simple cache of RemoteDeviceRefs. Note that if multiple calls to
  14. // SetRemoteDevices() are provided different sets of devices, the set of devices
  15. // returned by GetRemoteDevices() is the union of those different sets (i.e.,
  16. // devices are not deleted from the cache).
  17. //
  18. // All devices in the cache will have a unique Instance ID, if one exists,
  19. // and/or a unique legacy device ID, RemoteDevice::GetDeviceId(), if one exists.
  20. // Every device is guaranteed to have at least one non-trivial ID. If a device
  21. // is added with either ID matching an existing device, the existing device is
  22. // overwritten.
  23. //
  24. // Note: Even though CryptAuth v2 DeviceSync guarantees that all devices have an
  25. // Instance ID, there may still be uses of RemoteDeviceCache in multi-device
  26. // application code that solely uses the legacy device ID.
  27. class RemoteDeviceCache {
  28. public:
  29. class Factory {
  30. public:
  31. static std::unique_ptr<RemoteDeviceCache> Create();
  32. static void SetFactoryForTesting(Factory* test_factory);
  33. protected:
  34. virtual ~Factory();
  35. virtual std::unique_ptr<RemoteDeviceCache> CreateInstance() = 0;
  36. private:
  37. static Factory* test_factory_;
  38. };
  39. RemoteDeviceCache(const RemoteDeviceCache&) = delete;
  40. RemoteDeviceCache& operator=(const RemoteDeviceCache&) = delete;
  41. virtual ~RemoteDeviceCache();
  42. void SetRemoteDevices(const RemoteDeviceList& remote_devices);
  43. RemoteDeviceRefList GetRemoteDevices() const;
  44. // Looks up device in cache by Instance ID, |instance_id|, and by the legacy
  45. // device ID from RemoteDevice::GetDeviceId(), |legacy_device_id|. Returns the
  46. // first device that matches either ID. Returns null if no such device exists.
  47. //
  48. // For best results, pass in both IDs when available since the device could
  49. // have been written to the cache with one of the IDs missing.
  50. absl::optional<RemoteDeviceRef> GetRemoteDevice(
  51. const absl::optional<std::string>& instance_id,
  52. const absl::optional<std::string>& legacy_device_id) const;
  53. private:
  54. RemoteDeviceCache();
  55. std::shared_ptr<RemoteDevice> GetRemoteDeviceFromCache(
  56. const absl::optional<std::string>& instance_id,
  57. const absl::optional<std::string>& legacy_device_id) const;
  58. std::vector<std::shared_ptr<RemoteDevice>> cached_remote_devices_;
  59. };
  60. } // namespace ash::multidevice
  61. #endif // ASH_COMPONENTS_MULTIDEVICE_REMOTE_DEVICE_CACHE_H_