asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 1 | // Copyright 2015 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 | |
| 5 | #include "base/feature_list.h" |
Joel Fernandes | 541c6d0 | 2021-05-10 13:42:16 | [diff] [blame] | 6 | #include <string> |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 7 | |
Hans Wennborg | a8c0b33 | 2021-05-06 14:48:08 | [diff] [blame] | 8 | // feature_list.h is a widely included header and its size impacts build |
| 9 | // time. Try not to raise this limit unless necessary. See |
| 10 | // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/wmax_tokens.md |
| 11 | #ifndef NACL_TC_REV |
Peter Boström | a709c13c | 2021-07-22 20:43:27 | [diff] [blame] | 12 | #pragma clang max_tokens_here 530000 |
Hans Wennborg | a8c0b33 | 2021-05-06 14:48:08 | [diff] [blame] | 13 | #endif |
| 14 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include <stddef.h> |
| 16 | |
Will Harris | 196c69c | 2020-12-15 22:57:48 | [diff] [blame] | 17 | #include "base/base_paths.h" |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 18 | #include "base/base_switches.h" |
Lei Zhang | c0f9fc5 | 2021-05-22 08:00:53 | [diff] [blame] | 19 | #include "base/containers/contains.h" |
Wez | 1dc3d68b | 2019-12-10 06:25:46 | [diff] [blame] | 20 | #include "base/debug/alias.h" |
Joel Fernandes | 034037c | 2020-09-11 05:16:20 | [diff] [blame] | 21 | #include "base/debug/stack_trace.h" |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 22 | #include "base/logging.h" |
asvitkine | 9499b8d | 2016-08-09 05:37:07 | [diff] [blame] | 23 | #include "base/memory/ptr_util.h" |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 24 | #include "base/metrics/field_trial.h" |
Hans Wennborg | a8c0b33 | 2021-05-06 14:48:08 | [diff] [blame] | 25 | #include "base/metrics/persistent_memory_allocator.h" |
Will Harris | 196c69c | 2020-12-15 22:57:48 | [diff] [blame] | 26 | #include "base/path_service.h" |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 27 | #include "base/pickle.h" |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 29 | #include "base/strings/string_split.h" |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 30 | #include "base/strings/string_util.h" |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 31 | #include "base/strings/stringprintf.h" |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 32 | #include "build/build_config.h" |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 33 | |
| 34 | namespace base { |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | // Pointer to the FeatureList instance singleton that was set via |
| 39 | // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to |
| 40 | // have more control over initialization timing. Leaky. |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 41 | FeatureList* g_feature_list_instance = nullptr; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 42 | |
Wez | 1dc3d68b | 2019-12-10 06:25:46 | [diff] [blame] | 43 | // Tracks whether the FeatureList instance was initialized via an accessor, and |
| 44 | // which Feature that accessor was for, if so. |
| 45 | const Feature* g_initialized_from_accessor = nullptr; |
joedow | 958f047 | 2016-07-07 22:08:55 | [diff] [blame] | 46 | |
Ken Rockot | 30f7575 | 2019-10-12 08:07:41 | [diff] [blame] | 47 | #if DCHECK_IS_ON() |
Will Harris | 196c69c | 2020-12-15 22:57:48 | [diff] [blame] | 48 | // Tracks whether the use of base::Feature is allowed for this module. |
| 49 | // See ForbidUseForCurrentModule(). |
| 50 | bool g_use_allowed = true; |
| 51 | |
Ken Rockot | 30f7575 | 2019-10-12 08:07:41 | [diff] [blame] | 52 | const char* g_reason_overrides_disallowed = nullptr; |
| 53 | |
| 54 | void DCheckOverridesAllowed() { |
| 55 | const bool feature_overrides_allowed = !g_reason_overrides_disallowed; |
| 56 | DCHECK(feature_overrides_allowed) << g_reason_overrides_disallowed; |
| 57 | } |
| 58 | #else |
| 59 | void DCheckOverridesAllowed() {} |
| 60 | #endif |
| 61 | |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 62 | // An allocator entry for a feature in shared memory. The FeatureEntry is |
| 63 | // followed by a base::Pickle object that contains the feature and trial name. |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 64 | struct FeatureEntry { |
bcwhite | 3f999d3 | 2017-01-11 12:42:13 | [diff] [blame] | 65 | // SHA1(FeatureEntry): Increment this if structure changes! |
| 66 | static constexpr uint32_t kPersistentTypeId = 0x06567CA6 + 1; |
| 67 | |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 68 | // Expected size for 32/64-bit check. |
| 69 | static constexpr size_t kExpectedInstanceSize = 8; |
| 70 | |
| 71 | // Specifies whether a feature override enables or disables the feature. Same |
| 72 | // values as the OverrideState enum in feature_list.h |
| 73 | uint32_t override_state; |
| 74 | |
| 75 | // Size of the pickled structure, NOT the total size of this entry. |
| 76 | uint32_t pickle_size; |
| 77 | |
| 78 | // Reads the feature and trial name from the pickle. Calling this is only |
| 79 | // valid on an initialized entry that's in shared memory. |
| 80 | bool GetFeatureAndTrialName(StringPiece* feature_name, |
| 81 | StringPiece* trial_name) const { |
| 82 | const char* src = |
| 83 | reinterpret_cast<const char*>(this) + sizeof(FeatureEntry); |
| 84 | |
| 85 | Pickle pickle(src, pickle_size); |
| 86 | PickleIterator pickle_iter(pickle); |
| 87 | |
| 88 | if (!pickle_iter.ReadStringPiece(feature_name)) |
| 89 | return false; |
| 90 | |
| 91 | // Return true because we are not guaranteed to have a trial name anyways. |
| 92 | auto sink = pickle_iter.ReadStringPiece(trial_name); |
| 93 | ALLOW_UNUSED_LOCAL(sink); |
| 94 | return true; |
| 95 | } |
| 96 | }; |
| 97 | |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 98 | // Some characters are not allowed to appear in feature names or the associated |
| 99 | // field trial names, as they are used as special characters for command-line |
| 100 | // serialization. This function checks that the strings are ASCII (since they |
| 101 | // are used in command-line API functions that require ASCII) and whether there |
| 102 | // are any reserved characters present, returning true if the string is valid. |
| 103 | // Only called in DCHECKs. |
| 104 | bool IsValidFeatureOrFieldTrialName(const std::string& name) { |
asvitkine | 6d31c52e | 2016-03-22 15:37:52 | [diff] [blame] | 105 | return IsStringASCII(name) && name.find_first_of(",<*") == std::string::npos; |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 106 | } |
| 107 | |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 108 | // Splits |first| into two parts by the |separator| where the first part will be |
| 109 | // returned updated in |first| and the second part will be returned as |second|. |
| 110 | // This function returns false if there is more than one |separator| in |first|. |
| 111 | // If there is no |separator| presented in |first|, this function will not |
| 112 | // modify |first| and |second|. It's used for splitting the |enable_features| |
| 113 | // flag into feature name, field trial name and feature parameters. |
| 114 | bool SplitIntoTwo(const std::string& separator, |
| 115 | StringPiece* first, |
| 116 | std::string* second) { |
| 117 | std::vector<StringPiece> parts = |
| 118 | SplitStringPiece(*first, separator, TRIM_WHITESPACE, SPLIT_WANT_ALL); |
| 119 | if (parts.size() == 2) { |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 120 | *second = std::string(parts[1]); |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 121 | } else if (parts.size() > 2) { |
| 122 | DLOG(ERROR) << "Only one '" << separator |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 123 | << "' is allowed but got: " << *first; |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | *first = parts[0]; |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | // Checks and parses the |enable_features| flag and sets |
| 131 | // |parsed_enable_features| to be a comma-separated list of features, |
| 132 | // |force_fieldtrials| to be a comma-separated list of field trials that each |
| 133 | // feature want to associate with and |force_fieldtrial_params| to be the field |
| 134 | // trial parameters for each field trial. |
| 135 | // Returns true if |enable_features| is parsable, otherwise false. |
| 136 | bool ParseEnableFeatures(const std::string& enable_features, |
| 137 | std::string* parsed_enable_features, |
| 138 | std::string* force_fieldtrials, |
| 139 | std::string* force_fieldtrial_params) { |
| 140 | std::vector<std::string> enable_features_list; |
| 141 | std::vector<std::string> force_fieldtrials_list; |
| 142 | std::vector<std::string> force_fieldtrial_params_list; |
| 143 | for (auto& enable_feature : |
| 144 | FeatureList::SplitFeatureListString(enable_features)) { |
| 145 | // First, check whether ":" is present. If true, feature parameters were |
| 146 | // set for this feature. |
| 147 | std::string feature_params; |
| 148 | if (!SplitIntoTwo(":", &enable_feature, &feature_params)) |
| 149 | return false; |
| 150 | // Then, check whether "." is present. If true, a group was specified for |
| 151 | // this feature. |
| 152 | std::string group; |
| 153 | if (!SplitIntoTwo(".", &enable_feature, &group)) |
| 154 | return false; |
| 155 | // Finally, check whether "<" is present. If true, a study was specified for |
| 156 | // this feature. |
| 157 | std::string study; |
| 158 | if (!SplitIntoTwo("<", &enable_feature, &study)) |
| 159 | return false; |
| 160 | |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 161 | const std::string feature_name(enable_feature); |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 162 | // If feature params were set but group and study weren't, associate the |
| 163 | // feature and its feature params to a synthetic field trial as the |
| 164 | // feature params only make sense when it's combined with a field trial. |
| 165 | if (!feature_params.empty()) { |
| 166 | study = study.empty() ? "Study" + feature_name : study; |
| 167 | group = group.empty() ? "Group" + feature_name : group; |
| 168 | force_fieldtrials_list.push_back(study + "/" + group); |
| 169 | force_fieldtrial_params_list.push_back(study + "." + group + ":" + |
| 170 | feature_params); |
| 171 | } |
| 172 | enable_features_list.push_back( |
| 173 | study.empty() ? feature_name : (feature_name + "<" + study)); |
| 174 | } |
| 175 | |
| 176 | *parsed_enable_features = JoinString(enable_features_list, ","); |
| 177 | // Field trial separator is currently a slash. See |
| 178 | // |kPersistentStringSeparator| in base/metrics/field_trial.cc. |
| 179 | *force_fieldtrials = JoinString(force_fieldtrials_list, "/"); |
| 180 | *force_fieldtrial_params = JoinString(force_fieldtrial_params_list, ","); |
| 181 | return true; |
| 182 | } |
| 183 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 184 | } // namespace |
| 185 | |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 186 | #if defined(DCHECK_IS_CONFIGURABLE) |
Wez | a6ca5b9 | 2018-03-23 19:03:07 | [diff] [blame] | 187 | const Feature kDCheckIsFatalFeature{"DcheckIsFatal", |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 188 | FEATURE_DISABLED_BY_DEFAULT}; |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 189 | #endif // defined(DCHECK_IS_CONFIGURABLE) |
Sigurdur Asgeirsson | ad25d87 | 2017-09-20 19:10:29 | [diff] [blame] | 190 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 191 | FeatureList::FeatureList() = default; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 192 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 193 | FeatureList::~FeatureList() = default; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 194 | |
Ken Rockot | 30f7575 | 2019-10-12 08:07:41 | [diff] [blame] | 195 | FeatureList::ScopedDisallowOverrides::ScopedDisallowOverrides( |
| 196 | const char* reason) |
| 197 | #if DCHECK_IS_ON() |
| 198 | : previous_reason_(g_reason_overrides_disallowed) { |
| 199 | g_reason_overrides_disallowed = reason; |
| 200 | } |
| 201 | #else |
| 202 | { |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | FeatureList::ScopedDisallowOverrides::~ScopedDisallowOverrides() { |
| 207 | #if DCHECK_IS_ON() |
| 208 | g_reason_overrides_disallowed = previous_reason_; |
| 209 | #endif |
| 210 | } |
| 211 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 212 | void FeatureList::InitializeFromCommandLine( |
| 213 | const std::string& enable_features, |
| 214 | const std::string& disable_features) { |
| 215 | DCHECK(!initialized_); |
| 216 | |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 217 | std::string parsed_enable_features; |
| 218 | std::string force_fieldtrials; |
| 219 | std::string force_fieldtrial_params; |
| 220 | bool parse_enable_features_result = |
| 221 | ParseEnableFeatures(enable_features, &parsed_enable_features, |
| 222 | &force_fieldtrials, &force_fieldtrial_params); |
| 223 | DCHECK(parse_enable_features_result) << StringPrintf( |
| 224 | "The --%s list is unparsable or invalid, please check the format.", |
| 225 | ::switches::kEnableFeatures); |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 226 | |
Weilun Shi | 1cd8fb9 | 2020-07-17 23:31:00 | [diff] [blame] | 227 | // Only create field trials when field_trial_list is available. Some tests |
| 228 | // don't have field trial list available. |
| 229 | if (FieldTrialList::GetInstance()) { |
| 230 | bool associate_params_result = AssociateFieldTrialParamsFromString( |
| 231 | force_fieldtrial_params, &UnescapeValue); |
| 232 | DCHECK(associate_params_result) << StringPrintf( |
| 233 | "The field trial parameters part of the --%s list is invalid. Make " |
| 234 | "sure " |
| 235 | "you %%-encode the following characters in param values: %%:/.,", |
| 236 | ::switches::kEnableFeatures); |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 237 | |
Weilun Shi | 1cd8fb9 | 2020-07-17 23:31:00 | [diff] [blame] | 238 | bool create_trials_result = |
| 239 | FieldTrialList::CreateTrialsFromString(force_fieldtrials); |
| 240 | DCHECK(create_trials_result) |
| 241 | << StringPrintf("Invalid field trials are specified in --%s.", |
| 242 | ::switches::kEnableFeatures); |
| 243 | } |
| 244 | |
| 245 | // Process disabled features first, so that disabled ones take precedence over |
| 246 | // enabled ones (since RegisterOverride() uses insert()). |
| 247 | RegisterOverridesFromCommandLine(disable_features, OVERRIDE_DISABLE_FEATURE); |
| 248 | RegisterOverridesFromCommandLine(parsed_enable_features, |
| 249 | OVERRIDE_ENABLE_FEATURE); |
| 250 | |
| 251 | initialized_from_command_line_ = true; |
Weilun Shi | e81c6b9 | 2020-07-06 20:33:59 | [diff] [blame] | 252 | } |
| 253 | |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 254 | void FeatureList::InitializeFromSharedMemory( |
| 255 | PersistentMemoryAllocator* allocator) { |
| 256 | DCHECK(!initialized_); |
| 257 | |
| 258 | PersistentMemoryAllocator::Iterator iter(allocator); |
bcwhite | 3f999d3 | 2017-01-11 12:42:13 | [diff] [blame] | 259 | const FeatureEntry* entry; |
| 260 | while ((entry = iter.GetNextOfObject<FeatureEntry>()) != nullptr) { |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 261 | OverrideState override_state = |
| 262 | static_cast<OverrideState>(entry->override_state); |
| 263 | |
| 264 | StringPiece feature_name; |
| 265 | StringPiece trial_name; |
| 266 | if (!entry->GetFeatureAndTrialName(&feature_name, &trial_name)) |
| 267 | continue; |
| 268 | |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 269 | FieldTrial* trial = FieldTrialList::Find(trial_name); |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 270 | RegisterOverride(feature_name, override_state, trial); |
| 271 | } |
| 272 | } |
| 273 | |
Xianzhu Wang | 05355f4a | 2020-09-02 01:22:16 | [diff] [blame] | 274 | bool FeatureList::IsFeatureOverridden(const std::string& feature_name) const { |
| 275 | return overrides_.count(feature_name); |
| 276 | } |
| 277 | |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 278 | bool FeatureList::IsFeatureOverriddenFromCommandLine( |
Collin Baker | 3435ba66 | 2020-10-07 18:07:09 | [diff] [blame] | 279 | const std::string& feature_name) const { |
| 280 | auto it = overrides_.find(feature_name); |
| 281 | return it != overrides_.end() && !it->second.overridden_by_field_trial; |
| 282 | } |
| 283 | |
| 284 | bool FeatureList::IsFeatureOverriddenFromCommandLine( |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 285 | const std::string& feature_name, |
| 286 | OverrideState state) const { |
| 287 | auto it = overrides_.find(feature_name); |
Collin Baker | 3435ba66 | 2020-10-07 18:07:09 | [diff] [blame] | 288 | return it != overrides_.end() && !it->second.overridden_by_field_trial && |
| 289 | it->second.overridden_state == state; |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 290 | } |
| 291 | |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 292 | void FeatureList::AssociateReportingFieldTrial( |
| 293 | const std::string& feature_name, |
| 294 | OverrideState for_overridden_state, |
| 295 | FieldTrial* field_trial) { |
| 296 | DCHECK( |
| 297 | IsFeatureOverriddenFromCommandLine(feature_name, for_overridden_state)); |
| 298 | |
| 299 | // Only one associated field trial is supported per feature. This is generally |
| 300 | // enforced server-side. |
| 301 | OverrideEntry* entry = &overrides_.find(feature_name)->second; |
| 302 | if (entry->field_trial) { |
| 303 | NOTREACHED() << "Feature " << feature_name |
| 304 | << " already has trial: " << entry->field_trial->trial_name() |
| 305 | << ", associating trial: " << field_trial->trial_name(); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | entry->field_trial = field_trial; |
| 310 | } |
| 311 | |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 312 | void FeatureList::RegisterFieldTrialOverride(const std::string& feature_name, |
| 313 | OverrideState override_state, |
| 314 | FieldTrial* field_trial) { |
| 315 | DCHECK(field_trial); |
Jan Wilken Dörrie | f61e74c | 2019-06-07 08:20:02 | [diff] [blame] | 316 | DCHECK(!Contains(overrides_, feature_name) || |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 317 | !overrides_.find(feature_name)->second.field_trial) |
Caitlin Fischer | 84739802 | 2021-07-28 02:58:31 | [diff] [blame] | 318 | << "Feature " << feature_name << " is overriden multiple times in these " |
| 319 | << "trials: " |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 320 | << overrides_.find(feature_name)->second.field_trial->trial_name() |
Caitlin Fischer | 84739802 | 2021-07-28 02:58:31 | [diff] [blame] | 321 | << " and " << field_trial->trial_name() << ". " |
| 322 | << "Check the trial (study) in (1) the server config, " |
| 323 | << "(2) fieldtrial_testing_config.json, (3) about_flags.cc, and " |
| 324 | << "(4) client-side field trials."; |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 325 | |
| 326 | RegisterOverride(feature_name, override_state, field_trial); |
| 327 | } |
| 328 | |
Lily Chen | d49e375 | 2019-08-09 19:05:24 | [diff] [blame] | 329 | void FeatureList::RegisterExtraFeatureOverrides( |
| 330 | const std::vector<FeatureOverrideInfo>& extra_overrides) { |
| 331 | for (const FeatureOverrideInfo& override_info : extra_overrides) { |
| 332 | RegisterOverride(override_info.first.get().name, override_info.second, |
| 333 | /* field_trial = */ nullptr); |
| 334 | } |
| 335 | } |
| 336 | |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 337 | void FeatureList::AddFeaturesToAllocator(PersistentMemoryAllocator* allocator) { |
| 338 | DCHECK(initialized_); |
| 339 | |
| 340 | for (const auto& override : overrides_) { |
| 341 | Pickle pickle; |
| 342 | pickle.WriteString(override.first); |
| 343 | if (override.second.field_trial) |
| 344 | pickle.WriteString(override.second.field_trial->trial_name()); |
| 345 | |
| 346 | size_t total_size = sizeof(FeatureEntry) + pickle.size(); |
bcwhite | cf6a9e8 | 2017-02-09 20:44:23 | [diff] [blame] | 347 | FeatureEntry* entry = allocator->New<FeatureEntry>(total_size); |
bcwhite | 3f999d3 | 2017-01-11 12:42:13 | [diff] [blame] | 348 | if (!entry) |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 349 | return; |
| 350 | |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 351 | entry->override_state = override.second.overridden_state; |
| 352 | entry->pickle_size = pickle.size(); |
| 353 | |
| 354 | char* dst = reinterpret_cast<char*>(entry) + sizeof(FeatureEntry); |
| 355 | memcpy(dst, pickle.data(), pickle.size()); |
| 356 | |
bcwhite | 3f999d3 | 2017-01-11 12:42:13 | [diff] [blame] | 357 | allocator->MakeIterable(entry); |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 361 | void FeatureList::GetFeatureOverrides(std::string* enable_overrides, |
| 362 | std::string* disable_overrides) { |
Alexei Svitkine | 223d228 | 2018-02-08 00:18:35 | [diff] [blame] | 363 | GetFeatureOverridesImpl(enable_overrides, disable_overrides, false); |
| 364 | } |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 365 | |
Alexei Svitkine | 223d228 | 2018-02-08 00:18:35 | [diff] [blame] | 366 | void FeatureList::GetCommandLineFeatureOverrides( |
| 367 | std::string* enable_overrides, |
| 368 | std::string* disable_overrides) { |
| 369 | GetFeatureOverridesImpl(enable_overrides, disable_overrides, true); |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 370 | } |
| 371 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 372 | // static |
| 373 | bool FeatureList::IsEnabled(const Feature& feature) { |
Will Harris | 196c69c | 2020-12-15 22:57:48 | [diff] [blame] | 374 | #if DCHECK_IS_ON() |
| 375 | CHECK(g_use_allowed) << "base::Feature not permitted for this module."; |
| 376 | #endif |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 377 | if (!g_feature_list_instance) { |
Wez | 1dc3d68b | 2019-12-10 06:25:46 | [diff] [blame] | 378 | g_initialized_from_accessor = &feature; |
joedow | 958f047 | 2016-07-07 22:08:55 | [diff] [blame] | 379 | return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; |
| 380 | } |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 381 | return g_feature_list_instance->IsFeatureEnabled(feature); |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // static |
Leszek Swirski | f68e123f | 2021-07-22 18:23:39 | [diff] [blame] | 385 | absl::optional<bool> FeatureList::GetStateIfOverridden(const Feature& feature) { |
| 386 | #if DCHECK_IS_ON() |
| 387 | CHECK(g_use_allowed) << "base::Feature not permitted for this module."; |
| 388 | #endif |
| 389 | if (!g_feature_list_instance) { |
| 390 | g_initialized_from_accessor = &feature; |
| 391 | // If there is no feature list, there can be no overrides. |
| 392 | return absl::nullopt; |
| 393 | } |
| 394 | return g_feature_list_instance->IsFeatureEnabledIfOverridden(feature); |
| 395 | } |
| 396 | |
| 397 | // static |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 398 | FieldTrial* FeatureList::GetFieldTrial(const Feature& feature) { |
Will Harris | 196c69c | 2020-12-15 22:57:48 | [diff] [blame] | 399 | #if DCHECK_IS_ON() |
| 400 | // See documentation for ForbidUseForCurrentModule. |
| 401 | CHECK(g_use_allowed) << "base::Feature not permitted for this module."; |
| 402 | #endif |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 403 | if (!g_feature_list_instance) { |
Wez | 1dc3d68b | 2019-12-10 06:25:46 | [diff] [blame] | 404 | g_initialized_from_accessor = &feature; |
Alexei Svitkine | c49d1f40 | 2017-09-18 23:00:41 | [diff] [blame] | 405 | return nullptr; |
| 406 | } |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 407 | return g_feature_list_instance->GetAssociatedFieldTrial(feature); |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // static |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 411 | std::vector<StringPiece> FeatureList::SplitFeatureListString( |
| 412 | StringPiece input) { |
mgiuca | 30f7588 | 2017-03-28 02:07:19 | [diff] [blame] | 413 | return SplitStringPiece(input, ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
asvitkine | 03007d0 | 2015-10-21 22:50:06 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // static |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame] | 417 | bool FeatureList::InitializeInstance(const std::string& enable_features, |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 418 | const std::string& disable_features) { |
Lily Chen | d49e375 | 2019-08-09 19:05:24 | [diff] [blame] | 419 | return InitializeInstance(enable_features, disable_features, |
| 420 | std::vector<FeatureOverrideInfo>()); |
| 421 | } |
| 422 | |
| 423 | // static |
| 424 | bool FeatureList::InitializeInstance( |
| 425 | const std::string& enable_features, |
| 426 | const std::string& disable_features, |
| 427 | const std::vector<FeatureOverrideInfo>& extra_overrides) { |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 428 | // We want to initialize a new instance here to support command-line features |
| 429 | // in testing better. For example, we initialize a dummy instance in |
| 430 | // base/test/test_suite.cc, and override it in content/browser/ |
| 431 | // browser_main_loop.cc. |
| 432 | // On the other hand, we want to avoid re-initialization from command line. |
| 433 | // For example, we initialize an instance in chrome/browser/ |
| 434 | // chrome_browser_main.cc and do not override it in content/browser/ |
| 435 | // browser_main_loop.cc. |
joedow | 958f047 | 2016-07-07 22:08:55 | [diff] [blame] | 436 | // If the singleton was previously initialized from within an accessor, we |
| 437 | // want to prevent callers from reinitializing the singleton and masking the |
| 438 | // accessor call(s) which likely returned incorrect information. |
Wez | 1dc3d68b | 2019-12-10 06:25:46 | [diff] [blame] | 439 | if (g_initialized_from_accessor) { |
| 440 | DEBUG_ALIAS_FOR_CSTR(accessor_name, g_initialized_from_accessor->name, 128); |
| 441 | CHECK(!g_initialized_from_accessor); |
| 442 | } |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame] | 443 | bool instance_existed_before = false; |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 444 | if (g_feature_list_instance) { |
| 445 | if (g_feature_list_instance->initialized_from_command_line_) |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame] | 446 | return false; |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 447 | |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 448 | delete g_feature_list_instance; |
| 449 | g_feature_list_instance = nullptr; |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame] | 450 | instance_existed_before = true; |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 451 | } |
| 452 | |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 453 | std::unique_ptr<FeatureList> feature_list(new FeatureList); |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 454 | feature_list->InitializeFromCommandLine(enable_features, disable_features); |
Lily Chen | d49e375 | 2019-08-09 19:05:24 | [diff] [blame] | 455 | feature_list->RegisterExtraFeatureOverrides(extra_overrides); |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 456 | FeatureList::SetInstance(std::move(feature_list)); |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame] | 457 | return !instance_existed_before; |
asvitkine | 9d96abf | 2015-11-02 21:52:08 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // static |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 461 | FeatureList* FeatureList::GetInstance() { |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 462 | return g_feature_list_instance; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // static |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 466 | void FeatureList::SetInstance(std::unique_ptr<FeatureList> instance) { |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 467 | DCHECK(!g_feature_list_instance); |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 468 | instance->FinalizeInitialization(); |
| 469 | |
| 470 | // Note: Intentional leak of global singleton. |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 471 | g_feature_list_instance = instance.release(); |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 472 | |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 473 | #if defined(DCHECK_IS_CONFIGURABLE) |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 474 | // Update the behaviour of LOGGING_DCHECK to match the Feature configuration. |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 475 | // DCHECK is also forced to be FATAL if we are running a death-test. |
Gabriel Charette | e9421ec | 2020-05-21 18:20:03 | [diff] [blame] | 476 | // TODO(crbug.com/1057995#c11): --gtest_internal_run_death_test doesn't |
| 477 | // currently run through this codepath, mitigated in |
| 478 | // base::TestSuite::Initialize() for now. |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 479 | // TODO(asvitkine): If we find other use-cases that need integrating here |
| 480 | // then define a proper API/hook for the purpose. |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 481 | if (FeatureList::IsEnabled(kDCheckIsFatalFeature) || |
| 482 | CommandLine::ForCurrentProcess()->HasSwitch( |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 483 | "gtest_internal_run_death_test")) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 484 | logging::LOGGING_DCHECK = logging::LOG_FATAL; |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 485 | } else { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 486 | logging::LOGGING_DCHECK = logging::LOG_INFO; |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 487 | } |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 488 | #endif // defined(DCHECK_IS_CONFIGURABLE) |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | // static |
asvitkine | 9499b8d | 2016-08-09 05:37:07 | [diff] [blame] | 492 | std::unique_ptr<FeatureList> FeatureList::ClearInstanceForTesting() { |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 493 | FeatureList* old_instance = g_feature_list_instance; |
| 494 | g_feature_list_instance = nullptr; |
Wez | 1dc3d68b | 2019-12-10 06:25:46 | [diff] [blame] | 495 | g_initialized_from_accessor = nullptr; |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 496 | return WrapUnique(old_instance); |
asvitkine | 9499b8d | 2016-08-09 05:37:07 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // static |
| 500 | void FeatureList::RestoreInstanceForTesting( |
| 501 | std::unique_ptr<FeatureList> instance) { |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 502 | DCHECK(!g_feature_list_instance); |
asvitkine | 9499b8d | 2016-08-09 05:37:07 | [diff] [blame] | 503 | // Note: Intentional leak of global singleton. |
Daniel Bratell | 8712018 | 2018-02-22 19:07:26 | [diff] [blame] | 504 | g_feature_list_instance = instance.release(); |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 505 | } |
| 506 | |
Will Harris | 196c69c | 2020-12-15 22:57:48 | [diff] [blame] | 507 | // static |
| 508 | void FeatureList::ForbidUseForCurrentModule() { |
| 509 | #if DCHECK_IS_ON() |
| 510 | // Verify there hasn't been any use prior to being called. |
| 511 | DCHECK(!g_initialized_from_accessor); |
| 512 | g_use_allowed = false; |
| 513 | #endif // DCHECK_IS_ON() |
| 514 | } |
| 515 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 516 | void FeatureList::FinalizeInitialization() { |
| 517 | DCHECK(!initialized_); |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 518 | // Store the field trial list pointer for DCHECKing. |
| 519 | field_trial_list_ = FieldTrialList::GetInstance(); |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 520 | initialized_ = true; |
| 521 | } |
| 522 | |
| 523 | bool FeatureList::IsFeatureEnabled(const Feature& feature) { |
Leszek Swirski | f68e123f | 2021-07-22 18:23:39 | [diff] [blame] | 524 | OverrideState overridden_state = GetOverrideState(feature); |
| 525 | |
| 526 | // If marked as OVERRIDE_USE_DEFAULT, simply return the default state below. |
| 527 | if (overridden_state != OVERRIDE_USE_DEFAULT) |
| 528 | return overridden_state == OVERRIDE_ENABLE_FEATURE; |
| 529 | |
| 530 | return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; |
| 531 | } |
| 532 | |
| 533 | absl::optional<bool> FeatureList::IsFeatureEnabledIfOverridden( |
| 534 | const Feature& feature) { |
| 535 | OverrideState overridden_state = GetOverrideState(feature); |
| 536 | |
| 537 | // If marked as OVERRIDE_USE_DEFAULT, fall through to returning empty. |
| 538 | if (overridden_state != OVERRIDE_USE_DEFAULT) |
| 539 | return overridden_state == OVERRIDE_ENABLE_FEATURE; |
| 540 | |
| 541 | return absl::nullopt; |
| 542 | } |
| 543 | |
| 544 | FeatureList::OverrideState FeatureList::GetOverrideState( |
| 545 | const Feature& feature) { |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 546 | DCHECK(initialized_); |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 547 | DCHECK(IsValidFeatureOrFieldTrialName(feature.name)) << feature.name; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 548 | DCHECK(CheckFeatureIdentity(feature)) << feature.name; |
| 549 | |
| 550 | auto it = overrides_.find(feature.name); |
| 551 | if (it != overrides_.end()) { |
| 552 | const OverrideEntry& entry = it->second; |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 553 | |
| 554 | // Activate the corresponding field trial, if necessary. |
| 555 | if (entry.field_trial) |
| 556 | entry.field_trial->group(); |
| 557 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 558 | // TODO(asvitkine) Expand this section as more support is added. |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 559 | |
Leszek Swirski | f68e123f | 2021-07-22 18:23:39 | [diff] [blame] | 560 | return entry.overridden_state; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 561 | } |
Leszek Swirski | f68e123f | 2021-07-22 18:23:39 | [diff] [blame] | 562 | // Otherwise, report that we want to use the default state. |
| 563 | return OVERRIDE_USE_DEFAULT; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 564 | } |
| 565 | |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 566 | FieldTrial* FeatureList::GetAssociatedFieldTrial(const Feature& feature) { |
| 567 | DCHECK(initialized_); |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 568 | DCHECK(CheckFeatureIdentity(feature)) << feature.name; |
| 569 | |
Joel Fernandes | 541c6d0 | 2021-05-10 13:42:16 | [diff] [blame] | 570 | return GetAssociatedFieldTrialByFeatureName(feature.name); |
| 571 | } |
| 572 | |
| 573 | const base::FeatureList::OverrideEntry* |
| 574 | FeatureList::GetOverrideEntryByFeatureName(StringPiece name) { |
| 575 | DCHECK(initialized_); |
| 576 | DCHECK(IsValidFeatureOrFieldTrialName(std::string(name))) << name; |
| 577 | |
| 578 | auto it = overrides_.find(name); |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 579 | if (it != overrides_.end()) { |
| 580 | const OverrideEntry& entry = it->second; |
Joel Fernandes | 541c6d0 | 2021-05-10 13:42:16 | [diff] [blame] | 581 | return &entry; |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 582 | } |
Joel Fernandes | 541c6d0 | 2021-05-10 13:42:16 | [diff] [blame] | 583 | return nullptr; |
| 584 | } |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 585 | |
Joel Fernandes | 541c6d0 | 2021-05-10 13:42:16 | [diff] [blame] | 586 | FieldTrial* FeatureList::GetAssociatedFieldTrialByFeatureName( |
| 587 | StringPiece name) { |
| 588 | DCHECK(initialized_); |
| 589 | |
| 590 | const base::FeatureList::OverrideEntry* entry = |
| 591 | GetOverrideEntryByFeatureName(name); |
| 592 | if (entry) { |
| 593 | return entry->field_trial; |
| 594 | } |
| 595 | return nullptr; |
| 596 | } |
| 597 | |
| 598 | FieldTrial* FeatureList::GetEnabledFieldTrialByFeatureName(StringPiece name) { |
| 599 | DCHECK(initialized_); |
| 600 | |
| 601 | const base::FeatureList::OverrideEntry* entry = |
| 602 | GetOverrideEntryByFeatureName(name); |
| 603 | if (entry && |
| 604 | entry->overridden_state == base::FeatureList::OVERRIDE_ENABLE_FEATURE) { |
| 605 | return entry->field_trial; |
| 606 | } |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 607 | return nullptr; |
| 608 | } |
| 609 | |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 610 | void FeatureList::RegisterOverridesFromCommandLine( |
| 611 | const std::string& feature_list, |
| 612 | OverrideState overridden_state) { |
| 613 | for (const auto& value : SplitFeatureListString(feature_list)) { |
mgiuca | 30f7588 | 2017-03-28 02:07:19 | [diff] [blame] | 614 | StringPiece feature_name = value; |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 615 | FieldTrial* trial = nullptr; |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 616 | |
| 617 | // The entry may be of the form FeatureName<FieldTrialName - in which case, |
| 618 | // this splits off the field trial name and associates it with the override. |
| 619 | std::string::size_type pos = feature_name.find('<'); |
| 620 | if (pos != std::string::npos) { |
Jan Wilken Dörrie | 884fe9b | 2020-01-29 10:16:31 | [diff] [blame] | 621 | feature_name = StringPiece(value.data(), pos); |
Jan Wilken Dörrie | e784313 | 2021-02-16 20:32:59 | [diff] [blame] | 622 | trial = FieldTrialList::Find(value.substr(pos + 1)); |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 623 | #if !defined(OS_NACL) |
| 624 | // If the below DCHECK fires, it means a non-existent trial name was |
| 625 | // specified via the "Feature<Trial" command-line syntax. |
Camillo Bruni | 6efb358 | 2021-03-29 17:05:41 | [diff] [blame] | 626 | DCHECK(trial) << "trial='" << value.substr(pos + 1) << "' does not exist"; |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 627 | #endif // !defined(OS_NACL) |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | RegisterOverride(feature_name, overridden_state, trial); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | void FeatureList::RegisterOverride(StringPiece feature_name, |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 635 | OverrideState overridden_state, |
| 636 | FieldTrial* field_trial) { |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 637 | DCHECK(!initialized_); |
Ken Rockot | 30f7575 | 2019-10-12 08:07:41 | [diff] [blame] | 638 | DCheckOverridesAllowed(); |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 639 | if (field_trial) { |
| 640 | DCHECK(IsValidFeatureOrFieldTrialName(field_trial->trial_name())) |
| 641 | << field_trial->trial_name(); |
| 642 | } |
Jan Wilken Dörrie | f05bb10 | 2020-08-18 19:35:56 | [diff] [blame] | 643 | if (StartsWith(feature_name, "*")) { |
asvitkine | 6d31c52e | 2016-03-22 15:37:52 | [diff] [blame] | 644 | feature_name = feature_name.substr(1); |
| 645 | overridden_state = OVERRIDE_USE_DEFAULT; |
| 646 | } |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 647 | |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 648 | // Note: The semantics of emplace() is that it does not overwrite the entry if |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 649 | // one already exists for the key. Thus, only the first override for a given |
| 650 | // feature name takes effect. |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 651 | overrides_.emplace(std::string(feature_name), |
| 652 | OverrideEntry(overridden_state, field_trial)); |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 653 | } |
| 654 | |
Alexei Svitkine | 223d228 | 2018-02-08 00:18:35 | [diff] [blame] | 655 | void FeatureList::GetFeatureOverridesImpl(std::string* enable_overrides, |
| 656 | std::string* disable_overrides, |
| 657 | bool command_line_only) { |
| 658 | DCHECK(initialized_); |
| 659 | |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 660 | // Check that the FieldTrialList this is associated with, if any, is the |
| 661 | // active one. If not, it likely indicates that this FeatureList has override |
| 662 | // entries from a freed FieldTrial, which may be caused by an incorrect test |
| 663 | // set up. |
Mikel Astiz | 504e67f | 2019-11-28 16:25:07 | [diff] [blame] | 664 | if (field_trial_list_) |
| 665 | DCHECK_EQ(field_trial_list_, FieldTrialList::GetInstance()); |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 666 | |
Alexei Svitkine | 223d228 | 2018-02-08 00:18:35 | [diff] [blame] | 667 | enable_overrides->clear(); |
| 668 | disable_overrides->clear(); |
| 669 | |
| 670 | // Note: Since |overrides_| is a std::map, iteration will be in alphabetical |
| 671 | // order. This is not guaranteed to users of this function, but is useful for |
| 672 | // tests to assume the order. |
| 673 | for (const auto& entry : overrides_) { |
| 674 | if (command_line_only && |
| 675 | (entry.second.field_trial != nullptr || |
| 676 | entry.second.overridden_state == OVERRIDE_USE_DEFAULT)) { |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | std::string* target_list = nullptr; |
| 681 | switch (entry.second.overridden_state) { |
| 682 | case OVERRIDE_USE_DEFAULT: |
| 683 | case OVERRIDE_ENABLE_FEATURE: |
| 684 | target_list = enable_overrides; |
| 685 | break; |
| 686 | case OVERRIDE_DISABLE_FEATURE: |
| 687 | target_list = disable_overrides; |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | if (!target_list->empty()) |
| 692 | target_list->push_back(','); |
| 693 | if (entry.second.overridden_state == OVERRIDE_USE_DEFAULT) |
| 694 | target_list->push_back('*'); |
| 695 | target_list->append(entry.first); |
| 696 | if (entry.second.field_trial) { |
| 697 | target_list->push_back('<'); |
| 698 | target_list->append(entry.second.field_trial->trial_name()); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 703 | bool FeatureList::CheckFeatureIdentity(const Feature& feature) { |
| 704 | AutoLock auto_lock(feature_identity_tracker_lock_); |
| 705 | |
| 706 | auto it = feature_identity_tracker_.find(feature.name); |
| 707 | if (it == feature_identity_tracker_.end()) { |
| 708 | // If it's not tracked yet, register it. |
| 709 | feature_identity_tracker_[feature.name] = &feature; |
| 710 | return true; |
| 711 | } |
| 712 | // Compare address of |feature| to the existing tracked entry. |
| 713 | return it->second == &feature; |
| 714 | } |
| 715 | |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 716 | FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state, |
| 717 | FieldTrial* field_trial) |
| 718 | : overridden_state(overridden_state), |
| 719 | field_trial(field_trial), |
| 720 | overridden_by_field_trial(field_trial != nullptr) {} |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 721 | |
| 722 | } // namespace base |