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

Side by Side Diff: chrome/browser/renderer_host/safe_browsing_resource_throttle.cc

Issue 1420053005: Move code in components/safe_browsing_db and chrome/browser/s_b/ under the safe_browsing namespace. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@02_components_move
Patch Set: Merging the latest changes from trunk. Created 5 years, 1 month 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/renderer_host/safe_browsing_resource_throttle.h" 5 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 64
65 } // namespace 65 } // namespace
66 66
67 // TODO(eroman): Downgrade these CHECK()s to DCHECKs once there is more 67 // TODO(eroman): Downgrade these CHECK()s to DCHECKs once there is more
68 // unit test coverage. 68 // unit test coverage.
69 69
70 // static 70 // static
71 SafeBrowsingResourceThrottle* SafeBrowsingResourceThrottle::MaybeCreate( 71 SafeBrowsingResourceThrottle* SafeBrowsingResourceThrottle::MaybeCreate(
72 net::URLRequest* request, 72 net::URLRequest* request,
73 content::ResourceType resource_type, 73 content::ResourceType resource_type,
74 SafeBrowsingService* sb_service) { 74 safe_browsing::SafeBrowsingService* sb_service) {
75 if (sb_service->database_manager()->IsSupported()) { 75 if (sb_service->database_manager()->IsSupported()) {
76 return new SafeBrowsingResourceThrottle(request, resource_type, sb_service); 76 return new SafeBrowsingResourceThrottle(request, resource_type, sb_service);
77 } 77 }
78 return nullptr; 78 return nullptr;
79 } 79 }
80 80
81 SafeBrowsingResourceThrottle::SafeBrowsingResourceThrottle( 81 SafeBrowsingResourceThrottle::SafeBrowsingResourceThrottle(
82 const net::URLRequest* request, 82 const net::URLRequest* request,
83 content::ResourceType resource_type, 83 content::ResourceType resource_type,
84 SafeBrowsingService* sb_service) 84 safe_browsing::SafeBrowsingService* sb_service)
85 : state_(STATE_NONE), 85 : state_(STATE_NONE),
86 defer_state_(DEFERRED_NONE), 86 defer_state_(DEFERRED_NONE),
87 threat_type_(SB_THREAT_TYPE_SAFE), 87 threat_type_(safe_browsing::SB_THREAT_TYPE_SAFE),
88 database_manager_(sb_service->database_manager()), 88 database_manager_(sb_service->database_manager()),
89 ui_manager_(sb_service->ui_manager()), 89 ui_manager_(sb_service->ui_manager()),
90 request_(request), 90 request_(request),
91 resource_type_(resource_type), 91 resource_type_(resource_type),
92 bound_net_log_(net::BoundNetLog::Make(request->net_log().net_log(), 92 bound_net_log_(net::BoundNetLog::Make(request->net_log().net_log(),
93 NetLog::SOURCE_SAFE_BROWSING)) {} 93 NetLog::SOURCE_SAFE_BROWSING)) {}
94 94
95 SafeBrowsingResourceThrottle::~SafeBrowsingResourceThrottle() { 95 SafeBrowsingResourceThrottle::~SafeBrowsingResourceThrottle() {
96 if (defer_state_ != DEFERRED_NONE) { 96 if (defer_state_ != DEFERRED_NONE) {
97 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, nullptr, nullptr); 97 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, nullptr, nullptr);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 } 194 }
195 195
196 const char* SafeBrowsingResourceThrottle::GetNameForLogging() const { 196 const char* SafeBrowsingResourceThrottle::GetNameForLogging() const {
197 return "SafeBrowsingResourceThrottle"; 197 return "SafeBrowsingResourceThrottle";
198 } 198 }
199 199
200 // SafeBrowsingService::Client implementation, called on the IO thread once 200 // SafeBrowsingService::Client implementation, called on the IO thread once
201 // the URL has been classified. 201 // the URL has been classified.
202 void SafeBrowsingResourceThrottle::OnCheckBrowseUrlResult( 202 void SafeBrowsingResourceThrottle::OnCheckBrowseUrlResult(
203 const GURL& url, 203 const GURL& url,
204 SBThreatType threat_type, 204 safe_browsing::SBThreatType threat_type,
205 const std::string& metadata) { 205 const std::string& metadata) {
206 CHECK_EQ(state_, STATE_CHECKING_URL); 206 CHECK_EQ(state_, STATE_CHECKING_URL);
207 CHECK_EQ(url, url_being_checked_); 207 CHECK_EQ(url, url_being_checked_);
208 208
209 timer_.Stop(); // Cancel the timeout timer. 209 timer_.Stop(); // Cancel the timeout timer.
210 threat_type_ = threat_type; 210 threat_type_ = threat_type;
211 state_ = STATE_NONE; 211 state_ = STATE_NONE;
212 212
213 if (defer_state_ != DEFERRED_NONE) { 213 if (defer_state_ != DEFERRED_NONE) {
214 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, nullptr, nullptr); 214 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, nullptr, nullptr);
215 } 215 }
216 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_CHECKING_URL, "result", 216 EndNetLogEvent(
217 threat_type_ == SB_THREAT_TYPE_SAFE ? "safe" : "unsafe"); 217 NetLog::TYPE_SAFE_BROWSING_CHECKING_URL, "result",
218 threat_type_ == safe_browsing::SB_THREAT_TYPE_SAFE ? "safe" : "unsafe");
218 219
219 if (threat_type == SB_THREAT_TYPE_SAFE) { 220 if (threat_type == safe_browsing::SB_THREAT_TYPE_SAFE) {
220 RecordHistogramResourceTypeSafe(resource_type_); 221 RecordHistogramResourceTypeSafe(resource_type_);
221 if (defer_state_ != DEFERRED_NONE) { 222 if (defer_state_ != DEFERRED_NONE) {
222 // Log how much time the safe browsing check cost us. 223 // Log how much time the safe browsing check cost us.
223 ui_manager_->LogPauseDelay(base::TimeTicks::Now() - defer_start_time_); 224 ui_manager_->LogPauseDelay(base::TimeTicks::Now() - defer_start_time_);
224 ResumeRequest(); 225 ResumeRequest();
225 } else { 226 } else {
226 ui_manager_->LogPauseDelay(base::TimeDelta()); 227 ui_manager_->LogPauseDelay(base::TimeDelta());
227 } 228 }
228 return; 229 return;
229 } 230 }
230 231
231 if (request_->load_flags() & net::LOAD_PREFETCH) { 232 if (request_->load_flags() & net::LOAD_PREFETCH) {
232 // Don't prefetch resources that fail safe browsing, disallow them. 233 // Don't prefetch resources that fail safe browsing, disallow them.
233 controller()->Cancel(); 234 controller()->Cancel();
234 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.UnsafePrefetchCanceled", 235 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.UnsafePrefetchCanceled",
235 resource_type_, content::RESOURCE_TYPE_LAST_TYPE); 236 resource_type_, content::RESOURCE_TYPE_LAST_TYPE);
236 return; 237 return;
237 } 238 }
238 239
239 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.Unsafe", resource_type_, 240 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.Unsafe", resource_type_,
240 content::RESOURCE_TYPE_LAST_TYPE); 241 content::RESOURCE_TYPE_LAST_TYPE);
241 242
242 const content::ResourceRequestInfo* info = 243 const content::ResourceRequestInfo* info =
243 content::ResourceRequestInfo::ForRequest(request_); 244 content::ResourceRequestInfo::ForRequest(request_);
244 245
245 SafeBrowsingUIManager::UnsafeResource resource; 246 safe_browsing::SafeBrowsingUIManager::UnsafeResource resource;
246 resource.url = url; 247 resource.url = url;
247 resource.original_url = request_->original_url(); 248 resource.original_url = request_->original_url();
248 resource.redirect_urls = redirect_urls_; 249 resource.redirect_urls = redirect_urls_;
249 resource.is_subresource = resource_type_ != content::RESOURCE_TYPE_MAIN_FRAME; 250 resource.is_subresource = resource_type_ != content::RESOURCE_TYPE_MAIN_FRAME;
250 resource.is_subframe = resource_type_ == content::RESOURCE_TYPE_SUB_FRAME; 251 resource.is_subframe = resource_type_ == content::RESOURCE_TYPE_SUB_FRAME;
251 resource.threat_type = threat_type; 252 resource.threat_type = threat_type;
252 resource.threat_metadata = metadata; 253 resource.threat_metadata = metadata;
253 resource.callback = base::Bind( 254 resource.callback = base::Bind(
254 &SafeBrowsingResourceThrottle::OnBlockingPageComplete, AsWeakPtr()); 255 &SafeBrowsingResourceThrottle::OnBlockingPageComplete, AsWeakPtr());
255 resource.render_process_host_id = info->GetChildID(); 256 resource.render_process_host_id = info->GetChildID();
256 resource.render_view_id = info->GetRouteID(); 257 resource.render_view_id = info->GetRouteID();
257 resource.threat_source = SafeBrowsingUIManager::FROM_DEVICE; 258 resource.threat_source = safe_browsing::SafeBrowsingUIManager::FROM_DEVICE;
258 259
259 state_ = STATE_DISPLAYING_BLOCKING_PAGE; 260 state_ = STATE_DISPLAYING_BLOCKING_PAGE;
260 261
261 content::BrowserThread::PostTask( 262 content::BrowserThread::PostTask(
262 content::BrowserThread::UI, 263 content::BrowserThread::UI,
263 FROM_HERE, 264 FROM_HERE,
264 base::Bind(&SafeBrowsingResourceThrottle::StartDisplayingBlockingPage, 265 base::Bind(&SafeBrowsingResourceThrottle::StartDisplayingBlockingPage,
265 AsWeakPtr(), ui_manager_, resource)); 266 AsWeakPtr(), ui_manager_, resource));
266 } 267 }
267 268
268 void SafeBrowsingResourceThrottle::StartDisplayingBlockingPage( 269 void SafeBrowsingResourceThrottle::StartDisplayingBlockingPage(
269 const base::WeakPtr<SafeBrowsingResourceThrottle>& throttle, 270 const base::WeakPtr<SafeBrowsingResourceThrottle>& throttle,
270 scoped_refptr<SafeBrowsingUIManager> ui_manager, 271 scoped_refptr<safe_browsing::SafeBrowsingUIManager> ui_manager,
271 const SafeBrowsingUIManager::UnsafeResource& resource) { 272 const safe_browsing::SafeBrowsingUIManager::UnsafeResource& resource) {
272 content::RenderViewHost* rvh = content::RenderViewHost::FromID( 273 content::RenderViewHost* rvh = content::RenderViewHost::FromID(
273 resource.render_process_host_id, resource.render_view_id); 274 resource.render_process_host_id, resource.render_view_id);
274 if (rvh) { 275 if (rvh) {
275 content::WebContents* web_contents = 276 content::WebContents* web_contents =
276 content::WebContents::FromRenderViewHost(rvh); 277 content::WebContents::FromRenderViewHost(rvh);
277 prerender::PrerenderContents* prerender_contents = 278 prerender::PrerenderContents* prerender_contents =
278 prerender::PrerenderContents::FromWebContents(web_contents); 279 prerender::PrerenderContents::FromWebContents(web_contents);
279 280
280 if (prerender_contents) { 281 if (prerender_contents) {
281 prerender_contents->Destroy(prerender::FINAL_STATUS_SAFE_BROWSING); 282 prerender_contents->Destroy(prerender::FINAL_STATUS_SAFE_BROWSING);
(...skipping 15 matching lines...) Expand all
297 } 298 }
298 299
299 // SafeBrowsingService::UrlCheckCallback implementation, called on the IO 300 // SafeBrowsingService::UrlCheckCallback implementation, called on the IO
300 // thread when the user has decided to proceed with the current request, or 301 // thread when the user has decided to proceed with the current request, or
301 // go back. 302 // go back.
302 void SafeBrowsingResourceThrottle::OnBlockingPageComplete(bool proceed) { 303 void SafeBrowsingResourceThrottle::OnBlockingPageComplete(bool proceed) {
303 CHECK_EQ(state_, STATE_DISPLAYING_BLOCKING_PAGE); 304 CHECK_EQ(state_, STATE_DISPLAYING_BLOCKING_PAGE);
304 state_ = STATE_NONE; 305 state_ = STATE_NONE;
305 306
306 if (proceed) { 307 if (proceed) {
307 threat_type_ = SB_THREAT_TYPE_SAFE; 308 threat_type_ = safe_browsing::SB_THREAT_TYPE_SAFE;
308 if (defer_state_ != DEFERRED_NONE) { 309 if (defer_state_ != DEFERRED_NONE) {
309 ResumeRequest(); 310 ResumeRequest();
310 } 311 }
311 } else { 312 } else {
312 controller()->Cancel(); 313 controller()->Cancel();
313 } 314 }
314 } 315 }
315 316
316 bool SafeBrowsingResourceThrottle::CheckUrl(const GURL& url) { 317 bool SafeBrowsingResourceThrottle::CheckUrl(const GURL& url) {
317 CHECK_EQ(state_, STATE_NONE); 318 CHECK_EQ(state_, STATE_NONE);
318 // To reduce aggregate latency on mobile, check only the most dangerous 319 // To reduce aggregate latency on mobile, check only the most dangerous
319 // resource types. 320 // resource types.
320 if (!database_manager_->CanCheckResourceType(resource_type_)) { 321 if (!database_manager_->CanCheckResourceType(resource_type_)) {
321 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.Skipped", resource_type_, 322 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.Skipped", resource_type_,
322 content::RESOURCE_TYPE_LAST_TYPE); 323 content::RESOURCE_TYPE_LAST_TYPE);
323 return true; 324 return true;
324 } 325 }
325 326
326 bool succeeded_synchronously = database_manager_->CheckBrowseUrl(url, this); 327 bool succeeded_synchronously = database_manager_->CheckBrowseUrl(url, this);
327 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.Checked", resource_type_, 328 UMA_HISTOGRAM_ENUMERATION("SB2.ResourceTypes.Checked", resource_type_,
328 content::RESOURCE_TYPE_LAST_TYPE); 329 content::RESOURCE_TYPE_LAST_TYPE);
329 330
330 if (succeeded_synchronously) { 331 if (succeeded_synchronously) {
331 RecordHistogramResourceTypeSafe(resource_type_); 332 RecordHistogramResourceTypeSafe(resource_type_);
332 threat_type_ = SB_THREAT_TYPE_SAFE; 333 threat_type_ = safe_browsing::SB_THREAT_TYPE_SAFE;
333 ui_manager_->LogPauseDelay(base::TimeDelta()); // No delay. 334 ui_manager_->LogPauseDelay(base::TimeDelta()); // No delay.
334 return true; 335 return true;
335 } 336 }
336 337
337 state_ = STATE_CHECKING_URL; 338 state_ = STATE_CHECKING_URL;
338 url_being_checked_ = url; 339 url_being_checked_ = url;
339 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_CHECKING_URL, url, nullptr, 340 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_CHECKING_URL, url, nullptr,
340 nullptr); 341 nullptr);
341 342
342 // Start a timer to abort the check if it takes too long. 343 // Start a timer to abort the check if it takes too long.
343 // TODO(nparker): Set this only when we defer, based on remaining time, 344 // TODO(nparker): Set this only when we defer, based on remaining time,
344 // so we don't cancel earlier than necessary. 345 // so we don't cancel earlier than necessary.
345 timer_.Start(FROM_HERE, 346 timer_.Start(FROM_HERE,
346 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), 347 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs),
347 this, &SafeBrowsingResourceThrottle::OnCheckUrlTimeout); 348 this, &SafeBrowsingResourceThrottle::OnCheckUrlTimeout);
348 349
349 return false; 350 return false;
350 } 351 }
351 352
352 void SafeBrowsingResourceThrottle::OnCheckUrlTimeout() { 353 void SafeBrowsingResourceThrottle::OnCheckUrlTimeout() {
353 CHECK_EQ(state_, STATE_CHECKING_URL); 354 CHECK_EQ(state_, STATE_CHECKING_URL);
354 355
355 database_manager_->CancelCheck(this); 356 database_manager_->CancelCheck(this);
356 OnCheckBrowseUrlResult( 357 OnCheckBrowseUrlResult(
357 url_being_checked_, SB_THREAT_TYPE_SAFE, std::string()); 358 url_being_checked_, safe_browsing::SB_THREAT_TYPE_SAFE, std::string());
358 } 359 }
359 360
360 void SafeBrowsingResourceThrottle::ResumeRequest() { 361 void SafeBrowsingResourceThrottle::ResumeRequest() {
361 CHECK_EQ(state_, STATE_NONE); 362 CHECK_EQ(state_, STATE_NONE);
362 CHECK_NE(defer_state_, DEFERRED_NONE); 363 CHECK_NE(defer_state_, DEFERRED_NONE);
363 364
364 bool resume = true; 365 bool resume = true;
365 if (defer_state_ == DEFERRED_UNCHECKED_REDIRECT) { 366 if (defer_state_ == DEFERRED_UNCHECKED_REDIRECT) {
366 // Save the redirect urls for possible malware detail reporting later. 367 // Save the redirect urls for possible malware detail reporting later.
367 redirect_urls_.push_back(unchecked_redirect_url_); 368 redirect_urls_.push_back(unchecked_redirect_url_);
368 if (!CheckUrl(unchecked_redirect_url_)) { 369 if (!CheckUrl(unchecked_redirect_url_)) {
369 // We're now waiting for the unchecked_redirect_url_. 370 // We're now waiting for the unchecked_redirect_url_.
370 defer_state_ = DEFERRED_REDIRECT; 371 defer_state_ = DEFERRED_REDIRECT;
371 resume = false; 372 resume = false;
372 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, 373 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED,
373 unchecked_redirect_url_, "defer_reason", 374 unchecked_redirect_url_, "defer_reason",
374 "resumed_redirect"); 375 "resumed_redirect");
375 } 376 }
376 } 377 }
377 378
378 if (resume) { 379 if (resume) {
379 defer_state_ = DEFERRED_NONE; 380 defer_state_ = DEFERRED_NONE;
380 controller()->Resume(); 381 controller()->Resume();
381 } 382 }
382 } 383 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698