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

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

Issue 6711021: Safe-browsing PrefixSet cleanups. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: log when size is broken Created 9 years, 9 months 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 | Annotate | Revision Log
OLDNEW
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
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_BROKEN_SIZE,
218 PREFIX_SET_GETPREFIXES_FIRST_BROKEN,
219 PREFIX_SET_SBPREFIX_WAS_BROKEN,
217 220
218 // Memory space for histograms is determined by the max. ALWAYS ADD 221 // Memory space for histograms is determined by the max. ALWAYS ADD
219 // NEW VALUES BEFORE THIS ONE. 222 // NEW VALUES BEFORE THIS ONE.
220 PREFIX_SET_EVENT_MAX 223 PREFIX_SET_EVENT_MAX
221 }; 224 };
222 225
223 void RecordPrefixSetInfo(PrefixSetEvent event_type) { 226 void RecordPrefixSetInfo(PrefixSetEvent event_type) {
224 UMA_HISTOGRAM_ENUMERATION("SB2.PrefixSetEvent", event_type, 227 UMA_HISTOGRAM_ENUMERATION("SB2.PrefixSetEvent", event_type,
225 PREFIX_SET_EVENT_MAX); 228 PREFIX_SET_EVENT_MAX);
226 } 229 }
227 230
231 // Verify that |GetPrefixes()| returns the same set of prefixes as
232 // |prefixes|. This is necessary so that the
233 // PREFIX_SET_EVENT_BLOOM_MISS_PREFIX_HIT_INVALID histogram in
234 // ContainsBrowseUrl() can be trustworthy.
235 void CheckPrefixSet(const safe_browsing::PrefixSet& prefix_set,
236 const std::vector<SBPrefix>& prefixes) {
237 std::vector<SBPrefix> restored;
238 prefix_set.GetPrefixes(&restored);
239
240 const std::set<SBPrefix> unique(prefixes.begin(), prefixes.end());
241
242 // Expect them to be equal.
243 if (restored.size() == unique.size() &&
244 std::equal(unique.begin(), unique.end(), restored.begin()))
245 return;
246
247 // Log BROKEN for continuity with previous release, and SIZE to
248 // distinguish which test failed.
249 NOTREACHED();
250 RecordPrefixSetInfo(PREFIX_SET_GETPREFIXES_BROKEN);
251 if (restored.size() != unique.size())
252 RecordPrefixSetInfo(PREFIX_SET_GETPREFIXES_BROKEN_SIZE);
253
254 // Try to distinguish between updates from one broken user and a
255 // distributed problem.
256 static bool logged_broken = false;
257 if (!logged_broken) {
258 RecordPrefixSetInfo(PREFIX_SET_GETPREFIXES_FIRST_BROKEN);
259 logged_broken = true;
260 }
261
262 // This seems so very very unlikely. But if it ever were true, then
263 // it could explain why GetPrefixes() seemed broken.
264 if (sizeof(int) != sizeof(int32))
265 RecordPrefixSetInfo(PREFIX_SET_SBPREFIX_WAS_BROKEN);
266 }
267
228 } // namespace 268 } // namespace
229 269
230 // The default SafeBrowsingDatabaseFactory. 270 // The default SafeBrowsingDatabaseFactory.
231 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory { 271 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory {
232 public: 272 public:
233 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase( 273 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase(
234 bool enable_download_protection, 274 bool enable_download_protection,
235 bool enable_client_side_whitelist) { 275 bool enable_client_side_whitelist) {
236 return new SafeBrowsingDatabaseNew( 276 return new SafeBrowsingDatabaseNew(
237 new SafeBrowsingStoreFile, 277 new SafeBrowsingStoreFile,
(...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after
966 } 1006 }
967 1007
968 std::vector<SBPrefix> prefixes; 1008 std::vector<SBPrefix> prefixes;
969 for (size_t i = 0; i < add_prefixes.size(); ++i) { 1009 for (size_t i = 0; i < add_prefixes.size(); ++i) {
970 prefixes.push_back(add_prefixes[i].prefix); 1010 prefixes.push_back(add_prefixes[i].prefix);
971 } 1011 }
972 std::sort(prefixes.begin(), prefixes.end()); 1012 std::sort(prefixes.begin(), prefixes.end());
973 scoped_ptr<safe_browsing::PrefixSet> 1013 scoped_ptr<safe_browsing::PrefixSet>
974 prefix_set(new safe_browsing::PrefixSet(prefixes)); 1014 prefix_set(new safe_browsing::PrefixSet(prefixes));
975 1015
976 // Verify that |GetPrefixes()| returns the same set of prefixes as 1016 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 1017
987 // This needs to be in sorted order by prefix for efficient access. 1018 // This needs to be in sorted order by prefix for efficient access.
988 std::sort(add_full_hashes.begin(), add_full_hashes.end(), 1019 std::sort(add_full_hashes.begin(), add_full_hashes.end(),
989 SBAddFullHashPrefixLess); 1020 SBAddFullHashPrefixLess);
990 1021
991 // Swap in the newly built filter and cache. 1022 // Swap in the newly built filter and cache.
992 { 1023 {
993 base::AutoLock locked(lookup_lock_); 1024 base::AutoLock locked(lookup_lock_);
994 full_browse_hashes_.swap(add_full_hashes); 1025 full_browse_hashes_.swap(add_full_hashes);
995 1026
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 // Manually re-generate the prefix set from the main database. 1120 // Manually re-generate the prefix set from the main database.
1090 // TODO(shess): Write/read for prefix set. 1121 // TODO(shess): Write/read for prefix set.
1091 std::vector<SBAddPrefix> add_prefixes; 1122 std::vector<SBAddPrefix> add_prefixes;
1092 browse_store_->GetAddPrefixes(&add_prefixes); 1123 browse_store_->GetAddPrefixes(&add_prefixes);
1093 std::vector<SBPrefix> prefixes; 1124 std::vector<SBPrefix> prefixes;
1094 for (size_t i = 0; i < add_prefixes.size(); ++i) { 1125 for (size_t i = 0; i < add_prefixes.size(); ++i) {
1095 prefixes.push_back(add_prefixes[i].prefix); 1126 prefixes.push_back(add_prefixes[i].prefix);
1096 } 1127 }
1097 std::sort(prefixes.begin(), prefixes.end()); 1128 std::sort(prefixes.begin(), prefixes.end());
1098 prefix_set_.reset(new safe_browsing::PrefixSet(prefixes)); 1129 prefix_set_.reset(new safe_browsing::PrefixSet(prefixes));
1099 1130 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 } 1131 }
1112 1132
1113 bool SafeBrowsingDatabaseNew::Delete() { 1133 bool SafeBrowsingDatabaseNew::Delete() {
1114 DCHECK_EQ(creation_loop_, MessageLoop::current()); 1134 DCHECK_EQ(creation_loop_, MessageLoop::current());
1115 1135
1116 const bool r1 = browse_store_->Delete(); 1136 const bool r1 = browse_store_->Delete();
1117 if (!r1) 1137 if (!r1)
1118 RecordFailure(FAILURE_DATABASE_STORE_DELETE); 1138 RecordFailure(FAILURE_DATABASE_STORE_DELETE);
1119 1139
1120 const bool r2 = download_store_.get() ? download_store_->Delete() : true; 1140 const bool r2 = download_store_.get() ? download_store_->Delete() : true;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 if (std::binary_search(new_csd_whitelist.begin(), new_csd_whitelist.end(), 1193 if (std::binary_search(new_csd_whitelist.begin(), new_csd_whitelist.end(),
1174 kill_switch)) { 1194 kill_switch)) {
1175 // The kill switch is whitelisted hence we whitelist all URLs. 1195 // The kill switch is whitelisted hence we whitelist all URLs.
1176 CsdWhitelistAllUrls(); 1196 CsdWhitelistAllUrls();
1177 } else { 1197 } else {
1178 base::AutoLock locked(lookup_lock_); 1198 base::AutoLock locked(lookup_lock_);
1179 csd_whitelist_all_urls_ = false; 1199 csd_whitelist_all_urls_ = false;
1180 csd_whitelist_.swap(new_csd_whitelist); 1200 csd_whitelist_.swap(new_csd_whitelist);
1181 } 1201 }
1182 } 1202 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/prefix_set_unittest.cc ('k') | chrome/browser/safe_browsing/safe_browsing_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698