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 <string_view> 9 #include <vector> 10 11 #include "base/containers/contains.h" 12 #include "base/memory/raw_ptr.h" 13 #include "base/metrics/metrics_hashes.h" 14 #include "base/metrics/statistics_recorder.h" 15 #include "base/strings/string_split.h" 16 17 namespace metrics { 18 ExpiredHistogramsChecker(base::span<const uint32_t> expired_histogram_hashes,const std::string & allowlist_str)19ExpiredHistogramsChecker::ExpiredHistogramsChecker( 20 base::span<const uint32_t> expired_histogram_hashes, 21 const std::string& allowlist_str) 22 : expired_histogram_hashes_(expired_histogram_hashes) { 23 InitAllowlist(allowlist_str); 24 } 25 26 ExpiredHistogramsChecker::~ExpiredHistogramsChecker() = default; 27 ShouldRecord(uint32_t histogram_hash) const28bool 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 } 33 return !std::binary_search(std::begin(expired_histogram_hashes_), 34 std::end(expired_histogram_hashes_), 35 histogram_hash); 36 } 37 InitAllowlist(const std::string & allowlist_str)38void ExpiredHistogramsChecker::InitAllowlist(const std::string& allowlist_str) { 39 std::vector<std::string_view> allowlist_names = base::SplitStringPiece( 40 allowlist_str, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 41 for (std::string_view name : allowlist_names) { 42 allowlist_.insert(base::HashMetricNameAs32Bits(name)); 43 } 44 } 45 46 } // namespace metrics 47