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

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

Issue 744183002: More explicit thread checking in SafeBrowsingDatabase. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@a3_deadcode
Patch Set: extract race fix to CL#793663003 Created 6 years 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/safe_browsing/safe_browsing_database.h" 5 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 return base::FilePath(db_filename.value() + kIPBlacklistDBFile); 425 return base::FilePath(db_filename.value() + kIPBlacklistDBFile);
426 } 426 }
427 427
428 // static 428 // static
429 base::FilePath SafeBrowsingDatabase::UnwantedSoftwareDBFilename( 429 base::FilePath SafeBrowsingDatabase::UnwantedSoftwareDBFilename(
430 const base::FilePath& db_filename) { 430 const base::FilePath& db_filename) {
431 return base::FilePath(db_filename.value() + kUnwantedSoftwareDBFile); 431 return base::FilePath(db_filename.value() + kUnwantedSoftwareDBFile);
432 } 432 }
433 433
434 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { 434 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) {
435 // Stores are not thread safe.
436 DCHECK(thread_checker_.CalledOnValidThread());
437
435 if (list_id == safe_browsing_util::PHISH || 438 if (list_id == safe_browsing_util::PHISH ||
436 list_id == safe_browsing_util::MALWARE) { 439 list_id == safe_browsing_util::MALWARE) {
437 return browse_store_.get(); 440 return browse_store_.get();
438 } else if (list_id == safe_browsing_util::BINURL) { 441 } else if (list_id == safe_browsing_util::BINURL) {
439 return download_store_.get(); 442 return download_store_.get();
440 } else if (list_id == safe_browsing_util::CSDWHITELIST) { 443 } else if (list_id == safe_browsing_util::CSDWHITELIST) {
441 return csd_whitelist_store_.get(); 444 return csd_whitelist_store_.get();
442 } else if (list_id == safe_browsing_util::DOWNLOADWHITELIST) { 445 } else if (list_id == safe_browsing_util::DOWNLOADWHITELIST) {
443 return download_whitelist_store_.get(); 446 return download_whitelist_store_.get();
444 } else if (list_id == safe_browsing_util::EXTENSIONBLACKLIST) { 447 } else if (list_id == safe_browsing_util::EXTENSIONBLACKLIST) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 483
481 SafeBrowsingDatabaseNew::SafeBrowsingDatabaseNew( 484 SafeBrowsingDatabaseNew::SafeBrowsingDatabaseNew(
482 SafeBrowsingStore* browse_store, 485 SafeBrowsingStore* browse_store,
483 SafeBrowsingStore* download_store, 486 SafeBrowsingStore* download_store,
484 SafeBrowsingStore* csd_whitelist_store, 487 SafeBrowsingStore* csd_whitelist_store,
485 SafeBrowsingStore* download_whitelist_store, 488 SafeBrowsingStore* download_whitelist_store,
486 SafeBrowsingStore* extension_blacklist_store, 489 SafeBrowsingStore* extension_blacklist_store,
487 SafeBrowsingStore* side_effect_free_whitelist_store, 490 SafeBrowsingStore* side_effect_free_whitelist_store,
488 SafeBrowsingStore* ip_blacklist_store, 491 SafeBrowsingStore* ip_blacklist_store,
489 SafeBrowsingStore* unwanted_software_store) 492 SafeBrowsingStore* unwanted_software_store)
490 : creation_loop_(base::MessageLoop::current()), 493 : browse_store_(browse_store),
491 browse_store_(browse_store),
492 download_store_(download_store), 494 download_store_(download_store),
493 csd_whitelist_store_(csd_whitelist_store), 495 csd_whitelist_store_(csd_whitelist_store),
494 download_whitelist_store_(download_whitelist_store), 496 download_whitelist_store_(download_whitelist_store),
495 extension_blacklist_store_(extension_blacklist_store), 497 extension_blacklist_store_(extension_blacklist_store),
496 side_effect_free_whitelist_store_(side_effect_free_whitelist_store), 498 side_effect_free_whitelist_store_(side_effect_free_whitelist_store),
497 ip_blacklist_store_(ip_blacklist_store), 499 ip_blacklist_store_(ip_blacklist_store),
498 unwanted_software_store_(unwanted_software_store), 500 unwanted_software_store_(unwanted_software_store),
499 corruption_detected_(false), 501 corruption_detected_(false),
500 change_detected_(false), 502 change_detected_(false),
501 reset_factory_(this) { 503 reset_factory_(this) {
502 DCHECK(browse_store_.get()); 504 DCHECK(browse_store_.get());
503 } 505 }
504 506
505 SafeBrowsingDatabaseNew::~SafeBrowsingDatabaseNew() { 507 SafeBrowsingDatabaseNew::~SafeBrowsingDatabaseNew() {
506 // The DCHECK is disabled due to crbug.com/338486 . 508 // The DCHECK is disabled due to crbug.com/338486 .
507 // DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 509 // DCHECK(thread_checker_.CalledOnValidThread());
508 } 510 }
509 511
510 void SafeBrowsingDatabaseNew::Init(const base::FilePath& filename_base) { 512 void SafeBrowsingDatabaseNew::Init(const base::FilePath& filename_base) {
511 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 513 DCHECK(thread_checker_.CalledOnValidThread());
512 514
513 // This should not be run multiple times. 515 // This should not be run multiple times.
514 DCHECK(filename_base_.empty()); 516 DCHECK(filename_base_.empty());
515 517
516 filename_base_ = filename_base; 518 filename_base_ = filename_base;
517 519
518 // TODO(shess): The various stores are really only necessary while doing 520 // TODO(shess): The various stores are really only necessary while doing
519 // updates (see |UpdateFinished()|) or when querying a store directly (see 521 // updates (see |UpdateFinished()|) or when querying a store directly (see
520 // |ContainsDownloadUrl()|). 522 // |ContainsDownloadUrl()|).
521 // The store variables are also tested to see if a list is enabled. Perhaps 523 // The store variables are also tested to see if a list is enabled. Perhaps
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 std::vector<SBAddFullHash> full_hashes; 640 std::vector<SBAddFullHash> full_hashes;
639 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) { 641 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) {
640 LoadIpBlacklist(full_hashes); 642 LoadIpBlacklist(full_hashes);
641 } else { 643 } else {
642 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. 644 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list.
643 } 645 }
644 } 646 }
645 } 647 }
646 648
647 bool SafeBrowsingDatabaseNew::ResetDatabase() { 649 bool SafeBrowsingDatabaseNew::ResetDatabase() {
648 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 650 DCHECK(thread_checker_.CalledOnValidThread());
649 651
650 // Delete files on disk. 652 // Delete files on disk.
651 // TODO(shess): Hard to see where one might want to delete without a 653 // TODO(shess): Hard to see where one might want to delete without a
652 // reset. Perhaps inline |Delete()|? 654 // reset. Perhaps inline |Delete()|?
653 if (!Delete()) 655 if (!Delete())
654 return false; 656 return false;
655 657
656 // Reset objects in memory. 658 // Reset objects in memory.
657 { 659 {
658 base::AutoLock locked(lookup_lock_); 660 base::AutoLock locked(lookup_lock_);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 } 713 }
712 714
713 bool SafeBrowsingDatabaseNew::PrefixSetContainsUrlHashes( 715 bool SafeBrowsingDatabaseNew::PrefixSetContainsUrlHashes(
714 const std::vector<SBFullHash>& full_hashes, 716 const std::vector<SBFullHash>& full_hashes,
715 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set_getter, 717 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set_getter,
716 std::vector<SBPrefix>* prefix_hits, 718 std::vector<SBPrefix>* prefix_hits,
717 std::vector<SBFullHashResult>* cache_hits) { 719 std::vector<SBFullHashResult>* cache_hits) {
718 // Used to determine cache expiration. 720 // Used to determine cache expiration.
719 const base::Time now = base::Time::Now(); 721 const base::Time now = base::Time::Now();
720 722
721 // This function is called on the I/O thread, prevent changes to 723 // This function can be called on any thread, so lock against any changes
722 // filter and caches.
723 base::AutoLock locked(lookup_lock_); 724 base::AutoLock locked(lookup_lock_);
724 725
725 // |prefix_set| is empty until it is either read from disk, or the first 726 // |prefix_set| is empty until it is either read from disk, or the first
726 // update populates it. Bail out without a hit if not yet available. 727 // update populates it. Bail out without a hit if not yet available.
727 // |prefix_set_getter| can only be accessed while holding |lookup_lock_| hence 728 // |prefix_set_getter| can only be accessed while holding |lookup_lock_| hence
728 // why it is passed as a parameter rather than passing the |prefix_set| 729 // why it is passed as a parameter rather than passing the |prefix_set|
729 // directly. 730 // directly.
730 const safe_browsing::PrefixSet* prefix_set = prefix_set_getter->get(); 731 const safe_browsing::PrefixSet* prefix_set = prefix_set_getter->get();
731 if (!prefix_set) 732 if (!prefix_set)
732 return false; 733 return false;
(...skipping 11 matching lines...) Expand all
744 std::sort(prefix_hits->begin(), prefix_hits->end()); 745 std::sort(prefix_hits->begin(), prefix_hits->end());
745 prefix_hits->erase(std::unique(prefix_hits->begin(), prefix_hits->end()), 746 prefix_hits->erase(std::unique(prefix_hits->begin(), prefix_hits->end()),
746 prefix_hits->end()); 747 prefix_hits->end());
747 748
748 return !prefix_hits->empty() || !cache_hits->empty(); 749 return !prefix_hits->empty() || !cache_hits->empty();
749 } 750 }
750 751
751 bool SafeBrowsingDatabaseNew::ContainsDownloadUrl( 752 bool SafeBrowsingDatabaseNew::ContainsDownloadUrl(
752 const std::vector<GURL>& urls, 753 const std::vector<GURL>& urls,
753 std::vector<SBPrefix>* prefix_hits) { 754 std::vector<SBPrefix>* prefix_hits) {
754 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 755 DCHECK(thread_checker_.CalledOnValidThread());
755 756
756 // Ignore this check when download checking is not enabled. 757 // Ignore this check when download checking is not enabled.
757 if (!download_store_.get()) 758 if (!download_store_.get())
758 return false; 759 return false;
759 760
760 std::vector<SBPrefix> prefixes; 761 std::vector<SBPrefix> prefixes;
761 GetDownloadUrlPrefixes(urls, &prefixes); 762 GetDownloadUrlPrefixes(urls, &prefixes);
762 return MatchAddPrefixes(download_store_.get(), 763 return MatchAddPrefixes(download_store_.get(),
763 safe_browsing_util::BINURL % 2, 764 safe_browsing_util::BINURL % 2,
764 prefixes, 765 prefixes,
765 prefix_hits); 766 prefix_hits);
766 } 767 }
767 768
768 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { 769 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) {
769 // This method is theoretically thread-safe but we expect all calls to
770 // originate from the IO thread.
771 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
772 std::vector<SBFullHash> full_hashes; 770 std::vector<SBFullHash> full_hashes;
773 UrlToFullHashes(url, true, &full_hashes); 771 UrlToFullHashes(url, true, &full_hashes);
774 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes); 772 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes);
775 } 773 }
776 774
777 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedUrl(const GURL& url) { 775 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedUrl(const GURL& url) {
778 std::vector<SBFullHash> full_hashes; 776 std::vector<SBFullHash> full_hashes;
779 UrlToFullHashes(url, true, &full_hashes); 777 UrlToFullHashes(url, true, &full_hashes);
780 return ContainsWhitelistedHashes(download_whitelist_, full_hashes); 778 return ContainsWhitelistedHashes(download_whitelist_, full_hashes);
781 } 779 }
782 780
783 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( 781 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes(
784 const std::vector<SBPrefix>& prefixes, 782 const std::vector<SBPrefix>& prefixes,
785 std::vector<SBPrefix>* prefix_hits) { 783 std::vector<SBPrefix>* prefix_hits) {
786 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 784 DCHECK(thread_checker_.CalledOnValidThread());
785
787 if (!extension_blacklist_store_) 786 if (!extension_blacklist_store_)
788 return false; 787 return false;
789 788
790 return MatchAddPrefixes(extension_blacklist_store_.get(), 789 return MatchAddPrefixes(extension_blacklist_store_.get(),
791 safe_browsing_util::EXTENSIONBLACKLIST % 2, 790 safe_browsing_util::EXTENSIONBLACKLIST % 2,
792 prefixes, 791 prefixes,
793 prefix_hits); 792 prefix_hits);
794 } 793 }
795 794
796 bool SafeBrowsingDatabaseNew::ContainsSideEffectFreeWhitelistUrl( 795 bool SafeBrowsingDatabaseNew::ContainsSideEffectFreeWhitelistUrl(
(...skipping 21 matching lines...) Expand all
818 817
819 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { 818 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) {
820 net::IPAddressNumber ip_number; 819 net::IPAddressNumber ip_number;
821 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) 820 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number))
822 return false; 821 return false;
823 if (ip_number.size() == net::kIPv4AddressSize) 822 if (ip_number.size() == net::kIPv4AddressSize)
824 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); 823 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number);
825 if (ip_number.size() != net::kIPv6AddressSize) 824 if (ip_number.size() != net::kIPv6AddressSize)
826 return false; // better safe than sorry. 825 return false; // better safe than sorry.
827 826
828 // This function can be called from any thread. 827 // This function can be called on any thread, so lock against any changes
829 base::AutoLock locked(lookup_lock_); 828 base::AutoLock locked(lookup_lock_);
830 for (IPBlacklist::const_iterator it = ip_blacklist_.begin(); 829 for (IPBlacklist::const_iterator it = ip_blacklist_.begin();
831 it != ip_blacklist_.end(); 830 it != ip_blacklist_.end();
832 ++it) { 831 ++it) {
833 const std::string& mask = it->first; 832 const std::string& mask = it->first;
834 DCHECK_EQ(mask.size(), ip_number.size()); 833 DCHECK_EQ(mask.size(), ip_number.size());
835 std::string subnet(net::kIPv6AddressSize, '\0'); 834 std::string subnet(net::kIPv6AddressSize, '\0');
836 for (size_t i = 0; i < net::kIPv6AddressSize; ++i) { 835 for (size_t i = 0; i < net::kIPv6AddressSize; ++i) {
837 subnet[i] = ip_number[i] & mask[i]; 836 subnet[i] = ip_number[i] & mask[i];
838 } 837 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 } 869 }
871 } 870 }
872 return false; 871 return false;
873 } 872 }
874 873
875 // Helper to insert add-chunk entries. 874 // Helper to insert add-chunk entries.
876 void SafeBrowsingDatabaseNew::InsertAddChunk( 875 void SafeBrowsingDatabaseNew::InsertAddChunk(
877 SafeBrowsingStore* store, 876 SafeBrowsingStore* store,
878 const safe_browsing_util::ListType list_id, 877 const safe_browsing_util::ListType list_id,
879 const SBChunkData& chunk_data) { 878 const SBChunkData& chunk_data) {
880 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 879 DCHECK(thread_checker_.CalledOnValidThread());
881 DCHECK(store); 880 DCHECK(store);
882 881
883 // The server can give us a chunk that we already have because 882 // The server can give us a chunk that we already have because
884 // it's part of a range. Don't add it again. 883 // it's part of a range. Don't add it again.
885 const int chunk_id = chunk_data.ChunkNumber(); 884 const int chunk_id = chunk_data.ChunkNumber();
886 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); 885 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id);
887 if (store->CheckAddChunk(encoded_chunk_id)) 886 if (store->CheckAddChunk(encoded_chunk_id))
888 return; 887 return;
889 888
890 store->SetAddChunk(encoded_chunk_id); 889 store->SetAddChunk(encoded_chunk_id);
(...skipping 10 matching lines...) Expand all
901 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); 900 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i));
902 } 901 }
903 } 902 }
904 } 903 }
905 904
906 // Helper to insert sub-chunk entries. 905 // Helper to insert sub-chunk entries.
907 void SafeBrowsingDatabaseNew::InsertSubChunk( 906 void SafeBrowsingDatabaseNew::InsertSubChunk(
908 SafeBrowsingStore* store, 907 SafeBrowsingStore* store,
909 const safe_browsing_util::ListType list_id, 908 const safe_browsing_util::ListType list_id,
910 const SBChunkData& chunk_data) { 909 const SBChunkData& chunk_data) {
911 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 910 DCHECK(thread_checker_.CalledOnValidThread());
912 DCHECK(store); 911 DCHECK(store);
913 912
914 // The server can give us a chunk that we already have because 913 // The server can give us a chunk that we already have because
915 // it's part of a range. Don't add it again. 914 // it's part of a range. Don't add it again.
916 const int chunk_id = chunk_data.ChunkNumber(); 915 const int chunk_id = chunk_data.ChunkNumber();
917 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); 916 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id);
918 if (store->CheckSubChunk(encoded_chunk_id)) 917 if (store->CheckSubChunk(encoded_chunk_id))
919 return; 918 return;
920 919
921 store->SetSubChunk(encoded_chunk_id); 920 store->SetSubChunk(encoded_chunk_id);
(...skipping 14 matching lines...) Expand all
936 const int encoded_add_chunk_id = EncodeChunkId(add_chunk_id, list_id); 935 const int encoded_add_chunk_id = EncodeChunkId(add_chunk_id, list_id);
937 store->WriteSubHash(encoded_chunk_id, encoded_add_chunk_id, 936 store->WriteSubHash(encoded_chunk_id, encoded_add_chunk_id,
938 chunk_data.FullHashAt(i)); 937 chunk_data.FullHashAt(i));
939 } 938 }
940 } 939 }
941 } 940 }
942 941
943 void SafeBrowsingDatabaseNew::InsertChunks( 942 void SafeBrowsingDatabaseNew::InsertChunks(
944 const std::string& list_name, 943 const std::string& list_name,
945 const std::vector<SBChunkData*>& chunks) { 944 const std::vector<SBChunkData*>& chunks) {
946 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 945 DCHECK(thread_checker_.CalledOnValidThread());
947 946
948 if (corruption_detected_ || chunks.empty()) 947 if (corruption_detected_ || chunks.empty())
949 return; 948 return;
950 949
951 const base::TimeTicks before = base::TimeTicks::Now(); 950 const base::TimeTicks before = base::TimeTicks::Now();
952 951
953 // TODO(shess): The caller should just pass list_id. 952 // TODO(shess): The caller should just pass list_id.
954 const safe_browsing_util::ListType list_id = 953 const safe_browsing_util::ListType list_id =
955 safe_browsing_util::GetListId(list_name); 954 safe_browsing_util::GetListId(list_name);
956 955
(...skipping 14 matching lines...) Expand all
971 NOTREACHED(); 970 NOTREACHED();
972 } 971 }
973 } 972 }
974 store->FinishChunk(); 973 store->FinishChunk();
975 974
976 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before); 975 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before);
977 } 976 }
978 977
979 void SafeBrowsingDatabaseNew::DeleteChunks( 978 void SafeBrowsingDatabaseNew::DeleteChunks(
980 const std::vector<SBChunkDelete>& chunk_deletes) { 979 const std::vector<SBChunkDelete>& chunk_deletes) {
981 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 980 DCHECK(thread_checker_.CalledOnValidThread());
982 981
983 if (corruption_detected_ || chunk_deletes.empty()) 982 if (corruption_detected_ || chunk_deletes.empty())
984 return; 983 return;
985 984
986 const std::string& list_name = chunk_deletes.front().list_name; 985 const std::string& list_name = chunk_deletes.front().list_name;
987 const safe_browsing_util::ListType list_id = 986 const safe_browsing_util::ListType list_id =
988 safe_browsing_util::GetListId(list_name); 987 safe_browsing_util::GetListId(list_name);
989 988
990 SafeBrowsingStore* store = GetStore(list_id); 989 SafeBrowsingStore* store = GetStore(list_id);
991 if (!store) return; 990 if (!store) return;
(...skipping 12 matching lines...) Expand all
1004 } 1003 }
1005 } 1004 }
1006 } 1005 }
1007 1006
1008 void SafeBrowsingDatabaseNew::CacheHashResults( 1007 void SafeBrowsingDatabaseNew::CacheHashResults(
1009 const std::vector<SBPrefix>& prefixes, 1008 const std::vector<SBPrefix>& prefixes,
1010 const std::vector<SBFullHashResult>& full_hits, 1009 const std::vector<SBFullHashResult>& full_hits,
1011 const base::TimeDelta& cache_lifetime) { 1010 const base::TimeDelta& cache_lifetime) {
1012 const base::Time expire_after = base::Time::Now() + cache_lifetime; 1011 const base::Time expire_after = base::Time::Now() + cache_lifetime;
1013 1012
1014 // This is called on the I/O thread, lock against updates. 1013 // This function can be called on any thread, so lock against any changes
1015 base::AutoLock locked(lookup_lock_); 1014 base::AutoLock locked(lookup_lock_);
1016 1015
1017 // Create or reset all cached results for these prefixes. 1016 // Create or reset all cached results for these prefixes.
1018 for (size_t i = 0; i < prefixes.size(); ++i) { 1017 for (size_t i = 0; i < prefixes.size(); ++i) {
1019 prefix_gethash_cache_[prefixes[i]] = SBCachedFullHashResult(expire_after); 1018 prefix_gethash_cache_[prefixes[i]] = SBCachedFullHashResult(expire_after);
1020 } 1019 }
1021 1020
1022 // Insert any fullhash hits. Note that there may be one, multiple, or no 1021 // Insert any fullhash hits. Note that there may be one, multiple, or no
1023 // fullhashes for any given entry in |prefixes|. 1022 // fullhashes for any given entry in |prefixes|.
1024 for (size_t i = 0; i < full_hits.size(); ++i) { 1023 for (size_t i = 0; i < full_hits.size(); ++i) {
1025 const SBPrefix prefix = full_hits[i].hash.prefix; 1024 const SBPrefix prefix = full_hits[i].hash.prefix;
1026 prefix_gethash_cache_[prefix].full_hashes.push_back(full_hits[i]); 1025 prefix_gethash_cache_[prefix].full_hashes.push_back(full_hits[i]);
1027 } 1026 }
1028 } 1027 }
1029 1028
1030 bool SafeBrowsingDatabaseNew::UpdateStarted( 1029 bool SafeBrowsingDatabaseNew::UpdateStarted(
1031 std::vector<SBListChunkRanges>* lists) { 1030 std::vector<SBListChunkRanges>* lists) {
1032 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 1031 DCHECK(thread_checker_.CalledOnValidThread());
1033 DCHECK(lists); 1032 DCHECK(lists);
1034 1033
1035 // If |BeginUpdate()| fails, reset the database. 1034 // If |BeginUpdate()| fails, reset the database.
1036 if (!browse_store_->BeginUpdate()) { 1035 if (!browse_store_->BeginUpdate()) {
1037 RecordFailure(FAILURE_BROWSE_DATABASE_UPDATE_BEGIN); 1036 RecordFailure(FAILURE_BROWSE_DATABASE_UPDATE_BEGIN);
1038 HandleCorruptDatabase(); 1037 HandleCorruptDatabase();
1039 return false; 1038 return false;
1040 } 1039 }
1041 1040
1042 if (download_store_.get() && !download_store_->BeginUpdate()) { 1041 if (download_store_.get() && !download_store_->BeginUpdate()) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 UpdateChunkRangesForList(unwanted_software_store_.get(), 1120 UpdateChunkRangesForList(unwanted_software_store_.get(),
1122 safe_browsing_util::kUnwantedUrlList, 1121 safe_browsing_util::kUnwantedUrlList,
1123 lists); 1122 lists);
1124 1123
1125 corruption_detected_ = false; 1124 corruption_detected_ = false;
1126 change_detected_ = false; 1125 change_detected_ = false;
1127 return true; 1126 return true;
1128 } 1127 }
1129 1128
1130 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { 1129 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) {
1131 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 1130 DCHECK(thread_checker_.CalledOnValidThread());
1132 1131
1133 // The update may have failed due to corrupt storage (for instance, 1132 // The update may have failed due to corrupt storage (for instance,
1134 // an excessive number of invalid add_chunks and sub_chunks). 1133 // an excessive number of invalid add_chunks and sub_chunks).
1135 // Double-check that the databases are valid. 1134 // Double-check that the databases are valid.
1136 // TODO(shess): Providing a checksum for the add_chunk and sub_chunk 1135 // TODO(shess): Providing a checksum for the add_chunk and sub_chunk
1137 // sections would allow throwing a corruption error in 1136 // sections would allow throwing a corruption error in
1138 // UpdateStarted(). 1137 // UpdateStarted().
1139 if (!update_succeeded) { 1138 if (!update_succeeded) {
1140 if (!browse_store_->CheckValidity()) 1139 if (!browse_store_->CheckValidity())
1141 DLOG(ERROR) << "Safe-browsing browse database corrupt."; 1140 DLOG(ERROR) << "Safe-browsing browse database corrupt.";
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1243 &unwanted_software_prefix_set_, 1242 &unwanted_software_prefix_set_,
1244 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH, 1243 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH,
1245 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE); 1244 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE);
1246 } 1245 }
1247 } 1246 }
1248 1247
1249 void SafeBrowsingDatabaseNew::UpdateWhitelistStore( 1248 void SafeBrowsingDatabaseNew::UpdateWhitelistStore(
1250 const base::FilePath& store_filename, 1249 const base::FilePath& store_filename,
1251 SafeBrowsingStore* store, 1250 SafeBrowsingStore* store,
1252 SBWhitelist* whitelist) { 1251 SBWhitelist* whitelist) {
1252 DCHECK(thread_checker_.CalledOnValidThread());
1253
1253 if (!store) 1254 if (!store)
1254 return; 1255 return;
1255 1256
1256 // Note: |builder| will not be empty. The current data store implementation 1257 // Note: |builder| will not be empty. The current data store implementation
1257 // stores all full-length hashes as both full and prefix hashes. 1258 // stores all full-length hashes as both full and prefix hashes.
1258 safe_browsing::PrefixSetBuilder builder; 1259 safe_browsing::PrefixSetBuilder builder;
1259 std::vector<SBAddFullHash> full_hashes; 1260 std::vector<SBAddFullHash> full_hashes;
1260 if (!store->FinishUpdate(&builder, &full_hashes)) { 1261 if (!store->FinishUpdate(&builder, &full_hashes)) {
1261 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH); 1262 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH);
1262 WhitelistEverything(whitelist); 1263 WhitelistEverything(whitelist);
1263 return; 1264 return;
1264 } 1265 }
1265 1266
1266 #if defined(OS_MACOSX) 1267 #if defined(OS_MACOSX)
1267 base::mac::SetFileBackupExclusion(store_filename); 1268 base::mac::SetFileBackupExclusion(store_filename);
1268 #endif 1269 #endif
1269 1270
1270 LoadWhitelist(full_hashes, whitelist); 1271 LoadWhitelist(full_hashes, whitelist);
1271 } 1272 }
1272 1273
1273 int64 SafeBrowsingDatabaseNew::UpdateHashPrefixStore( 1274 int64 SafeBrowsingDatabaseNew::UpdateHashPrefixStore(
1274 const base::FilePath& store_filename, 1275 const base::FilePath& store_filename,
1275 SafeBrowsingStore* store, 1276 SafeBrowsingStore* store,
1276 FailureType failure_type) { 1277 FailureType failure_type) {
1278 DCHECK(thread_checker_.CalledOnValidThread());
1279
1277 // These results are not used after this call. Simply ignore the 1280 // These results are not used after this call. Simply ignore the
1278 // returned value after FinishUpdate(...). 1281 // returned value after FinishUpdate(...).
1279 safe_browsing::PrefixSetBuilder builder; 1282 safe_browsing::PrefixSetBuilder builder;
1280 std::vector<SBAddFullHash> add_full_hashes_result; 1283 std::vector<SBAddFullHash> add_full_hashes_result;
1281 1284
1282 if (!store->FinishUpdate(&builder, &add_full_hashes_result)) 1285 if (!store->FinishUpdate(&builder, &add_full_hashes_result))
1283 RecordFailure(failure_type); 1286 RecordFailure(failure_type);
1284 1287
1285 #if defined(OS_MACOSX) 1288 #if defined(OS_MACOSX)
1286 base::mac::SetFileBackupExclusion(store_filename); 1289 base::mac::SetFileBackupExclusion(store_filename);
1287 #endif 1290 #endif
1288 1291
1289 return GetFileSizeOrZero(store_filename); 1292 return GetFileSizeOrZero(store_filename);
1290 } 1293 }
1291 1294
1292 void SafeBrowsingDatabaseNew::UpdatePrefixSetUrlStore( 1295 void SafeBrowsingDatabaseNew::UpdatePrefixSetUrlStore(
1293 const base::FilePath& db_filename, 1296 const base::FilePath& db_filename,
1294 SafeBrowsingStore* url_store, 1297 SafeBrowsingStore* url_store,
1295 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, 1298 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set,
1296 FailureType finish_failure_type, 1299 FailureType finish_failure_type,
1297 FailureType write_failure_type) { 1300 FailureType write_failure_type) {
1301 DCHECK(thread_checker_.CalledOnValidThread());
1302
1298 // Measure the amount of IO during the filter build. 1303 // Measure the amount of IO during the filter build.
1299 base::IoCounters io_before, io_after; 1304 base::IoCounters io_before, io_after;
1300 base::ProcessHandle handle = base::GetCurrentProcessHandle(); 1305 base::ProcessHandle handle = base::GetCurrentProcessHandle();
1301 scoped_ptr<base::ProcessMetrics> metric( 1306 scoped_ptr<base::ProcessMetrics> metric(
1302 #if !defined(OS_MACOSX) 1307 #if !defined(OS_MACOSX)
1303 base::ProcessMetrics::CreateProcessMetrics(handle) 1308 base::ProcessMetrics::CreateProcessMetrics(handle)
1304 #else 1309 #else
1305 // Getting stats only for the current process is enough, so NULL is fine. 1310 // Getting stats only for the current process is enough, so NULL is fine.
1306 base::ProcessMetrics::CreateProcessMetrics(handle, NULL) 1311 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)
1307 #endif 1312 #endif
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1363 const int64 file_size = GetFileSizeOrZero(db_filename); 1368 const int64 file_size = GetFileSizeOrZero(db_filename);
1364 UMA_HISTOGRAM_COUNTS("SB2.BrowseDatabaseKilobytes", 1369 UMA_HISTOGRAM_COUNTS("SB2.BrowseDatabaseKilobytes",
1365 static_cast<int>(file_size / 1024)); 1370 static_cast<int>(file_size / 1024));
1366 1371
1367 #if defined(OS_MACOSX) 1372 #if defined(OS_MACOSX)
1368 base::mac::SetFileBackupExclusion(db_filename); 1373 base::mac::SetFileBackupExclusion(db_filename);
1369 #endif 1374 #endif
1370 } 1375 }
1371 1376
1372 void SafeBrowsingDatabaseNew::UpdateSideEffectFreeWhitelistStore() { 1377 void SafeBrowsingDatabaseNew::UpdateSideEffectFreeWhitelistStore() {
1378 DCHECK(thread_checker_.CalledOnValidThread());
1379
1373 safe_browsing::PrefixSetBuilder builder; 1380 safe_browsing::PrefixSetBuilder builder;
1374 std::vector<SBAddFullHash> add_full_hashes_result; 1381 std::vector<SBAddFullHash> add_full_hashes_result;
1375 1382
1376 if (!side_effect_free_whitelist_store_->FinishUpdate( 1383 if (!side_effect_free_whitelist_store_->FinishUpdate(
1377 &builder, &add_full_hashes_result)) { 1384 &builder, &add_full_hashes_result)) {
1378 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH); 1385 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH);
1379 return; 1386 return;
1380 } 1387 }
1381 scoped_ptr<const safe_browsing::PrefixSet> new_prefix_set( 1388 scoped_ptr<const safe_browsing::PrefixSet> new_prefix_set(
1382 builder.GetPrefixSetNoHashes()); 1389 builder.GetPrefixSetNoHashes());
(...skipping 27 matching lines...) Expand all
1410 static_cast<int>(file_size / 1024)); 1417 static_cast<int>(file_size / 1024));
1411 1418
1412 #if defined(OS_MACOSX) 1419 #if defined(OS_MACOSX)
1413 base::mac::SetFileBackupExclusion(side_effect_free_whitelist_filename); 1420 base::mac::SetFileBackupExclusion(side_effect_free_whitelist_filename);
1414 base::mac::SetFileBackupExclusion( 1421 base::mac::SetFileBackupExclusion(
1415 side_effect_free_whitelist_prefix_set_filename); 1422 side_effect_free_whitelist_prefix_set_filename);
1416 #endif 1423 #endif
1417 } 1424 }
1418 1425
1419 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { 1426 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() {
1427 DCHECK(thread_checker_.CalledOnValidThread());
1428
1420 // Note: prefixes will not be empty. The current data store implementation 1429 // Note: prefixes will not be empty. The current data store implementation
1421 // stores all full-length hashes as both full and prefix hashes. 1430 // stores all full-length hashes as both full and prefix hashes.
1422 safe_browsing::PrefixSetBuilder builder; 1431 safe_browsing::PrefixSetBuilder builder;
1423 std::vector<SBAddFullHash> full_hashes; 1432 std::vector<SBAddFullHash> full_hashes;
1424 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { 1433 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) {
1425 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); 1434 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH);
1426 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. 1435 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list.
1427 return; 1436 return;
1428 } 1437 }
1429 1438
1430 #if defined(OS_MACOSX) 1439 #if defined(OS_MACOSX)
1431 base::mac::SetFileBackupExclusion(IpBlacklistDBFilename(filename_base_)); 1440 base::mac::SetFileBackupExclusion(IpBlacklistDBFilename(filename_base_));
1432 #endif 1441 #endif
1433 1442
1434 LoadIpBlacklist(full_hashes); 1443 LoadIpBlacklist(full_hashes);
1435 } 1444 }
1436 1445
1437 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { 1446 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() {
1447 DCHECK(thread_checker_.CalledOnValidThread());
1448
1438 // Reset the database after the current task has unwound (but only 1449 // Reset the database after the current task has unwound (but only
1439 // reset once within the scope of a given task). 1450 // reset once within the scope of a given task).
1440 if (!reset_factory_.HasWeakPtrs()) { 1451 if (!reset_factory_.HasWeakPtrs()) {
1441 RecordFailure(FAILURE_DATABASE_CORRUPT); 1452 RecordFailure(FAILURE_DATABASE_CORRUPT);
1442 base::MessageLoop::current()->PostTask(FROM_HERE, 1453 base::MessageLoop::current()->PostTask(FROM_HERE,
1443 base::Bind(&SafeBrowsingDatabaseNew::OnHandleCorruptDatabase, 1454 base::Bind(&SafeBrowsingDatabaseNew::OnHandleCorruptDatabase,
1444 reset_factory_.GetWeakPtr())); 1455 reset_factory_.GetWeakPtr()));
1445 } 1456 }
1446 } 1457 }
1447 1458
1448 void SafeBrowsingDatabaseNew::OnHandleCorruptDatabase() { 1459 void SafeBrowsingDatabaseNew::OnHandleCorruptDatabase() {
1460 DCHECK(thread_checker_.CalledOnValidThread());
1461
1449 RecordFailure(FAILURE_DATABASE_CORRUPT_HANDLER); 1462 RecordFailure(FAILURE_DATABASE_CORRUPT_HANDLER);
1450 corruption_detected_ = true; // Stop updating the database. 1463 corruption_detected_ = true; // Stop updating the database.
1451 ResetDatabase(); 1464 ResetDatabase();
1452 1465
1453 // NOTE(shess): ResetDatabase() should remove the corruption, so this should 1466 // NOTE(shess): ResetDatabase() should remove the corruption, so this should
1454 // only happen once. If you are here because you are hitting this after a 1467 // only happen once. If you are here because you are hitting this after a
1455 // restart, then I would be very interested in working with you to figure out 1468 // restart, then I would be very interested in working with you to figure out
1456 // what is happening, since it may affect real users. 1469 // what is happening, since it may affect real users.
1457 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset"; 1470 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset";
1458 } 1471 }
1459 1472
1460 // TODO(shess): I'm not clear why this code doesn't have any 1473 // TODO(shess): I'm not clear why this code doesn't have any
1461 // real error-handling. 1474 // real error-handling.
1462 void SafeBrowsingDatabaseNew::LoadPrefixSet( 1475 void SafeBrowsingDatabaseNew::LoadPrefixSet(
1463 const base::FilePath& db_filename, 1476 const base::FilePath& db_filename,
1464 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, 1477 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set,
1465 FailureType read_failure_type) { 1478 FailureType read_failure_type) {
1479 DCHECK(thread_checker_.CalledOnValidThread());
1480
1466 if (!prefix_set) 1481 if (!prefix_set)
1467 return; 1482 return;
1468 1483
1469 DCHECK_EQ(creation_loop_, base::MessageLoop::current());
1470 DCHECK(!filename_base_.empty()); 1484 DCHECK(!filename_base_.empty());
1471 1485
1472 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename); 1486 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename);
1473 1487
1474 // Only use the prefix set if database is present and non-empty. 1488 // Only use the prefix set if database is present and non-empty.
1475 if (!GetFileSizeOrZero(db_filename)) 1489 if (!GetFileSizeOrZero(db_filename))
1476 return; 1490 return;
1477 1491
1478 // Cleanup any stale bloom filter (no longer used). 1492 // Cleanup any stale bloom filter (no longer used).
1479 // TODO(shess): Track existence to drive removal of this code? 1493 // TODO(shess): Track existence to drive removal of this code?
1480 const base::FilePath bloom_filter_filename = 1494 const base::FilePath bloom_filter_filename =
1481 BloomFilterForFilename(db_filename); 1495 BloomFilterForFilename(db_filename);
1482 base::DeleteFile(bloom_filter_filename, false); 1496 base::DeleteFile(bloom_filter_filename, false);
1483 1497
1484 const base::TimeTicks before = base::TimeTicks::Now(); 1498 const base::TimeTicks before = base::TimeTicks::Now();
1485 *prefix_set = safe_browsing::PrefixSet::LoadFile(prefix_set_filename); 1499 *prefix_set = safe_browsing::PrefixSet::LoadFile(prefix_set_filename);
1486 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before); 1500 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before);
1487 1501
1488 if (!prefix_set->get()) 1502 if (!prefix_set->get())
1489 RecordFailure(read_failure_type); 1503 RecordFailure(read_failure_type);
1490 } 1504 }
1491 1505
1492 bool SafeBrowsingDatabaseNew::Delete() { 1506 bool SafeBrowsingDatabaseNew::Delete() {
1493 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 1507 DCHECK(thread_checker_.CalledOnValidThread());
1494 DCHECK(!filename_base_.empty()); 1508 DCHECK(!filename_base_.empty());
1495 1509
1496 // TODO(shess): This is a mess. SafeBrowsingFileStore::Delete() closes the 1510 // TODO(shess): This is a mess. SafeBrowsingFileStore::Delete() closes the
1497 // store before calling DeleteStore(). DeleteStore() deletes transient files 1511 // store before calling DeleteStore(). DeleteStore() deletes transient files
1498 // in addition to the main file. Probably all of these should be converted to 1512 // in addition to the main file. Probably all of these should be converted to
1499 // a helper which calls Delete() if the store exists, else DeleteStore() on 1513 // a helper which calls Delete() if the store exists, else DeleteStore() on
1500 // the generated filename. 1514 // the generated filename.
1501 1515
1502 // TODO(shess): Determine if the histograms are useful in any way. I cannot 1516 // TODO(shess): Determine if the histograms are useful in any way. I cannot
1503 // recall any action taken as a result of their values, in which case it might 1517 // recall any action taken as a result of their values, in which case it might
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1566 if (!r11) 1580 if (!r11)
1567 RecordFailure(FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE); 1581 RecordFailure(FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE);
1568 1582
1569 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10 && r11; 1583 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10 && r11;
1570 } 1584 }
1571 1585
1572 void SafeBrowsingDatabaseNew::WritePrefixSet( 1586 void SafeBrowsingDatabaseNew::WritePrefixSet(
1573 const base::FilePath& db_filename, 1587 const base::FilePath& db_filename,
1574 const safe_browsing::PrefixSet* prefix_set, 1588 const safe_browsing::PrefixSet* prefix_set,
1575 FailureType write_failure_type) { 1589 FailureType write_failure_type) {
1576 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 1590 DCHECK(thread_checker_.CalledOnValidThread());
1577 1591
1578 if (!prefix_set) 1592 if (!prefix_set)
1579 return; 1593 return;
1580 1594
1581 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename); 1595 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename);
1582 1596
1583 const base::TimeTicks before = base::TimeTicks::Now(); 1597 const base::TimeTicks before = base::TimeTicks::Now();
1584 const bool write_ok = prefix_set->WriteFile(prefix_set_filename); 1598 const bool write_ok = prefix_set->WriteFile(prefix_set_filename);
1585 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); 1599 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before);
1586 1600
1587 const int64 file_size = GetFileSizeOrZero(prefix_set_filename); 1601 const int64 file_size = GetFileSizeOrZero(prefix_set_filename);
1588 UMA_HISTOGRAM_COUNTS("SB2.PrefixSetKilobytes", 1602 UMA_HISTOGRAM_COUNTS("SB2.PrefixSetKilobytes",
1589 static_cast<int>(file_size / 1024)); 1603 static_cast<int>(file_size / 1024));
1590 1604
1591 if (!write_ok) 1605 if (!write_ok)
1592 RecordFailure(write_failure_type); 1606 RecordFailure(write_failure_type);
1593 1607
1594 #if defined(OS_MACOSX) 1608 #if defined(OS_MACOSX)
1595 base::mac::SetFileBackupExclusion(prefix_set_filename); 1609 base::mac::SetFileBackupExclusion(prefix_set_filename);
1596 #endif 1610 #endif
1597 } 1611 }
1598 1612
1599 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelist* whitelist) { 1613 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelist* whitelist) {
1614 DCHECK(thread_checker_.CalledOnValidThread());
1600 base::AutoLock locked(lookup_lock_); 1615 base::AutoLock locked(lookup_lock_);
1601 whitelist->second = true; 1616 whitelist->second = true;
1602 whitelist->first.clear(); 1617 whitelist->first.clear();
1603 } 1618 }
1604 1619
1605 void SafeBrowsingDatabaseNew::LoadWhitelist( 1620 void SafeBrowsingDatabaseNew::LoadWhitelist(
1606 const std::vector<SBAddFullHash>& full_hashes, 1621 const std::vector<SBAddFullHash>& full_hashes,
1607 SBWhitelist* whitelist) { 1622 SBWhitelist* whitelist) {
1608 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 1623 DCHECK(thread_checker_.CalledOnValidThread());
1624
1609 if (full_hashes.size() > kMaxWhitelistSize) { 1625 if (full_hashes.size() > kMaxWhitelistSize) {
1610 WhitelistEverything(whitelist); 1626 WhitelistEverything(whitelist);
1611 return; 1627 return;
1612 } 1628 }
1613 1629
1614 std::vector<SBFullHash> new_whitelist; 1630 std::vector<SBFullHash> new_whitelist;
1615 new_whitelist.reserve(full_hashes.size()); 1631 new_whitelist.reserve(full_hashes.size());
1616 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); 1632 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin();
1617 it != full_hashes.end(); ++it) { 1633 it != full_hashes.end(); ++it) {
1618 new_whitelist.push_back(it->full_hash); 1634 new_whitelist.push_back(it->full_hash);
1619 } 1635 }
1620 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess); 1636 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess);
1621 1637
1622 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl); 1638 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl);
1623 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), 1639 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(),
1624 kill_switch, SBFullHashLess)) { 1640 kill_switch, SBFullHashLess)) {
1625 // The kill switch is whitelisted hence we whitelist all URLs. 1641 // The kill switch is whitelisted hence we whitelist all URLs.
1626 WhitelistEverything(whitelist); 1642 WhitelistEverything(whitelist);
1627 } else { 1643 } else {
1628 base::AutoLock locked(lookup_lock_); 1644 base::AutoLock locked(lookup_lock_);
1629 whitelist->second = false; 1645 whitelist->second = false;
1630 whitelist->first.swap(new_whitelist); 1646 whitelist->first.swap(new_whitelist);
1631 } 1647 }
1632 } 1648 }
1633 1649
1634 void SafeBrowsingDatabaseNew::LoadIpBlacklist( 1650 void SafeBrowsingDatabaseNew::LoadIpBlacklist(
1635 const std::vector<SBAddFullHash>& full_hashes) { 1651 const std::vector<SBAddFullHash>& full_hashes) {
1636 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); 1652 DCHECK(thread_checker_.CalledOnValidThread());
1653
1637 IPBlacklist new_blacklist; 1654 IPBlacklist new_blacklist;
1638 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); 1655 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin();
1639 it != full_hashes.end(); 1656 it != full_hashes.end();
1640 ++it) { 1657 ++it) {
1641 const char* full_hash = it->full_hash.full_hash; 1658 const char* full_hash = it->full_hash.full_hash;
1642 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash)); 1659 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash));
1643 // The format of the IP blacklist is: 1660 // The format of the IP blacklist is:
1644 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes. 1661 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes.
1645 std::string hashed_ip_prefix(full_hash, base::kSHA1Length); 1662 std::string hashed_ip_prefix(full_hash, base::kSHA1Length);
1646 size_t prefix_size = static_cast<uint8>(full_hash[base::kSHA1Length]); 1663 size_t prefix_size = static_cast<uint8>(full_hash[base::kSHA1Length]);
(...skipping 27 matching lines...) Expand all
1674 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { 1691 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() {
1675 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); 1692 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl);
1676 std::vector<SBFullHash> full_hashes; 1693 std::vector<SBFullHash> full_hashes;
1677 full_hashes.push_back(malware_kill_switch); 1694 full_hashes.push_back(malware_kill_switch);
1678 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes); 1695 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes);
1679 } 1696 }
1680 1697
1681 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { 1698 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() {
1682 return csd_whitelist_.second; 1699 return csd_whitelist_.second;
1683 } 1700 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_database.h ('k') | chrome/browser/safe_browsing/safe_browsing_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698