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

Side by Side Diff: components/subresource_filter/core/browser/subresource_filter_features.h

Issue 2844063002: Add support for multiple simultaneous subresource_filter::Configurations. (Closed)
Patch Set: Polish + rebase. 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
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 #ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURES_H _ 5 #ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURES_H _
6 #define COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURES_H _ 6 #define COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURES_H _
7 7
8 #include <iosfwd>
9 #include <vector>
10
8 #include "base/feature_list.h" 11 #include "base/feature_list.h"
9 #include "base/macros.h" 12 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
11 #include "components/subresource_filter/core/common/activation_level.h" 14 #include "components/subresource_filter/core/common/activation_level.h"
12 #include "components/subresource_filter/core/common/activation_list.h" 15 #include "components/subresource_filter/core/common/activation_list.h"
13 #include "components/subresource_filter/core/common/activation_scope.h" 16 #include "components/subresource_filter/core/common/activation_scope.h"
14 17
15 namespace subresource_filter { 18 namespace subresource_filter {
16 19
17 // Encapsulates all parameters that define how the subresource filter feature 20 // Encapsulates a set of parameters that define how the subresource filter
18 // should operate. 21 // feature should operate. Each configuration consists of three parts as
22 // described in detail below.
23 //
24 // There can be multiple configuration enabled at the same time. For each
25 // navigation, however, subresource filtering will be activated according to
26 // exactly one of these enabled configuration, if any. Namely, the configuration
27 // with the highest priority among those whose |activation_conditions| are
28 // satisfied for the navigation.
Charlie Harrison 2017/05/03 14:52:18 A note about the specific case of the ruleset flav
engedy 2017/05/05 12:25:42 Done.
29 //
30 // The priorities for configurations are defined as follows. The configuration
31 // with the higher priority is:
32 // -- the one with the higher |activation_level|,
33 // -- in case of a tie, the one with the more specific |activation_scope|,
34 // -- in case of a tie, the one with the more recently added |activation_list|,
35 // -- then at this point there should not be a tie anymore. If there is one, a
36 // configuration will be selected deterministically, but arbitrarily.
37 // See details in the .cc file and the example ordering in the unit test.
19 struct Configuration { 38 struct Configuration {
39 // The conditions that determine whether subresource filtering should be
40 // activated for a given main frame navigation using this configuration.
41 struct ActivationConditions {
42 // The activation scope. That is, the subset of page loads where subresource
43 // filtering should be activated.
44 ActivationScope activation_scope = ActivationScope::NO_SITES;
45
46 // The activation list to use when the |activation_scope| is
47 // ACTIVATION_LIST, ignored otherwise.
48 ActivationList activation_list = ActivationList::NONE;
49 };
50
51 // The details of how subresource filtering should operate for a given main
52 // frame navigation if the conditions above are met (and this is the highest
53 // priority configuration among those whose conditions are met).
54 struct ActivationOptions {
55 // The maximum degree to which subresource filtering should be activated on
56 // any RenderFrame.
57 ActivationLevel activation_level = ActivationLevel::DISABLED;
58
59 // A number in the range [0, 1], indicating the fraction of page loads that
60 // should have extended performance measurements enabled.
61 double performance_measurement_rate = 0.0;
62
63 // Whether notifications indicating that a subresource was disallowed should
64 // be suppressed in the UI.
65 bool should_suppress_notifications = false;
66
67 // Whether to whitelist a site when a page loaded from that site is
68 // reloaded.
69 bool should_whitelist_site_on_reload = false;
70 };
71
72 // General settings the apply outside of the scope a navigation.
pkalinnikov 2017/05/04 12:04:23 nit: ... that apply ... of the scope of a ...
engedy 2017/05/05 12:25:42 Done.
73 struct GeneralSettings {
74 // The ruleset flavor to download through the component updater. The empty
75 // string indicates that the default ruleset should be used.
76 std::string ruleset_flavor;
77 };
78
Charlie Harrison 2017/05/03 14:52:18 Please add a comment somewhere around here to upda
engedy 2017/05/05 12:25:42 Done. Do you know of any cool ways to static_asser
Charlie Harrison 2017/05/05 13:12:52 Not in a clean way, though I wouldn't call myself
pkalinnikov 2017/05/05 13:29:58 One weird way would be smth like: static_assert(si
Charlie Harrison 2017/05/05 14:00:08 I don't think this is very portable, but there are
engedy 2017/05/05 19:24:10 Yeah, that's very not portable. Even within the sa
20 Configuration(); 79 Configuration();
21 Configuration(ActivationLevel activation_level, 80 Configuration(ActivationLevel activation_level,
22 ActivationScope activation_scope, 81 ActivationScope activation_scope,
23 ActivationList activation_list = ActivationList::NONE); 82 ActivationList activation_list = ActivationList::NONE);
83 Configuration(const Configuration&);
24 Configuration(Configuration&&); 84 Configuration(Configuration&&);
25 ~Configuration(); 85 ~Configuration();
86 Configuration& operator=(const Configuration&);
26 Configuration& operator=(Configuration&&); 87 Configuration& operator=(Configuration&&);
27 88
28 // The maximum degree to which subresource filtering should be activated on 89 bool operator==(const Configuration& rhs) const;
29 // any RenderFrame. This will be ActivationLevel::DISABLED unless the feature 90 bool operator!=(const Configuration& rhs) const;
30 // is enabled and variation parameters prescribe a higher activation level.
31 ActivationLevel activation_level = ActivationLevel::DISABLED;
32 91
33 // The activation scope. That is, the subset of page loads where subresource 92 // Factory methods for preset configurations.
34 // filtering should be activated. This will be ActivationScope::NO_SITES 93 //
35 // unless the feature is =enabled and variation parameters prescribe a wider 94 // To add a new preset:
36 // activation scope. 95 // 1.) Define a named factory method here.
37 ActivationScope activation_scope = ActivationScope::NO_SITES; 96 // 2.) Define a name for the configuration to be used in variation params.
97 // 3.) Register it into |kAvailablePresetConfigurations| in the .cc file.
98 // 4.) Update unittests to cover the new preset.
99 static Configuration MakePresetForLiveRunOnPhishingSites();
100 static Configuration MakePresetForPerformanceTestingDryRunOnAllSites();
38 101
39 // The activation list to use when the |activation_scope| is ACTIVATION_LIST. 102 ActivationConditions activation_conditions;
40 // This will be ActivationList::NONE unless variation parameters prescribe a 103 ActivationOptions activation_options;
41 // recognized list. 104 GeneralSettings general_settings;
42 ActivationList activation_list = ActivationList::NONE;
43
44 // A number in the range [0, 1], indicating the fraction of page loads that
45 // should have extended performance measurements enabled. The rate will
46 // be 0 unless a greater frequency is specified by variation parameters.
47 double performance_measurement_rate = 0.0;
48
49 // Whether notifications indicating that a subresource was disallowed should
50 // be suppressed in the UI.
51 bool should_suppress_notifications = false;
52
53 // The ruleset flavor to download through the component updater. This or the
54 // empty string if the default ruleset should be used.
55 std::string ruleset_flavor;
56
57 // Whether to whitelist a site when a page loaded from that site is reloaded.
58 bool should_whitelist_site_on_reload = false;
59 }; 105 };
60 106
61 // TODO(engedy): Make this an actual list once all call sites are prepared to 107 // For logging in tests.
62 // handle multiple simultaneous configurations. 108 std::ostream& operator<<(std::ostream& os, const Configuration& config);
109
110 // Thread-safe, ref-counted wrapper around an immutable list of configurations.
63 class ConfigurationList : public base::RefCountedThreadSafe<ConfigurationList> { 111 class ConfigurationList : public base::RefCountedThreadSafe<ConfigurationList> {
64 public: 112 public:
113 explicit ConfigurationList(std::vector<Configuration> configs);
65 explicit ConfigurationList(Configuration config); 114 explicit ConfigurationList(Configuration config);
66 115
67 const Configuration& the_one_and_only() const { return config_; } 116 // Retrieves the configurations pre-sorted in decreasing order of priority
117 // (see the comment above the definition of Configuration for details).
118 const std::vector<Configuration>& configs_by_decreasing_priority() const {
119 return configs_by_decreasing_priority_;
120 }
121
122 // Returns the most specific ruleset flavor string that is prescribed by any
123 // of the configurations.
124 //
125 // The `most specific` flavor is defined as the longest flavor string, and, in
pkalinnikov 2017/05/04 12:04:23 As discussed offline, can we get rid of length-fir
engedy 2017/05/05 12:25:42 Done.
126 // case of a tie, the lexicographically greatest among those. The ruleset
127 // flavor is derived independently of activation `priorities`.
Charlie Harrison 2017/05/03 14:52:18 A comment why this is the case would be nice.
engedy 2017/05/05 12:25:42 No longer applicable.
128 std::string GetMostSpecificRulesetFlavor() const;
68 129
69 private: 130 private:
70 friend class base::RefCountedThreadSafe<ConfigurationList>; 131 friend class base::RefCountedThreadSafe<ConfigurationList>;
71 ~ConfigurationList(); 132 ~ConfigurationList();
72 133
73 const Configuration config_; 134 const std::vector<Configuration> configs_by_decreasing_priority_;
74 135
75 DISALLOW_COPY_AND_ASSIGN(ConfigurationList); 136 DISALLOW_COPY_AND_ASSIGN(ConfigurationList);
76 }; 137 };
77 138
78 // Retrieves all currently enabled subresource filtering configurations. The 139 // Retrieves all currently enabled subresource filtering configurations. The
79 // configurations are parsed on first access and then the result is cached. 140 // configurations are parsed on first access and then the result is cached.
80 // 141 //
81 // In tests, however, the config may be altered in-between navigations, so 142 // In tests, however, the config may be changed in-between navigations, so
82 // callers should not hold on to the result for long. 143 // callers should not hold on to the result for long.
83 scoped_refptr<ConfigurationList> GetActiveConfigurations(); 144 scoped_refptr<ConfigurationList> GetEnabledConfigurations();
84 145
85 namespace testing { 146 namespace testing {
86 147
87 // Returns the currently cached active ConfigurationList, if any, and replaces 148 // Returns the currently cached enabled ConfigurationList, if any, and replaces
88 // it with |new_configs|, which may be nullptr to clear the cache. 149 // it with |new_configs|, which may be nullptr to clear the cache.
89 scoped_refptr<ConfigurationList> GetAndSetActivateConfigurations( 150 scoped_refptr<ConfigurationList> GetAndSetActivateConfigurations(
90 scoped_refptr<ConfigurationList> new_configs); 151 scoped_refptr<ConfigurationList> new_configs);
91 152
92 } // namespace testing 153 } // namespace testing
93 154
94 // Feature and variation parameter definitions ------------------------------- 155 // Feature and variation parameter definitions -------------------------------
95 156
96 // The master toggle to enable/disable the Safe Browsing Subresource Filter. 157 // The master toggle to enable/disable the Safe Browsing Subresource Filter.
97 extern const base::Feature kSafeBrowsingSubresourceFilter; 158 extern const base::Feature kSafeBrowsingSubresourceFilter;
(...skipping 10 matching lines...) Expand all
108 extern const char kActivationScopeParameterName[]; 169 extern const char kActivationScopeParameterName[];
109 extern const char kActivationScopeAllSites[]; 170 extern const char kActivationScopeAllSites[];
110 extern const char kActivationScopeActivationList[]; 171 extern const char kActivationScopeActivationList[];
111 extern const char kActivationScopeNoSites[]; 172 extern const char kActivationScopeNoSites[];
112 173
113 extern const char kActivationListsParameterName[]; 174 extern const char kActivationListsParameterName[];
114 extern const char kActivationListSocialEngineeringAdsInterstitial[]; 175 extern const char kActivationListSocialEngineeringAdsInterstitial[];
115 extern const char kActivationListPhishingInterstitial[]; 176 extern const char kActivationListPhishingInterstitial[];
116 extern const char kActivationListSubresourceFilter[]; 177 extern const char kActivationListSubresourceFilter[];
117 178
118 extern const char kRulesetFlavorParameterName[];
119
120 extern const char kPerformanceMeasurementRateParameterName[]; 179 extern const char kPerformanceMeasurementRateParameterName[];
121 180
122 extern const char kSuppressNotificationsParameterName[]; 181 extern const char kSuppressNotificationsParameterName[];
123 182
124 extern const char kWhitelistSiteOnReloadParameterName[]; 183 extern const char kWhitelistSiteOnReloadParameterName[];
125 184
185 extern const char kRulesetFlavorParameterName[];
186
187 extern const char kEnablePresetsParameterName[];
188 extern const char kPresetLiveRunOnPhishingSites[];
189 extern const char kPresetPerformanceTestingDryRunOnAllSites[];
190
126 } // namespace subresource_filter 191 } // namespace subresource_filter
127 192
128 #endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURE S_H_ 193 #endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURE S_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698