Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/variations/variations_request_scheduler_mobile. h" | 5 #include "chrome/browser/metrics/variations/variations_request_scheduler_mobile. h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "chrome/common/pref_names.h" | 8 #include "chrome/common/pref_names.h" |
| 9 | 9 |
| 10 namespace chrome_variations { | 10 namespace chrome_variations { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // Time before attempting a seed fetch after a ScheduleFetch(), in seconds. | |
| 15 const int kScheduleFetchDelaySeconds = 5; | |
| 16 | |
| 14 // Time between seed fetches, in hours. | 17 // Time between seed fetches, in hours. |
| 15 const int kSeedFetchPeriodHours = 5; | 18 const int kSeedFetchPeriodHours = 5; |
| 16 | 19 |
| 17 } // namespace | 20 } // namespace |
| 18 | 21 |
| 19 VariationsRequestSchedulerMobile::VariationsRequestSchedulerMobile( | 22 VariationsRequestSchedulerMobile::VariationsRequestSchedulerMobile( |
| 20 const base::Closure& task, | 23 const base::Closure& task, |
| 21 PrefService* local_state) : | 24 PrefService* local_state) : |
| 22 VariationsRequestScheduler(task), local_state_(local_state) { | 25 VariationsRequestScheduler(task), local_state_(local_state) { |
| 23 } | 26 } |
| 24 | 27 |
| 25 VariationsRequestSchedulerMobile::~VariationsRequestSchedulerMobile() { | 28 VariationsRequestSchedulerMobile::~VariationsRequestSchedulerMobile() { |
| 26 } | 29 } |
| 27 | 30 |
| 28 void VariationsRequestSchedulerMobile::Start() { | 31 void VariationsRequestSchedulerMobile::Start() { |
| 29 // Check the time of the last request. If it has been longer than the fetch | 32 // Check the time of the last request. If it has been longer than the fetch |
| 30 // period, run the task. Otherwise, do nothing. Note that no future requests | 33 // period, run the task. Otherwise, do nothing. Note that no future requests |
| 31 // are scheduled since it is unlikely that the mobile process would live long | 34 // are scheduled since it is unlikely that the mobile process would live long |
| 32 // enough for the timer to fire. | 35 // enough for the timer to fire. |
| 33 const base::Time last_fetch_time = base::Time::FromInternalValue( | 36 const base::Time last_fetch_time = base::Time::FromInternalValue( |
| 34 local_state_->GetInt64(prefs::kVariationsLastFetchTime)); | 37 local_state_->GetInt64(prefs::kVariationsLastFetchTime)); |
| 35 if (base::Time::Now() > | 38 if (base::Time::Now() > |
| 36 last_fetch_time + base::TimeDelta::FromHours(kSeedFetchPeriodHours)) { | 39 last_fetch_time + base::TimeDelta::FromHours(kSeedFetchPeriodHours)) { |
| 40 last_request_time_ = base::Time::Now(); | |
| 37 task().Run(); | 41 task().Run(); |
| 38 } | 42 } |
| 39 } | 43 } |
| 40 | 44 |
| 41 void VariationsRequestSchedulerMobile::Reset() { | 45 void VariationsRequestSchedulerMobile::Reset() { |
| 42 } | 46 } |
| 43 | 47 |
| 48 void VariationsRequestSchedulerMobile::OnAppEnterForeground() { | |
| 49 // Verify we haven't just attempted a fetch (which has not completed). This | |
| 50 // is mainly used to verify we don't trigger a second fetch for the | |
| 51 // OnAppEnterForeground right after startup. | |
| 52 if (base::Time::Now() < | |
| 53 last_request_time_ + base::TimeDelta::FromHours(kSeedFetchPeriodHours)) { | |
|
Alexei Svitkine (slow)
2014/02/14 19:11:51
Nit: Align.
rkaplow
2014/02/14 19:18:04
Done.
| |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 // Since Start() launches a one-off execution, we can reuse it here. Also | |
| 58 // note that since Start() verifies that the seed needs to be refreshed, we | |
| 59 // do not verify here. | |
| 60 schedule_fetch_timer_.Start( | |
| 61 FROM_HERE, | |
| 62 base::TimeDelta::FromSeconds(kScheduleFetchDelaySeconds), | |
| 63 this, | |
| 64 &VariationsRequestSchedulerMobile::Start); | |
| 65 } | |
| 66 | |
| 44 // static | 67 // static |
| 45 VariationsRequestScheduler* VariationsRequestScheduler::Create( | 68 VariationsRequestScheduler* VariationsRequestScheduler::Create( |
| 46 const base::Closure& task, | 69 const base::Closure& task, |
| 47 PrefService* local_state) { | 70 PrefService* local_state) { |
| 48 return new VariationsRequestSchedulerMobile(task, local_state); | 71 return new VariationsRequestSchedulerMobile(task, local_state); |
| 49 } | 72 } |
| 50 | 73 |
| 51 } // namespace chrome_variations | 74 } // namespace chrome_variations |
| OLD | NEW |