blob: 0570f5ba03ce6f0415f919b67d69ad2f41a96b56 [file] [log] [blame]
dalecurtis4a9839a2017-05-04 23:40:471// Copyright 2017 The Chromium Authors. All rights reserved.
2// 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"
11#include "base/threading/thread_local.h"
Miyoung Shin4ccc37cf2019-07-27 03:45:0212#include "components/metrics/public/mojom/single_sample_metrics.mojom.h"
dalecurtis4a9839a2017-05-04 23:40:4713#include "components/metrics/single_sample_metrics.h"
Julie Jeongeun Kim2ee1de49b2019-09-21 11:50:3114#include "mojo/public/cpp/bindings/remote.h"
dalecurtis4a9839a2017-05-04 23:40:4715
16namespace metrics {
17
18// SingleSampleMetricsFactory implementation for creating SingleSampleMetric
19// instances that communicate over mojo to instances in another process.
20//
21// Persistance outside of the current process allows these metrics to record a
22// sample even in the event of sudden process termination. As an example, this
23// is useful for garbage collected objects which may never get a chance to run
24// their destructors in the event of a fast shutdown event (process kill).
25class SingleSampleMetricsFactoryImpl : public base::SingleSampleMetricsFactory {
26 public:
27 // Constructs a factory capable of vending single sample metrics from any
28 // thread. |create_provider_cb| will be called from arbitrary threads to
29 // create providers as necessary; the callback must handle thread safety.
30 //
31 // We use a callback here to avoid taking additional DEPS on content and a
32 // service_manager::Connector() for simplicitly and to avoid the need for
33 // using the service test harness just for instantiating this class.
34 explicit SingleSampleMetricsFactoryImpl(CreateProviderCB create_provider_cb);
Peter Boström09c01822021-09-20 22:43:2735
36 SingleSampleMetricsFactoryImpl(const SingleSampleMetricsFactoryImpl&) =
37 delete;
38 SingleSampleMetricsFactoryImpl& operator=(
39 const SingleSampleMetricsFactoryImpl&) = delete;
40
dalecurtis4a9839a2017-05-04 23:40:4741 ~SingleSampleMetricsFactoryImpl() override;
42
43 // base::SingleSampleMetricsFactory:
44 std::unique_ptr<base::SingleSampleMetric> CreateCustomCountsMetric(
45 const std::string& histogram_name,
46 base::HistogramBase::Sample min,
47 base::HistogramBase::Sample max,
48 uint32_t bucket_count) override;
49
50 // Providers live forever in production, but tests should be kind and clean up
51 // after themselves to avoid tests trampling on one another. Destroys the
52 // provider in the TLS slot for the calling thread.
53 void DestroyProviderForTesting();
54
55 private:
56 // Creates a single sample metric.
57 std::unique_ptr<base::SingleSampleMetric> CreateMetric(
58 const std::string& histogram_name,
59 base::HistogramBase::Sample min,
60 base::HistogramBase::Sample max,
61 uint32_t bucket_count,
62 int32_t flags);
63
64 // Gets the SingleSampleMetricsProvider for the current thread. If none
65 // exists, then a new instance is created and set in the TLS slot.
66 mojom::SingleSampleMetricsProvider* GetProvider();
67
68 CreateProviderCB create_provider_cb_;
69
70 // Per thread storage slot for the mojo provider.
Julie Jeongeun Kim2ee1de49b2019-09-21 11:50:3171 base::ThreadLocalPointer<mojo::Remote<mojom::SingleSampleMetricsProvider>>
72 provider_tls_;
dalecurtis4a9839a2017-05-04 23:40:4773};
74
75} // namespace metrics
76
Lei Zhang2fdfa8922021-04-22 02:15:4377#endif // COMPONENTS_METRICS_SINGLE_SAMPLE_METRICS_FACTORY_IMPL_H_