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 | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 13 | #include "base/metrics/field_trial.h" |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 14 | #include "base/strings/string_split.h" |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Pointer to the FeatureList instance singleton that was set via |
| 22 | // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to |
| 23 | // have more control over initialization timing. Leaky. |
| 24 | FeatureList* g_instance = nullptr; |
| 25 | |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 26 | // Some characters are not allowed to appear in feature names or the associated |
| 27 | // field trial names, as they are used as special characters for command-line |
| 28 | // serialization. This function checks that the strings are ASCII (since they |
| 29 | // are used in command-line API functions that require ASCII) and whether there |
| 30 | // are any reserved characters present, returning true if the string is valid. |
| 31 | // Only called in DCHECKs. |
| 32 | bool IsValidFeatureOrFieldTrialName(const std::string& name) { |
asvitkine | 6d31c52e | 2016-03-22 15:37:52 | [diff] [blame] | 33 | return IsStringASCII(name) && name.find_first_of(",<*") == std::string::npos; |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 34 | } |
| 35 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 36 | } // namespace |
| 37 | |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 38 | FeatureList::FeatureList() |
| 39 | : initialized_(false), |
| 40 | initialized_from_command_line_(false) { |
| 41 | } |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 42 | |
| 43 | FeatureList::~FeatureList() {} |
| 44 | |
| 45 | void FeatureList::InitializeFromCommandLine( |
| 46 | const std::string& enable_features, |
| 47 | const std::string& disable_features) { |
| 48 | DCHECK(!initialized_); |
| 49 | |
| 50 | // Process disabled features first, so that disabled ones take precedence over |
| 51 | // enabled ones (since RegisterOverride() uses insert()). |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 52 | RegisterOverridesFromCommandLine(disable_features, OVERRIDE_DISABLE_FEATURE); |
| 53 | RegisterOverridesFromCommandLine(enable_features, OVERRIDE_ENABLE_FEATURE); |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 54 | |
| 55 | initialized_from_command_line_ = true; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 56 | } |
| 57 | |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 58 | bool FeatureList::IsFeatureOverriddenFromCommandLine( |
| 59 | const std::string& feature_name, |
| 60 | OverrideState state) const { |
| 61 | auto it = overrides_.find(feature_name); |
| 62 | return it != overrides_.end() && it->second.overridden_state == state && |
| 63 | !it->second.overridden_by_field_trial; |
| 64 | } |
| 65 | |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 66 | void FeatureList::AssociateReportingFieldTrial( |
| 67 | const std::string& feature_name, |
| 68 | OverrideState for_overridden_state, |
| 69 | FieldTrial* field_trial) { |
| 70 | DCHECK( |
| 71 | IsFeatureOverriddenFromCommandLine(feature_name, for_overridden_state)); |
| 72 | |
| 73 | // Only one associated field trial is supported per feature. This is generally |
| 74 | // enforced server-side. |
| 75 | OverrideEntry* entry = &overrides_.find(feature_name)->second; |
| 76 | if (entry->field_trial) { |
| 77 | NOTREACHED() << "Feature " << feature_name |
| 78 | << " already has trial: " << entry->field_trial->trial_name() |
| 79 | << ", associating trial: " << field_trial->trial_name(); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | entry->field_trial = field_trial; |
| 84 | } |
| 85 | |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 86 | void FeatureList::RegisterFieldTrialOverride(const std::string& feature_name, |
| 87 | OverrideState override_state, |
| 88 | FieldTrial* field_trial) { |
| 89 | DCHECK(field_trial); |
| 90 | DCHECK(!ContainsKey(overrides_, feature_name) || |
| 91 | !overrides_.find(feature_name)->second.field_trial) |
| 92 | << "Feature " << feature_name |
| 93 | << " has conflicting field trial overrides: " |
| 94 | << overrides_.find(feature_name)->second.field_trial->trial_name() |
| 95 | << " / " << field_trial->trial_name(); |
| 96 | |
| 97 | RegisterOverride(feature_name, override_state, field_trial); |
| 98 | } |
| 99 | |
| 100 | void FeatureList::GetFeatureOverrides(std::string* enable_overrides, |
| 101 | std::string* disable_overrides) { |
| 102 | DCHECK(initialized_); |
| 103 | |
| 104 | enable_overrides->clear(); |
| 105 | disable_overrides->clear(); |
| 106 | |
asvitkine | 6d31c52e | 2016-03-22 15:37:52 | [diff] [blame] | 107 | // Note: Since |overrides_| is a std::map, iteration will be in alphabetical |
| 108 | // order. This not guaranteed to users of this function, but is useful for |
| 109 | // tests to assume the order. |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 110 | for (const auto& entry : overrides_) { |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 111 | std::string* target_list = nullptr; |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 112 | switch (entry.second.overridden_state) { |
asvitkine | 6d31c52e | 2016-03-22 15:37:52 | [diff] [blame] | 113 | case OVERRIDE_USE_DEFAULT: |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 114 | case OVERRIDE_ENABLE_FEATURE: |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 115 | target_list = enable_overrides; |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 116 | break; |
| 117 | case OVERRIDE_DISABLE_FEATURE: |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 118 | target_list = disable_overrides; |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 119 | break; |
| 120 | } |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 121 | |
| 122 | if (!target_list->empty()) |
| 123 | target_list->push_back(','); |
asvitkine | 6d31c52e | 2016-03-22 15:37:52 | [diff] [blame] | 124 | if (entry.second.overridden_state == OVERRIDE_USE_DEFAULT) |
| 125 | target_list->push_back('*'); |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 126 | target_list->append(entry.first); |
| 127 | if (entry.second.field_trial) { |
| 128 | target_list->push_back('<'); |
| 129 | target_list->append(entry.second.field_trial->trial_name()); |
| 130 | } |
asvitkine | 8634019 | 2015-12-01 00:45:29 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 134 | // static |
| 135 | bool FeatureList::IsEnabled(const Feature& feature) { |
| 136 | return GetInstance()->IsFeatureEnabled(feature); |
| 137 | } |
| 138 | |
| 139 | // static |
asvitkine | 03007d0 | 2015-10-21 22:50:06 | [diff] [blame] | 140 | std::vector<std::string> FeatureList::SplitFeatureListString( |
| 141 | const std::string& input) { |
| 142 | return SplitString(input, ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 143 | } |
| 144 | |
| 145 | // static |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame^] | 146 | bool FeatureList::InitializeInstance(const std::string& enable_features, |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 147 | const std::string& disable_features) { |
| 148 | // We want to initialize a new instance here to support command-line features |
| 149 | // in testing better. For example, we initialize a dummy instance in |
| 150 | // base/test/test_suite.cc, and override it in content/browser/ |
| 151 | // browser_main_loop.cc. |
| 152 | // On the other hand, we want to avoid re-initialization from command line. |
| 153 | // For example, we initialize an instance in chrome/browser/ |
| 154 | // chrome_browser_main.cc and do not override it in content/browser/ |
| 155 | // browser_main_loop.cc. |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame^] | 156 | bool instance_existed_before = false; |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 157 | if (g_instance) { |
| 158 | if (g_instance->initialized_from_command_line_) |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame^] | 159 | return false; |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 160 | |
| 161 | delete g_instance; |
| 162 | g_instance = nullptr; |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame^] | 163 | instance_existed_before = true; |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 164 | } |
| 165 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 166 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
changwan | 5b9da19 | 2016-03-31 07:36:19 | [diff] [blame] | 167 | feature_list->InitializeFromCommandLine(enable_features, disable_features); |
| 168 | base::FeatureList::SetInstance(std::move(feature_list)); |
asvitkine | e6be55d | 2016-04-04 23:29:50 | [diff] [blame^] | 169 | return !instance_existed_before; |
asvitkine | 9d96abf | 2015-11-02 21:52:08 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // static |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 173 | FeatureList* FeatureList::GetInstance() { |
| 174 | return g_instance; |
| 175 | } |
| 176 | |
| 177 | // static |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 178 | void FeatureList::SetInstance(std::unique_ptr<FeatureList> instance) { |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 179 | DCHECK(!g_instance); |
| 180 | instance->FinalizeInitialization(); |
| 181 | |
| 182 | // Note: Intentional leak of global singleton. |
| 183 | g_instance = instance.release(); |
| 184 | } |
| 185 | |
| 186 | // static |
| 187 | void FeatureList::ClearInstanceForTesting() { |
| 188 | delete g_instance; |
| 189 | g_instance = nullptr; |
| 190 | } |
| 191 | |
| 192 | void FeatureList::FinalizeInitialization() { |
| 193 | DCHECK(!initialized_); |
| 194 | initialized_ = true; |
| 195 | } |
| 196 | |
| 197 | bool FeatureList::IsFeatureEnabled(const Feature& feature) { |
| 198 | DCHECK(initialized_); |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 199 | DCHECK(IsValidFeatureOrFieldTrialName(feature.name)) << feature.name; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 200 | DCHECK(CheckFeatureIdentity(feature)) << feature.name; |
| 201 | |
| 202 | auto it = overrides_.find(feature.name); |
| 203 | if (it != overrides_.end()) { |
| 204 | const OverrideEntry& entry = it->second; |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 205 | |
| 206 | // Activate the corresponding field trial, if necessary. |
| 207 | if (entry.field_trial) |
| 208 | entry.field_trial->group(); |
| 209 | |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 210 | // TODO(asvitkine) Expand this section as more support is added. |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 211 | |
| 212 | // If marked as OVERRIDE_USE_DEFAULT, simply return the default state below. |
| 213 | if (entry.overridden_state != OVERRIDE_USE_DEFAULT) |
| 214 | return entry.overridden_state == OVERRIDE_ENABLE_FEATURE; |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 215 | } |
| 216 | // Otherwise, return the default state. |
| 217 | return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; |
| 218 | } |
| 219 | |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 220 | void FeatureList::RegisterOverridesFromCommandLine( |
| 221 | const std::string& feature_list, |
| 222 | OverrideState overridden_state) { |
| 223 | for (const auto& value : SplitFeatureListString(feature_list)) { |
| 224 | StringPiece feature_name(value); |
| 225 | base::FieldTrial* trial = nullptr; |
| 226 | |
| 227 | // The entry may be of the form FeatureName<FieldTrialName - in which case, |
| 228 | // this splits off the field trial name and associates it with the override. |
| 229 | std::string::size_type pos = feature_name.find('<'); |
| 230 | if (pos != std::string::npos) { |
| 231 | feature_name.set(value.data(), pos); |
| 232 | trial = base::FieldTrialList::Find(value.substr(pos + 1)); |
| 233 | } |
| 234 | |
| 235 | RegisterOverride(feature_name, overridden_state, trial); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void FeatureList::RegisterOverride(StringPiece feature_name, |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 240 | OverrideState overridden_state, |
| 241 | FieldTrial* field_trial) { |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 242 | DCHECK(!initialized_); |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 243 | if (field_trial) { |
| 244 | DCHECK(IsValidFeatureOrFieldTrialName(field_trial->trial_name())) |
| 245 | << field_trial->trial_name(); |
| 246 | } |
asvitkine | 6d31c52e | 2016-03-22 15:37:52 | [diff] [blame] | 247 | if (feature_name.starts_with("*")) { |
| 248 | feature_name = feature_name.substr(1); |
| 249 | overridden_state = OVERRIDE_USE_DEFAULT; |
| 250 | } |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 251 | |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 252 | // Note: The semantics of insert() is that it does not overwrite the entry if |
| 253 | // one already exists for the key. Thus, only the first override for a given |
| 254 | // feature name takes effect. |
| 255 | overrides_.insert(std::make_pair( |
asvitkine | b2e44d8 | 2015-12-01 04:10:28 | [diff] [blame] | 256 | feature_name.as_string(), OverrideEntry(overridden_state, field_trial))); |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | bool FeatureList::CheckFeatureIdentity(const Feature& feature) { |
| 260 | AutoLock auto_lock(feature_identity_tracker_lock_); |
| 261 | |
| 262 | auto it = feature_identity_tracker_.find(feature.name); |
| 263 | if (it == feature_identity_tracker_.end()) { |
| 264 | // If it's not tracked yet, register it. |
| 265 | feature_identity_tracker_[feature.name] = &feature; |
| 266 | return true; |
| 267 | } |
| 268 | // Compare address of |feature| to the existing tracked entry. |
| 269 | return it->second == &feature; |
| 270 | } |
| 271 | |
asvitkine | 8423d17 | 2015-09-28 23:23:44 | [diff] [blame] | 272 | FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state, |
| 273 | FieldTrial* field_trial) |
| 274 | : overridden_state(overridden_state), |
| 275 | field_trial(field_trial), |
| 276 | overridden_by_field_trial(field_trial != nullptr) {} |
asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame] | 277 | |
| 278 | } // namespace base |