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