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 1311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1322 if (unwanted_software_store_) | 1322 if (unwanted_software_store_) |
1323 unwanted_software_store_->CancelUpdate(); | 1323 unwanted_software_store_->CancelUpdate(); |
1324 return; | 1324 return; |
1325 } | 1325 } |
1326 | 1326 |
1327 if (download_store_) { | 1327 if (download_store_) { |
1328 int64 size_bytes = UpdateHashPrefixStore( | 1328 int64 size_bytes = UpdateHashPrefixStore( |
1329 DownloadDBFilename(filename_base_), | 1329 DownloadDBFilename(filename_base_), |
1330 download_store_.get(), | 1330 download_store_.get(), |
1331 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); | 1331 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); |
1332 UMA_HISTOGRAM_COUNTS("SB2.DownloadDatabaseKilobytes", | 1332 UMA_HISTOGRAM_COUNTS("SB2.DatabaseKilobytes.Download", |
1333 static_cast<int>(size_bytes / 1024)); | 1333 static_cast<int>(size_bytes / 1024)); |
1334 } | 1334 } |
1335 | 1335 |
1336 UpdatePrefixSetUrlStore(BrowseDBFilename(filename_base_), | 1336 UpdatePrefixSetUrlStore(BrowseDBFilename(filename_base_), |
1337 browse_store_.get(), | 1337 browse_store_.get(), |
1338 PrefixSetId::BROWSE, | 1338 PrefixSetId::BROWSE, |
1339 FAILURE_BROWSE_DATABASE_UPDATE_FINISH, | 1339 FAILURE_BROWSE_DATABASE_UPDATE_FINISH, |
1340 FAILURE_BROWSE_PREFIX_SET_WRITE, | 1340 FAILURE_BROWSE_PREFIX_SET_WRITE, |
1341 true); | 1341 true); |
1342 | 1342 |
1343 UpdateWhitelistStore(CsdWhitelistDBFilename(filename_base_), | 1343 UpdateWhitelistStore(CsdWhitelistDBFilename(filename_base_), |
1344 csd_whitelist_store_.get(), | 1344 csd_whitelist_store_.get(), |
1345 SBWhitelistId::CSD); | 1345 SBWhitelistId::CSD); |
1346 UpdateWhitelistStore(DownloadWhitelistDBFilename(filename_base_), | 1346 UpdateWhitelistStore(DownloadWhitelistDBFilename(filename_base_), |
1347 download_whitelist_store_.get(), | 1347 download_whitelist_store_.get(), |
1348 SBWhitelistId::DOWNLOAD); | 1348 SBWhitelistId::DOWNLOAD); |
1349 | 1349 |
1350 if (extension_blacklist_store_) { | 1350 if (extension_blacklist_store_) { |
1351 int64 size_bytes = UpdateHashPrefixStore( | 1351 int64 size_bytes = UpdateHashPrefixStore( |
1352 ExtensionBlacklistDBFilename(filename_base_), | 1352 ExtensionBlacklistDBFilename(filename_base_), |
1353 extension_blacklist_store_.get(), | 1353 extension_blacklist_store_.get(), |
1354 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); | 1354 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); |
1355 UMA_HISTOGRAM_COUNTS("SB2.ExtensionBlacklistKilobytes", | 1355 UMA_HISTOGRAM_COUNTS("SB2.DatabaseKilobytes.ExtensionBlacklist", |
1356 static_cast<int>(size_bytes / 1024)); | 1356 static_cast<int>(size_bytes / 1024)); |
1357 } | 1357 } |
1358 | 1358 |
1359 if (side_effect_free_whitelist_store_) { | 1359 if (side_effect_free_whitelist_store_) { |
1360 UpdatePrefixSetUrlStore(SideEffectFreeWhitelistDBFilename(filename_base_), | 1360 UpdatePrefixSetUrlStore(SideEffectFreeWhitelistDBFilename(filename_base_), |
1361 side_effect_free_whitelist_store_.get(), | 1361 side_effect_free_whitelist_store_.get(), |
1362 PrefixSetId::SIDE_EFFECT_FREE_WHITELIST, | 1362 PrefixSetId::SIDE_EFFECT_FREE_WHITELIST, |
1363 FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH, | 1363 FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH, |
1364 FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE, | 1364 FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE, |
1365 false); | 1365 false); |
(...skipping 24 matching lines...) Expand all Loading... | |
1390 // Note: |builder| will not be empty. The current data store implementation | 1390 // Note: |builder| will not be empty. The current data store implementation |
1391 // stores all full-length hashes as both full and prefix hashes. | 1391 // stores all full-length hashes as both full and prefix hashes. |
1392 PrefixSetBuilder builder; | 1392 PrefixSetBuilder builder; |
1393 std::vector<SBAddFullHash> full_hashes; | 1393 std::vector<SBAddFullHash> full_hashes; |
1394 if (!store->FinishUpdate(&builder, &full_hashes)) { | 1394 if (!store->FinishUpdate(&builder, &full_hashes)) { |
1395 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH); | 1395 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH); |
1396 WhitelistEverything(whitelist_id); | 1396 WhitelistEverything(whitelist_id); |
1397 return; | 1397 return; |
1398 } | 1398 } |
1399 | 1399 |
1400 const int64 file_size = GetFileSizeOrZero(store_filename); | |
1401 RecordSBWhitelistCountsHistogram("SB2.DatabaseKilobytes", whitelist_id, | |
1402 static_cast<int>(file_size / 1024)); | |
1403 | |
1400 #if defined(OS_MACOSX) | 1404 #if defined(OS_MACOSX) |
1401 base::mac::SetFileBackupExclusion(store_filename); | 1405 base::mac::SetFileBackupExclusion(store_filename); |
1402 #endif | 1406 #endif |
1403 | 1407 |
1404 LoadWhitelist(full_hashes, whitelist_id); | 1408 LoadWhitelist(full_hashes, whitelist_id); |
1405 } | 1409 } |
1406 | 1410 |
1407 int64 SafeBrowsingDatabaseNew::UpdateHashPrefixStore( | 1411 int64 SafeBrowsingDatabaseNew::UpdateHashPrefixStore( |
1408 const base::FilePath& store_filename, | 1412 const base::FilePath& store_filename, |
1409 SafeBrowsingStore* store, | 1413 SafeBrowsingStore* store, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1477 } | 1481 } |
1478 | 1482 |
1479 // Swap in the newly built filter. | 1483 // Swap in the newly built filter. |
1480 { | 1484 { |
1481 scoped_ptr<WriteTransaction> txn = state_manager_.BeginWriteTransaction(); | 1485 scoped_ptr<WriteTransaction> txn = state_manager_.BeginWriteTransaction(); |
1482 txn->SwapPrefixSet(prefix_set_id, new_prefix_set.Pass()); | 1486 txn->SwapPrefixSet(prefix_set_id, new_prefix_set.Pass()); |
1483 } | 1487 } |
1484 | 1488 |
1485 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); | 1489 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); |
1486 | 1490 |
1487 { | 1491 WritePrefixSet(db_filename, prefix_set_id, write_failure_type); |
1488 // Persist the prefix set to disk. Do not grab the lock to avoid contention | |
1489 // while writing to disk. This is safe as only this thread can ever modify | |
1490 // |state_manager_|'s prefix sets anyways. | |
1491 scoped_ptr<ReadTransaction> txn = | |
1492 state_manager_.BeginReadTransactionNoLockOnMainThread(); | |
1493 WritePrefixSet(db_filename, txn->GetPrefixSet(prefix_set_id), | |
1494 write_failure_type); | |
1495 } | |
1496 | 1492 |
1497 // Gather statistics. | 1493 // Gather statistics. |
1498 if (got_counters && metric->GetIOCounters(&io_after)) { | 1494 if (got_counters && metric->GetIOCounters(&io_after)) { |
1499 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", | 1495 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", |
1500 static_cast<int>(io_after.ReadTransferCount - | 1496 static_cast<int>(io_after.ReadTransferCount - |
1501 io_before.ReadTransferCount) / 1024); | 1497 io_before.ReadTransferCount) / 1024); |
1502 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", | 1498 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", |
1503 static_cast<int>(io_after.WriteTransferCount - | 1499 static_cast<int>(io_after.WriteTransferCount - |
1504 io_before.WriteTransferCount) / 1024); | 1500 io_before.WriteTransferCount) / 1024); |
1505 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", | 1501 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", |
1506 static_cast<int>(io_after.ReadOperationCount - | 1502 static_cast<int>(io_after.ReadOperationCount - |
1507 io_before.ReadOperationCount)); | 1503 io_before.ReadOperationCount)); |
1508 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", | 1504 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", |
1509 static_cast<int>(io_after.WriteOperationCount - | 1505 static_cast<int>(io_after.WriteOperationCount - |
1510 io_before.WriteOperationCount)); | 1506 io_before.WriteOperationCount)); |
1511 } | 1507 } |
1512 | 1508 |
1513 const int64 file_size = GetFileSizeOrZero(db_filename); | 1509 const int64 file_size = GetFileSizeOrZero(db_filename); |
1514 UMA_HISTOGRAM_COUNTS("SB2.DatabaseKilobytes", | 1510 RecordPrefixSetCountsHistogram("SB2.DatabaseKilobytes", prefix_set_id, |
1515 static_cast<int>(file_size / 1024)); | 1511 static_cast<int>(file_size / 1024)); |
Scott Hess - ex-Googler
2014/12/18 23:57:55
I find the prefix-set references misleading, here.
gab
2014/12/19 15:25:54
Right but I was trying to avoid introducing yet an
| |
1516 | 1512 |
1517 #if defined(OS_MACOSX) | 1513 #if defined(OS_MACOSX) |
1518 base::mac::SetFileBackupExclusion(db_filename); | 1514 base::mac::SetFileBackupExclusion(db_filename); |
1519 #endif | 1515 #endif |
1520 } | 1516 } |
1521 | 1517 |
1522 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { | 1518 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { |
1523 DCHECK(thread_checker_.CalledOnValidThread()); | 1519 DCHECK(thread_checker_.CalledOnValidThread()); |
1524 | 1520 |
1525 // Note: prefixes will not be empty. The current data store implementation | 1521 // Note: prefixes will not be empty. The current data store implementation |
1526 // stores all full-length hashes as both full and prefix hashes. | 1522 // stores all full-length hashes as both full and prefix hashes. |
1527 PrefixSetBuilder builder; | 1523 PrefixSetBuilder builder; |
1528 std::vector<SBAddFullHash> full_hashes; | 1524 std::vector<SBAddFullHash> full_hashes; |
1529 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { | 1525 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { |
1530 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); | 1526 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); |
1531 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. | 1527 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. |
1532 return; | 1528 return; |
1533 } | 1529 } |
1534 | 1530 |
1531 const base::FilePath ip_blacklist_filename = | |
1532 IpBlacklistDBFilename(filename_base_); | |
1533 | |
1534 const int64 file_size = GetFileSizeOrZero(ip_blacklist_filename); | |
1535 UMA_HISTOGRAM_COUNTS("SB2.DatabaseKilobytes.IPBlacklist", | |
1536 static_cast<int>(file_size / 1024)); | |
1537 | |
1535 #if defined(OS_MACOSX) | 1538 #if defined(OS_MACOSX) |
1536 base::mac::SetFileBackupExclusion(IpBlacklistDBFilename(filename_base_)); | 1539 base::mac::SetFileBackupExclusion(ip_blacklist_filename); |
1537 #endif | 1540 #endif |
1538 | 1541 |
1539 LoadIpBlacklist(full_hashes); | 1542 LoadIpBlacklist(full_hashes); |
1540 } | 1543 } |
1541 | 1544 |
1542 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { | 1545 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { |
1543 DCHECK(thread_checker_.CalledOnValidThread()); | 1546 DCHECK(thread_checker_.CalledOnValidThread()); |
1544 | 1547 |
1545 // Reset the database after the current task has unwound (but only | 1548 // Reset the database after the current task has unwound (but only |
1546 // reset once within the scope of a given task). | 1549 // reset once within the scope of a given task). |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1669 | 1672 |
1670 const bool r11 = | 1673 const bool r11 = |
1671 base::DeleteFile(UnwantedSoftwareDBFilename(filename_base_), false); | 1674 base::DeleteFile(UnwantedSoftwareDBFilename(filename_base_), false); |
1672 if (!r11) | 1675 if (!r11) |
1673 RecordFailure(FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE); | 1676 RecordFailure(FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE); |
1674 | 1677 |
1675 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10 && r11; | 1678 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10 && r11; |
1676 } | 1679 } |
1677 | 1680 |
1678 void SafeBrowsingDatabaseNew::WritePrefixSet(const base::FilePath& db_filename, | 1681 void SafeBrowsingDatabaseNew::WritePrefixSet(const base::FilePath& db_filename, |
1679 const PrefixSet* prefix_set, | 1682 PrefixSetId prefix_set_id, |
1680 FailureType write_failure_type) { | 1683 FailureType write_failure_type) { |
1681 DCHECK(thread_checker_.CalledOnValidThread()); | 1684 DCHECK(thread_checker_.CalledOnValidThread()); |
1682 | 1685 |
1686 // Do not grab the lock to avoid contention while writing to disk. This is | |
1687 // safe as only this thread can ever modify |state_manager_|'s prefix sets | |
1688 // anyways. | |
1689 scoped_ptr<ReadTransaction> txn = | |
1690 state_manager_.BeginReadTransactionNoLockOnMainThread(); | |
1691 const PrefixSet* prefix_set = txn->GetPrefixSet(prefix_set_id); | |
1692 | |
1683 if (!prefix_set) | 1693 if (!prefix_set) |
1684 return; | 1694 return; |
1685 | 1695 |
1686 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename); | 1696 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename); |
1687 | 1697 |
1688 const base::TimeTicks before = base::TimeTicks::Now(); | 1698 const base::TimeTicks before = base::TimeTicks::Now(); |
1689 const bool write_ok = prefix_set->WriteFile(prefix_set_filename); | 1699 const bool write_ok = prefix_set->WriteFile(prefix_set_filename); |
1690 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); | 1700 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); |
1691 | 1701 |
1692 const int64 file_size = GetFileSizeOrZero(prefix_set_filename); | 1702 const int64 file_size = GetFileSizeOrZero(prefix_set_filename); |
1693 UMA_HISTOGRAM_COUNTS("SB2.PrefixSetKilobytes", | 1703 RecordPrefixSetCountsHistogram("SB2.PrefixSetKilobytes", prefix_set_id, |
1694 static_cast<int>(file_size / 1024)); | 1704 static_cast<int>(file_size / 1024)); |
1695 | 1705 |
1696 if (!write_ok) | 1706 if (!write_ok) |
1697 RecordFailure(write_failure_type); | 1707 RecordFailure(write_failure_type); |
1698 | 1708 |
1699 #if defined(OS_MACOSX) | 1709 #if defined(OS_MACOSX) |
1700 base::mac::SetFileBackupExclusion(prefix_set_filename); | 1710 base::mac::SetFileBackupExclusion(prefix_set_filename); |
1701 #endif | 1711 #endif |
1702 } | 1712 } |
1703 | 1713 |
1704 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelistId whitelist_id) { | 1714 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelistId whitelist_id) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1791 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1801 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
1792 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); | 1802 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); |
1793 return txn->GetSBWhitelist(SBWhitelistId::CSD)->second; | 1803 return txn->GetSBWhitelist(SBWhitelistId::CSD)->second; |
1794 } | 1804 } |
1795 | 1805 |
1796 SafeBrowsingDatabaseNew::PrefixGetHashCache* | 1806 SafeBrowsingDatabaseNew::PrefixGetHashCache* |
1797 SafeBrowsingDatabaseNew::GetUnsynchronizedPrefixGetHashCacheForTesting() { | 1807 SafeBrowsingDatabaseNew::GetUnsynchronizedPrefixGetHashCacheForTesting() { |
1798 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); | 1808 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); |
1799 return txn->prefix_gethash_cache(); | 1809 return txn->prefix_gethash_cache(); |
1800 } | 1810 } |
1811 | |
1812 void SafeBrowsingDatabaseNew::RecordPrefixSetCountsHistogram( | |
1813 const std::string& histogram_name_base, | |
1814 PrefixSetId prefix_set_id, | |
1815 int sample) { | |
1816 std::string histogram_name_full(histogram_name_base); | |
1817 switch (prefix_set_id) { | |
1818 case PrefixSetId::BROWSE: | |
1819 histogram_name_full.append(".Browse"); | |
1820 break; | |
1821 case PrefixSetId::SIDE_EFFECT_FREE_WHITELIST: | |
1822 histogram_name_full.append(".SideEffectFreeWhitelist"); | |
1823 break; | |
1824 case PrefixSetId::UNWANTED_SOFTWARE: | |
1825 histogram_name_full.append(".UnwantedSoftware"); | |
1826 break; | |
1827 } | |
1828 | |
1829 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | |
1830 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | |
1831 histogram_name_full, 1, 1000000, 50, | |
1832 base::HistogramBase::kUmaTargetedHistogramFlag); | |
1833 | |
1834 histogram_pointer->Add(sample); | |
1835 } | |
1836 | |
1837 void SafeBrowsingDatabaseNew::RecordSBWhitelistCountsHistogram( | |
1838 const std::string& histogram_name_base, | |
1839 SBWhitelistId whitelist_id, | |
1840 int sample) { | |
1841 std::string histogram_name_full(histogram_name_base); | |
1842 switch (whitelist_id) { | |
1843 case SBWhitelistId::CSD: | |
1844 histogram_name_full.append(".CSD"); | |
1845 break; | |
1846 case SBWhitelistId::DOWNLOAD: | |
1847 histogram_name_full.append(".DownloadWhitelist"); | |
1848 break; | |
1849 } | |
1850 | |
1851 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | |
1852 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | |
1853 histogram_name_full, 1, 1000000, 50, | |
1854 base::HistogramBase::kUmaTargetedHistogramFlag); | |
1855 | |
1856 histogram_pointer->Add(sample); | |
1857 } | |
Scott Hess - ex-Googler
2014/12/18 23:57:55
I think these might usefully use a helper on the o
gab
2014/12/19 15:25:53
It felt to me like that avoiding the readability o
| |
OLD | NEW |