Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_database.cc

Issue 910953002: Move SafeBrowsing to the blocking pool via an experiment. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address more comments. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 5 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 // Find full-hash matches. 310 // Find full-hash matches.
311 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; 311 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes;
312 for (size_t i = 0; i < cached_hashes.size(); ++i) { 312 for (size_t i = 0; i < cached_hashes.size(); ++i) {
313 if (SBFullHashEqual(full_hash, cached_hashes[i].hash)) 313 if (SBFullHashEqual(full_hash, cached_hashes[i].hash))
314 results->push_back(cached_hashes[i]); 314 results->push_back(cached_hashes[i]);
315 } 315 }
316 316
317 return true; 317 return true;
318 } 318 }
319 319
320 SafeBrowsingStoreFile* CreateStore(
321 bool enable,
322 scoped_refptr<base::SequencedTaskRunner> task_runner) {
323 if (!enable)
324 return nullptr;
325 return new SafeBrowsingStoreFile(task_runner);
326 }
327
320 } // namespace 328 } // namespace
321 329
322 // The default SafeBrowsingDatabaseFactory. 330 // The default SafeBrowsingDatabaseFactory.
323 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory { 331 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory {
324 public: 332 public:
325 SafeBrowsingDatabase* CreateSafeBrowsingDatabase( 333 SafeBrowsingDatabase* CreateSafeBrowsingDatabase(
334 scoped_refptr<base::SequencedTaskRunner> db_task_runner,
326 bool enable_download_protection, 335 bool enable_download_protection,
327 bool enable_client_side_whitelist, 336 bool enable_client_side_whitelist,
328 bool enable_download_whitelist, 337 bool enable_download_whitelist,
329 bool enable_extension_blacklist, 338 bool enable_extension_blacklist,
330 bool enable_side_effect_free_whitelist, 339 bool enable_side_effect_free_whitelist,
331 bool enable_ip_blacklist, 340 bool enable_ip_blacklist,
332 bool enable_unwanted_software_list) override { 341 bool enable_unwanted_software_list) override {
333 return new SafeBrowsingDatabaseNew( 342 return new SafeBrowsingDatabaseNew(
334 new SafeBrowsingStoreFile, // browse_store 343 db_task_runner, CreateStore(true, db_task_runner), // browse_store
335 enable_download_protection ? new SafeBrowsingStoreFile : NULL, 344 CreateStore(enable_download_protection, db_task_runner),
336 enable_client_side_whitelist ? new SafeBrowsingStoreFile : NULL, 345 CreateStore(enable_client_side_whitelist, db_task_runner),
337 enable_download_whitelist ? new SafeBrowsingStoreFile : NULL, 346 CreateStore(enable_download_whitelist, db_task_runner),
338 new SafeBrowsingStoreFile, // inclusion_whitelist_store 347 CreateStore(true, db_task_runner), // inclusion_whitelist_store
339 enable_extension_blacklist ? new SafeBrowsingStoreFile : NULL, 348 CreateStore(enable_extension_blacklist, db_task_runner),
340 enable_side_effect_free_whitelist ? new SafeBrowsingStoreFile : NULL, 349 CreateStore(enable_side_effect_free_whitelist, db_task_runner),
341 enable_ip_blacklist ? new SafeBrowsingStoreFile : NULL, 350 CreateStore(enable_ip_blacklist, db_task_runner),
342 enable_unwanted_software_list ? new SafeBrowsingStoreFile : NULL); 351 CreateStore(enable_unwanted_software_list, db_task_runner));
343 } 352 }
344 353
345 SafeBrowsingDatabaseFactoryImpl() { } 354 SafeBrowsingDatabaseFactoryImpl() { }
346 355
347 private: 356 private:
348 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactoryImpl); 357 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactoryImpl);
349 }; 358 };
350 359
351 // static 360 // static
352 SafeBrowsingDatabaseFactory* SafeBrowsingDatabase::factory_ = NULL; 361 SafeBrowsingDatabaseFactory* SafeBrowsingDatabase::factory_ = NULL;
353 362
354 // Factory method, non-thread safe. Caller has to make sure this is called 363 // Factory method, should be called on the Safe Browsing sequenced task runner,
355 // on SafeBrowsing Thread. 364 // which is also passed to the function as |current_task_runner|.
356 // TODO(shess): There's no need for a factory any longer. Convert 365 // TODO(shess): There's no need for a factory any longer. Convert
357 // SafeBrowsingDatabaseNew to SafeBrowsingDatabase, and have Create() 366 // SafeBrowsingDatabaseNew to SafeBrowsingDatabase, and have Create()
358 // callers just construct things directly. 367 // callers just construct things directly.
359 SafeBrowsingDatabase* SafeBrowsingDatabase::Create( 368 SafeBrowsingDatabase* SafeBrowsingDatabase::Create(
369 scoped_refptr<base::SequencedTaskRunner> current_task_runner,
360 bool enable_download_protection, 370 bool enable_download_protection,
361 bool enable_client_side_whitelist, 371 bool enable_client_side_whitelist,
362 bool enable_download_whitelist, 372 bool enable_download_whitelist,
363 bool enable_extension_blacklist, 373 bool enable_extension_blacklist,
364 bool enable_side_effect_free_whitelist, 374 bool enable_side_effect_free_whitelist,
365 bool enable_ip_blacklist, 375 bool enable_ip_blacklist,
366 bool enable_unwanted_software_list) { 376 bool enable_unwanted_software_list) {
377 DCHECK(current_task_runner->RunsTasksOnCurrentThread());
367 if (!factory_) 378 if (!factory_)
368 factory_ = new SafeBrowsingDatabaseFactoryImpl(); 379 factory_ = new SafeBrowsingDatabaseFactoryImpl();
369 return factory_->CreateSafeBrowsingDatabase(enable_download_protection, 380 return factory_->CreateSafeBrowsingDatabase(
370 enable_client_side_whitelist, 381 current_task_runner, enable_download_protection,
371 enable_download_whitelist, 382 enable_client_side_whitelist, enable_download_whitelist,
372 enable_extension_blacklist, 383 enable_extension_blacklist, enable_side_effect_free_whitelist,
373 enable_side_effect_free_whitelist, 384 enable_ip_blacklist, enable_unwanted_software_list);
374 enable_ip_blacklist,
375 enable_unwanted_software_list);
376 } 385 }
377 386
378 SafeBrowsingDatabase::~SafeBrowsingDatabase() { 387 SafeBrowsingDatabase::~SafeBrowsingDatabase() {
379 } 388 }
380 389
381 // static 390 // static
382 base::FilePath SafeBrowsingDatabase::BrowseDBFilename( 391 base::FilePath SafeBrowsingDatabase::BrowseDBFilename(
383 const base::FilePath& db_base_filename) { 392 const base::FilePath& db_base_filename) {
384 return base::FilePath(db_base_filename.value() + kBrowseDBFile); 393 return base::FilePath(db_base_filename.value() + kBrowseDBFile);
385 } 394 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 } 448 }
440 449
441 // static 450 // static
442 base::FilePath SafeBrowsingDatabase::UnwantedSoftwareDBFilename( 451 base::FilePath SafeBrowsingDatabase::UnwantedSoftwareDBFilename(
443 const base::FilePath& db_filename) { 452 const base::FilePath& db_filename) {
444 return base::FilePath(db_filename.value() + kUnwantedSoftwareDBFile); 453 return base::FilePath(db_filename.value() + kUnwantedSoftwareDBFile);
445 } 454 }
446 455
447 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { 456 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) {
448 // Stores are not thread safe. 457 // Stores are not thread safe.
449 DCHECK(thread_checker_.CalledOnValidThread()); 458 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
450 459
451 if (list_id == safe_browsing_util::PHISH || 460 if (list_id == safe_browsing_util::PHISH ||
452 list_id == safe_browsing_util::MALWARE) { 461 list_id == safe_browsing_util::MALWARE) {
453 return browse_store_.get(); 462 return browse_store_.get();
454 } else if (list_id == safe_browsing_util::BINURL) { 463 } else if (list_id == safe_browsing_util::BINURL) {
455 return download_store_.get(); 464 return download_store_.get();
456 } else if (list_id == safe_browsing_util::CSDWHITELIST) { 465 } else if (list_id == safe_browsing_util::CSDWHITELIST) {
457 return csd_whitelist_store_.get(); 466 return csd_whitelist_store_.get();
458 } else if (list_id == safe_browsing_util::DOWNLOADWHITELIST) { 467 } else if (list_id == safe_browsing_util::DOWNLOADWHITELIST) {
459 return download_whitelist_store_.get(); 468 return download_whitelist_store_.get();
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 DONT_LOCK_ON_MAIN_THREAD 537 DONT_LOCK_ON_MAIN_THREAD
529 }; 538 };
530 539
531 ReadTransaction(const ThreadSafeStateManager* outer, 540 ReadTransaction(const ThreadSafeStateManager* outer,
532 AutoLockRequirement auto_lock_requirement) 541 AutoLockRequirement auto_lock_requirement)
533 : outer_(outer) { 542 : outer_(outer) {
534 DCHECK(outer_); 543 DCHECK(outer_);
535 if (auto_lock_requirement == AutoLockRequirement::LOCK) 544 if (auto_lock_requirement == AutoLockRequirement::LOCK)
536 transaction_lock_.reset(new base::AutoLock(outer_->lock_)); 545 transaction_lock_.reset(new base::AutoLock(outer_->lock_));
537 else 546 else
538 DCHECK(outer_->thread_checker_.CalledOnValidThread()); 547 DCHECK(outer_->db_task_runner_->RunsTasksOnCurrentThread());
539 } 548 }
540 549
541 const ThreadSafeStateManager* outer_; 550 const ThreadSafeStateManager* outer_;
542 scoped_ptr<base::AutoLock> transaction_lock_; 551 scoped_ptr<base::AutoLock> transaction_lock_;
543 552
544 DISALLOW_COPY_AND_ASSIGN(ReadTransaction); 553 DISALLOW_COPY_AND_ASSIGN(ReadTransaction);
545 }; 554 };
546 555
547 class SafeBrowsingDatabaseNew::ThreadSafeStateManager::WriteTransaction { 556 class SafeBrowsingDatabaseNew::ThreadSafeStateManager::WriteTransaction {
548 public: 557 public:
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 593
585 void clear_prefix_gethash_cache() { outer_->prefix_gethash_cache_.clear(); } 594 void clear_prefix_gethash_cache() { outer_->prefix_gethash_cache_.clear(); }
586 595
587 private: 596 private:
588 // Only ThreadSafeStateManager is allowed to build a WriteTransaction. 597 // Only ThreadSafeStateManager is allowed to build a WriteTransaction.
589 friend class ThreadSafeStateManager; 598 friend class ThreadSafeStateManager;
590 599
591 explicit WriteTransaction(ThreadSafeStateManager* outer) 600 explicit WriteTransaction(ThreadSafeStateManager* outer)
592 : outer_(outer), transaction_lock_(outer_->lock_) { 601 : outer_(outer), transaction_lock_(outer_->lock_) {
593 DCHECK(outer_); 602 DCHECK(outer_);
594 DCHECK(outer_->thread_checker_.CalledOnValidThread()); 603 DCHECK(outer_->db_task_runner_->RunsTasksOnCurrentThread());
595 } 604 }
596 605
597 SBWhitelist* SBWhitelistForId(SBWhitelistId id) { 606 SBWhitelist* SBWhitelistForId(SBWhitelistId id) {
598 switch (id) { 607 switch (id) {
599 case SBWhitelistId::CSD: 608 case SBWhitelistId::CSD:
600 return &outer_->csd_whitelist_; 609 return &outer_->csd_whitelist_;
601 case SBWhitelistId::DOWNLOAD: 610 case SBWhitelistId::DOWNLOAD:
602 return &outer_->download_whitelist_; 611 return &outer_->download_whitelist_;
603 case SBWhitelistId::INCLUSION: 612 case SBWhitelistId::INCLUSION:
604 return &outer_->inclusion_whitelist_; 613 return &outer_->inclusion_whitelist_;
605 } 614 }
606 NOTREACHED(); 615 NOTREACHED();
607 return nullptr; 616 return nullptr;
608 } 617 }
609 618
610 ThreadSafeStateManager* outer_; 619 ThreadSafeStateManager* outer_;
611 base::AutoLock transaction_lock_; 620 base::AutoLock transaction_lock_;
612 621
613 DISALLOW_COPY_AND_ASSIGN(WriteTransaction); 622 DISALLOW_COPY_AND_ASSIGN(WriteTransaction);
614 }; 623 };
615 624
616 SafeBrowsingDatabaseNew::ThreadSafeStateManager::ThreadSafeStateManager( 625 SafeBrowsingDatabaseNew::ThreadSafeStateManager::ThreadSafeStateManager(
617 const base::ThreadChecker& thread_checker) 626 scoped_refptr<const base::SequencedTaskRunner> db_task_runner)
618 : thread_checker_(thread_checker) { 627 : db_task_runner_(db_task_runner) {
619 } 628 }
620 629
621 SafeBrowsingDatabaseNew::ThreadSafeStateManager::~ThreadSafeStateManager() { 630 SafeBrowsingDatabaseNew::ThreadSafeStateManager::~ThreadSafeStateManager() {
622 } 631 }
623 632
633 SafeBrowsingDatabaseNew::DatabaseStateManager::DatabaseStateManager(
634 scoped_refptr<const base::SequencedTaskRunner> db_task_runner)
635 : db_task_runner_(db_task_runner),
636 corruption_detected_(false),
637 change_detected_(false) {
638 }
639
640 SafeBrowsingDatabaseNew::DatabaseStateManager::~DatabaseStateManager() {
641 }
642
624 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> 643 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction>
625 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginReadTransaction() { 644 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginReadTransaction() {
626 return make_scoped_ptr( 645 return make_scoped_ptr(
627 new ReadTransaction(this, ReadTransaction::AutoLockRequirement::LOCK)); 646 new ReadTransaction(this, ReadTransaction::AutoLockRequirement::LOCK));
628 } 647 }
629 648
630 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> SafeBrowsingDatabaseNew:: 649 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> SafeBrowsingDatabaseNew::
631 ThreadSafeStateManager::BeginReadTransactionNoLockOnMainThread() { 650 ThreadSafeStateManager::BeginReadTransactionNoLockOnMainThread() {
632 return make_scoped_ptr(new ReadTransaction( 651 return make_scoped_ptr(new ReadTransaction(
633 this, ReadTransaction::AutoLockRequirement::DONT_LOCK_ON_MAIN_THREAD)); 652 this, ReadTransaction::AutoLockRequirement::DONT_LOCK_ON_MAIN_THREAD));
634 } 653 }
635 654
636 scoped_ptr<SafeBrowsingDatabaseNew::WriteTransaction> 655 scoped_ptr<SafeBrowsingDatabaseNew::WriteTransaction>
637 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginWriteTransaction() { 656 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginWriteTransaction() {
638 return make_scoped_ptr(new WriteTransaction(this)); 657 return make_scoped_ptr(new WriteTransaction(this));
639 } 658 }
640 659
641 SafeBrowsingDatabaseNew::SafeBrowsingDatabaseNew()
642 : SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile, // browse_store
643 NULL, // download_store
644 NULL, // csd_whitelist_store
645 NULL, // download_whitelist_store
646 NULL, // inclusion_whitelist_store
647 NULL, // extension_blacklist_store
648 NULL, // side_effect_free_whitelist_store
649 NULL, // ip_blacklist_store
650 NULL) { // unwanted_software_store
651 DCHECK(browse_store_.get());
652 DCHECK(!download_store_.get());
653 DCHECK(!csd_whitelist_store_.get());
654 DCHECK(!download_whitelist_store_.get());
655 DCHECK(!inclusion_whitelist_store_.get());
656 DCHECK(!extension_blacklist_store_.get());
657 DCHECK(!side_effect_free_whitelist_store_.get());
658 DCHECK(!ip_blacklist_store_.get());
659 DCHECK(!unwanted_software_store_.get());
660 }
661
662 SafeBrowsingDatabaseNew::SafeBrowsingDatabaseNew( 660 SafeBrowsingDatabaseNew::SafeBrowsingDatabaseNew(
661 scoped_refptr<base::SequencedTaskRunner> db_task_runner,
663 SafeBrowsingStore* browse_store, 662 SafeBrowsingStore* browse_store,
664 SafeBrowsingStore* download_store, 663 SafeBrowsingStore* download_store,
665 SafeBrowsingStore* csd_whitelist_store, 664 SafeBrowsingStore* csd_whitelist_store,
666 SafeBrowsingStore* download_whitelist_store, 665 SafeBrowsingStore* download_whitelist_store,
667 SafeBrowsingStore* inclusion_whitelist_store, 666 SafeBrowsingStore* inclusion_whitelist_store,
668 SafeBrowsingStore* extension_blacklist_store, 667 SafeBrowsingStore* extension_blacklist_store,
669 SafeBrowsingStore* side_effect_free_whitelist_store, 668 SafeBrowsingStore* side_effect_free_whitelist_store,
670 SafeBrowsingStore* ip_blacklist_store, 669 SafeBrowsingStore* ip_blacklist_store,
671 SafeBrowsingStore* unwanted_software_store) 670 SafeBrowsingStore* unwanted_software_store)
672 : state_manager_(thread_checker_), 671 : db_task_runner_(db_task_runner),
673 db_state_manager_(thread_checker_), 672 state_manager_(db_task_runner_),
673 db_state_manager_(db_task_runner_),
674 browse_store_(browse_store), 674 browse_store_(browse_store),
675 download_store_(download_store), 675 download_store_(download_store),
676 csd_whitelist_store_(csd_whitelist_store), 676 csd_whitelist_store_(csd_whitelist_store),
677 download_whitelist_store_(download_whitelist_store), 677 download_whitelist_store_(download_whitelist_store),
678 inclusion_whitelist_store_(inclusion_whitelist_store), 678 inclusion_whitelist_store_(inclusion_whitelist_store),
679 extension_blacklist_store_(extension_blacklist_store), 679 extension_blacklist_store_(extension_blacklist_store),
680 side_effect_free_whitelist_store_(side_effect_free_whitelist_store), 680 side_effect_free_whitelist_store_(side_effect_free_whitelist_store),
681 ip_blacklist_store_(ip_blacklist_store), 681 ip_blacklist_store_(ip_blacklist_store),
682 unwanted_software_store_(unwanted_software_store), 682 unwanted_software_store_(unwanted_software_store),
683 reset_factory_(this) { 683 reset_factory_(this) {
684 DCHECK(browse_store_.get()); 684 DCHECK(browse_store_.get());
685 } 685 }
686 686
687 SafeBrowsingDatabaseNew::~SafeBrowsingDatabaseNew() { 687 SafeBrowsingDatabaseNew::~SafeBrowsingDatabaseNew() {
688 // The DCHECK is disabled due to crbug.com/338486 . 688 // The DCHECK is disabled due to crbug.com/338486 .
689 // DCHECK(thread_checker_.CalledOnValidThread()); 689 // DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
690 } 690 }
691 691
692 void SafeBrowsingDatabaseNew::Init(const base::FilePath& filename_base) { 692 void SafeBrowsingDatabaseNew::Init(const base::FilePath& filename_base) {
693 DCHECK(thread_checker_.CalledOnValidThread()); 693 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
694 694
695 db_state_manager_.init_filename_base(filename_base); 695 db_state_manager_.init_filename_base(filename_base);
696 696
697 // TODO(shess): The various stores are really only necessary while doing 697 // TODO(shess): The various stores are really only necessary while doing
698 // updates (see |UpdateFinished()|) or when querying a store directly (see 698 // updates (see |UpdateFinished()|) or when querying a store directly (see
699 // |ContainsDownloadUrl()|). 699 // |ContainsDownloadUrl()|).
700 // The store variables are also tested to see if a list is enabled. Perhaps 700 // The store variables are also tested to see if a list is enabled. Perhaps
701 // the stores could be refactored into an update object so that they are only 701 // the stores could be refactored into an update object so that they are only
702 // live in memory while being actively used. The sense of enabled probably 702 // live in memory while being actively used. The sense of enabled probably
703 // belongs in protocol_manager or database_manager. 703 // belongs in protocol_manager or database_manager.
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 std::vector<SBAddFullHash> full_hashes; 833 std::vector<SBAddFullHash> full_hashes;
834 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) { 834 if (ip_blacklist_store_->GetAddFullHashes(&full_hashes)) {
835 LoadIpBlacklist(full_hashes); 835 LoadIpBlacklist(full_hashes);
836 } else { 836 } else {
837 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. 837 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list.
838 } 838 }
839 } 839 }
840 } 840 }
841 841
842 bool SafeBrowsingDatabaseNew::ResetDatabase() { 842 bool SafeBrowsingDatabaseNew::ResetDatabase() {
843 DCHECK(thread_checker_.CalledOnValidThread()); 843 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
844 844
845 // Delete files on disk. 845 // Delete files on disk.
846 // TODO(shess): Hard to see where one might want to delete without a 846 // TODO(shess): Hard to see where one might want to delete without a
847 // reset. Perhaps inline |Delete()|? 847 // reset. Perhaps inline |Delete()|?
848 if (!Delete()) 848 if (!Delete())
849 return false; 849 return false;
850 850
851 // Reset objects in memory. 851 // Reset objects in memory.
852 scoped_ptr<WriteTransaction> txn = state_manager_.BeginWriteTransaction(); 852 scoped_ptr<WriteTransaction> txn = state_manager_.BeginWriteTransaction();
853 txn->clear_prefix_gethash_cache(); 853 txn->clear_prefix_gethash_cache();
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 std::sort(prefix_hits->begin(), prefix_hits->end()); 933 std::sort(prefix_hits->begin(), prefix_hits->end());
934 prefix_hits->erase(std::unique(prefix_hits->begin(), prefix_hits->end()), 934 prefix_hits->erase(std::unique(prefix_hits->begin(), prefix_hits->end()),
935 prefix_hits->end()); 935 prefix_hits->end());
936 936
937 return !prefix_hits->empty() || !cache_hits->empty(); 937 return !prefix_hits->empty() || !cache_hits->empty();
938 } 938 }
939 939
940 bool SafeBrowsingDatabaseNew::ContainsDownloadUrl( 940 bool SafeBrowsingDatabaseNew::ContainsDownloadUrl(
941 const std::vector<GURL>& urls, 941 const std::vector<GURL>& urls,
942 std::vector<SBPrefix>* prefix_hits) { 942 std::vector<SBPrefix>* prefix_hits) {
943 DCHECK(thread_checker_.CalledOnValidThread()); 943 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
944 944
945 // Ignore this check when download checking is not enabled. 945 // Ignore this check when download checking is not enabled.
946 if (!download_store_.get()) 946 if (!download_store_.get())
947 return false; 947 return false;
948 948
949 std::vector<SBPrefix> prefixes; 949 std::vector<SBPrefix> prefixes;
950 GetDownloadUrlPrefixes(urls, &prefixes); 950 GetDownloadUrlPrefixes(urls, &prefixes);
951 return MatchAddPrefixes(download_store_.get(), 951 return MatchAddPrefixes(download_store_.get(),
952 safe_browsing_util::BINURL % 2, 952 safe_browsing_util::BINURL % 2,
953 prefixes, 953 prefixes,
(...skipping 14 matching lines...) Expand all
968 968
969 bool SafeBrowsingDatabaseNew::ContainsInclusionWhitelistedUrl(const GURL& url) { 969 bool SafeBrowsingDatabaseNew::ContainsInclusionWhitelistedUrl(const GURL& url) {
970 std::vector<SBFullHash> full_hashes; 970 std::vector<SBFullHash> full_hashes;
971 UrlToFullHashes(url, true, &full_hashes); 971 UrlToFullHashes(url, true, &full_hashes);
972 return ContainsWhitelistedHashes(SBWhitelistId::INCLUSION, full_hashes); 972 return ContainsWhitelistedHashes(SBWhitelistId::INCLUSION, full_hashes);
973 } 973 }
974 974
975 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( 975 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes(
976 const std::vector<SBPrefix>& prefixes, 976 const std::vector<SBPrefix>& prefixes,
977 std::vector<SBPrefix>* prefix_hits) { 977 std::vector<SBPrefix>* prefix_hits) {
978 DCHECK(thread_checker_.CalledOnValidThread()); 978 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
979 979
980 if (!extension_blacklist_store_) 980 if (!extension_blacklist_store_)
981 return false; 981 return false;
982 982
983 return MatchAddPrefixes(extension_blacklist_store_.get(), 983 return MatchAddPrefixes(extension_blacklist_store_.get(),
984 safe_browsing_util::EXTENSIONBLACKLIST % 2, 984 safe_browsing_util::EXTENSIONBLACKLIST % 2,
985 prefixes, 985 prefixes,
986 prefix_hits); 986 prefix_hits);
987 } 987 }
988 988
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 } 1065 }
1066 } 1066 }
1067 return false; 1067 return false;
1068 } 1068 }
1069 1069
1070 // Helper to insert add-chunk entries. 1070 // Helper to insert add-chunk entries.
1071 void SafeBrowsingDatabaseNew::InsertAddChunk( 1071 void SafeBrowsingDatabaseNew::InsertAddChunk(
1072 SafeBrowsingStore* store, 1072 SafeBrowsingStore* store,
1073 const safe_browsing_util::ListType list_id, 1073 const safe_browsing_util::ListType list_id,
1074 const SBChunkData& chunk_data) { 1074 const SBChunkData& chunk_data) {
1075 DCHECK(thread_checker_.CalledOnValidThread()); 1075 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1076 DCHECK(store); 1076 DCHECK(store);
1077 1077
1078 // The server can give us a chunk that we already have because 1078 // The server can give us a chunk that we already have because
1079 // it's part of a range. Don't add it again. 1079 // it's part of a range. Don't add it again.
1080 const int chunk_id = chunk_data.ChunkNumber(); 1080 const int chunk_id = chunk_data.ChunkNumber();
1081 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); 1081 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id);
1082 if (store->CheckAddChunk(encoded_chunk_id)) 1082 if (store->CheckAddChunk(encoded_chunk_id))
1083 return; 1083 return;
1084 1084
1085 store->SetAddChunk(encoded_chunk_id); 1085 store->SetAddChunk(encoded_chunk_id);
1086 if (chunk_data.IsPrefix()) { 1086 if (chunk_data.IsPrefix()) {
1087 const size_t c = chunk_data.PrefixCount(); 1087 const size_t c = chunk_data.PrefixCount();
1088 for (size_t i = 0; i < c; ++i) { 1088 for (size_t i = 0; i < c; ++i) {
1089 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); 1089 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i));
1090 } 1090 }
1091 } else { 1091 } else {
1092 const size_t c = chunk_data.FullHashCount(); 1092 const size_t c = chunk_data.FullHashCount();
1093 for (size_t i = 0; i < c; ++i) { 1093 for (size_t i = 0; i < c; ++i) {
1094 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); 1094 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i));
1095 } 1095 }
1096 } 1096 }
1097 } 1097 }
1098 1098
1099 // Helper to insert sub-chunk entries. 1099 // Helper to insert sub-chunk entries.
1100 void SafeBrowsingDatabaseNew::InsertSubChunk( 1100 void SafeBrowsingDatabaseNew::InsertSubChunk(
1101 SafeBrowsingStore* store, 1101 SafeBrowsingStore* store,
1102 const safe_browsing_util::ListType list_id, 1102 const safe_browsing_util::ListType list_id,
1103 const SBChunkData& chunk_data) { 1103 const SBChunkData& chunk_data) {
1104 DCHECK(thread_checker_.CalledOnValidThread()); 1104 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1105 DCHECK(store); 1105 DCHECK(store);
1106 1106
1107 // The server can give us a chunk that we already have because 1107 // The server can give us a chunk that we already have because
1108 // it's part of a range. Don't add it again. 1108 // it's part of a range. Don't add it again.
1109 const int chunk_id = chunk_data.ChunkNumber(); 1109 const int chunk_id = chunk_data.ChunkNumber();
1110 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); 1110 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id);
1111 if (store->CheckSubChunk(encoded_chunk_id)) 1111 if (store->CheckSubChunk(encoded_chunk_id))
1112 return; 1112 return;
1113 1113
1114 store->SetSubChunk(encoded_chunk_id); 1114 store->SetSubChunk(encoded_chunk_id);
(...skipping 12 matching lines...) Expand all
1127 const int encoded_add_chunk_id = EncodeChunkId(add_chunk_id, list_id); 1127 const int encoded_add_chunk_id = EncodeChunkId(add_chunk_id, list_id);
1128 store->WriteSubHash(encoded_chunk_id, encoded_add_chunk_id, 1128 store->WriteSubHash(encoded_chunk_id, encoded_add_chunk_id,
1129 chunk_data.FullHashAt(i)); 1129 chunk_data.FullHashAt(i));
1130 } 1130 }
1131 } 1131 }
1132 } 1132 }
1133 1133
1134 void SafeBrowsingDatabaseNew::InsertChunks( 1134 void SafeBrowsingDatabaseNew::InsertChunks(
1135 const std::string& list_name, 1135 const std::string& list_name,
1136 const std::vector<SBChunkData*>& chunks) { 1136 const std::vector<SBChunkData*>& chunks) {
1137 DCHECK(thread_checker_.CalledOnValidThread()); 1137 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1138 1138
1139 if (db_state_manager_.corruption_detected() || chunks.empty()) 1139 if (db_state_manager_.corruption_detected() || chunks.empty())
1140 return; 1140 return;
1141 1141
1142 const base::TimeTicks before = base::TimeTicks::Now(); 1142 const base::TimeTicks before = base::TimeTicks::Now();
1143 1143
1144 // TODO(shess): The caller should just pass list_id. 1144 // TODO(shess): The caller should just pass list_id.
1145 const safe_browsing_util::ListType list_id = 1145 const safe_browsing_util::ListType list_id =
1146 safe_browsing_util::GetListId(list_name); 1146 safe_browsing_util::GetListId(list_name);
1147 1147
(...skipping 14 matching lines...) Expand all
1162 NOTREACHED(); 1162 NOTREACHED();
1163 } 1163 }
1164 } 1164 }
1165 store->FinishChunk(); 1165 store->FinishChunk();
1166 1166
1167 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before); 1167 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before);
1168 } 1168 }
1169 1169
1170 void SafeBrowsingDatabaseNew::DeleteChunks( 1170 void SafeBrowsingDatabaseNew::DeleteChunks(
1171 const std::vector<SBChunkDelete>& chunk_deletes) { 1171 const std::vector<SBChunkDelete>& chunk_deletes) {
1172 DCHECK(thread_checker_.CalledOnValidThread()); 1172 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1173 1173
1174 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) 1174 if (db_state_manager_.corruption_detected() || chunk_deletes.empty())
1175 return; 1175 return;
1176 1176
1177 const std::string& list_name = chunk_deletes.front().list_name; 1177 const std::string& list_name = chunk_deletes.front().list_name;
1178 const safe_browsing_util::ListType list_id = 1178 const safe_browsing_util::ListType list_id =
1179 safe_browsing_util::GetListId(list_name); 1179 safe_browsing_util::GetListId(list_name);
1180 1180
1181 SafeBrowsingStore* store = GetStore(list_id); 1181 SafeBrowsingStore* store = GetStore(list_id);
1182 if (!store) return; 1182 if (!store) return;
(...skipping 30 matching lines...) Expand all
1213 // Insert any fullhash hits. Note that there may be one, multiple, or no 1213 // Insert any fullhash hits. Note that there may be one, multiple, or no
1214 // fullhashes for any given entry in |prefixes|. 1214 // fullhashes for any given entry in |prefixes|.
1215 for (size_t i = 0; i < full_hits.size(); ++i) { 1215 for (size_t i = 0; i < full_hits.size(); ++i) {
1216 const SBPrefix prefix = full_hits[i].hash.prefix; 1216 const SBPrefix prefix = full_hits[i].hash.prefix;
1217 (*prefix_gethash_cache)[prefix].full_hashes.push_back(full_hits[i]); 1217 (*prefix_gethash_cache)[prefix].full_hashes.push_back(full_hits[i]);
1218 } 1218 }
1219 } 1219 }
1220 1220
1221 bool SafeBrowsingDatabaseNew::UpdateStarted( 1221 bool SafeBrowsingDatabaseNew::UpdateStarted(
1222 std::vector<SBListChunkRanges>* lists) { 1222 std::vector<SBListChunkRanges>* lists) {
1223 DCHECK(thread_checker_.CalledOnValidThread()); 1223 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1224 DCHECK(lists); 1224 DCHECK(lists);
1225 1225
1226 // If |BeginUpdate()| fails, reset the database. 1226 // If |BeginUpdate()| fails, reset the database.
1227 if (!browse_store_->BeginUpdate()) { 1227 if (!browse_store_->BeginUpdate()) {
1228 RecordFailure(FAILURE_BROWSE_DATABASE_UPDATE_BEGIN); 1228 RecordFailure(FAILURE_BROWSE_DATABASE_UPDATE_BEGIN);
1229 HandleCorruptDatabase(); 1229 HandleCorruptDatabase();
1230 return false; 1230 return false;
1231 } 1231 }
1232 1232
1233 if (download_store_.get() && !download_store_->BeginUpdate()) { 1233 if (download_store_.get() && !download_store_->BeginUpdate()) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1319 UpdateChunkRangesForList(unwanted_software_store_.get(), 1319 UpdateChunkRangesForList(unwanted_software_store_.get(),
1320 safe_browsing_util::kUnwantedUrlList, 1320 safe_browsing_util::kUnwantedUrlList,
1321 lists); 1321 lists);
1322 1322
1323 db_state_manager_.reset_corruption_detected(); 1323 db_state_manager_.reset_corruption_detected();
1324 db_state_manager_.reset_change_detected(); 1324 db_state_manager_.reset_change_detected();
1325 return true; 1325 return true;
1326 } 1326 }
1327 1327
1328 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { 1328 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) {
1329 DCHECK(thread_checker_.CalledOnValidThread()); 1329 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1330 1330
1331 // The update may have failed due to corrupt storage (for instance, 1331 // The update may have failed due to corrupt storage (for instance,
1332 // an excessive number of invalid add_chunks and sub_chunks). 1332 // an excessive number of invalid add_chunks and sub_chunks).
1333 // Double-check that the databases are valid. 1333 // Double-check that the databases are valid.
1334 // TODO(shess): Providing a checksum for the add_chunk and sub_chunk 1334 // TODO(shess): Providing a checksum for the add_chunk and sub_chunk
1335 // sections would allow throwing a corruption error in 1335 // sections would allow throwing a corruption error in
1336 // UpdateStarted(). 1336 // UpdateStarted().
1337 if (!update_succeeded) { 1337 if (!update_succeeded) {
1338 if (!browse_store_->CheckValidity()) 1338 if (!browse_store_->CheckValidity())
1339 DLOG(ERROR) << "Safe-browsing browse database corrupt."; 1339 DLOG(ERROR) << "Safe-browsing browse database corrupt.";
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1451 unwanted_software_store_.get(), PrefixSetId::UNWANTED_SOFTWARE, 1451 unwanted_software_store_.get(), PrefixSetId::UNWANTED_SOFTWARE,
1452 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH, 1452 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH,
1453 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE, true); 1453 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE, true);
1454 } 1454 }
1455 } 1455 }
1456 1456
1457 void SafeBrowsingDatabaseNew::UpdateWhitelistStore( 1457 void SafeBrowsingDatabaseNew::UpdateWhitelistStore(
1458 const base::FilePath& store_filename, 1458 const base::FilePath& store_filename,
1459 SafeBrowsingStore* store, 1459 SafeBrowsingStore* store,
1460 SBWhitelistId whitelist_id) { 1460 SBWhitelistId whitelist_id) {
1461 DCHECK(thread_checker_.CalledOnValidThread()); 1461 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1462 1462
1463 if (!store) 1463 if (!store)
1464 return; 1464 return;
1465 1465
1466 // Note: |builder| will not be empty. The current data store implementation 1466 // Note: |builder| will not be empty. The current data store implementation
1467 // stores all full-length hashes as both full and prefix hashes. 1467 // stores all full-length hashes as both full and prefix hashes.
1468 PrefixSetBuilder builder; 1468 PrefixSetBuilder builder;
1469 std::vector<SBAddFullHash> full_hashes; 1469 std::vector<SBAddFullHash> full_hashes;
1470 if (!store->FinishUpdate(&builder, &full_hashes)) { 1470 if (!store->FinishUpdate(&builder, &full_hashes)) {
1471 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH); 1471 RecordFailure(FAILURE_WHITELIST_DATABASE_UPDATE_FINISH);
1472 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); 1472 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id);
1473 return; 1473 return;
1474 } 1474 }
1475 1475
1476 RecordFileSizeHistogram(store_filename); 1476 RecordFileSizeHistogram(store_filename);
1477 1477
1478 #if defined(OS_MACOSX) 1478 #if defined(OS_MACOSX)
1479 base::mac::SetFileBackupExclusion(store_filename); 1479 base::mac::SetFileBackupExclusion(store_filename);
1480 #endif 1480 #endif
1481 1481
1482 LoadWhitelist(full_hashes, whitelist_id); 1482 LoadWhitelist(full_hashes, whitelist_id);
1483 } 1483 }
1484 1484
1485 void SafeBrowsingDatabaseNew::UpdateHashPrefixStore( 1485 void SafeBrowsingDatabaseNew::UpdateHashPrefixStore(
1486 const base::FilePath& store_filename, 1486 const base::FilePath& store_filename,
1487 SafeBrowsingStore* store, 1487 SafeBrowsingStore* store,
1488 FailureType failure_type) { 1488 FailureType failure_type) {
1489 DCHECK(thread_checker_.CalledOnValidThread()); 1489 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1490 1490
1491 // These results are not used after this call. Simply ignore the 1491 // These results are not used after this call. Simply ignore the
1492 // returned value after FinishUpdate(...). 1492 // returned value after FinishUpdate(...).
1493 PrefixSetBuilder builder; 1493 PrefixSetBuilder builder;
1494 std::vector<SBAddFullHash> add_full_hashes_result; 1494 std::vector<SBAddFullHash> add_full_hashes_result;
1495 1495
1496 if (!store->FinishUpdate(&builder, &add_full_hashes_result)) 1496 if (!store->FinishUpdate(&builder, &add_full_hashes_result))
1497 RecordFailure(failure_type); 1497 RecordFailure(failure_type);
1498 1498
1499 RecordFileSizeHistogram(store_filename); 1499 RecordFileSizeHistogram(store_filename);
1500 1500
1501 #if defined(OS_MACOSX) 1501 #if defined(OS_MACOSX)
1502 base::mac::SetFileBackupExclusion(store_filename); 1502 base::mac::SetFileBackupExclusion(store_filename);
1503 #endif 1503 #endif
1504 } 1504 }
1505 1505
1506 void SafeBrowsingDatabaseNew::UpdatePrefixSetUrlStore( 1506 void SafeBrowsingDatabaseNew::UpdatePrefixSetUrlStore(
1507 const base::FilePath& db_filename, 1507 const base::FilePath& db_filename,
1508 SafeBrowsingStore* url_store, 1508 SafeBrowsingStore* url_store,
1509 PrefixSetId prefix_set_id, 1509 PrefixSetId prefix_set_id,
1510 FailureType finish_failure_type, 1510 FailureType finish_failure_type,
1511 FailureType write_failure_type, 1511 FailureType write_failure_type,
1512 bool store_full_hashes_in_prefix_set) { 1512 bool store_full_hashes_in_prefix_set) {
1513 DCHECK(thread_checker_.CalledOnValidThread()); 1513 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1514 DCHECK(url_store); 1514 DCHECK(url_store);
1515 1515
1516 // Measure the amount of IO during the filter build. 1516 // Measure the amount of IO during the filter build.
1517 base::IoCounters io_before, io_after; 1517 base::IoCounters io_before, io_after;
1518 base::ProcessHandle handle = base::GetCurrentProcessHandle(); 1518 base::ProcessHandle handle = base::GetCurrentProcessHandle();
1519 scoped_ptr<base::ProcessMetrics> metric( 1519 scoped_ptr<base::ProcessMetrics> metric(
1520 #if !defined(OS_MACOSX) 1520 #if !defined(OS_MACOSX)
1521 base::ProcessMetrics::CreateProcessMetrics(handle) 1521 base::ProcessMetrics::CreateProcessMetrics(handle)
1522 #else 1522 #else
1523 // Getting stats only for the current process is enough, so NULL is fine. 1523 // Getting stats only for the current process is enough, so NULL is fine.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1583 } 1583 }
1584 1584
1585 RecordFileSizeHistogram(db_filename); 1585 RecordFileSizeHistogram(db_filename);
1586 1586
1587 #if defined(OS_MACOSX) 1587 #if defined(OS_MACOSX)
1588 base::mac::SetFileBackupExclusion(db_filename); 1588 base::mac::SetFileBackupExclusion(db_filename);
1589 #endif 1589 #endif
1590 } 1590 }
1591 1591
1592 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() { 1592 void SafeBrowsingDatabaseNew::UpdateIpBlacklistStore() {
1593 DCHECK(thread_checker_.CalledOnValidThread()); 1593 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1594 1594
1595 // Note: prefixes will not be empty. The current data store implementation 1595 // Note: prefixes will not be empty. The current data store implementation
1596 // stores all full-length hashes as both full and prefix hashes. 1596 // stores all full-length hashes as both full and prefix hashes.
1597 PrefixSetBuilder builder; 1597 PrefixSetBuilder builder;
1598 std::vector<SBAddFullHash> full_hashes; 1598 std::vector<SBAddFullHash> full_hashes;
1599 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) { 1599 if (!ip_blacklist_store_->FinishUpdate(&builder, &full_hashes)) {
1600 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH); 1600 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_FINISH);
1601 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list. 1601 LoadIpBlacklist(std::vector<SBAddFullHash>()); // Clear the list.
1602 return; 1602 return;
1603 } 1603 }
1604 1604
1605 const base::FilePath ip_blacklist_filename = 1605 const base::FilePath ip_blacklist_filename =
1606 IpBlacklistDBFilename(db_state_manager_.filename_base()); 1606 IpBlacklistDBFilename(db_state_manager_.filename_base());
1607 1607
1608 RecordFileSizeHistogram(ip_blacklist_filename); 1608 RecordFileSizeHistogram(ip_blacklist_filename);
1609 1609
1610 #if defined(OS_MACOSX) 1610 #if defined(OS_MACOSX)
1611 base::mac::SetFileBackupExclusion(ip_blacklist_filename); 1611 base::mac::SetFileBackupExclusion(ip_blacklist_filename);
1612 #endif 1612 #endif
1613 1613
1614 LoadIpBlacklist(full_hashes); 1614 LoadIpBlacklist(full_hashes);
1615 } 1615 }
1616 1616
1617 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() { 1617 void SafeBrowsingDatabaseNew::HandleCorruptDatabase() {
1618 DCHECK(thread_checker_.CalledOnValidThread()); 1618 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1619 1619
1620 // Reset the database after the current task has unwound (but only 1620 // Reset the database after the current task has unwound (but only
1621 // reset once within the scope of a given task). 1621 // reset once within the scope of a given task).
1622 if (!reset_factory_.HasWeakPtrs()) { 1622 if (!reset_factory_.HasWeakPtrs()) {
1623 RecordFailure(FAILURE_DATABASE_CORRUPT); 1623 RecordFailure(FAILURE_DATABASE_CORRUPT);
1624 base::MessageLoop::current()->PostTask(FROM_HERE, 1624 db_task_runner_->PostTask(
1625 base::Bind(&SafeBrowsingDatabaseNew::OnHandleCorruptDatabase, 1625 FROM_HERE, base::Bind(&SafeBrowsingDatabaseNew::OnHandleCorruptDatabase,
1626 reset_factory_.GetWeakPtr())); 1626 reset_factory_.GetWeakPtr()));
1627 } 1627 }
1628 } 1628 }
1629 1629
1630 void SafeBrowsingDatabaseNew::OnHandleCorruptDatabase() { 1630 void SafeBrowsingDatabaseNew::OnHandleCorruptDatabase() {
1631 DCHECK(thread_checker_.CalledOnValidThread()); 1631 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1632 1632
1633 RecordFailure(FAILURE_DATABASE_CORRUPT_HANDLER); 1633 RecordFailure(FAILURE_DATABASE_CORRUPT_HANDLER);
1634 db_state_manager_.set_corruption_detected(); // Stop updating the database. 1634 db_state_manager_.set_corruption_detected(); // Stop updating the database.
1635 ResetDatabase(); 1635 ResetDatabase();
1636 1636
1637 // NOTE(shess): ResetDatabase() should remove the corruption, so this should 1637 // NOTE(shess): ResetDatabase() should remove the corruption, so this should
1638 // only happen once. If you are here because you are hitting this after a 1638 // only happen once. If you are here because you are hitting this after a
1639 // restart, then I would be very interested in working with you to figure out 1639 // restart, then I would be very interested in working with you to figure out
1640 // what is happening, since it may affect real users. 1640 // what is happening, since it may affect real users.
1641 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset"; 1641 DLOG(FATAL) << "SafeBrowsing database was corrupt and reset";
1642 } 1642 }
1643 1643
1644 // TODO(shess): I'm not clear why this code doesn't have any 1644 // TODO(shess): I'm not clear why this code doesn't have any
1645 // real error-handling. 1645 // real error-handling.
1646 void SafeBrowsingDatabaseNew::LoadPrefixSet(const base::FilePath& db_filename, 1646 void SafeBrowsingDatabaseNew::LoadPrefixSet(const base::FilePath& db_filename,
1647 WriteTransaction* txn, 1647 WriteTransaction* txn,
1648 PrefixSetId prefix_set_id, 1648 PrefixSetId prefix_set_id,
1649 FailureType read_failure_type) { 1649 FailureType read_failure_type) {
1650 DCHECK(thread_checker_.CalledOnValidThread()); 1650 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1651 DCHECK(txn); 1651 DCHECK(txn);
1652 DCHECK(!db_state_manager_.filename_base().empty()); 1652 DCHECK(!db_state_manager_.filename_base().empty());
1653 1653
1654 // Only use the prefix set if database is present and non-empty. 1654 // Only use the prefix set if database is present and non-empty.
1655 if (!GetFileSizeOrZero(db_filename)) 1655 if (!GetFileSizeOrZero(db_filename))
1656 return; 1656 return;
1657 1657
1658 // Cleanup any stale bloom filter (no longer used). 1658 // Cleanup any stale bloom filter (no longer used).
1659 // TODO(shess): Track existence to drive removal of this code? 1659 // TODO(shess): Track existence to drive removal of this code?
1660 const base::FilePath bloom_filter_filename = 1660 const base::FilePath bloom_filter_filename =
1661 BloomFilterForFilename(db_filename); 1661 BloomFilterForFilename(db_filename);
1662 base::DeleteFile(bloom_filter_filename, false); 1662 base::DeleteFile(bloom_filter_filename, false);
1663 1663
1664 const base::TimeTicks before = base::TimeTicks::Now(); 1664 const base::TimeTicks before = base::TimeTicks::Now();
1665 scoped_ptr<const PrefixSet> new_prefix_set = 1665 scoped_ptr<const PrefixSet> new_prefix_set =
1666 PrefixSet::LoadFile(PrefixSetForFilename(db_filename)); 1666 PrefixSet::LoadFile(PrefixSetForFilename(db_filename));
1667 if (!new_prefix_set.get()) 1667 if (!new_prefix_set.get())
1668 RecordFailure(read_failure_type); 1668 RecordFailure(read_failure_type);
1669 txn->SwapPrefixSet(prefix_set_id, new_prefix_set.Pass()); 1669 txn->SwapPrefixSet(prefix_set_id, new_prefix_set.Pass());
1670 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before); 1670 UMA_HISTOGRAM_TIMES("SB2.PrefixSetLoad", base::TimeTicks::Now() - before);
1671 } 1671 }
1672 1672
1673 bool SafeBrowsingDatabaseNew::Delete() { 1673 bool SafeBrowsingDatabaseNew::Delete() {
1674 DCHECK(thread_checker_.CalledOnValidThread()); 1674 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1675 DCHECK(!db_state_manager_.filename_base().empty()); 1675 DCHECK(!db_state_manager_.filename_base().empty());
1676 1676
1677 // TODO(shess): This is a mess. SafeBrowsingFileStore::Delete() closes the 1677 // TODO(shess): This is a mess. SafeBrowsingFileStore::Delete() closes the
1678 // store before calling DeleteStore(). DeleteStore() deletes transient files 1678 // store before calling DeleteStore(). DeleteStore() deletes transient files
1679 // in addition to the main file. Probably all of these should be converted to 1679 // in addition to the main file. Probably all of these should be converted to
1680 // a helper which calls Delete() if the store exists, else DeleteStore() on 1680 // a helper which calls Delete() if the store exists, else DeleteStore() on
1681 // the generated filename. 1681 // the generated filename.
1682 1682
1683 // TODO(shess): Determine if the histograms are useful in any way. I cannot 1683 // TODO(shess): Determine if the histograms are useful in any way. I cannot
1684 // recall any action taken as a result of their values, in which case it might 1684 // recall any action taken as a result of their values, in which case it might
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1753 if (!r12) 1753 if (!r12)
1754 RecordFailure(FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE); 1754 RecordFailure(FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE);
1755 1755
1756 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10 && r11 && 1756 return r1 && r2 && r3 && r4 && r5 && r6 && r7 && r8 && r9 && r10 && r11 &&
1757 r12; 1757 r12;
1758 } 1758 }
1759 1759
1760 void SafeBrowsingDatabaseNew::WritePrefixSet(const base::FilePath& db_filename, 1760 void SafeBrowsingDatabaseNew::WritePrefixSet(const base::FilePath& db_filename,
1761 PrefixSetId prefix_set_id, 1761 PrefixSetId prefix_set_id,
1762 FailureType write_failure_type) { 1762 FailureType write_failure_type) {
1763 DCHECK(thread_checker_.CalledOnValidThread()); 1763 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1764 1764
1765 // Do not grab the lock to avoid contention while writing to disk. This is 1765 // Do not grab the lock to avoid contention while writing to disk. This is
1766 // safe as only this thread can ever modify |state_manager_|'s prefix sets 1766 // safe as only this thread can ever modify |state_manager_|'s prefix sets
1767 // anyways. 1767 // anyways.
gab 2015/02/23 19:53:33 This comment (and use-case) below is still correct
Alexei Svitkine (slow) 2015/02/24 18:08:57 Done. Also updated the method name that said OnMai
1768 scoped_ptr<ReadTransaction> txn = 1768 scoped_ptr<ReadTransaction> txn =
1769 state_manager_.BeginReadTransactionNoLockOnMainThread(); 1769 state_manager_.BeginReadTransactionNoLockOnMainThread();
1770 const PrefixSet* prefix_set = txn->GetPrefixSet(prefix_set_id); 1770 const PrefixSet* prefix_set = txn->GetPrefixSet(prefix_set_id);
1771 1771
1772 if (!prefix_set) 1772 if (!prefix_set)
1773 return; 1773 return;
1774 1774
1775 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename); 1775 const base::FilePath prefix_set_filename = PrefixSetForFilename(db_filename);
1776 1776
1777 const base::TimeTicks before = base::TimeTicks::Now(); 1777 const base::TimeTicks before = base::TimeTicks::Now();
1778 const bool write_ok = prefix_set->WriteFile(prefix_set_filename); 1778 const bool write_ok = prefix_set->WriteFile(prefix_set_filename);
1779 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before); 1779 UMA_HISTOGRAM_TIMES("SB2.PrefixSetWrite", base::TimeTicks::Now() - before);
1780 1780
1781 RecordFileSizeHistogram(prefix_set_filename); 1781 RecordFileSizeHistogram(prefix_set_filename);
1782 1782
1783 if (!write_ok) 1783 if (!write_ok)
1784 RecordFailure(write_failure_type); 1784 RecordFailure(write_failure_type);
1785 1785
1786 #if defined(OS_MACOSX) 1786 #if defined(OS_MACOSX)
1787 base::mac::SetFileBackupExclusion(prefix_set_filename); 1787 base::mac::SetFileBackupExclusion(prefix_set_filename);
1788 #endif 1788 #endif
1789 } 1789 }
1790 1790
1791 void SafeBrowsingDatabaseNew::LoadWhitelist( 1791 void SafeBrowsingDatabaseNew::LoadWhitelist(
1792 const std::vector<SBAddFullHash>& full_hashes, 1792 const std::vector<SBAddFullHash>& full_hashes,
1793 SBWhitelistId whitelist_id) { 1793 SBWhitelistId whitelist_id) {
1794 DCHECK(thread_checker_.CalledOnValidThread()); 1794 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1795 1795
1796 if (full_hashes.size() > kMaxWhitelistSize) { 1796 if (full_hashes.size() > kMaxWhitelistSize) {
1797 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); 1797 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id);
1798 return; 1798 return;
1799 } 1799 }
1800 1800
1801 std::vector<SBFullHash> new_whitelist; 1801 std::vector<SBFullHash> new_whitelist;
1802 new_whitelist.reserve(full_hashes.size()); 1802 new_whitelist.reserve(full_hashes.size());
1803 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); 1803 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin();
1804 it != full_hashes.end(); ++it) { 1804 it != full_hashes.end(); ++it) {
1805 new_whitelist.push_back(it->full_hash); 1805 new_whitelist.push_back(it->full_hash);
1806 } 1806 }
1807 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess); 1807 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess);
1808 1808
1809 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl); 1809 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl);
1810 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), 1810 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(),
1811 kill_switch, SBFullHashLess)) { 1811 kill_switch, SBFullHashLess)) {
1812 // The kill switch is whitelisted hence we whitelist all URLs. 1812 // The kill switch is whitelisted hence we whitelist all URLs.
1813 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); 1813 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id);
1814 } else { 1814 } else {
1815 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, 1815 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id,
1816 &new_whitelist); 1816 &new_whitelist);
1817 } 1817 }
1818 } 1818 }
1819 1819
1820 void SafeBrowsingDatabaseNew::LoadIpBlacklist( 1820 void SafeBrowsingDatabaseNew::LoadIpBlacklist(
1821 const std::vector<SBAddFullHash>& full_hashes) { 1821 const std::vector<SBAddFullHash>& full_hashes) {
1822 DCHECK(thread_checker_.CalledOnValidThread()); 1822 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1823 1823
1824 IPBlacklist new_blacklist; 1824 IPBlacklist new_blacklist;
1825 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); 1825 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin();
1826 it != full_hashes.end(); 1826 it != full_hashes.end();
1827 ++it) { 1827 ++it) {
1828 const char* full_hash = it->full_hash.full_hash; 1828 const char* full_hash = it->full_hash.full_hash;
1829 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash)); 1829 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash));
1830 // The format of the IP blacklist is: 1830 // The format of the IP blacklist is:
1831 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes. 1831 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes.
1832 std::string hashed_ip_prefix(full_hash, base::kSHA1Length); 1832 std::string hashed_ip_prefix(full_hash, base::kSHA1Length);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1918 else 1918 else
1919 NOTREACHED(); // Add support for new lists above. 1919 NOTREACHED(); // Add support for new lists above.
1920 1920
1921 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. 1921 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro.
1922 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( 1922 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet(
1923 histogram_name, 1, 1000000, 50, 1923 histogram_name, 1, 1000000, 50,
1924 base::HistogramBase::kUmaTargetedHistogramFlag); 1924 base::HistogramBase::kUmaTargetedHistogramFlag);
1925 1925
1926 histogram_pointer->Add(file_size_kilobytes); 1926 histogram_pointer->Add(file_size_kilobytes);
1927 } 1927 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698