| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 UPDATE_RESULT_BACKUP_NETWORK_SUCCESS, | 52 UPDATE_RESULT_BACKUP_NETWORK_SUCCESS, |
| 53 UPDATE_RESULT_MAX, | 53 UPDATE_RESULT_MAX, |
| 54 UPDATE_RESULT_BACKUP_START = UPDATE_RESULT_BACKUP_CONNECT_FAIL, | 54 UPDATE_RESULT_BACKUP_START = UPDATE_RESULT_BACKUP_CONNECT_FAIL, |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 void RecordUpdateResult(UpdateResult result) { | 57 void RecordUpdateResult(UpdateResult result) { |
| 58 DCHECK(result >= 0 && result < UPDATE_RESULT_MAX); | 58 DCHECK(result >= 0 && result < UPDATE_RESULT_MAX); |
| 59 UMA_HISTOGRAM_ENUMERATION("SB2.UpdateResult", result, UPDATE_RESULT_MAX); | 59 UMA_HISTOGRAM_ENUMERATION("SB2.UpdateResult", result, UPDATE_RESULT_MAX); |
| 60 } | 60 } |
| 61 | 61 |
| 62 const char kSBUpdateFrequencyFinchExperiment[] = "SafeBrowsingUpdateFrequency"; | 62 constexpr char kSBUpdateFrequencyFinchExperiment[] = |
| 63 const char kSBUpdateFrequencyFinchParam[] = "NextUpdateIntervalInMinutes"; | 63 "SafeBrowsingUpdateFrequency"; |
| 64 constexpr char kSBUpdateFrequencyFinchParam[] = "NextUpdateIntervalInMinutes"; |
| 64 | 65 |
| 65 // This will be used for experimenting on a small subset of the population to | 66 // This will be used for experimenting on a small subset of the population to |
| 66 // better estimate the benefit of updating the safe browsing hashes more | 67 // better estimate the benefit of updating the safe browsing hashes more |
| 67 // frequently. | 68 // frequently. |
| 68 base::TimeDelta GetNextUpdateIntervalFromFinch() { | 69 base::TimeDelta GetNextUpdateIntervalFromFinch() { |
| 69 std::string num_str = variations::GetVariationParamValue( | 70 std::string num_str = variations::GetVariationParamValue( |
| 70 kSBUpdateFrequencyFinchExperiment, kSBUpdateFrequencyFinchParam); | 71 kSBUpdateFrequencyFinchExperiment, kSBUpdateFrequencyFinchParam); |
| 71 int finch_next_update_interval_minutes = 0; | 72 int finch_next_update_interval_minutes = 0; |
| 72 if (!base::StringToInt(num_str, &finch_next_update_interval_minutes)) { | 73 if (!base::StringToInt(num_str, &finch_next_update_interval_minutes)) { |
| 73 finch_next_update_interval_minutes = 0; // Defaults to 0. | 74 finch_next_update_interval_minutes = 0; // Defaults to 0. |
| 74 } | 75 } |
| 75 return base::TimeDelta::FromMinutes(finch_next_update_interval_minutes); | 76 return base::TimeDelta::FromMinutes(finch_next_update_interval_minutes); |
| 76 } | 77 } |
| 77 | 78 |
| 78 } // namespace | 79 } // namespace |
| 79 | 80 |
| 80 namespace safe_browsing { | 81 namespace safe_browsing { |
| 81 | 82 |
| 82 // Minimum time, in seconds, from start up before we must issue an update query. | 83 // Minimum time, in seconds, from start up before we must issue an update query. |
| 83 static const int kSbTimerStartIntervalSecMin = 60; | 84 constexpr int kSbTimerStartIntervalSecMin = 60; |
| 84 | 85 |
| 85 // Maximum time, in seconds, from start up before we must issue an update query. | 86 // Maximum time, in seconds, from start up before we must issue an update query. |
| 86 static const int kSbTimerStartIntervalSecMax = 300; | 87 constexpr int kSbTimerStartIntervalSecMax = 300; |
| 87 | 88 |
| 88 // The maximum time, in seconds, to wait for a response to an update request. | 89 // The maximum time to wait for a response to an update request. |
| 89 static const int kSbMaxUpdateWaitSec = 30; | 90 constexpr base::TimeDelta kSbMaxUpdateWait = base::TimeDelta::FromSeconds(30); |
| 90 | 91 |
| 91 // Maximum back off multiplier. | 92 // Maximum back off multiplier. |
| 92 static const size_t kSbMaxBackOff = 8; | 93 constexpr size_t kSbMaxBackOff = 8; |
| 93 | 94 |
| 94 const char kGetHashUmaResponseMetricName[] = "SB2.GetHashResponseOrErrorCode"; | 95 constexpr char kGetHashUmaResponseMetricName[] = |
| 95 const char kGetChunkUmaResponseMetricName[] = "SB2.GetChunkResponseOrErrorCode"; | 96 "SB2.GetHashResponseOrErrorCode"; |
| 97 constexpr char kGetChunkUmaResponseMetricName[] = |
| 98 "SB2.GetChunkResponseOrErrorCode"; |
| 96 | 99 |
| 97 // The default SBProtocolManagerFactory. | 100 // The default SBProtocolManagerFactory. |
| 98 class SBProtocolManagerFactoryImpl : public SBProtocolManagerFactory { | 101 class SBProtocolManagerFactoryImpl : public SBProtocolManagerFactory { |
| 99 public: | 102 public: |
| 100 SBProtocolManagerFactoryImpl() {} | 103 SBProtocolManagerFactoryImpl() {} |
| 101 ~SBProtocolManagerFactoryImpl() override {} | 104 ~SBProtocolManagerFactoryImpl() override {} |
| 102 | 105 |
| 103 std::unique_ptr<SafeBrowsingProtocolManager> CreateProtocolManager( | 106 std::unique_ptr<SafeBrowsingProtocolManager> CreateProtocolManager( |
| 104 SafeBrowsingProtocolManagerDelegate* delegate, | 107 SafeBrowsingProtocolManagerDelegate* delegate, |
| 105 net::URLRequestContextGetter* request_context_getter, | 108 net::URLRequestContextGetter* request_context_getter, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 const net::URLRequestStatus& status, | 186 const net::URLRequestStatus& status, |
| 184 int response_code) { | 187 int response_code) { |
| 185 UMA_HISTOGRAM_SPARSE_SLOWLY( | 188 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 186 metric_name, status.is_success() ? response_code : status.error()); | 189 metric_name, status.is_success() ? response_code : status.error()); |
| 187 } | 190 } |
| 188 | 191 |
| 189 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { | 192 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { |
| 190 return update_timer_.IsRunning(); | 193 return update_timer_.IsRunning(); |
| 191 } | 194 } |
| 192 | 195 |
| 196 // static |
| 197 base::TimeDelta SafeBrowsingProtocolManager::GetUpdateTimeoutForTesting() { |
| 198 return kSbMaxUpdateWait; |
| 199 } |
| 200 |
| 193 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() {} | 201 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() {} |
| 194 | 202 |
| 195 // We can only have one update or chunk request outstanding, but there may be | 203 // We can only have one update or chunk request outstanding, but there may be |
| 196 // multiple GetHash requests pending since we don't want to serialize them and | 204 // multiple GetHash requests pending since we don't want to serialize them and |
| 197 // slow down the user. | 205 // slow down the user. |
| 198 void SafeBrowsingProtocolManager::GetFullHash( | 206 void SafeBrowsingProtocolManager::GetFullHash( |
| 199 const std::vector<SBPrefix>& prefixes, | 207 const std::vector<SBPrefix>& prefixes, |
| 200 FullHashCallback callback, | 208 FullHashCallback callback, |
| 201 bool is_download, | 209 bool is_download, |
| 202 ExtendedReportingLevel reporting_level) { | 210 ExtendedReportingLevel reporting_level) { |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 request_ = net::URLFetcher::Create(url_fetcher_id_++, backup_update_url, | 591 request_ = net::URLFetcher::Create(url_fetcher_id_++, backup_update_url, |
| 584 net::URLFetcher::POST, this); | 592 net::URLFetcher::POST, this); |
| 585 data_use_measurement::DataUseUserData::AttachToFetcher( | 593 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 586 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 594 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
| 587 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 595 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 588 request_->SetRequestContext(request_context_getter_.get()); | 596 request_->SetRequestContext(request_context_getter_.get()); |
| 589 request_->SetUploadData("text/plain", update_list_data_); | 597 request_->SetUploadData("text/plain", update_list_data_); |
| 590 request_->Start(); | 598 request_->Start(); |
| 591 | 599 |
| 592 // Begin the update request timeout. | 600 // Begin the update request timeout. |
| 593 timeout_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), | 601 timeout_timer_.Start(FROM_HERE, kSbMaxUpdateWait, this, |
| 594 this, | |
| 595 &SafeBrowsingProtocolManager::UpdateResponseTimeout); | 602 &SafeBrowsingProtocolManager::UpdateResponseTimeout); |
| 596 | 603 |
| 597 return true; | 604 return true; |
| 598 } | 605 } |
| 599 | 606 |
| 600 void SafeBrowsingProtocolManager::IssueChunkRequest() { | 607 void SafeBrowsingProtocolManager::IssueChunkRequest() { |
| 601 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 608 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 602 // We are only allowed to have one request outstanding at any time. Also, | 609 // We are only allowed to have one request outstanding at any time. Also, |
| 603 // don't get the next url until the previous one has been written to disk so | 610 // don't get the next url until the previous one has been written to disk so |
| 604 // that we don't use too much memory. | 611 // that we don't use too much memory. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 request_ = net::URLFetcher::Create(url_fetcher_id_++, update_url, | 672 request_ = net::URLFetcher::Create(url_fetcher_id_++, update_url, |
| 666 net::URLFetcher::POST, this); | 673 net::URLFetcher::POST, this); |
| 667 data_use_measurement::DataUseUserData::AttachToFetcher( | 674 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 668 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 675 request_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
| 669 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 676 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 670 request_->SetRequestContext(request_context_getter_.get()); | 677 request_->SetRequestContext(request_context_getter_.get()); |
| 671 request_->SetUploadData("text/plain", update_list_data_); | 678 request_->SetUploadData("text/plain", update_list_data_); |
| 672 request_->Start(); | 679 request_->Start(); |
| 673 | 680 |
| 674 // Begin the update request timeout. | 681 // Begin the update request timeout. |
| 675 timeout_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), | 682 timeout_timer_.Start(FROM_HERE, kSbMaxUpdateWait, this, |
| 676 this, | |
| 677 &SafeBrowsingProtocolManager::UpdateResponseTimeout); | 683 &SafeBrowsingProtocolManager::UpdateResponseTimeout); |
| 678 } | 684 } |
| 679 | 685 |
| 680 // If we haven't heard back from the server with an update response, this method | 686 // If we haven't heard back from the server with an update response, this method |
| 681 // will run. Close the current update session and schedule another update. | 687 // will run. Close the current update session and schedule another update. |
| 682 void SafeBrowsingProtocolManager::UpdateResponseTimeout() { | 688 void SafeBrowsingProtocolManager::UpdateResponseTimeout() { |
| 683 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 689 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 684 DCHECK(request_type_ == UPDATE_REQUEST || | 690 DCHECK(request_type_ == UPDATE_REQUEST || |
| 685 request_type_ == BACKUP_UPDATE_REQUEST); | 691 request_type_ == BACKUP_UPDATE_REQUEST); |
| 686 request_.reset(); | 692 request_.reset(); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 : callback(callback), is_download(is_download) {} | 804 : callback(callback), is_download(is_download) {} |
| 799 | 805 |
| 800 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( | 806 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( |
| 801 const FullHashDetails& other) = default; | 807 const FullHashDetails& other) = default; |
| 802 | 808 |
| 803 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} | 809 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} |
| 804 | 810 |
| 805 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} | 811 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} |
| 806 | 812 |
| 807 } // namespace safe_browsing | 813 } // namespace safe_browsing |
| OLD | NEW |