position_cache_impl.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 SERVICES_DEVICE_GEOLOCATION_POSITION_CACHE_IMPL_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_POSITION_CACHE_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. #include "net/base/network_change_notifier.h"
  14. #include "services/device/geolocation/position_cache.h"
  15. #include "services/device/public/mojom/geoposition.mojom.h"
  16. namespace base {
  17. class TickClock;
  18. } // namespace base
  19. namespace device {
  20. class PositionCacheImpl
  21. : public PositionCache,
  22. public net::NetworkChangeNotifier::NetworkChangeObserver {
  23. public:
  24. // The maximum size of the cache of positions.
  25. static const size_t kMaximumSize;
  26. // The maximum time an entry can reside in cache before forced eviction.
  27. // This is to ensure the user's location cannot be tracked arbitrarily far
  28. // back in history.
  29. static const base::TimeDelta kMaximumLifetime;
  30. // |clock| is used to measure time left until kMaximumLifetime.
  31. explicit PositionCacheImpl(const base::TickClock* clock);
  32. PositionCacheImpl(const PositionCacheImpl&) = delete;
  33. PositionCacheImpl& operator=(const PositionCacheImpl&) = delete;
  34. ~PositionCacheImpl() override;
  35. void CachePosition(const WifiData& wifi_data,
  36. const mojom::Geoposition& position) override;
  37. const mojom::Geoposition* FindPosition(
  38. const WifiData& wifi_data) const override;
  39. size_t GetPositionCacheSize() const override;
  40. const mojom::Geoposition& GetLastUsedNetworkPosition() const override;
  41. void SetLastUsedNetworkPosition(const mojom::Geoposition& position) override;
  42. // net::NetworkChangeNotifier::NetworkChangeObserver
  43. void OnNetworkChanged(
  44. net::NetworkChangeNotifier::ConnectionType type) override;
  45. private:
  46. // In order to avoid O(N) comparisons while searching for the right WifiData,
  47. // we hash the contents of those objects and use the hashes as cache keys.
  48. using Hash = std::u16string;
  49. class CacheEntry {
  50. public:
  51. CacheEntry(const Hash& hash,
  52. const mojom::Geoposition& position,
  53. std::unique_ptr<base::OneShotTimer> eviction_timer);
  54. CacheEntry(const CacheEntry&) = delete;
  55. CacheEntry& operator=(const CacheEntry&) = delete;
  56. ~CacheEntry();
  57. CacheEntry(CacheEntry&&);
  58. CacheEntry& operator=(CacheEntry&&);
  59. inline bool operator==(const Hash& hash) const { return hash_ == hash; }
  60. const mojom::Geoposition* position() const { return &position_; }
  61. private:
  62. Hash hash_;
  63. mojom::Geoposition position_;
  64. std::unique_ptr<base::OneShotTimer> eviction_timer_;
  65. };
  66. static Hash MakeKey(const WifiData& wifi_data);
  67. void EvictEntry(const Hash& hash);
  68. raw_ptr<const base::TickClock> clock_;
  69. std::vector<CacheEntry> data_;
  70. mojom::Geoposition last_used_position_;
  71. };
  72. } // namespace device
  73. #endif // SERVICES_DEVICE_GEOLOCATION_POSITION_CACHE_IMPL_H_