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