• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const uint32_t * expired_histogram_hashes,size_t size,const std::string & allowlist_str)18 ExpiredHistogramsChecker::ExpiredHistogramsChecker(
19     const uint32_t* expired_histogram_hashes,
20     size_t size,
21     const std::string& allowlist_str)
22     : expired_histogram_hashes_(expired_histogram_hashes), size_(size) {
23   InitAllowlist(allowlist_str);
24 }
25 
~ExpiredHistogramsChecker()26 ExpiredHistogramsChecker::~ExpiredHistogramsChecker() {}
27 
ShouldRecord(uint32_t histogram_hash) const28 bool ExpiredHistogramsChecker::ShouldRecord(uint32_t histogram_hash) const {
29   // If histogram is explicitly allowed then it should always be recorded.
30   if (base::Contains(allowlist_, histogram_hash))
31     return true;
32   return !std::binary_search(expired_histogram_hashes_,
33                              expired_histogram_hashes_ + size_, histogram_hash);
34 }
35 
InitAllowlist(const std::string & allowlist_str)36 void ExpiredHistogramsChecker::InitAllowlist(const std::string& allowlist_str) {
37   std::vector<base::StringPiece> allowlist_names = base::SplitStringPiece(
38       allowlist_str, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
39   for (base::StringPiece name : allowlist_names)
40     allowlist_.insert(base::HashMetricNameAs32Bits(name));
41 }
42 
43 }  // namespace metrics
44