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 class SingleSampleMetricImpl : public base::SingleSampleMetric { |
| 12 public: |
| 13 SingleSampleMetricImpl(mojom::SingleSampleMetricPtr metric) |
| 14 : metric_(std::move(metric)) {} |
| 15 |
| 16 ~SingleSampleMetricImpl() override { |
| 17 DCHECK(thread_checker_.CalledOnValidThread()); |
| 18 } |
| 19 |
| 20 void SetSample(base::HistogramBase::Sample sample) override { |
| 21 DCHECK(thread_checker_.CalledOnValidThread()); |
| 22 metric_->SetSample(sample); |
| 23 } |
| 24 |
| 25 private: |
| 26 base::ThreadChecker thread_checker_; |
| 27 mojom::SingleSampleMetricPtr metric_; |
| 28 |
| 29 DISALLOW_COPY_AND_ASSIGN(SingleSampleMetricImpl); |
| 30 }; |
| 31 |
| 32 SingleSampleMetricsFactoryImpl::SingleSampleMetricsFactoryImpl( |
| 33 CreateProviderCB create_provider_cb) |
| 34 : create_provider_cb_(std::move(create_provider_cb)) {} |
| 35 |
| 36 SingleSampleMetricsFactoryImpl::~SingleSampleMetricsFactoryImpl() {} |
| 37 |
| 38 std::unique_ptr<base::SingleSampleMetric> |
| 39 SingleSampleMetricsFactoryImpl::CreateCustomCountsMetric( |
| 40 const std::string& name, |
| 41 base::HistogramBase::Sample min, |
| 42 base::HistogramBase::Sample max, |
| 43 uint32_t bucket_count) { |
| 44 return CreateMetric(name, min, max, bucket_count, |
| 45 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 46 } |
| 47 |
| 48 void SingleSampleMetricsFactoryImpl::DestroyProviderForTesting() { |
| 49 if (auto* provider = provider_.Get()) |
| 50 delete provider; |
| 51 provider_.Set(nullptr); |
| 52 } |
| 53 |
| 54 std::unique_ptr<base::SingleSampleMetric> |
| 55 SingleSampleMetricsFactoryImpl::CreateMetric(const std::string& name, |
| 56 base::HistogramBase::Sample min, |
| 57 base::HistogramBase::Sample max, |
| 58 uint32_t bucket_count, |
| 59 int32_t flags) { |
| 60 mojom::SingleSampleMetricPtr metric; |
| 61 GetProvider()->AcquireSingleSampleMetric( |
| 62 name, min, max, bucket_count, flags, |
| 63 mojo::MakeRequest<mojom::SingleSampleMetric>(&metric)); |
| 64 return base::MakeUnique<SingleSampleMetricImpl>(std::move(metric)); |
| 65 } |
| 66 |
| 67 mojom::SingleSampleMetricsProvider* |
| 68 SingleSampleMetricsFactoryImpl::GetProvider() { |
| 69 if (auto* provider = provider_.Get()) |
| 70 return provider->get(); |
| 71 |
| 72 // Providers persist per-thread until process shutdown. |
| 73 mojom::SingleSampleMetricsProviderPtr* provider = |
| 74 new mojom::SingleSampleMetricsProviderPtr(); |
| 75 provider_.Set(provider); |
| 76 |
| 77 // Create a provider for the current thread. |
| 78 create_provider_cb_.Run(mojo::MakeRequest(provider)); |
| 79 return provider->get(); |
| 80 } |
| 81 |
| 82 } // namespace metrics |
OLD | NEW |