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_value_histograms_factory_impl.h" |
| 6 |
| 7 #include "base/single_thread_task_runner.h" |
| 8 #include "base/threading/thread_checker.h" |
| 9 #include "content/public/common/service_names.mojom.h" |
| 10 #include "services/service_manager/public/cpp/connector.h" |
| 11 |
| 12 namespace metrics { |
| 13 |
| 14 // Helper class which ensures we have a provider which is safe to use on the |
| 15 // current thread. It trampolines to the thread the service_manager::Connector() |
| 16 // in order to create a SingleValueHistogramsProvider. |
| 17 class SingleValueHistogramsProviderTrampoline { |
| 18 public: |
| 19 SingleValueHistogramsProviderTrampoline( |
| 20 service_manager::Connector* connector, |
| 21 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 22 if (task_runner->BelongsToCurrentThread()) { |
| 23 BindOnCorrectThread(connector, mojo::MakeRequest(&provider_)); |
| 24 return; |
| 25 } |
| 26 |
| 27 task_runner->PostTask( |
| 28 FROM_HERE, |
| 29 base::BindOnce( |
| 30 &SingleValueHistogramsProviderTrampoline::BindOnCorrectThread, |
| 31 base::Unretained(this), connector, mojo::MakeRequest(&provider_))); |
| 32 } |
| 33 |
| 34 ~SingleValueHistogramsProviderTrampoline() { |
| 35 // This trampoline persists until process shutdown. |
| 36 NOTREACHED(); |
| 37 } |
| 38 |
| 39 mojom::SingleValueHistogramsProvider* GetProvider() { |
| 40 return provider_.get(); |
| 41 } |
| 42 |
| 43 private: |
| 44 void BindOnCorrectThread( |
| 45 service_manager::Connector* connector, |
| 46 mojom::SingleValueHistogramsProviderRequest request) { |
| 47 connector->BindInterface(content::mojom::kBrowserServiceName, |
| 48 std::move(request)); |
| 49 } |
| 50 |
| 51 mojom::SingleValueHistogramsProviderPtr provider_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(SingleValueHistogramsProviderTrampoline); |
| 54 }; |
| 55 |
| 56 // Implementation of the single value counts histogram variant. |
| 57 class SingleValueCountsHistogramImpl : public base::SingleValueCountsHistogram { |
| 58 public: |
| 59 SingleValueCountsHistogramImpl(mojom::SingleValueCountsHistogramPtr metric) |
| 60 : metric_(std::move(metric)) {} |
| 61 |
| 62 ~SingleValueCountsHistogramImpl() override { |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 } |
| 65 |
| 66 void SetSample(base::HistogramBase::Sample sample) override { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 metric_->SetSample(sample); |
| 69 } |
| 70 |
| 71 private: |
| 72 base::ThreadChecker thread_checker_; |
| 73 mojom::SingleValueCountsHistogramPtr metric_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(SingleValueCountsHistogramImpl); |
| 76 }; |
| 77 |
| 78 SingleValueHistogramsFactoryImpl::SingleValueHistogramsFactoryImpl( |
| 79 service_manager::Connector* connector, |
| 80 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 81 : connector_(connector), task_runner_(std::move(task_runner)) {} |
| 82 |
| 83 SingleValueHistogramsFactoryImpl::~SingleValueHistogramsFactoryImpl() { |
| 84 // This factory persists until process shutdown. |
| 85 NOTREACHED(); |
| 86 } |
| 87 |
| 88 std::unique_ptr<base::SingleValueCountsHistogram> |
| 89 SingleValueHistogramsFactoryImpl::CreateSingleValueCountsHistogram( |
| 90 const std::string& name, |
| 91 base::HistogramBase::Sample min, |
| 92 base::HistogramBase::Sample max, |
| 93 uint32_t bucket_count) { |
| 94 mojom::SingleValueCountsHistogramPtr metric; |
| 95 GetProvider()->AcquireSingleValueCountsHistogram( |
| 96 name, min, max, bucket_count, |
| 97 mojo::MakeRequest<mojom::SingleValueCountsHistogram>(&metric)); |
| 98 return base::MakeUnique<SingleValueCountsHistogramImpl>(std::move(metric)); |
| 99 } |
| 100 |
| 101 mojom::SingleValueHistogramsProvider* |
| 102 SingleValueHistogramsFactoryImpl::GetProvider() { |
| 103 if (auto* provider = provider_.Get()) |
| 104 return provider->GetProvider(); |
| 105 |
| 106 // Provider trampolines persist per-thread until process shutdown. |
| 107 SingleValueHistogramsProviderTrampoline* provider = |
| 108 new SingleValueHistogramsProviderTrampoline(connector_, task_runner_); |
| 109 provider_.Set(provider); |
| 110 return provider->GetProvider(); |
| 111 } |
| 112 |
| 113 } // namespace metrics |
OLD | NEW |