blob: 4d055855fb695879705562dba800d8793b6d0488 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2017 The Chromium Authors
dalecurtis4a9839a2017-05-04 23:40:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lei Zhang2fdfa8922021-04-22 02:15:435#ifndef COMPONENTS_METRICS_SINGLE_SAMPLE_METRICS_FACTORY_IMPL_H_
6#define COMPONENTS_METRICS_SINGLE_SAMPLE_METRICS_FACTORY_IMPL_H_
dalecurtis4a9839a2017-05-04 23:40:477
8#include <string>
9
10#include "base/metrics/single_sample_metrics.h"
Miyoung Shin4ccc37cf2019-07-27 03:45:0211#include "components/metrics/public/mojom/single_sample_metrics.mojom.h"
dalecurtis4a9839a2017-05-04 23:40:4712#include "components/metrics/single_sample_metrics.h"
13
14namespace metrics {
15
16// SingleSampleMetricsFactory implementation for creating SingleSampleMetric
17// instances that communicate over mojo to instances in another process.
18//
19// Persistance outside of the current process allows these metrics to record a
20// sample even in the event of sudden process termination. As an example, this
21// is useful for garbage collected objects which may never get a chance to run
22// their destructors in the event of a fast shutdown event (process kill).
23class SingleSampleMetricsFactoryImpl : public base::SingleSampleMetricsFactory {
24 public:
25 // Constructs a factory capable of vending single sample metrics from any
26 // thread. |create_provider_cb| will be called from arbitrary threads to
27 // create providers as necessary; the callback must handle thread safety.
28 //
29 // We use a callback here to avoid taking additional DEPS on content and a
30 // service_manager::Connector() for simplicitly and to avoid the need for
31 // using the service test harness just for instantiating this class.
32 explicit SingleSampleMetricsFactoryImpl(CreateProviderCB create_provider_cb);
Peter Boström09c01822021-09-20 22:43:2733
34 SingleSampleMetricsFactoryImpl(const SingleSampleMetricsFactoryImpl&) =
35 delete;
36 SingleSampleMetricsFactoryImpl& operator=(
37 const SingleSampleMetricsFactoryImpl&) = delete;
38
dalecurtis4a9839a2017-05-04 23:40:4739 ~SingleSampleMetricsFactoryImpl() override;
40
41 // base::SingleSampleMetricsFactory:
42 std::unique_ptr<base::SingleSampleMetric> CreateCustomCountsMetric(
43 const std::string& histogram_name,
Ramon Cano Aparicio9d696372025-01-16 17:56:2344 base::HistogramBase::Sample32 min,
45 base::HistogramBase::Sample32 max,
dalecurtis4a9839a2017-05-04 23:40:4746 uint32_t bucket_count) override;
47
48 // Providers live forever in production, but tests should be kind and clean up
49 // after themselves to avoid tests trampling on one another. Destroys the
50 // provider in the TLS slot for the calling thread.
51 void DestroyProviderForTesting();
52
53 private:
54 // Creates a single sample metric.
55 std::unique_ptr<base::SingleSampleMetric> CreateMetric(
56 const std::string& histogram_name,
Ramon Cano Aparicio9d696372025-01-16 17:56:2357 base::HistogramBase::Sample32 min,
58 base::HistogramBase::Sample32 max,
dalecurtis4a9839a2017-05-04 23:40:4759 uint32_t bucket_count,
60 int32_t flags);
61
62 // Gets the SingleSampleMetricsProvider for the current thread. If none
63 // exists, then a new instance is created and set in the TLS slot.
64 mojom::SingleSampleMetricsProvider* GetProvider();
65
66 CreateProviderCB create_provider_cb_;
dalecurtis4a9839a2017-05-04 23:40:4767};
68
69} // namespace metrics
70
Lei Zhang2fdfa8922021-04-22 02:15:4371#endif // COMPONENTS_METRICS_SINGLE_SAMPLE_METRICS_FACTORY_IMPL_H_