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 "components/subresource_filter/core/browser/subresource_filter_features .h" | 5 #include "components/subresource_filter/core/browser/subresource_filter_features .h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <string> | 8 #include <string> |
9 #include <tuple> | |
8 #include <utility> | 10 #include <utility> |
9 | 11 |
10 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
11 #include "base/metrics/field_trial_params.h" | 13 #include "base/metrics/field_trial_params.h" |
12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
14 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
16 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
17 #include "components/variations/variations_associated_data.h" | 19 #include "components/variations/variations_associated_data.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 configuration.ruleset_flavor = | 107 configuration.ruleset_flavor = |
106 TakeVariationParamOrReturnEmpty(¶ms, kRulesetFlavorParameterName); | 108 TakeVariationParamOrReturnEmpty(¶ms, kRulesetFlavorParameterName); |
107 | 109 |
108 configuration.should_whitelist_site_on_reload = | 110 configuration.should_whitelist_site_on_reload = |
109 ParseBool(TakeVariationParamOrReturnEmpty( | 111 ParseBool(TakeVariationParamOrReturnEmpty( |
110 ¶ms, kWhitelistSiteOnReloadParameterName)); | 112 ¶ms, kWhitelistSiteOnReloadParameterName)); |
111 | 113 |
112 return configuration; | 114 return configuration; |
113 } | 115 } |
114 | 116 |
117 std::vector<Configuration> MakeConfigVector(Configuration config) { | |
118 std::vector<Configuration> configs; | |
119 configs.push_back(std::move(config)); | |
120 return configs; | |
121 } | |
122 | |
123 size_t GetActivationLevelPriority(ActivationLevel activation_level) { | |
124 constexpr ActivationLevel kValuesInIncreasingOrderOfPriority[] = { | |
Charlie Harrison
2017/04/26 21:28:11
I see the appeal here but maybe it would just be e
engedy
2017/05/03 12:04:45
Done.
| |
125 ActivationLevel::DISABLED, ActivationLevel::DRYRUN, | |
126 ActivationLevel::ENABLED}; | |
127 static_assert(ActivationLevel::LAST == ActivationLevel::ENABLED, | |
128 "Priority not defined for ActivationLevel."); | |
129 const auto* const it = | |
130 std::find(std::begin(kValuesInIncreasingOrderOfPriority), | |
131 std::end(kValuesInIncreasingOrderOfPriority), activation_level); | |
132 DCHECK(it < std::end(kValuesInIncreasingOrderOfPriority)); | |
133 return it - std::begin(kValuesInIncreasingOrderOfPriority); | |
134 } | |
135 | |
136 size_t GetActivationScopePriority(ActivationScope activation_scope) { | |
137 // The more specific ACTIVATION_LIST trumps the blanket scopes. | |
138 constexpr ActivationScope kValuesInIncreasingOrderOfPriority[] = { | |
139 ActivationScope::NO_SITES, ActivationScope::ALL_SITES, | |
140 ActivationScope::ACTIVATION_LIST}; | |
141 static_assert(ActivationScope::LAST == ActivationScope::ALL_SITES, | |
142 "Priority not defined for ActivationScope."); | |
143 const auto* const it = | |
144 std::find(std::begin(kValuesInIncreasingOrderOfPriority), | |
145 std::end(kValuesInIncreasingOrderOfPriority), activation_scope); | |
146 DCHECK(it < std::end(kValuesInIncreasingOrderOfPriority)); | |
147 return it - std::begin(kValuesInIncreasingOrderOfPriority); | |
148 } | |
149 | |
150 // Returns the priority of the |configuration|, where a greater value indicates | |
151 // higher priority. | |
152 std::tuple<size_t, size_t, size_t> GetConfigurationPriority( | |
153 const Configuration& configuration) { | |
154 return std::make_tuple( | |
155 GetActivationLevelPriority(configuration.activation_level), | |
156 GetActivationScopePriority(configuration.activation_scope), | |
157 static_cast<size_t>(configuration.activation_list)); | |
158 } | |
159 | |
160 std::vector<Configuration> SortConfigsByDecreasingPriority( | |
161 std::vector<Configuration> configs) { | |
162 std::sort(configs.begin(), configs.end(), | |
163 [](const Configuration& a, const Configuration& b) { | |
164 return GetConfigurationPriority(a) > GetConfigurationPriority(b); | |
Charlie Harrison
2017/04/26 21:28:11
How does this operator work (can you include a com
engedy
2017/05/03 12:04:45
Yep, added comment.
| |
165 }); | |
166 return configs; | |
167 } | |
168 | |
115 base::LazyInstance<base::Lock>::Leaky g_active_configurations_lock = | 169 base::LazyInstance<base::Lock>::Leaky g_active_configurations_lock = |
116 LAZY_INSTANCE_INITIALIZER; | 170 LAZY_INSTANCE_INITIALIZER; |
117 | 171 |
118 base::LazyInstance<scoped_refptr<ConfigurationList>>::Leaky | 172 base::LazyInstance<scoped_refptr<ConfigurationList>>::Leaky |
119 g_active_configurations = LAZY_INSTANCE_INITIALIZER; | 173 g_active_configurations = LAZY_INSTANCE_INITIALIZER; |
120 | 174 |
121 } // namespace | 175 } // namespace |
122 | 176 |
123 const base::Feature kSafeBrowsingSubresourceFilter{ | 177 const base::Feature kSafeBrowsingSubresourceFilter{ |
124 "SubresourceFilter", base::FEATURE_DISABLED_BY_DEFAULT}; | 178 "SubresourceFilter", base::FEATURE_DISABLED_BY_DEFAULT}; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 Configuration::Configuration(ActivationLevel activation_level, | 210 Configuration::Configuration(ActivationLevel activation_level, |
157 ActivationScope activation_scope, | 211 ActivationScope activation_scope, |
158 ActivationList activation_list) | 212 ActivationList activation_list) |
159 : activation_level(activation_level), | 213 : activation_level(activation_level), |
160 activation_scope(activation_scope), | 214 activation_scope(activation_scope), |
161 activation_list(activation_list) {} | 215 activation_list(activation_list) {} |
162 Configuration::Configuration(Configuration&&) = default; | 216 Configuration::Configuration(Configuration&&) = default; |
163 Configuration::~Configuration() = default; | 217 Configuration::~Configuration() = default; |
164 Configuration& Configuration::operator=(Configuration&&) = default; | 218 Configuration& Configuration::operator=(Configuration&&) = default; |
165 | 219 |
220 ConfigurationList::ConfigurationList(std::vector<Configuration> configs) | |
221 : ordered_configs_(SortConfigsByDecreasingPriority(std::move(configs))) {} | |
166 ConfigurationList::ConfigurationList(Configuration config) | 222 ConfigurationList::ConfigurationList(Configuration config) |
167 : config_(std::move(config)) {} | 223 : ConfigurationList(MakeConfigVector(std::move(config))) {} |
168 ConfigurationList::~ConfigurationList() = default; | 224 ConfigurationList::~ConfigurationList() = default; |
169 | 225 |
226 std::string ConfigurationList::GetMostSpecificRulesetFlavor() const { | |
Charlie Harrison
2017/04/26 21:28:11
Please comment what "most specific" means and how
engedy
2017/05/03 12:04:45
Documented in the header, and cleaned up the imple
| |
227 if (ordered_configs_.empty()) | |
228 return std::string(); | |
229 const auto it_max = std::max_element( | |
230 ordered_configs_.begin(), ordered_configs_.end(), | |
231 [](const Configuration& a, const Configuration& b) { | |
232 if (a.ruleset_flavor.size() == b.ruleset_flavor.size()) | |
233 return a.ruleset_flavor < b.ruleset_flavor; | |
234 return a.ruleset_flavor.size() < b.ruleset_flavor.size(); | |
235 }); | |
236 DCHECK(it_max != ordered_configs_.end()); | |
237 return it_max->ruleset_flavor; | |
238 } | |
239 | |
170 scoped_refptr<ConfigurationList> GetActiveConfigurations() { | 240 scoped_refptr<ConfigurationList> GetActiveConfigurations() { |
171 base::AutoLock lock(g_active_configurations_lock.Get()); | 241 base::AutoLock lock(g_active_configurations_lock.Get()); |
172 if (!g_active_configurations.Get()) { | 242 if (!g_active_configurations.Get()) { |
173 g_active_configurations.Get() = | 243 g_active_configurations.Get() = |
174 base::MakeShared<ConfigurationList>(ParseFieldTrialConfiguration()); | 244 base::MakeShared<ConfigurationList>(ParseFieldTrialConfiguration()); |
175 } | 245 } |
176 return g_active_configurations.Get(); | 246 return g_active_configurations.Get(); |
177 } | 247 } |
178 | 248 |
179 namespace testing { | 249 namespace testing { |
180 | 250 |
181 scoped_refptr<ConfigurationList> GetAndSetActivateConfigurations( | 251 scoped_refptr<ConfigurationList> GetAndSetActivateConfigurations( |
182 scoped_refptr<ConfigurationList> new_configs) { | 252 scoped_refptr<ConfigurationList> new_configs) { |
183 base::AutoLock lock(g_active_configurations_lock.Get()); | 253 base::AutoLock lock(g_active_configurations_lock.Get()); |
184 auto old_configs = std::move(g_active_configurations.Get()); | 254 auto old_configs = std::move(g_active_configurations.Get()); |
185 g_active_configurations.Get() = std::move(new_configs); | 255 g_active_configurations.Get() = std::move(new_configs); |
186 return old_configs; | 256 return old_configs; |
187 } | 257 } |
188 | 258 |
189 } // namespace testing | 259 } // namespace testing |
190 | 260 |
191 } // namespace subresource_filter | 261 } // namespace subresource_filter |
OLD | NEW |