| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 DISALLOW_COPY_AND_ASSIGN(ThreadSafeStateManager); | 448 DISALLOW_COPY_AND_ASSIGN(ThreadSafeStateManager); |
| 449 }; | 449 }; |
| 450 | 450 |
| 451 // Forward the above inner-definitions to alleviate some verbosity in the | 451 // Forward the above inner-definitions to alleviate some verbosity in the |
| 452 // impl. | 452 // impl. |
| 453 using SBWhitelistId = ThreadSafeStateManager::SBWhitelistId; | 453 using SBWhitelistId = ThreadSafeStateManager::SBWhitelistId; |
| 454 using PrefixSetId = ThreadSafeStateManager::PrefixSetId; | 454 using PrefixSetId = ThreadSafeStateManager::PrefixSetId; |
| 455 using ReadTransaction = ThreadSafeStateManager::ReadTransaction; | 455 using ReadTransaction = ThreadSafeStateManager::ReadTransaction; |
| 456 using WriteTransaction = ThreadSafeStateManager::WriteTransaction; | 456 using WriteTransaction = ThreadSafeStateManager::WriteTransaction; |
| 457 | 457 |
| 458 // Manages the non-thread safe (i.e. only to be accessed to the database's |
| 459 // main thread) state of this class. |
| 460 class DatabaseStateManager { |
| 461 public: |
| 462 explicit DatabaseStateManager(const base::ThreadChecker& thread_checker) |
| 463 : thread_checker_(thread_checker), |
| 464 corruption_detected_(false), |
| 465 change_detected_(false) {} |
| 466 |
| 467 void init_filename_base(const base::FilePath& filename_base) { |
| 468 DCHECK(thread_checker_.CalledOnValidThread()); |
| 469 DCHECK(filename_base_.empty()) << "filename already initialized"; |
| 470 filename_base_ = filename_base; |
| 471 } |
| 472 |
| 473 const base::FilePath& filename_base() { |
| 474 DCHECK(thread_checker_.CalledOnValidThread()); |
| 475 return filename_base_; |
| 476 } |
| 477 |
| 478 void set_corruption_detected() { |
| 479 DCHECK(thread_checker_.CalledOnValidThread()); |
| 480 corruption_detected_ = true; |
| 481 } |
| 482 |
| 483 void reset_corruption_detected() { |
| 484 DCHECK(thread_checker_.CalledOnValidThread()); |
| 485 corruption_detected_ = false; |
| 486 } |
| 487 |
| 488 bool corruption_detected() { |
| 489 DCHECK(thread_checker_.CalledOnValidThread()); |
| 490 return corruption_detected_; |
| 491 } |
| 492 |
| 493 void set_change_detected() { |
| 494 DCHECK(thread_checker_.CalledOnValidThread()); |
| 495 change_detected_ = true; |
| 496 } |
| 497 |
| 498 void reset_change_detected() { |
| 499 DCHECK(thread_checker_.CalledOnValidThread()); |
| 500 change_detected_ = false; |
| 501 } |
| 502 |
| 503 bool change_detected() { |
| 504 DCHECK(thread_checker_.CalledOnValidThread()); |
| 505 return change_detected_; |
| 506 } |
| 507 |
| 508 private: |
| 509 // The SafeBrowsingDatabase's ThreadChecker, used to verify that this class' |
| 510 // state is only ever accessed from the database's main thread. |
| 511 const base::ThreadChecker& thread_checker_; |
| 512 |
| 513 // The base filename passed to Init(), used to generate the store and prefix |
| 514 // set filenames used to store data on disk. |
| 515 base::FilePath filename_base_; |
| 516 |
| 517 // Set if corruption is detected during the course of an update. |
| 518 // Causes the update functions to fail with no side effects, until |
| 519 // the next call to |UpdateStarted()|. |
| 520 bool corruption_detected_; |
| 521 |
| 522 // Set to true if any chunks are added or deleted during an update. |
| 523 // Used to optimize away database update. |
| 524 bool change_detected_; |
| 525 |
| 526 DISALLOW_COPY_AND_ASSIGN(DatabaseStateManager); |
| 527 }; |
| 528 |
| 458 bool PrefixSetContainsUrl(const GURL& url, | 529 bool PrefixSetContainsUrl(const GURL& url, |
| 459 PrefixSetId prefix_set_id, | 530 PrefixSetId prefix_set_id, |
| 460 std::vector<SBPrefix>* prefix_hits, | 531 std::vector<SBPrefix>* prefix_hits, |
| 461 std::vector<SBFullHashResult>* cache_hits); | 532 std::vector<SBFullHashResult>* cache_hits); |
| 462 | 533 |
| 463 // Exposed for testing of PrefixSetContainsUrlHashes() on the | 534 // Exposed for testing of PrefixSetContainsUrlHashes() on the |
| 464 // PrefixSet backing kMalwareList. | 535 // PrefixSet backing kMalwareList. |
| 465 bool ContainsBrowseUrlHashesForTesting( | 536 bool ContainsBrowseUrlHashesForTesting( |
| 466 const std::vector<SBFullHash>& full_hashes, | 537 const std::vector<SBFullHash>& full_hashes, |
| 467 std::vector<SBPrefix>* prefix_hits, | 538 std::vector<SBPrefix>* prefix_hits, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 PrefixGetHashCache* GetUnsynchronizedPrefixGetHashCacheForTesting(); | 632 PrefixGetHashCache* GetUnsynchronizedPrefixGetHashCacheForTesting(); |
| 562 | 633 |
| 563 // Records a file size histogram for the database or PrefixSet backed by | 634 // Records a file size histogram for the database or PrefixSet backed by |
| 564 // |filename|. | 635 // |filename|. |
| 565 void RecordFileSizeHistogram(const base::FilePath& file_path); | 636 void RecordFileSizeHistogram(const base::FilePath& file_path); |
| 566 | 637 |
| 567 base::ThreadChecker thread_checker_; | 638 base::ThreadChecker thread_checker_; |
| 568 | 639 |
| 569 ThreadSafeStateManager state_manager_; | 640 ThreadSafeStateManager state_manager_; |
| 570 | 641 |
| 571 // The base filename passed to Init(), used to generate the store and prefix | 642 DatabaseStateManager db_state_manager_; |
| 572 // set filenames used to store data on disk. | |
| 573 base::FilePath filename_base_; | |
| 574 | 643 |
| 575 // Underlying persistent store for chunk data. | 644 // Underlying persistent stores for chunk data: |
| 576 // For browsing related (phishing and malware URLs) chunks and prefixes. | 645 // - |browse_store_|: For browsing related (phishing and malware URLs) |
| 577 scoped_ptr<SafeBrowsingStore> browse_store_; | 646 // chunks and prefixes. |
| 647 // - |download_store_|: For download related (download URL and binary hash) |
| 648 // chunks and prefixes. |
| 649 // - |csd_whitelist_store_|: For the client-side phishing detection |
| 650 // whitelist chunks and full-length hashes. This list only contains 256 |
| 651 // bit hashes. |
| 652 // - |download_whitelist_store_|: For the download whitelist chunks and |
| 653 // full-length hashes. This list only contains 256 bit hashes. |
| 654 // - |inclusion_whitelist_store_|: For the inclusion whitelist. Same format |
| 655 // as |download_whitelist_store_|. |
| 656 // - |extension_blacklist_store_|: For extension IDs. |
| 657 // - |side_effect_free_whitelist_store_|: For side-effect free whitelist. |
| 658 // - |ip_blacklist_store_|: For IP blacklist. |
| 659 // - |unwanted_software_store_|: For unwanted software list (format |
| 660 // identical to browsing lists). |
| 661 // |
| 662 // The stores themselves will be modified throughout the existence of this |
| 663 // database, but shouldn't ever be swapped out (hence the const scoped_ptr -- |
| 664 // which could be swapped for C++11's std::optional when that's available). |
| 665 // They are NonThreadSafe and should thus only be accessed on the database's |
| 666 // main thread as enforced by SafeBrowsingStoreFile's implementation. |
| 667 const scoped_ptr<SafeBrowsingStore> browse_store_; |
| 668 const scoped_ptr<SafeBrowsingStore> download_store_; |
| 669 const scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; |
| 670 const scoped_ptr<SafeBrowsingStore> download_whitelist_store_; |
| 671 const scoped_ptr<SafeBrowsingStore> inclusion_whitelist_store_; |
| 672 const scoped_ptr<SafeBrowsingStore> extension_blacklist_store_; |
| 673 const scoped_ptr<SafeBrowsingStore> side_effect_free_whitelist_store_; |
| 674 const scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; |
| 675 const scoped_ptr<SafeBrowsingStore> unwanted_software_store_; |
| 578 | 676 |
| 579 // For download related (download URL and binary hash) chunks and prefixes. | 677 // Used to schedule resetting the database because of corruption. This factory |
| 580 scoped_ptr<SafeBrowsingStore> download_store_; | 678 // and the WeakPtrs it issues should only be used on the database's main |
| 581 | 679 // thread. |
| 582 // For the client-side phishing detection whitelist chunks and full-length | |
| 583 // hashes. This list only contains 256 bit hashes. | |
| 584 scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; | |
| 585 | |
| 586 // For the download whitelist chunks and full-length hashes. This list only | |
| 587 // contains 256 bit hashes. | |
| 588 scoped_ptr<SafeBrowsingStore> download_whitelist_store_; | |
| 589 | |
| 590 // For the off-domain inclusion whitelist chunks and full-length hashes. This | |
| 591 // list only contains 256 bit hashes. | |
| 592 scoped_ptr<SafeBrowsingStore> inclusion_whitelist_store_; | |
| 593 | |
| 594 // For extension IDs. | |
| 595 scoped_ptr<SafeBrowsingStore> extension_blacklist_store_; | |
| 596 | |
| 597 // For side-effect free whitelist. | |
| 598 scoped_ptr<SafeBrowsingStore> side_effect_free_whitelist_store_; | |
| 599 | |
| 600 // For IP blacklist. | |
| 601 scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; | |
| 602 | |
| 603 // For unwanted software list. | |
| 604 scoped_ptr<SafeBrowsingStore> unwanted_software_store_; | |
| 605 | |
| 606 // Set if corruption is detected during the course of an update. | |
| 607 // Causes the update functions to fail with no side effects, until | |
| 608 // the next call to |UpdateStarted()|. | |
| 609 bool corruption_detected_; | |
| 610 | |
| 611 // Set to true if any chunks are added or deleted during an update. | |
| 612 // Used to optimize away database update. | |
| 613 bool change_detected_; | |
| 614 | |
| 615 // Used to schedule resetting the database because of corruption. | |
| 616 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 680 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
| 617 }; | 681 }; |
| 618 | 682 |
| 619 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 683 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
| OLD | NEW |