Chromium Code Reviews| 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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | |
| 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | |
| 7 | |
| 8 // A class that implements the stateless methods used by the GetHashUpdate and | |
| 9 // GetFullHash stubby calls made by Chrome using the SafeBrowsing V4 protocol. | |
| 10 | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "net/url_request/url_request_status.h" | |
| 15 #include "url/gurl.h" | |
| 16 | |
| 17 namespace { | |
|
Nathan Parker
2016/02/22 22:39:32
Do you want this in an anonymous namespace in the
vakh (use Gerrit instead)
2016/02/23 01:39:36
Done.
| |
| 18 | |
| 19 // Enumerate parsing failures for histogramming purposes. DO NOT CHANGE | |
| 20 // THE ORDERING OF THESE VALUES. | |
| 21 enum ParseResultType { | |
|
Nathan Parker
2016/02/22 22:39:32
Is this the union of Full-Hash checks and update c
vakh (use Gerrit instead)
2016/02/23 01:39:36
At first I thought both modules could share this,
| |
| 22 // Error parsing the protocol buffer from a string. | |
| 23 PARSE_FROM_STRING_ERROR = 0, | |
| 24 | |
| 25 // A match in the response had an unexpected THREAT_ENTRY_TYPE. | |
| 26 UNEXPECTED_THREAT_ENTRY_TYPE_ERROR = 1, | |
| 27 | |
| 28 // A match in the response had an unexpected THREAT_TYPE. | |
| 29 UNEXPECTED_THREAT_TYPE_ERROR = 2, | |
| 30 | |
| 31 // A match in the response had an unexpected PLATFORM_TYPE. | |
| 32 UNEXPECTED_PLATFORM_TYPE_ERROR = 3, | |
| 33 | |
| 34 // A match in the response contained no metadata where metadata was | |
| 35 // expected. | |
| 36 NO_METADATA_ERROR = 4, | |
| 37 | |
| 38 // A match in the response contained a ThreatType that was inconsistent | |
| 39 // with the other matches. | |
| 40 INCONSISTENT_THREAT_TYPE_ERROR = 5, | |
| 41 | |
| 42 // Memory space for histograms is determined by the max. ALWAYS | |
| 43 // ADD NEW VALUES BEFORE THIS ONE. | |
| 44 PARSE_RESULT_TYPE_MAX = 6 | |
| 45 }; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 namespace safe_browsing { | |
| 50 | |
| 51 // Safe Browsing V4 server URL prefix. | |
| 52 extern const char kSbV4UrlPrefix[]; | |
|
Nathan Parker
2016/02/22 22:39:32
If this isn't needed by anyone other that the v4_p
vakh (use Gerrit instead)
2016/02/23 01:39:36
Done.
| |
| 53 | |
| 54 // Config passed to the constructor of a V4 protocol manager. | |
| 55 struct V4ProtocolConfig { | |
| 56 // The safe browsing client name sent in each request. | |
| 57 std::string client_name; | |
| 58 | |
| 59 // Current product version sent in each request. | |
| 60 std::string version; | |
| 61 | |
| 62 // The Google API key. | |
| 63 std::string key_param; | |
| 64 }; | |
| 65 | |
| 66 class V4ProtocolManagerUtil { | |
| 67 public: | |
| 68 ~V4ProtocolManagerUtil() {}; | |
|
Nathan Parker
2016/02/22 22:39:32
Probably don't need this. And you could make the
vakh (use Gerrit instead)
2016/02/23 01:39:36
Done.
| |
| 69 | |
| 70 // Record HTTP response code when there's no error in fetching an HTTP | |
| 71 // request, and the error code, when there is. | |
| 72 // |metric_name| is the name of the UMA metric to record the response code or | |
| 73 // error code against, |status| represents the status of the HTTP request, and | |
| 74 // |response code| represents the HTTP response code received from the server. | |
| 75 static void RecordHttpResponseOrErrorCode(const char* metric_name, | |
| 76 const net::URLRequestStatus& status, | |
| 77 int response_code); | |
| 78 | |
| 79 // Generates a Pver4 request URL. | |
| 80 // |request_base64| is the serialized request protocol buffer encoded in | |
| 81 // base 64. | |
| 82 // |method_name| is the name of the method to call, as specified in the proto, | |
| 83 // |config| is an instance of V4ProtocolConfig that stores the client config. | |
| 84 static GURL GetRequestUrl(const std::string& request_base64, | |
| 85 const std::string& method_name, | |
| 86 const V4ProtocolConfig& config); | |
| 87 | |
| 88 // Worker function for calculating the GetHash backoff times. | |
| 89 // |multiplier| is doubled for each consecutive error after the | |
| 90 // first, and |error_count| is incremented with each call. | |
| 91 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, | |
|
kcarattini
2016/02/23 00:49:13
If this is specific to GetHash then why put it in
vakh (use Gerrit instead)
2016/02/23 01:39:36
Done. Updated the comment.
Backoff is used by both
| |
| 92 size_t* multiplier); | |
| 93 | |
| 94 private: | |
| 95 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, | |
| 96 TestBackOffLogic); | |
| 97 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, | |
| 98 TestGetRequestUrl); | |
| 99 | |
| 100 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). | |
| 101 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| | |
| 102 // should contain the entire url prefix including scheme, host and path. | |
| 103 static std::string ComposeUrl(const std::string& prefix, | |
| 104 const std::string& method, | |
| 105 const std::string& request_base64, | |
| 106 const std::string& client_id, | |
| 107 const std::string& version, | |
| 108 const std::string& key_param); | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); | |
| 111 }; | |
| 112 | |
| 113 } // namespace safe_browsing | |
| 114 | |
| 115 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | |
| OLD | NEW |