| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_bloom.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_database_bloom.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 return true; | 354 return true; |
| 355 } | 355 } |
| 356 | 356 |
| 357 void SafeBrowsingDatabaseBloom::InsertChunks(const std::string& list_name, | 357 void SafeBrowsingDatabaseBloom::InsertChunks(const std::string& list_name, |
| 358 std::deque<SBChunk>* chunks) { | 358 std::deque<SBChunk>* chunks) { |
| 359 if (chunks->empty()) | 359 if (chunks->empty()) |
| 360 return; | 360 return; |
| 361 | 361 |
| 362 base::Time insert_start = base::Time::Now(); | 362 base::Time insert_start = base::Time::Now(); |
| 363 | 363 |
| 364 int list_id = GetListId(list_name); | 364 int list_id = safe_browsing_util::GetListId(list_name); |
| 365 ChunkType chunk_type = chunks->front().is_add ? ADD_CHUNK : SUB_CHUNK; | 365 ChunkType chunk_type = chunks->front().is_add ? ADD_CHUNK : SUB_CHUNK; |
| 366 | 366 |
| 367 while (!chunks->empty()) { | 367 while (!chunks->empty()) { |
| 368 SBChunk& chunk = chunks->front(); | 368 SBChunk& chunk = chunks->front(); |
| 369 chunk.list_id = list_id; | 369 chunk.list_id = list_id; |
| 370 int chunk_id = chunk.chunk_number; | 370 int chunk_id = chunk.chunk_number; |
| 371 | 371 |
| 372 // The server can give us a chunk that we already have because it's part of | 372 // The server can give us a chunk that we already have because it's part of |
| 373 // a range. Don't add it again. | 373 // a range. Don't add it again. |
| 374 if (!ChunkExists(list_id, chunk_type, chunk_id)) { | 374 if (!ChunkExists(list_id, chunk_type, chunk_id)) { |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 HandleCorruptDatabase(); | 630 HandleCorruptDatabase(); |
| 631 | 631 |
| 632 return add_count; | 632 return add_count; |
| 633 } | 633 } |
| 634 | 634 |
| 635 void SafeBrowsingDatabaseBloom::DeleteChunks( | 635 void SafeBrowsingDatabaseBloom::DeleteChunks( |
| 636 std::vector<SBChunkDelete>* chunk_deletes) { | 636 std::vector<SBChunkDelete>* chunk_deletes) { |
| 637 if (chunk_deletes->empty()) | 637 if (chunk_deletes->empty()) |
| 638 return; | 638 return; |
| 639 | 639 |
| 640 int list_id = GetListId(chunk_deletes->front().list_name); | 640 int list_id = safe_browsing_util::GetListId(chunk_deletes->front().list_name); |
| 641 | 641 |
| 642 for (size_t i = 0; i < chunk_deletes->size(); ++i) { | 642 for (size_t i = 0; i < chunk_deletes->size(); ++i) { |
| 643 const SBChunkDelete& chunk = (*chunk_deletes)[i]; | 643 const SBChunkDelete& chunk = (*chunk_deletes)[i]; |
| 644 std::vector<int> chunk_numbers; | 644 std::vector<int> chunk_numbers; |
| 645 RangesToChunks(chunk.chunk_del, &chunk_numbers); | 645 RangesToChunks(chunk.chunk_del, &chunk_numbers); |
| 646 for (size_t del = 0; del < chunk_numbers.size(); ++del) { | 646 for (size_t del = 0; del < chunk_numbers.size(); ++del) { |
| 647 int encoded_chunk = EncodeChunkId(chunk_numbers[del], list_id); | 647 int encoded_chunk = EncodeChunkId(chunk_numbers[del], list_id); |
| 648 if (chunk.is_sub_del) | 648 if (chunk.is_sub_del) |
| 649 sub_del_cache_.insert(encoded_chunk); | 649 sub_del_cache_.insert(encoded_chunk); |
| 650 else | 650 else |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 } | 696 } |
| 697 | 697 |
| 698 void SafeBrowsingDatabaseBloom::GetListsInfo( | 698 void SafeBrowsingDatabaseBloom::GetListsInfo( |
| 699 std::vector<SBListChunkRanges>* lists) { | 699 std::vector<SBListChunkRanges>* lists) { |
| 700 DCHECK(lists); | 700 DCHECK(lists); |
| 701 lists->clear(); | 701 lists->clear(); |
| 702 | 702 |
| 703 ReadChunkNumbers(); | 703 ReadChunkNumbers(); |
| 704 | 704 |
| 705 lists->push_back(SBListChunkRanges(safe_browsing_util::kMalwareList)); | 705 lists->push_back(SBListChunkRanges(safe_browsing_util::kMalwareList)); |
| 706 GetChunkIds(MALWARE, ADD_CHUNK, &lists->back().adds); | 706 GetChunkIds(safe_browsing_util::MALWARE, ADD_CHUNK, &lists->back().adds); |
| 707 GetChunkIds(MALWARE, SUB_CHUNK, &lists->back().subs); | 707 GetChunkIds(safe_browsing_util::MALWARE, SUB_CHUNK, &lists->back().subs); |
| 708 | 708 |
| 709 lists->push_back(SBListChunkRanges(safe_browsing_util::kPhishingList)); | 709 lists->push_back(SBListChunkRanges(safe_browsing_util::kPhishingList)); |
| 710 GetChunkIds(PHISH, ADD_CHUNK, &lists->back().adds); | 710 GetChunkIds(safe_browsing_util::PHISH, ADD_CHUNK, &lists->back().adds); |
| 711 GetChunkIds(PHISH, SUB_CHUNK, &lists->back().subs); | 711 GetChunkIds(safe_browsing_util::PHISH, SUB_CHUNK, &lists->back().subs); |
| 712 | 712 |
| 713 return; | 713 return; |
| 714 } | 714 } |
| 715 | 715 |
| 716 /* static */ | |
| 717 int SafeBrowsingDatabaseBloom::GetListId(const std::string& name) { | |
| 718 if (name == safe_browsing_util::kMalwareList) | |
| 719 return MALWARE; | |
| 720 else if (name == safe_browsing_util::kPhishingList) | |
| 721 return PHISH; | |
| 722 | |
| 723 NOTREACHED(); | |
| 724 return -1; | |
| 725 } | |
| 726 | |
| 727 /* static */ | |
| 728 std::string SafeBrowsingDatabaseBloom::GetListName(int list_id) { | |
| 729 switch (list_id) { | |
| 730 case MALWARE: | |
| 731 return safe_browsing_util::kMalwareList; | |
| 732 case PHISH: | |
| 733 return safe_browsing_util::kPhishingList; | |
| 734 default: | |
| 735 NOTREACHED(); | |
| 736 return ""; | |
| 737 } | |
| 738 } | |
| 739 | |
| 740 void SafeBrowsingDatabaseBloom::ReadChunkNumbers() { | 716 void SafeBrowsingDatabaseBloom::ReadChunkNumbers() { |
| 741 add_chunk_cache_.clear(); | 717 add_chunk_cache_.clear(); |
| 742 sub_chunk_cache_.clear(); | 718 sub_chunk_cache_.clear(); |
| 743 | 719 |
| 744 // Read in the add chunk numbers. | 720 // Read in the add chunk numbers. |
| 745 SQLITE_UNIQUE_STATEMENT(read_adds, *statement_cache_, | 721 SQLITE_UNIQUE_STATEMENT(read_adds, *statement_cache_, |
| 746 "SELECT chunk FROM add_chunks"); | 722 "SELECT chunk FROM add_chunks"); |
| 747 if (!read_adds.is_valid()) { | 723 if (!read_adds.is_valid()) { |
| 748 NOTREACHED(); | 724 NOTREACHED(); |
| 749 return; | 725 return; |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1406 // or if this particular GetHash was received in the past 45 minutes. | 1382 // or if this particular GetHash was received in the past 45 minutes. |
| 1407 // If an entry is does not meet the time criteria above, we are not | 1383 // If an entry is does not meet the time criteria above, we are not |
| 1408 // allowed to use it since it might have become stale. We keep it | 1384 // allowed to use it since it might have become stale. We keep it |
| 1409 // around, though, and may be able to use it in the future once we | 1385 // around, though, and may be able to use it in the future once we |
| 1410 // receive the next update (that doesn't sub it). | 1386 // receive the next update (that doesn't sub it). |
| 1411 if (max_age < last_update || eit->received > max_age) { | 1387 if (max_age < last_update || eit->received > max_age) { |
| 1412 SBFullHashResult full_hash; | 1388 SBFullHashResult full_hash; |
| 1413 memcpy(&full_hash.hash.full_hash, | 1389 memcpy(&full_hash.hash.full_hash, |
| 1414 &eit->full_hash.full_hash, | 1390 &eit->full_hash.full_hash, |
| 1415 sizeof(SBFullHash)); | 1391 sizeof(SBFullHash)); |
| 1416 full_hash.list_name = GetListName(eit->list_id); | 1392 full_hash.list_name = safe_browsing_util::GetListName(eit->list_id); |
| 1417 full_hash.add_chunk_id = eit->add_chunk_id; | 1393 full_hash.add_chunk_id = eit->add_chunk_id; |
| 1418 full_hits->push_back(full_hash); | 1394 full_hits->push_back(full_hash); |
| 1419 } | 1395 } |
| 1420 ++eit; | 1396 ++eit; |
| 1421 } | 1397 } |
| 1422 | 1398 |
| 1423 if (entries.empty()) | 1399 if (entries.empty()) |
| 1424 hash_cache_->erase(hit); | 1400 hash_cache_->erase(hit); |
| 1425 } | 1401 } |
| 1426 } | 1402 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1442 } | 1418 } |
| 1443 | 1419 |
| 1444 const Time now = Time::Now(); | 1420 const Time now = Time::Now(); |
| 1445 for (std::vector<SBFullHashResult>::const_iterator it = full_hits.begin(); | 1421 for (std::vector<SBFullHashResult>::const_iterator it = full_hits.begin(); |
| 1446 it != full_hits.end(); ++it) { | 1422 it != full_hits.end(); ++it) { |
| 1447 SBPrefix prefix; | 1423 SBPrefix prefix; |
| 1448 memcpy(&prefix, &it->hash.full_hash, sizeof(SBPrefix)); | 1424 memcpy(&prefix, &it->hash.full_hash, sizeof(SBPrefix)); |
| 1449 HashList& entries = (*hash_cache_)[prefix]; | 1425 HashList& entries = (*hash_cache_)[prefix]; |
| 1450 HashCacheEntry entry; | 1426 HashCacheEntry entry; |
| 1451 entry.received = now; | 1427 entry.received = now; |
| 1452 entry.list_id = GetListId(it->list_name); | 1428 entry.list_id = safe_browsing_util::GetListId(it->list_name); |
| 1453 entry.add_chunk_id = EncodeChunkId(it->add_chunk_id, entry.list_id); | 1429 entry.add_chunk_id = EncodeChunkId(it->add_chunk_id, entry.list_id); |
| 1454 memcpy(&entry.full_hash, &it->hash.full_hash, sizeof(SBFullHash)); | 1430 memcpy(&entry.full_hash, &it->hash.full_hash, sizeof(SBFullHash)); |
| 1455 entries.push_back(entry); | 1431 entries.push_back(entry); |
| 1456 | 1432 |
| 1457 // Also push a copy to the pending write queue. | 1433 // Also push a copy to the pending write queue. |
| 1458 pending_full_hashes_.push_back(entry); | 1434 pending_full_hashes_.push_back(entry); |
| 1459 } | 1435 } |
| 1460 } | 1436 } |
| 1461 | 1437 |
| 1462 bool SafeBrowsingDatabaseBloom::ClearCachedEntry(SBPrefix prefix, | 1438 bool SafeBrowsingDatabaseBloom::ClearCachedEntry(SBPrefix prefix, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1513 if (did_resume_) { | 1489 if (did_resume_) { |
| 1514 PlatformThread::Sleep(kOnResumeHoldupMs); | 1490 PlatformThread::Sleep(kOnResumeHoldupMs); |
| 1515 did_resume_ = false; | 1491 did_resume_ = false; |
| 1516 } | 1492 } |
| 1517 } | 1493 } |
| 1518 | 1494 |
| 1519 // This database is always synchronous since we don't need to worry about | 1495 // This database is always synchronous since we don't need to worry about |
| 1520 // blocking any incoming reads. | 1496 // blocking any incoming reads. |
| 1521 void SafeBrowsingDatabaseBloom::SetSynchronous() { | 1497 void SafeBrowsingDatabaseBloom::SetSynchronous() { |
| 1522 } | 1498 } |
| OLD | NEW |