Ahmed Nasr | 8d2c5df5 | 2022-11-11 23:05:08 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
| 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_COLLECTOR_BASE_H_ |
| 6 | #define COMPONENTS_REPORTING_METRICS_COLLECTOR_BASE_H_ |
| 7 | |
| 8 | #include "base/memory/raw_ptr.h" |
| 9 | #include "base/memory/weak_ptr.h" |
| 10 | #include "base/sequence_checker.h" |
| 11 | #include "components/reporting/proto/synced/metric_data.pb.h" |
| 12 | #include "third_party/abseil-cpp/absl/types/optional.h" |
| 13 | |
| 14 | namespace reporting { |
| 15 | |
| 16 | class Sampler; |
| 17 | |
| 18 | // A base class for metric data collection. |
| 19 | class CollectorBase { |
| 20 | public: |
| 21 | explicit CollectorBase(Sampler* sampler); |
| 22 | |
| 23 | CollectorBase(const CollectorBase& other) = delete; |
| 24 | CollectorBase& operator=(const CollectorBase& other) = delete; |
| 25 | |
| 26 | virtual ~CollectorBase(); |
| 27 | |
Ahmed Nasr | 8d2c5df5 | 2022-11-11 23:05:08 | [diff] [blame] | 28 | // Collect metric data provided by `sampler_` asynchronously. |
Ahmed Nasr | 201a911 | 2022-11-28 18:02:47 | [diff] [blame] | 29 | virtual void Collect(bool is_event_driven); |
Ahmed Nasr | 8d2c5df5 | 2022-11-11 23:05:08 | [diff] [blame] | 30 | |
Ahmed Nasr | 201a911 | 2022-11-28 18:02:47 | [diff] [blame] | 31 | protected: |
Ahmed Nasr | 8d2c5df5 | 2022-11-11 23:05:08 | [diff] [blame] | 32 | // Callback executed when metric data is collected. |
| 33 | virtual void OnMetricDataCollected( |
Ahmed Nasr | 201a911 | 2022-11-28 18:02:47 | [diff] [blame] | 34 | bool is_event_driven, |
Ahmed Nasr | 8d2c5df5 | 2022-11-11 23:05:08 | [diff] [blame] | 35 | absl::optional<MetricData> metric_data) = 0; |
| 36 | |
| 37 | void CheckOnSequence() const; |
| 38 | |
Ahmed Nasr | 201a911 | 2022-11-28 18:02:47 | [diff] [blame] | 39 | virtual bool CanCollect() const = 0; |
| 40 | |
Ahmed Nasr | 8d2c5df5 | 2022-11-11 23:05:08 | [diff] [blame] | 41 | private: |
| 42 | SEQUENCE_CHECKER(sequence_checker_); |
| 43 | |
| 44 | const raw_ptr<Sampler> sampler_; |
| 45 | |
| 46 | base::WeakPtrFactory<CollectorBase> weak_ptr_factory_{this}; |
| 47 | }; |
| 48 | } // namespace reporting |
| 49 | |
| 50 | #endif // COMPONENTS_REPORTING_METRICS_COLLECTOR_BASE_H_ |