• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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_STRUCTURED_STRUCTURED_METRICS_PROVIDER_H_
6 #define COMPONENTS_METRICS_STRUCTURED_STRUCTURED_METRICS_PROVIDER_H_
7 
8 #include "base/memory/weak_ptr.h"
9 #include "base/time/time.h"
10 #include "components/metrics/metrics_provider.h"
11 #include "components/metrics/structured/structured_metrics_recorder.h"
12 
13 namespace metrics::structured {
14 
15 // StructuredMetricsProvider is responsible for filling out the
16 // |structured_metrics_event| section of the UMA proto. This class should not be
17 // instantiated except by the ChromeMetricsServiceClient. This class is not
18 // thread safe and should only be called on the browser UI sequence, because
19 // calls from the metrics service come on the UI sequence.
20 //
21 // On a call to ProvideCurrentSessionData, the cache of unsent logs is added to
22 // a ChromeUserMetricsExtension for upload, and is then cleared.
23 class StructuredMetricsProvider final : public metrics::MetricsProvider {
24  public:
25   explicit StructuredMetricsProvider(
26       raw_ptr<StructuredMetricsRecorder> structured_metrics_recorder);
27   ~StructuredMetricsProvider() override;
28   StructuredMetricsProvider(const StructuredMetricsProvider&) = delete;
29   StructuredMetricsProvider& operator=(const StructuredMetricsProvider&) =
30       delete;
31 
recorder()32   StructuredMetricsRecorder& recorder() {
33     return *structured_metrics_recorder_;
34   }
35 
36  private:
37   friend class StructuredMetricsProviderTest;
38   friend class TestStructuredMetricsProvider;
39 
40   // Should only be used for tests.
41   //
42   // TODO(crbug/1350322): Use this ctor to replace existing ctor.
43   StructuredMetricsProvider(
44       base::TimeDelta min_independent_metrics_interval,
45       raw_ptr<StructuredMetricsRecorder> structured_metrics_recorder);
46 
47   // Removes all in-memory and on-disk events.
48   void Purge();
49 
50   // metrics::MetricsProvider:
51   void OnRecordingEnabled() override;
52   void OnRecordingDisabled() override;
53   void ProvideCurrentSessionData(
54       metrics::ChromeUserMetricsExtension* uma_proto) override;
55   bool HasIndependentMetrics() override;
56   void ProvideIndependentMetrics(base::OnceClosure serialize_log_callback,
57                                  base::OnceCallback<void(bool)> done_callback,
58                                  ChromeUserMetricsExtension* uma_proto,
59                                  base::HistogramSnapshotManager*) override;
60 
61   // Tracks the recording state of the StructuredMetricsRecorder.
62   bool recording_enabled_ = false;
63 
64   // The last time we provided independent metrics.
65   base::Time last_provided_independent_metrics_;
66 
67   // The minimum waiting time between successive deliveries of independent
68   // metrics to the metrics service via ProvideIndependentMetrics. This is set
69   // carefully: metrics logs are stored in a queue of limited size, and are
70   // uploaded roughly every 30 minutes.
71   //
72   // If this value is 0, then there will be no waiting time and events will be
73   // available on every ProvideIndependentMetrics.
74   base::TimeDelta min_independent_metrics_interval_;
75 
76   raw_ptr<StructuredMetricsRecorder> structured_metrics_recorder_;
77 
78   base::WeakPtrFactory<StructuredMetricsProvider> weak_factory_{this};
79 };
80 
81 }  // namespace metrics::structured
82 
83 #endif  // COMPONENTS_METRICS_STRUCTURED_STRUCTURED_METRICS_PROVIDER_H_
84