• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
5 #include "base/metrics/single_sample_metrics.h"
6 
7 #include "base/memory/ptr_util.h"
8 #include "base/metrics/histogram.h"
9 
10 namespace base {
11 
12 static SingleSampleMetricsFactory* g_factory = nullptr;
13 
14 // static
Get()15 SingleSampleMetricsFactory* SingleSampleMetricsFactory::Get() {
16   if (!g_factory)
17     g_factory = new DefaultSingleSampleMetricsFactory();
18 
19   return g_factory;
20 }
21 
22 // static
SetFactory(std::unique_ptr<SingleSampleMetricsFactory> factory)23 void SingleSampleMetricsFactory::SetFactory(
24     std::unique_ptr<SingleSampleMetricsFactory> factory) {
25   DCHECK(!g_factory);
26   g_factory = factory.release();
27 }
28 
29 // static
DeleteFactoryForTesting()30 void SingleSampleMetricsFactory::DeleteFactoryForTesting() {
31   DCHECK(g_factory);
32   delete g_factory;
33   g_factory = nullptr;
34 }
35 
36 std::unique_ptr<SingleSampleMetric>
CreateCustomCountsMetric(const std::string & histogram_name,HistogramBase::Sample min,HistogramBase::Sample max,uint32_t bucket_count)37 DefaultSingleSampleMetricsFactory::CreateCustomCountsMetric(
38     const std::string& histogram_name,
39     HistogramBase::Sample min,
40     HistogramBase::Sample max,
41     uint32_t bucket_count) {
42   return std::make_unique<DefaultSingleSampleMetric>(
43       histogram_name, min, max, bucket_count,
44       HistogramBase::kUmaTargetedHistogramFlag);
45 }
46 
DefaultSingleSampleMetric(const std::string & histogram_name,HistogramBase::Sample min,HistogramBase::Sample max,uint32_t bucket_count,int32_t flags)47 DefaultSingleSampleMetric::DefaultSingleSampleMetric(
48     const std::string& histogram_name,
49     HistogramBase::Sample min,
50     HistogramBase::Sample max,
51     uint32_t bucket_count,
52     int32_t flags)
53     : histogram_(Histogram::FactoryGet(histogram_name,
54                                        min,
55                                        max,
56                                        bucket_count,
57                                        flags)) {
58   // Bad construction parameters may lead to |histogram_| being null; DCHECK to
59   // find accidental errors in production. We must still handle the nullptr in
60   // destruction though since this construction may come from another untrusted
61   // process.
62   DCHECK(histogram_);
63 }
64 
~DefaultSingleSampleMetric()65 DefaultSingleSampleMetric::~DefaultSingleSampleMetric() {
66   // |histogram_| may be nullptr if bad construction parameters are given.
67   if (sample_ < 0 || !histogram_)
68     return;
69   histogram_->Add(sample_);
70 }
71 
SetSample(HistogramBase::Sample sample)72 void DefaultSingleSampleMetric::SetSample(HistogramBase::Sample sample) {
73   DCHECK_GE(sample, 0);
74   sample_ = sample;
75 }
76 
77 }  // namespace base
78