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/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver.h" | |
| 11 #include "components/subresource_filter/content/common/subresource_filter_messag es.h" | 10 #include "components/subresource_filter/content/common/subresource_filter_messag es.h" |
| 12 #include "components/subresource_filter/core/browser/subresource_filter_client.h " | 11 #include "components/subresource_filter/core/browser/subresource_filter_client.h " |
| 13 #include "components/subresource_filter/core/browser/subresource_filter_features .h" | 12 #include "components/subresource_filter/core/browser/subresource_filter_features .h" |
| 14 #include "components/subresource_filter/core/common/activation_list.h" | 13 #include "components/subresource_filter/core/common/activation_list.h" |
| 15 #include "components/subresource_filter/core/common/time_measurements.h" | 14 #include "components/subresource_filter/core/common/time_measurements.h" |
| 16 #include "content/public/browser/navigation_handle.h" | 15 #include "content/public/browser/navigation_handle.h" |
| 17 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 19 #include "ipc/ipc_message_macros.h" | 18 #include "ipc/ipc_message_macros.h" |
| 20 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 return static_cast<ContentSubresourceFilterDriverFactory*>( | 69 return static_cast<ContentSubresourceFilterDriverFactory*>( |
| 71 web_contents->GetUserData(kWebContentsUserDataKey)); | 70 web_contents->GetUserData(kWebContentsUserDataKey)); |
| 72 } | 71 } |
| 73 | 72 |
| 74 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory( | 73 ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory( |
| 75 content::WebContents* web_contents, | 74 content::WebContents* web_contents, |
| 76 std::unique_ptr<SubresourceFilterClient> client) | 75 std::unique_ptr<SubresourceFilterClient> client) |
| 77 : content::WebContentsObserver(web_contents), | 76 : content::WebContentsObserver(web_contents), |
| 78 client_(std::move(client)), | 77 client_(std::move(client)), |
| 79 activation_level_(ActivationLevel::DISABLED), | 78 activation_level_(ActivationLevel::DISABLED), |
| 80 measure_performance_(false) { | 79 measure_performance_(false) {} |
| 81 content::RenderFrameHost* main_frame_host = web_contents->GetMainFrame(); | |
| 82 if (main_frame_host && main_frame_host->IsRenderFrameLive()) | |
| 83 CreateDriverForFrameHostIfNeeded(main_frame_host); | |
| 84 } | |
| 85 | 80 |
| 86 ContentSubresourceFilterDriverFactory:: | 81 ContentSubresourceFilterDriverFactory:: |
| 87 ~ContentSubresourceFilterDriverFactory() {} | 82 ~ContentSubresourceFilterDriverFactory() {} |
| 88 | 83 |
| 89 void ContentSubresourceFilterDriverFactory::CreateDriverForFrameHostIfNeeded( | |
| 90 content::RenderFrameHost* render_frame_host) { | |
| 91 auto iterator_and_inserted = | |
| 92 frame_drivers_.insert(std::make_pair(render_frame_host, nullptr)); | |
| 93 if (iterator_and_inserted.second) { | |
| 94 iterator_and_inserted.first->second.reset( | |
| 95 new ContentSubresourceFilterDriver(render_frame_host)); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() { | 84 void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() { |
| 100 if (ShouldSuppressNotifications()) | 85 if (ShouldSuppressNotifications()) |
| 101 return; | 86 return; |
| 102 | 87 |
| 103 client_->ToggleNotificationVisibility(activation_level_ == | 88 client_->ToggleNotificationVisibility(activation_level_ == |
| 104 ActivationLevel::ENABLED); | 89 ActivationLevel::ENABLED); |
| 105 } | 90 } |
| 106 | 91 |
| 107 void ContentSubresourceFilterDriverFactory::OnDocumentLoadStatistics( | 92 void ContentSubresourceFilterDriverFactory::OnDocumentLoadStatistics( |
| 108 const DocumentLoadStatistics& statistics) { | 93 const DocumentLoadStatistics& statistics) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 void ContentSubresourceFilterDriverFactory::ActivateForFrameHostIfNeeded( | 157 void ContentSubresourceFilterDriverFactory::ActivateForFrameHostIfNeeded( |
| 173 content::RenderFrameHost* render_frame_host, | 158 content::RenderFrameHost* render_frame_host, |
| 174 const GURL& url, | 159 const GURL& url, |
| 175 bool failed_navigation) { | 160 bool failed_navigation) { |
| 176 // PlzNavigate: For failed navigations, ReadyToCommitNavigation is still | 161 // PlzNavigate: For failed navigations, ReadyToCommitNavigation is still |
| 177 // called, so we end up here; but there is no longer a failed provisional load | 162 // called, so we end up here; but there is no longer a failed provisional load |
| 178 // on the renderer side, so an activation message sent from here would turn on | 163 // on the renderer side, so an activation message sent from here would turn on |
| 179 // filtering for the subsequent error page load. This is probably harmless, | 164 // filtering for the subsequent error page load. This is probably harmless, |
| 180 // but not sending an activation message is even cleaner. | 165 // but not sending an activation message is even cleaner. |
| 181 if (activation_level_ != ActivationLevel::DISABLED && !failed_navigation) { | 166 if (activation_level_ != ActivationLevel::DISABLED && !failed_navigation) { |
| 182 auto* driver = DriverFromFrameHost(render_frame_host); | 167 render_frame_host->Send( |
| 183 DCHECK(driver); | 168 new SubresourceFilterMsg_ActivateForNextCommittedLoad( |
| 184 driver->ActivateForNextCommittedLoad(GetMaximumActivationLevel(), | 169 render_frame_host->GetRoutingID(), activation_level_, |
|
Charlie Harrison
2017/02/15 19:22:23
Is removing GetMaximumActivationLevel here just a
engedy
2017/02/15 21:36:55
Yes, the two should always have the same value, so
| |
| 185 measure_performance_); | 170 measure_performance_)); |
| 186 } | 171 } |
| 187 } | 172 } |
| 188 | 173 |
| 189 void ContentSubresourceFilterDriverFactory::OnReloadRequested() { | 174 void ContentSubresourceFilterDriverFactory::OnReloadRequested() { |
| 190 UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumReloads", true); | 175 UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumReloads", true); |
| 191 const GURL& whitelist_url = web_contents()->GetLastCommittedURL(); | 176 const GURL& whitelist_url = web_contents()->GetLastCommittedURL(); |
| 192 AddHostOfURLToWhitelistSet(whitelist_url); | 177 AddHostOfURLToWhitelistSet(whitelist_url); |
| 193 web_contents()->GetController().Reload(content::ReloadType::NORMAL, true); | 178 web_contents()->GetController().Reload(content::ReloadType::NORMAL, true); |
| 194 } | 179 } |
| 195 | 180 |
| 196 void ContentSubresourceFilterDriverFactory::SetDriverForFrameHostForTesting( | |
| 197 content::RenderFrameHost* render_frame_host, | |
| 198 std::unique_ptr<ContentSubresourceFilterDriver> driver) { | |
| 199 auto iterator_and_inserted = | |
| 200 frame_drivers_.insert(std::make_pair(render_frame_host, nullptr)); | |
| 201 iterator_and_inserted.first->second = std::move(driver); | |
| 202 } | |
| 203 | |
| 204 ContentSubresourceFilterDriver* | |
| 205 ContentSubresourceFilterDriverFactory::DriverFromFrameHost( | |
| 206 content::RenderFrameHost* render_frame_host) { | |
| 207 auto iterator = frame_drivers_.find(render_frame_host); | |
| 208 return iterator == frame_drivers_.end() ? nullptr : iterator->second.get(); | |
| 209 } | |
| 210 | |
| 211 void ContentSubresourceFilterDriverFactory::ResetActivationState() { | 181 void ContentSubresourceFilterDriverFactory::ResetActivationState() { |
| 212 navigation_chain_.clear(); | 182 navigation_chain_.clear(); |
| 213 activation_list_matches_.clear(); | 183 activation_list_matches_.clear(); |
| 214 activation_level_ = ActivationLevel::DISABLED; | 184 activation_level_ = ActivationLevel::DISABLED; |
| 215 measure_performance_ = false; | 185 measure_performance_ = false; |
| 216 aggregated_document_statistics_ = DocumentLoadStatistics(); | 186 aggregated_document_statistics_ = DocumentLoadStatistics(); |
| 217 } | 187 } |
| 218 | 188 |
| 219 void ContentSubresourceFilterDriverFactory::DidStartNavigation( | 189 void ContentSubresourceFilterDriverFactory::DidStartNavigation( |
| 220 content::NavigationHandle* navigation_handle) { | 190 content::NavigationHandle* navigation_handle) { |
| 221 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) { | 191 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) { |
| 222 ResetActivationState(); | 192 ResetActivationState(); |
| 223 navigation_chain_.push_back(navigation_handle->GetURL()); | 193 navigation_chain_.push_back(navigation_handle->GetURL()); |
| 224 client_->ToggleNotificationVisibility(false); | 194 client_->ToggleNotificationVisibility(false); |
| 225 } | 195 } |
| 226 } | 196 } |
| 227 | 197 |
| 228 void ContentSubresourceFilterDriverFactory::DidRedirectNavigation( | 198 void ContentSubresourceFilterDriverFactory::DidRedirectNavigation( |
| 229 content::NavigationHandle* navigation_handle) { | 199 content::NavigationHandle* navigation_handle) { |
| 230 DCHECK(!navigation_handle->IsSamePage()); | 200 DCHECK(!navigation_handle->IsSamePage()); |
| 231 if (navigation_handle->IsInMainFrame()) | 201 if (navigation_handle->IsInMainFrame()) |
| 232 navigation_chain_.push_back(navigation_handle->GetURL()); | 202 navigation_chain_.push_back(navigation_handle->GetURL()); |
| 233 } | 203 } |
| 234 | 204 |
| 235 void ContentSubresourceFilterDriverFactory::RenderFrameCreated( | |
| 236 content::RenderFrameHost* render_frame_host) { | |
| 237 CreateDriverForFrameHostIfNeeded(render_frame_host); | |
| 238 } | |
| 239 | |
| 240 void ContentSubresourceFilterDriverFactory::RenderFrameDeleted( | |
| 241 content::RenderFrameHost* render_frame_host) { | |
| 242 frame_drivers_.erase(render_frame_host); | |
| 243 } | |
| 244 | |
| 245 void ContentSubresourceFilterDriverFactory::ReadyToCommitNavigation( | 205 void ContentSubresourceFilterDriverFactory::ReadyToCommitNavigation( |
| 246 content::NavigationHandle* navigation_handle) { | 206 content::NavigationHandle* navigation_handle) { |
| 247 DCHECK(!navigation_handle->IsSamePage()); | 207 DCHECK(!navigation_handle->IsSamePage()); |
| 248 content::RenderFrameHost* render_frame_host = | 208 content::RenderFrameHost* render_frame_host = |
| 249 navigation_handle->GetRenderFrameHost(); | 209 navigation_handle->GetRenderFrameHost(); |
| 250 GURL url = navigation_handle->GetURL(); | 210 GURL url = navigation_handle->GetURL(); |
| 251 const content::Referrer& referrer = navigation_handle->GetReferrer(); | 211 const content::Referrer& referrer = navigation_handle->GetReferrer(); |
| 252 ui::PageTransition transition = navigation_handle->GetPageTransition(); | 212 ui::PageTransition transition = navigation_handle->GetPageTransition(); |
| 253 ReadyToCommitNavigationInternal( | 213 ReadyToCommitNavigationInternal( |
| 254 render_frame_host, url, referrer, transition, | 214 render_frame_host, url, referrer, transition, |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 if (!hits_pattern) | 344 if (!hits_pattern) |
| 385 return; | 345 return; |
| 386 UMA_HISTOGRAM_ENUMERATION( | 346 UMA_HISTOGRAM_ENUMERATION( |
| 387 "SubresourceFilter.PageLoad.RedirectChainMatchPattern", hits_pattern, | 347 "SubresourceFilter.PageLoad.RedirectChainMatchPattern", hits_pattern, |
| 388 0x10 /* max value */); | 348 0x10 /* max value */); |
| 389 UMA_HISTOGRAM_COUNTS("SubresourceFilter.PageLoad.RedirectChainLength", | 349 UMA_HISTOGRAM_COUNTS("SubresourceFilter.PageLoad.RedirectChainLength", |
| 390 navigation_chain_.size()); | 350 navigation_chain_.size()); |
| 391 } | 351 } |
| 392 | 352 |
| 393 } // namespace subresource_filter | 353 } // namespace subresource_filter |
| OLD | NEW |