Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/feature_list.h" | 5 #include "base/feature_list.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/field_trial.h" | |
| 10 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Pointer to the FeatureList instance singleton that was set via | 17 // Pointer to the FeatureList instance singleton that was set via |
| 17 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to | 18 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to |
| 18 // have more control over initialization timing. Leaky. | 19 // have more control over initialization timing. Leaky. |
| 19 FeatureList* g_instance = nullptr; | 20 FeatureList* g_instance = nullptr; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 30 FeatureList::~FeatureList() {} | 31 FeatureList::~FeatureList() {} |
| 31 | 32 |
| 32 void FeatureList::InitializeFromCommandLine( | 33 void FeatureList::InitializeFromCommandLine( |
| 33 const std::string& enable_features, | 34 const std::string& enable_features, |
| 34 const std::string& disable_features) { | 35 const std::string& disable_features) { |
| 35 DCHECK(!initialized_); | 36 DCHECK(!initialized_); |
| 36 | 37 |
| 37 // Process disabled features first, so that disabled ones take precedence over | 38 // Process disabled features first, so that disabled ones take precedence over |
| 38 // enabled ones (since RegisterOverride() uses insert()). | 39 // enabled ones (since RegisterOverride() uses insert()). |
| 39 for (const auto& feature_name : SplitFeatureListString(disable_features)) { | 40 for (const auto& feature_name : SplitFeatureListString(disable_features)) { |
| 40 RegisterOverride(feature_name, OVERRIDE_DISABLE_FEATURE); | 41 RegisterOverride(feature_name, OVERRIDE_DISABLE_FEATURE, nullptr); |
| 41 } | 42 } |
| 42 for (const auto& feature_name : SplitFeatureListString(enable_features)) { | 43 for (const auto& feature_name : SplitFeatureListString(enable_features)) { |
| 43 RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE); | 44 RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE, nullptr); |
| 44 } | 45 } |
| 45 } | 46 } |
| 46 | 47 |
| 48 void FeatureList::RegisterFieldTrialOverride(const std::string& feature_name, | |
| 49 OverrideState override_state, | |
| 50 FieldTrial* field_trial) { | |
| 51 RegisterOverride(feature_name, override_state, field_trial); | |
| 52 } | |
| 53 | |
| 54 FieldTrial* FeatureList::AssociateReportingFieldTrial( | |
| 55 const std::string& feature_name, | |
| 56 OverrideState for_overridden_state, | |
| 57 const std::string& field_trial_name, | |
| 58 const std::string& group_name) { | |
| 59 auto it = overrides_.find(feature_name); | |
| 60 // No association if there's no override entry or its state doesn't match | |
| 61 // |for_overridden_state|. | |
| 62 if (it == overrides_.end() || | |
| 63 it->second.overridden_state != for_overridden_state) { | |
| 64 return nullptr; | |
| 65 } | |
| 66 | |
| 67 // Only one associated field trial is supported per feature. This is generally | |
| 68 // enforced server-side. | |
| 69 if (it->second.field_trial) { | |
|
Ilya Sherman
2015/09/16 00:50:43
Maybe it's ok if the already set field trial match
Alexei Svitkine (slow)
2015/09/16 20:54:08
Is there a specific scenario you're imagining? In
Ilya Sherman
2015/09/17 01:02:46
I think you're right. I was still trying to wrap
| |
| 70 NOTREACHED(); | |
|
Ilya Sherman
2015/09/16 00:50:43
nit: Probably helpful to print out both field tria
Alexei Svitkine (slow)
2015/09/22 21:19:59
Done.
| |
| 71 return nullptr; | |
| 72 } | |
| 73 it->second.field_trial = | |
| 74 FieldTrialList::CreateFieldTrial(field_trial_name, group_name); | |
|
Ilya Sherman
2015/09/16 00:50:43
Hmm, why create the field trial? What guarantees
Alexei Svitkine (slow)
2015/09/16 20:54:08
The field trial should only be created through thi
Ilya Sherman
2015/09/17 01:02:46
I agree that it's simpler in a way, but it also se
Alexei Svitkine (slow)
2015/09/22 21:19:59
Yeah, I feel like the callback would be even harde
Ilya Sherman
2015/09/24 01:11:32
After thinking about this more, I think I actually
Alexei Svitkine (slow)
2015/09/24 20:49:20
I'm ok with this approach. It does let some code t
Ilya Sherman
2015/09/25 03:47:22
Nice, I like the explicit command line check even
| |
| 75 return it->second.field_trial; | |
| 76 } | |
| 77 | |
| 47 // static | 78 // static |
| 48 bool FeatureList::IsEnabled(const Feature& feature) { | 79 bool FeatureList::IsEnabled(const Feature& feature) { |
| 49 return GetInstance()->IsFeatureEnabled(feature); | 80 return GetInstance()->IsFeatureEnabled(feature); |
| 50 } | 81 } |
| 51 | 82 |
| 52 // static | 83 // static |
| 53 FeatureList* FeatureList::GetInstance() { | 84 FeatureList* FeatureList::GetInstance() { |
| 54 return g_instance; | 85 return g_instance; |
| 55 } | 86 } |
| 56 | 87 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 74 initialized_ = true; | 105 initialized_ = true; |
| 75 } | 106 } |
| 76 | 107 |
| 77 bool FeatureList::IsFeatureEnabled(const Feature& feature) { | 108 bool FeatureList::IsFeatureEnabled(const Feature& feature) { |
| 78 DCHECK(initialized_); | 109 DCHECK(initialized_); |
| 79 DCHECK(CheckFeatureIdentity(feature)) << feature.name; | 110 DCHECK(CheckFeatureIdentity(feature)) << feature.name; |
| 80 | 111 |
| 81 auto it = overrides_.find(feature.name); | 112 auto it = overrides_.find(feature.name); |
| 82 if (it != overrides_.end()) { | 113 if (it != overrides_.end()) { |
| 83 const OverrideEntry& entry = it->second; | 114 const OverrideEntry& entry = it->second; |
| 84 // TODO(asvitkine) Expand this section as more support is added. | 115 |
| 116 // Activate the corresponding field trial, if necessary. | |
| 117 if (entry.field_trial) | |
| 118 entry.field_trial->group(); | |
| 119 | |
| 85 return entry.overridden_state == OVERRIDE_ENABLE_FEATURE; | 120 return entry.overridden_state == OVERRIDE_ENABLE_FEATURE; |
| 86 } | 121 } |
| 87 // Otherwise, return the default state. | 122 // Otherwise, return the default state. |
| 88 return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; | 123 return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; |
| 89 } | 124 } |
| 90 | 125 |
| 91 void FeatureList::RegisterOverride(const std::string& feature_name, | 126 void FeatureList::RegisterOverride(const std::string& feature_name, |
| 92 OverrideState overridden_state) { | 127 OverrideState overridden_state, |
| 128 FieldTrial* field_trial) { | |
| 93 DCHECK(!initialized_); | 129 DCHECK(!initialized_); |
| 94 overrides_.insert(make_pair(feature_name, OverrideEntry(overridden_state))); | 130 overrides_.insert( |
| 131 make_pair(feature_name, OverrideEntry(overridden_state, field_trial))); | |
|
Ilya Sherman
2015/09/16 00:50:43
It's pretty subtle that this does not overwrite in
Alexei Svitkine (slow)
2015/09/22 21:19:59
Done.
| |
| 95 } | 132 } |
| 96 | 133 |
| 97 bool FeatureList::CheckFeatureIdentity(const Feature& feature) { | 134 bool FeatureList::CheckFeatureIdentity(const Feature& feature) { |
| 98 AutoLock auto_lock(feature_identity_tracker_lock_); | 135 AutoLock auto_lock(feature_identity_tracker_lock_); |
| 99 | 136 |
| 100 auto it = feature_identity_tracker_.find(feature.name); | 137 auto it = feature_identity_tracker_.find(feature.name); |
| 101 if (it == feature_identity_tracker_.end()) { | 138 if (it == feature_identity_tracker_.end()) { |
| 102 // If it's not tracked yet, register it. | 139 // If it's not tracked yet, register it. |
| 103 feature_identity_tracker_[feature.name] = &feature; | 140 feature_identity_tracker_[feature.name] = &feature; |
| 104 return true; | 141 return true; |
| 105 } | 142 } |
| 106 // Compare address of |feature| to the existing tracked entry. | 143 // Compare address of |feature| to the existing tracked entry. |
| 107 return it->second == &feature; | 144 return it->second == &feature; |
| 108 } | 145 } |
| 109 | 146 |
| 110 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state) | 147 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state, |
| 111 : overridden_state(overridden_state) {} | 148 FieldTrial* field_trial) |
| 149 : overridden_state(overridden_state), field_trial(field_trial) {} | |
| 112 | 150 |
| 113 } // namespace base | 151 } // namespace base |
| OLD | NEW |