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/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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // TODO(lzheng): It was reasonable when database is saved in sqlite, but | 100 // TODO(lzheng): It was reasonable when database is saved in sqlite, but |
101 // there should be better ways to save chunk_id and list_id after we use | 101 // there should be better ways to save chunk_id and list_id after we use |
102 // SafeBrowsingStoreFile. | 102 // SafeBrowsingStoreFile. |
103 int GetListIdBit(const int encoded_chunk_id) { | 103 int GetListIdBit(const int encoded_chunk_id) { |
104 return encoded_chunk_id & 1; | 104 return encoded_chunk_id & 1; |
105 } | 105 } |
106 int DecodeChunkId(int encoded_chunk_id) { | 106 int DecodeChunkId(int encoded_chunk_id) { |
107 return encoded_chunk_id >> 1; | 107 return encoded_chunk_id >> 1; |
108 } | 108 } |
109 int EncodeChunkId(const int chunk, const int list_id) { | 109 int EncodeChunkId(const int chunk, const int list_id) { |
110 DCHECK_NE(list_id, safe_browsing::INVALID); | 110 DCHECK_NE(list_id, safe_browsing_util::INVALID); |
111 return chunk << 1 | list_id % 2; | 111 return chunk << 1 | list_id % 2; |
112 } | 112 } |
113 | 113 |
114 // Generate the set of full hashes to check for |url|. If | 114 // Generate the set of full hashes to check for |url|. If |
115 // |include_whitelist_hashes| is true we will generate additional path-prefixes | 115 // |include_whitelist_hashes| is true we will generate additional path-prefixes |
116 // to match against the csd whitelist. E.g., if the path-prefix /foo is on the | 116 // to match against the csd whitelist. E.g., if the path-prefix /foo is on the |
117 // whitelist it should also match /foo/bar which is not the case for all the | 117 // whitelist it should also match /foo/bar which is not the case for all the |
118 // other lists. We'll also always add a pattern for the empty path. | 118 // other lists. We'll also always add a pattern for the empty path. |
119 // TODO(shess): This function is almost the same as | 119 // TODO(shess): This function is almost the same as |
120 // |CompareFullHashes()| in safe_browsing_util.cc, except that code | 120 // |CompareFullHashes()| in safe_browsing_util.cc, except that code |
121 // does an early exit on match. Since match should be the infrequent | 121 // does an early exit on match. Since match should be the infrequent |
122 // case (phishing or malware found), consider combining this function | 122 // case (phishing or malware found), consider combining this function |
123 // with that one. | 123 // with that one. |
124 void UrlToFullHashes(const GURL& url, | 124 void UrlToFullHashes(const GURL& url, |
125 bool include_whitelist_hashes, | 125 bool include_whitelist_hashes, |
126 std::vector<SBFullHash>* full_hashes) { | 126 std::vector<SBFullHash>* full_hashes) { |
127 std::vector<std::string> hosts; | 127 std::vector<std::string> hosts; |
128 if (url.HostIsIPAddress()) { | 128 if (url.HostIsIPAddress()) { |
129 hosts.push_back(url.host()); | 129 hosts.push_back(url.host()); |
130 } else { | 130 } else { |
131 safe_browsing::GenerateHostsToCheck(url, &hosts); | 131 safe_browsing_util::GenerateHostsToCheck(url, &hosts); |
132 } | 132 } |
133 | 133 |
134 std::vector<std::string> paths; | 134 std::vector<std::string> paths; |
135 safe_browsing::GeneratePathsToCheck(url, &paths); | 135 safe_browsing_util::GeneratePathsToCheck(url, &paths); |
136 | 136 |
137 for (size_t i = 0; i < hosts.size(); ++i) { | 137 for (size_t i = 0; i < hosts.size(); ++i) { |
138 for (size_t j = 0; j < paths.size(); ++j) { | 138 for (size_t j = 0; j < paths.size(); ++j) { |
139 const std::string& path = paths[j]; | 139 const std::string& path = paths[j]; |
140 full_hashes->push_back(safe_browsing::SBFullHashForString( | 140 full_hashes->push_back(SBFullHashForString(hosts[i] + path)); |
141 hosts[i] + path)); | |
142 | 141 |
143 // We may have /foo as path-prefix in the whitelist which should | 142 // We may have /foo as path-prefix in the whitelist which should |
144 // also match with /foo/bar and /foo?bar. Hence, for every path | 143 // also match with /foo/bar and /foo?bar. Hence, for every path |
145 // that ends in '/' we also add the path without the slash. | 144 // that ends in '/' we also add the path without the slash. |
146 if (include_whitelist_hashes && | 145 if (include_whitelist_hashes && |
147 path.size() > 1 && | 146 path.size() > 1 && |
148 path[path.size() - 1] == '/') { | 147 path[path.size() - 1] == '/') { |
149 full_hashes->push_back( | 148 full_hashes->push_back( |
150 safe_browsing::SBFullHashForString( | 149 SBFullHashForString(hosts[i] + path.substr(0, path.size() - 1))); |
151 hosts[i] + path.substr(0, path.size() - 1))); | |
152 } | 150 } |
153 } | 151 } |
154 } | 152 } |
155 } | 153 } |
156 | 154 |
157 // Helper function to compare addprefixes in |store| with |prefixes|. | 155 // Helper function to compare addprefixes in |store| with |prefixes|. |
158 // The |list_bit| indicates which list (url or hash) to compare. | 156 // The |list_bit| indicates which list (url or hash) to compare. |
159 // | 157 // |
160 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain | 158 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain |
161 // the actual matching prefixes. | 159 // the actual matching prefixes. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // Always decode 2 ranges, even if only the first one is expected. | 223 // Always decode 2 ranges, even if only the first one is expected. |
226 // The loop below will only load as many into |lists| as |listnames| | 224 // The loop below will only load as many into |lists| as |listnames| |
227 // indicates. | 225 // indicates. |
228 std::vector<std::string> adds(2); | 226 std::vector<std::string> adds(2); |
229 std::vector<std::string> subs(2); | 227 std::vector<std::string> subs(2); |
230 GetChunkRanges(add_chunks, &adds); | 228 GetChunkRanges(add_chunks, &adds); |
231 GetChunkRanges(sub_chunks, &subs); | 229 GetChunkRanges(sub_chunks, &subs); |
232 | 230 |
233 for (size_t i = 0; i < listnames.size(); ++i) { | 231 for (size_t i = 0; i < listnames.size(); ++i) { |
234 const std::string& listname = listnames[i]; | 232 const std::string& listname = listnames[i]; |
235 DCHECK_EQ(safe_browsing::GetListId(listname) % 2, | 233 DCHECK_EQ(safe_browsing_util::GetListId(listname) % 2, |
236 static_cast<int>(i % 2)); | 234 static_cast<int>(i % 2)); |
237 DCHECK_NE(safe_browsing::GetListId(listname), | 235 DCHECK_NE(safe_browsing_util::GetListId(listname), |
238 safe_browsing::INVALID); | 236 safe_browsing_util::INVALID); |
239 lists->push_back(SBListChunkRanges(listname)); | 237 lists->push_back(SBListChunkRanges(listname)); |
240 lists->back().adds.swap(adds[i]); | 238 lists->back().adds.swap(adds[i]); |
241 lists->back().subs.swap(subs[i]); | 239 lists->back().subs.swap(subs[i]); |
242 } | 240 } |
243 } | 241 } |
244 | 242 |
245 void UpdateChunkRangesForLists(SafeBrowsingStore* store, | 243 void UpdateChunkRangesForLists(SafeBrowsingStore* store, |
246 const std::string& listname0, | 244 const std::string& listname0, |
247 const std::string& listname1, | 245 const std::string& listname1, |
248 std::vector<SBListChunkRanges>* lists) { | 246 std::vector<SBListChunkRanges>* lists) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 // Remove expired entries. | 282 // Remove expired entries. |
285 SBCachedFullHashResult& cached_result = citer->second; | 283 SBCachedFullHashResult& cached_result = citer->second; |
286 if (cached_result.expire_after <= expire_base) { | 284 if (cached_result.expire_after <= expire_base) { |
287 cache->erase(citer); | 285 cache->erase(citer); |
288 return false; | 286 return false; |
289 } | 287 } |
290 | 288 |
291 // Find full-hash matches. | 289 // Find full-hash matches. |
292 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; | 290 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; |
293 for (size_t i = 0; i < cached_hashes.size(); ++i) { | 291 for (size_t i = 0; i < cached_hashes.size(); ++i) { |
294 if (safe_browsing::SBFullHashEqual(full_hash, cached_hashes[i].hash)) | 292 if (SBFullHashEqual(full_hash, cached_hashes[i].hash)) |
295 results->push_back(cached_hashes[i]); | 293 results->push_back(cached_hashes[i]); |
296 } | 294 } |
297 | 295 |
298 return true; | 296 return true; |
299 } | 297 } |
300 | 298 |
301 SafeBrowsingStoreFile* CreateStore( | 299 SafeBrowsingStoreFile* CreateStore( |
302 bool enable, | 300 bool enable, |
303 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 301 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
304 if (!enable) | 302 if (!enable) |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 UrlToFullHashes(urls[i], false, &full_hashes); | 438 UrlToFullHashes(urls[i], false, &full_hashes); |
441 | 439 |
442 for (size_t i = 0; i < full_hashes.size(); ++i) | 440 for (size_t i = 0; i < full_hashes.size(); ++i) |
443 prefixes->push_back(full_hashes[i].prefix); | 441 prefixes->push_back(full_hashes[i].prefix); |
444 } | 442 } |
445 | 443 |
446 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { | 444 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { |
447 // Stores are not thread safe. | 445 // Stores are not thread safe. |
448 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 446 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
449 | 447 |
450 if (list_id == safe_browsing::PHISH || | 448 if (list_id == safe_browsing_util::PHISH || |
451 list_id == safe_browsing::MALWARE) { | 449 list_id == safe_browsing_util::MALWARE) { |
452 return browse_store_.get(); | 450 return browse_store_.get(); |
453 } else if (list_id == safe_browsing::BINURL) { | 451 } else if (list_id == safe_browsing_util::BINURL) { |
454 return download_store_.get(); | 452 return download_store_.get(); |
455 } else if (list_id == safe_browsing::CSDWHITELIST) { | 453 } else if (list_id == safe_browsing_util::CSDWHITELIST) { |
456 return csd_whitelist_store_.get(); | 454 return csd_whitelist_store_.get(); |
457 } else if (list_id == safe_browsing::DOWNLOADWHITELIST) { | 455 } else if (list_id == safe_browsing_util::DOWNLOADWHITELIST) { |
458 return download_whitelist_store_.get(); | 456 return download_whitelist_store_.get(); |
459 } else if (list_id == safe_browsing::INCLUSIONWHITELIST) { | 457 } else if (list_id == safe_browsing_util::INCLUSIONWHITELIST) { |
460 return inclusion_whitelist_store_.get(); | 458 return inclusion_whitelist_store_.get(); |
461 } else if (list_id == safe_browsing::EXTENSIONBLACKLIST) { | 459 } else if (list_id == safe_browsing_util::EXTENSIONBLACKLIST) { |
462 return extension_blacklist_store_.get(); | 460 return extension_blacklist_store_.get(); |
463 } else if (list_id == safe_browsing::IPBLACKLIST) { | 461 } else if (list_id == safe_browsing_util::IPBLACKLIST) { |
464 return ip_blacklist_store_.get(); | 462 return ip_blacklist_store_.get(); |
465 } else if (list_id == safe_browsing::UNWANTEDURL) { | 463 } else if (list_id == safe_browsing_util::UNWANTEDURL) { |
466 return unwanted_software_store_.get(); | 464 return unwanted_software_store_.get(); |
467 } | 465 } |
468 return NULL; | 466 return NULL; |
469 } | 467 } |
470 | 468 |
471 // static | 469 // static |
472 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { | 470 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { |
473 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, | 471 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, |
474 FAILURE_DATABASE_MAX); | 472 FAILURE_DATABASE_MAX); |
475 } | 473 } |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( | 908 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( |
911 const std::vector<SBPrefix>& prefixes, | 909 const std::vector<SBPrefix>& prefixes, |
912 std::vector<SBPrefix>* prefix_hits) { | 910 std::vector<SBPrefix>* prefix_hits) { |
913 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 911 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
914 | 912 |
915 // Ignore this check when download checking is not enabled. | 913 // Ignore this check when download checking is not enabled. |
916 if (!download_store_.get()) | 914 if (!download_store_.get()) |
917 return false; | 915 return false; |
918 | 916 |
919 return MatchAddPrefixes(download_store_.get(), | 917 return MatchAddPrefixes(download_store_.get(), |
920 safe_browsing::BINURL % 2, | 918 safe_browsing_util::BINURL % 2, |
921 prefixes, | 919 prefixes, |
922 prefix_hits); | 920 prefix_hits); |
923 } | 921 } |
924 | 922 |
925 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { | 923 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { |
926 std::vector<SBFullHash> full_hashes; | 924 std::vector<SBFullHash> full_hashes; |
927 UrlToFullHashes(url, true, &full_hashes); | 925 UrlToFullHashes(url, true, &full_hashes); |
928 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 926 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
929 } | 927 } |
930 | 928 |
(...skipping 11 matching lines...) Expand all Loading... |
942 | 940 |
943 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( | 941 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( |
944 const std::vector<SBPrefix>& prefixes, | 942 const std::vector<SBPrefix>& prefixes, |
945 std::vector<SBPrefix>* prefix_hits) { | 943 std::vector<SBPrefix>* prefix_hits) { |
946 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 944 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
947 | 945 |
948 if (!extension_blacklist_store_) | 946 if (!extension_blacklist_store_) |
949 return false; | 947 return false; |
950 | 948 |
951 return MatchAddPrefixes(extension_blacklist_store_.get(), | 949 return MatchAddPrefixes(extension_blacklist_store_.get(), |
952 safe_browsing::EXTENSIONBLACKLIST % 2, | 950 safe_browsing_util::EXTENSIONBLACKLIST % 2, |
953 prefixes, | 951 prefixes, |
954 prefix_hits); | 952 prefix_hits); |
955 } | 953 } |
956 | 954 |
957 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { | 955 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { |
958 net::IPAddressNumber ip_number; | 956 net::IPAddressNumber ip_number; |
959 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) | 957 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) |
960 return false; | 958 return false; |
961 if (ip_number.size() == net::kIPv4AddressSize) | 959 if (ip_number.size() == net::kIPv4AddressSize) |
962 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); | 960 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); |
(...skipping 19 matching lines...) Expand all Loading... |
982 if (it->second.count(hash) > 0) { | 980 if (it->second.count(hash) > 0) { |
983 return true; | 981 return true; |
984 } | 982 } |
985 } | 983 } |
986 return false; | 984 return false; |
987 } | 985 } |
988 | 986 |
989 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( | 987 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( |
990 const std::string& str) { | 988 const std::string& str) { |
991 std::vector<SBFullHash> hashes; | 989 std::vector<SBFullHash> hashes; |
992 hashes.push_back(safe_browsing::SBFullHashForString(str)); | 990 hashes.push_back(SBFullHashForString(str)); |
993 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); | 991 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); |
994 } | 992 } |
995 | 993 |
996 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( | 994 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( |
997 SBWhitelistId whitelist_id, | 995 SBWhitelistId whitelist_id, |
998 const std::vector<SBFullHash>& hashes) { | 996 const std::vector<SBFullHash>& hashes) { |
999 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); | 997 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); |
1000 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); | 998 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); |
1001 if (whitelist->second) | 999 if (whitelist->second) |
1002 return true; | 1000 return true; |
1003 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); | 1001 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); |
1004 it != hashes.end(); ++it) { | 1002 it != hashes.end(); ++it) { |
1005 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), | 1003 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), |
1006 *it, safe_browsing::SBFullHashLess)) { | 1004 *it, SBFullHashLess)) { |
1007 return true; | 1005 return true; |
1008 } | 1006 } |
1009 } | 1007 } |
1010 return false; | 1008 return false; |
1011 } | 1009 } |
1012 | 1010 |
1013 // Helper to insert add-chunk entries. | 1011 // Helper to insert add-chunk entries. |
1014 void SafeBrowsingDatabaseNew::InsertAddChunk( | 1012 void SafeBrowsingDatabaseNew::InsertAddChunk( |
1015 SafeBrowsingStore* store, | 1013 SafeBrowsingStore* store, |
1016 const safe_browsing::ListType list_id, | 1014 const safe_browsing_util::ListType list_id, |
1017 const SBChunkData& chunk_data) { | 1015 const SBChunkData& chunk_data) { |
1018 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1016 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1019 DCHECK(store); | 1017 DCHECK(store); |
1020 | 1018 |
1021 // The server can give us a chunk that we already have because | 1019 // The server can give us a chunk that we already have because |
1022 // it's part of a range. Don't add it again. | 1020 // it's part of a range. Don't add it again. |
1023 const int chunk_id = chunk_data.ChunkNumber(); | 1021 const int chunk_id = chunk_data.ChunkNumber(); |
1024 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1022 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1025 if (store->CheckAddChunk(encoded_chunk_id)) | 1023 if (store->CheckAddChunk(encoded_chunk_id)) |
1026 return; | 1024 return; |
1027 | 1025 |
1028 store->SetAddChunk(encoded_chunk_id); | 1026 store->SetAddChunk(encoded_chunk_id); |
1029 if (chunk_data.IsPrefix()) { | 1027 if (chunk_data.IsPrefix()) { |
1030 const size_t c = chunk_data.PrefixCount(); | 1028 const size_t c = chunk_data.PrefixCount(); |
1031 for (size_t i = 0; i < c; ++i) { | 1029 for (size_t i = 0; i < c; ++i) { |
1032 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); | 1030 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); |
1033 } | 1031 } |
1034 } else { | 1032 } else { |
1035 const size_t c = chunk_data.FullHashCount(); | 1033 const size_t c = chunk_data.FullHashCount(); |
1036 for (size_t i = 0; i < c; ++i) { | 1034 for (size_t i = 0; i < c; ++i) { |
1037 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); | 1035 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); |
1038 } | 1036 } |
1039 } | 1037 } |
1040 } | 1038 } |
1041 | 1039 |
1042 // Helper to insert sub-chunk entries. | 1040 // Helper to insert sub-chunk entries. |
1043 void SafeBrowsingDatabaseNew::InsertSubChunk( | 1041 void SafeBrowsingDatabaseNew::InsertSubChunk( |
1044 SafeBrowsingStore* store, | 1042 SafeBrowsingStore* store, |
1045 const safe_browsing::ListType list_id, | 1043 const safe_browsing_util::ListType list_id, |
1046 const SBChunkData& chunk_data) { | 1044 const SBChunkData& chunk_data) { |
1047 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1045 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1048 DCHECK(store); | 1046 DCHECK(store); |
1049 | 1047 |
1050 // The server can give us a chunk that we already have because | 1048 // The server can give us a chunk that we already have because |
1051 // it's part of a range. Don't add it again. | 1049 // it's part of a range. Don't add it again. |
1052 const int chunk_id = chunk_data.ChunkNumber(); | 1050 const int chunk_id = chunk_data.ChunkNumber(); |
1053 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1051 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1054 if (store->CheckSubChunk(encoded_chunk_id)) | 1052 if (store->CheckSubChunk(encoded_chunk_id)) |
1055 return; | 1053 return; |
(...skipping 22 matching lines...) Expand all Loading... |
1078 const std::string& list_name, | 1076 const std::string& list_name, |
1079 const std::vector<SBChunkData*>& chunks) { | 1077 const std::vector<SBChunkData*>& chunks) { |
1080 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1078 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1081 | 1079 |
1082 if (db_state_manager_.corruption_detected() || chunks.empty()) | 1080 if (db_state_manager_.corruption_detected() || chunks.empty()) |
1083 return; | 1081 return; |
1084 | 1082 |
1085 const base::TimeTicks before = base::TimeTicks::Now(); | 1083 const base::TimeTicks before = base::TimeTicks::Now(); |
1086 | 1084 |
1087 // TODO(shess): The caller should just pass list_id. | 1085 // TODO(shess): The caller should just pass list_id. |
1088 const safe_browsing::ListType list_id = | 1086 const safe_browsing_util::ListType list_id = |
1089 safe_browsing::GetListId(list_name); | 1087 safe_browsing_util::GetListId(list_name); |
1090 | 1088 |
1091 SafeBrowsingStore* store = GetStore(list_id); | 1089 SafeBrowsingStore* store = GetStore(list_id); |
1092 if (!store) return; | 1090 if (!store) return; |
1093 | 1091 |
1094 db_state_manager_.set_change_detected(); | 1092 db_state_manager_.set_change_detected(); |
1095 | 1093 |
1096 // TODO(shess): I believe that the list is always add or sub. Can this use | 1094 // TODO(shess): I believe that the list is always add or sub. Can this use |
1097 // that productively? | 1095 // that productively? |
1098 store->BeginChunk(); | 1096 store->BeginChunk(); |
1099 for (size_t i = 0; i < chunks.size(); ++i) { | 1097 for (size_t i = 0; i < chunks.size(); ++i) { |
(...skipping 11 matching lines...) Expand all Loading... |
1111 } | 1109 } |
1112 | 1110 |
1113 void SafeBrowsingDatabaseNew::DeleteChunks( | 1111 void SafeBrowsingDatabaseNew::DeleteChunks( |
1114 const std::vector<SBChunkDelete>& chunk_deletes) { | 1112 const std::vector<SBChunkDelete>& chunk_deletes) { |
1115 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1113 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1116 | 1114 |
1117 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) | 1115 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) |
1118 return; | 1116 return; |
1119 | 1117 |
1120 const std::string& list_name = chunk_deletes.front().list_name; | 1118 const std::string& list_name = chunk_deletes.front().list_name; |
1121 const safe_browsing::ListType list_id = | 1119 const safe_browsing_util::ListType list_id = |
1122 safe_browsing::GetListId(list_name); | 1120 safe_browsing_util::GetListId(list_name); |
1123 | 1121 |
1124 SafeBrowsingStore* store = GetStore(list_id); | 1122 SafeBrowsingStore* store = GetStore(list_id); |
1125 if (!store) return; | 1123 if (!store) return; |
1126 | 1124 |
1127 db_state_manager_.set_change_detected(); | 1125 db_state_manager_.set_change_detected(); |
1128 | 1126 |
1129 for (size_t i = 0; i < chunk_deletes.size(); ++i) { | 1127 for (size_t i = 0; i < chunk_deletes.size(); ++i) { |
1130 std::vector<int> chunk_numbers; | 1128 std::vector<int> chunk_numbers; |
1131 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); | 1129 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); |
1132 for (size_t j = 0; j < chunk_numbers.size(); ++j) { | 1130 for (size_t j = 0; j < chunk_numbers.size(); ++j) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1216 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); | 1214 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); |
1217 HandleCorruptDatabase(); | 1215 HandleCorruptDatabase(); |
1218 return false; | 1216 return false; |
1219 } | 1217 } |
1220 | 1218 |
1221 // Cached fullhash results must be cleared on every database update (whether | 1219 // Cached fullhash results must be cleared on every database update (whether |
1222 // successful or not). | 1220 // successful or not). |
1223 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); | 1221 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); |
1224 | 1222 |
1225 UpdateChunkRangesForLists(browse_store_.get(), | 1223 UpdateChunkRangesForLists(browse_store_.get(), |
1226 safe_browsing::kMalwareList, | 1224 safe_browsing_util::kMalwareList, |
1227 safe_browsing::kPhishingList, | 1225 safe_browsing_util::kPhishingList, |
1228 lists); | 1226 lists); |
1229 | 1227 |
1230 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been | 1228 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been |
1231 // deprecated. Code to delete the list from the store shows ~15k hits/day as | 1229 // deprecated. Code to delete the list from the store shows ~15k hits/day as |
1232 // of Feb 2014, so it has been removed. Everything _should_ be resilient to | 1230 // of Feb 2014, so it has been removed. Everything _should_ be resilient to |
1233 // extra data of that sort. | 1231 // extra data of that sort. |
1234 UpdateChunkRangesForList(download_store_.get(), | 1232 UpdateChunkRangesForList(download_store_.get(), |
1235 safe_browsing::kBinUrlList, lists); | 1233 safe_browsing_util::kBinUrlList, lists); |
1236 | 1234 |
1237 UpdateChunkRangesForList(csd_whitelist_store_.get(), | 1235 UpdateChunkRangesForList(csd_whitelist_store_.get(), |
1238 safe_browsing::kCsdWhiteList, lists); | 1236 safe_browsing_util::kCsdWhiteList, lists); |
1239 | 1237 |
1240 UpdateChunkRangesForList(download_whitelist_store_.get(), | 1238 UpdateChunkRangesForList(download_whitelist_store_.get(), |
1241 safe_browsing::kDownloadWhiteList, lists); | 1239 safe_browsing_util::kDownloadWhiteList, lists); |
1242 | 1240 |
1243 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), | 1241 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), |
1244 safe_browsing::kInclusionWhitelist, lists); | 1242 safe_browsing_util::kInclusionWhitelist, lists); |
1245 | 1243 |
1246 UpdateChunkRangesForList(extension_blacklist_store_.get(), | 1244 UpdateChunkRangesForList(extension_blacklist_store_.get(), |
1247 safe_browsing::kExtensionBlacklist, lists); | 1245 safe_browsing_util::kExtensionBlacklist, lists); |
1248 | 1246 |
1249 UpdateChunkRangesForList(ip_blacklist_store_.get(), | 1247 UpdateChunkRangesForList(ip_blacklist_store_.get(), |
1250 safe_browsing::kIPBlacklist, lists); | 1248 safe_browsing_util::kIPBlacklist, lists); |
1251 | 1249 |
1252 UpdateChunkRangesForList(unwanted_software_store_.get(), | 1250 UpdateChunkRangesForList(unwanted_software_store_.get(), |
1253 safe_browsing::kUnwantedUrlList, | 1251 safe_browsing_util::kUnwantedUrlList, |
1254 lists); | 1252 lists); |
1255 | 1253 |
1256 db_state_manager_.reset_corruption_detected(); | 1254 db_state_manager_.reset_corruption_detected(); |
1257 db_state_manager_.reset_change_detected(); | 1255 db_state_manager_.reset_change_detected(); |
1258 return true; | 1256 return true; |
1259 } | 1257 } |
1260 | 1258 |
1261 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { | 1259 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { |
1262 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1260 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1263 | 1261 |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1697 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1695 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1698 return; | 1696 return; |
1699 } | 1697 } |
1700 | 1698 |
1701 std::vector<SBFullHash> new_whitelist; | 1699 std::vector<SBFullHash> new_whitelist; |
1702 new_whitelist.reserve(full_hashes.size()); | 1700 new_whitelist.reserve(full_hashes.size()); |
1703 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); | 1701 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); |
1704 it != full_hashes.end(); ++it) { | 1702 it != full_hashes.end(); ++it) { |
1705 new_whitelist.push_back(it->full_hash); | 1703 new_whitelist.push_back(it->full_hash); |
1706 } | 1704 } |
1707 std::sort(new_whitelist.begin(), new_whitelist.end(), | 1705 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess); |
1708 safe_browsing::SBFullHashLess); | |
1709 | 1706 |
1710 SBFullHash kill_switch = safe_browsing::SBFullHashForString( | 1707 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl); |
1711 kWhitelistKillSwitchUrl); | |
1712 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), | 1708 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), |
1713 kill_switch, safe_browsing::SBFullHashLess)) { | 1709 kill_switch, SBFullHashLess)) { |
1714 // The kill switch is whitelisted hence we whitelist all URLs. | 1710 // The kill switch is whitelisted hence we whitelist all URLs. |
1715 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1711 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1716 } else { | 1712 } else { |
1717 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, | 1713 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, |
1718 &new_whitelist); | 1714 &new_whitelist); |
1719 } | 1715 } |
1720 } | 1716 } |
1721 | 1717 |
1722 void SafeBrowsingDatabaseNew::LoadIpBlacklist( | 1718 void SafeBrowsingDatabaseNew::LoadIpBlacklist( |
1723 const std::vector<SBAddFullHash>& full_hashes) { | 1719 const std::vector<SBAddFullHash>& full_hashes) { |
(...skipping 29 matching lines...) Expand all Loading... |
1753 << " prefix_size:" << prefix_size | 1749 << " prefix_size:" << prefix_size |
1754 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), | 1750 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), |
1755 hashed_ip_prefix.size()); | 1751 hashed_ip_prefix.size()); |
1756 new_blacklist[mask].insert(hashed_ip_prefix); | 1752 new_blacklist[mask].insert(hashed_ip_prefix); |
1757 } | 1753 } |
1758 | 1754 |
1759 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); | 1755 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); |
1760 } | 1756 } |
1761 | 1757 |
1762 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { | 1758 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { |
1763 SBFullHash malware_kill_switch = safe_browsing::SBFullHashForString( | 1759 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); |
1764 kMalwareIPKillSwitchUrl); | |
1765 std::vector<SBFullHash> full_hashes; | 1760 std::vector<SBFullHash> full_hashes; |
1766 full_hashes.push_back(malware_kill_switch); | 1761 full_hashes.push_back(malware_kill_switch); |
1767 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 1762 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
1768 } | 1763 } |
1769 | 1764 |
1770 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1765 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
1771 return state_manager_.BeginReadTransaction() | 1766 return state_manager_.BeginReadTransaction() |
1772 ->GetSBWhitelist(SBWhitelistId::CSD) | 1767 ->GetSBWhitelist(SBWhitelistId::CSD) |
1773 ->second; | 1768 ->second; |
1774 } | 1769 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1827 else | 1822 else |
1828 NOTREACHED(); // Add support for new lists above. | 1823 NOTREACHED(); // Add support for new lists above. |
1829 | 1824 |
1830 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | 1825 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. |
1831 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | 1826 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( |
1832 histogram_name, 1, 1000000, 50, | 1827 histogram_name, 1, 1000000, 50, |
1833 base::HistogramBase::kUmaTargetedHistogramFlag); | 1828 base::HistogramBase::kUmaTargetedHistogramFlag); |
1834 | 1829 |
1835 histogram_pointer->Add(file_size_kilobytes); | 1830 histogram_pointer->Add(file_size_kilobytes); |
1836 } | 1831 } |
OLD | NEW |