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/time/time.h" |
19 #include "chrome/browser/safe_browsing/safe_browsing_store.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_store.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 class MessageLoop; | 23 class MessageLoop; |
23 class Time; | |
24 } | 24 } |
25 | 25 |
26 namespace safe_browsing { | 26 namespace safe_browsing { |
27 class PrefixSet; | 27 class PrefixSet; |
28 } | 28 } |
29 | 29 |
30 class GURL; | 30 class GURL; |
31 class SafeBrowsingDatabase; | 31 class SafeBrowsingDatabase; |
32 | 32 |
33 // Factory for creating SafeBrowsingDatabase. Tests implement this factory | 33 // Factory for creating SafeBrowsingDatabase. Tests implement this factory |
(...skipping 14 matching lines...) Expand all Loading... |
48 }; | 48 }; |
49 | 49 |
50 // Contains full_hash elements which are cached in memory. Differs from | 50 // Contains full_hash elements which are cached in memory. Differs from |
51 // SBAddFullHash in deriving |list_id| from |chunk_id|. Differs from | 51 // SBAddFullHash in deriving |list_id| from |chunk_id|. Differs from |
52 // SBFullHashResult in adding |received| for later expiration. | 52 // SBFullHashResult in adding |received| for later expiration. |
53 // TODO(shess): Remove/refactor this as part of converting to v2.3 caching | 53 // TODO(shess): Remove/refactor this as part of converting to v2.3 caching |
54 // semantics. | 54 // semantics. |
55 struct SBFullHashCached { | 55 struct SBFullHashCached { |
56 SBFullHash hash; | 56 SBFullHash hash; |
57 int list_id; // TODO(shess): Use safe_browsing_util::ListType. | 57 int list_id; // TODO(shess): Use safe_browsing_util::ListType. |
58 int received; // time_t like SBAddFullHash. | 58 base::Time expire_after; |
59 }; | 59 }; |
60 | 60 |
61 // Encapsulates on-disk databases that for safebrowsing. There are | 61 // Encapsulates on-disk databases that for safebrowsing. There are |
62 // four databases: browse, download, download whitelist and | 62 // four databases: browse, download, download whitelist and |
63 // client-side detection (csd) whitelist databases. The browse database contains | 63 // client-side detection (csd) whitelist databases. The browse database contains |
64 // information about phishing and malware urls. The download database contains | 64 // information about phishing and malware urls. The download database contains |
65 // URLs for bad binaries (e.g: those containing virus) and hash of | 65 // URLs for bad binaries (e.g: those containing virus) and hash of |
66 // these downloaded contents. The download whitelist contains whitelisted | 66 // these downloaded contents. The download whitelist contains whitelisted |
67 // download hosting sites as well as whitelisted binary signing certificates | 67 // download hosting sites as well as whitelisted binary signing certificates |
68 // etc. The csd whitelist database contains URLs that will never be considered | 68 // etc. The csd whitelist database contains URLs that will never be considered |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // Initializes the database with the given filename. | 100 // Initializes the database with the given filename. |
101 virtual void Init(const base::FilePath& filename) = 0; | 101 virtual void Init(const base::FilePath& filename) = 0; |
102 | 102 |
103 // Deletes the current database and creates a new one. | 103 // Deletes the current database and creates a new one. |
104 virtual bool ResetDatabase() = 0; | 104 virtual bool ResetDatabase() = 0; |
105 | 105 |
106 // Returns false if |url| is not in the browse database. If it returns true, | 106 // Returns false if |url| is not in the browse database. If it returns true, |
107 // then |prefix_hits| contains the list of prefix matches, and |cached_hits| | 107 // then |prefix_hits| contains the list of prefix matches, and |cached_hits| |
108 // contains the cached gethash results for those prefixes (if any). This | 108 // contains the cached gethash results for those prefixes (if any). This |
109 // function is safe to call from threads other than the creation thread. | 109 // function is safe to call from threads other than the creation thread. |
110 virtual bool ContainsBrowseUrl(const GURL& url, | 110 virtual bool ContainsBrowseUrl( |
111 std::vector<SBPrefix>* prefix_hits, | 111 const GURL& url, |
112 std::vector<SBFullHashResult>* cached_hits, | 112 std::vector<SBPrefix>* prefix_hits, |
113 base::Time last_update) = 0; | 113 std::vector<SBFullHashResult>* cached_hits) = 0; |
114 | 114 |
115 // Returns false if none of |urls| are in Download database. If it returns | 115 // Returns false if none of |urls| are in Download database. If it returns |
116 // true, |prefix_hits| should contain the prefixes for the URLs that were in | 116 // true, |prefix_hits| should contain the prefixes for the URLs that were in |
117 // the database. This function could ONLY be accessed from creation thread. | 117 // the database. This function could ONLY be accessed from creation thread. |
118 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, | 118 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, |
119 std::vector<SBPrefix>* prefix_hits) = 0; | 119 std::vector<SBPrefix>* prefix_hits) = 0; |
120 | 120 |
121 // Returns false if |url| is not on the client-side phishing detection | 121 // Returns false if |url| is not on the client-side phishing detection |
122 // whitelist. Otherwise, this function returns true. Note: the whitelist | 122 // whitelist. Otherwise, this function returns true. Note: the whitelist |
123 // only contains full-length hashes so we don't return any prefix hit. | 123 // only contains full-length hashes so we don't return any prefix hit. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 const SBChunkList& chunks) = 0; | 175 const SBChunkList& chunks) = 0; |
176 virtual void DeleteChunks( | 176 virtual void DeleteChunks( |
177 const std::vector<SBChunkDelete>& chunk_deletes) = 0; | 177 const std::vector<SBChunkDelete>& chunk_deletes) = 0; |
178 virtual void UpdateFinished(bool update_succeeded) = 0; | 178 virtual void UpdateFinished(bool update_succeeded) = 0; |
179 | 179 |
180 // Store the results of a GetHash response. In the case of empty results, we | 180 // Store the results of a GetHash response. In the case of empty results, we |
181 // cache the prefixes until the next update so that we don't have to issue | 181 // cache the prefixes until the next update so that we don't have to issue |
182 // further GetHash requests we know will be empty. | 182 // further GetHash requests we know will be empty. |
183 virtual void CacheHashResults( | 183 virtual void CacheHashResults( |
184 const std::vector<SBPrefix>& prefixes, | 184 const std::vector<SBPrefix>& prefixes, |
185 const std::vector<SBFullHashResult>& full_hits) = 0; | 185 const std::vector<SBFullHashResult>& full_hits, |
| 186 const base::TimeDelta& cache_lifetime) = 0; |
186 | 187 |
187 // Returns true if the malware IP blacklisting killswitch URL is present | 188 // Returns true if the malware IP blacklisting killswitch URL is present |
188 // in the csd whitelist. | 189 // in the csd whitelist. |
189 virtual bool IsMalwareIPMatchKillSwitchOn() = 0; | 190 virtual bool IsMalwareIPMatchKillSwitchOn() = 0; |
190 | 191 |
191 // The name of the bloom-filter file for the given database file. | 192 // The name of the bloom-filter file for the given database file. |
192 // NOTE(shess): OBSOLETE. Present for deleting stale files. | 193 // NOTE(shess): OBSOLETE. Present for deleting stale files. |
193 static base::FilePath BloomFilterForFilename( | 194 static base::FilePath BloomFilterForFilename( |
194 const base::FilePath& db_filename); | 195 const base::FilePath& db_filename); |
195 | 196 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 292 |
292 // Create a database with a browse store. This is a legacy interface that | 293 // Create a database with a browse store. This is a legacy interface that |
293 // useds Sqlite. | 294 // useds Sqlite. |
294 SafeBrowsingDatabaseNew(); | 295 SafeBrowsingDatabaseNew(); |
295 | 296 |
296 virtual ~SafeBrowsingDatabaseNew(); | 297 virtual ~SafeBrowsingDatabaseNew(); |
297 | 298 |
298 // Implement SafeBrowsingDatabase interface. | 299 // Implement SafeBrowsingDatabase interface. |
299 virtual void Init(const base::FilePath& filename) OVERRIDE; | 300 virtual void Init(const base::FilePath& filename) OVERRIDE; |
300 virtual bool ResetDatabase() OVERRIDE; | 301 virtual bool ResetDatabase() OVERRIDE; |
301 virtual bool ContainsBrowseUrl(const GURL& url, | 302 virtual bool ContainsBrowseUrl( |
302 std::vector<SBPrefix>* prefix_hits, | 303 const GURL& url, |
303 std::vector<SBFullHashResult>* cached_hits, | 304 std::vector<SBPrefix>* prefix_hits, |
304 base::Time last_update) OVERRIDE; | 305 std::vector<SBFullHashResult>* cached_hits) OVERRIDE; |
305 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, | 306 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, |
306 std::vector<SBPrefix>* prefix_hits) OVERRIDE; | 307 std::vector<SBPrefix>* prefix_hits) OVERRIDE; |
307 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) OVERRIDE; | 308 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) OVERRIDE; |
308 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) OVERRIDE; | 309 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) OVERRIDE; |
309 virtual bool ContainsDownloadWhitelistedString( | 310 virtual bool ContainsDownloadWhitelistedString( |
310 const std::string& str) OVERRIDE; | 311 const std::string& str) OVERRIDE; |
311 virtual bool ContainsExtensionPrefixes( | 312 virtual bool ContainsExtensionPrefixes( |
312 const std::vector<SBPrefix>& prefixes, | 313 const std::vector<SBPrefix>& prefixes, |
313 std::vector<SBPrefix>* prefix_hits) OVERRIDE; | 314 std::vector<SBPrefix>* prefix_hits) OVERRIDE; |
314 virtual bool ContainsSideEffectFreeWhitelistUrl(const GURL& url) OVERRIDE; | 315 virtual bool ContainsSideEffectFreeWhitelistUrl(const GURL& url) OVERRIDE; |
315 virtual bool ContainsMalwareIP(const std::string& ip_address) OVERRIDE; | 316 virtual bool ContainsMalwareIP(const std::string& ip_address) OVERRIDE; |
316 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) OVERRIDE; | 317 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) OVERRIDE; |
317 virtual void InsertChunks(const std::string& list_name, | 318 virtual void InsertChunks(const std::string& list_name, |
318 const SBChunkList& chunks) OVERRIDE; | 319 const SBChunkList& chunks) OVERRIDE; |
319 virtual void DeleteChunks( | 320 virtual void DeleteChunks( |
320 const std::vector<SBChunkDelete>& chunk_deletes) OVERRIDE; | 321 const std::vector<SBChunkDelete>& chunk_deletes) OVERRIDE; |
321 virtual void UpdateFinished(bool update_succeeded) OVERRIDE; | 322 virtual void UpdateFinished(bool update_succeeded) OVERRIDE; |
322 virtual void CacheHashResults( | 323 virtual void CacheHashResults( |
323 const std::vector<SBPrefix>& prefixes, | 324 const std::vector<SBPrefix>& prefixes, |
324 const std::vector<SBFullHashResult>& full_hits) OVERRIDE; | 325 const std::vector<SBFullHashResult>& full_hits, |
| 326 const base::TimeDelta& cache_lifetime) OVERRIDE; |
325 | 327 |
326 // Returns the value of malware_kill_switch_; | 328 // Returns the value of malware_kill_switch_; |
327 virtual bool IsMalwareIPMatchKillSwitchOn() OVERRIDE; | 329 virtual bool IsMalwareIPMatchKillSwitchOn() OVERRIDE; |
328 | 330 |
329 private: | 331 private: |
330 friend class SafeBrowsingDatabaseTest; | 332 friend class SafeBrowsingDatabaseTest; |
331 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseTest, HashCaching); | 333 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseTest, HashCaching); |
332 | 334 |
333 // A SafeBrowsing whitelist contains a list of whitelisted full-hashes (stored | 335 // A SafeBrowsing whitelist contains a list of whitelisted full-hashes (stored |
334 // in a sorted vector) as well as a boolean flag indicating whether all | 336 // in a sorted vector) as well as a boolean flag indicating whether all |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 // Used to check if a prefix was in the browse database. | 473 // Used to check if a prefix was in the browse database. |
472 base::FilePath browse_prefix_set_filename_; | 474 base::FilePath browse_prefix_set_filename_; |
473 scoped_ptr<safe_browsing::PrefixSet> browse_prefix_set_; | 475 scoped_ptr<safe_browsing::PrefixSet> browse_prefix_set_; |
474 | 476 |
475 // Used to check if a prefix was in the browse database. | 477 // Used to check if a prefix was in the browse database. |
476 base::FilePath side_effect_free_whitelist_prefix_set_filename_; | 478 base::FilePath side_effect_free_whitelist_prefix_set_filename_; |
477 scoped_ptr<safe_browsing::PrefixSet> side_effect_free_whitelist_prefix_set_; | 479 scoped_ptr<safe_browsing::PrefixSet> side_effect_free_whitelist_prefix_set_; |
478 }; | 480 }; |
479 | 481 |
480 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 482 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
OLD | NEW |