Chromium Code Reviews| Index: components/subresource_filter/content/browser/subresource_filter_activation_navigation_throttle.cc |
| diff --git a/components/subresource_filter/content/browser/subresource_filter_activation_navigation_throttle.cc b/components/subresource_filter/content/browser/subresource_filter_activation_navigation_throttle.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c35a366c0a63cb7a52fe7d5070598c8eeb04e966 |
| --- /dev/null |
| +++ b/components/subresource_filter/content/browser/subresource_filter_activation_navigation_throttle.cc |
| @@ -0,0 +1,99 @@ |
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/subresource_filter/content/browser/subresource_filter_activation_navigation_throttle.h" |
| + |
| +#include "components/safe_browsing_db/v4_local_database_manager.h" |
| +#include "components/subresource_filter/content/browser/content_subresource_filter_driver_factory.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace { |
| + |
| +// Maximum time in milliseconds to wait for the safe browsing service to |
| +// verify a URL. After this amount of time the outstanding check will be |
| +// aborted, and the URL will be treated as if it doesn't belong to the |
| +// Subresource Filter only list. |
| +const int kCheckUrlTimeoutMs = 5000; |
| + |
| +} // namespace |
| + |
| +namespace subresource_filter { |
| + |
| +SubresourceFilterActivationNavigationThrottle:: |
| + SubresourceFilterActivationNavigationThrottle( |
| + content::NavigationHandle* handle, |
| + const scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>& |
| + database_manager) |
| + : NavigationThrottle(handle), database_manager_(database_manager) {} |
| + |
| +SubresourceFilterActivationNavigationThrottle:: |
| + ~SubresourceFilterActivationNavigationThrottle() {} |
| + |
| +content::NavigationThrottle::ThrottleCheckResult |
| +SubresourceFilterActivationNavigationThrottle::WillProcessResponse() { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SubresourceFilterActivationNavigationThrottle::CheckUrlOnIO, |
| + base::Unretained(this), navigation_handle()->GetURL())); |
| + return content::NavigationThrottle::ThrottleCheckResult::DEFER; |
| +} |
| + |
| +void SubresourceFilterActivationNavigationThrottle::CheckUrlOnIO( |
| + const GURL& url) { |
| + url_being_checked_ = url; |
| + if (database_manager_->CheckUrlForSubresourceFilter(url, this)) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&SubresourceFilterActivationNavigationThrottle:: |
| + OnCheckUrlResultOnUI, |
| + AsWeakPtr(), url, safe_browsing::SB_THREAT_TYPE_SAFE, |
| + safe_browsing::ThreatPatternType::NONE)); |
| + } |
| + timer_.Start( |
| + FROM_HERE, base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), this, |
| + &SubresourceFilterActivationNavigationThrottle::OnCheckUrlTimeout); |
| +} |
| + |
| +// SafeBrowsingService::Client implementation, called on the IO thread once |
|
engedy
2017/02/17 20:50:52
nit: I'd just remove this comment (sign) when we a
melandory
2017/02/28 12:16:05
Done.
|
| +// the URL has been classified. |
| +void SubresourceFilterActivationNavigationThrottle::OnCheckBrowseUrlResult( |
| + const GURL& url, |
| + safe_browsing::SBThreatType threat_type, |
| + const safe_browsing::ThreatMetadata& metadata) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + timer_.Stop(); // Cancel the timeout timer. |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind( |
| + &SubresourceFilterActivationNavigationThrottle::OnCheckUrlResultOnUI, |
| + AsWeakPtr(), url, threat_type, metadata.threat_pattern_type)); |
| +} |
| + |
| +void SubresourceFilterActivationNavigationThrottle::OnCheckUrlResultOnUI( |
| + const GURL& url, |
| + safe_browsing::SBThreatType threat_type, |
| + safe_browsing::ThreatPatternType pattern_type) { |
| + content::WebContents* web_contents = navigation_handle()->GetWebContents(); |
| + if (web_contents) { |
| + using subresource_filter::ContentSubresourceFilterDriverFactory; |
| + ContentSubresourceFilterDriverFactory* driver_factory = |
| + ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); |
| + DCHECK(driver_factory); |
| + |
| + driver_factory->OnMainResourceMatchedSafeBrowsingBlacklist( |
| + url, std::vector<GURL>(), threat_type, pattern_type); |
| + } |
| + navigation_handle()->Resume(); |
| +} |
| + |
| +void SubresourceFilterActivationNavigationThrottle::OnCheckUrlTimeout() { |
| + database_manager_->CancelCheck(this); |
| + |
| + OnCheckBrowseUrlResult(url_being_checked_, safe_browsing::SB_THREAT_TYPE_SAFE, |
| + safe_browsing::ThreatMetadata()); |
| +} |
| + |
| +} // namespace subresource_filter |