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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // | 163 // |
164 // This function can ONLY be accessed from the creation thread. | 164 // This function can ONLY be accessed from the creation thread. |
165 virtual bool ContainsExtensionPrefixes( | 165 virtual bool ContainsExtensionPrefixes( |
166 const std::vector<SBPrefix>& prefixes, | 166 const std::vector<SBPrefix>& prefixes, |
167 std::vector<SBPrefix>* prefix_hits) = 0; | 167 std::vector<SBPrefix>* prefix_hits) = 0; |
168 | 168 |
169 // Returns true iff the given IP is currently on the csd malware IP blacklist. | 169 // Returns true iff the given IP is currently on the csd malware IP blacklist. |
170 // This function is safe to call from any thread. | 170 // This function is safe to call from any thread. |
171 virtual bool ContainsMalwareIP(const std::string& ip_address) = 0; | 171 virtual bool ContainsMalwareIP(const std::string& ip_address) = 0; |
172 | 172 |
| 173 // Populates |prefix_hits| with any prefixes in |prefixes| that have matches |
| 174 // in the database. Returns true iff there were any matches. |
| 175 // |
| 176 // This function can ONLY by accessed from the creation thread. |
| 177 virtual bool ContainsResourceUrlPrefixes( |
| 178 const std::vector<SBPrefix>& prefixes, |
| 179 std::vector<SBPrefix>* prefix_hits) = 0; |
| 180 |
173 // A database transaction should look like: | 181 // A database transaction should look like: |
174 // | 182 // |
175 // std::vector<SBListChunkRanges> lists; | 183 // std::vector<SBListChunkRanges> lists; |
176 // if (db.UpdateStarted(&lists)) { | 184 // if (db.UpdateStarted(&lists)) { |
177 // // Do something with |lists|. | 185 // // Do something with |lists|. |
178 // | 186 // |
179 // // Process add/sub commands. | 187 // // Process add/sub commands. |
180 // db.InsertChunks(list_name, chunks); | 188 // db.InsertChunks(list_name, chunks); |
181 // | 189 // |
182 // // Process adddel/subdel commands. | 190 // // Process adddel/subdel commands. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 const base::FilePath& side_effect_free_whitelist_base_filename); | 262 const base::FilePath& side_effect_free_whitelist_base_filename); |
255 | 263 |
256 // Filename for the csd malware IP blacklist database. | 264 // Filename for the csd malware IP blacklist database. |
257 static base::FilePath IpBlacklistDBFilename( | 265 static base::FilePath IpBlacklistDBFilename( |
258 const base::FilePath& ip_blacklist_base_filename); | 266 const base::FilePath& ip_blacklist_base_filename); |
259 | 267 |
260 // Filename for the unwanted software blacklist database. | 268 // Filename for the unwanted software blacklist database. |
261 static base::FilePath UnwantedSoftwareDBFilename( | 269 static base::FilePath UnwantedSoftwareDBFilename( |
262 const base::FilePath& db_filename); | 270 const base::FilePath& db_filename); |
263 | 271 |
| 272 static base::FilePath ResourceBlacklistDBFilename( |
| 273 const base::FilePath& db_filename); |
| 274 |
264 // Get the prefixes matching the download |urls|. | 275 // Get the prefixes matching the download |urls|. |
265 static void GetDownloadUrlPrefixes(const std::vector<GURL>& urls, | 276 static void GetDownloadUrlPrefixes(const std::vector<GURL>& urls, |
266 std::vector<SBPrefix>* prefixes); | 277 std::vector<SBPrefix>* prefixes); |
267 | 278 |
268 // SafeBrowsing Database failure types for histogramming purposes. Explicitly | 279 // SafeBrowsing Database failure types for histogramming purposes. Explicitly |
269 // label new values and do not re-use old values. Also make sure to reflect | 280 // label new values and do not re-use old values. Also make sure to reflect |
270 // modifications made below in the SB2DatabaseFailure histogram enum. | 281 // modifications made below in the SB2DatabaseFailure histogram enum. |
271 enum FailureType { | 282 enum FailureType { |
272 FAILURE_DATABASE_CORRUPT = 0, | 283 FAILURE_DATABASE_CORRUPT = 0, |
273 FAILURE_DATABASE_CORRUPT_HANDLER = 1, | 284 FAILURE_DATABASE_CORRUPT_HANDLER = 1, |
(...skipping 23 matching lines...) Expand all Loading... |
297 // Obsolete: FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE = 25, | 308 // Obsolete: FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE = 25, |
298 FAILURE_IP_BLACKLIST_UPDATE_BEGIN = 26, | 309 FAILURE_IP_BLACKLIST_UPDATE_BEGIN = 26, |
299 FAILURE_IP_BLACKLIST_UPDATE_FINISH = 27, | 310 FAILURE_IP_BLACKLIST_UPDATE_FINISH = 27, |
300 FAILURE_IP_BLACKLIST_UPDATE_INVALID = 28, | 311 FAILURE_IP_BLACKLIST_UPDATE_INVALID = 28, |
301 FAILURE_IP_BLACKLIST_DELETE = 29, | 312 FAILURE_IP_BLACKLIST_DELETE = 29, |
302 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN = 30, | 313 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN = 30, |
303 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH = 31, | 314 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH = 31, |
304 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_READ = 32, | 315 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_READ = 32, |
305 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE = 33, | 316 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE = 33, |
306 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE = 34, | 317 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE = 34, |
| 318 FAILURE_RESOURCE_BLACKLIST_UPDATE_BEGIN = 35, |
| 319 FAILURE_RESOURCE_BLACKLIST_UPDATE_FINISH = 36, |
| 320 FAILURE_RESOURCE_BLACKLIST_DELETE = 37, |
307 | 321 |
308 // Memory space for histograms is determined by the max. ALWAYS | 322 // Memory space for histograms is determined by the max. ALWAYS |
309 // ADD NEW VALUES BEFORE THIS ONE. | 323 // ADD NEW VALUES BEFORE THIS ONE. |
310 FAILURE_DATABASE_MAX | 324 FAILURE_DATABASE_MAX |
311 }; | 325 }; |
312 | 326 |
313 static void RecordFailure(FailureType failure_type); | 327 static void RecordFailure(FailureType failure_type); |
314 | 328 |
315 private: | 329 private: |
316 // The factory used to instantiate a SafeBrowsingDatabase object. | 330 // The factory used to instantiate a SafeBrowsingDatabase object. |
317 // Useful for tests, so they can provide their own implementation of | 331 // Useful for tests, so they can provide their own implementation of |
318 // SafeBrowsingDatabase. | 332 // SafeBrowsingDatabase. |
319 static SafeBrowsingDatabaseFactory* factory_; | 333 static SafeBrowsingDatabaseFactory* factory_; |
320 }; | 334 }; |
321 | 335 |
322 class SafeBrowsingDatabaseNew : public SafeBrowsingDatabase { | 336 class SafeBrowsingDatabaseNew : public SafeBrowsingDatabase { |
323 public: | 337 public: |
324 // Create a database with the stores below. Takes ownership of all store | 338 // Create a database with the stores below. Takes ownership of all store |
325 // objects handed to this constructor. Ignores all future operations on lists | 339 // objects handed to this constructor. Ignores all future operations on lists |
326 // for which the store is initialized to NULL. | 340 // for which the store is initialized to NULL. |
327 SafeBrowsingDatabaseNew( | 341 SafeBrowsingDatabaseNew( |
328 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 342 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
329 SafeBrowsingStore* browse_store, | 343 SafeBrowsingStore* browse_store, |
330 SafeBrowsingStore* download_store, | 344 SafeBrowsingStore* download_store, |
331 SafeBrowsingStore* csd_whitelist_store, | 345 SafeBrowsingStore* csd_whitelist_store, |
332 SafeBrowsingStore* download_whitelist_store, | 346 SafeBrowsingStore* download_whitelist_store, |
333 SafeBrowsingStore* inclusion_whitelist_store, | 347 SafeBrowsingStore* inclusion_whitelist_store, |
334 SafeBrowsingStore* extension_blacklist_store, | 348 SafeBrowsingStore* extension_blacklist_store, |
335 SafeBrowsingStore* ip_blacklist_store, | 349 SafeBrowsingStore* ip_blacklist_store, |
336 SafeBrowsingStore* unwanted_software_store); | 350 SafeBrowsingStore* unwanted_software_store, |
| 351 SafeBrowsingStore* resource_blacklist_store); |
337 | 352 |
338 ~SafeBrowsingDatabaseNew() override; | 353 ~SafeBrowsingDatabaseNew() override; |
339 | 354 |
340 // Implement SafeBrowsingDatabase interface. | 355 // Implement SafeBrowsingDatabase interface. |
341 void Init(const base::FilePath& filename) override; | 356 void Init(const base::FilePath& filename) override; |
342 bool ResetDatabase() override; | 357 bool ResetDatabase() override; |
343 bool ContainsBrowseUrl(const GURL& url, | 358 bool ContainsBrowseUrl(const GURL& url, |
344 std::vector<SBPrefix>* prefix_hits, | 359 std::vector<SBPrefix>* prefix_hits, |
345 std::vector<SBFullHashResult>* cache_hits) override; | 360 std::vector<SBFullHashResult>* cache_hits) override; |
346 bool ContainsBrowseHashes(const std::vector<SBFullHash>& full_hashes, | 361 bool ContainsBrowseHashes(const std::vector<SBFullHash>& full_hashes, |
347 std::vector<SBPrefix>* prefix_hits, | 362 std::vector<SBPrefix>* prefix_hits, |
348 std::vector<SBFullHashResult>* cache_hits) override; | 363 std::vector<SBFullHashResult>* cache_hits) override; |
349 bool ContainsUnwantedSoftwareUrl( | 364 bool ContainsUnwantedSoftwareUrl( |
350 const GURL& url, | 365 const GURL& url, |
351 std::vector<SBPrefix>* prefix_hits, | 366 std::vector<SBPrefix>* prefix_hits, |
352 std::vector<SBFullHashResult>* cache_hits) override; | 367 std::vector<SBFullHashResult>* cache_hits) override; |
353 bool ContainsUnwantedSoftwareHashes( | 368 bool ContainsUnwantedSoftwareHashes( |
354 const std::vector<SBFullHash>& full_hashes, | 369 const std::vector<SBFullHash>& full_hashes, |
355 std::vector<SBPrefix>* prefix_hits, | 370 std::vector<SBPrefix>* prefix_hits, |
356 std::vector<SBFullHashResult>* cache_hits) override; | 371 std::vector<SBFullHashResult>* cache_hits) override; |
357 bool ContainsDownloadUrlPrefixes(const std::vector<SBPrefix>& prefixes, | 372 bool ContainsDownloadUrlPrefixes(const std::vector<SBPrefix>& prefixes, |
358 std::vector<SBPrefix>* prefix_hits) override; | 373 std::vector<SBPrefix>* prefix_hits) override; |
359 bool ContainsCsdWhitelistedUrl(const GURL& url) override; | 374 bool ContainsCsdWhitelistedUrl(const GURL& url) override; |
360 bool ContainsDownloadWhitelistedUrl(const GURL& url) override; | 375 bool ContainsDownloadWhitelistedUrl(const GURL& url) override; |
361 bool ContainsDownloadWhitelistedString(const std::string& str) override; | 376 bool ContainsDownloadWhitelistedString(const std::string& str) override; |
362 bool ContainsInclusionWhitelistedUrl(const GURL& url) override; | 377 bool ContainsInclusionWhitelistedUrl(const GURL& url) override; |
363 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, | 378 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, |
364 std::vector<SBPrefix>* prefix_hits) override; | 379 std::vector<SBPrefix>* prefix_hits) override; |
365 bool ContainsMalwareIP(const std::string& ip_address) override; | 380 bool ContainsMalwareIP(const std::string& ip_address) override; |
| 381 bool ContainsResourceUrlPrefixes(const std::vector<SBPrefix>& prefixes, |
| 382 std::vector<SBPrefix>* prefix_hits) override; |
| 383 |
366 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override; | 384 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override; |
367 void InsertChunks( | 385 void InsertChunks( |
368 const std::string& list_name, | 386 const std::string& list_name, |
369 const std::vector<scoped_ptr<SBChunkData>>& chunks) override; | 387 const std::vector<scoped_ptr<SBChunkData>>& chunks) override; |
370 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override; | 388 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override; |
371 void UpdateFinished(bool update_succeeded) override; | 389 void UpdateFinished(bool update_succeeded) override; |
372 void CacheHashResults(const std::vector<SBPrefix>& prefixes, | 390 void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
373 const std::vector<SBFullHashResult>& full_hits, | 391 const std::vector<SBFullHashResult>& full_hits, |
374 const base::TimeDelta& cache_lifetime) override; | 392 const base::TimeDelta& cache_lifetime) override; |
375 | 393 |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 // whitelist chunks and full-length hashes. This list only contains 256 | 692 // whitelist chunks and full-length hashes. This list only contains 256 |
675 // bit hashes. | 693 // bit hashes. |
676 // - |download_whitelist_store_|: For the download whitelist chunks and | 694 // - |download_whitelist_store_|: For the download whitelist chunks and |
677 // full-length hashes. This list only contains 256 bit hashes. | 695 // full-length hashes. This list only contains 256 bit hashes. |
678 // - |inclusion_whitelist_store_|: For the inclusion whitelist. Same format | 696 // - |inclusion_whitelist_store_|: For the inclusion whitelist. Same format |
679 // as |download_whitelist_store_|. | 697 // as |download_whitelist_store_|. |
680 // - |extension_blacklist_store_|: For extension IDs. | 698 // - |extension_blacklist_store_|: For extension IDs. |
681 // - |ip_blacklist_store_|: For IP blacklist. | 699 // - |ip_blacklist_store_|: For IP blacklist. |
682 // - |unwanted_software_store_|: For unwanted software list (format | 700 // - |unwanted_software_store_|: For unwanted software list (format |
683 // identical to browsing lists). | 701 // identical to browsing lists). |
| 702 // - |resource_blacklist_store_|: For script resource list (format identical |
| 703 // to browsing lists). |
684 // | 704 // |
685 // The stores themselves will be modified throughout the existence of this | 705 // The stores themselves will be modified throughout the existence of this |
686 // database, but shouldn't ever be swapped out (hence the const scoped_ptr -- | 706 // database, but shouldn't ever be swapped out (hence the const scoped_ptr -- |
687 // which could be swapped for C++11's std::optional when that's available). | 707 // which could be swapped for C++11's std::optional when that's available). |
688 // They are NonThreadSafe and should thus only be accessed on the database's | 708 // They are NonThreadSafe and should thus only be accessed on the database's |
689 // main thread as enforced by SafeBrowsingStoreFile's implementation. | 709 // main thread as enforced by SafeBrowsingStoreFile's implementation. |
690 const scoped_ptr<SafeBrowsingStore> browse_store_; | 710 const scoped_ptr<SafeBrowsingStore> browse_store_; |
691 const scoped_ptr<SafeBrowsingStore> download_store_; | 711 const scoped_ptr<SafeBrowsingStore> download_store_; |
692 const scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; | 712 const scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; |
693 const scoped_ptr<SafeBrowsingStore> download_whitelist_store_; | 713 const scoped_ptr<SafeBrowsingStore> download_whitelist_store_; |
694 const scoped_ptr<SafeBrowsingStore> inclusion_whitelist_store_; | 714 const scoped_ptr<SafeBrowsingStore> inclusion_whitelist_store_; |
695 const scoped_ptr<SafeBrowsingStore> extension_blacklist_store_; | 715 const scoped_ptr<SafeBrowsingStore> extension_blacklist_store_; |
696 const scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; | 716 const scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; |
697 const scoped_ptr<SafeBrowsingStore> unwanted_software_store_; | 717 const scoped_ptr<SafeBrowsingStore> unwanted_software_store_; |
| 718 const scoped_ptr<SafeBrowsingStore> resource_blacklist_store_; |
698 | 719 |
699 // Used to schedule resetting the database because of corruption. This factory | 720 // Used to schedule resetting the database because of corruption. This factory |
700 // and the WeakPtrs it issues should only be used on the database's main | 721 // and the WeakPtrs it issues should only be used on the database's main |
701 // thread. | 722 // thread. |
702 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 723 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
703 }; | 724 }; |
704 | 725 |
705 } // namespace safe_browsing | 726 } // namespace safe_browsing |
706 | 727 |
707 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 728 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
OLD | NEW |