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

Side by Side Diff: components/safe_browsing_db/v4_local_database_manager.cc

Issue 2616653002: Have a list of pending checks instead of pending clients (Closed)
Patch Set: Don't care about timing out of SB check for redirect loops. Created 3 years, 11 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 // This file should not be build on Android but is currently getting built. 5 // This file should not be build on Android but is currently getting built.
6 // TODO(vakh): Fix that: http://crbug.com/621647 6 // TODO(vakh): Fix that: http://crbug.com/621647
7 7
8 #include "components/safe_browsing_db/v4_local_database_manager.h" 8 #include "components/safe_browsing_db/v4_local_database_manager.h"
9 9
10 #include <vector> 10 #include <vector>
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 } 153 }
154 154
155 // 155 //
156 // Start: SafeBrowsingDatabaseManager implementation 156 // Start: SafeBrowsingDatabaseManager implementation
157 // 157 //
158 158
159 void V4LocalDatabaseManager::CancelCheck(Client* client) { 159 void V4LocalDatabaseManager::CancelCheck(Client* client) {
160 DCHECK_CURRENTLY_ON(BrowserThread::IO); 160 DCHECK_CURRENTLY_ON(BrowserThread::IO);
161 DCHECK(enabled_); 161 DCHECK(enabled_);
162 162
163 auto it = pending_clients_.find(client); 163 auto pending_it = std::find_if(
164 if (it != pending_clients_.end()) { 164 std::begin(pending_checks_), std::end(pending_checks_),
165 pending_clients_.erase(it); 165 [&client](const PendingCheck* check) { return check->client == client; });
166 if (pending_it != pending_checks_.end()) {
167 pending_checks_.erase(pending_it);
166 } 168 }
167 169
168 auto queued_it = 170 auto queued_it =
169 std::find_if(std::begin(queued_checks_), std::end(queued_checks_), 171 std::find_if(std::begin(queued_checks_), std::end(queued_checks_),
170 [&client](const std::unique_ptr<PendingCheck>& check) { 172 [&client](const std::unique_ptr<PendingCheck>& check) {
171 return check->client == client; 173 return check->client == client;
172 }); 174 });
173 if (queued_it != queued_checks_.end()) { 175 if (queued_it != queued_checks_.end()) {
174 queued_checks_.erase(queued_it); 176 queued_checks_.erase(queued_it);
175 } 177 }
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 SetupDatabase(); 370 SetupDatabase();
369 371
370 enabled_ = true; 372 enabled_ = true;
371 } 373 }
372 374
373 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { 375 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) {
374 DCHECK_CURRENTLY_ON(BrowserThread::IO); 376 DCHECK_CURRENTLY_ON(BrowserThread::IO);
375 377
376 enabled_ = false; 378 enabled_ = false;
377 379
378 pending_clients_.clear(); 380 pending_checks_.clear();
379 381
380 RespondSafeToQueuedChecks(); 382 RespondSafeToQueuedChecks();
381 383
382 // Delete the V4Database. Any pending writes to disk are completed. 384 // Delete the V4Database. Any pending writes to disk are completed.
383 // This operation happens on the task_runner on which v4_database_ operates 385 // This operation happens on the task_runner on which v4_database_ operates
384 // and doesn't block the IO thread. 386 // and doesn't block the IO thread.
385 V4Database::Destroy(std::move(v4_database_)); 387 V4Database::Destroy(std::move(v4_database_));
386 388
387 // Delete the V4UpdateProtocolManager. 389 // Delete the V4UpdateProtocolManager.
388 // This cancels any in-flight update request. 390 // This cancels any in-flight update request.
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 if (!v4_database_) { 529 if (!v4_database_) {
528 queued_checks_.push_back(std::move(check)); 530 queued_checks_.push_back(std::move(check));
529 return false; 531 return false;
530 } 532 }
531 533
532 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; 534 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
533 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) { 535 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) {
534 return true; 536 return true;
535 } 537 }
536 538
539 pending_checks_.insert(check.get());
537 // Post on the IO thread to enforce async behavior. 540 // Post on the IO thread to enforce async behavior.
538 pending_clients_.insert(check->client);
539 BrowserThread::PostTask( 541 BrowserThread::PostTask(
540 BrowserThread::IO, FROM_HERE, 542 BrowserThread::IO, FROM_HERE,
541 base::Bind(&V4LocalDatabaseManager::PerformFullHashCheck, this, 543 base::Bind(&V4LocalDatabaseManager::PerformFullHashCheck, this,
542 base::Passed(std::move(check)), 544 base::Passed(std::move(check)),
543 full_hash_to_store_and_hash_prefixes)); 545 full_hash_to_store_and_hash_prefixes));
544 546
545 return false; 547 return false;
546 } 548 }
547 549
548 bool V4LocalDatabaseManager::HandleHashSynchronously( 550 bool V4LocalDatabaseManager::HandleHashSynchronously(
(...skipping 16 matching lines...) Expand all
565 567
566 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>( 568 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>(
567 nullptr, ClientCallbackType::CHECK_OTHER, stores_to_check, 569 nullptr, ClientCallbackType::CHECK_OTHER, stores_to_check,
568 std::vector<GURL>(1, url)); 570 std::vector<GURL>(1, url));
569 571
570 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; 572 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
571 return GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes); 573 return GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes);
572 } 574 }
573 575
574 void V4LocalDatabaseManager::OnFullHashResponse( 576 void V4LocalDatabaseManager::OnFullHashResponse(
575 std::unique_ptr<PendingCheck> pending_check, 577 std::unique_ptr<PendingCheck> check,
576 const std::vector<FullHashInfo>& full_hash_infos) { 578 const std::vector<FullHashInfo>& full_hash_infos) {
577 DCHECK_CURRENTLY_ON(BrowserThread::IO); 579 DCHECK_CURRENTLY_ON(BrowserThread::IO);
578 580
579 if (!enabled_) { 581 if (!enabled_) {
580 DCHECK(pending_clients_.empty()); 582 DCHECK(pending_checks_.empty());
581 return; 583 return;
582 } 584 }
583 585
584 const auto it = pending_clients_.find(pending_check->client); 586 const auto it = pending_checks_.find(check.get());
585 if (it == pending_clients_.end()) { 587 if (it == pending_checks_.end()) {
586 // The check has since been cancelled. 588 // The check has since been cancelled.
587 return; 589 return;
588 } 590 }
589 591
590 // Find out the most severe threat, if any, to report to the client. 592 // Find out the most severe threat, if any, to report to the client.
591 GetSeverestThreatTypeAndMetadata(&pending_check->result_threat_type, 593 GetSeverestThreatTypeAndMetadata(&check->result_threat_type,
592 &pending_check->url_metadata, 594 &check->url_metadata, full_hash_infos);
593 full_hash_infos); 595 pending_checks_.erase(it);
594 pending_clients_.erase(it); 596 RespondToClient(std::move(check));
595 RespondToClient(std::move(pending_check));
596 } 597 }
597 598
598 void V4LocalDatabaseManager::PerformFullHashCheck( 599 void V4LocalDatabaseManager::PerformFullHashCheck(
599 std::unique_ptr<PendingCheck> check, 600 std::unique_ptr<PendingCheck> check,
600 const FullHashToStoreAndHashPrefixesMap& 601 const FullHashToStoreAndHashPrefixesMap&
601 full_hash_to_store_and_hash_prefixes) { 602 full_hash_to_store_and_hash_prefixes) {
602 DCHECK_CURRENTLY_ON(BrowserThread::IO); 603 DCHECK_CURRENTLY_ON(BrowserThread::IO);
603 604
604 DCHECK(enabled_); 605 DCHECK(enabled_);
605 DCHECK(!full_hash_to_store_and_hash_prefixes.empty()); 606 DCHECK(!full_hash_to_store_and_hash_prefixes.empty());
606 607
607 v4_get_hash_protocol_manager_->GetFullHashes( 608 v4_get_hash_protocol_manager_->GetFullHashes(
608 full_hash_to_store_and_hash_prefixes, 609 full_hash_to_store_and_hash_prefixes,
609 base::Bind(&V4LocalDatabaseManager::OnFullHashResponse, 610 base::Bind(&V4LocalDatabaseManager::OnFullHashResponse,
610 weak_factory_.GetWeakPtr(), base::Passed(std::move(check)))); 611 weak_factory_.GetWeakPtr(), base::Passed(std::move(check))));
611 } 612 }
612 613
613 void V4LocalDatabaseManager::ProcessQueuedChecks() { 614 void V4LocalDatabaseManager::ProcessQueuedChecks() {
614 DCHECK_CURRENTLY_ON(BrowserThread::IO); 615 DCHECK_CURRENTLY_ON(BrowserThread::IO);
615 616
616 // Steal the queue to protect against reentrant CancelCheck() calls. 617 // Steal the queue to protect against reentrant CancelCheck() calls.
617 QueuedChecks checks; 618 QueuedChecks checks;
618 checks.swap(queued_checks_); 619 checks.swap(queued_checks_);
619 620
620 for (auto& it : checks) { 621 for (auto& it : checks) {
621 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; 622 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
622 if (!GetPrefixMatches(it, &full_hash_to_store_and_hash_prefixes)) { 623 if (!GetPrefixMatches(it, &full_hash_to_store_and_hash_prefixes)) {
623 RespondToClient(std::move(it)); 624 RespondToClient(std::move(it));
624 } else { 625 } else {
625 pending_clients_.insert(it->client); 626 pending_checks_.insert(it.get());
626 PerformFullHashCheck(std::move(it), full_hash_to_store_and_hash_prefixes); 627 PerformFullHashCheck(std::move(it), full_hash_to_store_and_hash_prefixes);
627 } 628 }
628 } 629 }
629 } 630 }
630 631
631 void V4LocalDatabaseManager::RespondSafeToQueuedChecks() { 632 void V4LocalDatabaseManager::RespondSafeToQueuedChecks() {
632 DCHECK_CURRENTLY_ON(BrowserThread::IO); 633 DCHECK_CURRENTLY_ON(BrowserThread::IO);
633 634
634 // Steal the queue to protect against reentrant CancelCheck() calls. 635 // Steal the queue to protect against reentrant CancelCheck() calls.
635 QueuedChecks checks; 636 QueuedChecks checks;
636 checks.swap(queued_checks_); 637 checks.swap(queued_checks_);
637 638
638 for (std::unique_ptr<PendingCheck>& it : checks) { 639 for (std::unique_ptr<PendingCheck>& it : checks) {
639 RespondToClient(std::move(it)); 640 RespondToClient(std::move(it));
640 } 641 }
641 } 642 }
642 643
643 void V4LocalDatabaseManager::RespondToClient( 644 void V4LocalDatabaseManager::RespondToClient(
644 std::unique_ptr<PendingCheck> check) { 645 std::unique_ptr<PendingCheck> check) {
645 DCHECK(check.get()); 646 DCHECK(check.get());
646 647
647 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) { 648 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) {
648 DCHECK_EQ(1u, check->urls.size()); 649 DCHECK_EQ(1u, check->urls.size());
649 // TODO(vakh): Remove these CHECKs after fixing bugs 660293, 660359.
650 CHECK(check.get());
651 CHECK(check->client);
652 CHECK_LE(1u, check->urls.size());
653 CHECK(check->urls[0].is_valid());
654 check->client->OnCheckBrowseUrlResult( 650 check->client->OnCheckBrowseUrlResult(
655 check->urls[0], check->result_threat_type, check->url_metadata); 651 check->urls[0], check->result_threat_type, check->url_metadata);
656 } else if (check->client_callback_type == 652 } else if (check->client_callback_type ==
657 ClientCallbackType::CHECK_DOWNLOAD_URLS) { 653 ClientCallbackType::CHECK_DOWNLOAD_URLS) {
658 check->client->OnCheckDownloadUrlResult(check->urls, 654 check->client->OnCheckDownloadUrlResult(check->urls,
659 check->result_threat_type); 655 check->result_threat_type);
660 } else if (check->client_callback_type == 656 } else if (check->client_callback_type ==
661 ClientCallbackType::CHECK_EXTENSION_IDS) { 657 ClientCallbackType::CHECK_EXTENSION_IDS) {
662 const std::set<FullHash> extension_ids(check->full_hashes.begin(), 658 const std::set<FullHash> extension_ids(check->full_hashes.begin(),
663 check->full_hashes.end()); 659 check->full_hashes.end());
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 db_updated_callback_); 703 db_updated_callback_);
708 } 704 }
709 705
710 bool V4LocalDatabaseManager::AreStoresAvailableNow( 706 bool V4LocalDatabaseManager::AreStoresAvailableNow(
711 const StoresToCheck& stores_to_check) const { 707 const StoresToCheck& stores_to_check) const {
712 return enabled_ && v4_database_ && 708 return enabled_ && v4_database_ &&
713 v4_database_->AreStoresAvailable(stores_to_check); 709 v4_database_->AreStoresAvailable(stores_to_check);
714 } 710 }
715 711
716 } // namespace safe_browsing 712 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698