wifi_data.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2013 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/wifi_data.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <algorithm>
  8. #include <limits>
  9. #include "base/check.h"
  10. namespace device {
  11. AccessPointData::AccessPointData()
  12. : radio_signal_strength(std::numeric_limits<int32_t>::min()),
  13. channel(std::numeric_limits<int32_t>::min()),
  14. signal_to_noise(std::numeric_limits<int32_t>::min()) {}
  15. AccessPointData::~AccessPointData() = default;
  16. WifiData::WifiData() = default;
  17. WifiData::WifiData(const WifiData& other) = default;
  18. WifiData::~WifiData() = default;
  19. bool WifiData::DiffersSignificantly(const WifiData& other) const {
  20. // More than 4 or 50% of access points added or removed is significant.
  21. static const size_t kMinChangedAccessPoints = 4;
  22. const size_t min_ap_count =
  23. std::min(access_point_data.size(), other.access_point_data.size());
  24. const size_t max_ap_count =
  25. std::max(access_point_data.size(), other.access_point_data.size());
  26. const size_t difference_threadhold =
  27. std::min(kMinChangedAccessPoints, min_ap_count / 2);
  28. if (max_ap_count > min_ap_count + difference_threadhold)
  29. return true;
  30. // Compute size of intersection of old and new sets.
  31. size_t num_common = 0;
  32. for (AccessPointDataSet::const_iterator iter = access_point_data.begin();
  33. iter != access_point_data.end(); iter++) {
  34. if (other.access_point_data.find(*iter) != other.access_point_data.end()) {
  35. ++num_common;
  36. }
  37. }
  38. DCHECK(num_common <= min_ap_count);
  39. // Test how many have changed.
  40. return max_ap_count > num_common + difference_threadhold;
  41. }
  42. } // namespace device