| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/renderer_host/safe_browsing_navigation_throttle.h" |
| 6 |
| 7 #include "chrome/browser/prerender/prerender_contents.h" |
| 8 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/navigation_handle.h" |
| 11 #include "content/public/browser/resource_request_info.h" |
| 12 |
| 13 // static |
| 14 std::unique_ptr<content::NavigationThrottle> |
| 15 SafeBrowsingNavigationThrottle::MaybeCreate( |
| 16 content::NavigationHandle* handle, |
| 17 safe_browsing::SafeBrowsingService* sb_service) { |
| 18 if (sb_service->database_manager()->IsSupported()) { |
| 19 return std::unique_ptr<content::NavigationThrottle>( |
| 20 new SafeBrowsingNavigationThrottle(handle, sb_service)); |
| 21 } |
| 22 return nullptr; |
| 23 } |
| 24 |
| 25 SafeBrowsingNavigationThrottle::~SafeBrowsingNavigationThrottle() { |
| 26 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 27 } |
| 28 |
| 29 SafeBrowsingNavigationThrottle::SafeBrowsingNavigationThrottle( |
| 30 content::NavigationHandle* handle, |
| 31 safe_browsing::SafeBrowsingService* sb_service) |
| 32 : NavigationThrottle(handle), |
| 33 database_manager_(sb_service->database_manager()), |
| 34 ui_manager_(sb_service->ui_manager()) { |
| 35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 36 } |
| 37 |
| 38 content::NavigationThrottle::ThrottleCheckResult |
| 39 SafeBrowsingNavigationThrottle::WillStartRequest() { |
| 40 content::BrowserThread::PostTask( |
| 41 content::BrowserThread::IO, FROM_HERE, |
| 42 base::Bind( |
| 43 &SafeBrowsingNavigationThrottle::CheckUrlOnIO, |
| 44 base::Unretained( |
| 45 this) /* TODO(scottmg) not sure about this; can't be weak */, |
| 46 navigation_handle()->GetURL())); |
| 47 return ThrottleCheckResult::DEFER; |
| 48 } |
| 49 |
| 50 content::NavigationThrottle::ThrottleCheckResult |
| 51 SafeBrowsingNavigationThrottle::WillRedirectRequest() { |
| 52 return content::NavigationThrottle::ThrottleCheckResult::PROCEED; |
| 53 } |
| 54 |
| 55 void SafeBrowsingNavigationThrottle::OnCheckBrowseUrlResult( |
| 56 const GURL& url, |
| 57 safe_browsing::SBThreatType result, |
| 58 const safe_browsing::ThreatMetadata& metadata) { |
| 59 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 60 content::BrowserThread::PostTask( |
| 61 content::BrowserThread::UI, FROM_HERE, |
| 62 base::Bind(&SafeBrowsingNavigationThrottle::OnCheckUrlResultOnUI, |
| 63 AsWeakPtr(), url, result, metadata)); |
| 64 } |
| 65 |
| 66 void SafeBrowsingNavigationThrottle::CheckUrlOnIO(const GURL& url) { |
| 67 bool succeeded_synchronously = database_manager_->CheckBrowseUrl(url, this); |
| 68 if (succeeded_synchronously) { |
| 69 content::BrowserThread::PostTask( |
| 70 content::BrowserThread::UI, FROM_HERE, |
| 71 base::Bind(&SafeBrowsingNavigationThrottle::OnCheckUrlResultOnUI, |
| 72 AsWeakPtr(), url, safe_browsing::SB_THREAT_TYPE_SAFE, |
| 73 safe_browsing::ThreatMetadata())); |
| 74 } |
| 75 // TODO(scottmg): Timeout == let it go. |
| 76 } |
| 77 |
| 78 content::WebContents* |
| 79 SafeBrowsingNavigationThrottle::GetNavigationHandlesWebContents() { |
| 80 return navigation_handle()->GetWebContents(); |
| 81 } |
| 82 |
| 83 void SafeBrowsingNavigationThrottle::OnCheckUrlResultOnUI( |
| 84 const GURL& url, |
| 85 safe_browsing::SBThreatType result, |
| 86 const safe_browsing::ThreatMetadata& metadata) { |
| 87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 88 if (result == safe_browsing::SB_THREAT_TYPE_SAFE) { |
| 89 navigation_handle()->Resume(); |
| 90 return; |
| 91 } |
| 92 |
| 93 subresource_filter::ContentSubresourceFilterDriverFactory* driver_factory = |
| 94 subresource_filter::ContentSubresourceFilterDriverFactory:: |
| 95 FromWebContents(navigation_handle()->GetWebContents()); |
| 96 DCHECK(driver_factory); |
| 97 driver_factory->OnMainResourceMatchedSafeBrowsingBlacklist( |
| 98 url, std::vector<GURL>(), metadata.threat_pattern_type); |
| 99 |
| 100 prerender::PrerenderContents* prerender_contents = |
| 101 prerender::PrerenderContents::FromWebContents( |
| 102 navigation_handle()->GetWebContents()); |
| 103 if (prerender_contents) { |
| 104 prerender_contents->Destroy(prerender::FINAL_STATUS_SAFE_BROWSING); |
| 105 navigation_handle()->CancelDeferredNavigation(CANCEL); |
| 106 return; |
| 107 } |
| 108 |
| 109 safe_browsing::SafeBrowsingUIManager::UnsafeResource resource; |
| 110 resource.url = url; |
| 111 resource.original_url = navigation_handle()->GetURL(); // TODO(scottmg) |
| 112 resource.redirect_urls = std::vector<GURL>(); // TODO(scottmg) |
| 113 resource.is_subresource = false; |
| 114 resource.is_subframe = |
| 115 !navigation_handle()->IsInMainFrame(); // TODO(scottmg): Correct? |
| 116 resource.threat_type = result; |
| 117 resource.threat_metadata = metadata; |
| 118 resource.callback = base::Bind( |
| 119 &SafeBrowsingNavigationThrottle::OnBlockingPageComplete, AsWeakPtr()); |
| 120 resource.callback_thread = content::BrowserThread::GetTaskRunnerForThread( |
| 121 content::BrowserThread::UI); |
| 122 // This stays on the UI thread, so we can just return the WebContents*. |
| 123 resource.web_contents_getter = base::Bind( |
| 124 &SafeBrowsingNavigationThrottle::GetNavigationHandlesWebContents, |
| 125 base::Unretained(this)); |
| 126 resource.threat_source = database_manager_->GetThreatSource(); |
| 127 ui_manager_->DisplayBlockingPage(resource); |
| 128 } |
| 129 |
| 130 void SafeBrowsingNavigationThrottle::OnBlockingPageComplete(bool proceed) { |
| 131 if (proceed) { |
| 132 navigation_handle()->Resume(); |
| 133 } else { |
| 134 navigation_handle()->CancelDeferredNavigation(CANCEL); |
| 135 } |
| 136 } |
| OLD | NEW |