Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 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 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/metrics/stats_counters.h" | 11 #include "base/metrics/stats_counters.h" |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 // - Any bloom filter miss should be a prefix set miss. | 207 // - Any bloom filter miss should be a prefix set miss. |
| 208 // - Any prefix set hit should be a bloom filter hit. | 208 // - Any prefix set hit should be a bloom filter hit. |
| 209 // - Bloom filter false positives are prefix set misses. | 209 // - Bloom filter false positives are prefix set misses. |
| 210 // The following is to log actual performance to verify this. | 210 // The following is to log actual performance to verify this. |
| 211 enum PrefixSetEvent { | 211 enum PrefixSetEvent { |
| 212 PREFIX_SET_EVENT_HIT, | 212 PREFIX_SET_EVENT_HIT, |
| 213 PREFIX_SET_EVENT_BLOOM_HIT, | 213 PREFIX_SET_EVENT_BLOOM_HIT, |
| 214 PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT, | 214 PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT, |
| 215 PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT_INVALID, | 215 PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT_INVALID, |
| 216 PREFIX_SET_GETPREFIXES_BROKEN, | 216 PREFIX_SET_GETPREFIXES_BROKEN, |
| 217 PREFIX_SET_GETPREFIXES_FIRST_BROKEN, | |
| 218 PREFIX_SET_SBPREFIX_WAS_BROKEN, | |
| 217 | 219 |
| 218 // Memory space for histograms is determined by the max. ALWAYS ADD | 220 // Memory space for histograms is determined by the max. ALWAYS ADD |
| 219 // NEW VALUES BEFORE THIS ONE. | 221 // NEW VALUES BEFORE THIS ONE. |
| 220 PREFIX_SET_EVENT_MAX | 222 PREFIX_SET_EVENT_MAX |
| 221 }; | 223 }; |
| 222 | 224 |
| 223 void RecordPrefixSetInfo(PrefixSetEvent event_type) { | 225 void RecordPrefixSetInfo(PrefixSetEvent event_type) { |
| 224 UMA_HISTOGRAM_ENUMERATION("SB2.PrefixSetEvent", event_type, | 226 UMA_HISTOGRAM_ENUMERATION("SB2.PrefixSetEvent", event_type, |
| 225 PREFIX_SET_EVENT_MAX); | 227 PREFIX_SET_EVENT_MAX); |
| 226 } | 228 } |
| 227 | 229 |
| 230 // Verify that |GetPrefixes()| returns the same set of prefixes as | |
| 231 // |prefixes|. This is necessary so that the | |
| 232 // PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT_INVALID histogram in | |
| 233 // ContainsBrowseUrl() can be trustworthy. | |
| 234 void CheckPrefixSet(const safe_browsing::PrefixSet& prefix_set, | |
| 235 const std::vector<SBPrefix>& prefixes) { | |
| 236 std::vector<SBPrefix> restored; | |
| 237 prefix_set.GetPrefixes(&restored); | |
| 238 | |
| 239 const std::set<SBPrefix> unique(prefixes.begin(), prefixes.end()); | |
| 240 | |
| 241 // Expect them to be equal. | |
| 242 if (restored.size() == unique.size() && | |
| 243 std::equal(unique.begin(), unique.end(), restored.begin())) | |
| 244 return; | |
| 245 | |
| 246 // Log BROKEN for continuity with previous release, and SIZE to | |
| 247 // distinguish which test failed. | |
| 248 NOTREACHED(); | |
| 249 RecordPrefixSetInfo(PREFIX_SET_GETPREFIXES_BROKEN); | |
|
lzheng
2011/03/18 00:37:34
When you get here, do you want to see if it is res
Scott Hess - ex-Googler
2011/03/18 02:04:59
Actually, I had another histogram for this, I can
| |
| 250 | |
| 251 // Try to distinguish between updates from one broken user and a | |
| 252 // distributed problem. | |
| 253 static bool logged_broken = false; | |
| 254 if (!logged_broken) { | |
| 255 RecordPrefixSetInfo(PREFIX_SET_GETPREFIXES_FIRST_BROKEN); | |
| 256 logged_broken = true; | |
| 257 } | |
| 258 | |
| 259 // This seems so very very unlikely. But if it ever were true, then | |
| 260 // it could explain why GetPrefixes() seemed broken. | |
| 261 if (sizeof(int) != sizeof(int32)) | |
| 262 RecordPrefixSetInfo(PREFIX_SET_SBPREFIX_WAS_BROKEN); | |
| 263 } | |
| 264 | |
| 228 } // namespace | 265 } // namespace |
| 229 | 266 |
| 230 // The default SafeBrowsingDatabaseFactory. | 267 // The default SafeBrowsingDatabaseFactory. |
| 231 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory { | 268 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory { |
| 232 public: | 269 public: |
| 233 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase( | 270 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase( |
| 234 bool enable_download_protection, | 271 bool enable_download_protection, |
| 235 bool enable_client_side_whitelist) { | 272 bool enable_client_side_whitelist) { |
| 236 return new SafeBrowsingDatabaseNew( | 273 return new SafeBrowsingDatabaseNew( |
| 237 new SafeBrowsingStoreFile, | 274 new SafeBrowsingStoreFile, |
| (...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 966 } | 1003 } |
| 967 | 1004 |
| 968 std::vector<SBPrefix> prefixes; | 1005 std::vector<SBPrefix> prefixes; |
| 969 for (size_t i = 0; i < add_prefixes.size(); ++i) { | 1006 for (size_t i = 0; i < add_prefixes.size(); ++i) { |
| 970 prefixes.push_back(add_prefixes[i].prefix); | 1007 prefixes.push_back(add_prefixes[i].prefix); |
| 971 } | 1008 } |
| 972 std::sort(prefixes.begin(), prefixes.end()); | 1009 std::sort(prefixes.begin(), prefixes.end()); |
| 973 scoped_ptr<safe_browsing::PrefixSet> | 1010 scoped_ptr<safe_browsing::PrefixSet> |
| 974 prefix_set(new safe_browsing::PrefixSet(prefixes)); | 1011 prefix_set(new safe_browsing::PrefixSet(prefixes)); |
| 975 | 1012 |
| 976 // Verify that |GetPrefixes()| returns the same set of prefixes as | 1013 CheckPrefixSet(*(prefix_set.get()), prefixes); |
| 977 // was passed to the constructor. | |
| 978 std::vector<SBPrefix> restored; | |
| 979 prefix_set->GetPrefixes(&restored); | |
| 980 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end()); | |
| 981 if (restored.size() != prefixes.size() || | |
| 982 !std::equal(prefixes.begin(), prefixes.end(), restored.begin())) { | |
| 983 NOTREACHED(); | |
| 984 RecordPrefixSetInfo(PREFIX_SET_GETPREFIXES_BROKEN); | |
| 985 } | |
| 986 | 1014 |
| 987 // This needs to be in sorted order by prefix for efficient access. | 1015 // This needs to be in sorted order by prefix for efficient access. |
| 988 std::sort(add_full_hashes.begin(), add_full_hashes.end(), | 1016 std::sort(add_full_hashes.begin(), add_full_hashes.end(), |
| 989 SBAddFullHashPrefixLess); | 1017 SBAddFullHashPrefixLess); |
| 990 | 1018 |
| 991 // Swap in the newly built filter and cache. | 1019 // Swap in the newly built filter and cache. |
| 992 { | 1020 { |
| 993 base::AutoLock locked(lookup_lock_); | 1021 base::AutoLock locked(lookup_lock_); |
| 994 full_browse_hashes_.swap(add_full_hashes); | 1022 full_browse_hashes_.swap(add_full_hashes); |
| 995 | 1023 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1089 // Manually re-generate the prefix set from the main database. | 1117 // Manually re-generate the prefix set from the main database. |
| 1090 // TODO(shess): Write/read for prefix set. | 1118 // TODO(shess): Write/read for prefix set. |
| 1091 std::vector<SBAddPrefix> add_prefixes; | 1119 std::vector<SBAddPrefix> add_prefixes; |
| 1092 browse_store_->GetAddPrefixes(&add_prefixes); | 1120 browse_store_->GetAddPrefixes(&add_prefixes); |
| 1093 std::vector<SBPrefix> prefixes; | 1121 std::vector<SBPrefix> prefixes; |
| 1094 for (size_t i = 0; i < add_prefixes.size(); ++i) { | 1122 for (size_t i = 0; i < add_prefixes.size(); ++i) { |
| 1095 prefixes.push_back(add_prefixes[i].prefix); | 1123 prefixes.push_back(add_prefixes[i].prefix); |
| 1096 } | 1124 } |
| 1097 std::sort(prefixes.begin(), prefixes.end()); | 1125 std::sort(prefixes.begin(), prefixes.end()); |
| 1098 prefix_set_.reset(new safe_browsing::PrefixSet(prefixes)); | 1126 prefix_set_.reset(new safe_browsing::PrefixSet(prefixes)); |
| 1099 | 1127 CheckPrefixSet(*(prefix_set_.get()), prefixes); |
| 1100 // Double-check the prefixes so that the | |
| 1101 // PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT_INVALID histogram in | |
| 1102 // ContainsBrowseUrl() can be trustworthy. | |
| 1103 std::vector<SBPrefix> restored; | |
| 1104 prefix_set_->GetPrefixes(&restored); | |
| 1105 std::set<SBPrefix> unique(prefixes.begin(), prefixes.end()); | |
| 1106 if (restored.size() != unique.size() || | |
| 1107 !std::equal(unique.begin(), unique.end(), restored.begin())) { | |
| 1108 NOTREACHED(); | |
| 1109 RecordPrefixSetInfo(PREFIX_SET_GETPREFIXES_BROKEN); | |
| 1110 } | |
| 1111 } | 1128 } |
| 1112 | 1129 |
| 1113 bool SafeBrowsingDatabaseNew::Delete() { | 1130 bool SafeBrowsingDatabaseNew::Delete() { |
| 1114 DCHECK_EQ(creation_loop_, MessageLoop::current()); | 1131 DCHECK_EQ(creation_loop_, MessageLoop::current()); |
| 1115 | 1132 |
| 1116 const bool r1 = browse_store_->Delete(); | 1133 const bool r1 = browse_store_->Delete(); |
| 1117 if (!r1) | 1134 if (!r1) |
| 1118 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1135 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
| 1119 | 1136 |
| 1120 const bool r2 = download_store_.get() ? download_store_->Delete() : true; | 1137 const bool r2 = download_store_.get() ? download_store_->Delete() : true; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1173 if (std::binary_search(new_csd_whitelist.begin(), new_csd_whitelist.end(), | 1190 if (std::binary_search(new_csd_whitelist.begin(), new_csd_whitelist.end(), |
| 1174 kill_switch)) { | 1191 kill_switch)) { |
| 1175 // The kill switch is whitelisted hence we whitelist all URLs. | 1192 // The kill switch is whitelisted hence we whitelist all URLs. |
| 1176 CsdWhitelistAllUrls(); | 1193 CsdWhitelistAllUrls(); |
| 1177 } else { | 1194 } else { |
| 1178 base::AutoLock locked(lookup_lock_); | 1195 base::AutoLock locked(lookup_lock_); |
| 1179 csd_whitelist_all_urls_ = false; | 1196 csd_whitelist_all_urls_ = false; |
| 1180 csd_whitelist_.swap(new_csd_whitelist); | 1197 csd_whitelist_.swap(new_csd_whitelist); |
| 1181 } | 1198 } |
| 1182 } | 1199 } |
| OLD | NEW |