| 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "base/threading/thread_checker.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "chrome/browser/safe_browsing/safe_browsing_store.h" | 21 #include "chrome/browser/safe_browsing/safe_browsing_store.h" |
| 21 | 22 |
| 22 namespace base { | |
| 23 class MessageLoop; | |
| 24 } | |
| 25 | |
| 26 namespace safe_browsing { | 23 namespace safe_browsing { |
| 27 class PrefixSet; | 24 class PrefixSet; |
| 28 } | 25 } |
| 29 | 26 |
| 30 class GURL; | 27 class GURL; |
| 31 class SafeBrowsingDatabase; | 28 class SafeBrowsingDatabase; |
| 32 | 29 |
| 33 // Factory for creating SafeBrowsingDatabase. Tests implement this factory | 30 // Factory for creating SafeBrowsingDatabase. Tests implement this factory |
| 34 // to create fake Databases for testing. | 31 // to create fake Databases for testing. |
| 35 class SafeBrowsingDatabaseFactory { | 32 class SafeBrowsingDatabaseFactory { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 std::vector<SBFullHashResult>* cache_hits) = 0; | 116 std::vector<SBFullHashResult>* cache_hits) = 0; |
| 120 | 117 |
| 121 // Returns false if none of |urls| are in Download database. If it returns | 118 // Returns false if none of |urls| are in Download database. If it returns |
| 122 // true, |prefix_hits| should contain the prefixes for the URLs that were in | 119 // true, |prefix_hits| should contain the prefixes for the URLs that were in |
| 123 // the database. This function could ONLY be accessed from creation thread. | 120 // the database. This function could ONLY be accessed from creation thread. |
| 124 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, | 121 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, |
| 125 std::vector<SBPrefix>* prefix_hits) = 0; | 122 std::vector<SBPrefix>* prefix_hits) = 0; |
| 126 | 123 |
| 127 // Returns false if |url| is not on the client-side phishing detection | 124 // Returns false if |url| is not on the client-side phishing detection |
| 128 // whitelist. Otherwise, this function returns true. Note: the whitelist | 125 // whitelist. Otherwise, this function returns true. Note: the whitelist |
| 129 // only contains full-length hashes so we don't return any prefix hit. | 126 // only contains full-length hashes so we don't return any prefix hit. This |
| 130 // This function should only be called from the IO thread. | 127 // function is safe to call from any thread. |
| 131 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) = 0; | 128 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) = 0; |
| 132 | 129 |
| 133 // The download whitelist is used for two purposes: a white-domain list of | 130 // The download whitelist is used for two purposes: a white-domain list of |
| 134 // sites that are considered to host only harmless binaries as well as a | 131 // sites that are considered to host only harmless binaries as well as a |
| 135 // whitelist of arbitrary strings such as hashed certificate authorities that | 132 // whitelist of arbitrary strings such as hashed certificate authorities that |
| 136 // are considered to be trusted. The two methods below let you lookup | 133 // are considered to be trusted. The two methods below let you lookup the |
| 137 // the whitelist either for a URL or an arbitrary string. These methods will | 134 // whitelist either for a URL or an arbitrary string. These methods will |
| 138 // return false if no match is found and true otherwise. | 135 // return false if no match is found and true otherwise. This function is safe |
| 139 // This function could ONLY be accessed from the IO thread. | 136 // to call from any thread. |
| 140 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) = 0; | 137 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) = 0; |
| 141 virtual bool ContainsDownloadWhitelistedString(const std::string& str) = 0; | 138 virtual bool ContainsDownloadWhitelistedString(const std::string& str) = 0; |
| 142 | 139 |
| 143 // Populates |prefix_hits| with any prefixes in |prefixes| that have matches | 140 // Populates |prefix_hits| with any prefixes in |prefixes| that have matches |
| 144 // in the database. | 141 // in the database. |
| 145 // | 142 // |
| 146 // This function can ONLY be accessed from the creation thread. | 143 // This function can ONLY be accessed from the creation thread. |
| 147 virtual bool ContainsExtensionPrefixes( | 144 virtual bool ContainsExtensionPrefixes( |
| 148 const std::vector<SBPrefix>& prefixes, | 145 const std::vector<SBPrefix>& prefixes, |
| 149 std::vector<SBPrefix>* prefix_hits) = 0; | 146 std::vector<SBPrefix>* prefix_hits) = 0; |
| 150 | 147 |
| 151 // Returns false unless the hash of |url| is on the side-effect free | 148 // Returns false unless the hash of |url| is on the side-effect free |
| 152 // whitelist. | 149 // whitelist. This function is safe to call from any thread. |
| 153 virtual bool ContainsSideEffectFreeWhitelistUrl(const GURL& url) = 0; | 150 virtual bool ContainsSideEffectFreeWhitelistUrl(const GURL& url) = 0; |
| 154 | 151 |
| 155 // Returns true iff the given IP is currently on the csd malware IP blacklist. | 152 // Returns true iff the given IP is currently on the csd malware IP blacklist. |
| 153 // This function is safe to call from any thread. |
| 156 virtual bool ContainsMalwareIP(const std::string& ip_address) = 0; | 154 virtual bool ContainsMalwareIP(const std::string& ip_address) = 0; |
| 157 | 155 |
| 158 // A database transaction should look like: | 156 // A database transaction should look like: |
| 159 // | 157 // |
| 160 // std::vector<SBListChunkRanges> lists; | 158 // std::vector<SBListChunkRanges> lists; |
| 161 // if (db.UpdateStarted(&lists)) { | 159 // if (db.UpdateStarted(&lists)) { |
| 162 // // Do something with |lists|. | 160 // // Do something with |lists|. |
| 163 // | 161 // |
| 164 // // Process add/sub commands. | 162 // // Process add/sub commands. |
| 165 // db.InsertChunks(list_name, chunks); | 163 // db.InsertChunks(list_name, chunks); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 178 // the other functions. | 176 // the other functions. |
| 179 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; | 177 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; |
| 180 virtual void InsertChunks(const std::string& list_name, | 178 virtual void InsertChunks(const std::string& list_name, |
| 181 const std::vector<SBChunkData*>& chunks) = 0; | 179 const std::vector<SBChunkData*>& chunks) = 0; |
| 182 virtual void DeleteChunks( | 180 virtual void DeleteChunks( |
| 183 const std::vector<SBChunkDelete>& chunk_deletes) = 0; | 181 const std::vector<SBChunkDelete>& chunk_deletes) = 0; |
| 184 virtual void UpdateFinished(bool update_succeeded) = 0; | 182 virtual void UpdateFinished(bool update_succeeded) = 0; |
| 185 | 183 |
| 186 // Store the results of a GetHash response. In the case of empty results, we | 184 // Store the results of a GetHash response. In the case of empty results, we |
| 187 // cache the prefixes until the next update so that we don't have to issue | 185 // cache the prefixes until the next update so that we don't have to issue |
| 188 // further GetHash requests we know will be empty. | 186 // further GetHash requests we know will be empty. This function is safe to |
| 187 // call from any thread. |
| 189 virtual void CacheHashResults( | 188 virtual void CacheHashResults( |
| 190 const std::vector<SBPrefix>& prefixes, | 189 const std::vector<SBPrefix>& prefixes, |
| 191 const std::vector<SBFullHashResult>& full_hits, | 190 const std::vector<SBFullHashResult>& full_hits, |
| 192 const base::TimeDelta& cache_lifetime) = 0; | 191 const base::TimeDelta& cache_lifetime) = 0; |
| 193 | 192 |
| 194 // Returns true if the malware IP blacklisting killswitch URL is present | 193 // Returns true if the malware IP blacklisting killswitch URL is present |
| 195 // in the csd whitelist. | 194 // in the csd whitelist. This function is safe to call from any thread. |
| 196 virtual bool IsMalwareIPMatchKillSwitchOn() = 0; | 195 virtual bool IsMalwareIPMatchKillSwitchOn() = 0; |
| 197 | 196 |
| 198 // Returns true if the whitelist killswitch URL is present in the csd | 197 // Returns true if the whitelist killswitch URL is present in the csd |
| 199 // whitelist. | 198 // whitelist. This function is safe to call from any thread. |
| 200 virtual bool IsCsdWhitelistKillSwitchOn() = 0; | 199 virtual bool IsCsdWhitelistKillSwitchOn() = 0; |
| 201 | 200 |
| 202 // The name of the bloom-filter file for the given database file. | 201 // The name of the bloom-filter file for the given database file. |
| 203 // NOTE(shess): OBSOLETE. Present for deleting stale files. | 202 // NOTE(shess): OBSOLETE. Present for deleting stale files. |
| 204 static base::FilePath BloomFilterForFilename( | 203 static base::FilePath BloomFilterForFilename( |
| 205 const base::FilePath& db_filename); | 204 const base::FilePath& db_filename); |
| 206 | 205 |
| 207 // The name of the prefix set file for the given database file. | 206 // The name of the prefix set file for the given database file. |
| 208 static base::FilePath PrefixSetForFilename(const base::FilePath& db_filename); | 207 static base::FilePath PrefixSetForFilename(const base::FilePath& db_filename); |
| 209 | 208 |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 void UpdateUrlStore(SafeBrowsingStore* url_store, | 464 void UpdateUrlStore(SafeBrowsingStore* url_store, |
| 466 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, | 465 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, |
| 467 FailureType failure_type); | 466 FailureType failure_type); |
| 468 | 467 |
| 469 void UpdateWhitelistStore(const base::FilePath& store_filename, | 468 void UpdateWhitelistStore(const base::FilePath& store_filename, |
| 470 SafeBrowsingStore* store, | 469 SafeBrowsingStore* store, |
| 471 SBWhitelist* whitelist); | 470 SBWhitelist* whitelist); |
| 472 void UpdateIpBlacklistStore(); | 471 void UpdateIpBlacklistStore(); |
| 473 | 472 |
| 474 // Used to verify that various calls are made from the thread the | 473 // Used to verify that various calls are made from the thread the |
| 475 // object was created on. | 474 // object was created on (i.e., the safe_browsing_thread). |
| 476 base::MessageLoop* creation_loop_; | 475 base::ThreadChecker thread_checker_; |
| 477 | 476 |
| 478 // The base filename passed to Init(), used to generate the store and prefix | 477 // The base filename passed to Init(), used to generate the store and prefix |
| 479 // set filenames used to store data on disk. | 478 // set filenames used to store data on disk. |
| 480 base::FilePath filename_base_; | 479 base::FilePath filename_base_; |
| 481 | 480 |
| 482 // Underlying persistent store for chunk data. | 481 // Underlying persistent store for chunk data. |
| 483 // For browsing related (phishing and malware URLs) chunks and prefixes. | 482 // For browsing related (phishing and malware URLs) chunks and prefixes. |
| 484 scoped_ptr<SafeBrowsingStore> browse_store_; | 483 scoped_ptr<SafeBrowsingStore> browse_store_; |
| 485 | 484 |
| 486 // For download related (download URL and binary hash) chunks and prefixes. | 485 // For download related (download URL and binary hash) chunks and prefixes. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 499 | 498 |
| 500 // For side-effect free whitelist. | 499 // For side-effect free whitelist. |
| 501 scoped_ptr<SafeBrowsingStore> side_effect_free_whitelist_store_; | 500 scoped_ptr<SafeBrowsingStore> side_effect_free_whitelist_store_; |
| 502 | 501 |
| 503 // For IP blacklist. | 502 // For IP blacklist. |
| 504 scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; | 503 scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; |
| 505 | 504 |
| 506 // For unwanted software list. | 505 // For unwanted software list. |
| 507 scoped_ptr<SafeBrowsingStore> unwanted_software_store_; | 506 scoped_ptr<SafeBrowsingStore> unwanted_software_store_; |
| 508 | 507 |
| 509 // Lock for protecting access to variables that may be used on the IO thread. | 508 // Lock for protecting access to variables that may be used on any threads. |
| 510 // This includes |(browse|unwanted_software)_prefix_set_|, | 509 // This includes all SBWhitelist's, PrefixSet's, and caches. |
| 511 // |prefix_gethash_cache_|, |csd_whitelist_|. | |
| 512 base::Lock lookup_lock_; | 510 base::Lock lookup_lock_; |
| 513 | 511 |
| 514 SBWhitelist csd_whitelist_; | 512 SBWhitelist csd_whitelist_; |
| 515 SBWhitelist download_whitelist_; | 513 SBWhitelist download_whitelist_; |
| 516 | 514 |
| 517 // The IP blacklist should be small. At most a couple hundred IPs. | 515 // The IP blacklist should be small. At most a couple hundred IPs. |
| 518 IPBlacklist ip_blacklist_; | 516 IPBlacklist ip_blacklist_; |
| 519 | 517 |
| 520 // Cache of gethash results for prefix stores. Entries should not be used if | 518 // Cache of gethash results for prefix stores. Entries should not be used if |
| 521 // they are older than their expire_after field. Cached misses will have | 519 // they are older than their expire_after field. Cached misses will have |
| (...skipping 18 matching lines...) Expand all Loading... |
| 540 scoped_ptr<const safe_browsing::PrefixSet> browse_prefix_set_; | 538 scoped_ptr<const safe_browsing::PrefixSet> browse_prefix_set_; |
| 541 scoped_ptr<const safe_browsing::PrefixSet> | 539 scoped_ptr<const safe_browsing::PrefixSet> |
| 542 side_effect_free_whitelist_prefix_set_; | 540 side_effect_free_whitelist_prefix_set_; |
| 543 scoped_ptr<const safe_browsing::PrefixSet> unwanted_software_prefix_set_; | 541 scoped_ptr<const safe_browsing::PrefixSet> unwanted_software_prefix_set_; |
| 544 | 542 |
| 545 // Used to schedule resetting the database because of corruption. | 543 // Used to schedule resetting the database because of corruption. |
| 546 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 544 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
| 547 }; | 545 }; |
| 548 | 546 |
| 549 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 547 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
| OLD | NEW |