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

Side by Side Diff: components/subresource_filter/content/browser/content_subresource_filter_driver_factory.cc

Issue 2798983002: Introduce subresource_filter::Configuration. (Closed)
Patch Set: Address comments from csharrison@. Created 3 years, 8 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 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" 5 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
6 6
7 #include "base/feature_list.h" 7 #include "base/feature_list.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/rand_util.h" 9 #include "base/rand_util.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 16 matching lines...) Expand all
27 27
28 namespace { 28 namespace {
29 29
30 const char kWebContentsUserDataKey[] = 30 const char kWebContentsUserDataKey[] =
31 "web_contents_subresource_filter_driver_factory"; 31 "web_contents_subresource_filter_driver_factory";
32 32
33 std::string DistillURLToHostAndPath(const GURL& url) { 33 std::string DistillURLToHostAndPath(const GURL& url) {
34 return url.host() + url.path(); 34 return url.host() + url.path();
35 } 35 }
36 36
37 // Returns true with a probability of GetPerformanceMeasurementRate() if 37 // Returns true with a probability given by |performance_measurement_rate| in
38 // ThreadTicks is supported, otherwise returns false. 38 // |configuration| if ThreadTicks is supported, otherwise returns false.
39 bool ShouldMeasurePerformanceForPageLoad() { 39 bool ShouldMeasurePerformanceForPageLoad(const Configuration& configuration) {
40 if (!base::ThreadTicks::IsSupported()) 40 if (!base::ThreadTicks::IsSupported())
41 return false; 41 return false;
42 // TODO(pkalinnikov): Cache |rate| and other variation params in 42 // TODO(pkalinnikov): Cache |rate| and other variation params in
pkalinnikov 2017/04/07 09:22:06 nit: You can now remove this TODO because this CL
engedy 2017/04/07 09:28:29 Done.
43 // ContentSubresourceFilterDriverFactory. 43 // ContentSubresourceFilterDriverFactory.
44 const double rate = GetPerformanceMeasurementRate(); 44 const double rate = configuration.performance_measurement_rate;
45 return rate == 1 || (rate > 0 && base::RandDouble() < rate); 45 return rate == 1 || (rate > 0 && base::RandDouble() < rate);
46 } 46 }
47 47
48 // Records histograms about the length of redirect chains, and about the pattern 48 // Records histograms about the length of redirect chains, and about the pattern
49 // of whether each URL in the chain matched the activation list. 49 // of whether each URL in the chain matched the activation list.
50 #define REPORT_REDIRECT_PATTERN_FOR_SUFFIX(suffix, hits_pattern, chain_size) \ 50 #define REPORT_REDIRECT_PATTERN_FOR_SUFFIX(suffix, hits_pattern, chain_size) \
51 do { \ 51 do { \
52 UMA_HISTOGRAM_ENUMERATION( \ 52 UMA_HISTOGRAM_ENUMERATION( \
53 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." suffix, \ 53 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." suffix, \
54 hits_pattern, 0x10); \ 54 hits_pattern, 0x10); \
(...skipping 29 matching lines...) Expand all
84 ui::PageTransition transition) { 84 ui::PageTransition transition) {
85 return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD) || 85 return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD) ||
86 // Some pages 'reload' from JavaScript by navigating to themselves. 86 // Some pages 'reload' from JavaScript by navigating to themselves.
87 url == referrer.url; 87 url == referrer.url;
88 } 88 }
89 89
90 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory( 90 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory(
91 content::WebContents* web_contents, 91 content::WebContents* web_contents,
92 std::unique_ptr<SubresourceFilterClient> client) 92 std::unique_ptr<SubresourceFilterClient> client)
93 : content::WebContentsObserver(web_contents), 93 : content::WebContentsObserver(web_contents),
94 configuration_(GetActiveConfiguration()),
94 client_(std::move(client)), 95 client_(std::move(client)),
95 throttle_manager_( 96 throttle_manager_(
96 base::MakeUnique<ContentSubresourceFilterThrottleManager>( 97 base::MakeUnique<ContentSubresourceFilterThrottleManager>(
97 this, 98 this,
98 client_->GetRulesetDealer(), 99 client_->GetRulesetDealer(),
99 web_contents)), 100 web_contents)),
100 activation_level_(ActivationLevel::DISABLED), 101 activation_level_(ActivationLevel::DISABLED),
101 activation_decision_(ActivationDecision::UNKNOWN), 102 activation_decision_(ActivationDecision::UNKNOWN),
102 measure_performance_(false) {} 103 measure_performance_(false) {}
103 104
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 140
140 void ContentSubresourceFilterDriverFactory::AddHostOfURLToWhitelistSet( 141 void ContentSubresourceFilterDriverFactory::AddHostOfURLToWhitelistSet(
141 const GURL& url) { 142 const GURL& url) {
142 if (url.has_host() && url.SchemeIsHTTPOrHTTPS()) 143 if (url.has_host() && url.SchemeIsHTTPOrHTTPS())
143 whitelisted_hosts_.insert(url.host()); 144 whitelisted_hosts_.insert(url.host());
144 } 145 }
145 146
146 ContentSubresourceFilterDriverFactory::ActivationDecision 147 ContentSubresourceFilterDriverFactory::ActivationDecision
147 ContentSubresourceFilterDriverFactory::ComputeActivationDecisionForMainFrameURL( 148 ContentSubresourceFilterDriverFactory::ComputeActivationDecisionForMainFrameURL(
148 const GURL& url) const { 149 const GURL& url) const {
149 if (GetMaximumActivationLevel() == ActivationLevel::DISABLED) 150 if (configuration_.activation_level == ActivationLevel::DISABLED)
150 return ActivationDecision::ACTIVATION_DISABLED; 151 return ActivationDecision::ACTIVATION_DISABLED;
151 152
152 ActivationScope scope = GetCurrentActivationScope(); 153 if (configuration_.activation_scope == ActivationScope::NO_SITES)
153 if (scope == ActivationScope::NO_SITES)
154 return ActivationDecision::ACTIVATION_DISABLED; 154 return ActivationDecision::ACTIVATION_DISABLED;
155 155
156 if (!url.SchemeIsHTTPOrHTTPS()) 156 if (!url.SchemeIsHTTPOrHTTPS())
157 return ActivationDecision::UNSUPPORTED_SCHEME; 157 return ActivationDecision::UNSUPPORTED_SCHEME;
158 if (IsWhitelisted(url)) 158 if (IsWhitelisted(url))
159 return ActivationDecision::URL_WHITELISTED; 159 return ActivationDecision::URL_WHITELISTED;
160 160
161 switch (scope) { 161 switch (configuration_.activation_scope) {
162 case ActivationScope::ALL_SITES: 162 case ActivationScope::ALL_SITES:
163 return ActivationDecision::ACTIVATED; 163 return ActivationDecision::ACTIVATED;
164 case ActivationScope::ACTIVATION_LIST: { 164 case ActivationScope::ACTIVATION_LIST: {
165 // The logic to ensure only http/https URLs are activated lives in 165 // The logic to ensure only http/https URLs are activated lives in
166 // AddActivationListMatch to ensure the activation list only has relevant 166 // AddActivationListMatch to ensure the activation list only has relevant
167 // entries. 167 // entries.
168 DCHECK(url.SchemeIsHTTPOrHTTPS() || 168 DCHECK(url.SchemeIsHTTPOrHTTPS() ||
169 !DidURLMatchActivationList(url, GetCurrentActivationList())); 169 !DidURLMatchActivationList(url, configuration_.activation_list));
170 bool should_activate = 170 bool should_activate =
171 DidURLMatchActivationList(url, GetCurrentActivationList()); 171 DidURLMatchActivationList(url, configuration_.activation_list);
172 if (GetCurrentActivationList() == ActivationList::PHISHING_INTERSTITIAL) { 172 if (configuration_.activation_list ==
173 ActivationList::PHISHING_INTERSTITIAL) {
173 // Handling special case, where activation on the phishing sites also 174 // Handling special case, where activation on the phishing sites also
174 // mean the activation on the sites with social engineering metadata. 175 // mean the activation on the sites with social engineering metadata.
175 should_activate |= DidURLMatchActivationList( 176 should_activate |= DidURLMatchActivationList(
176 url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL); 177 url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL);
177 } 178 }
178 return should_activate ? ActivationDecision::ACTIVATED 179 return should_activate ? ActivationDecision::ACTIVATED
179 : ActivationDecision::ACTIVATION_LIST_NOT_MATCHED; 180 : ActivationDecision::ACTIVATION_LIST_NOT_MATCHED;
180 } 181 }
181 default: 182 default:
182 return ActivationDecision::ACTIVATION_DISABLED; 183 return ActivationDecision::ACTIVATION_DISABLED;
(...skipping 23 matching lines...) Expand all
206 navigation_handle->GetNetErrorCode() != net::OK) { 207 navigation_handle->GetNetErrorCode() != net::OK) {
207 return; 208 return;
208 } 209 }
209 210
210 const GURL& url = navigation_handle->GetURL(); 211 const GURL& url = navigation_handle->GetURL();
211 const content::Referrer& referrer = navigation_handle->GetReferrer(); 212 const content::Referrer& referrer = navigation_handle->GetReferrer();
212 ui::PageTransition transition = navigation_handle->GetPageTransition(); 213 ui::PageTransition transition = navigation_handle->GetPageTransition();
213 214
214 RecordRedirectChainMatchPattern(); 215 RecordRedirectChainMatchPattern();
215 216
216 if (ShouldWhitelistSiteOnReload() && 217 if (configuration_.should_whitelist_site_on_reload &&
217 NavigationIsPageReload(url, referrer, transition)) { 218 NavigationIsPageReload(url, referrer, transition)) {
218 // Whitelist this host for the current as well as subsequent navigations. 219 // Whitelist this host for the current as well as subsequent navigations.
219 AddHostOfURLToWhitelistSet(url); 220 AddHostOfURLToWhitelistSet(url);
220 } 221 }
221 222
222 activation_decision_ = ComputeActivationDecisionForMainFrameURL(url); 223 activation_decision_ = ComputeActivationDecisionForMainFrameURL(url);
223 DCHECK(activation_decision_ != ActivationDecision::UNKNOWN); 224 DCHECK(activation_decision_ != ActivationDecision::UNKNOWN);
224 if (activation_decision_ != ActivationDecision::ACTIVATED) { 225 if (activation_decision_ != ActivationDecision::ACTIVATED) {
225 ResetActivationState(); 226 ResetActivationState();
226 return; 227 return;
227 } 228 }
228 229
229 activation_level_ = GetMaximumActivationLevel(); 230 activation_level_ = configuration_.activation_level;
230 measure_performance_ = activation_level_ != ActivationLevel::DISABLED && 231 measure_performance_ = activation_level_ != ActivationLevel::DISABLED &&
231 ShouldMeasurePerformanceForPageLoad(); 232 ShouldMeasurePerformanceForPageLoad(configuration_);
232 ActivationState state = ActivationState(activation_level_); 233 ActivationState state = ActivationState(activation_level_);
233 state.measure_performance = measure_performance_; 234 state.measure_performance = measure_performance_;
234 throttle_manager_->NotifyPageActivationComputed(navigation_handle, state); 235 throttle_manager_->NotifyPageActivationComputed(navigation_handle, state);
235 } 236 }
236 237
237 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() { 238 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() {
238 if (ShouldSuppressNotifications()) 239 if (configuration_.should_suppress_notifications)
239 return; 240 return;
240 241
241 client_->ToggleNotificationVisibility(activation_level_ == 242 client_->ToggleNotificationVisibility(activation_level_ ==
242 ActivationLevel::ENABLED); 243 ActivationLevel::ENABLED);
243 } 244 }
244 245
245 bool ContentSubresourceFilterDriverFactory::ShouldSuppressActivation( 246 bool ContentSubresourceFilterDriverFactory::ShouldSuppressActivation(
246 content::NavigationHandle* navigation_handle) { 247 content::NavigationHandle* navigation_handle) {
247 // Never suppress subframe navigations. 248 // Never suppress subframe navigations.
248 return navigation_handle->IsInMainFrame() && 249 return navigation_handle->IsInMainFrame() &&
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 REPORT_REDIRECT_PATTERN_FOR_SUFFIX("SubresourceFilterOnly", hits_pattern, 404 REPORT_REDIRECT_PATTERN_FOR_SUFFIX("SubresourceFilterOnly", hits_pattern,
404 chain_size); 405 chain_size);
405 break; 406 break;
406 default: 407 default:
407 NOTREACHED(); 408 NOTREACHED();
408 break; 409 break;
409 } 410 }
410 } 411 }
411 412
412 } // namespace subresource_filter 413 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698