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

Unified Diff: components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle.cc

Issue 2645283007: Add the client for accessing Subresource Filter only list. (Closed)
Patch Set: only changes specific to this cl Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle.cc
diff --git a/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle.cc b/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c88cd45e71747e50e0cad075e9f1dcf3eb22433e
--- /dev/null
+++ b/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle.cc
@@ -0,0 +1,141 @@
+// 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_safe_browsing_activation_throttle.h"
+
+#include "base/timer/timer.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
engedy 2017/03/21 13:44:16 nit: %s/safe browsing/Safe Browsing/g
melandory 2017/03/24 15:49:28 Done.
+// 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
engedy 2017/03/21 13:44:16 nit: s/doesn't/didn't/
melandory 2017/03/24 15:49:28 Done.
+// Subresource Filter only list.
+const int kCheckUrlTimeoutMs = 5000;
engedy 2017/03/21 13:44:16 nit: Apparently the fashionable way to write this
melandory 2017/03/24 15:49:28 Done.
+
+} // namespace
+
+namespace subresource_filter {
+
+class SubresourceFilterSafeBrowsingActivationThrottle::SBDatabaseClient
+ : public safe_browsing::SafeBrowsingDatabaseManager::Client {
+ public:
+ SBDatabaseClient(
+ scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
+ database_manager,
+ base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle,
+ scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner)
+ : database_manager_(std::move(database_manager)),
+ throttle_(throttle),
+ callback_task_runner_(callback_task_runner) {}
+
+ ~SBDatabaseClient() override {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
+ database_manager_->CancelCheck(this);
+ }
+
+ void CheckUrlOnIO(const GURL& url) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
+ if (database_manager_->CheckUrlForSubresourceFilter(url, this)) {
+ url_being_checked_ = url;
+ DCHECK(!url_being_checked_.is_empty());
vakh (use Gerrit instead) 2017/03/16 20:51:18 This CHECK should probably be at the beginning of
melandory 2017/03/24 15:49:28 Done.
+ OnCheckBrowseUrlResult(url, safe_browsing::SB_THREAT_TYPE_SAFE,
+ safe_browsing::ThreatMetadata());
+ return;
+ }
+ timer_.Start(FROM_HERE,
+ base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), this,
+ &SubresourceFilterSafeBrowsingActivationThrottle::
+ SBDatabaseClient::OnCheckUrlTimeout);
+ }
+
+ void OnCheckBrowseUrlResult(
+ const GURL& url,
+ safe_browsing::SBThreatType threat_type,
+ const safe_browsing::ThreatMetadata& metadata) override {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
vakh (use Gerrit instead) 2017/03/16 20:51:18 Optional: I would encourage you to add the check/d
melandory 2017/03/24 15:49:28 Done.
+ timer_.Stop(); // Cancel the timeout timer.
+ callback_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle::
+ OnCheckUrlResultOnUI,
+ throttle_, url, threat_type, metadata.threat_pattern_type));
+ }
+
+ // Callback for when the safe browsing check has taken longer than
+ // kCheckUrlTimeoutMs.
+ void OnCheckUrlTimeout() {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
+ database_manager_->CancelCheck(this);
+
+ OnCheckBrowseUrlResult(url_being_checked_,
+ safe_browsing::SB_THREAT_TYPE_SAFE,
+ safe_browsing::ThreatMetadata());
+ }
+
+ private:
+ scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_;
+
+ // Timer to abort the safe browsing check if it takes too long.
+ base::OneShotTimer timer_;
+ GURL url_being_checked_;
+ base::ThreadChecker thread_checker_;
engedy 2017/03/21 13:44:16 nit: No longer used.
melandory 2017/03/24 15:49:28 Done.
+
+ base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle_;
+ scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(SBDatabaseClient);
+};
+
+SubresourceFilterSafeBrowsingActivationThrottle::
+ SubresourceFilterSafeBrowsingActivationThrottle(
+ content::NavigationHandle* handle,
+ scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
+ database_manager)
+ : NavigationThrottle(handle),
+ callback_task_runner_(content::BrowserThread::GetTaskRunnerForThread(
engedy 2017/03/21 13:44:16 nit: It's a bit confusing to name both task runner
melandory 2017/03/24 15:49:28 Done.
+ content::BrowserThread::IO)),
+ database_client_(
+ new SubresourceFilterSafeBrowsingActivationThrottle::SBDatabaseClient(
+ std::move(database_manager),
+ AsWeakPtr(),
+ base::ThreadTaskRunnerHandle::Get()),
+ base::OnTaskRunnerDeleter(callback_task_runner_)) {}
+
+SubresourceFilterSafeBrowsingActivationThrottle::
+ ~SubresourceFilterSafeBrowsingActivationThrottle() {}
+
+content::NavigationThrottle::ThrottleCheckResult
+SubresourceFilterSafeBrowsingActivationThrottle::WillProcessResponse() {
+ callback_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle::
+ SBDatabaseClient::CheckUrlOnIO,
+ base::Unretained(database_client_.get()),
+ navigation_handle()->GetURL()));
+ return content::NavigationThrottle::ThrottleCheckResult::DEFER;
+}
+
+void SubresourceFilterSafeBrowsingActivationThrottle::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();
engedy 2017/03/21 13:44:16 Please add TODO and file a bug on adding histogram
melandory 2017/03/24 15:49:28 Done.
+}
+
+} // namespace subresource_filter

Powered by Google App Engine
This is Rietveld 408576698