• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 BASE_METRICS_RANGES_MANAGER_H_
6 #define BASE_METRICS_RANGES_MANAGER_H_
7 
8 #include <unordered_set>
9 #include "base/base_export.h"
10 #include "base/metrics/bucket_ranges.h"
11 
12 namespace base {
13 
14 // Manages BucketRanges and their lifetime. When registering a BucketRanges
15 // to a RangesManager instance, if an equivalent one already exists (one with
16 // the exact same ranges), the passed BucketRanges is deleted. This is useful to
17 // prevent duplicate instances of equivalent BucketRanges. Upon the destruction
18 // of a RangesManager instance, all BucketRanges managed by it are destroyed. A
19 // BucketRanges instance should not be registered to multiple RangesManagers.
20 class BASE_EXPORT RangesManager {
21  public:
22   RangesManager();
23 
24   RangesManager(const RangesManager&) = delete;
25   RangesManager& operator=(const RangesManager&) = delete;
26 
27   ~RangesManager();
28 
29   // Registers a BucketRanges. If an equivalent BucketRanges is already
30   // registered, then the argument |ranges| will be deleted. The returned value
31   // is always the registered BucketRanges (either the argument, or the
32   // pre-existing one). Registering a BucketRanges passes the ownership, and
33   // will be released when the RangesManager is released.
34   const BucketRanges* RegisterOrDeleteDuplicateRanges(
35       const BucketRanges* ranges);
36 
37   // Gets all registered BucketRanges. The order of returned BucketRanges is not
38   // guaranteed.
39   std::vector<const BucketRanges*> GetBucketRanges();
40 
41   // Some tests may instantiate temporary StatisticsRecorders, each having their
42   // own RangesManager. During the tests, ranges may get registered with a
43   // recorder that later gets released, which would release the ranges as well.
44   // Calling this method prevents this, as the tests may not expect them to be
45   // deleted.
46   void DoNotReleaseRangesOnDestroyForTesting();
47 
48  private:
49   // Removes all registered BucketRanges and destroys them. This is called in
50   // the destructor.
51   void ReleaseBucketRanges();
52 
53   // Used to get the hash of a BucketRanges, which is simply its checksum.
54   struct BucketRangesHash {
55     size_t operator()(const BucketRanges* a) const;
56   };
57 
58   // Comparator for BucketRanges. See `BucketRanges::Equals()`.
59   struct BucketRangesEqual {
60     bool operator()(const BucketRanges* a, const BucketRanges* b) const;
61   };
62 
63   // Type for a set of unique RangesBucket, with their hash and equivalence
64   // defined by `BucketRangesHash` and `BucketRangesEqual`.
65   typedef std::
66       unordered_set<const BucketRanges*, BucketRangesHash, BucketRangesEqual>
67           RangesMap;
68 
69   // The set of unique BucketRanges registered to the RangesManager.
70   RangesMap ranges_;
71 
72   // Whether or not to release the registered BucketRanges when this
73   // RangesManager is destroyed. See `DoNotReleaseRangesOnDestroyForTesting()`.
74   bool do_not_release_ranges_on_destroy_for_testing_ = false;
75 };
76 
77 }  // namespace base
78 
79 #endif  // BASE_METRICS_RANGES_MANAGER_H_
80