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

Side by Side Diff: components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle.cc

Issue 2834543003: [subresource_filter] SB throttle can send multiple speculative requests. (Closed)
Patch Set: fix constexpr declaration Created 3 years, 8 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
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/subresource_filter/content/browser/subresource_filter_safe_ browsing_activation_throttle.h" 5 #include "components/subresource_filter/content/browser/subresource_filter_safe_ browsing_activation_throttle.h"
6 6
7 #include <utility>
7 #include <vector> 8 #include <vector>
8 9
10 #include "base/metrics/histogram_macros.h"
9 #include "base/timer/timer.h" 11 #include "base/timer/timer.h"
10 #include "components/safe_browsing_db/v4_local_database_manager.h"
11 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" 12 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
13 #include "components/subresource_filter/content/browser/subresource_filter_safe_ browsing_client.h"
12 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/navigation_handle.h" 15 #include "content/public/browser/navigation_handle.h"
14 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
15 17
16 namespace {
17
18 // Maximum time in milliseconds to wait for the Safe Browsing service to
19 // verify a URL. After this amount of time the outstanding check will be
20 // aborted, and the URL will be treated as if it didn't belong to the
21 // Subresource Filter only list.
22 constexpr base::TimeDelta kCheckURLTimeout = base::TimeDelta::FromSeconds(5);
23
24 } // namespace
25
26 namespace subresource_filter { 18 namespace subresource_filter {
27 19
28 class SubresourceFilterSafeBrowsingActivationThrottle::SBDatabaseClient
29 : public safe_browsing::SafeBrowsingDatabaseManager::Client {
30 public:
31 SBDatabaseClient(
32 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
33 database_manager,
34 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle,
35 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
36 : database_manager_(std::move(database_manager)),
37 throttle_(throttle),
38 io_task_runner_(io_task_runner) {}
39
40 ~SBDatabaseClient() override {
41 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
42 database_manager_->CancelCheck(this);
43 }
44
45 void CheckUrlOnIO(const GURL& url) {
46 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
47 DCHECK(!url.is_empty());
48 url_being_checked_ = url;
49 if (database_manager_->CheckUrlForSubresourceFilter(url, this)) {
50 OnCheckBrowseUrlResult(url, safe_browsing::SB_THREAT_TYPE_SAFE,
51 safe_browsing::ThreatMetadata());
52 return;
53 }
54 timer_.Start(FROM_HERE, kCheckURLTimeout, this,
55 &SubresourceFilterSafeBrowsingActivationThrottle::
56 SBDatabaseClient::OnCheckUrlTimeout);
57 }
58
59 void OnCheckBrowseUrlResult(
60 const GURL& url,
61 safe_browsing::SBThreatType threat_type,
62 const safe_browsing::ThreatMetadata& metadata) override {
63 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
64 DCHECK_EQ(url_being_checked_, url);
65 timer_.Stop(); // Cancel the timeout timer.
66 io_task_runner_->PostTask(
67 FROM_HERE,
68 base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle::
69 OnCheckUrlResultOnUI,
70 throttle_, url, threat_type, metadata.threat_pattern_type));
71 }
72
73 // Callback for when the safe browsing check has taken longer than
74 // kCheckURLTimeout.
75 void OnCheckUrlTimeout() {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
77 database_manager_->CancelCheck(this);
78
79 OnCheckBrowseUrlResult(url_being_checked_,
80 safe_browsing::SB_THREAT_TYPE_SAFE,
81 safe_browsing::ThreatMetadata());
82 }
83
84 private:
85 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_;
86
87 // Timer to abort the safe browsing check if it takes too long.
88 base::OneShotTimer timer_;
89 GURL url_being_checked_;
90
91 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle_;
92 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
93
94 DISALLOW_COPY_AND_ASSIGN(SBDatabaseClient);
95 };
96
97 SubresourceFilterSafeBrowsingActivationThrottle:: 20 SubresourceFilterSafeBrowsingActivationThrottle::
98 SubresourceFilterSafeBrowsingActivationThrottle( 21 SubresourceFilterSafeBrowsingActivationThrottle(
99 content::NavigationHandle* handle, 22 content::NavigationHandle* handle,
23 std::unique_ptr<base::Timer> timer,
100 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> 24 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
101 database_manager) 25 database_manager)
102 : NavigationThrottle(handle), 26 : NavigationThrottle(handle),
27 database_manager_(std::move(database_manager)),
103 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( 28 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread(
104 content::BrowserThread::IO)), 29 content::BrowserThread::IO)),
105 database_client_( 30 database_client_(new SubresourceFilterSafeBrowsingClient(
106 new SubresourceFilterSafeBrowsingActivationThrottle::SBDatabaseClient( 31 std::move(timer),
107 std::move(database_manager), 32 database_manager_.get(),
108 AsWeakPtr(), 33 AsWeakPtr(),
109 base::ThreadTaskRunnerHandle::Get()), 34 base::ThreadTaskRunnerHandle::Get()),
110 base::OnTaskRunnerDeleter(io_task_runner_)) {} 35 base::OnTaskRunnerDeleter(io_task_runner_)) {}
111 36
112 SubresourceFilterSafeBrowsingActivationThrottle:: 37 SubresourceFilterSafeBrowsingActivationThrottle::
113 ~SubresourceFilterSafeBrowsingActivationThrottle() {} 38 ~SubresourceFilterSafeBrowsingActivationThrottle() {}
114 39
115 content::NavigationThrottle::ThrottleCheckResult 40 content::NavigationThrottle::ThrottleCheckResult
41 SubresourceFilterSafeBrowsingActivationThrottle::WillStartRequest() {
42 CheckUrl();
43 return content::NavigationThrottle::ThrottleCheckResult::PROCEED;
44 }
45
46 content::NavigationThrottle::ThrottleCheckResult
47 SubresourceFilterSafeBrowsingActivationThrottle::WillRedirectRequest() {
48 CheckUrl();
49 return content::NavigationThrottle::ThrottleCheckResult::PROCEED;
50 }
51
52 content::NavigationThrottle::ThrottleCheckResult
116 SubresourceFilterSafeBrowsingActivationThrottle::WillProcessResponse() { 53 SubresourceFilterSafeBrowsingActivationThrottle::WillProcessResponse() {
117 io_task_runner_->PostTask( 54 // No need to defer the navigation if the check already happened.
118 FROM_HERE, base::Bind(&SubresourceFilterSafeBrowsingActivationThrottle:: 55 if (last_received_request_id_ == current_request_id_) {
119 SBDatabaseClient::CheckUrlOnIO, 56 NotifyResult();
120 base::Unretained(database_client_.get()), 57 return content::NavigationThrottle::ThrottleCheckResult::PROCEED;
121 navigation_handle()->GetURL())); 58 }
59 defer_time_ = base::TimeTicks::Now();
122 return content::NavigationThrottle::ThrottleCheckResult::DEFER; 60 return content::NavigationThrottle::ThrottleCheckResult::DEFER;
123 } 61 }
124 62
125 void SubresourceFilterSafeBrowsingActivationThrottle::OnCheckUrlResultOnUI( 63 void SubresourceFilterSafeBrowsingActivationThrottle::OnCheckUrlResultOnUI(
126 const GURL& url, 64 const GURL& url,
65 int request_id,
127 safe_browsing::SBThreatType threat_type, 66 safe_browsing::SBThreatType threat_type,
128 safe_browsing::ThreatPatternType pattern_type) { 67 safe_browsing::ThreatPatternType pattern_type) {
68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
69 // Ignore results from previous requests if there is a more recent one in
70 // flight by the time this task is run.
71 if (current_request_id_ != request_id)
engedy 2017/04/20 18:56:23 Have you considered content based addressing for t
engedy 2017/04/20 18:58:17 requests, terms
Charlie Harrison 2017/04/24 18:25:10 The current PS keeps a list of check results, so I
72 return;
73 last_received_request_id_ = request_id;
74
75 DCHECK_EQ(navigation_handle()->GetURL(), url);
76 threat_type_ = threat_type;
77 pattern_type_ = pattern_type;
78
79 // If the throttle has deferred the navigation, there are no more redirects
80 // and this is the final URL.
81 if (!defer_time_.is_null()) {
82 NotifyResult();
83 navigation_handle()->Resume();
84 }
85 }
86
87 void SubresourceFilterSafeBrowsingActivationThrottle::CheckUrl() {
88 io_task_runner_->PostTask(
89 FROM_HERE,
90 base::Bind(&SubresourceFilterSafeBrowsingClient::CheckUrlOnIO,
91 base::Unretained(database_client_.get()),
92 navigation_handle()->GetURL(), ++current_request_id_));
93 }
94
95 void SubresourceFilterSafeBrowsingActivationThrottle::NotifyResult() {
129 content::WebContents* web_contents = navigation_handle()->GetWebContents(); 96 content::WebContents* web_contents = navigation_handle()->GetWebContents();
130 if (web_contents) { 97 if (!web_contents)
131 using subresource_filter::ContentSubresourceFilterDriverFactory; 98 return;
132 ContentSubresourceFilterDriverFactory* driver_factory = 99 using subresource_filter::ContentSubresourceFilterDriverFactory;
133 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); 100 ContentSubresourceFilterDriverFactory* driver_factory =
134 DCHECK(driver_factory); 101 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents);
102 DCHECK(driver_factory);
135 103
136 driver_factory->OnMainResourceMatchedSafeBrowsingBlacklist( 104 driver_factory->OnMainResourceMatchedSafeBrowsingBlacklist(
137 url, std::vector<GURL>(), threat_type, pattern_type); 105 navigation_handle()->GetURL(), std::vector<GURL>(), threat_type_,
138 } 106 pattern_type_);
139 // TODO(https://crbug.com/704508): We should measure the delay introduces by 107
140 // this check. Similarly, as it's done the Safe Browsing Resource throttle. 108 base::TimeDelta delay = defer_time_.is_null()
141 navigation_handle()->Resume(); 109 ? base::TimeDelta::FromMilliseconds(0)
110 : base::TimeTicks::Now() - defer_time_;
111 UMA_HISTOGRAM_TIMES("SubresourceFilter.SafeBrowsing.NavigationDelay", delay);
engedy 2017/04/20 18:56:23 While I do see an argument being made for this nam
Charlie Harrison 2017/04/24 18:25:10 Done and done.
142 } 112 }
143 113
144 } // namespace subresource_filter 114 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698