expiring_remote_device_cache.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_EXPIRING_REMOTE_DEVICE_CACHE_H_
  5. #define ASH_COMPONENTS_MULTIDEVICE_EXPIRING_REMOTE_DEVICE_CACHE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/components/multidevice/remote_device.h"
  9. #include "ash/components/multidevice/remote_device_ref.h"
  10. #include "base/containers/flat_set.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace ash::multidevice {
  13. class RemoteDeviceCache;
  14. // A helper class around RemoteDeviceCache which keeps track of which devices
  15. // have been removed from, or "expired" in, the cache.
  16. //
  17. // If the set of devices provided to SetRemoteDevicesAndInvalidateOldEntries()
  18. // is different from the set provided to a previous call to
  19. // SetRemoteDevicesAndInvalidateOldEntries(), then the devices in the previous
  20. // call which are not in the new call will be marked as stale. Stale devices are
  21. // still valid RemoteDeviceRefs (preventing clients from segfaulting), but will
  22. // not be returned by GetNonExpiredRemoteDevices().
  23. //
  24. // Note: Because RemoteDeviceCache supports both Instance IDs and legacy device
  25. // IDs, ExpiringRemoteDeviceCache does the same.
  26. class ExpiringRemoteDeviceCache {
  27. public:
  28. ExpiringRemoteDeviceCache();
  29. ExpiringRemoteDeviceCache(const ExpiringRemoteDeviceCache&) = delete;
  30. ExpiringRemoteDeviceCache& operator=(const ExpiringRemoteDeviceCache&) =
  31. delete;
  32. virtual ~ExpiringRemoteDeviceCache();
  33. void SetRemoteDevicesAndInvalidateOldEntries(
  34. const RemoteDeviceList& remote_devices);
  35. RemoteDeviceRefList GetNonExpiredRemoteDevices() const;
  36. // Add or update a RemoteDevice without marking any other devices in the cache
  37. // as stale.
  38. void UpdateRemoteDevice(const RemoteDevice& remote_device);
  39. // Looks up device in cache by Instance ID, |instance_id|, and by the legacy
  40. // device ID from RemoteDevice::GetDeviceId(), |legacy_device_id|. Returns the
  41. // first device that matches either ID. Returns null if no such device exists.
  42. //
  43. // For best results, pass in both IDs when available since the device could
  44. // have been written to the cache with one of the IDs missing.
  45. absl::optional<RemoteDeviceRef> GetRemoteDevice(
  46. const absl::optional<std::string>& instance_id,
  47. const absl::optional<std::string>& legacy_device_id) const;
  48. private:
  49. void RememberIdsFromLastSetCall(const RemoteDevice& device);
  50. std::unique_ptr<RemoteDeviceCache> remote_device_cache_;
  51. base::flat_set<std::string> legacy_device_ids_from_last_set_call_;
  52. base::flat_set<std::string> instance_ids_from_last_set_call_;
  53. };
  54. } // namespace ash::multidevice
  55. #endif // ASH_COMPONENTS_MULTIDEVICE_EXPIRING_REMOTE_DEVICE_CACHE_H_