Home | History | Annotate | Download | only in metrics
      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.
      4 
      5 #ifndef COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_
      6 #define COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_
      7 
      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 
     14 namespace metrics {
     15 
     16 // Scheduler task to drive a MetricsService object's uploading.
     17 class MetricsReportingScheduler {
     18  public:
     19   explicit MetricsReportingScheduler(const base::Closure& upload_callback);
     20   ~MetricsReportingScheduler();
     21 
     22   // Starts scheduling uploads. This in a no-op if the scheduler is already
     23   // running, so it is safe to call more than once.
     24   void Start();
     25 
     26   // Stops scheduling uploads.
     27   void Stop();
     28 
     29   // Callback from MetricsService when the startup init task has completed.
     30   void InitTaskComplete();
     31 
     32   // Callback from MetricsService when a triggered upload finishes.
     33   void UploadFinished(bool server_is_healthy, bool more_logs_remaining);
     34 
     35   // Callback from MetricsService when a triggered upload is cancelled by the
     36   // MetricsService.
     37   void UploadCancelled();
     38 
     39   // Sets the upload interval to a specific value, exposed for unit tests.
     40   void SetUploadIntervalForTesting(base::TimeDelta interval);
     41 
     42  private:
     43   // Timer callback indicating it's time for the MetricsService to upload
     44   // metrics.
     45   void TriggerUpload();
     46 
     47   // Schedules a future call to TriggerUpload if one isn't already pending.
     48   void ScheduleNextUpload();
     49 
     50   // Increases the upload interval each time it's called, to handle the case
     51   // where the server is having issues.
     52   void BackOffUploadInterval();
     53 
     54   // The MetricsService method to call when uploading should happen.
     55   const base::Closure upload_callback_;
     56 
     57   base::OneShotTimer<MetricsReportingScheduler> upload_timer_;
     58 
     59   // The interval between being told an upload is done and starting the next
     60   // upload.
     61   base::TimeDelta upload_interval_;
     62 
     63   // Indicates that the scheduler is running (i.e., that Start has been called
     64   // more recently than Stop).
     65   bool running_;
     66 
     67   // Indicates that the last triggered upload hasn't resolved yet.
     68   bool callback_pending_;
     69 
     70   // Whether the InitTaskComplete() callback has been called.
     71   bool init_task_complete_;
     72 
     73   // Whether the initial scheduled upload timer has fired before the init task
     74   // has been completed.
     75   bool waiting_for_init_task_complete_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(MetricsReportingScheduler);
     78 };
     79 
     80 }  // namespace metrics
     81 
     82 #endif  // COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_
     83