Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / metrics / metrics_reporting_scheduler.h
blob4d6102a14e7dfa815df518f323c70fb2af4853a8
1 // Copyright 2014 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 #ifndef COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_
6 #define COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "components/metrics/net/network_metrics_provider.h"
15 namespace metrics {
17 // Scheduler task to drive a MetricsService object's uploading.
18 class MetricsReportingScheduler {
19 public:
20 // Creates MetricsServiceScheduler object with the given |upload_callback|
21 // callback to call when uploading should happen and
22 // |upload_interval_callback| to determine the interval between uploads
23 // in steady state.
24 MetricsReportingScheduler(
25 const base::Closure& upload_callback,
26 const base::Callback<base::TimeDelta(void)>& upload_interval_callback);
27 ~MetricsReportingScheduler();
29 // Starts scheduling uploads. This in a no-op if the scheduler is already
30 // running, so it is safe to call more than once.
31 void Start();
33 // Stops scheduling uploads.
34 void Stop();
36 // Callback from MetricsService when the startup init task has completed.
37 void InitTaskComplete();
39 // Callback from MetricsService when a triggered upload finishes.
40 void UploadFinished(bool server_is_healthy, bool more_logs_remaining);
42 // Callback from MetricsService when a triggered upload is cancelled by the
43 // MetricsService.
44 void UploadCancelled();
46 // Sets the upload interval to a specific value, exposed for unit tests.
47 void SetUploadIntervalForTesting(base::TimeDelta interval);
49 private:
50 // Timer callback indicating it's time for the MetricsService to upload
51 // metrics.
52 void TriggerUpload();
54 // Schedules a future call to TriggerUpload if one isn't already pending.
55 void ScheduleNextUpload();
57 // Increases the upload interval each time it's called, to handle the case
58 // where the server is having issues.
59 void BackOffUploadInterval();
61 // Returns upload interval to use in steady state.
62 base::TimeDelta GetStandardUploadInterval();
64 // The MetricsService method to call when uploading should happen.
65 const base::Closure upload_callback_;
67 base::OneShotTimer<MetricsReportingScheduler> upload_timer_;
69 // The interval between being told an upload is done and starting the next
70 // upload.
71 base::TimeDelta upload_interval_;
73 // The tick count of the last time log upload has been finished and null if no
74 // upload has been done yet.
75 base::TimeTicks last_upload_finish_time_;
77 // Indicates that the scheduler is running (i.e., that Start has been called
78 // more recently than Stop).
79 bool running_;
81 // Indicates that the last triggered upload hasn't resolved yet.
82 bool callback_pending_;
84 // Whether the InitTaskComplete() callback has been called.
85 bool init_task_complete_;
87 // Whether the initial scheduled upload timer has fired before the init task
88 // has been completed.
89 bool waiting_for_init_task_complete_;
91 // Callback function used to get the standard upload time.
92 base::Callback<base::TimeDelta(void)> upload_interval_callback_;
94 DISALLOW_COPY_AND_ASSIGN(MetricsReportingScheduler);
97 } // namespace metrics
99 #endif // COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_