1 // Copyright 2017 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_METRICS_SINGLE_SAMPLE_METRICS_FACTORY_IMPL_H_ 6 #define COMPONENTS_METRICS_SINGLE_SAMPLE_METRICS_FACTORY_IMPL_H_ 7 8 #include <string> 9 10 #include "base/metrics/single_sample_metrics.h" 11 #include "components/metrics/public/mojom/single_sample_metrics.mojom.h" 12 #include "components/metrics/single_sample_metrics.h" 13 14 namespace metrics { 15 16 // SingleSampleMetricsFactory implementation for creating SingleSampleMetric 17 // instances that communicate over mojo to instances in another process. 18 // 19 // Persistance outside of the current process allows these metrics to record a 20 // sample even in the event of sudden process termination. As an example, this 21 // is useful for garbage collected objects which may never get a chance to run 22 // their destructors in the event of a fast shutdown event (process kill). 23 class SingleSampleMetricsFactoryImpl : public base::SingleSampleMetricsFactory { 24 public: 25 // Constructs a factory capable of vending single sample metrics from any 26 // thread. |create_provider_cb| will be called from arbitrary threads to 27 // create providers as necessary; the callback must handle thread safety. 28 // 29 // We use a callback here to avoid taking additional DEPS on content and a 30 // service_manager::Connector() for simplicitly and to avoid the need for 31 // using the service test harness just for instantiating this class. 32 explicit SingleSampleMetricsFactoryImpl(CreateProviderCB create_provider_cb); 33 34 SingleSampleMetricsFactoryImpl(const SingleSampleMetricsFactoryImpl&) = 35 delete; 36 SingleSampleMetricsFactoryImpl& operator=( 37 const SingleSampleMetricsFactoryImpl&) = delete; 38 39 ~SingleSampleMetricsFactoryImpl() override; 40 41 // base::SingleSampleMetricsFactory: 42 std::unique_ptr<base::SingleSampleMetric> CreateCustomCountsMetric( 43 const std::string& histogram_name, 44 base::HistogramBase::Sample min, 45 base::HistogramBase::Sample max, 46 uint32_t bucket_count) override; 47 48 // Providers live forever in production, but tests should be kind and clean up 49 // after themselves to avoid tests trampling on one another. Destroys the 50 // provider in the TLS slot for the calling thread. 51 void DestroyProviderForTesting(); 52 53 private: 54 // Creates a single sample metric. 55 std::unique_ptr<base::SingleSampleMetric> CreateMetric( 56 const std::string& histogram_name, 57 base::HistogramBase::Sample min, 58 base::HistogramBase::Sample max, 59 uint32_t bucket_count, 60 int32_t flags); 61 62 // Gets the SingleSampleMetricsProvider for the current thread. If none 63 // exists, then a new instance is created and set in the TLS slot. 64 mojom::SingleSampleMetricsProvider* GetProvider(); 65 66 CreateProviderCB create_provider_cb_; 67 }; 68 69 } // namespace metrics 70 71 #endif // COMPONENTS_METRICS_SINGLE_SAMPLE_METRICS_FACTORY_IMPL_H_ 72