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

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: fix HistoryNavTest 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/memory/ptr_util.h"
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
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)
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());
43 }
44
45 void CheckUrlOnIO(const GURL& url) {
46 DCHECK(thread_checker_.CalledOnValidThread());
47 if (database_manager_->CheckUrlForSubresourceFilter(url, this)) {
48 url_being_checked_ = url;
49 task_runner_->PostTask(
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.
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_ = base::MakeUnique<
109 SubresourceFilterSafeBrowsingActivationThrottle::ActivationClient>(
110 database_manager, AsWeakPtr(), base::ThreadTaskRunnerHandle::Get());
111 }
112
113 SubresourceFilterSafeBrowsingActivationThrottle::
114 ~SubresourceFilterSafeBrowsingActivationThrottle() {
115 task_runner_->DeleteSoon(FROM_HERE, activation_client_.release());
engedy 2017/03/02 12:00:43 nit: You get this for free with std::unique_ptr<T,
melandory 2017/03/06 15:00:00 Done.
116 }
117
118 content::NavigationThrottle::ThrottleCheckResult
119 SubresourceFilterSafeBrowsingActivationThrottle::WillProcessResponse() {
120 task_runner_->PostTask(
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698