Chromium Code Reviews| 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 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 482 DCHECK(browse_store_.get()); | 482 DCHECK(browse_store_.get()); |
| 483 } | 483 } |
| 484 | 484 |
| 485 SafeBrowsingDatabaseNew::~SafeBrowsingDatabaseNew() { | 485 SafeBrowsingDatabaseNew::~SafeBrowsingDatabaseNew() { |
| 486 // The DCHECK is disabled due to crbug.com/338486 . | 486 // The DCHECK is disabled due to crbug.com/338486 . |
| 487 // DCHECK_EQ(creation_loop_, base::MessageLoop::current()); | 487 // DCHECK_EQ(creation_loop_, base::MessageLoop::current()); |
| 488 } | 488 } |
| 489 | 489 |
| 490 void SafeBrowsingDatabaseNew::Init(const base::FilePath& filename_base) { | 490 void SafeBrowsingDatabaseNew::Init(const base::FilePath& filename_base) { |
| 491 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); | 491 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); |
| 492 // Ensure we haven't been run before. | |
| 493 DCHECK(browse_filename_.empty()); | |
| 494 DCHECK(download_filename_.empty()); | |
| 495 DCHECK(csd_whitelist_filename_.empty()); | |
| 496 DCHECK(download_whitelist_filename_.empty()); | |
| 497 DCHECK(extension_blacklist_filename_.empty()); | |
| 498 DCHECK(side_effect_free_whitelist_filename_.empty()); | |
| 499 DCHECK(ip_blacklist_filename_.empty()); | |
| 500 | 492 |
| 501 browse_filename_ = BrowseDBFilename(filename_base); | 493 // This should not be run multiple times. |
| 502 browse_prefix_set_filename_ = PrefixSetForFilename(browse_filename_); | 494 DCHECK(filename_base_.empty()); |
| 495 | |
| 496 filename_base_ = filename_base; | |
| 497 | |
| 498 // TODO(shess): The various stores are really only necessary while doing | |
| 499 // updates, or when querying a store directly (see |ContainsDownloadUrl()|). | |
| 500 // The store variables are also tested to see if a list is enabled. Perhaps | |
| 501 // the stores could be refactored into an update object so that they are only | |
| 502 // live in memory while being actively used. The sense of enabled probably | |
| 503 // belongs in protocol_manager or database_manager. | |
| 503 | 504 |
| 504 browse_store_->Init( | 505 browse_store_->Init( |
| 505 browse_filename_, | 506 BrowseDBFilename(filename_base_), |
| 506 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 507 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
| 507 base::Unretained(this))); | 508 base::Unretained(this))); |
| 508 | 509 |
| 509 { | 510 { |
| 510 // NOTE: There is no need to grab the lock in this function, since | 511 // NOTE: There is no need to grab the lock in this function, since |
| 511 // until it returns, there are no pointers to this class on other | 512 // until it returns, there are no pointers to this class on other |
| 512 // threads. Then again, that means there is no possibility of | 513 // threads. Then again, that means there is no possibility of |
| 513 // contention on the lock... | 514 // contention on the lock... |
| 514 base::AutoLock locked(lookup_lock_); | 515 base::AutoLock locked(lookup_lock_); |
| 515 cached_browse_hashes_.clear(); | 516 cached_browse_hashes_.clear(); |
| 516 LoadPrefixSet(); | 517 LoadPrefixSet(); |
| 517 } | 518 } |
| 518 | 519 |
| 519 if (download_store_.get()) { | 520 if (download_store_.get()) { |
| 520 download_filename_ = DownloadDBFilename(filename_base); | |
| 521 download_store_->Init( | 521 download_store_->Init( |
| 522 download_filename_, | 522 DownloadDBFilename(filename_base_), |
| 523 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 523 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
| 524 base::Unretained(this))); | 524 base::Unretained(this))); |
| 525 } | 525 } |
| 526 | 526 |
| 527 if (csd_whitelist_store_.get()) { | 527 if (csd_whitelist_store_.get()) { |
| 528 csd_whitelist_filename_ = CsdWhitelistDBFilename(filename_base); | |
| 529 csd_whitelist_store_->Init( | 528 csd_whitelist_store_->Init( |
| 530 csd_whitelist_filename_, | 529 CsdWhitelistDBFilename(filename_base_), |
| 531 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 530 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
| 532 base::Unretained(this))); | 531 base::Unretained(this))); |
| 533 | 532 |
| 534 std::vector<SBAddFullHash> full_hashes; | 533 std::vector<SBAddFullHash> full_hashes; |
| 535 if (csd_whitelist_store_->GetAddFullHashes(&full_hashes)) { | 534 if (csd_whitelist_store_->GetAddFullHashes(&full_hashes)) { |
| 536 LoadWhitelist(full_hashes, &csd_whitelist_); | 535 LoadWhitelist(full_hashes, &csd_whitelist_); |
| 537 } else { | 536 } else { |
| 538 WhitelistEverything(&csd_whitelist_); | 537 WhitelistEverything(&csd_whitelist_); |
| 539 } | 538 } |
| 540 } else { | 539 } else { |
| 541 WhitelistEverything(&csd_whitelist_); // Just to be safe. | 540 WhitelistEverything(&csd_whitelist_); // Just to be safe. |
| 542 } | 541 } |
| 543 | 542 |
| 544 if (download_whitelist_store_.get()) { | 543 if (download_whitelist_store_.get()) { |
| 545 download_whitelist_filename_ = DownloadWhitelistDBFilename(filename_base); | |
| 546 download_whitelist_store_->Init( | 544 download_whitelist_store_->Init( |
| 547 download_whitelist_filename_, | 545 DownloadWhitelistDBFilename(filename_base_), |
| 548 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 546 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
| 549 base::Unretained(this))); | 547 base::Unretained(this))); |
| 550 | 548 |
| 551 std::vector<SBAddFullHash> full_hashes; | 549 std::vector<SBAddFullHash> full_hashes; |
| 552 if (download_whitelist_store_->GetAddFullHashes(&full_hashes)) { | 550 if (download_whitelist_store_->GetAddFullHashes(&full_hashes)) { |
| 553 LoadWhitelist(full_hashes, &download_whitelist_); | 551 LoadWhitelist(full_hashes, &download_whitelist_); |
| 554 } else { | 552 } else { |
| 555 WhitelistEverything(&download_whitelist_); | 553 WhitelistEverything(&download_whitelist_); |
| 556 } | 554 } |
| 557 } else { | 555 } else { |
| 558 WhitelistEverything(&download_whitelist_); // Just to be safe. | 556 WhitelistEverything(&download_whitelist_); // Just to be safe. |
| 559 } | 557 } |
| 560 | 558 |
| 561 if (extension_blacklist_store_.get()) { | 559 if (extension_blacklist_store_.get()) { |
| 562 extension_blacklist_filename_ = ExtensionBlacklistDBFilename(filename_base); | |
| 563 extension_blacklist_store_->Init( | 560 extension_blacklist_store_->Init( |
| 564 extension_blacklist_filename_, | 561 ExtensionBlacklistDBFilename(filename_base_), |
| 565 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 562 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
| 566 base::Unretained(this))); | 563 base::Unretained(this))); |
| 567 } | 564 } |
| 568 | 565 |
| 569 if (side_effect_free_whitelist_store_.get()) { | 566 if (side_effect_free_whitelist_store_.get()) { |
| 570 side_effect_free_whitelist_filename_ = | 567 const base::FilePath side_effect_free_whitelist_filename = |
| 571 SideEffectFreeWhitelistDBFilename(filename_base); | 568 SideEffectFreeWhitelistDBFilename(filename_base_); |
| 572 side_effect_free_whitelist_prefix_set_filename_ = | 569 const base::FilePath side_effect_free_whitelist_prefix_set_filename = |
| 573 PrefixSetForFilename(side_effect_free_whitelist_filename_); | 570 PrefixSetForFilename(side_effect_free_whitelist_filename); |
| 574 side_effect_free_whitelist_store_->Init( | 571 side_effect_free_whitelist_store_->Init( |
| 575 side_effect_free_whitelist_filename_, | 572 side_effect_free_whitelist_filename, |
| 576 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 573 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
| 577 base::Unretained(this))); | 574 base::Unretained(this))); |
| 578 | 575 |
| 579 // If there is no database, the filter cannot be used. | 576 // Only use the prefix set if database is present and non-empty. |
| 580 base::File::Info db_info; | 577 if (GetFileSizeOrZero(side_effect_free_whitelist_filename)) { |
| 581 if (base::GetFileInfo(side_effect_free_whitelist_filename_, &db_info) | |
| 582 && db_info.size != 0) { | |
| 583 const base::TimeTicks before = base::TimeTicks::Now(); | 578 const base::TimeTicks before = base::TimeTicks::Now(); |
| 584 side_effect_free_whitelist_prefix_set_ = | 579 side_effect_free_whitelist_prefix_set_ = |
| 585 safe_browsing::PrefixSet::LoadFile( | 580 safe_browsing::PrefixSet::LoadFile( |
| 586 side_effect_free_whitelist_prefix_set_filename_); | 581 side_effect_free_whitelist_prefix_set_filename); |
| 587 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreeWhitelistPrefixSetLoad", | 582 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreeWhitelistPrefixSetLoad", |
| 588 base::TimeTicks::Now() - before); | 583 base::TimeTicks::Now() - before); |
| 589 if (!side_effect_free_whitelist_prefix_set_.get()) | 584 if (!side_effect_free_whitelist_prefix_set_.get()) |
| 590 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_READ); | 585 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_READ); |
| 591 } | 586 } |
| 592 } else { | 587 } else { |
| 593 // Delete any files of the side-effect free sidelist that may be around | 588 // Delete any files of the side-effect free sidelist that may be around |
| 594 // from when it was previously enabled. | 589 // from when it was previously enabled. |
| 595 SafeBrowsingStoreFile::DeleteStore( | 590 SafeBrowsingStoreFile::DeleteStore( |
| 596 SideEffectFreeWhitelistDBFilename(filename_base)); | 591 SideEffectFreeWhitelistDBFilename(filename_base_)); |
| 592 base::DeleteFile( | |
| 593 PrefixSetForFilename(SideEffectFreeWhitelistDBFilename(filename_base_)), | |
| 594 false); | |
|
Scott Hess - ex-Googler
2014/06/18 22:07:17
https://codereview.chromium.org/341553006/ adds th
| |
| 597 } | 595 } |
| 598 | 596 |
| 599 if (ip_blacklist_store_.get()) { | 597 if (ip_blacklist_store_.get()) { |
| 600 ip_blacklist_filename_ = IpBlacklistDBFilename(filename_base); | |
| 601 ip_blacklist_store_->Init( | 598 ip_blacklist_store_->Init( |
| 602 ip_blacklist_filename_, | 599 IpBlacklistDBFilename(filename_base_), |
| 603 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 600 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
| 604 base::Unretained(this))); | 601 base::Unretained(this))); |
| 605 | 602 |
| 606 std::vector<SBAddFullHash> full_hashes; | 603 std::vector<SBAddFullHash> full_hashes; |
| 607 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) { | 604 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) { |
| 608 LoadIpBlacklist(full_hashes); | 605 LoadIpBlacklist(full_hashes); |
| 609 } else { | 606 } else { |
| 610 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. | 607 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. |
| 611 } | 608 } |
| 612 } | 609 } |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1117 extension_blacklist_store_->CancelUpdate(); | 1114 extension_blacklist_store_->CancelUpdate(); |
| 1118 if (side_effect_free_whitelist_store_) | 1115 if (side_effect_free_whitelist_store_) |
| 1119 side_effect_free_whitelist_store_->CancelUpdate(); | 1116 side_effect_free_whitelist_store_->CancelUpdate(); |
| 1120 if (ip_blacklist_store_) | 1117 if (ip_blacklist_store_) |
| 1121 ip_blacklist_store_->CancelUpdate(); | 1118 ip_blacklist_store_->CancelUpdate(); |
| 1122 return; | 1119 return; |
| 1123 } | 1120 } |
| 1124 | 1121 |
| 1125 if (download_store_) { | 1122 if (download_store_) { |
| 1126 int64 size_bytes = UpdateHashPrefixStore( | 1123 int64 size_bytes = UpdateHashPrefixStore( |
| 1127 download_filename_, | 1124 DownloadDBFilename(filename_base_), |
| 1128 download_store_.get(), | 1125 download_store_.get(), |
| 1129 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); | 1126 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); |
| 1130 UMA_HISTOGRAM_COUNTS("SB2.DownloadDatabaseKilobytes", | 1127 UMA_HISTOGRAM_COUNTS("SB2.DownloadDatabaseKilobytes", |
| 1131 static_cast<int>(size_bytes / 1024)); | 1128 static_cast<int>(size_bytes / 1024)); |
| 1132 } | 1129 } |
| 1133 | 1130 |
| 1134 UpdateBrowseStore(); | 1131 UpdateBrowseStore(); |
| 1135 UpdateWhitelistStore(csd_whitelist_filename_, | 1132 UpdateWhitelistStore(CsdWhitelistDBFilename(filename_base_), |
| 1136 csd_whitelist_store_.get(), | 1133 csd_whitelist_store_.get(), |
| 1137 &csd_whitelist_); | 1134 &csd_whitelist_); |
| 1138 UpdateWhitelistStore(download_whitelist_filename_, | 1135 UpdateWhitelistStore(DownloadWhitelistDBFilename(filename_base_), |
| 1139 download_whitelist_store_.get(), | 1136 download_whitelist_store_.get(), |
| 1140 &download_whitelist_); | 1137 &download_whitelist_); |
| 1141 | 1138 |
| 1142 if (extension_blacklist_store_) { | 1139 if (extension_blacklist_store_) { |
| 1143 int64 size_bytes = UpdateHashPrefixStore( | 1140 int64 size_bytes = UpdateHashPrefixStore( |
| 1144 extension_blacklist_filename_, | 1141 ExtensionBlacklistDBFilename(filename_base_), |
| 1145 extension_blacklist_store_.get(), | 1142 extension_blacklist_store_.get(), |
| 1146 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); | 1143 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); |
| 1147 UMA_HISTOGRAM_COUNTS("SB2.ExtensionBlacklistKilobytes", | 1144 UMA_HISTOGRAM_COUNTS("SB2.ExtensionBlacklistKilobytes", |
| 1148 static_cast<int>(size_bytes / 1024)); | 1145 static_cast<int>(size_bytes / 1024)); |
| 1149 } | 1146 } |
| 1150 | 1147 |
| 1151 if (side_effect_free_whitelist_store_) | 1148 if (side_effect_free_whitelist_store_) |
| 1152 UpdateSideEffectFreeWhitelistStore(); | 1149 UpdateSideEffectFreeWhitelistStore(); |
| 1153 | 1150 |
| 1154 if (ip_blacklist_store_) | 1151 if (ip_blacklist_store_) |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1265 static_cast<int>(io_after.WriteTransferCount - | 1262 static_cast<int>(io_after.WriteTransferCount - |
| 1266 io_before.WriteTransferCount) / 1024); | 1263 io_before.WriteTransferCount) / 1024); |
| 1267 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", | 1264 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", |
| 1268 static_cast<int>(io_after.ReadOperationCount - | 1265 static_cast<int>(io_after.ReadOperationCount - |
| 1269 io_before.ReadOperationCount)); | 1266 io_before.ReadOperationCount)); |
| 1270 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", | 1267 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", |
| 1271 static_cast<int>(io_after.WriteOperationCount - | 1268 static_cast<int>(io_after.WriteOperationCount - |
| 1272 io_before.WriteOperationCount)); | 1269 io_before.WriteOperationCount)); |
| 1273 } | 1270 } |
| 1274 | 1271 |
| 1275 int64 file_size = GetFileSizeOrZero(browse_prefix_set_filename_); | 1272 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); |
| 1276 UMA_HISTOGRAM_COUNTS("SB2.PrefixSetKilobytes", | 1273 const int64 file_size = GetFileSizeOrZero(browse_filename); |
| 1277 static_cast<int>(file_size / 1024)); | |
| 1278 file_size = GetFileSizeOrZero(browse_filename_); | |
| 1279 UMA_HISTOGRAM_COUNTS("SB2.BrowseDatabaseKilobytes", | 1274 UMA_HISTOGRAM_COUNTS("SB2.BrowseDatabaseKilobytes", |
| 1280 static_cast<int>(file_size / 1024)); | 1275 static_cast<int>(file_size / 1024)); |
| 1281 | 1276 |
| 1282 #if defined(OS_MACOSX) | 1277 #if defined(OS_MACOSX) |
| 1283 base::mac::SetFileBackupExclusion(browse_filename_); | 1278 base::mac::SetFileBackupExclusion(browse_filename); |
| 1284 #endif | 1279 #endif |
| 1285 } | 1280 } |
| 1286 | 1281 |
| 1287 void SafeBrowsingDatabaseNew::UpdateSideEffectFreeWhitelistStore() { | 1282 void SafeBrowsingDatabaseNew::UpdateSideEffectFreeWhitelistStore() { |
| 1288 safe_browsing::PrefixSetBuilder builder; | 1283 safe_browsing::PrefixSetBuilder builder; |
| 1289 std::vector<SBAddFullHash> add_full_hashes_result; | 1284 std::vector<SBAddFullHash> add_full_hashes_result; |
| 1290 | 1285 |
| 1291 if (!side_effect_free_whitelist_store_->FinishUpdate( | 1286 if (!side_effect_free_whitelist_store_->FinishUpdate( |
| 1292 &builder, &add_full_hashes_result)) { | 1287 &builder, &add_full_hashes_result)) { |
| 1293 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH); | 1288 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH); |
| 1294 return; | 1289 return; |
| 1295 } | 1290 } |
| 1296 scoped_ptr<safe_browsing::PrefixSet> | 1291 scoped_ptr<safe_browsing::PrefixSet> |
| 1297 prefix_set(builder.GetPrefixSetNoHashes()); | 1292 prefix_set(builder.GetPrefixSetNoHashes()); |
| 1298 | 1293 |
| 1299 // Swap in the newly built prefix set. | 1294 // Swap in the newly built prefix set. |
| 1300 { | 1295 { |
| 1301 base::AutoLock locked(lookup_lock_); | 1296 base::AutoLock locked(lookup_lock_); |
| 1302 side_effect_free_whitelist_prefix_set_.swap(prefix_set); | 1297 side_effect_free_whitelist_prefix_set_.swap(prefix_set); |
| 1303 } | 1298 } |
| 1304 | 1299 |
| 1300 const base::FilePath side_effect_free_whitelist_filename = | |
| 1301 SideEffectFreeWhitelistDBFilename(filename_base_); | |
| 1302 const base::FilePath side_effect_free_whitelist_prefix_set_filename = | |
| 1303 PrefixSetForFilename(side_effect_free_whitelist_filename); | |
| 1305 const base::TimeTicks before = base::TimeTicks::Now(); | 1304 const base::TimeTicks before = base::TimeTicks::Now(); |
| 1306 const bool write_ok = side_effect_free_whitelist_prefix_set_->WriteFile( | 1305 const bool write_ok = side_effect_free_whitelist_prefix_set_->WriteFile( |
| 1307 side_effect_free_whitelist_prefix_set_filename_); | 1306 side_effect_free_whitelist_prefix_set_filename); |
| 1308 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreePrefixSetWrite", | 1307 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreePrefixSetWrite", |
| 1309 base::TimeTicks::Now() - before); | 1308 base::TimeTicks::Now() - before); |
| 1310 | 1309 |
| 1311 if (!write_ok) | 1310 if (!write_ok) |
| 1312 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE); | 1311 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE); |
| 1313 | 1312 |
| 1314 // Gather statistics. | 1313 // Gather statistics. |
| 1315 int64 file_size = GetFileSizeOrZero( | 1314 int64 file_size = GetFileSizeOrZero( |
| 1316 side_effect_free_whitelist_prefix_set_filename_); | 1315 side_effect_free_whitelist_prefix_set_filename); |
| 1317 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistPrefixSetKilobytes", | 1316 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistPrefixSetKilobytes", |
| 1318 static_cast<int>(file_size / 1024)); | 1317 static_cast<int>(file_size / 1024)); |
| 1319 file_size = GetFileSizeOrZero(side_effect_free_whitelist_filename_); | 1318 file_size = GetFileSizeOrZero(side_effect_free_whitelist_filename); |
| 1320 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistDatabaseKilobytes", | 1319 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistDatabaseKilobytes", |
| 1321 static_cast<int>(file_size / 1024)); | 1320 static_cast<int>(file_size / 1024)); |
| 1322 | 1321 |
| 1323 #if defined(OS_MACOSX) | 1322 #if defined(OS_MACOSX) |
| 1324 base::mac::SetFileBackupExclusion(side_effect_free_whitelist_filename_); | 1323 base::mac::SetFileBackupExclusion(side_effect_free_whitelist_filename); |
| 1325 base::mac::SetFileBackupExclusion( | 1324 base::mac::SetFileBackupExclusion( |
| 1326 side_effect_free_whitelist_prefix_set_filename_); | 1325 side_effect_free_whitelist_prefix_set_filename); |
| 1327 #endif | 1326 #endif |
| 1328 } | 1327 } |
| 1329 | 1328 |
| 1330 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { | 1329 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { |
| 1331 // Note: prefixes will not be empty. The current data store implementation | 1330 // Note: prefixes will not be empty. The current data store implementation |
| 1332 // stores all full-length hashes as both full and prefix hashes. | 1331 // stores all full-length hashes as both full and prefix hashes. |
| 1333 safe_browsing::PrefixSetBuilder builder; | 1332 safe_browsing::PrefixSetBuilder builder; |
| 1334 std::vector<SBAddFullHash> full_hashes; | 1333 std::vector<SBAddFullHash> full_hashes; |
| 1335 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { | 1334 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { |
| 1336 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); | 1335 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); |
| 1337 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. | 1336 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. |
| 1338 return; | 1337 return; |
| 1339 } | 1338 } |
| 1340 | 1339 |
| 1341 #if defined(OS_MACOSX) | 1340 #if defined(OS_MACOSX) |
| 1342 base::mac::SetFileBackupExclusion(ip_blacklist_filename_); | 1341 base::mac::SetFileBackupExclusion(IpBlacklistDBFilename(filename_base_)); |
| 1343 #endif | 1342 #endif |
| 1344 | 1343 |
| 1345 LoadIpBlacklist(full_hashes); | 1344 LoadIpBlacklist(full_hashes); |
| 1346 } | 1345 } |
| 1347 | 1346 |
| 1348 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { | 1347 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { |
| 1349 // Reset the database after the current task has unwound (but only | 1348 // Reset the database after the current task has unwound (but only |
| 1350 // reset once within the scope of a given task). | 1349 // reset once within the scope of a given task). |
| 1351 if (!reset_factory_.HasWeakPtrs()) { | 1350 if (!reset_factory_.HasWeakPtrs()) { |
| 1352 RecordFailure(FAILURE_DATABASE_CORRUPT); | 1351 RecordFailure(FAILURE_DATABASE_CORRUPT); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 1365 // only happen once. If you are here because you are hitting this after a | 1364 // only happen once. If you are here because you are hitting this after a |
| 1366 // restart, then I would be very interested in working with you to figure out | 1365 // restart, then I would be very interested in working with you to figure out |
| 1367 // what is happening, since it may affect real users. | 1366 // what is happening, since it may affect real users. |
| 1368 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset"; | 1367 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset"; |
| 1369 } | 1368 } |
| 1370 | 1369 |
| 1371 // TODO(shess): I'm not clear why this code doesn't have any | 1370 // TODO(shess): I'm not clear why this code doesn't have any |
| 1372 // real error-handling. | 1371 // real error-handling. |
| 1373 void SafeBrowsingDatabaseNew::LoadPrefixSet() { | 1372 void SafeBrowsingDatabaseNew::LoadPrefixSet() { |
| 1374 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); | 1373 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); |
| 1375 DCHECK(!browse_prefix_set_filename_.empty()); | 1374 DCHECK(!filename_base_.empty()); |
| 1376 | 1375 |
| 1377 // If there is no database, the filter cannot be used. | 1376 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); |
| 1378 base::File::Info db_info; | 1377 const base::FilePath browse_prefix_set_filename = |
| 1379 if (!base::GetFileInfo(browse_filename_, &db_info) || db_info.size == 0) | 1378 PrefixSetForFilename(browse_filename); |
| 1379 | |
| 1380 // Only use the prefix set if database is present and non-empty. | |
| 1381 if (!GetFileSizeOrZero(browse_filename)) | |
| 1380 return; | 1382 return; |
| 1381 | 1383 |
| 1382 // Cleanup any stale bloom filter (no longer used). | 1384 // Cleanup any stale bloom filter (no longer used). |
| 1383 // TODO(shess): Track failure to delete? | 1385 // TODO(shess): Track existence to drive removal of this code? |
| 1384 base::FilePath bloom_filter_filename = | 1386 const base::FilePath bloom_filter_filename = |
| 1385 BloomFilterForFilename(browse_filename_); | 1387 BloomFilterForFilename(browse_filename); |
| 1386 base::DeleteFile(bloom_filter_filename, false); | 1388 base::DeleteFile(bloom_filter_filename, false); |
| 1387 | 1389 |
| 1388 const base::TimeTicks before = base::TimeTicks::Now(); | 1390 const base::TimeTicks before = base::TimeTicks::Now(); |
| 1389 browse_prefix_set_ = safe_browsing::PrefixSet::LoadFile( | 1391 browse_prefix_set_ = safe_browsing::PrefixSet::LoadFile( |
| 1390 browse_prefix_set_filename_); | 1392 browse_prefix_set_filename); |
| 1391 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before); | 1393 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before); |
| 1392 | 1394 |
| 1393 if (!browse_prefix_set_.get()) | 1395 if (!browse_prefix_set_.get()) |
| 1394 RecordFailure(FAILURE_BROWSE_PREFIX_SET_READ); | 1396 RecordFailure(FAILURE_BROWSE_PREFIX_SET_READ); |
| 1395 } | 1397 } |
| 1396 | 1398 |
| 1397 bool SafeBrowsingDatabaseNew::Delete() { | 1399 bool SafeBrowsingDatabaseNew::Delete() { |
| 1398 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); | 1400 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); |
| 1401 DCHECK(!filename_base_.empty()); | |
| 1402 | |
| 1403 // TODO(shess): This is a mess. SafeBrowsingFileStore::Delete() closes the | |
| 1404 // store before calling DeleteStore(). DeleteStore() deletes transient files | |
| 1405 // in addition to the main file. Probably all of these should be converted to | |
| 1406 // a helper which calls Delete() if the store exists, else DeleteStore() on | |
| 1407 // the generated filename. | |
| 1408 | |
| 1409 // TODO(shess): Determine if the histograms are useful in any way. I cannot | |
| 1410 // recall any action taken as a result of their values, in which case it might | |
| 1411 // make more sense to histogram an overall thumbs-up/-down and just dig deeper | |
| 1412 // if something looks wrong. | |
| 1399 | 1413 |
| 1400 const bool r1 = browse_store_->Delete(); | 1414 const bool r1 = browse_store_->Delete(); |
| 1401 if (!r1) | 1415 if (!r1) |
| 1402 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1416 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
| 1403 | 1417 |
| 1404 const bool r2 = download_store_.get() ? download_store_->Delete() : true; | 1418 const bool r2 = download_store_.get() ? download_store_->Delete() : true; |
| 1405 if (!r2) | 1419 if (!r2) |
| 1406 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1420 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
| 1407 | 1421 |
| 1408 const bool r3 = csd_whitelist_store_.get() ? | 1422 const bool r3 = csd_whitelist_store_.get() ? |
| 1409 csd_whitelist_store_->Delete() : true; | 1423 csd_whitelist_store_->Delete() : true; |
| 1410 if (!r3) | 1424 if (!r3) |
| 1411 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1425 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
| 1412 | 1426 |
| 1413 const bool r4 = download_whitelist_store_.get() ? | 1427 const bool r4 = download_whitelist_store_.get() ? |
| 1414 download_whitelist_store_->Delete() : true; | 1428 download_whitelist_store_->Delete() : true; |
| 1415 if (!r4) | 1429 if (!r4) |
| 1416 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1430 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
| 1417 | 1431 |
| 1418 base::FilePath bloom_filter_filename = | 1432 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); |
| 1419 BloomFilterForFilename(browse_filename_); | 1433 const base::FilePath bloom_filter_filename = |
| 1434 BloomFilterForFilename(browse_filename); | |
| 1420 const bool r5 = base::DeleteFile(bloom_filter_filename, false); | 1435 const bool r5 = base::DeleteFile(bloom_filter_filename, false); |
| 1421 if (!r5) | 1436 if (!r5) |
| 1422 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); | 1437 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); |
| 1423 | 1438 |
| 1424 const bool r6 = base::DeleteFile(browse_prefix_set_filename_, false); | 1439 const base::FilePath browse_prefix_set_filename = |
| 1440 PrefixSetForFilename(browse_filename); | |
| 1441 const bool r6 = base::DeleteFile(browse_prefix_set_filename, false); | |
| 1425 if (!r6) | 1442 if (!r6) |
| 1426 RecordFailure(FAILURE_BROWSE_PREFIX_SET_DELETE); | 1443 RecordFailure(FAILURE_BROWSE_PREFIX_SET_DELETE); |
| 1427 | 1444 |
| 1428 const bool r7 = base::DeleteFile(extension_blacklist_filename_, false); | 1445 const base::FilePath extension_blacklist_filename = |
| 1446 ExtensionBlacklistDBFilename(filename_base_); | |
| 1447 const bool r7 = base::DeleteFile(extension_blacklist_filename, false); | |
| 1429 if (!r7) | 1448 if (!r7) |
| 1430 RecordFailure(FAILURE_EXTENSION_BLACKLIST_DELETE); | 1449 RecordFailure(FAILURE_EXTENSION_BLACKLIST_DELETE); |
| 1431 | 1450 |
| 1432 const bool r8 = base::DeleteFile(side_effect_free_whitelist_filename_, | 1451 const base::FilePath side_effect_free_whitelist_filename = |
| 1452 SideEffectFreeWhitelistDBFilename(filename_base_); | |
| 1453 const bool r8 = base::DeleteFile(side_effect_free_whitelist_filename, | |
| 1433 false); | 1454 false); |
| 1434 if (!r8) | 1455 if (!r8) |
| 1435 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_DELETE); | 1456 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_DELETE); |
| 1436 | 1457 |
| 1458 const base::FilePath side_effect_free_whitelist_prefix_set_filename = | |
| 1459 PrefixSetForFilename(side_effect_free_whitelist_filename); | |
| 1437 const bool r9 = base::DeleteFile( | 1460 const bool r9 = base::DeleteFile( |
| 1438 side_effect_free_whitelist_prefix_set_filename_, | 1461 side_effect_free_whitelist_prefix_set_filename, |
| 1439 false); | 1462 false); |
| 1440 if (!r9) | 1463 if (!r9) |
| 1441 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE); | 1464 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE); |
| 1442 | 1465 |
| 1443 const bool r10 = base::DeleteFile(ip_blacklist_filename_, false); | 1466 const bool r10 = base::DeleteFile(IpBlacklistDBFilename(filename_base_), |
| 1467 false); | |
| 1444 if (!r10) | 1468 if (!r10) |
| 1445 RecordFailure(FAILURE_IP_BLACKLIST_DELETE); | 1469 RecordFailure(FAILURE_IP_BLACKLIST_DELETE); |
| 1446 | 1470 |
| 1447 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10; | 1471 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10; |
| 1448 } | 1472 } |
| 1449 | 1473 |
| 1450 void SafeBrowsingDatabaseNew::WritePrefixSet() { | 1474 void SafeBrowsingDatabaseNew::WritePrefixSet() { |
| 1451 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); | 1475 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); |
| 1452 | 1476 |
| 1453 if (!browse_prefix_set_.get()) | 1477 if (!browse_prefix_set_.get()) |
| 1454 return; | 1478 return; |
| 1455 | 1479 |
| 1480 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); | |
| 1481 const base::FilePath browse_prefix_set_filename = | |
| 1482 PrefixSetForFilename(browse_filename); | |
| 1483 | |
| 1456 const base::TimeTicks before = base::TimeTicks::Now(); | 1484 const base::TimeTicks before = base::TimeTicks::Now(); |
| 1457 const bool write_ok = browse_prefix_set_->WriteFile( | 1485 const bool write_ok = browse_prefix_set_->WriteFile( |
| 1458 browse_prefix_set_filename_); | 1486 browse_prefix_set_filename); |
| 1459 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); | 1487 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); |
| 1460 | 1488 |
| 1489 const int64 file_size = GetFileSizeOrZero(browse_prefix_set_filename); | |
| 1490 UMA_HISTOGRAM_COUNTS("SB2.PrefixSetKilobytes", | |
| 1491 static_cast<int>(file_size / 1024)); | |
| 1492 | |
| 1461 if (!write_ok) | 1493 if (!write_ok) |
| 1462 RecordFailure(FAILURE_BROWSE_PREFIX_SET_WRITE); | 1494 RecordFailure(FAILURE_BROWSE_PREFIX_SET_WRITE); |
| 1463 | 1495 |
| 1464 #if defined(OS_MACOSX) | 1496 #if defined(OS_MACOSX) |
| 1465 base::mac::SetFileBackupExclusion(browse_prefix_set_filename_); | 1497 base::mac::SetFileBackupExclusion(browse_prefix_set_filename); |
| 1466 #endif | 1498 #endif |
| 1467 } | 1499 } |
| 1468 | 1500 |
| 1469 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelist* whitelist) { | 1501 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelist* whitelist) { |
| 1470 base::AutoLock locked(lookup_lock_); | 1502 base::AutoLock locked(lookup_lock_); |
| 1471 whitelist->second = true; | 1503 whitelist->second = true; |
| 1472 whitelist->first.clear(); | 1504 whitelist->first.clear(); |
| 1473 } | 1505 } |
| 1474 | 1506 |
| 1475 void SafeBrowsingDatabaseNew::LoadWhitelist( | 1507 void SafeBrowsingDatabaseNew::LoadWhitelist( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1544 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { | 1576 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { |
| 1545 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); | 1577 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); |
| 1546 std::vector<SBFullHash> full_hashes; | 1578 std::vector<SBFullHash> full_hashes; |
| 1547 full_hashes.push_back(malware_kill_switch); | 1579 full_hashes.push_back(malware_kill_switch); |
| 1548 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes); | 1580 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes); |
| 1549 } | 1581 } |
| 1550 | 1582 |
| 1551 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1583 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
| 1552 return csd_whitelist_.second; | 1584 return csd_whitelist_.second; |
| 1553 } | 1585 } |
| OLD | NEW |