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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 // matching hash prefixes which had no cached results and |cache_hits| | 110 // matching hash prefixes which had no cached results and |cache_hits| |
114 // contains any matching cached gethash results. This function is safe to | 111 // contains any matching cached gethash results. This function is safe to |
115 // call from any thread. | 112 // call from any thread. |
116 virtual bool ContainsUnwantedSoftwareUrl( | 113 virtual bool ContainsUnwantedSoftwareUrl( |
117 const GURL& url, | 114 const GURL& url, |
118 std::vector<SBPrefix>* prefix_hits, | 115 std::vector<SBPrefix>* prefix_hits, |
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 can ONLY be called from the 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 called 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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, | 462 scoped_ptr<const safe_browsing::PrefixSet>* prefix_set, |
464 FailureType failure_type); | 463 FailureType failure_type); |
465 | 464 |
466 void UpdateSideEffectFreeWhitelistStore(); | 465 void UpdateSideEffectFreeWhitelistStore(); |
467 void UpdateWhitelistStore(const base::FilePath& store_filename, | 466 void UpdateWhitelistStore(const base::FilePath& store_filename, |
468 SafeBrowsingStore* store, | 467 SafeBrowsingStore* store, |
469 SBWhitelist* whitelist); | 468 SBWhitelist* whitelist); |
470 void UpdateIpBlacklistStore(); | 469 void UpdateIpBlacklistStore(); |
471 | 470 |
472 // Used to verify that various calls are made from the thread the | 471 // Used to verify that various calls are made from the thread the |
473 // object was created on. | 472 // object was created on (i.e., the safe_browsing_thread). |
474 base::MessageLoop* creation_loop_; | 473 base::ThreadChecker thread_checker_; |
475 | 474 |
476 // The base filename passed to Init(), used to generate the store and prefix | 475 // The base filename passed to Init(), used to generate the store and prefix |
477 // set filenames used to store data on disk. | 476 // set filenames used to store data on disk. |
478 base::FilePath filename_base_; | 477 base::FilePath filename_base_; |
479 | 478 |
480 // Underlying persistent store for chunk data. | 479 // Underlying persistent store for chunk data. |
481 // For browsing related (phishing and malware URLs) chunks and prefixes. | 480 // For browsing related (phishing and malware URLs) chunks and prefixes. |
482 scoped_ptr<SafeBrowsingStore> browse_store_; | 481 scoped_ptr<SafeBrowsingStore> browse_store_; |
483 | 482 |
484 // For download related (download URL and binary hash) chunks and prefixes. | 483 // For download related (download URL and binary hash) chunks and prefixes. |
(...skipping 12 matching lines...) Expand all Loading... |
497 | 496 |
498 // For side-effect free whitelist. | 497 // For side-effect free whitelist. |
499 scoped_ptr<SafeBrowsingStore> side_effect_free_whitelist_store_; | 498 scoped_ptr<SafeBrowsingStore> side_effect_free_whitelist_store_; |
500 | 499 |
501 // For IP blacklist. | 500 // For IP blacklist. |
502 scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; | 501 scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; |
503 | 502 |
504 // For unwanted software list. | 503 // For unwanted software list. |
505 scoped_ptr<SafeBrowsingStore> unwanted_software_store_; | 504 scoped_ptr<SafeBrowsingStore> unwanted_software_store_; |
506 | 505 |
507 // Lock for protecting access to variables that may be used on the IO thread. | 506 // Lock for protecting access to variables that may be used on any threads. |
508 // This includes |(browse|unwanted_software)_prefix_set_|, | 507 // This includes all SBWhitelist's, PrefixSet's, and caches. |
509 // |prefix_gethash_cache_|, |csd_whitelist_|. | |
510 base::Lock lookup_lock_; | 508 base::Lock lookup_lock_; |
511 | 509 |
512 SBWhitelist csd_whitelist_; | 510 SBWhitelist csd_whitelist_; |
513 SBWhitelist download_whitelist_; | 511 SBWhitelist download_whitelist_; |
514 | 512 |
515 // The IP blacklist should be small. At most a couple hundred IPs. | 513 // The IP blacklist should be small. At most a couple hundred IPs. |
516 IPBlacklist ip_blacklist_; | 514 IPBlacklist ip_blacklist_; |
517 | 515 |
518 // Cache of gethash results for prefix stores. Entries should not be used if | 516 // Cache of gethash results for prefix stores. Entries should not be used if |
519 // they are older than their expire_after field. Cached misses will have | 517 // they are older than their expire_after field. Cached misses will have |
(...skipping 18 matching lines...) Expand all Loading... |
538 scoped_ptr<const safe_browsing::PrefixSet> browse_prefix_set_; | 536 scoped_ptr<const safe_browsing::PrefixSet> browse_prefix_set_; |
539 scoped_ptr<const safe_browsing::PrefixSet> | 537 scoped_ptr<const safe_browsing::PrefixSet> |
540 side_effect_free_whitelist_prefix_set_; | 538 side_effect_free_whitelist_prefix_set_; |
541 scoped_ptr<const safe_browsing::PrefixSet> unwanted_software_prefix_set_; | 539 scoped_ptr<const safe_browsing::PrefixSet> unwanted_software_prefix_set_; |
542 | 540 |
543 // Used to schedule resetting the database because of corruption. | 541 // Used to schedule resetting the database because of corruption. |
544 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 542 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
545 }; | 543 }; |
546 | 544 |
547 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 545 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
OLD | NEW |