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

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

Issue 8548003: Some easy NewRunnableMethod conversions in safe_browsing. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/safe_browsing/safe_browsing_service.h" 5 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
6 6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
7 #include "base/callback.h" 9 #include "base/callback.h"
8 #include "base/command_line.h" 10 #include "base/command_line.h"
9 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
10 #include "base/path_service.h" 12 #include "base/path_service.h"
11 #include "base/stl_util.h" 13 #include "base/stl_util.h"
12 #include "base/string_util.h" 14 #include "base/string_util.h"
13 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
14 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/metrics/metrics_service.h" 17 #include "chrome/browser/metrics/metrics_service.h"
16 #include "chrome/browser/prefs/pref_change_registrar.h" 18 #include "chrome/browser/prefs/pref_change_registrar.h"
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 } 121 }
120 122
121 SafeBrowsingService::UnsafeResource::~UnsafeResource() {} 123 SafeBrowsingService::UnsafeResource::~UnsafeResource() {}
122 124
123 SafeBrowsingService::SafeBrowsingCheck::SafeBrowsingCheck() 125 SafeBrowsingService::SafeBrowsingCheck::SafeBrowsingCheck()
124 : full_hash(NULL), 126 : full_hash(NULL),
125 client(NULL), 127 client(NULL),
126 need_get_hash(false), 128 need_get_hash(false),
127 result(SAFE), 129 result(SAFE),
128 is_download(false), 130 is_download(false),
129 timeout_task(NULL) { 131 cancel_timeout(NULL) {
130 } 132 }
131 133
132 SafeBrowsingService::SafeBrowsingCheck::~SafeBrowsingCheck() {} 134 SafeBrowsingService::SafeBrowsingCheck::~SafeBrowsingCheck() {}
133 135
134 void SafeBrowsingService::Client::OnSafeBrowsingResult( 136 void SafeBrowsingService::Client::OnSafeBrowsingResult(
135 const SafeBrowsingCheck& check) { 137 const SafeBrowsingCheck& check) {
136 if (!check.urls.empty()) { 138 if (!check.urls.empty()) {
137 139
138 DCHECK(!check.full_hash.get()); 140 DCHECK(!check.full_hash.get());
139 if (!check.is_download) { 141 if (!check.is_download) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 if (!enabled_ || !enable_download_protection_) 249 if (!enabled_ || !enable_download_protection_)
248 return true; 250 return true;
249 251
250 // We need to check the database for url prefix, and later may fetch the url 252 // We need to check the database for url prefix, and later may fetch the url
251 // from the safebrowsing backends. These need to be asynchronous. 253 // from the safebrowsing backends. These need to be asynchronous.
252 SafeBrowsingCheck* check = new SafeBrowsingCheck(); 254 SafeBrowsingCheck* check = new SafeBrowsingCheck();
253 check->urls = url_chain; 255 check->urls = url_chain;
254 StartDownloadCheck( 256 StartDownloadCheck(
255 check, 257 check,
256 client, 258 client,
257 NewRunnableMethod(this, 259 base::Bind(&SafeBrowsingService::CheckDownloadUrlOnSBThread, this, check),
258 &SafeBrowsingService::CheckDownloadUrlOnSBThread,
259 check),
260 download_urlcheck_timeout_ms_); 260 download_urlcheck_timeout_ms_);
261 return false; 261 return false;
262 } 262 }
263 263
264 bool SafeBrowsingService::CheckDownloadHash(const std::string& full_hash, 264 bool SafeBrowsingService::CheckDownloadHash(const std::string& full_hash,
265 Client* client) { 265 Client* client) {
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
267 DCHECK(!full_hash.empty()); 267 DCHECK(!full_hash.empty());
268 if (!enabled_ || !enable_download_protection_ || full_hash.empty()) 268 if (!enabled_ || !enable_download_protection_ || full_hash.empty())
269 return true; 269 return true;
270 270
271 // We need to check the database for url prefix, and later may fetch the url 271 // We need to check the database for url prefix, and later may fetch the url
272 // from the safebrowsing backends. These need to be asynchronous. 272 // from the safebrowsing backends. These need to be asynchronous.
273 SafeBrowsingCheck* check = new SafeBrowsingCheck(); 273 SafeBrowsingCheck* check = new SafeBrowsingCheck();
274 check->full_hash.reset(new SBFullHash); 274 check->full_hash.reset(new SBFullHash);
275 safe_browsing_util::StringToSBFullHash(full_hash, check->full_hash.get()); 275 safe_browsing_util::StringToSBFullHash(full_hash, check->full_hash.get());
276 StartDownloadCheck( 276 StartDownloadCheck(
277 check, 277 check,
278 client, 278 client,
279 NewRunnableMethod(this, 279 base::Bind(&SafeBrowsingService::CheckDownloadHashOnSBThread,this, check),
280 &SafeBrowsingService::CheckDownloadHashOnSBThread,
281 check),
282 download_hashcheck_timeout_ms_); 280 download_hashcheck_timeout_ms_);
283 return false; 281 return false;
284 } 282 }
285 283
286 bool SafeBrowsingService::MatchCsdWhitelistUrl(const GURL& url) { 284 bool SafeBrowsingService::MatchCsdWhitelistUrl(const GURL& url) {
287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
288 if (!enabled_ || !enable_csd_whitelist_ || !MakeDatabaseAvailable()) { 286 if (!enabled_ || !enable_csd_whitelist_ || !MakeDatabaseAvailable()) {
289 // There is something funky going on here -- for example, perhaps the user 287 // There is something funky going on here -- for example, perhaps the user
290 // has not restarted since enabling metrics reporting, so we haven't 288 // has not restarted since enabling metrics reporting, so we haven't
291 // enabled the csd whitelist yet. Just to be safe we return true in this 289 // enabled the csd whitelist yet. Just to be safe we return true in this
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 check->client = client; 340 check->client = client;
343 check->result = SAFE; 341 check->result = SAFE;
344 check->is_download = false; 342 check->is_download = false;
345 check->need_get_hash = full_hits.empty(); 343 check->need_get_hash = full_hits.empty();
346 check->prefix_hits.swap(prefix_hits); 344 check->prefix_hits.swap(prefix_hits);
347 check->full_hits.swap(full_hits); 345 check->full_hits.swap(full_hits);
348 checks_.insert(check); 346 checks_.insert(check);
349 347
350 BrowserThread::PostTask( 348 BrowserThread::PostTask(
351 BrowserThread::IO, FROM_HERE, 349 BrowserThread::IO, FROM_HERE,
352 NewRunnableMethod(this, &SafeBrowsingService::OnCheckDone, check)); 350 base::Bind(&SafeBrowsingService::OnCheckDone, this, check));
353 351
354 return false; 352 return false;
355 } 353 }
356 354
357 void SafeBrowsingService::CancelCheck(Client* client) { 355 void SafeBrowsingService::CancelCheck(Client* client) {
358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
359 for (CurrentChecks::iterator i = checks_.begin(); i != checks_.end(); ++i) { 357 for (CurrentChecks::iterator i = checks_.begin(); i != checks_.end(); ++i) {
360 // We can't delete matching checks here because the db thread has a copy of 358 // We can't delete matching checks here because the db thread has a copy of
361 // the pointer. Instead, we simply NULL out the client, and when the db 359 // the pointer. Instead, we simply NULL out the client, and when the db
362 // thread calls us back, we'll clean up the check. 360 // thread calls us back, we'll clean up the check.
(...skipping 30 matching lines...) Expand all
393 resource.redirect_urls = redirect_urls; 391 resource.redirect_urls = redirect_urls;
394 resource.is_subresource = is_subresource; 392 resource.is_subresource = is_subresource;
395 resource.threat_type= result; 393 resource.threat_type= result;
396 resource.client = client; 394 resource.client = client;
397 resource.render_process_host_id = render_process_host_id; 395 resource.render_process_host_id = render_process_host_id;
398 resource.render_view_id = render_view_id; 396 resource.render_view_id = render_view_id;
399 397
400 // The blocking page must be created from the UI thread. 398 // The blocking page must be created from the UI thread.
401 BrowserThread::PostTask( 399 BrowserThread::PostTask(
402 BrowserThread::UI, FROM_HERE, 400 BrowserThread::UI, FROM_HERE,
403 NewRunnableMethod( 401 base::Bind(&SafeBrowsingService::DoDisplayBlockingPage, this, resource));
404 this, &SafeBrowsingService::DoDisplayBlockingPage, resource));
405 } 402 }
406 403
407 void SafeBrowsingService::HandleGetHashResults( 404 void SafeBrowsingService::HandleGetHashResults(
408 SafeBrowsingCheck* check, 405 SafeBrowsingCheck* check,
409 const std::vector<SBFullHashResult>& full_hashes, 406 const std::vector<SBFullHashResult>& full_hashes,
410 bool can_cache) { 407 bool can_cache) {
411 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 408 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
412 409
413 if (!enabled_) 410 if (!enabled_)
414 return; 411 return;
(...skipping 13 matching lines...) Expand all
428 if (can_cache && MakeDatabaseAvailable()) { 425 if (can_cache && MakeDatabaseAvailable()) {
429 // Cache the GetHash results in memory: 426 // Cache the GetHash results in memory:
430 database_->CacheHashResults(prefixes, full_hashes); 427 database_->CacheHashResults(prefixes, full_hashes);
431 } 428 }
432 } 429 }
433 430
434 void SafeBrowsingService::HandleChunk(const std::string& list, 431 void SafeBrowsingService::HandleChunk(const std::string& list,
435 SBChunkList* chunks) { 432 SBChunkList* chunks) {
436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 433 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
437 DCHECK(enabled_); 434 DCHECK(enabled_);
438 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 435 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind(
439 this, &SafeBrowsingService::HandleChunkForDatabase, list, chunks)); 436 &SafeBrowsingService::HandleChunkForDatabase, this, list, chunks));
440 } 437 }
441 438
442 void SafeBrowsingService::HandleChunkDelete( 439 void SafeBrowsingService::HandleChunkDelete(
443 std::vector<SBChunkDelete>* chunk_deletes) { 440 std::vector<SBChunkDelete>* chunk_deletes) {
444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
445 DCHECK(enabled_); 442 DCHECK(enabled_);
446 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 443 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind(
447 this, &SafeBrowsingService::DeleteChunks, chunk_deletes)); 444 &SafeBrowsingService::DeleteChunks, this, chunk_deletes));
448 } 445 }
449 446
450 void SafeBrowsingService::UpdateStarted() { 447 void SafeBrowsingService::UpdateStarted() {
451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
452 DCHECK(enabled_); 449 DCHECK(enabled_);
453 DCHECK(!update_in_progress_); 450 DCHECK(!update_in_progress_);
454 update_in_progress_ = true; 451 update_in_progress_ = true;
455 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 452 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind(
456 this, &SafeBrowsingService::GetAllChunksFromDatabase)); 453 &SafeBrowsingService::GetAllChunksFromDatabase, this));
457 } 454 }
458 455
459 void SafeBrowsingService::UpdateFinished(bool update_succeeded) { 456 void SafeBrowsingService::UpdateFinished(bool update_succeeded) {
460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
461 DCHECK(enabled_); 458 DCHECK(enabled_);
462 if (update_in_progress_) { 459 if (update_in_progress_) {
463 update_in_progress_ = false; 460 update_in_progress_ = false;
464 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, 461 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE,
465 NewRunnableMethod(this, 462 base::Bind(&SafeBrowsingService::DatabaseUpdateFinished,
466 &SafeBrowsingService::DatabaseUpdateFinished, 463 this, update_succeeded));
467 update_succeeded));
468 } 464 }
469 } 465 }
470 466
471 bool SafeBrowsingService::IsUpdateInProgress() const { 467 bool SafeBrowsingService::IsUpdateInProgress() const {
472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 468 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
473 return update_in_progress_; 469 return update_in_progress_;
474 } 470 }
475 471
476 void SafeBrowsingService::OnBlockingPageDone( 472 void SafeBrowsingService::OnBlockingPageDone(
477 const std::vector<UnsafeResource>& resources, 473 const std::vector<UnsafeResource>& resources,
478 bool proceed) { 474 bool proceed) {
479 for (std::vector<UnsafeResource>::const_iterator iter = resources.begin(); 475 for (std::vector<UnsafeResource>::const_iterator iter = resources.begin();
480 iter != resources.end(); ++iter) { 476 iter != resources.end(); ++iter) {
481 const UnsafeResource& resource = *iter; 477 const UnsafeResource& resource = *iter;
482 NotifyClientBlockingComplete(resource.client, proceed); 478 NotifyClientBlockingComplete(resource.client, proceed);
483 479
484 if (proceed) { 480 if (proceed) {
485 BrowserThread::PostTask( 481 BrowserThread::PostTask(
486 BrowserThread::UI, 482 BrowserThread::UI,
487 FROM_HERE, 483 FROM_HERE,
488 NewRunnableMethod(this, 484 base::Bind(&SafeBrowsingService::UpdateWhitelist, this, resource));
489 &SafeBrowsingService::UpdateWhitelist,
490 resource));
491 } 485 }
492 } 486 }
493 } 487 }
494 488
495 void SafeBrowsingService::OnNewMacKeys(const std::string& client_key, 489 void SafeBrowsingService::OnNewMacKeys(const std::string& client_key,
496 const std::string& wrapped_key) { 490 const std::string& wrapped_key) {
497 PrefService* prefs = g_browser_process->local_state(); 491 PrefService* prefs = g_browser_process->local_state();
498 if (prefs) { 492 if (prefs) {
499 prefs->SetString(prefs::kSafeBrowsingClientKey, client_key); 493 prefs->SetString(prefs::kSafeBrowsingClientKey, client_key);
500 prefs->SetString(prefs::kSafeBrowsingWrappedKey, wrapped_key); 494 prefs->SetString(prefs::kSafeBrowsingWrappedKey, wrapped_key);
501 } 495 }
502 } 496 }
503 497
504 // static 498 // static
505 void SafeBrowsingService::RegisterPrefs(PrefService* prefs) { 499 void SafeBrowsingService::RegisterPrefs(PrefService* prefs) {
506 prefs->RegisterStringPref(prefs::kSafeBrowsingClientKey, ""); 500 prefs->RegisterStringPref(prefs::kSafeBrowsingClientKey, "");
507 prefs->RegisterStringPref(prefs::kSafeBrowsingWrappedKey, ""); 501 prefs->RegisterStringPref(prefs::kSafeBrowsingWrappedKey, "");
508 } 502 }
509 503
510 void SafeBrowsingService::ResetDatabase() { 504 void SafeBrowsingService::ResetDatabase() {
511 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
512 DCHECK(enabled_); 506 DCHECK(enabled_);
513 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 507 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind(
514 this, &SafeBrowsingService::OnResetDatabase)); 508 &SafeBrowsingService::OnResetDatabase, this));
515 } 509 }
516 510
517 void SafeBrowsingService::LogPauseDelay(base::TimeDelta time) { 511 void SafeBrowsingService::LogPauseDelay(base::TimeDelta time) {
518 UMA_HISTOGRAM_LONG_TIMES("SB2.Delay", time); 512 UMA_HISTOGRAM_LONG_TIMES("SB2.Delay", time);
519 } 513 }
520 514
521 void SafeBrowsingService::OnIOInitialize( 515 void SafeBrowsingService::OnIOInitialize(
522 const std::string& client_key, 516 const std::string& client_key,
523 const std::string& wrapped_key, 517 const std::string& wrapped_key,
524 net::URLRequestContextGetter* request_context_getter) { 518 net::URLRequestContextGetter* request_context_getter) {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 // Delete pending checks, calling back any clients with 'SAFE'. We have 619 // Delete pending checks, calling back any clients with 'SAFE'. We have
626 // to do this after the db thread returns because methods on it can have 620 // to do this after the db thread returns because methods on it can have
627 // copies of these pointers, so deleting them might lead to accessing garbage. 621 // copies of these pointers, so deleting them might lead to accessing garbage.
628 for (CurrentChecks::iterator it = checks_.begin(); 622 for (CurrentChecks::iterator it = checks_.begin();
629 it != checks_.end(); ++it) { 623 it != checks_.end(); ++it) {
630 SafeBrowsingCheck* check = *it; 624 SafeBrowsingCheck* check = *it;
631 if (check->client) { 625 if (check->client) {
632 check->result = SAFE; 626 check->result = SAFE;
633 check->client->OnSafeBrowsingResult(*check); 627 check->client->OnSafeBrowsingResult(*check);
634 } 628 }
635 if (check->timeout_task) 629 if (check->cancel_timeout)
636 check->timeout_task->Cancel(); 630 *check->cancel_timeout = true;
637 } 631 }
638 STLDeleteElements(&checks_); 632 STLDeleteElements(&checks_);
639 633
640 gethash_requests_.clear(); 634 gethash_requests_.clear();
641 } 635 }
642 636
643 bool SafeBrowsingService::DatabaseAvailable() const { 637 bool SafeBrowsingService::DatabaseAvailable() const {
644 base::AutoLock lock(database_lock_); 638 base::AutoLock lock(database_lock_);
645 return !closing_database_ && (database_ != NULL); 639 return !closing_database_ && (database_ != NULL);
646 } 640 }
647 641
648 bool SafeBrowsingService::MakeDatabaseAvailable() { 642 bool SafeBrowsingService::MakeDatabaseAvailable() {
649 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 643 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
650 DCHECK(enabled_); 644 DCHECK(enabled_);
651 if (DatabaseAvailable()) 645 if (DatabaseAvailable())
652 return true; 646 return true;
653 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, 647 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE,
654 NewRunnableMethod(this, &SafeBrowsingService::GetDatabase)); 648 base::IgnoreReturn<SafeBrowsingDatabase*>(
649 base::Bind(&SafeBrowsingService::GetDatabase, this)));
655 return false; 650 return false;
656 } 651 }
657 652
658 void SafeBrowsingService::CloseDatabase() { 653 void SafeBrowsingService::CloseDatabase() {
659 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 654 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
660 655
661 // Cases to avoid: 656 // Cases to avoid:
662 // * If |closing_database_| is true, continuing will queue up a second 657 // * If |closing_database_| is true, continuing will queue up a second
663 // request, |closing_database_| will be reset after handling the first 658 // request, |closing_database_| will be reset after handling the first
664 // request, and if any functions on the db thread recreate the database, we 659 // request, and if any functions on the db thread recreate the database, we
(...skipping 10 matching lines...) Expand all
675 // would lead to an infinite loop in DatabaseLoadComplete(), and even if it 670 // would lead to an infinite loop in DatabaseLoadComplete(), and even if it
676 // didn't, it would be pointless since we'd just want to recreate. 671 // didn't, it would be pointless since we'd just want to recreate.
677 // 672 //
678 // The first two cases above are handled by checking DatabaseAvailable(). 673 // The first two cases above are handled by checking DatabaseAvailable().
679 if (!DatabaseAvailable() || !queued_checks_.empty()) 674 if (!DatabaseAvailable() || !queued_checks_.empty())
680 return; 675 return;
681 676
682 closing_database_ = true; 677 closing_database_ = true;
683 if (safe_browsing_thread_.get()) { 678 if (safe_browsing_thread_.get()) {
684 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, 679 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE,
685 NewRunnableMethod(this, &SafeBrowsingService::OnCloseDatabase)); 680 base::Bind(&SafeBrowsingService::OnCloseDatabase, this));
686 } 681 }
687 } 682 }
688 683
689 SafeBrowsingDatabase* SafeBrowsingService::GetDatabase() { 684 SafeBrowsingDatabase* SafeBrowsingService::GetDatabase() {
690 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); 685 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop());
691 if (database_) 686 if (database_)
692 return database_; 687 return database_;
693 688
694 FilePath path; 689 FilePath path;
695 bool result = PathService::Get(chrome::DIR_USER_DATA, &path); 690 bool result = PathService::Get(chrome::DIR_USER_DATA, &path);
(...skipping 10 matching lines...) Expand all
706 database->Init(path); 701 database->Init(path);
707 { 702 {
708 // Acquiring the lock here guarantees correct ordering between the writes to 703 // Acquiring the lock here guarantees correct ordering between the writes to
709 // the new database object above, and the setting of |databse_| below. 704 // the new database object above, and the setting of |databse_| below.
710 base::AutoLock lock(database_lock_); 705 base::AutoLock lock(database_lock_);
711 database_ = database; 706 database_ = database;
712 } 707 }
713 708
714 BrowserThread::PostTask( 709 BrowserThread::PostTask(
715 BrowserThread::IO, FROM_HERE, 710 BrowserThread::IO, FROM_HERE,
716 NewRunnableMethod(this, &SafeBrowsingService::DatabaseLoadComplete)); 711 base::Bind(&SafeBrowsingService::DatabaseLoadComplete, this));
717 712
718 UMA_HISTOGRAM_TIMES("SB2.DatabaseOpen", base::TimeTicks::Now() - before); 713 UMA_HISTOGRAM_TIMES("SB2.DatabaseOpen", base::TimeTicks::Now() - before);
719 return database_; 714 return database_;
720 } 715 }
721 716
722 void SafeBrowsingService::OnCheckDone(SafeBrowsingCheck* check) { 717 void SafeBrowsingService::OnCheckDone(SafeBrowsingCheck* check) {
723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 718 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
724 719
725 if (!enabled_) 720 if (!enabled_)
726 return; 721 return;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 database_update_in_progress_ = true; 767 database_update_in_progress_ = true;
773 GetDatabase(); // This guarantees that |database_| is non-NULL. 768 GetDatabase(); // This guarantees that |database_| is non-NULL.
774 if (database_->UpdateStarted(&lists)) { 769 if (database_->UpdateStarted(&lists)) {
775 database_error = false; 770 database_error = false;
776 } else { 771 } else {
777 database_->UpdateFinished(false); 772 database_->UpdateFinished(false);
778 } 773 }
779 774
780 BrowserThread::PostTask( 775 BrowserThread::PostTask(
781 BrowserThread::IO, FROM_HERE, 776 BrowserThread::IO, FROM_HERE,
782 NewRunnableMethod( 777 base::Bind(&SafeBrowsingService::OnGetAllChunksFromDatabase,
783 this, &SafeBrowsingService::OnGetAllChunksFromDatabase, lists, 778 this, lists, database_error));
784 database_error));
785 } 779 }
786 780
787 void SafeBrowsingService::OnGetAllChunksFromDatabase( 781 void SafeBrowsingService::OnGetAllChunksFromDatabase(
788 const std::vector<SBListChunkRanges>& lists, bool database_error) { 782 const std::vector<SBListChunkRanges>& lists, bool database_error) {
789 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 783 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
790 if (enabled_) 784 if (enabled_)
791 protocol_manager_->OnGetChunksComplete(lists, database_error); 785 protocol_manager_->OnGetChunksComplete(lists, database_error);
792 } 786 }
793 787
794 void SafeBrowsingService::OnChunkInserted() { 788 void SafeBrowsingService::OnChunkInserted() {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 823
830 void SafeBrowsingService::HandleChunkForDatabase( 824 void SafeBrowsingService::HandleChunkForDatabase(
831 const std::string& list_name, SBChunkList* chunks) { 825 const std::string& list_name, SBChunkList* chunks) {
832 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); 826 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop());
833 if (chunks) { 827 if (chunks) {
834 GetDatabase()->InsertChunks(list_name, *chunks); 828 GetDatabase()->InsertChunks(list_name, *chunks);
835 delete chunks; 829 delete chunks;
836 } 830 }
837 BrowserThread::PostTask( 831 BrowserThread::PostTask(
838 BrowserThread::IO, FROM_HERE, 832 BrowserThread::IO, FROM_HERE,
839 NewRunnableMethod(this, &SafeBrowsingService::OnChunkInserted)); 833 base::Bind(&SafeBrowsingService::OnChunkInserted, this));
840 } 834 }
841 835
842 void SafeBrowsingService::DeleteChunks( 836 void SafeBrowsingService::DeleteChunks(
843 std::vector<SBChunkDelete>* chunk_deletes) { 837 std::vector<SBChunkDelete>* chunk_deletes) {
844 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); 838 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop());
845 if (chunk_deletes) { 839 if (chunk_deletes) {
846 GetDatabase()->DeleteChunks(*chunk_deletes); 840 GetDatabase()->DeleteChunks(*chunk_deletes);
847 delete chunk_deletes; 841 delete chunk_deletes;
848 } 842 }
849 } 843 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 #endif 910 #endif
917 911
918 // TODO(noelutz): remove this boolean variable since it should always be true 912 // TODO(noelutz): remove this boolean variable since it should always be true
919 // if SafeBrowsing is enabled. Unfortunately, we have no test data for this 913 // if SafeBrowsing is enabled. Unfortunately, we have no test data for this
920 // list right now. This means that we need to be able to disable this list 914 // list right now. This means that we need to be able to disable this list
921 // for the SafeBrowsing test to pass. 915 // for the SafeBrowsing test to pass.
922 enable_download_whitelist_ = enable_csd_whitelist_; 916 enable_download_whitelist_ = enable_csd_whitelist_;
923 917
924 BrowserThread::PostTask( 918 BrowserThread::PostTask(
925 BrowserThread::IO, FROM_HERE, 919 BrowserThread::IO, FROM_HERE,
926 NewRunnableMethod( 920 base::Bind(&SafeBrowsingService::OnIOInitialize,
927 this, &SafeBrowsingService::OnIOInitialize, client_key, wrapped_key, 921 this, client_key, wrapped_key, request_context_getter));
928 request_context_getter));
929 } 922 }
930 923
931 void SafeBrowsingService::Stop() { 924 void SafeBrowsingService::Stop() {
932 BrowserThread::PostTask( 925 BrowserThread::PostTask(
933 BrowserThread::IO, FROM_HERE, 926 BrowserThread::IO, FROM_HERE,
934 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); 927 base::Bind(&SafeBrowsingService::OnIOShutdown, this));
935 } 928 }
936 929
937 void SafeBrowsingService::OnCloseDatabase() { 930 void SafeBrowsingService::OnCloseDatabase() {
938 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); 931 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop());
939 DCHECK(closing_database_); 932 DCHECK(closing_database_);
940 933
941 // Because |closing_database_| is true, nothing on the IO thread will be 934 // Because |closing_database_| is true, nothing on the IO thread will be
942 // accessing the database, so it's safe to delete and then NULL the pointer. 935 // accessing the database, so it's safe to delete and then NULL the pointer.
943 delete database_; 936 delete database_;
944 database_ = NULL; 937 database_ = NULL;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 } 1011 }
1019 1012
1020 void SafeBrowsingService::DoDisplayBlockingPage( 1013 void SafeBrowsingService::DoDisplayBlockingPage(
1021 const UnsafeResource& resource) { 1014 const UnsafeResource& resource) {
1022 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1015 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1023 // Check if the user has already ignored our warning for this render_view 1016 // Check if the user has already ignored our warning for this render_view
1024 // and domain. 1017 // and domain.
1025 if (IsWhitelisted(resource)) { 1018 if (IsWhitelisted(resource)) {
1026 BrowserThread::PostTask( 1019 BrowserThread::PostTask(
1027 BrowserThread::IO, FROM_HERE, 1020 BrowserThread::IO, FROM_HERE,
1028 NewRunnableMethod(this, 1021 base::Bind(&SafeBrowsingService::NotifyClientBlockingComplete,
1029 &SafeBrowsingService::NotifyClientBlockingComplete, 1022 this, resource.client, true));
1030 resource.client, true));
1031 return; 1023 return;
1032 } 1024 }
1033 1025
1034 // The tab might have been closed. 1026 // The tab might have been closed.
1035 TabContents* tab_contents = 1027 TabContents* tab_contents =
1036 tab_util::GetTabContentsByID(resource.render_process_host_id, 1028 tab_util::GetTabContentsByID(resource.render_process_host_id,
1037 resource.render_view_id); 1029 resource.render_view_id);
1038 1030
1039 if (!tab_contents) { 1031 if (!tab_contents) {
1040 // The tab is gone and we did not have a chance at showing the interstitial. 1032 // The tab is gone and we did not have a chance at showing the interstitial.
1041 // Just act as if "Don't Proceed" were chosen. 1033 // Just act as if "Don't Proceed" were chosen.
1042 std::vector<UnsafeResource> resources; 1034 std::vector<UnsafeResource> resources;
1043 resources.push_back(resource); 1035 resources.push_back(resource);
1044 BrowserThread::PostTask( 1036 BrowserThread::PostTask(
1045 BrowserThread::IO, FROM_HERE, 1037 BrowserThread::IO, FROM_HERE,
1046 NewRunnableMethod( 1038 base::Bind(&SafeBrowsingService::OnBlockingPageDone,
1047 this, &SafeBrowsingService::OnBlockingPageDone, resources, false)); 1039 this, resources, false));
1048 return; 1040 return;
1049 } 1041 }
1050 1042
1051 if (resource.threat_type != SafeBrowsingService::SAFE && 1043 if (resource.threat_type != SafeBrowsingService::SAFE &&
1052 CanReportStats()) { 1044 CanReportStats()) {
1053 GURL page_url = tab_contents->GetURL(); 1045 GURL page_url = tab_contents->GetURL();
1054 GURL referrer_url; 1046 GURL referrer_url;
1055 NavigationEntry* entry = tab_contents->controller().GetActiveEntry(); 1047 NavigationEntry* entry = tab_contents->controller().GetActiveEntry();
1056 if (entry) 1048 if (entry)
1057 referrer_url = entry->referrer(); 1049 referrer_url = entry->referrer();
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 void SafeBrowsingService::CheckDownloadHashOnSBThread( 1139 void SafeBrowsingService::CheckDownloadHashOnSBThread(
1148 SafeBrowsingCheck* check) { 1140 SafeBrowsingCheck* check) {
1149 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); 1141 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop());
1150 DCHECK(enable_download_protection_); 1142 DCHECK(enable_download_protection_);
1151 1143
1152 if (!database_->ContainsDownloadHashPrefix(check->full_hash->prefix)) { 1144 if (!database_->ContainsDownloadHashPrefix(check->full_hash->prefix)) {
1153 // Good, we don't have hash for this url prefix. 1145 // Good, we don't have hash for this url prefix.
1154 check->result = SAFE; 1146 check->result = SAFE;
1155 BrowserThread::PostTask( 1147 BrowserThread::PostTask(
1156 BrowserThread::IO, FROM_HERE, 1148 BrowserThread::IO, FROM_HERE,
1157 NewRunnableMethod(this, 1149 base::Bind(&SafeBrowsingService::CheckDownloadHashDone, this, check));
1158 &SafeBrowsingService::CheckDownloadHashDone,
1159 check));
1160 return; 1150 return;
1161 } 1151 }
1162 1152
1163 check->need_get_hash = true; 1153 check->need_get_hash = true;
1164 check->prefix_hits.push_back(check->full_hash->prefix); 1154 check->prefix_hits.push_back(check->full_hash->prefix);
1165 BrowserThread::PostTask( 1155 BrowserThread::PostTask(
1166 BrowserThread::IO, FROM_HERE, 1156 BrowserThread::IO, FROM_HERE,
1167 NewRunnableMethod(this, &SafeBrowsingService::OnCheckDone, check)); 1157 base::Bind(&SafeBrowsingService::OnCheckDone, this, check));
1168 } 1158 }
1169 1159
1170 void SafeBrowsingService::CheckDownloadUrlOnSBThread(SafeBrowsingCheck* check) { 1160 void SafeBrowsingService::CheckDownloadUrlOnSBThread(SafeBrowsingCheck* check) {
1171 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); 1161 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop());
1172 DCHECK(enable_download_protection_); 1162 DCHECK(enable_download_protection_);
1173 1163
1174 std::vector<SBPrefix> prefix_hits; 1164 std::vector<SBPrefix> prefix_hits;
1175 1165
1176 if (!database_->ContainsDownloadUrl(check->urls, &prefix_hits)) { 1166 if (!database_->ContainsDownloadUrl(check->urls, &prefix_hits)) {
1177 // Good, we don't have hash for this url prefix. 1167 // Good, we don't have hash for this url prefix.
1178 check->result = SAFE; 1168 check->result = SAFE;
1179 BrowserThread::PostTask( 1169 BrowserThread::PostTask(
1180 BrowserThread::IO, FROM_HERE, 1170 BrowserThread::IO, FROM_HERE,
1181 NewRunnableMethod(this, 1171 base::Bind(&SafeBrowsingService::CheckDownloadUrlDone, this, check));
1182 &SafeBrowsingService::CheckDownloadUrlDone,
1183 check));
1184 return; 1172 return;
1185 } 1173 }
1186 1174
1187 check->need_get_hash = true; 1175 check->need_get_hash = true;
1188 check->prefix_hits.clear(); 1176 check->prefix_hits.clear();
1189 check->prefix_hits = prefix_hits; 1177 check->prefix_hits = prefix_hits;
1190 BrowserThread::PostTask( 1178 BrowserThread::PostTask(
1191 BrowserThread::IO, FROM_HERE, 1179 BrowserThread::IO, FROM_HERE,
1192 NewRunnableMethod(this, &SafeBrowsingService::OnCheckDone, check)); 1180 base::Bind(&SafeBrowsingService::OnCheckDone, this, check));
1193 } 1181 }
1194 1182
1195 void SafeBrowsingService::TimeoutCallback(SafeBrowsingCheck* check) { 1183 void SafeBrowsingService::TimeoutCallback(SafeBrowsingCheck* check,
1184 bool* cancel_timeout) {
1196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 1185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1197 DCHECK(check); 1186 DCHECK(check);
1198 1187
1199 if (!enabled_) 1188 if (!enabled_ || *cancel_timeout)
1200 return; 1189 return;
1201 1190
1202 DCHECK(checks_.find(check) != checks_.end()); 1191 DCHECK(checks_.find(check) != checks_.end());
1203 DCHECK_EQ(check->result, SAFE); 1192 DCHECK_EQ(check->result, SAFE);
1204 if (check->client) { 1193 if (check->client) {
1205 check->client->OnSafeBrowsingResult(*check); 1194 check->client->OnSafeBrowsingResult(*check);
1206 check->client = NULL; 1195 check->client = NULL;
1207 } 1196 }
1208 check->timeout_task = NULL; 1197
1198 // After this call |cancel_timeout| will be deleted.
1199 check->cancel_timeout = NULL;
1209 } 1200 }
1210 1201
1211 void SafeBrowsingService::CheckDownloadUrlDone(SafeBrowsingCheck* check) { 1202 void SafeBrowsingService::CheckDownloadUrlDone(SafeBrowsingCheck* check) {
1212 DCHECK(enable_download_protection_); 1203 DCHECK(enable_download_protection_);
1213 SafeBrowsingCheckDone(check); 1204 SafeBrowsingCheckDone(check);
1214 } 1205 }
1215 1206
1216 void SafeBrowsingService::CheckDownloadHashDone(SafeBrowsingCheck* check) { 1207 void SafeBrowsingService::CheckDownloadHashDone(SafeBrowsingCheck* check) {
1217 DCHECK(enable_download_protection_); 1208 DCHECK(enable_download_protection_);
1218 SafeBrowsingCheckDone(check); 1209 SafeBrowsingCheckDone(check);
1219 } 1210 }
1220 1211
1221 void SafeBrowsingService::SafeBrowsingCheckDone(SafeBrowsingCheck* check) { 1212 void SafeBrowsingService::SafeBrowsingCheckDone(SafeBrowsingCheck* check) {
1222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 1213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1223 DCHECK(check); 1214 DCHECK(check);
1224 1215
1225 if (!enabled_) 1216 if (!enabled_)
1226 return; 1217 return;
1227 1218
1228 VLOG(1) << "SafeBrowsingCheckDone: " << check->result; 1219 VLOG(1) << "SafeBrowsingCheckDone: " << check->result;
1229 DCHECK(checks_.find(check) != checks_.end()); 1220 DCHECK(checks_.find(check) != checks_.end());
1230 if (check->client) 1221 if (check->client)
1231 check->client->OnSafeBrowsingResult(*check); 1222 check->client->OnSafeBrowsingResult(*check);
1232 if (check->timeout_task) 1223 if (check->cancel_timeout)
1233 check->timeout_task->Cancel(); 1224 *check->cancel_timeout = true;
1234 checks_.erase(check); 1225 checks_.erase(check);
1235 delete check; 1226 delete check;
1236 } 1227 }
1237 1228
1238 void SafeBrowsingService::StartDownloadCheck(SafeBrowsingCheck* check, 1229 void SafeBrowsingService::StartDownloadCheck(SafeBrowsingCheck* check,
1239 Client* client, 1230 Client* client,
1240 CancelableTask* task, 1231 const base::Closure& task,
1241 int64 timeout_ms) { 1232 int64 timeout_ms) {
1233 scoped_ptr<bool> cancel_timeout(new bool(false));
1242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 1234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1243 check->client = client; 1235 check->client = client;
1244 check->result = SAFE; 1236 check->result = SAFE;
1245 check->is_download = true; 1237 check->is_download = true;
1246 check->timeout_task = 1238 check->cancel_timeout = cancel_timeout.get();
1247 NewRunnableMethod(this, &SafeBrowsingService::TimeoutCallback, check);
1248 checks_.insert(check); 1239 checks_.insert(check);
1249 1240
1250 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task); 1241 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task);
1251 1242
1252 MessageLoop::current()->PostDelayedTask( 1243 MessageLoop::current()->PostDelayedTask(FROM_HERE,
1253 FROM_HERE, check->timeout_task, timeout_ms); 1244 base::Bind(&SafeBrowsingService::TimeoutCallback,
1245 this, check, base::Owned(cancel_timeout.release())),
1246 timeout_ms);
1254 } 1247 }
1255 1248
1256 void SafeBrowsingService::UpdateWhitelist(const UnsafeResource& resource) { 1249 void SafeBrowsingService::UpdateWhitelist(const UnsafeResource& resource) {
1257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1258 // Whitelist this domain and warning type for the given tab. 1251 // Whitelist this domain and warning type for the given tab.
1259 WhiteListedEntry entry; 1252 WhiteListedEntry entry;
1260 entry.render_process_host_id = resource.render_process_host_id; 1253 entry.render_process_host_id = resource.render_process_host_id;
1261 entry.render_view_id = resource.render_view_id; 1254 entry.render_view_id = resource.render_view_id;
1262 entry.domain = net::RegistryControlledDomainService::GetDomainAndRegistry( 1255 entry.domain = net::RegistryControlledDomainService::GetDomainAndRegistry(
1263 resource.url); 1256 resource.url);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1361 Stop(); 1354 Stop();
1362 1355
1363 if (csd_service_.get()) 1356 if (csd_service_.get())
1364 csd_service_->SetEnabledAndRefreshState(enable); 1357 csd_service_->SetEnabledAndRefreshState(enable);
1365 if (download_service_.get()) { 1358 if (download_service_.get()) {
1366 download_service_->SetEnabled( 1359 download_service_->SetEnabled(
1367 enable && CommandLine::ForCurrentProcess()->HasSwitch( 1360 enable && CommandLine::ForCurrentProcess()->HasSwitch(
1368 switches::kEnableImprovedDownloadProtection)); 1361 switches::kEnableImprovedDownloadProtection));
1369 } 1362 }
1370 } 1363 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698