Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: base/test/scoped_feature_list.cc

Issue 2850073002: Revert of Change ScopedFeatureList to overrides FeatureList not reset (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/test/scoped_feature_list.h ('k') | base/test/scoped_feature_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/test/scoped_feature_list.h" 5 #include "base/test/scoped_feature_list.h"
6 6
7 #include <algorithm>
8 #include <string> 7 #include <string>
9 #include <vector>
10
11 #include "base/stl_util.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 8
15 namespace base { 9 namespace base {
16 namespace test { 10 namespace test {
17 11
18 namespace { 12 namespace {
19 13
20 std::vector<StringPiece> GetFeatureVector( 14 static std::string GetFeatureString(
21 const std::initializer_list<base::Feature>& features) { 15 const std::initializer_list<base::Feature>& features) {
22 std::vector<StringPiece> output; 16 std::string output;
23 for (const base::Feature& feature : features) { 17 for (const base::Feature& feature : features) {
24 output.push_back(feature.name); 18 if (!output.empty())
19 output += ",";
20 output += feature.name;
25 } 21 }
26
27 return output; 22 return output;
28 } 23 }
29 24
30 // Extracts a feature name from a feature state string. For example, given
31 // the input "*MyLovelyFeature<SomeFieldTrial", returns "MyLovelyFeature".
32 StringPiece GetFeatureName(StringPiece feature) {
33 StringPiece feature_name = feature;
34
35 // Remove default info.
36 if (feature_name.starts_with("*"))
37 feature_name = feature_name.substr(1);
38
39 // Remove field_trial info.
40 std::size_t index = feature_name.find("<");
41 if (index != std::string::npos)
42 feature_name = feature_name.substr(0, index);
43
44 return feature_name;
45 }
46
47 struct Features {
48 std::vector<StringPiece> enabled_feature_list;
49 std::vector<StringPiece> disabled_feature_list;
50 };
51
52 // Merges previously-specified feature overrides with those passed into one of
53 // the Init() methods. |features| should be a list of features previously
54 // overridden to be in the |override_state|. |merged_features| should contain
55 // the enabled and disabled features passed into the Init() method, plus any
56 // overrides merged as a result of previous calls to this function.
57 void OverrideFeatures(const std::string& features,
58 base::FeatureList::OverrideState override_state,
59 Features* merged_features) {
60 std::vector<StringPiece> features_list =
61 SplitStringPiece(features, ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
62
63 for (StringPiece feature : features_list) {
64 StringPiece feature_name = GetFeatureName(feature);
65
66 if (ContainsValue(merged_features->enabled_feature_list, feature_name) ||
67 ContainsValue(merged_features->disabled_feature_list, feature_name))
68 continue;
69
70 if (override_state == FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE) {
71 merged_features->enabled_feature_list.push_back(feature);
72 } else {
73 DCHECK_EQ(override_state,
74 FeatureList::OverrideState::OVERRIDE_DISABLE_FEATURE);
75 merged_features->disabled_feature_list.push_back(feature);
76 }
77 }
78 }
79
80 } // namespace 25 } // namespace
81 26
82 ScopedFeatureList::ScopedFeatureList() {} 27 ScopedFeatureList::ScopedFeatureList() {}
83 28
84 ScopedFeatureList::~ScopedFeatureList() { 29 ScopedFeatureList::~ScopedFeatureList() {
85 if (original_feature_list_) { 30 if (original_feature_list_) {
86 base::FeatureList::ClearInstanceForTesting(); 31 base::FeatureList::ClearInstanceForTesting();
87 base::FeatureList::RestoreInstanceForTesting( 32 base::FeatureList::RestoreInstanceForTesting(
88 std::move(original_feature_list_)); 33 std::move(original_feature_list_));
89 } 34 }
90 } 35 }
91 36
92 void ScopedFeatureList::Init() { 37 void ScopedFeatureList::Init() {
93 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 38 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
94 feature_list->InitializeFromCommandLine(std::string(), std::string()); 39 feature_list->InitializeFromCommandLine(std::string(), std::string());
95 InitWithFeatureList(std::move(feature_list)); 40 InitWithFeatureList(std::move(feature_list));
96 } 41 }
97 42
43 void ScopedFeatureList::InitWithFeatures(
44 const std::initializer_list<base::Feature>& enabled_features,
45 const std::initializer_list<base::Feature>& disabled_features) {
46 InitFromCommandLine(GetFeatureString(enabled_features),
47 GetFeatureString(disabled_features));
48 }
49
98 void ScopedFeatureList::InitWithFeatureList( 50 void ScopedFeatureList::InitWithFeatureList(
99 std::unique_ptr<FeatureList> feature_list) { 51 std::unique_ptr<FeatureList> feature_list) {
100 DCHECK(!original_feature_list_); 52 DCHECK(!original_feature_list_);
101 original_feature_list_ = base::FeatureList::ClearInstanceForTesting(); 53 original_feature_list_ = base::FeatureList::ClearInstanceForTesting();
102 base::FeatureList::SetInstance(std::move(feature_list)); 54 base::FeatureList::SetInstance(std::move(feature_list));
103 } 55 }
104 56
105 void ScopedFeatureList::InitFromCommandLine( 57 void ScopedFeatureList::InitFromCommandLine(
106 const std::string& enable_features, 58 const std::string& enable_features,
107 const std::string& disable_features) { 59 const std::string& disable_features) {
108 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 60 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
109 feature_list->InitializeFromCommandLine(enable_features, disable_features); 61 feature_list->InitializeFromCommandLine(enable_features, disable_features);
110 InitWithFeatureList(std::move(feature_list)); 62 InitWithFeatureList(std::move(feature_list));
111 } 63 }
112 64
113 void ScopedFeatureList::InitWithFeatures(
114 const std::initializer_list<base::Feature>& enabled_features,
115 const std::initializer_list<base::Feature>& disabled_features) {
116 Features merged_features;
117 merged_features.enabled_feature_list = GetFeatureVector(enabled_features);
118 merged_features.disabled_feature_list = GetFeatureVector(disabled_features);
119
120 base::FeatureList* feature_list = base::FeatureList::GetInstance();
121
122 // |current_enabled_features| and |current_disabled_features| must declare out
123 // of if scope to avoid them out of scope before JoinString calls because
124 // |merged_features| may contains StringPiece which holding pointer points to
125 // |current_enabled_features| and |current_disabled_features|.
126 std::string current_enabled_features;
127 std::string current_disabled_features;
128 if (feature_list) {
129 base::FeatureList::GetInstance()->GetFeatureOverrides(
130 &current_enabled_features, &current_disabled_features);
131 OverrideFeatures(current_enabled_features,
132 FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE,
133 &merged_features);
134 OverrideFeatures(current_disabled_features,
135 FeatureList::OverrideState::OVERRIDE_DISABLE_FEATURE,
136 &merged_features);
137 }
138
139 std::string enabled = JoinString(merged_features.enabled_feature_list, ",");
140 std::string disabled = JoinString(merged_features.disabled_feature_list, ",");
141 InitFromCommandLine(enabled, disabled);
142 }
143
144 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) { 65 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) {
145 InitWithFeatures({feature}, {}); 66 InitFromCommandLine(feature.name, std::string());
146 } 67 }
147 68
148 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) { 69 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) {
149 InitWithFeatures({}, {feature}); 70 InitFromCommandLine(std::string(), feature.name);
150 } 71 }
151 72
152 } // namespace test 73 } // namespace test
153 } // namespace base 74 } // namespace base
OLDNEW
« no previous file with comments | « base/test/scoped_feature_list.h ('k') | base/test/scoped_feature_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698