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 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
596 if (extension_blacklist_store_.get()) { | 596 if (extension_blacklist_store_.get()) { |
597 extension_blacklist_store_->Init( | 597 extension_blacklist_store_->Init( |
598 ExtensionBlacklistDBFilename(filename_base_), | 598 ExtensionBlacklistDBFilename(filename_base_), |
599 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 599 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
600 base::Unretained(this))); | 600 base::Unretained(this))); |
601 } | 601 } |
602 | 602 |
603 if (side_effect_free_whitelist_store_.get()) { | 603 if (side_effect_free_whitelist_store_.get()) { |
604 const base::FilePath side_effect_free_whitelist_filename = | 604 const base::FilePath side_effect_free_whitelist_filename = |
605 SideEffectFreeWhitelistDBFilename(filename_base_); | 605 SideEffectFreeWhitelistDBFilename(filename_base_); |
606 const base::FilePath side_effect_free_whitelist_prefix_set_filename = | |
607 PrefixSetForFilename(side_effect_free_whitelist_filename); | |
608 side_effect_free_whitelist_store_->Init( | 606 side_effect_free_whitelist_store_->Init( |
609 side_effect_free_whitelist_filename, | 607 side_effect_free_whitelist_filename, |
610 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 608 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
611 base::Unretained(this))); | 609 base::Unretained(this))); |
612 | 610 |
613 // Only use the prefix set if database is present and non-empty. | 611 LoadPrefixSet(side_effect_free_whitelist_filename, |
614 if (GetFileSizeOrZero(side_effect_free_whitelist_filename)) { | 612 &side_effect_free_whitelist_prefix_set_, |
615 const base::TimeTicks before = base::TimeTicks::Now(); | 613 FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_READ); |
616 side_effect_free_whitelist_prefix_set_ = | |
617 safe_browsing::PrefixSet::LoadFile( | |
618 side_effect_free_whitelist_prefix_set_filename); | |
619 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreeWhitelistPrefixSetLoad", | |
620 base::TimeTicks::Now() - before); | |
621 if (!side_effect_free_whitelist_prefix_set_.get()) | |
622 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_READ); | |
623 } | |
624 } else { | 614 } else { |
625 // Delete any files of the side-effect free sidelist that may be around | 615 // Delete any files of the side-effect free sidelist that may be around |
626 // from when it was previously enabled. | 616 // from when it was previously enabled. |
627 SafeBrowsingStoreFile::DeleteStore( | 617 SafeBrowsingStoreFile::DeleteStore( |
628 SideEffectFreeWhitelistDBFilename(filename_base_)); | 618 SideEffectFreeWhitelistDBFilename(filename_base_)); |
629 base::DeleteFile( | 619 base::DeleteFile( |
630 PrefixSetForFilename(SideEffectFreeWhitelistDBFilename(filename_base_)), | 620 PrefixSetForFilename(SideEffectFreeWhitelistDBFilename(filename_base_)), |
631 false); | 621 false); |
632 } | 622 } |
633 | 623 |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1241 download_store_.get(), | 1231 download_store_.get(), |
1242 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); | 1232 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); |
1243 UMA_HISTOGRAM_COUNTS("SB2.DownloadDatabaseKilobytes", | 1233 UMA_HISTOGRAM_COUNTS("SB2.DownloadDatabaseKilobytes", |
1244 static_cast<int>(size_bytes / 1024)); | 1234 static_cast<int>(size_bytes / 1024)); |
1245 } | 1235 } |
1246 | 1236 |
1247 UpdatePrefixSetUrlStore(BrowseDBFilename(filename_base_), | 1237 UpdatePrefixSetUrlStore(BrowseDBFilename(filename_base_), |
1248 browse_store_.get(), | 1238 browse_store_.get(), |
1249 &browse_prefix_set_, | 1239 &browse_prefix_set_, |
1250 FAILURE_BROWSE_DATABASE_UPDATE_FINISH, | 1240 FAILURE_BROWSE_DATABASE_UPDATE_FINISH, |
1251 FAILURE_BROWSE_PREFIX_SET_WRITE); | 1241 FAILURE_BROWSE_PREFIX_SET_WRITE, |
1242 true); | |
1252 | 1243 |
1253 UpdateWhitelistStore(CsdWhitelistDBFilename(filename_base_), | 1244 UpdateWhitelistStore(CsdWhitelistDBFilename(filename_base_), |
1254 csd_whitelist_store_.get(), | 1245 csd_whitelist_store_.get(), |
1255 &csd_whitelist_); | 1246 &csd_whitelist_); |
1256 UpdateWhitelistStore(DownloadWhitelistDBFilename(filename_base_), | 1247 UpdateWhitelistStore(DownloadWhitelistDBFilename(filename_base_), |
1257 download_whitelist_store_.get(), | 1248 download_whitelist_store_.get(), |
1258 &download_whitelist_); | 1249 &download_whitelist_); |
1259 | 1250 |
1260 if (extension_blacklist_store_) { | 1251 if (extension_blacklist_store_) { |
1261 int64 size_bytes = UpdateHashPrefixStore( | 1252 int64 size_bytes = UpdateHashPrefixStore( |
1262 ExtensionBlacklistDBFilename(filename_base_), | 1253 ExtensionBlacklistDBFilename(filename_base_), |
1263 extension_blacklist_store_.get(), | 1254 extension_blacklist_store_.get(), |
1264 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); | 1255 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); |
1265 UMA_HISTOGRAM_COUNTS("SB2.ExtensionBlacklistKilobytes", | 1256 UMA_HISTOGRAM_COUNTS("SB2.ExtensionBlacklistKilobytes", |
1266 static_cast<int>(size_bytes / 1024)); | 1257 static_cast<int>(size_bytes / 1024)); |
1267 } | 1258 } |
1268 | 1259 |
1269 if (side_effect_free_whitelist_store_) | 1260 if (side_effect_free_whitelist_store_) { |
1270 UpdateSideEffectFreeWhitelistStore(); | 1261 UpdatePrefixSetUrlStore(SideEffectFreeWhitelistDBFilename(filename_base_), |
1262 side_effect_free_whitelist_store_.get(), | |
1263 &side_effect_free_whitelist_prefix_set_, | |
1264 FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH, | |
1265 FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE, | |
1266 false); | |
1267 } | |
1271 | 1268 |
1272 if (ip_blacklist_store_) | 1269 if (ip_blacklist_store_) |
1273 UpdateIpBlacklistStore(); | 1270 UpdateIpBlacklistStore(); |
1274 | 1271 |
1275 if (unwanted_software_store_) { | 1272 if (unwanted_software_store_) { |
1276 UpdatePrefixSetUrlStore(UnwantedSoftwareDBFilename(filename_base_), | 1273 UpdatePrefixSetUrlStore(UnwantedSoftwareDBFilename(filename_base_), |
1277 unwanted_software_store_.get(), | 1274 unwanted_software_store_.get(), |
1278 &unwanted_software_prefix_set_, | 1275 &unwanted_software_prefix_set_, |
1279 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH, | 1276 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH, |
1280 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE); | 1277 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE, |
1278 true); | |
1281 } | 1279 } |
1282 } | 1280 } |
1283 | 1281 |
1284 void SafeBrowsingDatabaseNew::UpdateWhitelistStore( | 1282 void SafeBrowsingDatabaseNew::UpdateWhitelistStore( |
1285 const base::FilePath& store_filename, | 1283 const base::FilePath& store_filename, |
1286 SafeBrowsingStore* store, | 1284 SafeBrowsingStore* store, |
1287 SBWhitelist* whitelist) { | 1285 SBWhitelist* whitelist) { |
1288 DCHECK(thread_checker_.CalledOnValidThread()); | 1286 DCHECK(thread_checker_.CalledOnValidThread()); |
1289 | 1287 |
1290 if (!store) | 1288 if (!store) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1326 #endif | 1324 #endif |
1327 | 1325 |
1328 return GetFileSizeOrZero(store_filename); | 1326 return GetFileSizeOrZero(store_filename); |
1329 } | 1327 } |
1330 | 1328 |
1331 void SafeBrowsingDatabaseNew::UpdatePrefixSetUrlStore( | 1329 void SafeBrowsingDatabaseNew::UpdatePrefixSetUrlStore( |
1332 const base::FilePath& db_filename, | 1330 const base::FilePath& db_filename, |
1333 SafeBrowsingStore* url_store, | 1331 SafeBrowsingStore* url_store, |
1334 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, | 1332 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, |
1335 FailureType finish_failure_type, | 1333 FailureType finish_failure_type, |
1336 FailureType write_failure_type) { | 1334 FailureType write_failure_type, |
1335 bool store_full_hashes_in_prefix_set) { | |
1337 DCHECK(thread_checker_.CalledOnValidThread()); | 1336 DCHECK(thread_checker_.CalledOnValidThread()); |
1337 DCHECK(url_store); | |
1338 DCHECK(prefix_set); | |
1338 | 1339 |
1339 // Measure the amount of IO during the filter build. | 1340 // Measure the amount of IO during the filter build. |
1340 base::IoCounters io_before, io_after; | 1341 base::IoCounters io_before, io_after; |
1341 base::ProcessHandle handle = base::GetCurrentProcessHandle(); | 1342 base::ProcessHandle handle = base::GetCurrentProcessHandle(); |
1342 scoped_ptr<base::ProcessMetrics> metric( | 1343 scoped_ptr<base::ProcessMetrics> metric( |
1343 #if !defined(OS_MACOSX) | 1344 #if !defined(OS_MACOSX) |
1344 base::ProcessMetrics::CreateProcessMetrics(handle) | 1345 base::ProcessMetrics::CreateProcessMetrics(handle) |
1345 #else | 1346 #else |
1346 // Getting stats only for the current process is enough, so NULL is fine. | 1347 // Getting stats only for the current process is enough, so NULL is fine. |
1347 base::ProcessMetrics::CreateProcessMetrics(handle, NULL) | 1348 base::ProcessMetrics::CreateProcessMetrics(handle, NULL) |
(...skipping 10 matching lines...) Expand all Loading... | |
1358 // TODO(shess): Perhaps refactor to let builder accumulate full hashes on the | 1359 // TODO(shess): Perhaps refactor to let builder accumulate full hashes on the |
1359 // fly? Other clients use the SBAddFullHash vector, but AFAICT they only use | 1360 // fly? Other clients use the SBAddFullHash vector, but AFAICT they only use |
1360 // the SBFullHash portion. It would need an accessor on PrefixSet. | 1361 // the SBFullHash portion. It would need an accessor on PrefixSet. |
1361 safe_browsing::PrefixSetBuilder builder; | 1362 safe_browsing::PrefixSetBuilder builder; |
1362 std::vector<SBAddFullHash> add_full_hashes; | 1363 std::vector<SBAddFullHash> add_full_hashes; |
1363 if (!url_store->FinishUpdate(&builder, &add_full_hashes)) { | 1364 if (!url_store->FinishUpdate(&builder, &add_full_hashes)) { |
1364 RecordFailure(finish_failure_type); | 1365 RecordFailure(finish_failure_type); |
1365 return; | 1366 return; |
1366 } | 1367 } |
1367 | 1368 |
1368 std::vector<SBFullHash> full_hash_results; | 1369 scoped_ptr<const safe_browsing::PrefixSet> new_prefix_set; |
1369 for (size_t i = 0; i < add_full_hashes.size(); ++i) { | 1370 if (store_full_hashes_in_prefix_set) { |
1370 full_hash_results.push_back(add_full_hashes[i].full_hash); | 1371 std::vector<SBFullHash> full_hash_results; |
1372 for (size_t i = 0; i < add_full_hashes.size(); ++i) { | |
1373 full_hash_results.push_back(add_full_hashes[i].full_hash); | |
1374 } | |
1375 | |
1376 new_prefix_set = builder.GetPrefixSet(full_hash_results); | |
1377 } else { | |
1378 new_prefix_set = builder.GetPrefixSetNoHashes(); | |
1371 } | 1379 } |
1372 | 1380 |
1373 scoped_ptr<const safe_browsing::PrefixSet> new_prefix_set( | |
1374 builder.GetPrefixSet(full_hash_results)); | |
1375 | |
1376 // Swap in the newly built filter. | 1381 // Swap in the newly built filter. |
1377 { | 1382 { |
1378 base::AutoLock locked(lookup_lock_); | 1383 base::AutoLock locked(lookup_lock_); |
1379 prefix_set->swap(new_prefix_set); | 1384 prefix_set->swap(new_prefix_set); |
1380 } | 1385 } |
1381 | 1386 |
1382 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); | 1387 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); |
1383 | 1388 |
1384 // Persist the prefix set to disk. Note: there is no need to lock since the | 1389 // Persist the prefix set to disk. Note: there is no need to lock since the |
1385 // only write to |*prefix_set| is on this thread (in the swap() above). | 1390 // only write to |*prefix_set| is on this thread (in the swap() above). |
1386 WritePrefixSet(db_filename, prefix_set->get(), write_failure_type); | 1391 WritePrefixSet(db_filename, prefix_set->get(), write_failure_type); |
1387 | 1392 |
1388 // Gather statistics. | 1393 // Gather statistics. |
1389 if (got_counters && metric->GetIOCounters(&io_after)) { | 1394 if (got_counters && metric->GetIOCounters(&io_after)) { |
1390 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", | 1395 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", |
1391 static_cast<int>(io_after.ReadTransferCount - | 1396 static_cast<int>(io_after.ReadTransferCount - |
1392 io_before.ReadTransferCount) / 1024); | 1397 io_before.ReadTransferCount) / 1024); |
1393 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", | 1398 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", |
1394 static_cast<int>(io_after.WriteTransferCount - | 1399 static_cast<int>(io_after.WriteTransferCount - |
1395 io_before.WriteTransferCount) / 1024); | 1400 io_before.WriteTransferCount) / 1024); |
1396 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", | 1401 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", |
1397 static_cast<int>(io_after.ReadOperationCount - | 1402 static_cast<int>(io_after.ReadOperationCount - |
1398 io_before.ReadOperationCount)); | 1403 io_before.ReadOperationCount)); |
1399 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", | 1404 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", |
1400 static_cast<int>(io_after.WriteOperationCount - | 1405 static_cast<int>(io_after.WriteOperationCount - |
1401 io_before.WriteOperationCount)); | 1406 io_before.WriteOperationCount)); |
1402 } | 1407 } |
1403 | 1408 |
1404 const int64 file_size = GetFileSizeOrZero(db_filename); | 1409 const int64 file_size = GetFileSizeOrZero(db_filename); |
1405 UMA_HISTOGRAM_COUNTS("SB2.BrowseDatabaseKilobytes", | 1410 UMA_HISTOGRAM_COUNTS("SB2.DatabaseKilobytes", |
1406 static_cast<int>(file_size / 1024)); | 1411 static_cast<int>(file_size / 1024)); |
1407 | 1412 |
1408 #if defined(OS_MACOSX) | 1413 #if defined(OS_MACOSX) |
1409 base::mac::SetFileBackupExclusion(db_filename); | 1414 base::mac::SetFileBackupExclusion(db_filename); |
1410 #endif | 1415 #endif |
1411 } | 1416 } |
1412 | 1417 |
1413 void SafeBrowsingDatabaseNew::UpdateSideEffectFreeWhitelistStore() { | |
1414 DCHECK(thread_checker_.CalledOnValidThread()); | |
1415 | |
1416 safe_browsing::PrefixSetBuilder builder; | |
1417 std::vector<SBAddFullHash> add_full_hashes_result; | |
1418 | |
1419 if (!side_effect_free_whitelist_store_->FinishUpdate( | |
1420 &builder, &add_full_hashes_result)) { | |
1421 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH); | |
1422 return; | |
1423 } | |
1424 scoped_ptr<const safe_browsing::PrefixSet> new_prefix_set( | |
1425 builder.GetPrefixSetNoHashes()); | |
gab
2014/12/09 21:21:33
GetPrefixSetNoHashes() vs GetPrefixSet() seems to
| |
1426 | |
1427 // Swap in the newly built prefix set. | |
1428 { | |
1429 base::AutoLock locked(lookup_lock_); | |
1430 side_effect_free_whitelist_prefix_set_.swap(new_prefix_set); | |
1431 } | |
1432 | |
1433 const base::FilePath side_effect_free_whitelist_filename = | |
1434 SideEffectFreeWhitelistDBFilename(filename_base_); | |
1435 const base::FilePath side_effect_free_whitelist_prefix_set_filename = | |
1436 PrefixSetForFilename(side_effect_free_whitelist_filename); | |
1437 const base::TimeTicks before = base::TimeTicks::Now(); | |
1438 const bool write_ok = side_effect_free_whitelist_prefix_set_->WriteFile( | |
1439 side_effect_free_whitelist_prefix_set_filename); | |
1440 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreePrefixSetWrite", | |
1441 base::TimeTicks::Now() - before); | |
1442 | |
1443 if (!write_ok) | |
1444 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE); | |
1445 | |
1446 // Gather statistics. | |
1447 int64 file_size = GetFileSizeOrZero( | |
1448 side_effect_free_whitelist_prefix_set_filename); | |
1449 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistPrefixSetKilobytes", | |
1450 static_cast<int>(file_size / 1024)); | |
1451 file_size = GetFileSizeOrZero(side_effect_free_whitelist_filename); | |
1452 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistDatabaseKilobytes", | |
1453 static_cast<int>(file_size / 1024)); | |
1454 | |
1455 #if defined(OS_MACOSX) | |
1456 base::mac::SetFileBackupExclusion(side_effect_free_whitelist_filename); | |
1457 base::mac::SetFileBackupExclusion( | |
1458 side_effect_free_whitelist_prefix_set_filename); | |
1459 #endif | |
1460 } | |
1461 | |
1462 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { | 1418 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { |
1463 DCHECK(thread_checker_.CalledOnValidThread()); | 1419 DCHECK(thread_checker_.CalledOnValidThread()); |
1464 | 1420 |
1465 // Note: prefixes will not be empty. The current data store implementation | 1421 // Note: prefixes will not be empty. The current data store implementation |
1466 // stores all full-length hashes as both full and prefix hashes. | 1422 // stores all full-length hashes as both full and prefix hashes. |
1467 safe_browsing::PrefixSetBuilder builder; | 1423 safe_browsing::PrefixSetBuilder builder; |
1468 std::vector<SBAddFullHash> full_hashes; | 1424 std::vector<SBAddFullHash> full_hashes; |
1469 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { | 1425 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { |
1470 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); | 1426 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); |
1471 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. | 1427 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1736 } | 1692 } |
1737 | 1693 |
1738 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1694 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
1739 // This method is theoretically thread-safe but document that it is currently | 1695 // This method is theoretically thread-safe but document that it is currently |
1740 // only expected to be called on the IO thread. | 1696 // only expected to be called on the IO thread. |
1741 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1697 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1742 | 1698 |
1743 base::AutoLock locked(lookup_lock_); | 1699 base::AutoLock locked(lookup_lock_); |
1744 return csd_whitelist_.second; | 1700 return csd_whitelist_.second; |
1745 } | 1701 } |
OLD | NEW |