| 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 <memory> |
| 9 #include <set> | 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 20 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "chrome/browser/safe_browsing/safe_browsing_store.h" | 22 #include "chrome/browser/safe_browsing/safe_browsing_store.h" |
| 23 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 23 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 24 #include "components/safe_browsing_db/util.h" | 24 #include "components/safe_browsing_db/util.h" |
| 25 | 25 |
| 26 class GURL; | 26 class GURL; |
| 27 | 27 |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 // // back. | 201 // // back. |
| 202 // db.UpdateFinished(success); | 202 // db.UpdateFinished(success); |
| 203 // } | 203 // } |
| 204 // | 204 // |
| 205 // If UpdateStarted() returns true, the caller MUST eventually call | 205 // If UpdateStarted() returns true, the caller MUST eventually call |
| 206 // UpdateFinished(). If it returns false, the caller MUST NOT call | 206 // UpdateFinished(). If it returns false, the caller MUST NOT call |
| 207 // the other functions. | 207 // the other functions. |
| 208 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; | 208 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; |
| 209 virtual void InsertChunks( | 209 virtual void InsertChunks( |
| 210 const std::string& list_name, | 210 const std::string& list_name, |
| 211 const std::vector<scoped_ptr<SBChunkData>>& chunks) = 0; | 211 const std::vector<std::unique_ptr<SBChunkData>>& chunks) = 0; |
| 212 virtual void DeleteChunks( | 212 virtual void DeleteChunks( |
| 213 const std::vector<SBChunkDelete>& chunk_deletes) = 0; | 213 const std::vector<SBChunkDelete>& chunk_deletes) = 0; |
| 214 virtual void UpdateFinished(bool update_succeeded) = 0; | 214 virtual void UpdateFinished(bool update_succeeded) = 0; |
| 215 | 215 |
| 216 // Store the results of a GetHash response. In the case of empty results, we | 216 // Store the results of a GetHash response. In the case of empty results, we |
| 217 // cache the prefixes until the next update so that we don't have to issue | 217 // cache the prefixes until the next update so that we don't have to issue |
| 218 // further GetHash requests we know will be empty. This function is safe to | 218 // further GetHash requests we know will be empty. This function is safe to |
| 219 // call from any thread. | 219 // call from any thread. |
| 220 virtual void CacheHashResults( | 220 virtual void CacheHashResults( |
| 221 const std::vector<SBPrefix>& prefixes, | 221 const std::vector<SBPrefix>& prefixes, |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 bool ContainsModuleWhitelistedString(const std::string& str) override; | 390 bool ContainsModuleWhitelistedString(const std::string& str) override; |
| 391 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, | 391 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, |
| 392 std::vector<SBPrefix>* prefix_hits) override; | 392 std::vector<SBPrefix>* prefix_hits) override; |
| 393 bool ContainsMalwareIP(const std::string& ip_address) override; | 393 bool ContainsMalwareIP(const std::string& ip_address) override; |
| 394 bool ContainsResourceUrlPrefixes(const std::vector<SBPrefix>& prefixes, | 394 bool ContainsResourceUrlPrefixes(const std::vector<SBPrefix>& prefixes, |
| 395 std::vector<SBPrefix>* prefix_hits) override; | 395 std::vector<SBPrefix>* prefix_hits) override; |
| 396 | 396 |
| 397 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override; | 397 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override; |
| 398 void InsertChunks( | 398 void InsertChunks( |
| 399 const std::string& list_name, | 399 const std::string& list_name, |
| 400 const std::vector<scoped_ptr<SBChunkData>>& chunks) override; | 400 const std::vector<std::unique_ptr<SBChunkData>>& chunks) override; |
| 401 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override; | 401 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override; |
| 402 void UpdateFinished(bool update_succeeded) override; | 402 void UpdateFinished(bool update_succeeded) override; |
| 403 void CacheHashResults(const std::vector<SBPrefix>& prefixes, | 403 void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
| 404 const std::vector<SBFullHashResult>& full_hits, | 404 const std::vector<SBFullHashResult>& full_hits, |
| 405 const base::TimeDelta& cache_lifetime) override; | 405 const base::TimeDelta& cache_lifetime) override; |
| 406 | 406 |
| 407 // Returns the value of malware_kill_switch_; | 407 // Returns the value of malware_kill_switch_; |
| 408 bool IsMalwareIPMatchKillSwitchOn() override; | 408 bool IsMalwareIPMatchKillSwitchOn() override; |
| 409 | 409 |
| 410 // Returns true if the CSD whitelist has everything whitelisted. | 410 // Returns true if the CSD whitelist has everything whitelisted. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 // Obtained through BeginWriteTransaction(): a WriteTransaction allows | 468 // Obtained through BeginWriteTransaction(): a WriteTransaction allows |
| 469 // modification of the ThreadSafeStateManager's state. It should be used for | 469 // modification of the ThreadSafeStateManager's state. It should be used for |
| 470 // the shortest amount of time possible (e.g., pre-compute the new state | 470 // the shortest amount of time possible (e.g., pre-compute the new state |
| 471 // before grabbing a WriteTransaction to swap it in atomically). | 471 // before grabbing a WriteTransaction to swap it in atomically). |
| 472 class WriteTransaction; | 472 class WriteTransaction; |
| 473 | 473 |
| 474 explicit ThreadSafeStateManager( | 474 explicit ThreadSafeStateManager( |
| 475 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner); | 475 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner); |
| 476 ~ThreadSafeStateManager(); | 476 ~ThreadSafeStateManager(); |
| 477 | 477 |
| 478 scoped_ptr<ReadTransaction> BeginReadTransaction(); | 478 std::unique_ptr<ReadTransaction> BeginReadTransaction(); |
| 479 scoped_ptr<ReadTransaction> BeginReadTransactionNoLockOnMainTaskRunner(); | 479 std::unique_ptr<ReadTransaction> |
| 480 scoped_ptr<WriteTransaction> BeginWriteTransaction(); | 480 BeginReadTransactionNoLockOnMainTaskRunner(); |
| 481 std::unique_ptr<WriteTransaction> BeginWriteTransaction(); |
| 481 | 482 |
| 482 private: | 483 private: |
| 483 // The sequenced task runner for this object, used to verify that its state | 484 // The sequenced task runner for this object, used to verify that its state |
| 484 // is only ever accessed from the runner. | 485 // is only ever accessed from the runner. |
| 485 scoped_refptr<const base::SequencedTaskRunner> db_task_runner_; | 486 scoped_refptr<const base::SequencedTaskRunner> db_task_runner_; |
| 486 | 487 |
| 487 // Lock for protecting access to this class' state. | 488 // Lock for protecting access to this class' state. |
| 488 mutable base::Lock lock_; | 489 mutable base::Lock lock_; |
| 489 | 490 |
| 490 SBWhitelist csd_whitelist_; | 491 SBWhitelist csd_whitelist_; |
| 491 SBWhitelist download_whitelist_; | 492 SBWhitelist download_whitelist_; |
| 492 SBWhitelist inclusion_whitelist_; | 493 SBWhitelist inclusion_whitelist_; |
| 493 SBWhitelist module_whitelist_; | 494 SBWhitelist module_whitelist_; |
| 494 | 495 |
| 495 // The IP blacklist should be small. At most a couple hundred IPs. | 496 // The IP blacklist should be small. At most a couple hundred IPs. |
| 496 IPBlacklist ip_blacklist_; | 497 IPBlacklist ip_blacklist_; |
| 497 | 498 |
| 498 // PrefixSets to speed up lookups for particularly large lists. The | 499 // PrefixSets to speed up lookups for particularly large lists. The |
| 499 // PrefixSet themselves are never modified, instead a new one is swapped in | 500 // PrefixSet themselves are never modified, instead a new one is swapped in |
| 500 // on update. | 501 // on update. |
| 501 scoped_ptr<const PrefixSet> browse_prefix_set_; | 502 std::unique_ptr<const PrefixSet> browse_prefix_set_; |
| 502 scoped_ptr<const PrefixSet> unwanted_software_prefix_set_; | 503 std::unique_ptr<const PrefixSet> unwanted_software_prefix_set_; |
| 503 | 504 |
| 504 // Cache of gethash results for prefix stores. Entries should not be used if | 505 // Cache of gethash results for prefix stores. Entries should not be used if |
| 505 // they are older than their expire_after field. Cached misses will have | 506 // they are older than their expire_after field. Cached misses will have |
| 506 // empty full_hashes field. Cleared on each update. The cache is "mutable" | 507 // empty full_hashes field. Cleared on each update. The cache is "mutable" |
| 507 // as it can be written to from any transaction holding the lock, including | 508 // as it can be written to from any transaction holding the lock, including |
| 508 // ReadTransactions. | 509 // ReadTransactions. |
| 509 mutable PrefixGetHashCache prefix_gethash_cache_; | 510 mutable PrefixGetHashCache prefix_gethash_cache_; |
| 510 | 511 |
| 511 DISALLOW_COPY_AND_ASSIGN(ThreadSafeStateManager); | 512 DISALLOW_COPY_AND_ASSIGN(ThreadSafeStateManager); |
| 512 }; | 513 }; |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 // - |extension_blacklist_store_|: For extension IDs. | 714 // - |extension_blacklist_store_|: For extension IDs. |
| 714 // - |ip_blacklist_store_|: For IP blacklist. | 715 // - |ip_blacklist_store_|: For IP blacklist. |
| 715 // - |unwanted_software_store_|: For unwanted software list (format | 716 // - |unwanted_software_store_|: For unwanted software list (format |
| 716 // identical to browsing lists). | 717 // identical to browsing lists). |
| 717 // - |module_whitelist_store_|: For module whitelist. This list only | 718 // - |module_whitelist_store_|: For module whitelist. This list only |
| 718 // contains 256 bit hashes. | 719 // contains 256 bit hashes. |
| 719 // - |resource_blacklist_store_|: For script resource list (format identical | 720 // - |resource_blacklist_store_|: For script resource list (format identical |
| 720 // to browsing lists). | 721 // to browsing lists). |
| 721 // | 722 // |
| 722 // The stores themselves will be modified throughout the existence of this | 723 // The stores themselves will be modified throughout the existence of this |
| 723 // database, but shouldn't ever be swapped out (hence the const scoped_ptr -- | 724 // database, but shouldn't ever be swapped out (hence the const |
| 724 // which could be swapped for C++11's std::optional when that's available). | 725 // std::unique_ptr -- which could be swapped for C++11's std::optional when |
| 725 // They are NonThreadSafe and should thus only be accessed on the database's | 726 // that's available). They are NonThreadSafe and should thus only be accessed |
| 726 // main thread as enforced by SafeBrowsingStoreFile's implementation. | 727 // on the database's main thread as enforced by SafeBrowsingStoreFile's |
| 727 const scoped_ptr<SafeBrowsingStore> browse_store_; | 728 // implementation. |
| 728 const scoped_ptr<SafeBrowsingStore> download_store_; | 729 const std::unique_ptr<SafeBrowsingStore> browse_store_; |
| 729 const scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; | 730 const std::unique_ptr<SafeBrowsingStore> download_store_; |
| 730 const scoped_ptr<SafeBrowsingStore> download_whitelist_store_; | 731 const std::unique_ptr<SafeBrowsingStore> csd_whitelist_store_; |
| 731 const scoped_ptr<SafeBrowsingStore> inclusion_whitelist_store_; | 732 const std::unique_ptr<SafeBrowsingStore> download_whitelist_store_; |
| 732 const scoped_ptr<SafeBrowsingStore> extension_blacklist_store_; | 733 const std::unique_ptr<SafeBrowsingStore> inclusion_whitelist_store_; |
| 733 const scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; | 734 const std::unique_ptr<SafeBrowsingStore> extension_blacklist_store_; |
| 734 const scoped_ptr<SafeBrowsingStore> unwanted_software_store_; | 735 const std::unique_ptr<SafeBrowsingStore> ip_blacklist_store_; |
| 735 const scoped_ptr<SafeBrowsingStore> module_whitelist_store_; | 736 const std::unique_ptr<SafeBrowsingStore> unwanted_software_store_; |
| 736 const scoped_ptr<SafeBrowsingStore> resource_blacklist_store_; | 737 const std::unique_ptr<SafeBrowsingStore> module_whitelist_store_; |
| 738 const std::unique_ptr<SafeBrowsingStore> resource_blacklist_store_; |
| 737 | 739 |
| 738 // Used to schedule resetting the database because of corruption. This factory | 740 // Used to schedule resetting the database because of corruption. This factory |
| 739 // and the WeakPtrs it issues should only be used on the database's main | 741 // and the WeakPtrs it issues should only be used on the database's main |
| 740 // thread. | 742 // thread. |
| 741 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 743 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
| 742 }; | 744 }; |
| 743 | 745 |
| 744 } // namespace safe_browsing | 746 } // namespace safe_browsing |
| 745 | 747 |
| 746 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 748 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
| OLD | NEW |