xref: /aosp_15_r20/external/cronet/components/metrics/expired_histograms_checker.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 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 #include "components/metrics/expired_histograms_checker.h"
6 
7 #include <algorithm>
8 #include <vector>
9 
10 #include "base/containers/contains.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/metrics/metrics_hashes.h"
13 #include "base/metrics/statistics_recorder.h"
14 #include "base/strings/string_split.h"
15 
16 namespace metrics {
17 
ExpiredHistogramsChecker(base::span<const uint32_t> expired_histogram_hashes,const std::string & allowlist_str)18 ExpiredHistogramsChecker::ExpiredHistogramsChecker(
19     base::span<const uint32_t> expired_histogram_hashes,
20     const std::string& allowlist_str)
21     : expired_histogram_hashes_(expired_histogram_hashes) {
22   InitAllowlist(allowlist_str);
23 }
24 
25 ExpiredHistogramsChecker::~ExpiredHistogramsChecker() = default;
26 
ShouldRecord(uint32_t histogram_hash) const27 bool ExpiredHistogramsChecker::ShouldRecord(uint32_t histogram_hash) const {
28   // If histogram is explicitly allowed then it should always be recorded.
29   if (base::Contains(allowlist_, histogram_hash)) {
30     return true;
31   }
32   return !std::binary_search(std::begin(expired_histogram_hashes_),
33                              std::end(expired_histogram_hashes_),
34                              histogram_hash);
35 }
36 
InitAllowlist(const std::string & allowlist_str)37 void ExpiredHistogramsChecker::InitAllowlist(const std::string& allowlist_str) {
38   std::vector<base::StringPiece> allowlist_names = base::SplitStringPiece(
39       allowlist_str, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
40   for (base::StringPiece name : allowlist_names)
41     allowlist_.insert(base::HashMetricNameAs32Bits(name));
42 }
43 
44 }  // namespace metrics
45