blob: 84d86281a97451ad81849b7d30197ce22979a076 [file] [log] [blame]
Ahmed Nasr8d2c5df52022-11-11 23:05:081// 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
14namespace reporting {
15
16class Sampler;
17
18// A base class for metric data collection.
19class 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 Nasr8d2c5df52022-11-11 23:05:0828 // Collect metric data provided by `sampler_` asynchronously.
Ahmed Nasr201a9112022-11-28 18:02:4729 virtual void Collect(bool is_event_driven);
Ahmed Nasr8d2c5df52022-11-11 23:05:0830
Ahmed Nasr201a9112022-11-28 18:02:4731 protected:
Ahmed Nasr8d2c5df52022-11-11 23:05:0832 // Callback executed when metric data is collected.
33 virtual void OnMetricDataCollected(
Ahmed Nasr201a9112022-11-28 18:02:4734 bool is_event_driven,
Ahmed Nasr8d2c5df52022-11-11 23:05:0835 absl::optional<MetricData> metric_data) = 0;
36
37 void CheckOnSequence() const;
38
Ahmed Nasr201a9112022-11-28 18:02:4739 virtual bool CanCollect() const = 0;
40
Ahmed Nasr8d2c5df52022-11-11 23:05:0841 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_