fake_position_cache.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "services/device/geolocation/fake_position_cache.h"
  5. #include <algorithm>
  6. #include "services/device/geolocation/wifi_data.h"
  7. #include "services/device/public/cpp/geolocation/geoposition.h"
  8. namespace device {
  9. namespace {
  10. template <typename Set>
  11. bool SetsEqual(const Set& lhs, const Set& rhs) {
  12. // Since sets order elements via an operator, std::equal doesn't work. It
  13. // would require elements to be equal-comparable. Check if symmetric
  14. // difference is empty instead.
  15. std::vector<typename Set::value_type> symmetric_difference;
  16. std::set_symmetric_difference(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(),
  17. std::back_inserter(symmetric_difference),
  18. typename Set::value_compare());
  19. return symmetric_difference.empty();
  20. }
  21. } // namespace
  22. FakePositionCache::FakePositionCache() = default;
  23. FakePositionCache::~FakePositionCache() = default;
  24. void FakePositionCache::CachePosition(const WifiData& wifi_data,
  25. const mojom::Geoposition& position) {
  26. data.push_back(std::make_pair(wifi_data, position));
  27. }
  28. const mojom::Geoposition* FakePositionCache::FindPosition(
  29. const WifiData& wifi_data) const {
  30. auto it = std::find_if(
  31. data.begin(), data.end(), [&wifi_data](const auto& candidate_pair) {
  32. return SetsEqual(wifi_data.access_point_data,
  33. candidate_pair.first.access_point_data);
  34. });
  35. return it == data.end() ? nullptr : &(it->second);
  36. }
  37. size_t FakePositionCache::GetPositionCacheSize() const {
  38. return data.size();
  39. }
  40. const mojom::Geoposition& FakePositionCache::GetLastUsedNetworkPosition()
  41. const {
  42. return last_used_position;
  43. }
  44. void FakePositionCache::SetLastUsedNetworkPosition(
  45. const mojom::Geoposition& position) {
  46. last_used_position = position;
  47. }
  48. } // namespace device