| Index: components/metrics/single_value_histograms_factory_impl.cc
|
| diff --git a/components/metrics/single_value_histograms_factory_impl.cc b/components/metrics/single_value_histograms_factory_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2dc015b9d2a893d6a760623dd116819bd62c1cba
|
| --- /dev/null
|
| +++ b/components/metrics/single_value_histograms_factory_impl.cc
|
| @@ -0,0 +1,113 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "components/metrics/single_value_histograms_factory_impl.h"
|
| +
|
| +#include "base/single_thread_task_runner.h"
|
| +#include "base/threading/thread_checker.h"
|
| +#include "content/public/common/service_names.mojom.h"
|
| +#include "services/service_manager/public/cpp/connector.h"
|
| +
|
| +namespace metrics {
|
| +
|
| +// Helper class which ensures we have a provider which is safe to use on the
|
| +// current thread. It trampolines to the thread the service_manager::Connector()
|
| +// in order to create a SingleValueHistogramsProvider.
|
| +class SingleValueHistogramsProviderTrampoline {
|
| + public:
|
| + SingleValueHistogramsProviderTrampoline(
|
| + service_manager::Connector* connector,
|
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
|
| + if (task_runner->BelongsToCurrentThread()) {
|
| + BindOnCorrectThread(connector, mojo::MakeRequest(&provider_));
|
| + return;
|
| + }
|
| +
|
| + task_runner->PostTask(
|
| + FROM_HERE,
|
| + base::BindOnce(
|
| + &SingleValueHistogramsProviderTrampoline::BindOnCorrectThread,
|
| + base::Unretained(this), connector, mojo::MakeRequest(&provider_)));
|
| + }
|
| +
|
| + ~SingleValueHistogramsProviderTrampoline() {
|
| + // This trampoline persists until process shutdown.
|
| + NOTREACHED();
|
| + }
|
| +
|
| + mojom::SingleValueHistogramsProvider* GetProvider() {
|
| + return provider_.get();
|
| + }
|
| +
|
| + private:
|
| + void BindOnCorrectThread(
|
| + service_manager::Connector* connector,
|
| + mojom::SingleValueHistogramsProviderRequest request) {
|
| + connector->BindInterface(content::mojom::kBrowserServiceName,
|
| + std::move(request));
|
| + }
|
| +
|
| + mojom::SingleValueHistogramsProviderPtr provider_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(SingleValueHistogramsProviderTrampoline);
|
| +};
|
| +
|
| +// Implementation of the single value counts histogram variant.
|
| +class SingleValueCountsHistogramImpl : public base::SingleValueCountsHistogram {
|
| + public:
|
| + SingleValueCountsHistogramImpl(mojom::SingleValueCountsHistogramPtr metric)
|
| + : metric_(std::move(metric)) {}
|
| +
|
| + ~SingleValueCountsHistogramImpl() override {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + }
|
| +
|
| + void SetSample(base::HistogramBase::Sample sample) override {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + metric_->SetSample(sample);
|
| + }
|
| +
|
| + private:
|
| + base::ThreadChecker thread_checker_;
|
| + mojom::SingleValueCountsHistogramPtr metric_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(SingleValueCountsHistogramImpl);
|
| +};
|
| +
|
| +SingleValueHistogramsFactoryImpl::SingleValueHistogramsFactoryImpl(
|
| + service_manager::Connector* connector,
|
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner)
|
| + : connector_(connector), task_runner_(std::move(task_runner)) {}
|
| +
|
| +SingleValueHistogramsFactoryImpl::~SingleValueHistogramsFactoryImpl() {
|
| + // This factory persists until process shutdown.
|
| + NOTREACHED();
|
| +}
|
| +
|
| +std::unique_ptr<base::SingleValueCountsHistogram>
|
| +SingleValueHistogramsFactoryImpl::CreateSingleValueCountsHistogram(
|
| + const std::string& name,
|
| + base::HistogramBase::Sample min,
|
| + base::HistogramBase::Sample max,
|
| + uint32_t bucket_count) {
|
| + mojom::SingleValueCountsHistogramPtr metric;
|
| + GetProvider()->AcquireSingleValueCountsHistogram(
|
| + name, min, max, bucket_count,
|
| + mojo::MakeRequest<mojom::SingleValueCountsHistogram>(&metric));
|
| + return base::MakeUnique<SingleValueCountsHistogramImpl>(std::move(metric));
|
| +}
|
| +
|
| +mojom::SingleValueHistogramsProvider*
|
| +SingleValueHistogramsFactoryImpl::GetProvider() {
|
| + if (auto* provider = provider_.Get())
|
| + return provider->GetProvider();
|
| +
|
| + // Provider trampolines persist per-thread until process shutdown.
|
| + SingleValueHistogramsProviderTrampoline* provider =
|
| + new SingleValueHistogramsProviderTrampoline(connector_, task_runner_);
|
| + provider_.Set(provider);
|
| + return provider->GetProvider();
|
| +}
|
| +
|
| +} // namespace metrics
|
|
|