Chromium Code Reviews| 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/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 | 77 |
| 78 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory( | 78 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory( |
| 79 content::WebContents* web_contents, | 79 content::WebContents* web_contents, |
| 80 SubresourceFilterClient* client) | 80 SubresourceFilterClient* client) |
| 81 : content::WebContentsObserver(web_contents), | 81 : content::WebContentsObserver(web_contents), |
| 82 client_(client), | 82 client_(client), |
| 83 throttle_manager_( | 83 throttle_manager_( |
| 84 base::MakeUnique<ContentSubresourceFilterThrottleManager>( | 84 base::MakeUnique<ContentSubresourceFilterThrottleManager>( |
| 85 this, | 85 this, |
| 86 client_->GetRulesetDealer(), | 86 client_->GetRulesetDealer(), |
| 87 web_contents)), | 87 web_contents)) {} |
| 88 activation_level_(ActivationLevel::DISABLED), | |
| 89 activation_decision_(ActivationDecision::UNKNOWN), | |
| 90 measure_performance_(false) {} | |
| 91 | 88 |
| 92 ContentSubresourceFilterDriverFactory:: | 89 ContentSubresourceFilterDriverFactory:: |
| 93 ~ContentSubresourceFilterDriverFactory() {} | 90 ~ContentSubresourceFilterDriverFactory() {} |
| 94 | 91 |
| 95 void ContentSubresourceFilterDriverFactory:: | 92 void ContentSubresourceFilterDriverFactory:: |
| 96 OnMainResourceMatchedSafeBrowsingBlacklist( | 93 OnMainResourceMatchedSafeBrowsingBlacklist( |
| 97 const GURL& url, | 94 const GURL& url, |
| 98 const std::vector<GURL>& redirect_urls, | 95 const std::vector<GURL>& redirect_urls, |
| 99 safe_browsing::SBThreatType threat_type, | 96 safe_browsing::SBThreatType threat_type, |
| 100 safe_browsing::ThreatPatternType threat_type_metadata) { | 97 safe_browsing::ThreatPatternType threat_type_metadata) { |
| 101 AddActivationListMatch( | 98 AddActivationListMatch( |
| 102 url, GetListForThreatTypeAndMetadata(threat_type, threat_type_metadata)); | 99 url, GetListForThreatTypeAndMetadata(threat_type, threat_type_metadata)); |
| 103 } | 100 } |
| 104 | 101 |
| 105 ContentSubresourceFilterDriverFactory::ActivationDecision | 102 void ContentSubresourceFilterDriverFactory:: |
| 106 ContentSubresourceFilterDriverFactory:: | 103 ComputeActivationForMainFrameNavigation( |
| 107 ComputeActivationDecisionForMainFrameNavigation( | 104 content::NavigationHandle* navigation_handle) { |
| 108 content::NavigationHandle* navigation_handle) const { | |
| 109 const GURL& url(navigation_handle->GetURL()); | 105 const GURL& url(navigation_handle->GetURL()); |
| 110 | 106 |
| 111 const auto configurations = GetActiveConfigurations(); | 107 if (!url.SchemeIsHTTPOrHTTPS()) { |
|
Bryan McQuade
2017/05/17 13:55:48
looking at this now, does this mean that if the sy
engedy
2017/05/17 14:21:40
Unfortunately, it would make the code here less cl
| |
| 112 if (configurations->the_one_and_only().activation_level == | 108 activation_decision_ = ActivationDecision::UNSUPPORTED_SCHEME; |
| 113 ActivationLevel::DISABLED) | 109 activation_options_ = Configuration::ActivationOptions(); |
| 114 return ActivationDecision::ACTIVATION_DISABLED; | 110 return; |
| 111 } | |
| 115 | 112 |
| 116 if (configurations->the_one_and_only().activation_scope == | 113 const auto config_list = GetEnabledConfigurations(); |
| 117 ActivationScope::NO_SITES) | 114 const auto highest_priority_activated_config = |
| 118 return ActivationDecision::ACTIVATION_DISABLED; | 115 std::find_if(config_list->configs_by_decreasing_priority().begin(), |
| 116 config_list->configs_by_decreasing_priority().end(), | |
| 117 [&url, this](const Configuration& config) { | |
| 118 return DoesMainFrameURLSatisfyActivationConditions( | |
| 119 url, config.activation_conditions); | |
| 120 }); | |
| 119 | 121 |
| 120 if (!url.SchemeIsHTTPOrHTTPS()) | 122 if (highest_priority_activated_config == |
| 121 return ActivationDecision::UNSUPPORTED_SCHEME; | 123 config_list->configs_by_decreasing_priority().end()) { |
| 124 activation_decision_ = ActivationDecision::ACTIVATION_CONDITIONS_NOT_MET; | |
| 125 activation_options_ = Configuration::ActivationOptions(); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 122 // TODO(csharrison): The throttle manager also performs this check. Remove | 129 // TODO(csharrison): The throttle manager also performs this check. Remove |
| 123 // this one when the activation decision is sent directly to the throttle | 130 // this one when the activation decision is sent directly to the throttle |
| 124 // manager. | 131 // manager. |
| 125 if (client_->ShouldSuppressActivation(navigation_handle)) | 132 if (client_->ShouldSuppressActivation(navigation_handle)) { |
| 126 return ActivationDecision::URL_WHITELISTED; | 133 activation_decision_ = ActivationDecision::URL_WHITELISTED; |
| 134 activation_options_ = Configuration::ActivationOptions(); | |
| 135 return; | |
| 136 } | |
| 127 | 137 |
| 128 switch (configurations->the_one_and_only().activation_scope) { | 138 activation_options_ = highest_priority_activated_config->activation_options; |
| 139 activation_decision_ = | |
| 140 activation_options_.activation_level == ActivationLevel::DISABLED | |
| 141 ? ActivationDecision::ACTIVATION_DISABLED | |
| 142 : ActivationDecision::ACTIVATED; | |
| 143 } | |
| 144 | |
| 145 bool ContentSubresourceFilterDriverFactory:: | |
| 146 DoesMainFrameURLSatisfyActivationConditions( | |
| 147 const GURL& url, | |
| 148 const Configuration::ActivationConditions& conditions) const { | |
| 149 switch (conditions.activation_scope) { | |
| 129 case ActivationScope::ALL_SITES: | 150 case ActivationScope::ALL_SITES: |
| 130 return ActivationDecision::ACTIVATED; | 151 return true; |
| 131 case ActivationScope::ACTIVATION_LIST: { | 152 case ActivationScope::ACTIVATION_LIST: |
| 132 // The logic to ensure only http/https URLs are activated lives in | 153 if (DidURLMatchActivationList(url, conditions.activation_list)) |
| 133 // AddActivationListMatch to ensure the activation list only has relevant | 154 return true; |
| 134 // entries. | 155 if (conditions.activation_list == ActivationList::PHISHING_INTERSTITIAL && |
| 135 DCHECK(url.SchemeIsHTTPOrHTTPS() || | 156 DidURLMatchActivationList( |
| 136 !DidURLMatchActivationList( | 157 url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL)) { |
| 137 url, configurations->the_one_and_only().activation_list)); | |
| 138 bool should_activate = DidURLMatchActivationList( | |
| 139 url, configurations->the_one_and_only().activation_list); | |
| 140 if (configurations->the_one_and_only().activation_list == | |
| 141 ActivationList::PHISHING_INTERSTITIAL) { | |
| 142 // Handling special case, where activation on the phishing sites also | 158 // Handling special case, where activation on the phishing sites also |
| 143 // mean the activation on the sites with social engineering metadata. | 159 // mean the activation on the sites with social engineering metadata. |
| 144 should_activate |= DidURLMatchActivationList( | 160 return true; |
| 145 url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL); | |
| 146 } | 161 } |
| 147 return should_activate ? ActivationDecision::ACTIVATED | 162 return false; |
| 148 : ActivationDecision::ACTIVATION_LIST_NOT_MATCHED; | 163 case ActivationScope::NO_SITES: |
| 149 } | 164 return false; |
| 150 default: | |
| 151 return ActivationDecision::ACTIVATION_DISABLED; | |
| 152 } | 165 } |
| 166 NOTREACHED(); | |
| 167 return false; | |
| 153 } | 168 } |
| 154 | 169 |
| 155 void ContentSubresourceFilterDriverFactory::OnReloadRequested() { | 170 void ContentSubresourceFilterDriverFactory::OnReloadRequested() { |
| 156 UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumReloads", true); | 171 UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumReloads", true); |
| 157 const GURL& whitelist_url = web_contents()->GetLastCommittedURL(); | 172 const GURL& whitelist_url = web_contents()->GetLastCommittedURL(); |
| 158 | 173 |
| 159 // Only whitelist via content settings when using the experimental UI, | 174 // Only whitelist via content settings when using the experimental UI, |
| 160 // otherwise could get into a situation where content settings cannot be | 175 // otherwise could get into a situation where content settings cannot be |
| 161 // adjusted. | 176 // adjusted. |
| 162 if (base::FeatureList::IsEnabled( | 177 if (base::FeatureList::IsEnabled( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 175 navigation_handle->GetNetErrorCode() != net::OK) { | 190 navigation_handle->GetNetErrorCode() != net::OK) { |
| 176 return; | 191 return; |
| 177 } | 192 } |
| 178 | 193 |
| 179 const GURL& url = navigation_handle->GetURL(); | 194 const GURL& url = navigation_handle->GetURL(); |
| 180 const content::Referrer& referrer = navigation_handle->GetReferrer(); | 195 const content::Referrer& referrer = navigation_handle->GetReferrer(); |
| 181 ui::PageTransition transition = navigation_handle->GetPageTransition(); | 196 ui::PageTransition transition = navigation_handle->GetPageTransition(); |
| 182 | 197 |
| 183 RecordRedirectChainMatchPattern(); | 198 RecordRedirectChainMatchPattern(); |
| 184 | 199 |
| 185 const auto configurations = GetActiveConfigurations(); | 200 if (activation_options_.should_whitelist_site_on_reload && |
| 186 if (configurations->the_one_and_only().should_whitelist_site_on_reload && | |
| 187 NavigationIsPageReload(url, referrer, transition)) { | 201 NavigationIsPageReload(url, referrer, transition)) { |
| 188 // Whitelist this host for the current as well as subsequent navigations. | 202 // Whitelist this host for the current as well as subsequent navigations. |
| 189 client_->WhitelistInCurrentWebContents(url); | 203 client_->WhitelistInCurrentWebContents(url); |
| 190 } | 204 } |
| 191 | 205 |
| 192 activation_decision_ = | 206 ComputeActivationForMainFrameNavigation(navigation_handle); |
| 193 ComputeActivationDecisionForMainFrameNavigation(navigation_handle); | 207 DCHECK_NE(activation_decision_, ActivationDecision::UNKNOWN); |
| 194 DCHECK(activation_decision_ != ActivationDecision::UNKNOWN); | |
| 195 if (activation_decision_ != ActivationDecision::ACTIVATED) { | 208 if (activation_decision_ != ActivationDecision::ACTIVATED) { |
| 196 ResetActivationState(); | 209 DCHECK_EQ(activation_options_.activation_level, ActivationLevel::DISABLED); |
| 197 return; | 210 return; |
| 198 } | 211 } |
| 199 | 212 |
| 200 activation_level_ = configurations->the_one_and_only().activation_level; | 213 DCHECK_NE(activation_options_.activation_level, ActivationLevel::DISABLED); |
| 201 measure_performance_ = | 214 ActivationState state = ActivationState(activation_options_.activation_level); |
| 202 activation_level_ != ActivationLevel::DISABLED && | 215 state.measure_performance = ShouldMeasurePerformanceForPageLoad( |
| 203 ShouldMeasurePerformanceForPageLoad( | 216 activation_options_.performance_measurement_rate); |
| 204 configurations->the_one_and_only().performance_measurement_rate); | |
| 205 ActivationState state = ActivationState(activation_level_); | |
| 206 state.measure_performance = measure_performance_; | |
| 207 // TODO(csharrison): Set state.enable_logging based on metadata returns from | 217 // TODO(csharrison): Set state.enable_logging based on metadata returns from |
| 208 // the safe browsing filter, when it is available. Add tests for this | 218 // the safe browsing filter, when it is available. Add tests for this |
| 209 // behavior. | 219 // behavior. |
| 210 throttle_manager_->NotifyPageActivationComputed(navigation_handle, state); | 220 throttle_manager_->NotifyPageActivationComputed(navigation_handle, state); |
| 211 } | 221 } |
| 212 | 222 |
| 213 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() { | 223 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() { |
| 214 const auto configurations = GetActiveConfigurations(); | 224 if (activation_options_.should_suppress_notifications) |
| 215 if (configurations->the_one_and_only().should_suppress_notifications) | |
| 216 return; | 225 return; |
| 217 | 226 client_->ToggleNotificationVisibility(activation_options_.activation_level == |
| 218 client_->ToggleNotificationVisibility(activation_level_ == | |
| 219 ActivationLevel::ENABLED); | 227 ActivationLevel::ENABLED); |
| 220 } | 228 } |
| 221 | 229 |
| 222 bool ContentSubresourceFilterDriverFactory::ShouldSuppressActivation( | 230 bool ContentSubresourceFilterDriverFactory::ShouldSuppressActivation( |
| 223 content::NavigationHandle* navigation_handle) { | 231 content::NavigationHandle* navigation_handle) { |
| 224 return client_->ShouldSuppressActivation(navigation_handle); | 232 return client_->ShouldSuppressActivation(navigation_handle); |
| 225 } | 233 } |
| 226 | 234 |
| 227 void ContentSubresourceFilterDriverFactory::ResetActivationState() { | |
| 228 navigation_chain_.clear(); | |
| 229 activation_list_matches_.clear(); | |
| 230 activation_level_ = ActivationLevel::DISABLED; | |
| 231 measure_performance_ = false; | |
| 232 } | |
| 233 | |
| 234 void ContentSubresourceFilterDriverFactory::DidStartNavigation( | 235 void ContentSubresourceFilterDriverFactory::DidStartNavigation( |
| 235 content::NavigationHandle* navigation_handle) { | 236 content::NavigationHandle* navigation_handle) { |
| 236 if (navigation_handle->IsInMainFrame() && | 237 if (navigation_handle->IsInMainFrame() && |
| 237 !navigation_handle->IsSameDocument()) { | 238 !navigation_handle->IsSameDocument()) { |
| 238 activation_decision_ = ActivationDecision::UNKNOWN; | 239 activation_decision_ = ActivationDecision::UNKNOWN; |
| 239 ResetActivationState(); | 240 activation_list_matches_.clear(); |
| 240 navigation_chain_.push_back(navigation_handle->GetURL()); | 241 navigation_chain_ = {navigation_handle->GetURL()}; |
| 241 client_->ToggleNotificationVisibility(false); | 242 client_->ToggleNotificationVisibility(false); |
| 242 } | 243 } |
| 243 } | 244 } |
| 244 | 245 |
| 245 void ContentSubresourceFilterDriverFactory::DidRedirectNavigation( | 246 void ContentSubresourceFilterDriverFactory::DidRedirectNavigation( |
| 246 content::NavigationHandle* navigation_handle) { | 247 content::NavigationHandle* navigation_handle) { |
| 247 DCHECK(!navigation_handle->IsSameDocument()); | 248 DCHECK(!navigation_handle->IsSameDocument()); |
| 248 if (navigation_handle->IsInMainFrame()) | 249 if (navigation_handle->IsInMainFrame()) |
| 249 navigation_chain_.push_back(navigation_handle->GetURL()); | 250 navigation_chain_.push_back(navigation_handle->GetURL()); |
| 250 } | 251 } |
| 251 | 252 |
| 253 void ContentSubresourceFilterDriverFactory::DidFinishNavigation( | |
| 254 content::NavigationHandle* navigation_handle) { | |
| 255 if (navigation_handle->IsInMainFrame() && | |
| 256 !navigation_handle->IsSameDocument() && | |
| 257 activation_decision_ == ActivationDecision::UNKNOWN && | |
| 258 navigation_handle->HasCommitted()) { | |
| 259 activation_decision_ = ActivationDecision::ACTIVATION_DISABLED; | |
| 260 activation_options_ = Configuration::ActivationOptions(); | |
| 261 } | |
| 262 } | |
| 263 | |
| 252 bool ContentSubresourceFilterDriverFactory::DidURLMatchActivationList( | 264 bool ContentSubresourceFilterDriverFactory::DidURLMatchActivationList( |
| 253 const GURL& url, | 265 const GURL& url, |
| 254 ActivationList activation_list) const { | 266 ActivationList activation_list) const { |
| 255 auto match_types = | 267 auto match_types = |
| 256 activation_list_matches_.find(DistillURLToHostAndPath(url)); | 268 activation_list_matches_.find(DistillURLToHostAndPath(url)); |
| 257 return match_types != activation_list_matches_.end() && | 269 return match_types != activation_list_matches_.end() && |
| 258 match_types->second.find(activation_list) != match_types->second.end(); | 270 match_types->second.find(activation_list) != match_types->second.end(); |
| 259 } | 271 } |
| 260 | 272 |
| 261 void ContentSubresourceFilterDriverFactory::AddActivationListMatch( | 273 void ContentSubresourceFilterDriverFactory::AddActivationListMatch( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 REPORT_REDIRECT_PATTERN_FOR_SUFFIX("SubresourceFilterOnly", hits_pattern, | 336 REPORT_REDIRECT_PATTERN_FOR_SUFFIX("SubresourceFilterOnly", hits_pattern, |
| 325 chain_size); | 337 chain_size); |
| 326 break; | 338 break; |
| 327 default: | 339 default: |
| 328 NOTREACHED(); | 340 NOTREACHED(); |
| 329 break; | 341 break; |
| 330 } | 342 } |
| 331 } | 343 } |
| 332 | 344 |
| 333 } // namespace subresource_filter | 345 } // namespace subresource_filter |
| OLD | NEW |