OLD | NEW |
---|---|
(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/memory/ptr_util.h" | |
engedy
2017/03/10 14:39:32
nit: I think this is not used.
melandory
2017/03/15 13:41:36
Done.
| |
8 #include "base/timer/timer.h" | |
9 #include "components/safe_browsing_db/v4_local_database_manager.h" | |
10 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "content/public/browser/navigation_handle.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 | |
15 namespace { | |
16 | |
17 // Maximum time in milliseconds to wait for the safe browsing service to | |
18 // verify a URL. After this amount of time the outstanding check will be | |
19 // aborted, and the URL will be treated as if it doesn't belong to the | |
20 // Subresource Filter only list. | |
21 const int kCheckUrlTimeoutMs = 5000; | |
22 | |
23 } // namespace | |
24 | |
25 namespace subresource_filter { | |
26 | |
27 class SubresourceFilterSafeBrowsingActivationThrottle::ActivationClient | |
engedy
2017/03/10 14:39:32
nit: Given this is the client of the SB database,
melandory
2017/03/15 13:41:36
Done.
| |
28 : public safe_browsing::SafeBrowsingDatabaseManager::Client { | |
29 public: | |
30 ActivationClient( | |
31 const scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>& | |
32 database_manager, | |
33 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle, | |
34 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
engedy
2017/03/10 14:39:32
nit: s//callback_task_runner/g
Alternatively: use
melandory
2017/03/15 13:41:36
Done.
| |
35 : database_manager_(database_manager), | |
36 throttle_(throttle), | |
37 task_runner_(task_runner) { | |
38 thread_checker_.DetachFromThread(); | |
39 } | |
40 | |
41 ~ActivationClient() override { | |
42 DCHECK(thread_checker_.CalledOnValidThread()); | |
engedy
2017/03/10 14:39:32
Given that this class not only needs to be called
engedy
2017/03/10 14:39:32
Need to call CancelCheck here as well in case we g
melandory
2017/03/15 13:41:36
Done.
melandory
2017/03/15 13:41:36
Done.
| |
43 } | |
44 | |
45 void CheckUrlOnIO(const GURL& url) { | |
46 DCHECK(thread_checker_.CalledOnValidThread()); | |
engedy
2017/03/10 14:39:32
nit: Add DCHECK(url_being_checked.is_empty()); to
melandory
2017/03/15 13:41:36
Done.
| |
47 if (database_manager_->CheckUrlForSubresourceFilter(url, this)) { | |
engedy
2017/03/10 14:39:32
The comment above this method is a little unclear,
melandory
2017/03/15 13:41:36
Yep. It actually should be other way around. Unit
| |
48 url_being_checked_ = url; | |
49 task_runner_->PostTask( | |
engedy
2017/03/10 14:39:32
nit: Could we just call into OnCheckBrowserUrlResu
melandory
2017/03/15 13:41:36
Done.
| |
50 FROM_HERE, | |
51 base::Bind(&subresource_filter:: | |
52 SubresourceFilterSafeBrowsingActivationThrottle:: | |
53 OnCheckUrlResultOnUI, | |
54 throttle_, url, safe_browsing::SB_THREAT_TYPE_SAFE, | |
55 safe_browsing::ThreatPatternType::NONE)); | |
56 timer_.Start(FROM_HERE, | |
57 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), this, | |
58 &SubresourceFilterSafeBrowsingActivationThrottle:: | |
59 ActivationClient::OnCheckUrlTimeout); | |
60 } | |
61 } | |
62 | |
63 void OnCheckBrowseUrlResult( | |
64 const GURL& url, | |
65 safe_browsing::SBThreatType threat_type, | |
66 const safe_browsing::ThreatMetadata& metadata) override { | |
67 DCHECK(thread_checker_.CalledOnValidThread()); | |
68 timer_.Stop(); // Cancel the timeout timer. | |
69 task_runner_->PostTask( | |
70 FROM_HERE, | |
71 base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle:: | |
72 OnCheckUrlResultOnUI, | |
73 throttle_, url, threat_type, metadata.threat_pattern_type)); | |
74 } | |
75 | |
76 // Callback for when the safe browsing check has taken longer than | |
77 // kCheckUrlTimeoutMs. | |
78 void OnCheckUrlTimeout() { | |
79 DCHECK(thread_checker_.CalledOnValidThread()); | |
80 database_manager_->CancelCheck(this); | |
81 | |
82 OnCheckBrowseUrlResult(url_being_checked_, | |
83 safe_browsing::SB_THREAT_TYPE_SAFE, | |
84 safe_browsing::ThreatMetadata()); | |
85 } | |
86 | |
87 private: | |
88 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_; | |
89 // Timer to abort the safe browsing check if it takes too long. | |
engedy
2017/03/10 14:39:32
nit: blank line above comment
melandory
2017/03/15 13:41:36
Done.
| |
90 base::OneShotTimer timer_; | |
91 GURL url_being_checked_; | |
92 base::ThreadChecker thread_checker_; | |
93 | |
94 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle_; | |
95 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ActivationClient); | |
98 }; | |
99 | |
100 SubresourceFilterSafeBrowsingActivationThrottle:: | |
101 SubresourceFilterSafeBrowsingActivationThrottle( | |
102 content::NavigationHandle* handle, | |
103 const scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>& | |
104 database_manager) | |
105 : NavigationThrottle(handle), | |
106 task_runner_(content::BrowserThread::GetTaskRunnerForThread( | |
107 content::BrowserThread::IO)), | |
108 activation_client_( | |
109 new SubresourceFilterSafeBrowsingActivationThrottle::ActivationClient( | |
110 database_manager, | |
111 AsWeakPtr(), | |
112 base::ThreadTaskRunnerHandle::Get()), | |
113 base::OnTaskRunnerDeleter(task_runner_)) {} | |
114 | |
115 SubresourceFilterSafeBrowsingActivationThrottle:: | |
116 ~SubresourceFilterSafeBrowsingActivationThrottle() {} | |
117 | |
118 content::NavigationThrottle::ThrottleCheckResult | |
119 SubresourceFilterSafeBrowsingActivationThrottle::WillProcessResponse() { | |
120 task_runner_->PostTask( | |
engedy
2017/03/10 14:39:32
nit: Could post directly to content::BrowserThread
melandory
2017/03/15 13:41:36
I like to keep task_runner if you don't have stron
| |
121 FROM_HERE, base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle:: | |
122 ActivationClient::CheckUrlOnIO, | |
123 base::Unretained(activation_client_.get()), | |
124 navigation_handle()->GetURL())); | |
125 return content::NavigationThrottle::ThrottleCheckResult::DEFER; | |
126 } | |
127 | |
128 void SubresourceFilterSafeBrowsingActivationThrottle::OnCheckUrlResultOnUI( | |
129 const GURL& url, | |
130 safe_browsing::SBThreatType threat_type, | |
131 safe_browsing::ThreatPatternType pattern_type) { | |
132 content::WebContents* web_contents = navigation_handle()->GetWebContents(); | |
133 if (web_contents) { | |
134 using subresource_filter::ContentSubresourceFilterDriverFactory; | |
135 ContentSubresourceFilterDriverFactory* driver_factory = | |
136 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); | |
137 DCHECK(driver_factory); | |
138 | |
139 driver_factory->OnMainResourceMatchedSafeBrowsingBlacklist( | |
140 url, std::vector<GURL>(), threat_type, pattern_type); | |
141 } | |
142 navigation_handle()->Resume(); | |
143 } | |
144 | |
145 } // namespace subresource_filter | |
OLD | NEW |