xref: /aosp_15_r20/external/cronet/components/metrics/library_support/histogram_manager.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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_LIBRARY_SUPPORT_HISTOGRAM_MANAGER_H_
6 #define COMPONENTS_METRICS_LIBRARY_SUPPORT_HISTOGRAM_MANAGER_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/lazy_instance.h"
14 #include "base/metrics/histogram_flattener.h"
15 #include "base/metrics/histogram_snapshot_manager.h"
16 #include "base/synchronization/lock.h"
17 #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
18 
19 namespace metrics {
20 
21 // A HistogramManager instance is created by the app. It is the central
22 // controller for the acquisition of log data, and recording deltas for
23 // transmission to an external server. Public APIs are all thread-safe.
24 class HistogramManager : public base::HistogramFlattener {
25  public:
26   HistogramManager();
27 
28   HistogramManager(const HistogramManager&) = delete;
29   HistogramManager& operator=(const HistogramManager&) = delete;
30 
31   ~HistogramManager() override;
32 
33   // Snapshot all histograms to record the delta into |uma_proto_| and then
34   // returns the serialized protobuf representation of the record in |data|.
35   // Returns true if it was successfully serialized.
36   bool GetDeltas(std::vector<uint8_t>* data);
37 
38   // TODO(mef): Hang Histogram Manager off java object instead of singleton.
39   static HistogramManager* GetInstance();
40 
41  private:
42   friend struct base::LazyInstanceTraitsBase<HistogramManager>;
43 
44   // base::HistogramFlattener:
45   void RecordDelta(const base::HistogramBase& histogram,
46                    const base::HistogramSamples& snapshot) override;
47 
48   base::HistogramSnapshotManager histogram_snapshot_manager_;
49 
50   // Stores the protocol buffer representation for this log.
51   metrics::ChromeUserMetricsExtension uma_proto_;
52 
53   // Should be acquired whenever GetDeltas() is executing to maintain
54   // thread-safety.
55   base::Lock get_deltas_lock_;
56 };
57 
58 }  // namespace metrics
59 
60 #endif  // COMPONENTS_METRICS_LIBRARY_SUPPORT_HISTOGRAM_MANAGER_H_
61