| OLD | NEW |
| 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/protocol_manager.h" | 5 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 193 |
| 194 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() {} | 194 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() {} |
| 195 | 195 |
| 196 // We can only have one update or chunk request outstanding, but there may be | 196 // We can only have one update or chunk request outstanding, but there may be |
| 197 // multiple GetHash requests pending since we don't want to serialize them and | 197 // multiple GetHash requests pending since we don't want to serialize them and |
| 198 // slow down the user. | 198 // slow down the user. |
| 199 void SafeBrowsingProtocolManager::GetFullHash( | 199 void SafeBrowsingProtocolManager::GetFullHash( |
| 200 const std::vector<SBPrefix>& prefixes, | 200 const std::vector<SBPrefix>& prefixes, |
| 201 FullHashCallback callback, | 201 FullHashCallback callback, |
| 202 bool is_download, | 202 bool is_download, |
| 203 bool is_extended_reporting) { | 203 ExtendedReportingLevel reporting_level) { |
| 204 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 204 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 205 // If we are in GetHash backoff, we need to check if we're past the next | 205 // If we are in GetHash backoff, we need to check if we're past the next |
| 206 // allowed time. If we are, we can proceed with the request. If not, we are | 206 // allowed time. If we are, we can proceed with the request. If not, we are |
| 207 // required to return empty results (i.e. treat the page as safe). | 207 // required to return empty results (i.e. treat the page as safe). |
| 208 if (gethash_error_count_ && Time::Now() <= next_gethash_time_) { | 208 if (gethash_error_count_ && Time::Now() <= next_gethash_time_) { |
| 209 RecordGetHashResult(is_download, GET_HASH_BACKOFF_ERROR); | 209 RecordGetHashResult(is_download, GET_HASH_BACKOFF_ERROR); |
| 210 std::vector<SBFullHashResult> full_hashes; | 210 std::vector<SBFullHashResult> full_hashes; |
| 211 callback.Run(full_hashes, base::TimeDelta()); | 211 callback.Run(full_hashes, base::TimeDelta()); |
| 212 return; | 212 return; |
| 213 } | 213 } |
| 214 GURL gethash_url = GetHashUrl(is_extended_reporting); | 214 GURL gethash_url = GetHashUrl(reporting_level); |
| 215 std::unique_ptr<net::URLFetcher> fetcher_ptr = net::URLFetcher::Create( | 215 std::unique_ptr<net::URLFetcher> fetcher_ptr = net::URLFetcher::Create( |
| 216 url_fetcher_id_++, gethash_url, net::URLFetcher::POST, this); | 216 url_fetcher_id_++, gethash_url, net::URLFetcher::POST, this); |
| 217 net::URLFetcher* fetcher = fetcher_ptr.get(); | 217 net::URLFetcher* fetcher = fetcher_ptr.get(); |
| 218 data_use_measurement::DataUseUserData::AttachToFetcher( | 218 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 219 fetcher, data_use_measurement::DataUseUserData::SAFE_BROWSING); | 219 fetcher, data_use_measurement::DataUseUserData::SAFE_BROWSING); |
| 220 hash_requests_[fetcher] = {std::move(fetcher_ptr), | 220 hash_requests_[fetcher] = {std::move(fetcher_ptr), |
| 221 FullHashDetails(callback, is_download)}; | 221 FullHashDetails(callback, is_download)}; |
| 222 | 222 |
| 223 const std::string get_hash = FormatGetHash(prefixes); | 223 const std::string get_hash = FormatGetHash(prefixes); |
| 224 | 224 |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 616 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
| 617 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 617 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 618 request_->SetRequestContext(request_context_getter_.get()); | 618 request_->SetRequestContext(request_context_getter_.get()); |
| 619 chunk_request_start_ = base::Time::Now(); | 619 chunk_request_start_ = base::Time::Now(); |
| 620 request_->Start(); | 620 request_->Start(); |
| 621 } | 621 } |
| 622 | 622 |
| 623 void SafeBrowsingProtocolManager::OnGetChunksComplete( | 623 void SafeBrowsingProtocolManager::OnGetChunksComplete( |
| 624 const std::vector<SBListChunkRanges>& lists, | 624 const std::vector<SBListChunkRanges>& lists, |
| 625 bool database_error, | 625 bool database_error, |
| 626 bool is_extended_reporting) { | 626 ExtendedReportingLevel extended_reporting_level) { |
| 627 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 627 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 628 DCHECK_EQ(request_type_, UPDATE_REQUEST); | 628 DCHECK_EQ(request_type_, UPDATE_REQUEST); |
| 629 DCHECK(update_list_data_.empty()); | 629 DCHECK(update_list_data_.empty()); |
| 630 if (database_error) { | 630 if (database_error) { |
| 631 // The update was not successful, but don't back off. | 631 // The update was not successful, but don't back off. |
| 632 UpdateFinished(false, false); | 632 UpdateFinished(false, false); |
| 633 return; | 633 return; |
| 634 } | 634 } |
| 635 | 635 |
| 636 // Format our stored chunks: | 636 // Format our stored chunks: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 655 } | 655 } |
| 656 if (!found_malware) { | 656 if (!found_malware) { |
| 657 update_list_data_.append(FormatList(SBListChunkRanges(kMalwareList))); | 657 update_list_data_.append(FormatList(SBListChunkRanges(kMalwareList))); |
| 658 } | 658 } |
| 659 | 659 |
| 660 // Large requests are (probably) a sign of database corruption. | 660 // Large requests are (probably) a sign of database corruption. |
| 661 // Record stats to inform decisions about whether to automate | 661 // Record stats to inform decisions about whether to automate |
| 662 // deletion of such databases. http://crbug.com/120219 | 662 // deletion of such databases. http://crbug.com/120219 |
| 663 UMA_HISTOGRAM_COUNTS("SB2.UpdateRequestSize", update_list_data_.size()); | 663 UMA_HISTOGRAM_COUNTS("SB2.UpdateRequestSize", update_list_data_.size()); |
| 664 | 664 |
| 665 GURL update_url = UpdateUrl(is_extended_reporting); | 665 GURL update_url = UpdateUrl(extended_reporting_level); |
| 666 request_ = net::URLFetcher::Create(url_fetcher_id_++, update_url, | 666 request_ = net::URLFetcher::Create(url_fetcher_id_++, update_url, |
| 667 net::URLFetcher::POST, this); | 667 net::URLFetcher::POST, this); |
| 668 data_use_measurement::DataUseUserData::AttachToFetcher( | 668 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 669 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 669 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
| 670 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 670 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 671 request_->SetRequestContext(request_context_getter_.get()); | 671 request_->SetRequestContext(request_context_getter_.get()); |
| 672 request_->SetUploadData("text/plain", update_list_data_); | 672 request_->SetUploadData("text/plain", update_list_data_); |
| 673 request_->Start(); | 673 request_->Start(); |
| 674 | 674 |
| 675 // Begin the update request timeout. | 675 // Begin the update request timeout. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 (static_cast<int>(backup_update_reason_) * 2) + update_success); | 729 (static_cast<int>(backup_update_reason_) * 2) + update_success); |
| 730 RecordUpdateResult(update_result); | 730 RecordUpdateResult(update_result); |
| 731 } | 731 } |
| 732 backup_update_reason_ = BACKUP_UPDATE_REASON_MAX; | 732 backup_update_reason_ = BACKUP_UPDATE_REASON_MAX; |
| 733 request_type_ = NO_REQUEST; | 733 request_type_ = NO_REQUEST; |
| 734 update_list_data_.clear(); | 734 update_list_data_.clear(); |
| 735 delegate_->UpdateFinished(success); | 735 delegate_->UpdateFinished(success); |
| 736 ScheduleNextUpdate(back_off); | 736 ScheduleNextUpdate(back_off); |
| 737 } | 737 } |
| 738 | 738 |
| 739 GURL SafeBrowsingProtocolManager::UpdateUrl(bool is_extended_reporting) const { | 739 GURL SafeBrowsingProtocolManager::UpdateUrl( |
| 740 ExtendedReportingLevel reporting_level) const { |
| 740 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 741 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( |
| 741 url_prefix_, "downloads", client_name_, version_, additional_query_, | 742 url_prefix_, "downloads", client_name_, version_, additional_query_, |
| 742 is_extended_reporting); | 743 reporting_level); |
| 743 return GURL(url); | 744 return GURL(url); |
| 744 } | 745 } |
| 745 | 746 |
| 746 GURL SafeBrowsingProtocolManager::BackupUpdateUrl( | 747 GURL SafeBrowsingProtocolManager::BackupUpdateUrl( |
| 747 BackupUpdateReason backup_update_reason) const { | 748 BackupUpdateReason backup_update_reason) const { |
| 748 DCHECK(backup_update_reason >= 0 && | 749 DCHECK(backup_update_reason >= 0 && |
| 749 backup_update_reason < BACKUP_UPDATE_REASON_MAX); | 750 backup_update_reason < BACKUP_UPDATE_REASON_MAX); |
| 750 DCHECK(!backup_url_prefixes_[backup_update_reason].empty()); | 751 DCHECK(!backup_url_prefixes_[backup_update_reason].empty()); |
| 751 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 752 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( |
| 752 backup_url_prefixes_[backup_update_reason], "downloads", client_name_, | 753 backup_url_prefixes_[backup_update_reason], "downloads", client_name_, |
| 753 version_, additional_query_); | 754 version_, additional_query_); |
| 754 return GURL(url); | 755 return GURL(url); |
| 755 } | 756 } |
| 756 | 757 |
| 757 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { | 758 GURL SafeBrowsingProtocolManager::GetHashUrl( |
| 759 ExtendedReportingLevel reporting_level) const { |
| 758 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 760 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( |
| 759 url_prefix_, "gethash", client_name_, version_, additional_query_, | 761 url_prefix_, "gethash", client_name_, version_, additional_query_, |
| 760 is_extended_reporting); | 762 reporting_level); |
| 761 return GURL(url); | 763 return GURL(url); |
| 762 } | 764 } |
| 763 | 765 |
| 764 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { | 766 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { |
| 765 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 767 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 766 std::string next_url; | 768 std::string next_url; |
| 767 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && | 769 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && |
| 768 !base::StartsWith(url, "https://", | 770 !base::StartsWith(url, "https://", |
| 769 base::CompareCase::INSENSITIVE_ASCII)) { | 771 base::CompareCase::INSENSITIVE_ASCII)) { |
| 770 // Use https if we updated via https, otherwise http (useful for testing). | 772 // Use https if we updated via https, otherwise http (useful for testing). |
| (...skipping 26 matching lines...) Expand all Loading... |
| 797 : callback(callback), is_download(is_download) {} | 799 : callback(callback), is_download(is_download) {} |
| 798 | 800 |
| 799 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( | 801 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( |
| 800 const FullHashDetails& other) = default; | 802 const FullHashDetails& other) = default; |
| 801 | 803 |
| 802 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} | 804 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} |
| 803 | 805 |
| 804 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} | 806 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} |
| 805 | 807 |
| 806 } // namespace safe_browsing | 808 } // namespace safe_browsing |
| OLD | NEW |