• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 #include "components/metrics/single_sample_metrics_factory_impl.h"
6 
7 #include <memory>
8 
9 #include "base/threading/thread_checker.h"
10 #include "mojo/public/cpp/bindings/pending_remote.h"
11 #include "mojo/public/cpp/bindings/remote.h"
12 
13 namespace metrics {
14 
15 namespace {
16 
17 class SingleSampleMetricImpl : public base::SingleSampleMetric {
18  public:
SingleSampleMetricImpl(mojo::PendingRemote<mojom::SingleSampleMetric> metric)19   SingleSampleMetricImpl(mojo::PendingRemote<mojom::SingleSampleMetric> metric)
20       : metric_(std::move(metric)) {}
21 
22   SingleSampleMetricImpl(const SingleSampleMetricImpl&) = delete;
23   SingleSampleMetricImpl& operator=(const SingleSampleMetricImpl&) = delete;
24 
~SingleSampleMetricImpl()25   ~SingleSampleMetricImpl() override {
26     DCHECK(thread_checker_.CalledOnValidThread());
27   }
28 
SetSample(base::HistogramBase::Sample sample)29   void SetSample(base::HistogramBase::Sample sample) override {
30     DCHECK(thread_checker_.CalledOnValidThread());
31     metric_->SetSample(sample);
32   }
33 
34  private:
35   base::ThreadChecker thread_checker_;
36   mojo::Remote<mojom::SingleSampleMetric> metric_;
37 };
38 
39 constinit thread_local mojo::Remote<mojom::SingleSampleMetricsProvider>*
40     provider = nullptr;
41 
42 }  // namespace
43 
SingleSampleMetricsFactoryImpl(CreateProviderCB create_provider_cb)44 SingleSampleMetricsFactoryImpl::SingleSampleMetricsFactoryImpl(
45     CreateProviderCB create_provider_cb)
46     : create_provider_cb_(std::move(create_provider_cb)) {}
47 
48 SingleSampleMetricsFactoryImpl::~SingleSampleMetricsFactoryImpl() = default;
49 
50 std::unique_ptr<base::SingleSampleMetric>
CreateCustomCountsMetric(const std::string & histogram_name,base::HistogramBase::Sample min,base::HistogramBase::Sample max,uint32_t bucket_count)51 SingleSampleMetricsFactoryImpl::CreateCustomCountsMetric(
52     const std::string& histogram_name,
53     base::HistogramBase::Sample min,
54     base::HistogramBase::Sample max,
55     uint32_t bucket_count) {
56   return CreateMetric(histogram_name, min, max, bucket_count,
57                       base::HistogramBase::kUmaTargetedHistogramFlag);
58 }
59 
DestroyProviderForTesting()60 void SingleSampleMetricsFactoryImpl::DestroyProviderForTesting() {
61   delete provider;
62   provider = nullptr;
63 }
64 
65 std::unique_ptr<base::SingleSampleMetric>
CreateMetric(const std::string & histogram_name,base::HistogramBase::Sample min,base::HistogramBase::Sample max,uint32_t bucket_count,int32_t flags)66 SingleSampleMetricsFactoryImpl::CreateMetric(const std::string& histogram_name,
67                                              base::HistogramBase::Sample min,
68                                              base::HistogramBase::Sample max,
69                                              uint32_t bucket_count,
70                                              int32_t flags) {
71   mojo::PendingRemote<mojom::SingleSampleMetric> metric;
72   GetProvider()->AcquireSingleSampleMetric(
73       histogram_name, min, max, bucket_count, flags,
74       metric.InitWithNewPipeAndPassReceiver());
75   return std::make_unique<SingleSampleMetricImpl>(std::move(metric));
76 }
77 
78 mojom::SingleSampleMetricsProvider*
GetProvider()79 SingleSampleMetricsFactoryImpl::GetProvider() {
80   // Check the current TLS slot to see if we have created a provider already for
81   // this thread.
82   if (!provider) {
83     // If not, create a new one which will persist until process shutdown and
84     // put it in the TLS slot for the current thread.
85     provider = new mojo::Remote<mojom::SingleSampleMetricsProvider>();
86 
87     // Start the provider connection and return it; it won't be fully connected
88     // until later, but mojo will buffer all calls prior to completion.
89     create_provider_cb_.Run(provider->BindNewPipeAndPassReceiver());
90   }
91   return provider->get();
92 }
93 
94 }  // namespace metrics
95