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