expired_histograms_checker.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. #include "components/metrics/expired_histograms_checker.h"
  5. #include <algorithm>
  6. #include <vector>
  7. #include "base/containers/contains.h"
  8. #include "base/metrics/metrics_hashes.h"
  9. #include "base/metrics/statistics_recorder.h"
  10. #include "base/strings/string_split.h"
  11. namespace metrics {
  12. ExpiredHistogramsChecker::ExpiredHistogramsChecker(
  13. const uint32_t* expired_histogram_hashes,
  14. size_t size,
  15. const std::string& allowlist_str)
  16. : expired_histogram_hashes_(expired_histogram_hashes), size_(size) {
  17. InitAllowlist(allowlist_str);
  18. }
  19. ExpiredHistogramsChecker::~ExpiredHistogramsChecker() {}
  20. bool ExpiredHistogramsChecker::ShouldRecord(uint32_t histogram_hash) const {
  21. // If histogram is explicitly allowed then it should always be recorded.
  22. if (base::Contains(allowlist_, histogram_hash))
  23. return true;
  24. return !std::binary_search(expired_histogram_hashes_.get(),
  25. expired_histogram_hashes_ + size_, histogram_hash);
  26. }
  27. void ExpiredHistogramsChecker::InitAllowlist(const std::string& allowlist_str) {
  28. std::vector<base::StringPiece> allowlist_names = base::SplitStringPiece(
  29. allowlist_str, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  30. for (base::StringPiece name : allowlist_names)
  31. allowlist_.insert(base::HashMetricNameAs32Bits(name));
  32. }
  33. } // namespace metrics