ranges_manager.h 3.0 KB

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