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