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