blob: 166cce254c1e1eacfbd3fad1ddc6e566a3b25d3b [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
28 protected:
29 // Collect metric data provided by `sampler_` asynchronously.
30 virtual void Collect();
31
32 // Callback executed when metric data is collected.
33 virtual void OnMetricDataCollected(
34 absl::optional<MetricData> metric_data) = 0;
35
36 void CheckOnSequence() const;
37
38 private:
39 SEQUENCE_CHECKER(sequence_checker_);
40
41 const raw_ptr<Sampler> sampler_;
42
43 base::WeakPtrFactory<CollectorBase> weak_ptr_factory_{this};
44};
45} // namespace reporting
46
47#endif // COMPONENTS_REPORTING_METRICS_COLLECTOR_BASE_H_