wifi_polling_policy.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef SERVICES_DEVICE_GEOLOCATION_WIFI_POLLING_POLICY_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_WIFI_POLLING_POLICY_H_
  6. #include <memory>
  7. #include "base/check.h"
  8. #include "base/time/time.h"
  9. namespace device {
  10. // Allows sharing and mocking of the update polling policy function.
  11. class WifiPollingPolicy {
  12. public:
  13. WifiPollingPolicy(const WifiPollingPolicy&) = delete;
  14. WifiPollingPolicy& operator=(const WifiPollingPolicy&) = delete;
  15. virtual ~WifiPollingPolicy() = default;
  16. // Methods for managing the single instance of WifiPollingPolicy. The WiFi
  17. // policy is global so it can outlive the WifiDataProvider instance, which is
  18. // shut down and destroyed when no WiFi scanning is active.
  19. static void Initialize(std::unique_ptr<WifiPollingPolicy>);
  20. static void Shutdown();
  21. static WifiPollingPolicy* Get();
  22. static bool IsInitialized();
  23. // Calculates the new polling interval for wifi scans, given the previous
  24. // interval and whether the last scan produced new results.
  25. virtual void UpdatePollingInterval(bool scan_results_differ) = 0;
  26. // Use InitialInterval to schedule the initial scan when the wifi data
  27. // provider is first started. Returns the number of milliseconds before the
  28. // initial scan should be performed. May return zero if the policy allows a
  29. // scan to be performed immediately.
  30. virtual int InitialInterval() = 0;
  31. // Use PollingInterval to schedule a new scan after the previous scan results
  32. // are available. Only use PollingInterval if WLAN hardware is available and
  33. // can perform scans for nearby access points. If the current interval is
  34. // complete, PollingInterval returns the duration for a new interval starting
  35. // at the current time.
  36. virtual int PollingInterval() = 0;
  37. // Use NoWifiInterval to schedule a new scan after the previous scan results
  38. // are available. NoWifiInterval is typically shorter than PollingInterval
  39. // and should not be used if wifi scanning is available in order to conserve
  40. // power. If the current interval is complete, NoWifiInterval returns the
  41. // duration for a new interval starting at the current time.
  42. virtual int NoWifiInterval() = 0;
  43. protected:
  44. WifiPollingPolicy() = default;
  45. };
  46. // Generic polling policy, constants are compile-time parameterized to allow
  47. // tuning on a per-platform basis.
  48. template <int DEFAULT_INTERVAL,
  49. int NO_CHANGE_INTERVAL,
  50. int TWO_NO_CHANGE_INTERVAL,
  51. int NO_WIFI_INTERVAL>
  52. class GenericWifiPollingPolicy : public WifiPollingPolicy {
  53. public:
  54. GenericWifiPollingPolicy() = default;
  55. // WifiPollingPolicy
  56. void UpdatePollingInterval(bool scan_results_differ) override {
  57. if (scan_results_differ) {
  58. polling_interval_ = DEFAULT_INTERVAL;
  59. } else if (polling_interval_ == DEFAULT_INTERVAL) {
  60. polling_interval_ = NO_CHANGE_INTERVAL;
  61. } else {
  62. DCHECK(polling_interval_ == NO_CHANGE_INTERVAL ||
  63. polling_interval_ == TWO_NO_CHANGE_INTERVAL);
  64. polling_interval_ = TWO_NO_CHANGE_INTERVAL;
  65. }
  66. }
  67. int InitialInterval() override { return ComputeInterval(polling_interval_); }
  68. int PollingInterval() override {
  69. int interval = ComputeInterval(polling_interval_);
  70. return interval <= 0 ? polling_interval_ : interval;
  71. }
  72. int NoWifiInterval() override {
  73. int interval = ComputeInterval(NO_WIFI_INTERVAL);
  74. return interval <= 0 ? NO_WIFI_INTERVAL : interval;
  75. }
  76. private:
  77. int ComputeInterval(int polling_interval) {
  78. base::Time now = base::Time::Now();
  79. int64_t remaining_millis = 0;
  80. if (!interval_start_.is_null()) {
  81. // If the new interval duration differs from the initial duration, use the
  82. // shorter duration.
  83. if (polling_interval < interval_duration_)
  84. interval_duration_ = polling_interval;
  85. // Compute the remaining duration of the current interval. If the interval
  86. // is not yet complete, we will schedule a scan to occur once it is.
  87. base::TimeDelta remaining =
  88. interval_start_ + base::Milliseconds(interval_duration_) - now;
  89. remaining_millis = remaining.InMilliseconds();
  90. }
  91. // If the current interval is complete (or if this is our first scan),
  92. // start a new interval beginning now.
  93. if (remaining_millis <= 0) {
  94. interval_start_ = now;
  95. interval_duration_ = polling_interval;
  96. remaining_millis = 0;
  97. }
  98. return remaining_millis;
  99. }
  100. // The current duration of the polling interval. When wifi data is
  101. // substantially the same from one scan to the next, this may be increased to
  102. // reduce the frequency of wifi scanning.
  103. int polling_interval_ = DEFAULT_INTERVAL;
  104. // The start time for the most recent interval. Initialized to the "null" time
  105. // value.
  106. base::Time interval_start_;
  107. // Duration for the interval starting at |interval_start_|.
  108. int interval_duration_ = DEFAULT_INTERVAL;
  109. };
  110. } // namespace device
  111. #endif // SERVICES_DEVICE_GEOLOCATION_WIFI_POLLING_POLICY_H_