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_)); |
597 base::DeleteFile( | 592 base::DeleteFile( |
598 PrefixSetForFilename(SideEffectFreeWhitelistDBFilename(filename_base)), | 593 PrefixSetForFilename(SideEffectFreeWhitelistDBFilename(filename_base_)), |
599 false); | 594 false); |
600 } | 595 } |
601 | 596 |
602 if (ip_blacklist_store_.get()) { | 597 if (ip_blacklist_store_.get()) { |
603 ip_blacklist_filename_ = IpBlacklistDBFilename(filename_base); | |
604 ip_blacklist_store_->Init( | 598 ip_blacklist_store_->Init( |
605 ip_blacklist_filename_, | 599 IpBlacklistDBFilename(filename_base_), |
606 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, | 600 base::Bind(&SafeBrowsingDatabaseNew::HandleCorruptDatabase, |
607 base::Unretained(this))); | 601 base::Unretained(this))); |
608 | 602 |
609 std::vector<SBAddFullHash> full_hashes; | 603 std::vector<SBAddFullHash> full_hashes; |
610 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) { | 604 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) { |
611 LoadIpBlacklist(full_hashes); | 605 LoadIpBlacklist(full_hashes); |
612 } else { | 606 } else { |
613 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. | 607 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. |
614 } | 608 } |
615 } | 609 } |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 extension_blacklist_store_->CancelUpdate(); | 1114 extension_blacklist_store_->CancelUpdate(); |
1121 if (side_effect_free_whitelist_store_) | 1115 if (side_effect_free_whitelist_store_) |
1122 side_effect_free_whitelist_store_->CancelUpdate(); | 1116 side_effect_free_whitelist_store_->CancelUpdate(); |
1123 if (ip_blacklist_store_) | 1117 if (ip_blacklist_store_) |
1124 ip_blacklist_store_->CancelUpdate(); | 1118 ip_blacklist_store_->CancelUpdate(); |
1125 return; | 1119 return; |
1126 } | 1120 } |
1127 | 1121 |
1128 if (download_store_) { | 1122 if (download_store_) { |
1129 int64 size_bytes = UpdateHashPrefixStore( | 1123 int64 size_bytes = UpdateHashPrefixStore( |
1130 download_filename_, | 1124 DownloadDBFilename(filename_base_), |
1131 download_store_.get(), | 1125 download_store_.get(), |
1132 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); | 1126 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH); |
1133 UMA_HISTOGRAM_COUNTS("SB2.DownloadDatabaseKilobytes", | 1127 UMA_HISTOGRAM_COUNTS("SB2.DownloadDatabaseKilobytes", |
1134 static_cast<int>(size_bytes / 1024)); | 1128 static_cast<int>(size_bytes / 1024)); |
1135 } | 1129 } |
1136 | 1130 |
1137 UpdateBrowseStore(); | 1131 UpdateBrowseStore(); |
1138 UpdateWhitelistStore(csd_whitelist_filename_, | 1132 UpdateWhitelistStore(CsdWhitelistDBFilename(filename_base_), |
1139 csd_whitelist_store_.get(), | 1133 csd_whitelist_store_.get(), |
1140 &csd_whitelist_); | 1134 &csd_whitelist_); |
1141 UpdateWhitelistStore(download_whitelist_filename_, | 1135 UpdateWhitelistStore(DownloadWhitelistDBFilename(filename_base_), |
1142 download_whitelist_store_.get(), | 1136 download_whitelist_store_.get(), |
1143 &download_whitelist_); | 1137 &download_whitelist_); |
1144 | 1138 |
1145 if (extension_blacklist_store_) { | 1139 if (extension_blacklist_store_) { |
1146 int64 size_bytes = UpdateHashPrefixStore( | 1140 int64 size_bytes = UpdateHashPrefixStore( |
1147 extension_blacklist_filename_, | 1141 ExtensionBlacklistDBFilename(filename_base_), |
1148 extension_blacklist_store_.get(), | 1142 extension_blacklist_store_.get(), |
1149 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); | 1143 FAILURE_EXTENSION_BLACKLIST_UPDATE_FINISH); |
1150 UMA_HISTOGRAM_COUNTS("SB2.ExtensionBlacklistKilobytes", | 1144 UMA_HISTOGRAM_COUNTS("SB2.ExtensionBlacklistKilobytes", |
1151 static_cast<int>(size_bytes / 1024)); | 1145 static_cast<int>(size_bytes / 1024)); |
1152 } | 1146 } |
1153 | 1147 |
1154 if (side_effect_free_whitelist_store_) | 1148 if (side_effect_free_whitelist_store_) |
1155 UpdateSideEffectFreeWhitelistStore(); | 1149 UpdateSideEffectFreeWhitelistStore(); |
1156 | 1150 |
1157 if (ip_blacklist_store_) | 1151 if (ip_blacklist_store_) |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1268 static_cast<int>(io_after.WriteTransferCount - | 1262 static_cast<int>(io_after.WriteTransferCount - |
1269 io_before.WriteTransferCount) / 1024); | 1263 io_before.WriteTransferCount) / 1024); |
1270 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", | 1264 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", |
1271 static_cast<int>(io_after.ReadOperationCount - | 1265 static_cast<int>(io_after.ReadOperationCount - |
1272 io_before.ReadOperationCount)); | 1266 io_before.ReadOperationCount)); |
1273 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", | 1267 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", |
1274 static_cast<int>(io_after.WriteOperationCount - | 1268 static_cast<int>(io_after.WriteOperationCount - |
1275 io_before.WriteOperationCount)); | 1269 io_before.WriteOperationCount)); |
1276 } | 1270 } |
1277 | 1271 |
1278 int64 file_size = GetFileSizeOrZero(browse_prefix_set_filename_); | 1272 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); |
1279 UMA_HISTOGRAM_COUNTS("SB2.PrefixSetKilobytes", | 1273 const int64 file_size = GetFileSizeOrZero(browse_filename); |
1280 static_cast<int>(file_size / 1024)); | |
1281 file_size = GetFileSizeOrZero(browse_filename_); | |
1282 UMA_HISTOGRAM_COUNTS("SB2.BrowseDatabaseKilobytes", | 1274 UMA_HISTOGRAM_COUNTS("SB2.BrowseDatabaseKilobytes", |
1283 static_cast<int>(file_size / 1024)); | 1275 static_cast<int>(file_size / 1024)); |
1284 | 1276 |
1285 #if defined(OS_MACOSX) | 1277 #if defined(OS_MACOSX) |
1286 base::mac::SetFileBackupExclusion(browse_filename_); | 1278 base::mac::SetFileBackupExclusion(browse_filename); |
1287 #endif | 1279 #endif |
1288 } | 1280 } |
1289 | 1281 |
1290 void SafeBrowsingDatabaseNew::UpdateSideEffectFreeWhitelistStore() { | 1282 void SafeBrowsingDatabaseNew::UpdateSideEffectFreeWhitelistStore() { |
1291 safe_browsing::PrefixSetBuilder builder; | 1283 safe_browsing::PrefixSetBuilder builder; |
1292 std::vector<SBAddFullHash> add_full_hashes_result; | 1284 std::vector<SBAddFullHash> add_full_hashes_result; |
1293 | 1285 |
1294 if (!side_effect_free_whitelist_store_->FinishUpdate( | 1286 if (!side_effect_free_whitelist_store_->FinishUpdate( |
1295 &builder, &add_full_hashes_result)) { | 1287 &builder, &add_full_hashes_result)) { |
1296 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH); | 1288 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_UPDATE_FINISH); |
1297 return; | 1289 return; |
1298 } | 1290 } |
1299 scoped_ptr<safe_browsing::PrefixSet> | 1291 scoped_ptr<safe_browsing::PrefixSet> |
1300 prefix_set(builder.GetPrefixSetNoHashes()); | 1292 prefix_set(builder.GetPrefixSetNoHashes()); |
1301 | 1293 |
1302 // Swap in the newly built prefix set. | 1294 // Swap in the newly built prefix set. |
1303 { | 1295 { |
1304 base::AutoLock locked(lookup_lock_); | 1296 base::AutoLock locked(lookup_lock_); |
1305 side_effect_free_whitelist_prefix_set_.swap(prefix_set); | 1297 side_effect_free_whitelist_prefix_set_.swap(prefix_set); |
1306 } | 1298 } |
1307 | 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); |
1308 const base::TimeTicks before = base::TimeTicks::Now(); | 1304 const base::TimeTicks before = base::TimeTicks::Now(); |
1309 const bool write_ok = side_effect_free_whitelist_prefix_set_->WriteFile( | 1305 const bool write_ok = side_effect_free_whitelist_prefix_set_->WriteFile( |
1310 side_effect_free_whitelist_prefix_set_filename_); | 1306 side_effect_free_whitelist_prefix_set_filename); |
1311 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreePrefixSetWrite", | 1307 UMA_HISTOGRAM_TIMES("SB2.SideEffectFreePrefixSetWrite", |
1312 base::TimeTicks::Now() - before); | 1308 base::TimeTicks::Now() - before); |
1313 | 1309 |
1314 if (!write_ok) | 1310 if (!write_ok) |
1315 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE); | 1311 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_WRITE); |
1316 | 1312 |
1317 // Gather statistics. | 1313 // Gather statistics. |
1318 int64 file_size = GetFileSizeOrZero( | 1314 int64 file_size = GetFileSizeOrZero( |
1319 side_effect_free_whitelist_prefix_set_filename_); | 1315 side_effect_free_whitelist_prefix_set_filename); |
1320 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistPrefixSetKilobytes", | 1316 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistPrefixSetKilobytes", |
1321 static_cast<int>(file_size / 1024)); | 1317 static_cast<int>(file_size / 1024)); |
1322 file_size = GetFileSizeOrZero(side_effect_free_whitelist_filename_); | 1318 file_size = GetFileSizeOrZero(side_effect_free_whitelist_filename); |
1323 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistDatabaseKilobytes", | 1319 UMA_HISTOGRAM_COUNTS("SB2.SideEffectFreeWhitelistDatabaseKilobytes", |
1324 static_cast<int>(file_size / 1024)); | 1320 static_cast<int>(file_size / 1024)); |
1325 | 1321 |
1326 #if defined(OS_MACOSX) | 1322 #if defined(OS_MACOSX) |
1327 base::mac::SetFileBackupExclusion(side_effect_free_whitelist_filename_); | 1323 base::mac::SetFileBackupExclusion(side_effect_free_whitelist_filename); |
1328 base::mac::SetFileBackupExclusion( | 1324 base::mac::SetFileBackupExclusion( |
1329 side_effect_free_whitelist_prefix_set_filename_); | 1325 side_effect_free_whitelist_prefix_set_filename); |
1330 #endif | 1326 #endif |
1331 } | 1327 } |
1332 | 1328 |
1333 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { | 1329 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { |
1334 // Note: prefixes will not be empty. The current data store implementation | 1330 // Note: prefixes will not be empty. The current data store implementation |
1335 // stores all full-length hashes as both full and prefix hashes. | 1331 // stores all full-length hashes as both full and prefix hashes. |
1336 safe_browsing::PrefixSetBuilder builder; | 1332 safe_browsing::PrefixSetBuilder builder; |
1337 std::vector<SBAddFullHash> full_hashes; | 1333 std::vector<SBAddFullHash> full_hashes; |
1338 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { | 1334 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { |
1339 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); | 1335 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); |
1340 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. | 1336 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. |
1341 return; | 1337 return; |
1342 } | 1338 } |
1343 | 1339 |
1344 #if defined(OS_MACOSX) | 1340 #if defined(OS_MACOSX) |
1345 base::mac::SetFileBackupExclusion(ip_blacklist_filename_); | 1341 base::mac::SetFileBackupExclusion(IpBlacklistDBFilename(filename_base_)); |
1346 #endif | 1342 #endif |
1347 | 1343 |
1348 LoadIpBlacklist(full_hashes); | 1344 LoadIpBlacklist(full_hashes); |
1349 } | 1345 } |
1350 | 1346 |
1351 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { | 1347 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { |
1352 // Reset the database after the current task has unwound (but only | 1348 // Reset the database after the current task has unwound (but only |
1353 // reset once within the scope of a given task). | 1349 // reset once within the scope of a given task). |
1354 if (!reset_factory_.HasWeakPtrs()) { | 1350 if (!reset_factory_.HasWeakPtrs()) { |
1355 RecordFailure(FAILURE_DATABASE_CORRUPT); | 1351 RecordFailure(FAILURE_DATABASE_CORRUPT); |
(...skipping 12 matching lines...) Expand all Loading... |
1368 // 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 |
1369 // 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 |
1370 // what is happening, since it may affect real users. | 1366 // what is happening, since it may affect real users. |
1371 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset"; | 1367 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset"; |
1372 } | 1368 } |
1373 | 1369 |
1374 // 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 |
1375 // real error-handling. | 1371 // real error-handling. |
1376 void SafeBrowsingDatabaseNew::LoadPrefixSet() { | 1372 void SafeBrowsingDatabaseNew::LoadPrefixSet() { |
1377 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); | 1373 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); |
1378 DCHECK(!browse_prefix_set_filename_.empty()); | 1374 DCHECK(!filename_base_.empty()); |
1379 | 1375 |
1380 // If there is no database, the filter cannot be used. | 1376 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); |
1381 base::File::Info db_info; | 1377 const base::FilePath browse_prefix_set_filename = |
1382 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)) |
1383 return; | 1382 return; |
1384 | 1383 |
1385 // Cleanup any stale bloom filter (no longer used). | 1384 // Cleanup any stale bloom filter (no longer used). |
1386 // TODO(shess): Track failure to delete? | 1385 // TODO(shess): Track existence to drive removal of this code? |
1387 base::FilePath bloom_filter_filename = | 1386 const base::FilePath bloom_filter_filename = |
1388 BloomFilterForFilename(browse_filename_); | 1387 BloomFilterForFilename(browse_filename); |
1389 base::DeleteFile(bloom_filter_filename, false); | 1388 base::DeleteFile(bloom_filter_filename, false); |
1390 | 1389 |
1391 const base::TimeTicks before = base::TimeTicks::Now(); | 1390 const base::TimeTicks before = base::TimeTicks::Now(); |
1392 browse_prefix_set_ = safe_browsing::PrefixSet::LoadFile( | 1391 browse_prefix_set_ = safe_browsing::PrefixSet::LoadFile( |
1393 browse_prefix_set_filename_); | 1392 browse_prefix_set_filename); |
1394 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before); | 1393 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before); |
1395 | 1394 |
1396 if (!browse_prefix_set_.get()) | 1395 if (!browse_prefix_set_.get()) |
1397 RecordFailure(FAILURE_BROWSE_PREFIX_SET_READ); | 1396 RecordFailure(FAILURE_BROWSE_PREFIX_SET_READ); |
1398 } | 1397 } |
1399 | 1398 |
1400 bool SafeBrowsingDatabaseNew::Delete() { | 1399 bool SafeBrowsingDatabaseNew::Delete() { |
1401 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. |
1402 | 1413 |
1403 const bool r1 = browse_store_->Delete(); | 1414 const bool r1 = browse_store_->Delete(); |
1404 if (!r1) | 1415 if (!r1) |
1405 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1416 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1406 | 1417 |
1407 const bool r2 = download_store_.get() ? download_store_->Delete() : true; | 1418 const bool r2 = download_store_.get() ? download_store_->Delete() : true; |
1408 if (!r2) | 1419 if (!r2) |
1409 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1420 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1410 | 1421 |
1411 const bool r3 = csd_whitelist_store_.get() ? | 1422 const bool r3 = csd_whitelist_store_.get() ? |
1412 csd_whitelist_store_->Delete() : true; | 1423 csd_whitelist_store_->Delete() : true; |
1413 if (!r3) | 1424 if (!r3) |
1414 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1425 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1415 | 1426 |
1416 const bool r4 = download_whitelist_store_.get() ? | 1427 const bool r4 = download_whitelist_store_.get() ? |
1417 download_whitelist_store_->Delete() : true; | 1428 download_whitelist_store_->Delete() : true; |
1418 if (!r4) | 1429 if (!r4) |
1419 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1430 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1420 | 1431 |
1421 base::FilePath bloom_filter_filename = | 1432 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); |
1422 BloomFilterForFilename(browse_filename_); | 1433 const base::FilePath bloom_filter_filename = |
| 1434 BloomFilterForFilename(browse_filename); |
1423 const bool r5 = base::DeleteFile(bloom_filter_filename, false); | 1435 const bool r5 = base::DeleteFile(bloom_filter_filename, false); |
1424 if (!r5) | 1436 if (!r5) |
1425 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); | 1437 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); |
1426 | 1438 |
1427 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); |
1428 if (!r6) | 1442 if (!r6) |
1429 RecordFailure(FAILURE_BROWSE_PREFIX_SET_DELETE); | 1443 RecordFailure(FAILURE_BROWSE_PREFIX_SET_DELETE); |
1430 | 1444 |
1431 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); |
1432 if (!r7) | 1448 if (!r7) |
1433 RecordFailure(FAILURE_EXTENSION_BLACKLIST_DELETE); | 1449 RecordFailure(FAILURE_EXTENSION_BLACKLIST_DELETE); |
1434 | 1450 |
1435 const bool r8 = base::DeleteFile(side_effect_free_whitelist_filename_, | 1451 const base::FilePath side_effect_free_whitelist_filename = |
1436 false); | 1452 SideEffectFreeWhitelistDBFilename(filename_base_); |
| 1453 const bool r8 = base::DeleteFile(side_effect_free_whitelist_filename, |
| 1454 false); |
1437 if (!r8) | 1455 if (!r8) |
1438 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_DELETE); | 1456 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_DELETE); |
1439 | 1457 |
| 1458 const base::FilePath side_effect_free_whitelist_prefix_set_filename = |
| 1459 PrefixSetForFilename(side_effect_free_whitelist_filename); |
1440 const bool r9 = base::DeleteFile( | 1460 const bool r9 = base::DeleteFile( |
1441 side_effect_free_whitelist_prefix_set_filename_, | 1461 side_effect_free_whitelist_prefix_set_filename, |
1442 false); | 1462 false); |
1443 if (!r9) | 1463 if (!r9) |
1444 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE); | 1464 RecordFailure(FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE); |
1445 | 1465 |
1446 const bool r10 = base::DeleteFile(ip_blacklist_filename_, false); | 1466 const bool r10 = base::DeleteFile(IpBlacklistDBFilename(filename_base_), |
| 1467 false); |
1447 if (!r10) | 1468 if (!r10) |
1448 RecordFailure(FAILURE_IP_BLACKLIST_DELETE); | 1469 RecordFailure(FAILURE_IP_BLACKLIST_DELETE); |
1449 | 1470 |
1450 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10; | 1471 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10; |
1451 } | 1472 } |
1452 | 1473 |
1453 void SafeBrowsingDatabaseNew::WritePrefixSet() { | 1474 void SafeBrowsingDatabaseNew::WritePrefixSet() { |
1454 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); | 1475 DCHECK_EQ(creation_loop_, base::MessageLoop::current()); |
1455 | 1476 |
1456 if (!browse_prefix_set_.get()) | 1477 if (!browse_prefix_set_.get()) |
1457 return; | 1478 return; |
1458 | 1479 |
| 1480 const base::FilePath browse_filename = BrowseDBFilename(filename_base_); |
| 1481 const base::FilePath browse_prefix_set_filename = |
| 1482 PrefixSetForFilename(browse_filename); |
| 1483 |
1459 const base::TimeTicks before = base::TimeTicks::Now(); | 1484 const base::TimeTicks before = base::TimeTicks::Now(); |
1460 const bool write_ok = browse_prefix_set_->WriteFile( | 1485 const bool write_ok = browse_prefix_set_->WriteFile( |
1461 browse_prefix_set_filename_); | 1486 browse_prefix_set_filename); |
1462 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); | 1487 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); |
1463 | 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 |
1464 if (!write_ok) | 1493 if (!write_ok) |
1465 RecordFailure(FAILURE_BROWSE_PREFIX_SET_WRITE); | 1494 RecordFailure(FAILURE_BROWSE_PREFIX_SET_WRITE); |
1466 | 1495 |
1467 #if defined(OS_MACOSX) | 1496 #if defined(OS_MACOSX) |
1468 base::mac::SetFileBackupExclusion(browse_prefix_set_filename_); | 1497 base::mac::SetFileBackupExclusion(browse_prefix_set_filename); |
1469 #endif | 1498 #endif |
1470 } | 1499 } |
1471 | 1500 |
1472 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelist* whitelist) { | 1501 void SafeBrowsingDatabaseNew::WhitelistEverything(SBWhitelist* whitelist) { |
1473 base::AutoLock locked(lookup_lock_); | 1502 base::AutoLock locked(lookup_lock_); |
1474 whitelist->second = true; | 1503 whitelist->second = true; |
1475 whitelist->first.clear(); | 1504 whitelist->first.clear(); |
1476 } | 1505 } |
1477 | 1506 |
1478 void SafeBrowsingDatabaseNew::LoadWhitelist( | 1507 void SafeBrowsingDatabaseNew::LoadWhitelist( |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1547 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { | 1576 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { |
1548 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); | 1577 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); |
1549 std::vector<SBFullHash> full_hashes; | 1578 std::vector<SBFullHash> full_hashes; |
1550 full_hashes.push_back(malware_kill_switch); | 1579 full_hashes.push_back(malware_kill_switch); |
1551 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes); | 1580 return ContainsWhitelistedHashes(csd_whitelist_, full_hashes); |
1552 } | 1581 } |
1553 | 1582 |
1554 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1583 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
1555 return csd_whitelist_.second; | 1584 return csd_whitelist_.second; |
1556 } | 1585 } |
OLD | NEW |