1 // Copyright 2017 The Chromium Authors
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/metrics/metrics_scheduler.h"
7 #include "build/build_config.h"
12 // The delay, in seconds, after startup before sending the first log message.
13 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
14 // Sessions are more likely to be short on a mobile device, so handle the
15 // initial log quickly.
16 const int kInitialIntervalSeconds = 15;
18 const int kInitialIntervalSeconds = 60;
23 MetricsScheduler::MetricsScheduler(const base::RepeatingClosure& task_callback,
24 bool fast_startup_for_testing)
25 : task_callback_(task_callback),
26 interval_(base::Seconds(
27 fast_startup_for_testing ? 0 : kInitialIntervalSeconds)),
29 callback_pending_(false) {}
31 MetricsScheduler::~MetricsScheduler() {}
33 void MetricsScheduler::Start() {
38 void MetricsScheduler::Stop() {
40 if (timer_.IsRunning())
45 int MetricsScheduler::GetInitialIntervalSeconds() {
46 return kInitialIntervalSeconds;
49 void MetricsScheduler::TaskDone(base::TimeDelta next_interval) {
50 DCHECK(callback_pending_);
51 callback_pending_ = false;
52 interval_ = next_interval;
57 void MetricsScheduler::TriggerTask() {
58 // This can happen in tests which set a very small timer interval.
59 if (callback_pending_)
62 callback_pending_ = true;
66 void MetricsScheduler::ScheduleNextTask() {
68 if (timer_.IsRunning() || callback_pending_)
71 timer_.Start(FROM_HERE, interval_, this, &MetricsScheduler::TriggerTask);
74 } // namespace metrics