1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/feature_engagement/internal/feature_config_event_storage_validator.h"
#include <unordered_map>
#include <unordered_set>
#include "base/feature_list.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "components/feature_engagement/public/configuration.h"
#include "components/feature_engagement/public/feature_list.h"
#include "components/feature_engagement/public/group_constants.h"
namespace feature_engagement {
FeatureConfigEventStorageValidator::FeatureConfigEventStorageValidator() =
default;
FeatureConfigEventStorageValidator::~FeatureConfigEventStorageValidator() =
default;
bool FeatureConfigEventStorageValidator::ShouldStore(
const std::string& event_name) const {
for (const auto& prefix : should_store_event_name_prefixes_) {
CHECK(!prefix.empty());
if (base::StartsWith(event_name, prefix)) {
return true;
}
}
return should_store_event_names_.find(event_name) !=
should_store_event_names_.end();
}
bool FeatureConfigEventStorageValidator::ShouldKeep(
const std::string& event_name,
uint32_t event_day,
uint32_t current_day) const {
for (const auto& prefix : should_store_event_name_prefixes_) {
CHECK(!prefix.empty());
if (base::StartsWith(event_name, prefix)) {
return true;
}
}
// Should not keep events that will happen in the future.
if (event_day > current_day)
return false;
// If no feature configuration mentioned the event, it should not be kept.
auto it = longest_storage_times_.find(event_name);
if (it == longest_storage_times_.end())
return false;
// Too old events should not be kept.
// Storage time of N=0: Nothing should be kept.
// Storage time of N=1: |current_day| should be kept.
// Storage time of N=2+: |current_day| plus |N-1| more days should be kept.
uint32_t longest_storage_time = it->second;
uint32_t age = current_day - event_day;
if (longest_storage_time <= age)
return false;
return true;
}
void FeatureConfigEventStorageValidator::InitializeFeatures(
const FeatureVector& features,
const GroupVector& groups,
const Configuration& configuration) {
for (const auto* feature : features) {
if (!base::FeatureList::IsEnabled(*feature))
continue;
InitializeFeatureConfig(configuration.GetFeatureConfig(*feature));
}
for (const auto* group : groups) {
if (!base::FeatureList::IsEnabled(*group)) {
continue;
}
InitializeGroupConfig(configuration.GetGroupConfig(*group));
}
#if BUILDFLAG(IS_CHROMEOS)
InitializeEventPrefixes(configuration);
#endif
}
void FeatureConfigEventStorageValidator::ClearForTesting() {
should_store_event_names_.clear();
longest_storage_times_.clear();
should_store_event_name_prefixes_.clear();
}
void FeatureConfigEventStorageValidator::InitializeFeatureConfig(
const FeatureConfig& feature_config) {
InitializeEventConfig(feature_config.used);
InitializeEventConfig(feature_config.trigger);
for (const auto& event_config : feature_config.event_configs)
InitializeEventConfig(event_config);
}
void FeatureConfigEventStorageValidator::InitializeGroupConfig(
const GroupConfig& group_config) {
InitializeEventConfig(group_config.trigger);
for (const auto& event_config : group_config.event_configs) {
InitializeEventConfig(event_config);
}
}
void FeatureConfigEventStorageValidator::InitializeEventConfig(
const EventConfig& event_config) {
// Minimum storage time is 1 day.
if (event_config.storage < 1u)
return;
// When minimum storage time is met, new events should always be stored.
should_store_event_names_.insert(event_config.name);
// Track the longest time any configuration wants to store a particular event.
uint32_t current_longest_time = longest_storage_times_[event_config.name];
if (event_config.storage > current_longest_time)
longest_storage_times_[event_config.name] = event_config.storage;
}
#if BUILDFLAG(IS_CHROMEOS)
void FeatureConfigEventStorageValidator::InitializeEventPrefixes(
const Configuration& configuration) {
const auto& prefixes = configuration.GetRegisteredAllowedEventPrefixes();
should_store_event_name_prefixes_.insert(prefixes.begin(), prefixes.end());
}
#endif
} // namespace feature_engagement
|