summaryrefslogtreecommitdiffstats
path: root/components/metrics/metrics_reporting_scheduler.h
blob: 4d6102a14e7dfa815df518f323c70fb2af4853a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_
#define COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/metrics/net/network_metrics_provider.h"

namespace metrics {

// Scheduler task to drive a MetricsService object's uploading.
class MetricsReportingScheduler {
 public:
  // Creates MetricsServiceScheduler object with the given |upload_callback|
  // callback to call when uploading should happen and
  // |upload_interval_callback| to determine the interval between uploads
  // in steady state.
  MetricsReportingScheduler(
      const base::Closure& upload_callback,
      const base::Callback<base::TimeDelta(void)>& upload_interval_callback);
  ~MetricsReportingScheduler();

  // Starts scheduling uploads. This in a no-op if the scheduler is already
  // running, so it is safe to call more than once.
  void Start();

  // Stops scheduling uploads.
  void Stop();

  // Callback from MetricsService when the startup init task has completed.
  void InitTaskComplete();

  // Callback from MetricsService when a triggered upload finishes.
  void UploadFinished(bool server_is_healthy, bool more_logs_remaining);

  // Callback from MetricsService when a triggered upload is cancelled by the
  // MetricsService.
  void UploadCancelled();

  // Sets the upload interval to a specific value, exposed for unit tests.
  void SetUploadIntervalForTesting(base::TimeDelta interval);

 private:
  // Timer callback indicating it's time for the MetricsService to upload
  // metrics.
  void TriggerUpload();

  // Schedules a future call to TriggerUpload if one isn't already pending.
  void ScheduleNextUpload();

  // Increases the upload interval each time it's called, to handle the case
  // where the server is having issues.
  void BackOffUploadInterval();

  // Returns upload interval to use in steady state.
  base::TimeDelta GetStandardUploadInterval();

  // The MetricsService method to call when uploading should happen.
  const base::Closure upload_callback_;

  base::OneShotTimer<MetricsReportingScheduler> upload_timer_;

  // The interval between being told an upload is done and starting the next
  // upload.
  base::TimeDelta upload_interval_;

  // The tick count of the last time log upload has been finished and null if no
  // upload has been done yet.
  base::TimeTicks last_upload_finish_time_;

  // Indicates that the scheduler is running (i.e., that Start has been called
  // more recently than Stop).
  bool running_;

  // Indicates that the last triggered upload hasn't resolved yet.
  bool callback_pending_;

  // Whether the InitTaskComplete() callback has been called.
  bool init_task_complete_;

  // Whether the initial scheduled upload timer has fired before the init task
  // has been completed.
  bool waiting_for_init_task_complete_;

  // Callback function used to get the standard upload time.
  base::Callback<base::TimeDelta(void)> upload_interval_callback_;

  DISALLOW_COPY_AND_ASSIGN(MetricsReportingScheduler);
};

}  // namespace metrics

#endif  // COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_