Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / variations / variations_request_scheduler.cc
blob9c239afa9130d05a6780e5943cacff7dc34f87bd
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.
5 #include "components/variations/variations_request_scheduler.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "components/variations/variations_associated_data.h"
10 namespace variations {
12 VariationsRequestScheduler::VariationsRequestScheduler(
13 const base::Closure& task) : task_(task) {
16 VariationsRequestScheduler::~VariationsRequestScheduler() {
19 void VariationsRequestScheduler::Start() {
20 task_.Run();
21 timer_.Start(FROM_HERE, GetFetchPeriod(), task_);
24 void VariationsRequestScheduler::Reset() {
25 if (timer_.IsRunning())
26 timer_.Reset();
27 one_shot_timer_.Stop();
30 void VariationsRequestScheduler::ScheduleFetchShortly() {
31 // Reset the regular timer to avoid it triggering soon after.
32 Reset();
33 // The delay before attempting a fetch shortly, in minutes.
34 const int kFetchShortlyDelayMinutes = 5;
35 one_shot_timer_.Start(FROM_HERE,
36 base::TimeDelta::FromMinutes(kFetchShortlyDelayMinutes),
37 task_);
40 void VariationsRequestScheduler::OnAppEnterForeground() {
41 NOTREACHED() << "Attempted to OnAppEnterForeground on non-mobile device";
44 base::TimeDelta VariationsRequestScheduler::GetFetchPeriod() const {
45 // The fetch interval can be overridden by a variation param.
46 std::string period_min_str =
47 variations::GetVariationParamValue("VarationsServiceControl",
48 "fetch_period_min");
49 size_t period_min;
50 if (base::StringToSizeT(period_min_str, &period_min))
51 return base::TimeDelta::FromMinutes(period_min);
53 // The default fetch interval is every 5 hours.
54 return base::TimeDelta::FromHours(5);
57 base::Closure VariationsRequestScheduler::task() const {
58 return task_;
61 #if !defined(OS_ANDROID) && !defined(OS_IOS)
62 // static
63 VariationsRequestScheduler* VariationsRequestScheduler::Create(
64 const base::Closure& task,
65 PrefService* local_state) {
66 return new VariationsRequestScheduler(task);
68 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
70 } // namespace variations