Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/safe_browsing_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 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 UMA_HISTOGRAM_ENUMERATION("SB2.PrefixSetEvent", event_type, | 257 UMA_HISTOGRAM_ENUMERATION("SB2.PrefixSetEvent", event_type, |
| 258 PREFIX_SET_EVENT_MAX); | 258 PREFIX_SET_EVENT_MAX); |
| 259 } | 259 } |
| 260 | 260 |
| 261 // Generate a |PrefixSet| instance from the contents of | 261 // Generate a |PrefixSet| instance from the contents of |
| 262 // |add_prefixes|. Additionally performs various checks to make sure | 262 // |add_prefixes|. Additionally performs various checks to make sure |
| 263 // that the resulting prefix set is valid, so that the | 263 // that the resulting prefix set is valid, so that the |
| 264 // PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT_INVALID histogram in | 264 // PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT_INVALID histogram in |
| 265 // ContainsBrowseUrl() can be trustworthy. | 265 // ContainsBrowseUrl() can be trustworthy. |
| 266 safe_browsing::PrefixSet* PrefixSetFromAddPrefixes( | 266 safe_browsing::PrefixSet* PrefixSetFromAddPrefixes( |
| 267 const std::vector<SBAddPrefix>& add_prefixes) { | 267 const SBAddPrefixContainer& add_prefixes) { |
| 268 // TODO(shess): If |add_prefixes| were sorted by the prefix, it | 268 // TODO(shess): If |add_prefixes| were sorted by the prefix, it |
| 269 // could be passed directly to |PrefixSet()|, removing the need for | 269 // could be passed directly to |PrefixSet()|, removing the need for |
| 270 // |prefixes|. For now, |prefixes| is useful while debugging | 270 // |prefixes|. For now, |prefixes| is useful while debugging |
| 271 // things. | 271 // things. |
| 272 std::vector<SBPrefix> prefixes; | 272 std::vector<SBPrefix> prefixes; |
| 273 prefixes.reserve(add_prefixes.size()); | 273 prefixes.reserve(add_prefixes.size()); |
| 274 for (size_t i = 0; i < add_prefixes.size(); ++i) { | 274 for (SBAddPrefixContainer::const_iterator iter = add_prefixes.begin(); |
| 275 prefixes.push_back(add_prefixes[i].prefix); | 275 iter != add_prefixes.end(); ++iter) { |
| 276 prefixes.push_back(iter->prefix); | |
| 276 } | 277 } |
| 277 | 278 |
| 278 std::sort(prefixes.begin(), prefixes.end()); | 279 std::sort(prefixes.begin(), prefixes.end()); |
| 279 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), | 280 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), |
| 280 prefixes.end()); | 281 prefixes.end()); |
| 281 | 282 |
| 282 scoped_ptr<safe_browsing::PrefixSet> | 283 scoped_ptr<safe_browsing::PrefixSet> |
| 283 prefix_set(new safe_browsing::PrefixSet(prefixes)); | 284 prefix_set(new safe_browsing::PrefixSet(prefixes)); |
| 284 | 285 |
| 285 std::vector<SBPrefix> restored; | 286 std::vector<SBPrefix> restored; |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 701 full_hits, last_update); | 702 full_hits, last_update); |
| 702 return true; | 703 return true; |
| 703 } | 704 } |
| 704 | 705 |
| 705 bool SafeBrowsingDatabaseNew::MatchDownloadAddPrefixes( | 706 bool SafeBrowsingDatabaseNew::MatchDownloadAddPrefixes( |
| 706 int list_bit, | 707 int list_bit, |
| 707 const std::vector<SBPrefix>& prefixes, | 708 const std::vector<SBPrefix>& prefixes, |
| 708 std::vector<SBPrefix>* prefix_hits) { | 709 std::vector<SBPrefix>* prefix_hits) { |
| 709 prefix_hits->clear(); | 710 prefix_hits->clear(); |
| 710 | 711 |
| 711 std::vector<SBAddPrefix> add_prefixes; | 712 SBAddPrefixContainer add_prefixes; |
| 712 download_store_->GetAddPrefixes(&add_prefixes); | 713 download_store_->GetAddPrefixes(&add_prefixes); |
| 713 for (size_t i = 0; i < add_prefixes.size(); ++i) { | 714 for (SBAddPrefixContainer::const_iterator iter = add_prefixes.begin(); |
| 715 iter != add_prefixes.end(); ++iter) { | |
| 714 for (size_t j = 0; j < prefixes.size(); ++j) { | 716 for (size_t j = 0; j < prefixes.size(); ++j) { |
| 715 const SBPrefix& prefix = prefixes[j]; | 717 const SBPrefix& prefix = prefixes[j]; |
| 716 if (prefix == add_prefixes[i].prefix && | 718 if (prefix == iter->prefix && |
| 717 GetListIdBit(add_prefixes[i].chunk_id) == list_bit) { | 719 GetListIdBit(iter->chunk_id) == list_bit) { |
| 718 prefix_hits->push_back(prefix); | 720 prefix_hits->push_back(prefix); |
| 719 } | 721 } |
| 720 } | 722 } |
| 721 } | 723 } |
| 722 return !prefix_hits->empty(); | 724 return !prefix_hits->empty(); |
| 723 } | 725 } |
| 724 | 726 |
| 725 bool SafeBrowsingDatabaseNew::ContainsDownloadUrl( | 727 bool SafeBrowsingDatabaseNew::ContainsDownloadUrl( |
| 726 const std::vector<GURL>& urls, | 728 const std::vector<GURL>& urls, |
| 727 std::vector<SBPrefix>* prefix_hits) { | 729 std::vector<SBPrefix>* prefix_hits) { |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1131 | 1133 |
| 1132 // For the whitelists, we don't cache and save full hashes since all | 1134 // For the whitelists, we don't cache and save full hashes since all |
| 1133 // hashes are already full. | 1135 // hashes are already full. |
| 1134 std::vector<SBAddFullHash> empty_add_hashes; | 1136 std::vector<SBAddFullHash> empty_add_hashes; |
| 1135 | 1137 |
| 1136 // Not needed for the whitelists. | 1138 // Not needed for the whitelists. |
| 1137 std::set<SBPrefix> empty_miss_cache; | 1139 std::set<SBPrefix> empty_miss_cache; |
| 1138 | 1140 |
| 1139 // Note: prefixes will not be empty. The current data store implementation | 1141 // Note: prefixes will not be empty. The current data store implementation |
| 1140 // stores all full-length hashes as both full and prefix hashes. | 1142 // stores all full-length hashes as both full and prefix hashes. |
| 1141 std::vector<SBAddPrefix> prefixes; | 1143 SBAddPrefixContainer prefixes; |
|
jar (doing other things)
2011/10/21 19:10:45
Use of SB as a part of the name actually tricked m
Scott Hess - ex-Googler
2011/10/21 23:39:32
SBAddPrefixContainer is defined in global scope in
| |
| 1142 std::vector<SBAddFullHash> full_hashes; | 1144 std::vector<SBAddFullHash> full_hashes; |
| 1143 if (!store->FinishUpdate(empty_add_hashes, empty_miss_cache, &prefixes, | 1145 if (!store->FinishUpdate(empty_add_hashes, empty_miss_cache, &prefixes, |
| 1144 &full_hashes)) { | 1146 &full_hashes)) { |
| 1145 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH); | 1147 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH); |
| 1146 WhitelistEverything(whitelist); | 1148 WhitelistEverything(whitelist); |
| 1147 return; | 1149 return; |
| 1148 } | 1150 } |
| 1149 | 1151 |
| 1150 #if defined(OS_MACOSX) | 1152 #if defined(OS_MACOSX) |
| 1151 base::mac::SetFileBackupExclusion(store_filename); | 1153 base::mac::SetFileBackupExclusion(store_filename); |
| 1152 #endif | 1154 #endif |
| 1153 | 1155 |
| 1154 LoadWhitelist(full_hashes, whitelist); | 1156 LoadWhitelist(full_hashes, whitelist); |
| 1155 } | 1157 } |
| 1156 | 1158 |
| 1157 void SafeBrowsingDatabaseNew::UpdateDownloadStore() { | 1159 void SafeBrowsingDatabaseNew::UpdateDownloadStore() { |
| 1158 if (!download_store_.get()) | 1160 if (!download_store_.get()) |
| 1159 return; | 1161 return; |
| 1160 | 1162 |
| 1161 // For download, we don't cache and save full hashes. | 1163 // For download, we don't cache and save full hashes. |
| 1162 std::vector<SBAddFullHash> empty_add_hashes; | 1164 std::vector<SBAddFullHash> empty_add_hashes; |
| 1163 | 1165 |
| 1164 // For download, backend lookup happens only if a prefix is in add list. | 1166 // For download, backend lookup happens only if a prefix is in add list. |
| 1165 // No need to pass in miss cache when call FinishUpdate to caculate | 1167 // No need to pass in miss cache when call FinishUpdate to caculate |
| 1166 // bloomfilter false positives. | 1168 // bloomfilter false positives. |
| 1167 std::set<SBPrefix> empty_miss_cache; | 1169 std::set<SBPrefix> empty_miss_cache; |
| 1168 | 1170 |
| 1169 // These results are not used after this call. Simply ignore the | 1171 // These results are not used after this call. Simply ignore the |
| 1170 // returned value after FinishUpdate(...). | 1172 // returned value after FinishUpdate(...). |
| 1171 std::vector<SBAddPrefix> add_prefixes_result; | 1173 SBAddPrefixContainer add_prefixes_result; |
|
jar (doing other things)
2011/10/21 19:10:45
It might even be the case that the type name shoul
Scott Hess - ex-Googler
2011/10/21 23:39:32
I'm reasonable with s/SBAddPrefixContainer/SBAddPr
| |
| 1172 std::vector<SBAddFullHash> add_full_hashes_result; | 1174 std::vector<SBAddFullHash> add_full_hashes_result; |
| 1173 | 1175 |
| 1174 if (!download_store_->FinishUpdate(empty_add_hashes, | 1176 if (!download_store_->FinishUpdate(empty_add_hashes, |
| 1175 empty_miss_cache, | 1177 empty_miss_cache, |
| 1176 &add_prefixes_result, | 1178 &add_prefixes_result, |
| 1177 &add_full_hashes_result)) | 1179 &add_full_hashes_result)) |
| 1178 RecordFailure(FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); | 1180 RecordFailure(FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); |
| 1179 | 1181 |
| 1180 int64 size_64; | 1182 int64 size_64; |
| 1181 if (file_util::GetFileSize(download_filename_, &size_64)) { | 1183 if (file_util::GetFileSize(download_filename_, &size_64)) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1211 #endif | 1213 #endif |
| 1212 ); | 1214 ); |
| 1213 | 1215 |
| 1214 // IoCounters are currently not supported on Mac, and may not be | 1216 // IoCounters are currently not supported on Mac, and may not be |
| 1215 // available for Linux, so we check the result and only show IO | 1217 // available for Linux, so we check the result and only show IO |
| 1216 // stats if they are available. | 1218 // stats if they are available. |
| 1217 const bool got_counters = metric->GetIOCounters(&io_before); | 1219 const bool got_counters = metric->GetIOCounters(&io_before); |
| 1218 | 1220 |
| 1219 const base::Time before = base::Time::Now(); | 1221 const base::Time before = base::Time::Now(); |
| 1220 | 1222 |
| 1221 std::vector<SBAddPrefix> add_prefixes; | 1223 SBAddPrefixContainer add_prefixes; |
| 1222 std::vector<SBAddFullHash> add_full_hashes; | 1224 std::vector<SBAddFullHash> add_full_hashes; |
| 1223 if (!browse_store_->FinishUpdate(pending_add_hashes, prefix_miss_cache_, | 1225 if (!browse_store_->FinishUpdate(pending_add_hashes, prefix_miss_cache_, |
| 1224 &add_prefixes, &add_full_hashes)) { | 1226 &add_prefixes, &add_full_hashes)) { |
| 1225 RecordFailure(FAILURE_BROWSE_DATABASE_UPDATE_FINISH); | 1227 RecordFailure(FAILURE_BROWSE_DATABASE_UPDATE_FINISH); |
| 1226 return; | 1228 return; |
| 1227 } | 1229 } |
| 1228 | 1230 |
| 1229 // Create and populate |filter| from |add_prefixes|. | 1231 // Create and populate |filter| from |add_prefixes|. |
| 1230 // TODO(shess): The bloom filter doesn't need to be a | 1232 // TODO(shess): The bloom filter doesn't need to be a |
| 1231 // scoped_refptr<> for this code. Refactor that away. | 1233 // scoped_refptr<> for this code. Refactor that away. |
| 1232 const int filter_size = | 1234 const int filter_size = |
| 1233 BloomFilter::FilterSizeForKeyCount(add_prefixes.size()); | 1235 BloomFilter::FilterSizeForKeyCount(add_prefixes.size()); |
| 1234 scoped_refptr<BloomFilter> filter(new BloomFilter(filter_size)); | 1236 scoped_refptr<BloomFilter> filter(new BloomFilter(filter_size)); |
| 1235 for (size_t i = 0; i < add_prefixes.size(); ++i) { | 1237 for (SBAddPrefixContainer::const_iterator iter = add_prefixes.begin(); |
| 1236 filter->Insert(add_prefixes[i].prefix); | 1238 iter != add_prefixes.end(); ++iter) { |
| 1239 filter->Insert(iter->prefix); | |
| 1237 } | 1240 } |
| 1238 | 1241 |
| 1239 scoped_ptr<safe_browsing::PrefixSet> | 1242 scoped_ptr<safe_browsing::PrefixSet> |
| 1240 prefix_set(PrefixSetFromAddPrefixes(add_prefixes)); | 1243 prefix_set(PrefixSetFromAddPrefixes(add_prefixes)); |
| 1241 | 1244 |
| 1242 // This needs to be in sorted order by prefix for efficient access. | 1245 // This needs to be in sorted order by prefix for efficient access. |
| 1243 std::sort(add_full_hashes.begin(), add_full_hashes.end(), | 1246 std::sort(add_full_hashes.begin(), add_full_hashes.end(), |
| 1244 SBAddFullHashPrefixLess); | 1247 SBAddFullHashPrefixLess); |
| 1245 | 1248 |
| 1246 // Swap in the newly built filter and cache. | 1249 // Swap in the newly built filter and cache. |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1338 const base::TimeTicks before = base::TimeTicks::Now(); | 1341 const base::TimeTicks before = base::TimeTicks::Now(); |
| 1339 browse_bloom_filter_ = BloomFilter::LoadFile(bloom_filter_filename_); | 1342 browse_bloom_filter_ = BloomFilter::LoadFile(bloom_filter_filename_); |
| 1340 DVLOG(1) << "SafeBrowsingDatabaseNew read bloom filter in " | 1343 DVLOG(1) << "SafeBrowsingDatabaseNew read bloom filter in " |
| 1341 << (base::TimeTicks::Now() - before).InMilliseconds() << " ms"; | 1344 << (base::TimeTicks::Now() - before).InMilliseconds() << " ms"; |
| 1342 | 1345 |
| 1343 if (!browse_bloom_filter_.get()) | 1346 if (!browse_bloom_filter_.get()) |
| 1344 RecordFailure(FAILURE_DATABASE_FILTER_READ); | 1347 RecordFailure(FAILURE_DATABASE_FILTER_READ); |
| 1345 | 1348 |
| 1346 // Manually re-generate the prefix set from the main database. | 1349 // Manually re-generate the prefix set from the main database. |
| 1347 // TODO(shess): Write/read for prefix set. | 1350 // TODO(shess): Write/read for prefix set. |
| 1348 std::vector<SBAddPrefix> add_prefixes; | 1351 SBAddPrefixContainer add_prefixes; |
| 1349 browse_store_->GetAddPrefixes(&add_prefixes); | 1352 browse_store_->GetAddPrefixes(&add_prefixes); |
|
jar (doing other things)
2011/10/21 19:10:45
I won't push for all teh names changed to be reada
Scott Hess - ex-Googler
2011/10/21 23:39:32
Unfortunately ... sigh. I hate this code.
The "a
| |
| 1350 prefix_set_.reset(PrefixSetFromAddPrefixes(add_prefixes)); | 1353 prefix_set_.reset(PrefixSetFromAddPrefixes(add_prefixes)); |
| 1351 } | 1354 } |
| 1352 | 1355 |
| 1353 bool SafeBrowsingDatabaseNew::Delete() { | 1356 bool SafeBrowsingDatabaseNew::Delete() { |
| 1354 DCHECK_EQ(creation_loop_, MessageLoop::current()); | 1357 DCHECK_EQ(creation_loop_, MessageLoop::current()); |
| 1355 | 1358 |
| 1356 const bool r1 = browse_store_->Delete(); | 1359 const bool r1 = browse_store_->Delete(); |
| 1357 if (!r1) | 1360 if (!r1) |
| 1358 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1361 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
| 1359 | 1362 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1425 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), | 1428 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), |
| 1426 kill_switch)) { | 1429 kill_switch)) { |
| 1427 // The kill switch is whitelisted hence we whitelist all URLs. | 1430 // The kill switch is whitelisted hence we whitelist all URLs. |
| 1428 WhitelistEverything(whitelist); | 1431 WhitelistEverything(whitelist); |
| 1429 } else { | 1432 } else { |
| 1430 base::AutoLock locked(lookup_lock_); | 1433 base::AutoLock locked(lookup_lock_); |
| 1431 whitelist->second = false; | 1434 whitelist->second = false; |
| 1432 whitelist->first.swap(new_whitelist); | 1435 whitelist->first.swap(new_whitelist); |
| 1433 } | 1436 } |
| 1434 } | 1437 } |
| OLD | NEW |