expiring_remote_device_cache.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "ash/components/multidevice/expiring_remote_device_cache.h"
  5. #include "ash/components/multidevice/remote_device_cache.h"
  6. #include "base/containers/contains.h"
  7. namespace ash::multidevice {
  8. ExpiringRemoteDeviceCache::ExpiringRemoteDeviceCache()
  9. : remote_device_cache_(RemoteDeviceCache::Factory::Create()) {}
  10. ExpiringRemoteDeviceCache::~ExpiringRemoteDeviceCache() = default;
  11. void ExpiringRemoteDeviceCache::SetRemoteDevicesAndInvalidateOldEntries(
  12. const RemoteDeviceList& remote_devices) {
  13. remote_device_cache_->SetRemoteDevices(remote_devices);
  14. legacy_device_ids_from_last_set_call_.clear();
  15. instance_ids_from_last_set_call_.clear();
  16. for (const auto& device : remote_devices)
  17. RememberIdsFromLastSetCall(device);
  18. }
  19. RemoteDeviceRefList ExpiringRemoteDeviceCache::GetNonExpiredRemoteDevices()
  20. const {
  21. // Only add to the output list if the entry is not stale.
  22. RemoteDeviceRefList remote_devices;
  23. for (auto device : remote_device_cache_->GetRemoteDevices()) {
  24. if ((!device.instance_id().empty() &&
  25. base::Contains(instance_ids_from_last_set_call_,
  26. device.instance_id())) ||
  27. (!device.GetDeviceId().empty() &&
  28. base::Contains(legacy_device_ids_from_last_set_call_,
  29. device.GetDeviceId()))) {
  30. remote_devices.push_back(device);
  31. }
  32. }
  33. return remote_devices;
  34. }
  35. void ExpiringRemoteDeviceCache::UpdateRemoteDevice(
  36. const RemoteDevice& remote_device) {
  37. remote_device_cache_->SetRemoteDevices({remote_device});
  38. RememberIdsFromLastSetCall(remote_device);
  39. }
  40. absl::optional<RemoteDeviceRef> ExpiringRemoteDeviceCache::GetRemoteDevice(
  41. const absl::optional<std::string>& instance_id,
  42. const absl::optional<std::string>& legacy_device_id) const {
  43. return remote_device_cache_->GetRemoteDevice(instance_id, legacy_device_id);
  44. }
  45. void ExpiringRemoteDeviceCache::RememberIdsFromLastSetCall(
  46. const RemoteDevice& device) {
  47. if (!device.instance_id.empty())
  48. instance_ids_from_last_set_call_.insert(device.instance_id);
  49. if (!device.GetDeviceId().empty())
  50. legacy_device_ids_from_last_set_call_.insert(device.GetDeviceId());
  51. }
  52. } // namespace ash::multidevice