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

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

Issue 2889913002: [subresource_filter] Remove Forwarding NavigationThrottles (Closed)
Patch Set: Remove silly forwarding throttles 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 #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"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "base/trace_event/trace_event.h" 12 #include "base/trace_event/trace_event.h"
13 #include "base/trace_event/trace_event_argument.h" 13 #include "base/trace_event/trace_event_argument.h"
14 #include "components/subresource_filter/content/browser/content_activation_list_ utils.h" 14 #include "components/subresource_filter/content/browser/content_activation_list_ utils.h"
15 #include "components/subresource_filter/content/browser/subresource_filter_clien t.h" 15 #include "components/subresource_filter/content/browser/subresource_filter_clien t.h"
16 #include "components/subresource_filter/core/browser/subresource_filter_features .h" 16 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
17 #include "components/subresource_filter/core/common/activation_list.h" 17 #include "components/subresource_filter/core/common/activation_list.h"
18 #include "components/subresource_filter/core/common/activation_state.h" 18 #include "components/subresource_filter/core/common/activation_state.h"
19 #include "content/public/browser/navigation_handle.h" 19 #include "content/public/browser/navigation_handle.h"
20 #include "content/public/browser/navigation_throttle.h"
21 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
22 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
23 #include "url/gurl.h" 22 #include "url/gurl.h"
24 23
25 DEFINE_WEB_CONTENTS_USER_DATA_KEY( 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
26 subresource_filter::ContentSubresourceFilterDriverFactory); 25 subresource_filter::ContentSubresourceFilterDriverFactory);
27 26
28 namespace subresource_filter { 27 namespace subresource_filter {
29 28
30 namespace { 29 namespace {
31 30
32 std::string DistillURLToHostAndPath(const GURL& url) {
33 return url.host() + url.path();
34 }
35
36 // Returns true with a probability given by |performance_measurement_rate| if 31 // Returns true with a probability given by |performance_measurement_rate| if
37 // ThreadTicks is supported, otherwise returns false. 32 // ThreadTicks is supported, otherwise returns false.
38 bool ShouldMeasurePerformanceForPageLoad(double performance_measurement_rate) { 33 bool ShouldMeasurePerformanceForPageLoad(double performance_measurement_rate) {
39 if (!base::ThreadTicks::IsSupported()) 34 if (!base::ThreadTicks::IsSupported())
40 return false; 35 return false;
41 return performance_measurement_rate == 1 || 36 return performance_measurement_rate == 1 ||
42 (performance_measurement_rate > 0 && 37 (performance_measurement_rate > 0 &&
43 base::RandDouble() < performance_measurement_rate); 38 base::RandDouble() < performance_measurement_rate);
44 } 39 }
45 40
(...skipping 29 matching lines...) Expand all
75 base::MakeUnique<ContentSubresourceFilterThrottleManager>( 70 base::MakeUnique<ContentSubresourceFilterThrottleManager>(
76 this, 71 this,
77 client_->GetRulesetDealer(), 72 client_->GetRulesetDealer(),
78 web_contents)) {} 73 web_contents)) {}
79 74
80 ContentSubresourceFilterDriverFactory:: 75 ContentSubresourceFilterDriverFactory::
81 ~ContentSubresourceFilterDriverFactory() {} 76 ~ContentSubresourceFilterDriverFactory() {}
82 77
83 void ContentSubresourceFilterDriverFactory:: 78 void ContentSubresourceFilterDriverFactory::
84 OnMainResourceMatchedSafeBrowsingBlacklist( 79 OnMainResourceMatchedSafeBrowsingBlacklist(
85 const GURL& url, 80 content::NavigationHandle* navigation_handle,
86 safe_browsing::SBThreatType threat_type, 81 safe_browsing::SBThreatType threat_type,
87 safe_browsing::ThreatPatternType threat_type_metadata) { 82 safe_browsing::ThreatPatternType threat_type_metadata) {
88 AddActivationListMatch( 83 DCHECK(navigation_handle->IsInMainFrame());
89 url, GetListForThreatTypeAndMetadata(threat_type, threat_type_metadata)); 84 DCHECK(!navigation_handle->IsSameDocument());
85 if (navigation_handle->GetNetErrorCode() != net::OK)
86 return;
87
88 ActivationList activation_list =
89 GetListForThreatTypeAndMetadata(threat_type, threat_type_metadata);
90 const GURL& url = navigation_handle->GetURL();
91 const content::Referrer& referrer = navigation_handle->GetReferrer();
92 ui::PageTransition transition = navigation_handle->GetPageTransition();
93
94 if (activation_options_.should_whitelist_site_on_reload &&
95 NavigationIsPageReload(url, referrer, transition)) {
96 // Whitelist this host for the current as well as subsequent navigations.
97 client_->WhitelistInCurrentWebContents(url);
98 }
99
100 ComputeActivationForMainFrameNavigation(navigation_handle, activation_list);
101 DCHECK_NE(activation_decision_, ActivationDecision::UNKNOWN);
102
103 // Check for whitelisted status last, so that the client gets an accurate
104 // indication of whether there would be activation otherwise.
105 bool whitelisted = client_->OnPageActivationComputed(
106 navigation_handle,
107 activation_options_.activation_level == ActivationLevel::ENABLED);
108
109 // Only reset the activation decision reason if we would have activated.
110 if (whitelisted && activation_decision_ == ActivationDecision::ACTIVATED) {
111 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"), "ActivationWhitelisted");
112 activation_decision_ = ActivationDecision::URL_WHITELISTED;
113 activation_options_ = Configuration::ActivationOptions();
114 }
115
116 if (activation_decision_ != ActivationDecision::ACTIVATED) {
117 DCHECK_EQ(activation_options_.activation_level, ActivationLevel::DISABLED);
118 return;
119 }
120
121 DCHECK_NE(activation_options_.activation_level, ActivationLevel::DISABLED);
122 ActivationState state = ActivationState(activation_options_.activation_level);
123 state.measure_performance = ShouldMeasurePerformanceForPageLoad(
124 activation_options_.performance_measurement_rate);
125 // TODO(csharrison): Set state.enable_logging based on metadata returns from
126 // the safe browsing filter, when it is available. Add tests for this
127 // behavior.
128 throttle_manager_->NotifyPageActivationComputed(navigation_handle, state);
90 } 129 }
91 130
92 void ContentSubresourceFilterDriverFactory:: 131 void ContentSubresourceFilterDriverFactory::
93 ComputeActivationForMainFrameNavigation( 132 ComputeActivationForMainFrameNavigation(
94 content::NavigationHandle* navigation_handle) { 133 content::NavigationHandle* navigation_handle,
134 ActivationList matched_list) {
95 const GURL& url(navigation_handle->GetURL()); 135 const GURL& url(navigation_handle->GetURL());
96 136
97 if (!url.SchemeIsHTTPOrHTTPS()) { 137 if (!url.SchemeIsHTTPOrHTTPS()) {
98 activation_decision_ = ActivationDecision::UNSUPPORTED_SCHEME; 138 activation_decision_ = ActivationDecision::UNSUPPORTED_SCHEME;
99 activation_options_ = Configuration::ActivationOptions(); 139 activation_options_ = Configuration::ActivationOptions();
100 return; 140 return;
101 } 141 }
102 142
103 const auto config_list = GetEnabledConfigurations(); 143 const auto config_list = GetEnabledConfigurations();
104 const auto highest_priority_activated_config = 144 const auto highest_priority_activated_config =
105 std::find_if(config_list->configs_by_decreasing_priority().begin(), 145 std::find_if(config_list->configs_by_decreasing_priority().begin(),
106 config_list->configs_by_decreasing_priority().end(), 146 config_list->configs_by_decreasing_priority().end(),
107 [&url, this](const Configuration& config) { 147 [&url, matched_list, this](const Configuration& config) {
108 return DoesMainFrameURLSatisfyActivationConditions( 148 return DoesMainFrameURLSatisfyActivationConditions(
109 url, config.activation_conditions); 149 url, config.activation_conditions, matched_list);
110 }); 150 });
111 151
112 bool has_activated_config = 152 bool has_activated_config =
113 highest_priority_activated_config != 153 highest_priority_activated_config !=
114 config_list->configs_by_decreasing_priority().end(); 154 config_list->configs_by_decreasing_priority().end();
115 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("loading"), 155 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("loading"),
116 "ContentSubresourceFilterDriverFactory::" 156 "ContentSubresourceFilterDriverFactory::"
117 "ComputeActivationForMainFrameNavigation", 157 "ComputeActivationForMainFrameNavigation",
118 "highest_priority_activated_config", 158 "highest_priority_activated_config",
119 has_activated_config 159 has_activated_config
120 ? highest_priority_activated_config->ToTracedValue() 160 ? highest_priority_activated_config->ToTracedValue()
121 : base::MakeUnique<base::trace_event::TracedValue>()); 161 : base::MakeUnique<base::trace_event::TracedValue>());
122 if (!has_activated_config) { 162 if (!has_activated_config) {
123 activation_decision_ = ActivationDecision::ACTIVATION_CONDITIONS_NOT_MET; 163 activation_decision_ = ActivationDecision::ACTIVATION_CONDITIONS_NOT_MET;
124 activation_options_ = Configuration::ActivationOptions(); 164 activation_options_ = Configuration::ActivationOptions();
125 return; 165 return;
126 } 166 }
127 167
128 activation_options_ = highest_priority_activated_config->activation_options; 168 activation_options_ = highest_priority_activated_config->activation_options;
129 activation_decision_ = 169 activation_decision_ =
130 activation_options_.activation_level == ActivationLevel::DISABLED 170 activation_options_.activation_level == ActivationLevel::DISABLED
131 ? ActivationDecision::ACTIVATION_DISABLED 171 ? ActivationDecision::ACTIVATION_DISABLED
132 : ActivationDecision::ACTIVATED; 172 : ActivationDecision::ACTIVATED;
133 } 173 }
134 174
135 bool ContentSubresourceFilterDriverFactory:: 175 bool ContentSubresourceFilterDriverFactory::
136 DoesMainFrameURLSatisfyActivationConditions( 176 DoesMainFrameURLSatisfyActivationConditions(
137 const GURL& url, 177 const GURL& url,
138 const Configuration::ActivationConditions& conditions) const { 178 const Configuration::ActivationConditions& conditions,
179 ActivationList matched_list) const {
139 switch (conditions.activation_scope) { 180 switch (conditions.activation_scope) {
140 case ActivationScope::ALL_SITES: 181 case ActivationScope::ALL_SITES:
141 return true; 182 return true;
142 case ActivationScope::ACTIVATION_LIST: 183 case ActivationScope::ACTIVATION_LIST:
143 if (DidURLMatchActivationList(url, conditions.activation_list)) 184 if (conditions.activation_list == matched_list)
144 return true; 185 return true;
145 if (conditions.activation_list == ActivationList::PHISHING_INTERSTITIAL && 186 if (conditions.activation_list == ActivationList::PHISHING_INTERSTITIAL &&
146 DidURLMatchActivationList( 187 matched_list == ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL) {
147 url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL)) {
148 // Handling special case, where activation on the phishing sites also 188 // Handling special case, where activation on the phishing sites also
149 // mean the activation on the sites with social engineering metadata. 189 // mean the activation on the sites with social engineering metadata.
150 return true; 190 return true;
151 } 191 }
152 return false; 192 return false;
153 case ActivationScope::NO_SITES: 193 case ActivationScope::NO_SITES:
154 return false; 194 return false;
155 } 195 }
156 NOTREACHED(); 196 NOTREACHED();
157 return false; 197 return false;
158 } 198 }
159 199
160 void ContentSubresourceFilterDriverFactory::OnReloadRequested() { 200 void ContentSubresourceFilterDriverFactory::OnReloadRequested() {
161 UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumReloads", true); 201 UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumReloads", true);
162 const GURL& whitelist_url = web_contents()->GetLastCommittedURL(); 202 const GURL& whitelist_url = web_contents()->GetLastCommittedURL();
163 203
164 // Only whitelist via content settings when using the experimental UI, 204 // Only whitelist via content settings when using the experimental UI,
165 // otherwise could get into a situation where content settings cannot be 205 // otherwise could get into a situation where content settings cannot be
166 // adjusted. 206 // adjusted.
167 if (base::FeatureList::IsEnabled( 207 if (base::FeatureList::IsEnabled(
168 subresource_filter::kSafeBrowsingSubresourceFilterExperimentalUI)) { 208 subresource_filter::kSafeBrowsingSubresourceFilterExperimentalUI)) {
169 client_->WhitelistByContentSettings(whitelist_url); 209 client_->WhitelistByContentSettings(whitelist_url);
170 } else { 210 } else {
171 client_->WhitelistInCurrentWebContents(whitelist_url); 211 client_->WhitelistInCurrentWebContents(whitelist_url);
172 } 212 }
173 web_contents()->GetController().Reload(content::ReloadType::NORMAL, true); 213 web_contents()->GetController().Reload(content::ReloadType::NORMAL, true);
174 } 214 }
175 215
176 void ContentSubresourceFilterDriverFactory::WillProcessResponse(
177 content::NavigationHandle* navigation_handle) {
178 DCHECK(!navigation_handle->IsSameDocument());
179 if (!navigation_handle->IsInMainFrame() ||
180 navigation_handle->GetNetErrorCode() != net::OK) {
181 return;
182 }
183
184 const GURL& url = navigation_handle->GetURL();
185 const content::Referrer& referrer = navigation_handle->GetReferrer();
186 ui::PageTransition transition = navigation_handle->GetPageTransition();
187
188 if (activation_options_.should_whitelist_site_on_reload &&
189 NavigationIsPageReload(url, referrer, transition)) {
190 // Whitelist this host for the current as well as subsequent navigations.
191 client_->WhitelistInCurrentWebContents(url);
192 }
193
194 ComputeActivationForMainFrameNavigation(navigation_handle);
195 DCHECK_NE(activation_decision_, ActivationDecision::UNKNOWN);
196
197 // Check for whitelisted status last, so that the client gets an accurate
198 // indication of whether there would be activation otherwise.
199 bool whitelisted = client_->OnPageActivationComputed(
200 navigation_handle,
201 activation_options_.activation_level == ActivationLevel::ENABLED);
202
203 // Only reset the activation decision reason if we would have activated.
204 if (whitelisted && activation_decision_ == ActivationDecision::ACTIVATED) {
205 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"), "ActivationWhitelisted");
206 activation_decision_ = ActivationDecision::URL_WHITELISTED;
207 activation_options_ = Configuration::ActivationOptions();
208 }
209
210 if (activation_decision_ != ActivationDecision::ACTIVATED) {
211 DCHECK_EQ(activation_options_.activation_level, ActivationLevel::DISABLED);
212 return;
213 }
214
215 DCHECK_NE(activation_options_.activation_level, ActivationLevel::DISABLED);
216 ActivationState state = ActivationState(activation_options_.activation_level);
217 state.measure_performance = ShouldMeasurePerformanceForPageLoad(
218 activation_options_.performance_measurement_rate);
219 // TODO(csharrison): Set state.enable_logging based on metadata returns from
220 // the safe browsing filter, when it is available. Add tests for this
221 // behavior.
222 throttle_manager_->NotifyPageActivationComputed(navigation_handle, state);
223 }
224
225 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() { 216 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() {
226 if (activation_options_.should_suppress_notifications) 217 if (activation_options_.should_suppress_notifications)
227 return; 218 return;
228 client_->ToggleNotificationVisibility(activation_options_.activation_level == 219 client_->ToggleNotificationVisibility(activation_options_.activation_level ==
229 ActivationLevel::ENABLED); 220 ActivationLevel::ENABLED);
230 } 221 }
231 222
232 void ContentSubresourceFilterDriverFactory::DidStartNavigation( 223 void ContentSubresourceFilterDriverFactory::DidStartNavigation(
233 content::NavigationHandle* navigation_handle) { 224 content::NavigationHandle* navigation_handle) {
234 if (navigation_handle->IsInMainFrame() && 225 if (navigation_handle->IsInMainFrame() &&
235 !navigation_handle->IsSameDocument()) { 226 !navigation_handle->IsSameDocument()) {
236 activation_decision_ = ActivationDecision::UNKNOWN; 227 activation_decision_ = ActivationDecision::UNKNOWN;
237 activation_list_matches_.clear();
238 client_->ToggleNotificationVisibility(false); 228 client_->ToggleNotificationVisibility(false);
239 } 229 }
240 } 230 }
241 231
242 void ContentSubresourceFilterDriverFactory::DidFinishNavigation( 232 void ContentSubresourceFilterDriverFactory::DidFinishNavigation(
243 content::NavigationHandle* navigation_handle) { 233 content::NavigationHandle* navigation_handle) {
244 if (navigation_handle->IsInMainFrame() && 234 if (navigation_handle->IsInMainFrame() &&
245 !navigation_handle->IsSameDocument() && 235 !navigation_handle->IsSameDocument() &&
246 activation_decision_ == ActivationDecision::UNKNOWN && 236 activation_decision_ == ActivationDecision::UNKNOWN &&
247 navigation_handle->HasCommitted()) { 237 navigation_handle->HasCommitted()) {
248 activation_decision_ = ActivationDecision::ACTIVATION_DISABLED; 238 activation_decision_ = ActivationDecision::ACTIVATION_DISABLED;
249 activation_options_ = Configuration::ActivationOptions(); 239 activation_options_ = Configuration::ActivationOptions();
250 } 240 }
251 } 241 }
252 242
253 bool ContentSubresourceFilterDriverFactory::DidURLMatchActivationList(
254 const GURL& url,
255 ActivationList activation_list) const {
256 auto match_types =
257 activation_list_matches_.find(DistillURLToHostAndPath(url));
258 return match_types != activation_list_matches_.end() &&
259 match_types->second.find(activation_list) != match_types->second.end();
260 }
261
262 void ContentSubresourceFilterDriverFactory::AddActivationListMatch(
263 const GURL& url,
264 ActivationList match_type) {
265 if (match_type == ActivationList::NONE)
266 return;
267 if (url.has_host() && url.SchemeIsHTTPOrHTTPS())
268 activation_list_matches_[DistillURLToHostAndPath(url)].insert(match_type);
269 }
270
271 } // namespace subresource_filter 243 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698