variations_request_scheduler.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 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 "components/variations/variations_request_scheduler.h"
  5. #include <stddef.h>
  6. #include "base/strings/string_number_conversions.h"
  7. #include "build/build_config.h"
  8. #include "components/variations/variations_associated_data.h"
  9. #include "components/variations/variations_switches.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace variations {
  12. namespace {
  13. // The minimum time between consecutive variations seed fetches.
  14. constexpr base::TimeDelta kVariationsSeedFetchIntervalMinimum =
  15. base::Minutes(1);
  16. // Returns the variations seed fetch interval specified through the
  17. // |kVariationsSeedFetchInterval| switch. The value returned is subject to a
  18. // minimum value, |kVariationsSeedFetchIntervalMinimum|. If no overridden value
  19. // is specified, or if it is malformed, an empty optional is returned.
  20. absl::optional<base::TimeDelta> GetOverriddenFetchPeriod() {
  21. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  22. const std::string switch_value =
  23. command_line->GetSwitchValueASCII(switches::kVariationsSeedFetchInterval);
  24. if (switch_value.empty())
  25. return absl::nullopt;
  26. int overridden_period;
  27. if (!base::StringToInt(switch_value, &overridden_period)) {
  28. LOG(DFATAL) << "Malformed value for --"
  29. << switches::kVariationsSeedFetchInterval << ". "
  30. << "Expected int, got: " << switch_value;
  31. return absl::nullopt;
  32. }
  33. return std::max(base::Minutes(overridden_period),
  34. kVariationsSeedFetchIntervalMinimum);
  35. }
  36. } // namespace
  37. VariationsRequestScheduler::VariationsRequestScheduler(
  38. const base::RepeatingClosure& task)
  39. : task_(task) {}
  40. VariationsRequestScheduler::~VariationsRequestScheduler() = default;
  41. void VariationsRequestScheduler::Start() {
  42. task_.Run();
  43. timer_.Start(FROM_HERE, GetFetchPeriod(), task_);
  44. }
  45. void VariationsRequestScheduler::Reset() {
  46. if (timer_.IsRunning())
  47. timer_.Reset();
  48. one_shot_timer_.Stop();
  49. }
  50. void VariationsRequestScheduler::ScheduleFetchShortly() {
  51. // Reset the regular timer to avoid it triggering soon after.
  52. Reset();
  53. // The delay before attempting a fetch shortly.
  54. base::TimeDelta fetch_shortly_delay = base::Minutes(5);
  55. // If there is a fetch interval specified in the command line, and it is
  56. // shorter than |fetch_shortly_delay|, use it instead.
  57. absl::optional<base::TimeDelta> overridden_period =
  58. GetOverriddenFetchPeriod();
  59. if (overridden_period.has_value()) {
  60. fetch_shortly_delay =
  61. std::min(fetch_shortly_delay, overridden_period.value());
  62. }
  63. one_shot_timer_.Start(FROM_HERE, fetch_shortly_delay, task_);
  64. }
  65. void VariationsRequestScheduler::OnAppEnterForeground() {
  66. NOTREACHED() << "Attempted to OnAppEnterForeground on non-mobile device";
  67. }
  68. base::TimeDelta VariationsRequestScheduler::GetFetchPeriod() const {
  69. // The fetch interval can be overridden through the command line.
  70. absl::optional<base::TimeDelta> overridden_period =
  71. GetOverriddenFetchPeriod();
  72. if (overridden_period.has_value())
  73. return overridden_period.value();
  74. // The fetch interval can be overridden by a variation param.
  75. std::string period_min_str =
  76. GetVariationParamValue("VariationsServiceControl", "fetch_period_min");
  77. size_t period_min;
  78. if (base::StringToSizeT(period_min_str, &period_min))
  79. return base::Minutes(period_min);
  80. // The default fetch interval is every 30 minutes.
  81. return base::Minutes(30);
  82. }
  83. base::RepeatingClosure VariationsRequestScheduler::task() const {
  84. return task_;
  85. }
  86. #if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
  87. // static
  88. VariationsRequestScheduler* VariationsRequestScheduler::Create(
  89. const base::RepeatingClosure& task,
  90. PrefService* local_state) {
  91. return new VariationsRequestScheduler(task);
  92. }
  93. #endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
  94. } // namespace variations