summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/metrics_reporting_scheduler.h
blob: 695f6ec5ea0b6d98b3fe1364975a3138f5a57af1 (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
// Copyright (c) 2012 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 CHROME_BROWSER_METRICS_METRICS_REPORTING_SCHEDULER_H_
#define CHROME_BROWSER_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"

// Scheduler task to drive a MetricsService object's uploading.
class MetricsReportingScheduler {
 public:
  explicit MetricsReportingScheduler(const base::Closure& upload_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 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();

 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();

  // 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_;

  // 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_;

  DISALLOW_COPY_AND_ASSIGN(MetricsReportingScheduler);
};

#endif  // CHROME_BROWSER_METRICS_METRICS_REPORTING_SCHEDULER_H_