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