Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
| 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
| 7 | 7 |
| 8 // A class that implements the stateless methods used by the GetHashUpdate and | 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. | 9 // GetFullHash stubby calls made by Chrome using the SafeBrowsing V4 protocol. |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/hash.h" | |
| 15 #include "components/safe_browsing_db/safebrowsing.pb.h" | |
| 14 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
| 15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 16 | 18 |
| 17 namespace safe_browsing { | 19 namespace safe_browsing { |
| 18 | |
| 19 // Config passed to the constructor of a V4 protocol manager. | 20 // Config passed to the constructor of a V4 protocol manager. |
| 20 struct V4ProtocolConfig { | 21 struct V4ProtocolConfig { |
| 21 // The safe browsing client name sent in each request. | 22 // The safe browsing client name sent in each request. |
| 22 std::string client_name; | 23 std::string client_name; |
| 23 | 24 |
| 24 // Current product version sent in each request. | 25 // Current product version sent in each request. |
| 25 std::string version; | 26 std::string version; |
| 26 | 27 |
| 27 // The Google API key. | 28 // The Google API key. |
| 28 std::string key_param; | 29 std::string key_param; |
| 29 }; | 30 }; |
| 30 | 31 |
| 32 // The information required to uniquely identify each list the client is | |
| 33 // interested in maintaining and downloading from the SafeBrowsing servers. | |
| 34 // For example, for digests of Malware binaries on Windows: | |
| 35 // platform_type = WINDOWS, | |
| 36 // threat_entry_type = BINARY_DIGEST, | |
| 37 // threat_type = MALWARE | |
| 38 struct UpdateListIdentifier { | |
| 39 PlatformType platform_type; | |
| 40 ThreatEntryType threat_entry_type; | |
| 41 ThreatType threat_type; | |
| 42 | |
| 43 bool operator==(const UpdateListIdentifier& other) const { | |
| 44 return platform_type == other.platform_type && | |
|
Nathan Parker
2016/03/25 23:51:01
The body of these should probably go in a .cc file
vakh (use Gerrit instead)
2016/03/26 00:06:56
Done.
| |
| 45 threat_entry_type == other.threat_entry_type && | |
| 46 threat_type == other.threat_type; | |
| 47 } | |
| 48 | |
| 49 bool operator!=(const UpdateListIdentifier& other) const { | |
| 50 return !operator==(other); | |
| 51 } | |
| 52 | |
| 53 size_t hash() const { | |
| 54 std::size_t first = std::hash<unsigned int>()(platform_type); | |
| 55 std::size_t second = std::hash<unsigned int>()(threat_entry_type); | |
| 56 std::size_t third = std::hash<unsigned int>()(threat_type); | |
| 57 | |
| 58 std::size_t interim = base::HashInts(first, second); | |
| 59 return base::HashInts(interim, third); | |
| 60 } | |
| 61 }; | |
| 62 | |
| 63 // Enumerate failures for histogramming purposes. DO NOT CHANGE THE | |
| 64 // ORDERING OF THESE VALUES. | |
| 65 enum V4OperationResult { | |
| 66 // 200 response code means that the server recognized the request. | |
| 67 STATUS_200 = 0, | |
| 68 | |
| 69 // Subset of successful responses where the response body wasn't parsable. | |
| 70 PARSE_ERROR = 1, | |
| 71 | |
| 72 // Operation request failed (network error). | |
| 73 NETWORK_ERROR = 2, | |
| 74 | |
| 75 // Operation request returned HTTP result code other than 200. | |
| 76 HTTP_ERROR = 3, | |
| 77 | |
| 78 // Operation attempted during error backoff, no request sent. | |
| 79 BACKOFF_ERROR = 4, | |
| 80 | |
| 81 // Operation attempted before min wait duration elapsed, no request sent. | |
| 82 MIN_WAIT_DURATION_ERROR = 5, | |
| 83 | |
| 84 // Identical operation already pending. | |
| 85 ALREADY_PENDING_ERROR = 6, | |
| 86 | |
| 87 // Memory space for histograms is determined by the max. ALWAYS | |
| 88 // ADD NEW VALUES BEFORE THIS ONE. | |
| 89 OPERATION_RESULT_MAX = 7 | |
| 90 }; | |
| 91 | |
| 92 // A class that provides static methods related to the Pver4 protocol. | |
| 31 class V4ProtocolManagerUtil { | 93 class V4ProtocolManagerUtil { |
| 32 public: | 94 public: |
| 33 // Record HTTP response code when there's no error in fetching an HTTP | 95 // Record HTTP response code when there's no error in fetching an HTTP |
| 34 // request, and the error code, when there is. | 96 // request, and the error code, when there is. |
| 35 // |metric_name| is the name of the UMA metric to record the response code or | 97 // |metric_name| is the name of the UMA metric to record the response code or |
| 36 // error code against, |status| represents the status of the HTTP request, and | 98 // error code against, |status| represents the status of the HTTP request, and |
| 37 // |response code| represents the HTTP response code received from the server. | 99 // |response code| represents the HTTP response code received from the server. |
| 38 static void RecordHttpResponseOrErrorCode(const char* metric_name, | 100 static void RecordHttpResponseOrErrorCode(const char* metric_name, |
| 39 const net::URLRequestStatus& status, | 101 const net::URLRequestStatus& status, |
| 40 int response_code); | 102 int response_code); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 69 const std::string& request_base64, | 131 const std::string& request_base64, |
| 70 const std::string& client_id, | 132 const std::string& client_id, |
| 71 const std::string& version, | 133 const std::string& version, |
| 72 const std::string& key_param); | 134 const std::string& key_param); |
| 73 | 135 |
| 74 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); | 136 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); |
| 75 }; | 137 }; |
| 76 | 138 |
| 77 } // namespace safe_browsing | 139 } // namespace safe_browsing |
| 78 | 140 |
| 141 namespace std { | |
| 142 template <> | |
| 143 struct hash<safe_browsing::UpdateListIdentifier> { | |
| 144 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { | |
|
Nathan Parker
2016/03/25 23:51:01
Cool! I'm glad you figured out how to do this. I'
vakh (use Gerrit instead)
2016/03/26 00:06:56
Acknowledged.
| |
| 145 return s.hash(); | |
| 146 } | |
| 147 }; | |
| 148 } | |
| 149 | |
| 79 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 150 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
| OLD | NEW |