Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Ahmed Nasr | f8079c03 | 2021-10-30 01:35:19 | [diff] [blame] | 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_REPORTING_METRICS_METRIC_REPORT_QUEUE_H_ |
| 6 | #define COMPONENTS_REPORTING_METRICS_METRIC_REPORT_QUEUE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Ahmed Nasr | 7ba217d | 2022-11-11 21:27:33 | [diff] [blame] | 11 | #include "base/functional/callback_helpers.h" |
Ahmed Nasr | f8079c03 | 2021-10-30 01:35:19 | [diff] [blame] | 12 | #include "base/task/sequenced_task_runner.h" |
| 13 | #include "base/time/time.h" |
| 14 | #include "components/reporting/client/report_queue.h" |
| 15 | #include "components/reporting/proto/synced/record_constants.pb.h" |
| 16 | |
| 17 | namespace reporting { |
| 18 | |
| 19 | class MetricData; |
| 20 | class MetricRateController; |
| 21 | class ReportingSettings; |
| 22 | |
| 23 | // Simple wrapper for `::reporting::ReportQueue` that can be set for periodic |
| 24 | // upload. |
| 25 | class MetricReportQueue { |
| 26 | public: |
| 27 | MetricReportQueue( |
| 28 | std::unique_ptr<ReportQueue, base::OnTaskRunnerDeleter> report_queue, |
| 29 | Priority priority); |
| 30 | |
| 31 | // Constructor used if the reporter is needed to upload records periodically. |
| 32 | MetricReportQueue( |
| 33 | std::unique_ptr<ReportQueue, base::OnTaskRunnerDeleter> report_queue, |
| 34 | Priority priority, |
| 35 | ReportingSettings* reporting_settings, |
| 36 | const std::string& rate_setting_path, |
| 37 | base::TimeDelta default_rate, |
| 38 | int rate_unit_to_ms = 1); |
| 39 | |
| 40 | MetricReportQueue() = delete; |
| 41 | MetricReportQueue(const MetricReportQueue& other) = delete; |
| 42 | MetricReportQueue& operator=(const MetricReportQueue& other) = delete; |
| 43 | |
| 44 | virtual ~MetricReportQueue(); |
| 45 | |
| 46 | // Enqueue the metric data. |
Ahmed Nasr | 7ba217d | 2022-11-11 21:27:33 | [diff] [blame] | 47 | virtual void Enqueue( |
| 48 | MetricData metric_data, |
| 49 | ReportQueue::EnqueueCallback callback = base::DoNothing()); |
Ahmed Nasr | f8079c03 | 2021-10-30 01:35:19 | [diff] [blame] | 50 | |
Ahmed Nasr | 7fab529 | 2022-01-19 23:57:28 | [diff] [blame] | 51 | // Initiate manual upload of records with `priority_` and restart timer if |
| 52 | // exists. |
| 53 | void Upload(); |
| 54 | |
Vignesh Shenvi | 17227fa | 2023-11-17 19:07:28 | [diff] [blame] | 55 | // Retrieves the reporting destination configured with the `report_queue_`. |
Vignesh Shenvi | 51b90be | 2023-12-01 01:15:52 | [diff] [blame] | 56 | virtual Destination GetDestination() const; |
Vignesh Shenvi | 17227fa | 2023-11-17 19:07:28 | [diff] [blame] | 57 | |
Ahmed Nasr | 7fab529 | 2022-01-19 23:57:28 | [diff] [blame] | 58 | private: |
Ahmed Nasr | f8079c03 | 2021-10-30 01:35:19 | [diff] [blame] | 59 | // Initiate upload of records with `priority_`. |
| 60 | virtual void Flush(); |
| 61 | |
Ahmed Nasr | f8079c03 | 2021-10-30 01:35:19 | [diff] [blame] | 62 | const std::unique_ptr<ReportQueue, base::OnTaskRunnerDeleter> report_queue_; |
| 63 | |
| 64 | const Priority priority_; |
| 65 | |
| 66 | std::unique_ptr<MetricRateController> rate_controller_; |
| 67 | }; |
| 68 | } // namespace reporting |
| 69 | |
| 70 | #endif // COMPONENTS_REPORTING_METRICS_METRIC_REPORT_QUEUE_H_ |