OLD | NEW |
---|---|
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> | |
7 #include <string> | 8 #include <string> |
9 #include <vector> | |
10 | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/string_util.h" | |
8 | 13 |
9 namespace base { | 14 namespace base { |
10 namespace test { | 15 namespace test { |
11 | 16 |
12 namespace { | 17 namespace { |
13 | 18 |
14 static std::string GetFeatureString( | 19 static std::vector<std::string> GetFeatureString( |
15 const std::initializer_list<base::Feature>& features) { | 20 const std::initializer_list<base::Feature>& features) { |
16 std::string output; | 21 std::vector<std::string> output; |
17 for (const base::Feature& feature : features) { | 22 for (const base::Feature& feature : features) { |
18 if (!output.empty()) | 23 output.push_back(feature.name); |
19 output += ","; | |
20 output += feature.name; | |
21 } | 24 } |
22 return output; | 25 return output; |
23 } | 26 } |
24 | 27 |
25 } // namespace | 28 } // namespace |
26 | 29 |
27 ScopedFeatureList::ScopedFeatureList() {} | 30 ScopedFeatureList::ScopedFeatureList() {} |
28 | 31 |
29 ScopedFeatureList::~ScopedFeatureList() { | 32 ScopedFeatureList::~ScopedFeatureList() { |
30 if (original_feature_list_) { | 33 if (original_feature_list_) { |
31 base::FeatureList::ClearInstanceForTesting(); | 34 base::FeatureList::ClearInstanceForTesting(); |
32 base::FeatureList::RestoreInstanceForTesting( | 35 base::FeatureList::RestoreInstanceForTesting( |
33 std::move(original_feature_list_)); | 36 std::move(original_feature_list_)); |
34 } | 37 } |
35 } | 38 } |
36 | 39 |
37 void ScopedFeatureList::Init() { | 40 void ScopedFeatureList::Init() { |
38 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 41 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
39 feature_list->InitializeFromCommandLine(std::string(), std::string()); | 42 feature_list->InitializeFromCommandLine(std::string(), std::string()); |
40 InitWithFeatureList(std::move(feature_list)); | 43 InitWithFeatureList(std::move(feature_list)); |
41 } | 44 } |
42 | 45 |
43 void ScopedFeatureList::InitWithFeatures( | 46 void ScopedFeatureList::InitWithFeatures( |
44 const std::initializer_list<base::Feature>& enabled_features, | 47 const std::initializer_list<base::Feature>& enabled_features, |
45 const std::initializer_list<base::Feature>& disabled_features) { | 48 const std::initializer_list<base::Feature>& disabled_features) { |
Ilya Sherman
2017/04/20 20:53:07
Could this still be the main entry point? The API
chaopeng
2017/04/21 02:36:21
Yes.
| |
46 InitFromCommandLine(GetFeatureString(enabled_features), | 49 InitWithFeatureListAndOverrides(GetFeatureString(enabled_features), |
47 GetFeatureString(disabled_features)); | 50 GetFeatureString(disabled_features)); |
48 } | 51 } |
49 | 52 |
50 void ScopedFeatureList::InitWithFeatureList( | 53 void ScopedFeatureList::InitWithFeatureList( |
51 std::unique_ptr<FeatureList> feature_list) { | 54 std::unique_ptr<FeatureList> feature_list) { |
Ilya Sherman
2017/04/20 20:53:07
Is this method really needed, or could it be repla
chaopeng
2017/04/21 02:36:21
This method has lot calls. I think it is better to
| |
52 DCHECK(!original_feature_list_); | 55 DCHECK(!original_feature_list_); |
53 original_feature_list_ = base::FeatureList::ClearInstanceForTesting(); | 56 original_feature_list_ = base::FeatureList::ClearInstanceForTesting(); |
54 base::FeatureList::SetInstance(std::move(feature_list)); | 57 base::FeatureList::SetInstance(std::move(feature_list)); |
55 } | 58 } |
56 | 59 |
57 void ScopedFeatureList::InitFromCommandLine( | 60 void ScopedFeatureList::InitFromCommandLine( |
58 const std::string& enable_features, | 61 const std::string& enable_features, |
59 const std::string& disable_features) { | 62 const std::string& disable_features) { |
Ilya Sherman
2017/04/20 20:53:07
Could this either be removed or call into InitWith
chaopeng
2017/04/21 02:36:21
same as below.
| |
60 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 63 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
61 feature_list->InitializeFromCommandLine(enable_features, disable_features); | 64 feature_list->InitializeFromCommandLine(enable_features, disable_features); |
62 InitWithFeatureList(std::move(feature_list)); | 65 InitWithFeatureList(std::move(feature_list)); |
63 } | 66 } |
64 | 67 |
65 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) { | 68 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) { |
66 InitFromCommandLine(feature.name, std::string()); | 69 std::vector<std::string> feature_list{feature.name}; |
70 InitWithFeatureListAndOverrides(feature_list, std::vector<std::string>()); | |
Ilya Sherman
2017/04/20 20:53:07
nit: I think this could be simplified to
InitWith
chaopeng
2017/04/21 02:36:21
Done.
| |
67 } | 71 } |
68 | 72 |
69 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) { | 73 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) { |
70 InitFromCommandLine(std::string(), feature.name); | 74 std::vector<std::string> feature_list{feature.name}; |
75 InitWithFeatureListAndOverrides(std::vector<std::string>(), feature_list); | |
76 } | |
77 | |
78 void OverrideFeature(const std::string& features, | |
Ilya Sherman
2017/04/20 20:53:06
Please add documentation for this function.
Ilya Sherman
2017/04/20 20:53:07
nit: s/Feature/Features
chaopeng
2017/04/21 02:36:21
Done.
| |
79 std::vector<std::string>* check_list, | |
80 std::vector<std::string>* add_to_list) { | |
Ilya Sherman
2017/04/20 20:53:07
I'd suggest writing this method like so:
void Add
chaopeng
2017/04/21 02:36:21
Done.
| |
81 std::vector<StringPiece> features_list = | |
82 SplitStringPiece(features, ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); | |
83 | |
84 for (StringPiece& feature : features_list) { | |
85 // Start with * means use default. | |
86 if (feature.starts_with("*")) | |
87 return; | |
Ilya Sherman
2017/04/20 20:53:07
Should this be a continue instead? (If so, please
chaopeng
2017/04/21 02:36:21
Yes, we should keep it.
| |
88 | |
89 // Remove field_trial info. | |
90 StringPiece feature_name = feature; | |
91 std::size_t index = feature.find("<"); | |
92 if (index != std::string::npos) | |
93 feature_name = feature.substr(0, index); | |
Ilya Sherman
2017/04/20 20:53:07
Hmm, why is the field trial info being removed? S
chaopeng
2017/04/21 02:36:21
Not remove it now.
| |
94 | |
95 if (std::any_of( | |
Ilya Sherman
2017/04/20 20:53:07
nit: std::find?
chaopeng
2017/04/21 02:36:21
change to count
| |
96 check_list->begin(), check_list->end(), | |
97 [&feature_name](std::string& s) { return feature_name == s; })) | |
98 return; | |
Ilya Sherman
2017/04/20 20:53:07
Ditto regarding continue vs. return.
chaopeng
2017/04/21 02:36:22
Done.
| |
99 | |
100 if (std::any_of( | |
101 add_to_list->begin(), add_to_list->end(), | |
102 [&feature_name](std::string& s) { return feature_name == s; })) | |
103 return; | |
Ilya Sherman
2017/04/20 20:53:07
Ditto regarding continue vs. return.
chaopeng
2017/04/21 02:36:21
Done.
| |
104 | |
105 add_to_list->push_back(feature_name.as_string()); | |
106 } | |
107 } | |
Ilya Sherman
2017/04/20 20:53:07
nit: Please move this into the anonymous namespace
chaopeng
2017/04/21 02:36:21
Done.
| |
108 | |
109 void ScopedFeatureList::InitWithFeatureListAndOverrides( | |
110 const std::vector<std::string>& override_enabled_features, | |
111 const std::vector<std::string>& override_disabled_features) { | |
112 std::vector<std::string> merged_enabled_features(override_enabled_features); | |
113 std::vector<std::string> merged_disabled_features(override_disabled_features); | |
114 | |
115 base::FeatureList* feature_list = base::FeatureList::GetInstance(); | |
116 if (feature_list) { | |
117 std::string enabled_features; | |
118 std::string disabled_features; | |
119 base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, | |
120 &disabled_features); | |
121 OverrideFeature(enabled_features, &merged_disabled_features, | |
122 &merged_enabled_features); | |
123 OverrideFeature(disabled_features, &merged_disabled_features, | |
124 &merged_enabled_features); | |
Ilya Sherman
2017/04/20 20:53:07
I think the latter two args are swapped? Please a
chaopeng
2017/04/21 02:36:21
Done.
| |
125 } | |
126 | |
127 std::string enabled = JoinString(merged_enabled_features, ","); | |
128 std::string disabled = JoinString(merged_disabled_features, ","); | |
129 InitFromCommandLine(enabled, disabled); | |
71 } | 130 } |
72 | 131 |
73 } // namespace test | 132 } // namespace test |
74 } // namespace base | 133 } // namespace base |
OLD | NEW |