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

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_url_checker_impl.cc

Issue 2924723002: Network service: SafeBrowsing check for frame-resources from browser. (Closed)
Patch Set: . Created 3 years, 6 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
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_url_checker_impl.h ('k') | chrome/renderer/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/safe_browsing/safe_browsing_url_checker_impl.h"
6
7 #include "chrome/browser/prerender/prerender_contents.h"
8 #include "chrome/browser/safe_browsing/ui_manager.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/web_contents.h"
11 #include "net/base/load_flags.h"
12
13 namespace safe_browsing {
14 namespace {
15
16 // TODO(yzshen): Share such value with safe_browsing::BaseResourceThrottle.
17 // Maximum time in milliseconds to wait for the SafeBrowsing service reputation
18 // check. After this amount of time the outstanding check will be aborted, and
19 // the resource will be treated as if it were safe.
20 const int kCheckUrlTimeoutMs = 5000;
21
22 } // namespace
23
24 SafeBrowsingUrlCheckerImpl::SafeBrowsingUrlCheckerImpl(
25 int load_flags,
26 content::ResourceType resource_type,
27 scoped_refptr<SafeBrowsingDatabaseManager> database_manager,
28 scoped_refptr<SafeBrowsingUIManager> ui_manager,
29 const base::Callback<content::WebContents*()>& web_contents_getter)
30 : load_flags_(load_flags),
31 resource_type_(resource_type),
32 web_contents_getter_(web_contents_getter),
33 database_manager_(std::move(database_manager)),
34 ui_manager_(std::move(ui_manager)),
35 weak_factory_(this) {}
36
37 SafeBrowsingUrlCheckerImpl::~SafeBrowsingUrlCheckerImpl() {
38 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
39
40 if (state_ == STATE_CHECKING_URL)
41 database_manager_->CancelCheck(this);
42 }
43
44 void SafeBrowsingUrlCheckerImpl::CheckUrl(const GURL& url,
45 CheckUrlCallback callback) {
46 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
47
48 DVLOG(1) << "SafeBrowsingUrlCheckerImpl checks URL: " << url;
49 urls_.push_back(url);
50 callbacks_.push_back(std::move(callback));
51
52 ProcessUrls();
53 }
54
55 void SafeBrowsingUrlCheckerImpl::OnCheckBrowseUrlResult(
56 const GURL& url,
57 SBThreatType threat_type,
58 const ThreatMetadata& metadata) {
59 DCHECK_EQ(STATE_CHECKING_URL, state_);
60 DCHECK_LT(next_index_, urls_.size());
61 DCHECK_EQ(urls_[next_index_], url);
62
63 timer_.Stop();
64 if (threat_type == SB_THREAT_TYPE_SAFE) {
65 state_ = STATE_NONE;
66 std::move(callbacks_[next_index_]).Run(true);
67 next_index_++;
68 ProcessUrls();
69 return;
70 }
71
72 if (load_flags_ & net::LOAD_PREFETCH) {
73 // TODO(yzshen): Destroy prerender contents if necessary.
74
75 BlockAndProcessUrls();
76 return;
77 }
78
79 security_interstitials::UnsafeResource resource;
80 resource.url = url;
81 resource.original_url = urls_[0];
82 if (urls_.size() > 1)
83 resource.redirect_urls = std::vector<GURL>(urls_.begin() + 1, urls_.end());
84 resource.is_subresource = resource_type_ != content::RESOURCE_TYPE_MAIN_FRAME;
85 resource.is_subframe = resource_type_ == content::RESOURCE_TYPE_SUB_FRAME;
86 resource.threat_type = threat_type;
87 resource.threat_metadata = metadata;
88 resource.callback =
89 base::Bind(&SafeBrowsingUrlCheckerImpl::OnBlockingPageComplete,
90 weak_factory_.GetWeakPtr());
91 resource.callback_thread = content::BrowserThread::GetTaskRunnerForThread(
92 content::BrowserThread::IO);
93 resource.web_contents_getter = web_contents_getter_;
94 resource.threat_source = database_manager_->GetThreatSource();
95
96 state_ = STATE_DISPLAYING_BLOCKING_PAGE;
97
98 content::BrowserThread::PostTask(
99 content::BrowserThread::UI, FROM_HERE,
100 base::Bind(&SafeBrowsingUrlCheckerImpl::StartDisplayingBlockingPage,
101 weak_factory_.GetWeakPtr(), ui_manager_, resource));
102 }
103
104 // static
105 void SafeBrowsingUrlCheckerImpl::StartDisplayingBlockingPage(
106 const base::WeakPtr<SafeBrowsingUrlCheckerImpl>& checker,
107 scoped_refptr<BaseUIManager> ui_manager,
108 const security_interstitials::UnsafeResource& resource) {
109 content::WebContents* web_contents = resource.web_contents_getter.Run();
110 if (web_contents) {
111 prerender::PrerenderContents* prerender_contents =
112 prerender::PrerenderContents::FromWebContents(web_contents);
113 if (prerender_contents) {
114 prerender_contents->Destroy(prerender::FINAL_STATUS_SAFE_BROWSING);
115 } else {
116 ui_manager->DisplayBlockingPage(resource);
117 return;
118 }
119 }
120
121 // Tab is gone or it's being prerendered.
122 content::BrowserThread::PostTask(
123 content::BrowserThread::IO, FROM_HERE,
124 base::BindOnce(&SafeBrowsingUrlCheckerImpl::BlockAndProcessUrls,
125 checker));
126 }
127
128 void SafeBrowsingUrlCheckerImpl::OnCheckUrlTimeout() {
129 database_manager_->CancelCheck(this);
130
131 OnCheckBrowseUrlResult(urls_[next_index_], safe_browsing::SB_THREAT_TYPE_SAFE,
132 ThreatMetadata());
133 }
134
135 void SafeBrowsingUrlCheckerImpl::ProcessUrls() {
136 DCHECK_NE(STATE_BLOCKED, state_);
137
138 if (state_ == STATE_CHECKING_URL ||
139 state_ == STATE_DISPLAYING_BLOCKING_PAGE) {
140 return;
141 }
142
143 while (next_index_ < urls_.size()) {
144 DCHECK_EQ(STATE_NONE, state_);
145 // TODO(yzshen): Consider moving CanCheckResourceType() to the renderer
146 // side. That would save some IPCs. It requires a method on the
147 // SafeBrowsing mojo interface to query all supported resource types.
148 if (!database_manager_->CanCheckResourceType(resource_type_) ||
149 database_manager_->CheckBrowseUrl(urls_[next_index_], this)) {
150 std::move(callbacks_[next_index_]).Run(true);
151 next_index_++;
152 continue;
153 }
154
155 state_ = STATE_CHECKING_URL;
156 // Start a timer to abort the check if it takes too long.
157 timer_.Start(FROM_HERE,
158 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), this,
159 &SafeBrowsingUrlCheckerImpl::OnCheckUrlTimeout);
160
161 break;
162 }
163 }
164
165 void SafeBrowsingUrlCheckerImpl::BlockAndProcessUrls() {
166 DVLOG(1) << "SafeBrowsingUrlCheckerImpl blocks URL: " << urls_[next_index_];
167 state_ = STATE_BLOCKED;
168
169 // If user decided to not proceed through a warning, mark all the remaining
170 // redirects as "bad".
171 for (; next_index_ < callbacks_.size(); ++next_index_)
172 std::move(callbacks_[next_index_]).Run(false);
173 }
174
175 void SafeBrowsingUrlCheckerImpl::OnBlockingPageComplete(bool proceed) {
176 DCHECK_EQ(STATE_DISPLAYING_BLOCKING_PAGE, state_);
177
178 if (proceed) {
179 state_ = STATE_NONE;
180 std::move(callbacks_[next_index_]).Run(true);
181 next_index_++;
182 ProcessUrls();
183 } else {
184 BlockAndProcessUrls();
185 }
186 }
187
188 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_url_checker_impl.h ('k') | chrome/renderer/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698