| 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/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 Loading... |
| 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| if |
| 38 // ThreadTicks is supported, otherwise returns false. | 38 // ThreadTicks is supported, otherwise returns false. |
| 39 bool ShouldMeasurePerformanceForPageLoad() { | 39 bool ShouldMeasurePerformanceForPageLoad(double performance_measurement_rate) { |
| 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 return performance_measurement_rate == 1 || |
| 43 // ContentSubresourceFilterDriverFactory. | 43 (performance_measurement_rate > 0 && |
| 44 const double rate = GetPerformanceMeasurementRate(); | 44 base::RandDouble() < performance_measurement_rate); |
| 45 return rate == 1 || (rate > 0 && base::RandDouble() < rate); | |
| 46 } | 45 } |
| 47 | 46 |
| 48 // Records histograms about the length of redirect chains, and about the pattern | 47 // Records histograms about the length of redirect chains, and about the pattern |
| 49 // of whether each URL in the chain matched the activation list. | 48 // of whether each URL in the chain matched the activation list. |
| 50 #define REPORT_REDIRECT_PATTERN_FOR_SUFFIX(suffix, hits_pattern, chain_size) \ | 49 #define REPORT_REDIRECT_PATTERN_FOR_SUFFIX(suffix, hits_pattern, chain_size) \ |
| 51 do { \ | 50 do { \ |
| 52 UMA_HISTOGRAM_ENUMERATION( \ | 51 UMA_HISTOGRAM_ENUMERATION( \ |
| 53 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." suffix, \ | 52 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." suffix, \ |
| 54 hits_pattern, 0x10); \ | 53 hits_pattern, 0x10); \ |
| 55 UMA_HISTOGRAM_COUNTS( \ | 54 UMA_HISTOGRAM_COUNTS( \ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 84 ui::PageTransition transition) { | 83 ui::PageTransition transition) { |
| 85 return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD) || | 84 return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD) || |
| 86 // Some pages 'reload' from JavaScript by navigating to themselves. | 85 // Some pages 'reload' from JavaScript by navigating to themselves. |
| 87 url == referrer.url; | 86 url == referrer.url; |
| 88 } | 87 } |
| 89 | 88 |
| 90 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory( | 89 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory( |
| 91 content::WebContents* web_contents, | 90 content::WebContents* web_contents, |
| 92 std::unique_ptr<SubresourceFilterClient> client) | 91 std::unique_ptr<SubresourceFilterClient> client) |
| 93 : content::WebContentsObserver(web_contents), | 92 : content::WebContentsObserver(web_contents), |
| 93 configuration_(GetActiveConfiguration()), |
| 94 client_(std::move(client)), | 94 client_(std::move(client)), |
| 95 throttle_manager_( | 95 throttle_manager_( |
| 96 base::MakeUnique<ContentSubresourceFilterThrottleManager>( | 96 base::MakeUnique<ContentSubresourceFilterThrottleManager>( |
| 97 this, | 97 this, |
| 98 client_->GetRulesetDealer(), | 98 client_->GetRulesetDealer(), |
| 99 web_contents)), | 99 web_contents)), |
| 100 activation_level_(ActivationLevel::DISABLED), | 100 activation_level_(ActivationLevel::DISABLED), |
| 101 activation_decision_(ActivationDecision::UNKNOWN), | 101 activation_decision_(ActivationDecision::UNKNOWN), |
| 102 measure_performance_(false) {} | 102 measure_performance_(false) {} |
| 103 | 103 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 139 |
| 140 void ContentSubresourceFilterDriverFactory::AddHostOfURLToWhitelistSet( | 140 void ContentSubresourceFilterDriverFactory::AddHostOfURLToWhitelistSet( |
| 141 const GURL& url) { | 141 const GURL& url) { |
| 142 if (url.has_host() && url.SchemeIsHTTPOrHTTPS()) | 142 if (url.has_host() && url.SchemeIsHTTPOrHTTPS()) |
| 143 whitelisted_hosts_.insert(url.host()); | 143 whitelisted_hosts_.insert(url.host()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 ContentSubresourceFilterDriverFactory::ActivationDecision | 146 ContentSubresourceFilterDriverFactory::ActivationDecision |
| 147 ContentSubresourceFilterDriverFactory::ComputeActivationDecisionForMainFrameURL( | 147 ContentSubresourceFilterDriverFactory::ComputeActivationDecisionForMainFrameURL( |
| 148 const GURL& url) const { | 148 const GURL& url) const { |
| 149 if (GetMaximumActivationLevel() == ActivationLevel::DISABLED) | 149 if (configuration_.activation_level == ActivationLevel::DISABLED) |
| 150 return ActivationDecision::ACTIVATION_DISABLED; | 150 return ActivationDecision::ACTIVATION_DISABLED; |
| 151 | 151 |
| 152 ActivationScope scope = GetCurrentActivationScope(); | 152 if (configuration_.activation_scope == ActivationScope::NO_SITES) |
| 153 if (scope == ActivationScope::NO_SITES) | |
| 154 return ActivationDecision::ACTIVATION_DISABLED; | 153 return ActivationDecision::ACTIVATION_DISABLED; |
| 155 | 154 |
| 156 if (!url.SchemeIsHTTPOrHTTPS()) | 155 if (!url.SchemeIsHTTPOrHTTPS()) |
| 157 return ActivationDecision::UNSUPPORTED_SCHEME; | 156 return ActivationDecision::UNSUPPORTED_SCHEME; |
| 158 if (IsWhitelisted(url)) | 157 if (IsWhitelisted(url)) |
| 159 return ActivationDecision::URL_WHITELISTED; | 158 return ActivationDecision::URL_WHITELISTED; |
| 160 | 159 |
| 161 switch (scope) { | 160 switch (configuration_.activation_scope) { |
| 162 case ActivationScope::ALL_SITES: | 161 case ActivationScope::ALL_SITES: |
| 163 return ActivationDecision::ACTIVATED; | 162 return ActivationDecision::ACTIVATED; |
| 164 case ActivationScope::ACTIVATION_LIST: { | 163 case ActivationScope::ACTIVATION_LIST: { |
| 165 // The logic to ensure only http/https URLs are activated lives in | 164 // The logic to ensure only http/https URLs are activated lives in |
| 166 // AddActivationListMatch to ensure the activation list only has relevant | 165 // AddActivationListMatch to ensure the activation list only has relevant |
| 167 // entries. | 166 // entries. |
| 168 DCHECK(url.SchemeIsHTTPOrHTTPS() || | 167 DCHECK(url.SchemeIsHTTPOrHTTPS() || |
| 169 !DidURLMatchActivationList(url, GetCurrentActivationList())); | 168 !DidURLMatchActivationList(url, configuration_.activation_list)); |
| 170 bool should_activate = | 169 bool should_activate = |
| 171 DidURLMatchActivationList(url, GetCurrentActivationList()); | 170 DidURLMatchActivationList(url, configuration_.activation_list); |
| 172 if (GetCurrentActivationList() == ActivationList::PHISHING_INTERSTITIAL) { | 171 if (configuration_.activation_list == |
| 172 ActivationList::PHISHING_INTERSTITIAL) { |
| 173 // Handling special case, where activation on the phishing sites also | 173 // Handling special case, where activation on the phishing sites also |
| 174 // mean the activation on the sites with social engineering metadata. | 174 // mean the activation on the sites with social engineering metadata. |
| 175 should_activate |= DidURLMatchActivationList( | 175 should_activate |= DidURLMatchActivationList( |
| 176 url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL); | 176 url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL); |
| 177 } | 177 } |
| 178 return should_activate ? ActivationDecision::ACTIVATED | 178 return should_activate ? ActivationDecision::ACTIVATED |
| 179 : ActivationDecision::ACTIVATION_LIST_NOT_MATCHED; | 179 : ActivationDecision::ACTIVATION_LIST_NOT_MATCHED; |
| 180 } | 180 } |
| 181 default: | 181 default: |
| 182 return ActivationDecision::ACTIVATION_DISABLED; | 182 return ActivationDecision::ACTIVATION_DISABLED; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 206 navigation_handle->GetNetErrorCode() != net::OK) { | 206 navigation_handle->GetNetErrorCode() != net::OK) { |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 | 209 |
| 210 const GURL& url = navigation_handle->GetURL(); | 210 const GURL& url = navigation_handle->GetURL(); |
| 211 const content::Referrer& referrer = navigation_handle->GetReferrer(); | 211 const content::Referrer& referrer = navigation_handle->GetReferrer(); |
| 212 ui::PageTransition transition = navigation_handle->GetPageTransition(); | 212 ui::PageTransition transition = navigation_handle->GetPageTransition(); |
| 213 | 213 |
| 214 RecordRedirectChainMatchPattern(); | 214 RecordRedirectChainMatchPattern(); |
| 215 | 215 |
| 216 if (ShouldWhitelistSiteOnReload() && | 216 if (configuration_.should_whitelist_site_on_reload && |
| 217 NavigationIsPageReload(url, referrer, transition)) { | 217 NavigationIsPageReload(url, referrer, transition)) { |
| 218 // Whitelist this host for the current as well as subsequent navigations. | 218 // Whitelist this host for the current as well as subsequent navigations. |
| 219 AddHostOfURLToWhitelistSet(url); | 219 AddHostOfURLToWhitelistSet(url); |
| 220 } | 220 } |
| 221 | 221 |
| 222 activation_decision_ = ComputeActivationDecisionForMainFrameURL(url); | 222 activation_decision_ = ComputeActivationDecisionForMainFrameURL(url); |
| 223 DCHECK(activation_decision_ != ActivationDecision::UNKNOWN); | 223 DCHECK(activation_decision_ != ActivationDecision::UNKNOWN); |
| 224 if (activation_decision_ != ActivationDecision::ACTIVATED) { | 224 if (activation_decision_ != ActivationDecision::ACTIVATED) { |
| 225 ResetActivationState(); | 225 ResetActivationState(); |
| 226 return; | 226 return; |
| 227 } | 227 } |
| 228 | 228 |
| 229 activation_level_ = GetMaximumActivationLevel(); | 229 activation_level_ = configuration_.activation_level; |
| 230 measure_performance_ = activation_level_ != ActivationLevel::DISABLED && | 230 measure_performance_ = activation_level_ != ActivationLevel::DISABLED && |
| 231 ShouldMeasurePerformanceForPageLoad(); | 231 ShouldMeasurePerformanceForPageLoad( |
| 232 configuration_.performance_measurement_rate); |
| 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 Loading... |
| 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 |
| OLD | NEW |