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" |
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 return; | 590 return; |
591 | 591 |
592 closing_database_ = true; | 592 closing_database_ = true; |
593 if (safe_browsing_thread_.get()) { | 593 if (safe_browsing_thread_.get()) { |
594 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, | 594 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, |
595 base::Bind(&SafeBrowsingDatabaseManager::OnCloseDatabase, this)); | 595 base::Bind(&SafeBrowsingDatabaseManager::OnCloseDatabase, this)); |
596 } | 596 } |
597 } | 597 } |
598 | 598 |
599 SafeBrowsingDatabase* SafeBrowsingDatabaseManager::GetDatabase() { | 599 SafeBrowsingDatabase* SafeBrowsingDatabaseManager::GetDatabase() { |
600 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 600 DCHECK_EQ(base::MessageLoop::current(), |
| 601 safe_browsing_thread_->message_loop()); |
601 if (database_) | 602 if (database_) |
602 return database_; | 603 return database_; |
603 startup_metric_utils::ScopedSlowStartupUMA | 604 startup_metric_utils::ScopedSlowStartupUMA |
604 scoped_timer("Startup.SlowStartupSafeBrowsingGetDatabase"); | 605 scoped_timer("Startup.SlowStartupSafeBrowsingGetDatabase"); |
605 const base::TimeTicks before = base::TimeTicks::Now(); | 606 const base::TimeTicks before = base::TimeTicks::Now(); |
606 | 607 |
607 SafeBrowsingDatabase* database = | 608 SafeBrowsingDatabase* database = |
608 SafeBrowsingDatabase::Create(enable_download_protection_, | 609 SafeBrowsingDatabase::Create(enable_download_protection_, |
609 enable_csd_whitelist_, | 610 enable_csd_whitelist_, |
610 enable_download_whitelist_, | 611 enable_download_whitelist_, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 is_download); | 676 is_download); |
676 } else { | 677 } else { |
677 // We may have cached results for previous GetHash queries. Since | 678 // We may have cached results for previous GetHash queries. Since |
678 // this data comes from cache, don't histogram hits. | 679 // this data comes from cache, don't histogram hits. |
679 HandleOneCheck(check, check->full_hits); | 680 HandleOneCheck(check, check->full_hits); |
680 } | 681 } |
681 } | 682 } |
682 | 683 |
683 void SafeBrowsingDatabaseManager::GetAllChunksFromDatabase( | 684 void SafeBrowsingDatabaseManager::GetAllChunksFromDatabase( |
684 GetChunksCallback callback) { | 685 GetChunksCallback callback) { |
685 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 686 DCHECK_EQ(base::MessageLoop::current(), |
| 687 safe_browsing_thread_->message_loop()); |
686 | 688 |
687 bool database_error = true; | 689 bool database_error = true; |
688 std::vector<SBListChunkRanges> lists; | 690 std::vector<SBListChunkRanges> lists; |
689 DCHECK(!database_update_in_progress_); | 691 DCHECK(!database_update_in_progress_); |
690 database_update_in_progress_ = true; | 692 database_update_in_progress_ = true; |
691 GetDatabase(); // This guarantees that |database_| is non-NULL. | 693 GetDatabase(); // This guarantees that |database_| is non-NULL. |
692 if (database_->UpdateStarted(&lists)) { | 694 if (database_->UpdateStarted(&lists)) { |
693 database_error = false; | 695 database_error = false; |
694 } else { | 696 } else { |
695 database_->UpdateFinished(false); | 697 database_->UpdateFinished(false); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 check.check_type); | 744 check.check_type); |
743 check.client->OnSafeBrowsingResult(sb_check); | 745 check.client->OnSafeBrowsingResult(sb_check); |
744 } | 746 } |
745 queued_checks_.pop_front(); | 747 queued_checks_.pop_front(); |
746 } | 748 } |
747 } | 749 } |
748 | 750 |
749 void SafeBrowsingDatabaseManager::AddDatabaseChunks( | 751 void SafeBrowsingDatabaseManager::AddDatabaseChunks( |
750 const std::string& list_name, SBChunkList* chunks, | 752 const std::string& list_name, SBChunkList* chunks, |
751 AddChunksCallback callback) { | 753 AddChunksCallback callback) { |
752 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 754 DCHECK_EQ(base::MessageLoop::current(), |
| 755 safe_browsing_thread_->message_loop()); |
753 if (chunks) { | 756 if (chunks) { |
754 GetDatabase()->InsertChunks(list_name, *chunks); | 757 GetDatabase()->InsertChunks(list_name, *chunks); |
755 delete chunks; | 758 delete chunks; |
756 } | 759 } |
757 BrowserThread::PostTask( | 760 BrowserThread::PostTask( |
758 BrowserThread::IO, FROM_HERE, | 761 BrowserThread::IO, FROM_HERE, |
759 base::Bind(&SafeBrowsingDatabaseManager::OnAddChunksComplete, this, | 762 base::Bind(&SafeBrowsingDatabaseManager::OnAddChunksComplete, this, |
760 callback)); | 763 callback)); |
761 } | 764 } |
762 | 765 |
763 void SafeBrowsingDatabaseManager::DeleteDatabaseChunks( | 766 void SafeBrowsingDatabaseManager::DeleteDatabaseChunks( |
764 std::vector<SBChunkDelete>* chunk_deletes) { | 767 std::vector<SBChunkDelete>* chunk_deletes) { |
765 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 768 DCHECK_EQ(base::MessageLoop::current(), |
| 769 safe_browsing_thread_->message_loop()); |
766 if (chunk_deletes) { | 770 if (chunk_deletes) { |
767 GetDatabase()->DeleteChunks(*chunk_deletes); | 771 GetDatabase()->DeleteChunks(*chunk_deletes); |
768 delete chunk_deletes; | 772 delete chunk_deletes; |
769 } | 773 } |
770 } | 774 } |
771 | 775 |
772 SBThreatType SafeBrowsingDatabaseManager::GetThreatTypeFromListname( | 776 SBThreatType SafeBrowsingDatabaseManager::GetThreatTypeFromListname( |
773 const std::string& list_name) { | 777 const std::string& list_name) { |
774 if (safe_browsing_util::IsPhishingList(list_name)) { | 778 if (safe_browsing_util::IsPhishingList(list_name)) { |
775 return SB_THREAT_TYPE_URL_PHISHING; | 779 return SB_THREAT_TYPE_URL_PHISHING; |
(...skipping 14 matching lines...) Expand all Loading... |
790 if (safe_browsing_util::IsExtensionList(list_name)) { | 794 if (safe_browsing_util::IsExtensionList(list_name)) { |
791 return SB_THREAT_TYPE_EXTENSION; | 795 return SB_THREAT_TYPE_EXTENSION; |
792 } | 796 } |
793 | 797 |
794 DVLOG(1) << "Unknown safe browsing list " << list_name; | 798 DVLOG(1) << "Unknown safe browsing list " << list_name; |
795 return SB_THREAT_TYPE_SAFE; | 799 return SB_THREAT_TYPE_SAFE; |
796 } | 800 } |
797 | 801 |
798 void SafeBrowsingDatabaseManager::DatabaseUpdateFinished( | 802 void SafeBrowsingDatabaseManager::DatabaseUpdateFinished( |
799 bool update_succeeded) { | 803 bool update_succeeded) { |
800 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 804 DCHECK_EQ(base::MessageLoop::current(), |
| 805 safe_browsing_thread_->message_loop()); |
801 GetDatabase()->UpdateFinished(update_succeeded); | 806 GetDatabase()->UpdateFinished(update_succeeded); |
802 DCHECK(database_update_in_progress_); | 807 DCHECK(database_update_in_progress_); |
803 database_update_in_progress_ = false; | 808 database_update_in_progress_ = false; |
804 BrowserThread::PostTask( | 809 BrowserThread::PostTask( |
805 BrowserThread::UI, FROM_HERE, | 810 BrowserThread::UI, FROM_HERE, |
806 base::Bind(&SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished, | 811 base::Bind(&SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished, |
807 this, update_succeeded)); | 812 this, update_succeeded)); |
808 } | 813 } |
809 | 814 |
810 void SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished( | 815 void SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished( |
811 bool update_succeeded) { | 816 bool update_succeeded) { |
812 content::NotificationService::current()->Notify( | 817 content::NotificationService::current()->Notify( |
813 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 818 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
814 content::Source<SafeBrowsingDatabaseManager>(this), | 819 content::Source<SafeBrowsingDatabaseManager>(this), |
815 content::Details<bool>(&update_succeeded)); | 820 content::Details<bool>(&update_succeeded)); |
816 } | 821 } |
817 | 822 |
818 void SafeBrowsingDatabaseManager::OnCloseDatabase() { | 823 void SafeBrowsingDatabaseManager::OnCloseDatabase() { |
819 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 824 DCHECK_EQ(base::MessageLoop::current(), |
| 825 safe_browsing_thread_->message_loop()); |
820 DCHECK(closing_database_); | 826 DCHECK(closing_database_); |
821 | 827 |
822 // Because |closing_database_| is true, nothing on the IO thread will be | 828 // Because |closing_database_| is true, nothing on the IO thread will be |
823 // accessing the database, so it's safe to delete and then NULL the pointer. | 829 // accessing the database, so it's safe to delete and then NULL the pointer. |
824 delete database_; | 830 delete database_; |
825 database_ = NULL; | 831 database_ = NULL; |
826 | 832 |
827 // Acquiring the lock here guarantees correct ordering between the resetting | 833 // Acquiring the lock here guarantees correct ordering between the resetting |
828 // of |database_| above and of |closing_database_| below, which ensures there | 834 // of |database_| above and of |closing_database_| below, which ensures there |
829 // won't be a window during which the IO thread falsely believes the database | 835 // won't be a window during which the IO thread falsely believes the database |
830 // is available. | 836 // is available. |
831 base::AutoLock lock(database_lock_); | 837 base::AutoLock lock(database_lock_); |
832 closing_database_ = false; | 838 closing_database_ = false; |
833 } | 839 } |
834 | 840 |
835 void SafeBrowsingDatabaseManager::OnResetDatabase() { | 841 void SafeBrowsingDatabaseManager::OnResetDatabase() { |
836 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 842 DCHECK_EQ(base::MessageLoop::current(), |
| 843 safe_browsing_thread_->message_loop()); |
837 GetDatabase()->ResetDatabase(); | 844 GetDatabase()->ResetDatabase(); |
838 } | 845 } |
839 | 846 |
840 void SafeBrowsingDatabaseManager::CacheHashResults( | 847 void SafeBrowsingDatabaseManager::CacheHashResults( |
841 const std::vector<SBPrefix>& prefixes, | 848 const std::vector<SBPrefix>& prefixes, |
842 const std::vector<SBFullHashResult>& full_hashes) { | 849 const std::vector<SBFullHashResult>& full_hashes) { |
843 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 850 DCHECK_EQ(base::MessageLoop::current(), |
| 851 safe_browsing_thread_->message_loop()); |
844 GetDatabase()->CacheHashResults(prefixes, full_hashes); | 852 GetDatabase()->CacheHashResults(prefixes, full_hashes); |
845 } | 853 } |
846 | 854 |
847 void SafeBrowsingDatabaseManager::OnHandleGetHashResults( | 855 void SafeBrowsingDatabaseManager::OnHandleGetHashResults( |
848 SafeBrowsingCheck* check, | 856 SafeBrowsingCheck* check, |
849 const std::vector<SBFullHashResult>& full_hashes) { | 857 const std::vector<SBFullHashResult>& full_hashes) { |
850 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 858 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
851 safe_browsing_util::ListType check_type = check->check_type; | 859 safe_browsing_util::ListType check_type = check->check_type; |
852 SBPrefix prefix = check->prefix_hits[0]; | 860 SBPrefix prefix = check->prefix_hits[0]; |
853 GetHashRequests::iterator it = gethash_requests_.find(prefix); | 861 GetHashRequests::iterator it = gethash_requests_.find(prefix); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 NOTREACHED(); | 915 NOTREACHED(); |
908 } | 916 } |
909 } | 917 } |
910 | 918 |
911 SafeBrowsingCheckDone(check); | 919 SafeBrowsingCheckDone(check); |
912 return is_threat; | 920 return is_threat; |
913 } | 921 } |
914 | 922 |
915 void SafeBrowsingDatabaseManager::CheckDownloadHashOnSBThread( | 923 void SafeBrowsingDatabaseManager::CheckDownloadHashOnSBThread( |
916 SafeBrowsingCheck* check) { | 924 SafeBrowsingCheck* check) { |
917 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 925 DCHECK_EQ(base::MessageLoop::current(), |
| 926 safe_browsing_thread_->message_loop()); |
918 DCHECK(enable_download_protection_); | 927 DCHECK(enable_download_protection_); |
919 | 928 |
920 DCHECK_EQ(1u, check->full_hashes.size()); | 929 DCHECK_EQ(1u, check->full_hashes.size()); |
921 SBFullHash full_hash = check->full_hashes[0]; | 930 SBFullHash full_hash = check->full_hashes[0]; |
922 | 931 |
923 if (!database_->ContainsDownloadHashPrefix(full_hash.prefix)) { | 932 if (!database_->ContainsDownloadHashPrefix(full_hash.prefix)) { |
924 // Good, we don't have hash for this url prefix. | 933 // Good, we don't have hash for this url prefix. |
925 BrowserThread::PostTask( | 934 BrowserThread::PostTask( |
926 BrowserThread::IO, FROM_HERE, | 935 BrowserThread::IO, FROM_HERE, |
927 base::Bind(&SafeBrowsingDatabaseManager::CheckDownloadHashDone, this, | 936 base::Bind(&SafeBrowsingDatabaseManager::CheckDownloadHashDone, this, |
928 check)); | 937 check)); |
929 return; | 938 return; |
930 } | 939 } |
931 | 940 |
932 check->need_get_hash = true; | 941 check->need_get_hash = true; |
933 check->prefix_hits.push_back(full_hash.prefix); | 942 check->prefix_hits.push_back(full_hash.prefix); |
934 BrowserThread::PostTask( | 943 BrowserThread::PostTask( |
935 BrowserThread::IO, FROM_HERE, | 944 BrowserThread::IO, FROM_HERE, |
936 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); | 945 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); |
937 } | 946 } |
938 | 947 |
939 void SafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread( | 948 void SafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread( |
940 SafeBrowsingCheck* check) { | 949 SafeBrowsingCheck* check) { |
941 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 950 DCHECK_EQ(base::MessageLoop::current(), |
| 951 safe_browsing_thread_->message_loop()); |
942 DCHECK(enable_download_protection_); | 952 DCHECK(enable_download_protection_); |
943 | 953 |
944 std::vector<SBPrefix> prefix_hits; | 954 std::vector<SBPrefix> prefix_hits; |
945 | 955 |
946 if (!database_->ContainsDownloadUrl(check->urls, &prefix_hits)) { | 956 if (!database_->ContainsDownloadUrl(check->urls, &prefix_hits)) { |
947 // Good, we don't have hash for this url prefix. | 957 // Good, we don't have hash for this url prefix. |
948 BrowserThread::PostTask( | 958 BrowserThread::PostTask( |
949 BrowserThread::IO, FROM_HERE, | 959 BrowserThread::IO, FROM_HERE, |
950 base::Bind(&SafeBrowsingDatabaseManager::CheckDownloadUrlDone, this, | 960 base::Bind(&SafeBrowsingDatabaseManager::CheckDownloadUrlDone, this, |
951 check)); | 961 check)); |
952 return; | 962 return; |
953 } | 963 } |
954 | 964 |
955 check->need_get_hash = true; | 965 check->need_get_hash = true; |
956 check->prefix_hits.clear(); | 966 check->prefix_hits.clear(); |
957 check->prefix_hits = prefix_hits; | 967 check->prefix_hits = prefix_hits; |
958 BrowserThread::PostTask( | 968 BrowserThread::PostTask( |
959 BrowserThread::IO, FROM_HERE, | 969 BrowserThread::IO, FROM_HERE, |
960 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); | 970 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); |
961 } | 971 } |
962 | 972 |
963 void SafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread( | 973 void SafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread( |
964 SafeBrowsingCheck* check) { | 974 SafeBrowsingCheck* check) { |
965 DCHECK_EQ(MessageLoop::current(), safe_browsing_thread_->message_loop()); | 975 DCHECK_EQ(base::MessageLoop::current(), |
| 976 safe_browsing_thread_->message_loop()); |
966 | 977 |
967 std::vector<SBPrefix> prefixes; | 978 std::vector<SBPrefix> prefixes; |
968 for (std::vector<SBFullHash>::iterator it = check->full_hashes.begin(); | 979 for (std::vector<SBFullHash>::iterator it = check->full_hashes.begin(); |
969 it != check->full_hashes.end(); ++it) { | 980 it != check->full_hashes.end(); ++it) { |
970 prefixes.push_back((*it).prefix); | 981 prefixes.push_back((*it).prefix); |
971 } | 982 } |
972 database_->ContainsExtensionPrefixes(prefixes, &check->prefix_hits); | 983 database_->ContainsExtensionPrefixes(prefixes, &check->prefix_hits); |
973 | 984 |
974 if (check->prefix_hits.empty()) { | 985 if (check->prefix_hits.empty()) { |
975 // No matches for any extensions. | 986 // No matches for any extensions. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1033 void SafeBrowsingDatabaseManager::StartSafeBrowsingCheck( | 1044 void SafeBrowsingDatabaseManager::StartSafeBrowsingCheck( |
1034 SafeBrowsingCheck* check, | 1045 SafeBrowsingCheck* check, |
1035 const base::Closure& task) { | 1046 const base::Closure& task) { |
1036 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1047 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
1037 check->timeout_factory_.reset( | 1048 check->timeout_factory_.reset( |
1038 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); | 1049 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); |
1039 checks_.insert(check); | 1050 checks_.insert(check); |
1040 | 1051 |
1041 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task); | 1052 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task); |
1042 | 1053 |
1043 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 1054 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
1044 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, | 1055 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, |
1045 check->timeout_factory_->GetWeakPtr(), check), | 1056 check->timeout_factory_->GetWeakPtr(), check), |
1046 check_timeout_); | 1057 check_timeout_); |
1047 } | 1058 } |
OLD | NEW |