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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 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 "components/subresource_filter/content/browser/subresource_filter_safe_ browsing_activation_throttle.h"
6
7 #include "base/timer/timer.h"
8 #include "components/safe_browsing_db/v4_local_database_manager.h"
9 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/navigation_handle.h"
12 #include "content/public/browser/web_contents.h"
13
14 namespace {
15
16 // 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.
17 // verify a URL. After this amount of time the outstanding check will be
18 // 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.
19 // Subresource Filter only list.
20 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.
21
22 } // namespace
23
24 namespace subresource_filter {
25
26 class SubresourceFilterSafeBrowsingActivationThrottle::SBDatabaseClient
27 : public safe_browsing::SafeBrowsingDatabaseManager::Client {
28 public:
29 SBDatabaseClient(
30 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
31 database_manager,
32 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle,
33 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner)
34 : database_manager_(std::move(database_manager)),
35 throttle_(throttle),
36 callback_task_runner_(callback_task_runner) {}
37
38 ~SBDatabaseClient() override {
39 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
40 database_manager_->CancelCheck(this);
41 }
42
43 void CheckUrlOnIO(const GURL& url) {
44 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
45 if (database_manager_->CheckUrlForSubresourceFilter(url, this)) {
46 url_being_checked_ = url;
47 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.
48 OnCheckBrowseUrlResult(url, safe_browsing::SB_THREAT_TYPE_SAFE,
49 safe_browsing::ThreatMetadata());
50 return;
51 }
52 timer_.Start(FROM_HERE,
53 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), this,
54 &SubresourceFilterSafeBrowsingActivationThrottle::
55 SBDatabaseClient::OnCheckUrlTimeout);
56 }
57
58 void OnCheckBrowseUrlResult(
59 const GURL& url,
60 safe_browsing::SBThreatType threat_type,
61 const safe_browsing::ThreatMetadata& metadata) override {
62 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.
63 timer_.Stop(); // Cancel the timeout timer.
64 callback_task_runner_->PostTask(
65 FROM_HERE,
66 base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle::
67 OnCheckUrlResultOnUI,
68 throttle_, url, threat_type, metadata.threat_pattern_type));
69 }
70
71 // Callback for when the safe browsing check has taken longer than
72 // kCheckUrlTimeoutMs.
73 void OnCheckUrlTimeout() {
74 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
75 database_manager_->CancelCheck(this);
76
77 OnCheckBrowseUrlResult(url_being_checked_,
78 safe_browsing::SB_THREAT_TYPE_SAFE,
79 safe_browsing::ThreatMetadata());
80 }
81
82 private:
83 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_;
84
85 // Timer to abort the safe browsing check if it takes too long.
86 base::OneShotTimer timer_;
87 GURL url_being_checked_;
88 base::ThreadChecker thread_checker_;
engedy 2017/03/21 13:44:16 nit: No longer used.
melandory 2017/03/24 15:49:28 Done.
89
90 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle_;
91 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_;
92
93 DISALLOW_COPY_AND_ASSIGN(SBDatabaseClient);
94 };
95
96 SubresourceFilterSafeBrowsingActivationThrottle::
97 SubresourceFilterSafeBrowsingActivationThrottle(
98 content::NavigationHandle* handle,
99 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
100 database_manager)
101 : NavigationThrottle(handle),
102 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.
103 content::BrowserThread::IO)),
104 database_client_(
105 new SubresourceFilterSafeBrowsingActivationThrottle::SBDatabaseClient(
106 std::move(database_manager),
107 AsWeakPtr(),
108 base::ThreadTaskRunnerHandle::Get()),
109 base::OnTaskRunnerDeleter(callback_task_runner_)) {}
110
111 SubresourceFilterSafeBrowsingActivationThrottle::
112 ~SubresourceFilterSafeBrowsingActivationThrottle() {}
113
114 content::NavigationThrottle::ThrottleCheckResult
115 SubresourceFilterSafeBrowsingActivationThrottle::WillProcessResponse() {
116 callback_task_runner_->PostTask(
117 FROM_HERE, base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle::
118 SBDatabaseClient::CheckUrlOnIO,
119 base::Unretained(database_client_.get()),
120 navigation_handle()->GetURL()));
121 return content::NavigationThrottle::ThrottleCheckResult::DEFER;
122 }
123
124 void SubresourceFilterSafeBrowsingActivationThrottle::OnCheckUrlResultOnUI(
125 const GURL& url,
126 safe_browsing::SBThreatType threat_type,
127 safe_browsing::ThreatPatternType pattern_type) {
128 content::WebContents* web_contents = navigation_handle()->GetWebContents();
129 if (web_contents) {
130 using subresource_filter::ContentSubresourceFilterDriverFactory;
131 ContentSubresourceFilterDriverFactory* driver_factory =
132 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents);
133 DCHECK(driver_factory);
134
135 driver_factory->OnMainResourceMatchedSafeBrowsingBlacklist(
136 url, std::vector<GURL>(), threat_type, pattern_type);
137 }
138 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.
139 }
140
141 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698