OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/metrics/sparse_histogram.h" |
| 9 #include "base/rand_util.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "net/base/escape.h" |
| 12 |
| 13 using base::Time; |
| 14 using base::TimeDelta; |
| 15 |
| 16 namespace safe_browsing { |
| 17 |
| 18 // The Safe Browsing V4 server URL prefix. |
| 19 const char kSbV4UrlPrefix[] = "https://safebrowsing.googleapis.com/v4"; |
| 20 |
| 21 // static |
| 22 // Backoff interval is MIN(((2^(n-1))*15 minutes) * (RAND + 1), 24 hours) where |
| 23 // n is the number of consecutive errors. |
| 24 base::TimeDelta V4ProtocolManagerUtil::GetNextBackOffInterval( |
| 25 size_t* error_count, |
| 26 size_t* multiplier) { |
| 27 DCHECK(multiplier && error_count); |
| 28 (*error_count)++; |
| 29 if (*error_count > 1 && *error_count < 9) { |
| 30 // With error count 9 and above we will hit the 24 hour max interval. |
| 31 // Cap the multiplier here to prevent integer overflow errors. |
| 32 *multiplier *= 2; |
| 33 } |
| 34 base::TimeDelta next = |
| 35 base::TimeDelta::FromMinutes(*multiplier * (1 + base::RandDouble()) * 15); |
| 36 |
| 37 base::TimeDelta day = base::TimeDelta::FromHours(24); |
| 38 |
| 39 if (next < day) |
| 40 return next; |
| 41 else |
| 42 return day; |
| 43 } |
| 44 |
| 45 // static |
| 46 void V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
| 47 const char* metric_name, |
| 48 const net::URLRequestStatus& status, |
| 49 int response_code) { |
| 50 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 51 metric_name, status.is_success() ? response_code : status.error()); |
| 52 } |
| 53 |
| 54 // static |
| 55 // The API hash call uses the pver4 Safe Browsing server. |
| 56 GURL V4ProtocolManagerUtil::GetRequestUrl( |
| 57 const std::string& request_base64, const std::string& method_name, |
| 58 const V4ProtocolConfig& config) { |
| 59 std::string url = |
| 60 ComposeUrl(kSbV4UrlPrefix, method_name, request_base64, |
| 61 config.client_name, config.version, config.key_param); |
| 62 return GURL(url); |
| 63 } |
| 64 |
| 65 // static |
| 66 std::string V4ProtocolManagerUtil::ComposeUrl( |
| 67 const std::string& prefix, |
| 68 const std::string& method, |
| 69 const std::string& request_base64, |
| 70 const std::string& client_id, |
| 71 const std::string& version, |
| 72 const std::string& key_param) { |
| 73 DCHECK(!prefix.empty() && !method.empty() && !client_id.empty() && |
| 74 !version.empty()); |
| 75 std::string url = |
| 76 base::StringPrintf("%s/%s/%s?alt=proto&client_id=%s&client_version=%s", |
| 77 prefix.c_str(), method.c_str(), request_base64.c_str(), |
| 78 client_id.c_str(), version.c_str()); |
| 79 if (!key_param.empty()) { |
| 80 base::StringAppendF(&url, "&key=%s", |
| 81 net::EscapeQueryParamValue(key_param, true).c_str()); |
| 82 } |
| 83 return url; |
| 84 } |
| 85 |
| 86 } // namespace safe_browsing |
OLD | NEW |