| OLD | NEW |
| (Empty) |
| 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/callback.h" | |
| 9 #include "base/macros.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" | |
| 14 | |
| 15 namespace metrics { | |
| 16 | |
| 17 // Scheduler task to drive a MetricsService object's uploading. | |
| 18 // TODO(holte): Remove this once we've switched to split schedulers. | |
| 19 class MetricsReportingScheduler { | |
| 20 public: | |
| 21 // Creates MetricsServiceScheduler object with the given |upload_callback| | |
| 22 // callback to call when uploading should happen and | |
| 23 // |upload_interval_callback| to determine the interval between uploads | |
| 24 // in steady state. | |
| 25 MetricsReportingScheduler( | |
| 26 const base::Closure& upload_callback, | |
| 27 const base::Callback<base::TimeDelta(void)>& upload_interval_callback); | |
| 28 virtual ~MetricsReportingScheduler(); | |
| 29 | |
| 30 // Starts scheduling uploads. This in a no-op if the scheduler is already | |
| 31 // running, so it is safe to call more than once. | |
| 32 void Start(); | |
| 33 | |
| 34 // Stops scheduling uploads. | |
| 35 void Stop(); | |
| 36 | |
| 37 // Callback from MetricsService when the startup init task has completed. | |
| 38 void InitTaskComplete(); | |
| 39 | |
| 40 // Callback from MetricsService when a triggered upload finishes. | |
| 41 void UploadFinished(bool server_is_healthy, bool more_logs_remaining); | |
| 42 | |
| 43 // Callback from MetricsService when a triggered upload is cancelled by the | |
| 44 // MetricsService. | |
| 45 void UploadCancelled(); | |
| 46 | |
| 47 // Sets the upload interval to a specific value, exposed for unit tests. | |
| 48 void SetUploadIntervalForTesting(base::TimeDelta interval); | |
| 49 | |
| 50 protected: | |
| 51 enum InitSequence { | |
| 52 TIMER_FIRED_FIRST, | |
| 53 INIT_TASK_COMPLETED_FIRST, | |
| 54 INIT_SEQUENCE_ENUM_SIZE, | |
| 55 }; | |
| 56 | |
| 57 private: | |
| 58 // Record the init sequence order histogram. | |
| 59 virtual void LogMetricsInitSequence(InitSequence sequence); | |
| 60 | |
| 61 // Record the upload interval time. | |
| 62 virtual void LogActualUploadInterval(base::TimeDelta interval); | |
| 63 | |
| 64 // Timer callback indicating it's time for the MetricsService to upload | |
| 65 // metrics. | |
| 66 void TriggerUpload(); | |
| 67 | |
| 68 // Schedules a future call to TriggerUpload if one isn't already pending. | |
| 69 void ScheduleNextUpload(); | |
| 70 | |
| 71 // Increases the upload interval each time it's called, to handle the case | |
| 72 // where the server is having issues. | |
| 73 void BackOffUploadInterval(); | |
| 74 | |
| 75 // Returns upload interval to use in steady state. | |
| 76 base::TimeDelta GetStandardUploadInterval(); | |
| 77 | |
| 78 // The MetricsService method to call when uploading should happen. | |
| 79 const base::Closure upload_callback_; | |
| 80 | |
| 81 base::OneShotTimer upload_timer_; | |
| 82 | |
| 83 // The interval between being told an upload is done and starting the next | |
| 84 // upload. | |
| 85 base::TimeDelta upload_interval_; | |
| 86 | |
| 87 // The tick count of the last time log upload has been finished and null if no | |
| 88 // upload has been done yet. | |
| 89 base::TimeTicks last_upload_finish_time_; | |
| 90 | |
| 91 // Indicates that the scheduler is running (i.e., that Start has been called | |
| 92 // more recently than Stop). | |
| 93 bool running_; | |
| 94 | |
| 95 // Indicates that the last triggered upload hasn't resolved yet. | |
| 96 bool callback_pending_; | |
| 97 | |
| 98 // Whether the InitTaskComplete() callback has been called. | |
| 99 bool init_task_complete_; | |
| 100 | |
| 101 // Whether the initial scheduled upload timer has fired before the init task | |
| 102 // has been completed. | |
| 103 bool waiting_for_init_task_complete_; | |
| 104 | |
| 105 // Callback function used to get the standard upload time. | |
| 106 base::Callback<base::TimeDelta(void)> upload_interval_callback_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(MetricsReportingScheduler); | |
| 109 }; | |
| 110 | |
| 111 } // namespace metrics | |
| 112 | |
| 113 #endif // COMPONENTS_METRICS_METRICS_REPORTING_SCHEDULER_H_ | |
| OLD | NEW |