| OLD | NEW |
| 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/safe_browsing/database_manager.h" | 5 #include "chrome/browser/safe_browsing/database_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/debug/leak_tracker.h" | 13 #include "base/debug/leak_tracker.h" |
| 14 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/profiler/scoped_tracker.h" | 15 #include "base/profiler/scoped_tracker.h" |
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 17 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 18 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 19 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/chrome_notification_types.h" | 21 #include "chrome/browser/chrome_notification_types.h" |
| 21 #include "chrome/browser/prerender/prerender_field_trial.h" | 22 #include "chrome/browser/prerender/prerender_field_trial.h" |
| 22 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 23 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 23 #include "chrome/browser/safe_browsing/download_protection_service.h" | 24 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 24 #include "chrome/browser/safe_browsing/malware_details.h" | 25 #include "chrome/browser/safe_browsing/malware_details.h" |
| 25 #include "chrome/browser/safe_browsing/protocol_manager.h" | 26 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 26 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 27 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 27 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 28 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 28 #include "chrome/browser/safe_browsing/ui_manager.h" | 29 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 29 #include "chrome/common/chrome_constants.h" | 30 #include "chrome/common/chrome_constants.h" |
| 30 #include "chrome/common/chrome_paths.h" | 31 #include "chrome/common/chrome_paths.h" |
| 31 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
| 32 #include "components/metrics/metrics_service.h" | |
| 33 #include "components/startup_metric_utils/startup_metric_utils.h" | 33 #include "components/startup_metric_utils/startup_metric_utils.h" |
| 34 #include "components/variations/variations_associated_data.h" |
| 34 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 35 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
| 36 #include "url/url_constants.h" | 37 #include "url/url_constants.h" |
| 37 | 38 |
| 38 using content::BrowserThread; | 39 using content::BrowserThread; |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 41 | 42 |
| 42 // Timeout for match checks, e.g. download URLs, hashes. | 43 // Timeout for match checks, e.g. download URLs, hashes. |
| 43 const int kCheckTimeoutMs = 10000; | 44 const int kCheckTimeoutMs = 10000; |
| (...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 | 590 |
| 590 // Cache the GetHash results. | 591 // Cache the GetHash results. |
| 591 if (cache_lifetime != base::TimeDelta() && MakeDatabaseAvailable()) | 592 if (cache_lifetime != base::TimeDelta() && MakeDatabaseAvailable()) |
| 592 database_->CacheHashResults(prefixes, full_hashes, cache_lifetime); | 593 database_->CacheHashResults(prefixes, full_hashes, cache_lifetime); |
| 593 } | 594 } |
| 594 | 595 |
| 595 void SafeBrowsingDatabaseManager::GetChunks(GetChunksCallback callback) { | 596 void SafeBrowsingDatabaseManager::GetChunks(GetChunksCallback callback) { |
| 596 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 597 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 597 DCHECK(enabled_); | 598 DCHECK(enabled_); |
| 598 DCHECK(!callback.is_null()); | 599 DCHECK(!callback.is_null()); |
| 599 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind( | 600 safe_browsing_task_runner_->PostTask( |
| 600 &SafeBrowsingDatabaseManager::GetAllChunksFromDatabase, this, callback)); | 601 FROM_HERE, |
| 602 base::Bind(&SafeBrowsingDatabaseManager::GetAllChunksFromDatabase, this, |
| 603 callback)); |
| 601 } | 604 } |
| 602 | 605 |
| 603 void SafeBrowsingDatabaseManager::AddChunks( | 606 void SafeBrowsingDatabaseManager::AddChunks( |
| 604 const std::string& list, | 607 const std::string& list, |
| 605 scoped_ptr<ScopedVector<SBChunkData> > chunks, | 608 scoped_ptr<ScopedVector<SBChunkData> > chunks, |
| 606 AddChunksCallback callback) { | 609 AddChunksCallback callback) { |
| 607 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 610 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 608 DCHECK(enabled_); | 611 DCHECK(enabled_); |
| 609 DCHECK(!callback.is_null()); | 612 DCHECK(!callback.is_null()); |
| 610 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind( | 613 safe_browsing_task_runner_->PostTask( |
| 611 &SafeBrowsingDatabaseManager::AddDatabaseChunks, this, list, | 614 FROM_HERE, base::Bind(&SafeBrowsingDatabaseManager::AddDatabaseChunks, |
| 612 base::Passed(&chunks), callback)); | 615 this, list, base::Passed(&chunks), callback)); |
| 613 } | 616 } |
| 614 | 617 |
| 615 void SafeBrowsingDatabaseManager::DeleteChunks( | 618 void SafeBrowsingDatabaseManager::DeleteChunks( |
| 616 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { | 619 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { |
| 617 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 620 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 618 DCHECK(enabled_); | 621 DCHECK(enabled_); |
| 619 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind( | 622 safe_browsing_task_runner_->PostTask( |
| 620 &SafeBrowsingDatabaseManager::DeleteDatabaseChunks, this, | 623 FROM_HERE, base::Bind(&SafeBrowsingDatabaseManager::DeleteDatabaseChunks, |
| 621 base::Passed(&chunk_deletes))); | 624 this, base::Passed(&chunk_deletes))); |
| 622 } | 625 } |
| 623 | 626 |
| 624 void SafeBrowsingDatabaseManager::UpdateStarted() { | 627 void SafeBrowsingDatabaseManager::UpdateStarted() { |
| 625 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 628 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 626 DCHECK(enabled_); | 629 DCHECK(enabled_); |
| 627 DCHECK(!update_in_progress_); | 630 DCHECK(!update_in_progress_); |
| 628 update_in_progress_ = true; | 631 update_in_progress_ = true; |
| 629 } | 632 } |
| 630 | 633 |
| 631 void SafeBrowsingDatabaseManager::UpdateFinished(bool update_succeeded) { | 634 void SafeBrowsingDatabaseManager::UpdateFinished(bool update_succeeded) { |
| 632 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 635 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 633 DCHECK(enabled_); | 636 DCHECK(enabled_); |
| 634 if (update_in_progress_) { | 637 if (update_in_progress_) { |
| 635 update_in_progress_ = false; | 638 update_in_progress_ = false; |
| 636 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, | 639 safe_browsing_task_runner_->PostTask( |
| 637 base::Bind(&SafeBrowsingDatabaseManager::DatabaseUpdateFinished, | 640 FROM_HERE, |
| 638 this, update_succeeded)); | 641 base::Bind(&SafeBrowsingDatabaseManager::DatabaseUpdateFinished, this, |
| 642 update_succeeded)); |
| 639 } | 643 } |
| 640 } | 644 } |
| 641 | 645 |
| 642 void SafeBrowsingDatabaseManager::ResetDatabase() { | 646 void SafeBrowsingDatabaseManager::ResetDatabase() { |
| 643 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 647 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 644 DCHECK(enabled_); | 648 DCHECK(enabled_); |
| 645 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind( | 649 safe_browsing_task_runner_->PostTask( |
| 646 &SafeBrowsingDatabaseManager::OnResetDatabase, this)); | 650 FROM_HERE, |
| 651 base::Bind(&SafeBrowsingDatabaseManager::OnResetDatabase, this)); |
| 647 } | 652 } |
| 648 | 653 |
| 649 void SafeBrowsingDatabaseManager::StartOnIOThread() { | 654 void SafeBrowsingDatabaseManager::StartOnIOThread() { |
| 650 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 655 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 651 if (enabled_) | 656 if (enabled_) |
| 652 return; | 657 return; |
| 653 | 658 |
| 654 DCHECK(!safe_browsing_thread_.get()); | 659 DCHECK(!safe_browsing_task_runner_); |
| 655 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455469 is fixed. | 660 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455469 is fixed. |
| 656 tracked_objects::ScopedTracker tracking_profile2( | 661 tracked_objects::ScopedTracker tracking_profile2( |
| 657 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 662 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 658 "455469 SafeBrowsingDatabaseManager::StartOnIOThread")); | 663 "455469 SafeBrowsingDatabaseManager::StartOnIOThread")); |
| 659 safe_browsing_thread_.reset(new base::Thread("Chrome_SafeBrowsingThread")); | 664 // Use the blocking pool instead of a dedicated thread for safe browsing work, |
| 660 if (!safe_browsing_thread_->Start()) | 665 // if specified by an experiment. |
| 661 return; | 666 const bool use_blocking_pool = |
| 667 variations::GetVariationParamValue("LightSpeed", "SBThreadingMode") == |
| 668 "BlockingPool"; |
| 669 if (use_blocking_pool) { |
| 670 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 671 safe_browsing_task_runner_ = |
| 672 pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 673 pool->GetSequenceToken(), |
| 674 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 675 } else { |
| 676 DCHECK(!safe_browsing_thread_.get()); |
| 677 |
| 678 safe_browsing_thread_.reset(new base::Thread("Chrome_SafeBrowsingThread")); |
| 679 if (!safe_browsing_thread_->Start()) |
| 680 return; |
| 681 safe_browsing_task_runner_ = safe_browsing_thread_->task_runner(); |
| 682 } |
| 683 |
| 662 enabled_ = true; | 684 enabled_ = true; |
| 663 | 685 |
| 664 MakeDatabaseAvailable(); | 686 MakeDatabaseAvailable(); |
| 665 } | 687 } |
| 666 | 688 |
| 667 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { | 689 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { |
| 668 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 690 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 669 | 691 |
| 670 DoStopOnIOThread(); | 692 DoStopOnIOThread(); |
| 671 if (shutdown) { | 693 if (shutdown) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 // could start using it on the IO thread and then have the second request | 749 // could start using it on the IO thread and then have the second request |
| 728 // handler delete it out from under us. | 750 // handler delete it out from under us. |
| 729 // * If |database_| is NULL, then either no creation request is in flight, in | 751 // * If |database_| is NULL, then either no creation request is in flight, in |
| 730 // which case we don't need to do anything, or one is in flight, in which | 752 // which case we don't need to do anything, or one is in flight, in which |
| 731 // case the database will be recreated before our deletion request is | 753 // case the database will be recreated before our deletion request is |
| 732 // handled, and could be used on the IO thread in that time period, leading | 754 // handled, and could be used on the IO thread in that time period, leading |
| 733 // to the same problem as above. | 755 // to the same problem as above. |
| 734 // Checking DatabaseAvailable() avoids both of these. | 756 // Checking DatabaseAvailable() avoids both of these. |
| 735 if (DatabaseAvailable()) { | 757 if (DatabaseAvailable()) { |
| 736 closing_database_ = true; | 758 closing_database_ = true; |
| 737 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, | 759 safe_browsing_task_runner_->PostTask( |
| 760 FROM_HERE, |
| 738 base::Bind(&SafeBrowsingDatabaseManager::OnCloseDatabase, this)); | 761 base::Bind(&SafeBrowsingDatabaseManager::OnCloseDatabase, this)); |
| 739 } | 762 } |
| 740 | 763 |
| 741 // Flush the database thread. Any in-progress database check results will be | 764 // Flush the database thread. Any in-progress database check results will be |
| 742 // ignored and cleaned up below. | 765 // ignored and cleaned up below. |
| 743 // | 766 // |
| 744 // Note that to avoid leaking the database, we rely on the fact that no new | 767 // Note that to avoid leaking the database, we rely on the fact that no new |
| 745 // tasks will be added to the db thread between the call above and this one. | 768 // tasks will be added to the db thread between the call above and this one. |
| 746 // See comments on the declaration of |safe_browsing_thread_|. | 769 // See comments on the declaration of |safe_browsing_thread_|. |
| 747 { | 770 if (safe_browsing_thread_) { |
| 748 // A ScopedAllowIO object is required to join the thread when calling Stop. | 771 // A ScopedAllowIO object is required to join the thread when calling Stop. |
| 749 // See http://crbug.com/72696. Note that we call Stop() first to clear out | 772 // See http://crbug.com/72696. Note that we call Stop() first to clear out |
| 750 // any remaining tasks before clearing safe_browsing_thread_. | 773 // any remaining tasks before clearing safe_browsing_thread_. |
| 751 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; | 774 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; |
| 752 safe_browsing_thread_->Stop(); | 775 safe_browsing_thread_->Stop(); |
| 753 safe_browsing_thread_.reset(); | 776 safe_browsing_thread_.reset(); |
| 754 } | 777 } |
| 778 safe_browsing_task_runner_ = nullptr; |
| 755 | 779 |
| 756 // Delete pending checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'. | 780 // Delete pending checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'. |
| 757 // We have to do this after the db thread returns because methods on it can | 781 // We have to do this after the db thread returns because methods on it can |
| 758 // have copies of these pointers, so deleting them might lead to accessing | 782 // have copies of these pointers, so deleting them might lead to accessing |
| 759 // garbage. | 783 // garbage. |
| 760 for (CurrentChecks::iterator it = checks_.begin(); | 784 for (CurrentChecks::iterator it = checks_.begin(); |
| 761 it != checks_.end(); ++it) { | 785 it != checks_.end(); ++it) { |
| 762 SafeBrowsingCheck* check = *it; | 786 SafeBrowsingCheck* check = *it; |
| 763 if (check->client) | 787 if (check->client) |
| 764 check->client->OnSafeBrowsingResult(*check); | 788 check->client->OnSafeBrowsingResult(*check); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 779 | 803 |
| 780 bool SafeBrowsingDatabaseManager::MakeDatabaseAvailable() { | 804 bool SafeBrowsingDatabaseManager::MakeDatabaseAvailable() { |
| 781 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 805 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 782 DCHECK(enabled_); | 806 DCHECK(enabled_); |
| 783 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455469 is fixed. | 807 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455469 is fixed. |
| 784 tracked_objects::ScopedTracker tracking_profile2( | 808 tracked_objects::ScopedTracker tracking_profile2( |
| 785 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 809 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 786 "455469 SafeBrowsingDatabaseManager::MakeDatabaseAvailable")); | 810 "455469 SafeBrowsingDatabaseManager::MakeDatabaseAvailable")); |
| 787 if (DatabaseAvailable()) | 811 if (DatabaseAvailable()) |
| 788 return true; | 812 return true; |
| 789 safe_browsing_thread_->message_loop()->PostTask( | 813 safe_browsing_task_runner_->PostTask( |
| 790 FROM_HERE, | 814 FROM_HERE, |
| 791 base::Bind(base::IgnoreResult(&SafeBrowsingDatabaseManager::GetDatabase), | 815 base::Bind(base::IgnoreResult(&SafeBrowsingDatabaseManager::GetDatabase), |
| 792 this)); | 816 this)); |
| 793 return false; | 817 return false; |
| 794 } | 818 } |
| 795 | 819 |
| 796 SafeBrowsingDatabase* SafeBrowsingDatabaseManager::GetDatabase() { | 820 SafeBrowsingDatabase* SafeBrowsingDatabaseManager::GetDatabase() { |
| 797 DCHECK_EQ(base::MessageLoop::current(), | 821 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 798 safe_browsing_thread_->message_loop()); | 822 |
| 799 if (database_) | 823 if (database_) |
| 800 return database_; | 824 return database_; |
| 801 startup_metric_utils::ScopedSlowStartupUMA | 825 startup_metric_utils::ScopedSlowStartupUMA |
| 802 scoped_timer("Startup.SlowStartupSafeBrowsingGetDatabase"); | 826 scoped_timer("Startup.SlowStartupSafeBrowsingGetDatabase"); |
| 803 const base::TimeTicks before = base::TimeTicks::Now(); | 827 const base::TimeTicks before = base::TimeTicks::Now(); |
| 804 | 828 |
| 805 SafeBrowsingDatabase* database = | 829 SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create( |
| 806 SafeBrowsingDatabase::Create(enable_download_protection_, | 830 safe_browsing_task_runner_, enable_download_protection_, |
| 807 enable_csd_whitelist_, | 831 enable_csd_whitelist_, enable_download_whitelist_, |
| 808 enable_download_whitelist_, | 832 enable_extension_blacklist_, enable_side_effect_free_whitelist_, |
| 809 enable_extension_blacklist_, | 833 enable_ip_blacklist_, enable_unwanted_software_blacklist_); |
| 810 enable_side_effect_free_whitelist_, | |
| 811 enable_ip_blacklist_, | |
| 812 enable_unwanted_software_blacklist_); | |
| 813 | 834 |
| 814 database->Init(SafeBrowsingService::GetBaseFilename()); | 835 database->Init(SafeBrowsingService::GetBaseFilename()); |
| 815 { | 836 { |
| 816 // Acquiring the lock here guarantees correct ordering between the writes to | 837 // Acquiring the lock here guarantees correct ordering between the writes to |
| 817 // the new database object above, and the setting of |database_| below. | 838 // the new database object above, and the setting of |database_| below. |
| 818 base::AutoLock lock(database_lock_); | 839 base::AutoLock lock(database_lock_); |
| 819 database_ = database; | 840 database_ = database; |
| 820 } | 841 } |
| 821 | 842 |
| 822 BrowserThread::PostTask( | 843 BrowserThread::PostTask( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 is_download); | 895 is_download); |
| 875 } else { | 896 } else { |
| 876 // We may have cached results for previous GetHash queries. Since | 897 // We may have cached results for previous GetHash queries. Since |
| 877 // this data comes from cache, don't histogram hits. | 898 // this data comes from cache, don't histogram hits. |
| 878 HandleOneCheck(check, check->cache_hits); | 899 HandleOneCheck(check, check->cache_hits); |
| 879 } | 900 } |
| 880 } | 901 } |
| 881 | 902 |
| 882 void SafeBrowsingDatabaseManager::GetAllChunksFromDatabase( | 903 void SafeBrowsingDatabaseManager::GetAllChunksFromDatabase( |
| 883 GetChunksCallback callback) { | 904 GetChunksCallback callback) { |
| 884 DCHECK_EQ(base::MessageLoop::current(), | 905 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 885 safe_browsing_thread_->message_loop()); | |
| 886 | 906 |
| 887 bool database_error = true; | 907 bool database_error = true; |
| 888 std::vector<SBListChunkRanges> lists; | 908 std::vector<SBListChunkRanges> lists; |
| 889 DCHECK(!database_update_in_progress_); | 909 DCHECK(!database_update_in_progress_); |
| 890 database_update_in_progress_ = true; | 910 database_update_in_progress_ = true; |
| 891 GetDatabase(); // This guarantees that |database_| is non-NULL. | 911 GetDatabase(); // This guarantees that |database_| is non-NULL. |
| 892 if (database_->UpdateStarted(&lists)) { | 912 if (database_->UpdateStarted(&lists)) { |
| 893 database_error = false; | 913 database_error = false; |
| 894 } else { | 914 } else { |
| 895 database_->UpdateFinished(false); | 915 database_->UpdateFinished(false); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 945 check.client->OnSafeBrowsingResult(sb_check); | 965 check.client->OnSafeBrowsingResult(sb_check); |
| 946 } | 966 } |
| 947 queued_checks_.pop_front(); | 967 queued_checks_.pop_front(); |
| 948 } | 968 } |
| 949 } | 969 } |
| 950 | 970 |
| 951 void SafeBrowsingDatabaseManager::AddDatabaseChunks( | 971 void SafeBrowsingDatabaseManager::AddDatabaseChunks( |
| 952 const std::string& list_name, | 972 const std::string& list_name, |
| 953 scoped_ptr<ScopedVector<SBChunkData> > chunks, | 973 scoped_ptr<ScopedVector<SBChunkData> > chunks, |
| 954 AddChunksCallback callback) { | 974 AddChunksCallback callback) { |
| 955 DCHECK_EQ(base::MessageLoop::current(), | 975 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 956 safe_browsing_thread_->message_loop()); | |
| 957 if (chunks) | 976 if (chunks) |
| 958 GetDatabase()->InsertChunks(list_name, chunks->get()); | 977 GetDatabase()->InsertChunks(list_name, chunks->get()); |
| 959 BrowserThread::PostTask( | 978 BrowserThread::PostTask( |
| 960 BrowserThread::IO, FROM_HERE, | 979 BrowserThread::IO, FROM_HERE, |
| 961 base::Bind(&SafeBrowsingDatabaseManager::OnAddChunksComplete, this, | 980 base::Bind(&SafeBrowsingDatabaseManager::OnAddChunksComplete, this, |
| 962 callback)); | 981 callback)); |
| 963 } | 982 } |
| 964 | 983 |
| 965 void SafeBrowsingDatabaseManager::DeleteDatabaseChunks( | 984 void SafeBrowsingDatabaseManager::DeleteDatabaseChunks( |
| 966 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { | 985 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { |
| 967 DCHECK_EQ(base::MessageLoop::current(), | 986 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 968 safe_browsing_thread_->message_loop()); | |
| 969 if (chunk_deletes) | 987 if (chunk_deletes) |
| 970 GetDatabase()->DeleteChunks(*chunk_deletes); | 988 GetDatabase()->DeleteChunks(*chunk_deletes); |
| 971 } | 989 } |
| 972 | 990 |
| 973 void SafeBrowsingDatabaseManager::DatabaseUpdateFinished( | 991 void SafeBrowsingDatabaseManager::DatabaseUpdateFinished( |
| 974 bool update_succeeded) { | 992 bool update_succeeded) { |
| 975 DCHECK_EQ(base::MessageLoop::current(), | 993 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 976 safe_browsing_thread_->message_loop()); | |
| 977 GetDatabase()->UpdateFinished(update_succeeded); | 994 GetDatabase()->UpdateFinished(update_succeeded); |
| 978 DCHECK(database_update_in_progress_); | 995 DCHECK(database_update_in_progress_); |
| 979 database_update_in_progress_ = false; | 996 database_update_in_progress_ = false; |
| 980 BrowserThread::PostTask( | 997 BrowserThread::PostTask( |
| 981 BrowserThread::UI, FROM_HERE, | 998 BrowserThread::UI, FROM_HERE, |
| 982 base::Bind(&SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished, | 999 base::Bind(&SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished, |
| 983 this, update_succeeded)); | 1000 this, update_succeeded)); |
| 984 } | 1001 } |
| 985 | 1002 |
| 986 void SafeBrowsingDatabaseManager::OnCloseDatabase() { | 1003 void SafeBrowsingDatabaseManager::OnCloseDatabase() { |
| 987 DCHECK_EQ(base::MessageLoop::current(), | 1004 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 988 safe_browsing_thread_->message_loop()); | |
| 989 DCHECK(closing_database_); | 1005 DCHECK(closing_database_); |
| 990 | 1006 |
| 991 // Because |closing_database_| is true, nothing on the IO thread will be | 1007 // Because |closing_database_| is true, nothing on the IO thread will be |
| 992 // accessing the database, so it's safe to delete and then NULL the pointer. | 1008 // accessing the database, so it's safe to delete and then NULL the pointer. |
| 993 delete database_; | 1009 delete database_; |
| 994 database_ = NULL; | 1010 database_ = NULL; |
| 995 | 1011 |
| 996 // Acquiring the lock here guarantees correct ordering between the resetting | 1012 // Acquiring the lock here guarantees correct ordering between the resetting |
| 997 // of |database_| above and of |closing_database_| below, which ensures there | 1013 // of |database_| above and of |closing_database_| below, which ensures there |
| 998 // won't be a window during which the IO thread falsely believes the database | 1014 // won't be a window during which the IO thread falsely believes the database |
| 999 // is available. | 1015 // is available. |
| 1000 base::AutoLock lock(database_lock_); | 1016 base::AutoLock lock(database_lock_); |
| 1001 closing_database_ = false; | 1017 closing_database_ = false; |
| 1002 } | 1018 } |
| 1003 | 1019 |
| 1004 void SafeBrowsingDatabaseManager::OnResetDatabase() { | 1020 void SafeBrowsingDatabaseManager::OnResetDatabase() { |
| 1005 DCHECK_EQ(base::MessageLoop::current(), | 1021 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 1006 safe_browsing_thread_->message_loop()); | 1022 |
| 1007 GetDatabase()->ResetDatabase(); | 1023 GetDatabase()->ResetDatabase(); |
| 1008 } | 1024 } |
| 1009 | 1025 |
| 1010 void SafeBrowsingDatabaseManager::OnHandleGetHashResults( | 1026 void SafeBrowsingDatabaseManager::OnHandleGetHashResults( |
| 1011 SafeBrowsingCheck* check, | 1027 SafeBrowsingCheck* check, |
| 1012 const std::vector<SBFullHashResult>& full_hashes) { | 1028 const std::vector<SBFullHashResult>& full_hashes) { |
| 1013 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1029 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1014 safe_browsing_util::ListType check_type = check->check_type; | 1030 safe_browsing_util::ListType check_type = check->check_type; |
| 1015 SBPrefix prefix = check->prefix_hits[0]; | 1031 SBPrefix prefix = check->prefix_hits[0]; |
| 1016 GetHashRequests::iterator it = gethash_requests_.find(prefix); | 1032 GetHashRequests::iterator it = gethash_requests_.find(prefix); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1078 is_threat = true; | 1094 is_threat = true; |
| 1079 } | 1095 } |
| 1080 } | 1096 } |
| 1081 | 1097 |
| 1082 SafeBrowsingCheckDone(check); | 1098 SafeBrowsingCheckDone(check); |
| 1083 return is_threat; | 1099 return is_threat; |
| 1084 } | 1100 } |
| 1085 | 1101 |
| 1086 void SafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread( | 1102 void SafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread( |
| 1087 SafeBrowsingCheck* check) { | 1103 SafeBrowsingCheck* check) { |
| 1088 DCHECK_EQ(base::MessageLoop::current(), | 1104 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 1089 safe_browsing_thread_->message_loop()); | |
| 1090 DCHECK(enable_download_protection_); | 1105 DCHECK(enable_download_protection_); |
| 1091 | 1106 |
| 1092 std::vector<SBPrefix> prefix_hits; | 1107 std::vector<SBPrefix> prefix_hits; |
| 1093 | 1108 |
| 1094 if (!database_->ContainsDownloadUrl(check->urls, &prefix_hits)) { | 1109 if (!database_->ContainsDownloadUrl(check->urls, &prefix_hits)) { |
| 1095 // Good, we don't have hash for this url prefix. | 1110 // Good, we don't have hash for this url prefix. |
| 1096 BrowserThread::PostTask( | 1111 BrowserThread::PostTask( |
| 1097 BrowserThread::IO, FROM_HERE, | 1112 BrowserThread::IO, FROM_HERE, |
| 1098 base::Bind(&SafeBrowsingDatabaseManager::CheckDownloadUrlDone, this, | 1113 base::Bind(&SafeBrowsingDatabaseManager::CheckDownloadUrlDone, this, |
| 1099 check)); | 1114 check)); |
| 1100 return; | 1115 return; |
| 1101 } | 1116 } |
| 1102 | 1117 |
| 1103 check->need_get_hash = true; | 1118 check->need_get_hash = true; |
| 1104 check->prefix_hits.clear(); | 1119 check->prefix_hits.clear(); |
| 1105 check->prefix_hits = prefix_hits; | 1120 check->prefix_hits = prefix_hits; |
| 1106 BrowserThread::PostTask( | 1121 BrowserThread::PostTask( |
| 1107 BrowserThread::IO, FROM_HERE, | 1122 BrowserThread::IO, FROM_HERE, |
| 1108 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); | 1123 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); |
| 1109 } | 1124 } |
| 1110 | 1125 |
| 1111 void SafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread( | 1126 void SafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread( |
| 1112 SafeBrowsingCheck* check) { | 1127 SafeBrowsingCheck* check) { |
| 1113 DCHECK_EQ(base::MessageLoop::current(), | 1128 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
| 1114 safe_browsing_thread_->message_loop()); | |
| 1115 | 1129 |
| 1116 std::vector<SBPrefix> prefixes; | 1130 std::vector<SBPrefix> prefixes; |
| 1117 for (std::vector<SBFullHash>::iterator it = check->full_hashes.begin(); | 1131 for (std::vector<SBFullHash>::iterator it = check->full_hashes.begin(); |
| 1118 it != check->full_hashes.end(); ++it) { | 1132 it != check->full_hashes.end(); ++it) { |
| 1119 prefixes.push_back((*it).prefix); | 1133 prefixes.push_back((*it).prefix); |
| 1120 } | 1134 } |
| 1121 database_->ContainsExtensionPrefixes(prefixes, &check->prefix_hits); | 1135 database_->ContainsExtensionPrefixes(prefixes, &check->prefix_hits); |
| 1122 | 1136 |
| 1123 if (check->prefix_hits.empty()) { | 1137 if (check->prefix_hits.empty()) { |
| 1124 // No matches for any extensions. | 1138 // No matches for any extensions. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1175 } | 1189 } |
| 1176 | 1190 |
| 1177 void SafeBrowsingDatabaseManager::StartSafeBrowsingCheck( | 1191 void SafeBrowsingDatabaseManager::StartSafeBrowsingCheck( |
| 1178 SafeBrowsingCheck* check, | 1192 SafeBrowsingCheck* check, |
| 1179 const base::Closure& task) { | 1193 const base::Closure& task) { |
| 1180 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1194 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1181 check->timeout_factory_.reset( | 1195 check->timeout_factory_.reset( |
| 1182 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); | 1196 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); |
| 1183 checks_.insert(check); | 1197 checks_.insert(check); |
| 1184 | 1198 |
| 1185 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task); | 1199 safe_browsing_task_runner_->PostTask(FROM_HERE, task); |
| 1186 | 1200 |
| 1187 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 1201 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 1188 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, | 1202 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, |
| 1189 check->timeout_factory_->GetWeakPtr(), check), | 1203 check->timeout_factory_->GetWeakPtr(), check), |
| 1190 check_timeout_); | 1204 check_timeout_); |
| 1191 } | 1205 } |
| OLD | NEW |