• 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.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/metrics/single_sample_metrics.h"
11 #include "base/threading/thread_checker.h"
12 #include "components/metrics/single_sample_metrics_factory_impl.h"
13 #include "mojo/public/cpp/bindings/pending_receiver.h"
14 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
15 
16 namespace metrics {
17 namespace {
18 
19 class MojoSingleSampleMetric : public mojom::SingleSampleMetric {
20  public:
MojoSingleSampleMetric(const std::string & histogram_name,base::HistogramBase::Sample min,base::HistogramBase::Sample max,uint32_t bucket_count,int32_t flags)21   MojoSingleSampleMetric(const std::string& histogram_name,
22                          base::HistogramBase::Sample min,
23                          base::HistogramBase::Sample max,
24                          uint32_t bucket_count,
25                          int32_t flags)
26       : metric_(histogram_name, min, max, bucket_count, flags) {}
27 
28   MojoSingleSampleMetric(const MojoSingleSampleMetric&) = delete;
29   MojoSingleSampleMetric& operator=(const MojoSingleSampleMetric&) = delete;
30 
~MojoSingleSampleMetric()31   ~MojoSingleSampleMetric() override {}
32 
33  private:
34   // mojom::SingleSampleMetric:
SetSample(base::HistogramBase::Sample sample)35   void SetSample(base::HistogramBase::Sample sample) override {
36     metric_.SetSample(sample);
37   }
38 
39   base::DefaultSingleSampleMetric metric_;
40 };
41 
42 class MojoSingleSampleMetricsProvider
43     : public mojom::SingleSampleMetricsProvider {
44  public:
MojoSingleSampleMetricsProvider()45   MojoSingleSampleMetricsProvider() {}
46 
47   MojoSingleSampleMetricsProvider(const MojoSingleSampleMetricsProvider&) =
48       delete;
49   MojoSingleSampleMetricsProvider& operator=(
50       const MojoSingleSampleMetricsProvider&) = delete;
51 
~MojoSingleSampleMetricsProvider()52   ~MojoSingleSampleMetricsProvider() override {
53     DCHECK(thread_checker_.CalledOnValidThread());
54   }
55 
56  private:
57   // mojom::SingleSampleMetricsProvider:
AcquireSingleSampleMetric(const std::string & histogram_name,base::HistogramBase::Sample min,base::HistogramBase::Sample max,uint32_t bucket_count,int32_t flags,mojo::PendingReceiver<mojom::SingleSampleMetric> receiver)58   void AcquireSingleSampleMetric(
59       const std::string& histogram_name,
60       base::HistogramBase::Sample min,
61       base::HistogramBase::Sample max,
62       uint32_t bucket_count,
63       int32_t flags,
64       mojo::PendingReceiver<mojom::SingleSampleMetric> receiver) override {
65     DCHECK(thread_checker_.CalledOnValidThread());
66     mojo::MakeSelfOwnedReceiver(
67         std::make_unique<MojoSingleSampleMetric>(histogram_name, min, max,
68                                                  bucket_count, flags),
69         std::move(receiver));
70   }
71 
72   // Providers must be created, used on, and destroyed on the same thread.
73   base::ThreadChecker thread_checker_;
74 };
75 
76 }  // namespace
77 
78 // static
InitializeSingleSampleMetricsFactory(CreateProviderCB create_provider_cb)79 void InitializeSingleSampleMetricsFactory(CreateProviderCB create_provider_cb) {
80   base::SingleSampleMetricsFactory::SetFactory(
81       std::make_unique<SingleSampleMetricsFactoryImpl>(
82           std::move(create_provider_cb)));
83 }
84 
85 // static
CreateSingleSampleMetricsProvider(mojo::PendingReceiver<mojom::SingleSampleMetricsProvider> receiver)86 void CreateSingleSampleMetricsProvider(
87     mojo::PendingReceiver<mojom::SingleSampleMetricsProvider> receiver) {
88   mojo::MakeSelfOwnedReceiver(
89       std::make_unique<MojoSingleSampleMetricsProvider>(), std::move(receiver));
90 }
91 
92 }  // namespace metrics
93