Plumb base::Feature field trial association to renderers.
This CL makes the FeatureList command-line initialization code
support an optional FieldTrial name for each feature that will
be associated with the feature override when specified. Also
changes the code that generates this command-line to also
include this name.
With this change, renderers will be launched with this
association specified on the command-line, so that querying
a Feature in the renderer only that's associated with a field
trial will result in that trial being activated (and sync'd with
the browser process by the existing mechanism).
Depends on:
https://codereview.chromium.org/1471693007
BUG=561077
Review URL: https://codereview.chromium.org/1484483002
Cr-Commit-Position: refs/heads/master@{#362343}
CrOS-Libchrome-Original-Commit: b2e44d8215dd9d4ed6316c6696b576cf77cb363c
diff --git a/base/feature_list.cc b/base/feature_list.cc
index c06ba8b..104bf08 100644
--- a/base/feature_list.cc
+++ b/base/feature_list.cc
@@ -10,6 +10,7 @@
#include "base/logging.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
namespace base {
@@ -20,6 +21,16 @@
// have more control over initialization timing. Leaky.
FeatureList* g_instance = nullptr;
+// Some characters are not allowed to appear in feature names or the associated
+// field trial names, as they are used as special characters for command-line
+// serialization. This function checks that the strings are ASCII (since they
+// are used in command-line API functions that require ASCII) and whether there
+// are any reserved characters present, returning true if the string is valid.
+// Only called in DCHECKs.
+bool IsValidFeatureOrFieldTrialName(const std::string& name) {
+ return IsStringASCII(name) && name.find_first_of(",<") == std::string::npos;
+}
+
} // namespace
FeatureList::FeatureList() : initialized_(false) {}
@@ -33,12 +44,8 @@
// Process disabled features first, so that disabled ones take precedence over
// enabled ones (since RegisterOverride() uses insert()).
- for (const auto& feature_name : SplitFeatureListString(disable_features)) {
- RegisterOverride(feature_name, OVERRIDE_DISABLE_FEATURE, nullptr);
- }
- for (const auto& feature_name : SplitFeatureListString(enable_features)) {
- RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE, nullptr);
- }
+ RegisterOverridesFromCommandLine(disable_features, OVERRIDE_DISABLE_FEATURE);
+ RegisterOverridesFromCommandLine(enable_features, OVERRIDE_ENABLE_FEATURE);
}
bool FeatureList::IsFeatureOverriddenFromCommandLine(
@@ -91,18 +98,23 @@
disable_overrides->clear();
for (const auto& entry : overrides_) {
+ std::string* target_list = nullptr;
switch (entry.second.overridden_state) {
case OVERRIDE_ENABLE_FEATURE:
- if (!enable_overrides->empty())
- enable_overrides->push_back(',');
- enable_overrides->append(entry.first);
+ target_list = enable_overrides;
break;
case OVERRIDE_DISABLE_FEATURE:
- if (!disable_overrides->empty())
- disable_overrides->push_back(',');
- disable_overrides->append(entry.first);
+ target_list = disable_overrides;
break;
}
+
+ if (!target_list->empty())
+ target_list->push_back(',');
+ target_list->append(entry.first);
+ if (entry.second.field_trial) {
+ target_list->push_back('<');
+ target_list->append(entry.second.field_trial->trial_name());
+ }
}
}
@@ -151,6 +163,7 @@
bool FeatureList::IsFeatureEnabled(const Feature& feature) {
DCHECK(initialized_);
+ DCHECK(IsValidFeatureOrFieldTrialName(feature.name)) << feature.name;
DCHECK(CheckFeatureIdentity(feature)) << feature.name;
auto it = overrides_.find(feature.name);
@@ -168,15 +181,39 @@
return feature.default_state == FEATURE_ENABLED_BY_DEFAULT;
}
-void FeatureList::RegisterOverride(const std::string& feature_name,
+void FeatureList::RegisterOverridesFromCommandLine(
+ const std::string& feature_list,
+ OverrideState overridden_state) {
+ for (const auto& value : SplitFeatureListString(feature_list)) {
+ StringPiece feature_name(value);
+ base::FieldTrial* trial = nullptr;
+
+ // The entry may be of the form FeatureName<FieldTrialName - in which case,
+ // this splits off the field trial name and associates it with the override.
+ std::string::size_type pos = feature_name.find('<');
+ if (pos != std::string::npos) {
+ feature_name.set(value.data(), pos);
+ trial = base::FieldTrialList::Find(value.substr(pos + 1));
+ }
+
+ RegisterOverride(feature_name, overridden_state, trial);
+ }
+}
+
+void FeatureList::RegisterOverride(StringPiece feature_name,
OverrideState overridden_state,
FieldTrial* field_trial) {
DCHECK(!initialized_);
+ if (field_trial) {
+ DCHECK(IsValidFeatureOrFieldTrialName(field_trial->trial_name()))
+ << field_trial->trial_name();
+ }
+
// Note: The semantics of insert() is that it does not overwrite the entry if
// one already exists for the key. Thus, only the first override for a given
// feature name takes effect.
overrides_.insert(std::make_pair(
- feature_name, OverrideEntry(overridden_state, field_trial)));
+ feature_name.as_string(), OverrideEntry(overridden_state, field_trial)));
}
bool FeatureList::CheckFeatureIdentity(const Feature& feature) {
diff --git a/base/feature_list.h b/base/feature_list.h
index 5091a33..3967263 100644
--- a/base/feature_list.h
+++ b/base/feature_list.h
@@ -13,6 +13,7 @@
#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_piece.h"
#include "base/synchronization/lock.h"
namespace base {
@@ -81,8 +82,10 @@
// Initializes feature overrides via command-line flags |enable_features| and
// |disable_features|, each of which is a comma-separated list of features to
// enable or disable, respectively. If a feature appears on both lists, then
- // it will be disabled. Must only be invoked during the initialization phase
- // (before FinalizeInitialization() has been called).
+ // it will be disabled. If a list entry has the format "FeatureName<TrialName"
+ // then this initialization will also associate the feature state override
+ // with the named field trial, if it exists. Must only be invoked during the
+ // initialization phase (before FinalizeInitialization() has been called).
void InitializeFromCommandLine(const std::string& enable_features,
const std::string& disable_features);
@@ -119,8 +122,11 @@
// Returns comma-separated lists of feature names (in the same format that is
// accepted by InitializeFromCommandLine()) corresponding to features that
- // have been overridden - either through command-line or via FieldTrials.
- // Must be called only after the instance has been initialized and registered.
+ // have been overridden - either through command-line or via FieldTrials. For
+ // those features that have an associated FieldTrial, the output entry will be
+ // of the format "FeatureName<TrialName", where "TrialName" is the name of the
+ // FieldTrial. Must be called only after the instance has been initialized and
+ // registered.
void GetFeatureOverrides(std::string* enable_overrides,
std::string* disable_overrides);
@@ -185,6 +191,13 @@
// Requires the FeatureList to have already been fully initialized.
bool IsFeatureEnabled(const Feature& feature);
+ // For each feature name in comma-separated list of strings |feature_list|,
+ // registers an override with the specified |overridden_state|. Also, will
+ // associate an optional named field trial if the entry is of the format
+ // "FeatureName<TrialName".
+ void RegisterOverridesFromCommandLine(const std::string& feature_list,
+ OverrideState overridden_state);
+
// Registers an override for feature |feature_name|. The override specifies
// whether the feature should be on or off (via |overridden_state|), which
// will take precedence over the feature's default state. If |field_trial| is
@@ -192,7 +205,7 @@
// the feature, which will activate the field trial when the feature state is
// queried. If an override is already registered for the given feature, it
// will not be changed.
- void RegisterOverride(const std::string& feature_name,
+ void RegisterOverride(StringPiece feature_name,
OverrideState overridden_state,
FieldTrial* field_trial);
diff --git a/base/feature_list_unittest.cc b/base/feature_list_unittest.cc
index 414c74a..29c1f16 100644
--- a/base/feature_list_unittest.cc
+++ b/base/feature_list_unittest.cc
@@ -335,8 +335,21 @@
std::string disable_features;
FeatureList::GetInstance()->GetFeatureOverrides(&enable_features,
&disable_features);
- EXPECT_EQ("A,OffByDefault,X", SortFeatureListString(enable_features));
+ EXPECT_EQ("A,OffByDefault<Trial,X", SortFeatureListString(enable_features));
EXPECT_EQ("D", SortFeatureListString(disable_features));
}
+TEST_F(FeatureListTest, InitializeFromCommandLine_WithFieldTrials) {
+ ClearFeatureListInstance();
+ FieldTrialList field_trial_list(nullptr);
+ FieldTrialList::CreateFieldTrial("Trial", "Group");
+ scoped_ptr<FeatureList> feature_list(new FeatureList);
+ feature_list->InitializeFromCommandLine("A,OffByDefault<Trial,X", "D");
+ RegisterFeatureListInstance(feature_list.Pass());
+
+ EXPECT_FALSE(FieldTrialList::IsTrialActive("Trial"));
+ EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault));
+ EXPECT_TRUE(FieldTrialList::IsTrialActive("Trial"));
+}
+
} // namespace base