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 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" | 5 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 const V4ProtocolConfig& config) | 102 const V4ProtocolConfig& config) |
103 : gethash_error_count_(0), | 103 : gethash_error_count_(0), |
104 gethash_back_off_mult_(1), | 104 gethash_back_off_mult_(1), |
105 next_gethash_time_(Time::FromDoubleT(0)), | 105 next_gethash_time_(Time::FromDoubleT(0)), |
106 config_(config), | 106 config_(config), |
107 request_context_getter_(request_context_getter), | 107 request_context_getter_(request_context_getter), |
108 url_fetcher_id_(0) { | 108 url_fetcher_id_(0) { |
109 } | 109 } |
110 | 110 |
111 // static | 111 // static |
112 void V4GetHashProtocolManager::RecordGetHashResult(ResultType result_type) { | 112 void V4GetHashProtocolManager::RecordGetHashResult( |
| 113 OperationResultType result_type) { |
113 UMA_HISTOGRAM_ENUMERATION("SafeBrowsing.GetV4HashResult", result_type, | 114 UMA_HISTOGRAM_ENUMERATION("SafeBrowsing.GetV4HashResult", result_type, |
114 GET_HASH_RESULT_MAX); | 115 V4ProtocolManagerUtil::OPERATION_RESULT_TYPE_MAX); |
115 } | 116 } |
116 | 117 |
117 V4GetHashProtocolManager::~V4GetHashProtocolManager() { | 118 V4GetHashProtocolManager::~V4GetHashProtocolManager() { |
118 // Delete in-progress SafeBrowsing requests. | 119 // Delete in-progress SafeBrowsing requests. |
119 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), | 120 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), |
120 hash_requests_.end()); | 121 hash_requests_.end()); |
121 hash_requests_.clear(); | 122 hash_requests_.clear(); |
122 } | 123 } |
123 | 124 |
124 std::string V4GetHashProtocolManager::GetHashRequest( | 125 std::string V4GetHashProtocolManager::GetHashRequest( |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 const std::vector<PlatformType>& platforms, | 242 const std::vector<PlatformType>& platforms, |
242 ThreatType threat_type, | 243 ThreatType threat_type, |
243 FullHashCallback callback) { | 244 FullHashCallback callback) { |
244 DCHECK(CalledOnValidThread()); | 245 DCHECK(CalledOnValidThread()); |
245 // We need to wait the minimum waiting duration, and if we are in backoff, | 246 // We need to wait the minimum waiting duration, and if we are in backoff, |
246 // we need to check if we're past the next allowed time. If we are, we can | 247 // we need to check if we're past the next allowed time. If we are, we can |
247 // proceed with the request. If not, we are required to return empty results | 248 // proceed with the request. If not, we are required to return empty results |
248 // (i.e. treat the page as safe). | 249 // (i.e. treat the page as safe). |
249 if (Time::Now() <= next_gethash_time_) { | 250 if (Time::Now() <= next_gethash_time_) { |
250 if (gethash_error_count_) { | 251 if (gethash_error_count_) { |
251 RecordGetHashResult(GET_HASH_BACKOFF_ERROR); | 252 RecordGetHashResult(V4ProtocolManagerUtil::BACKOFF_ERROR); |
252 } else { | 253 } else { |
253 RecordGetHashResult(GET_HASH_MIN_WAIT_DURATION_ERROR); | 254 RecordGetHashResult(V4ProtocolManagerUtil::MIN_WAIT_DURATION_ERROR); |
254 } | 255 } |
255 std::vector<SBFullHashResult> full_hashes; | 256 std::vector<SBFullHashResult> full_hashes; |
256 callback.Run(full_hashes, base::TimeDelta()); | 257 callback.Run(full_hashes, base::TimeDelta()); |
257 return; | 258 return; |
258 } | 259 } |
259 | 260 |
260 std::string req_base64 = GetHashRequest(prefixes, platforms, threat_type); | 261 std::string req_base64 = GetHashRequest(prefixes, platforms, threat_type); |
261 GURL gethash_url = GetHashUrl(req_base64); | 262 GURL gethash_url = GetHashUrl(req_base64); |
262 | 263 |
263 net::URLFetcher* fetcher = | 264 net::URLFetcher* fetcher = |
(...skipping 30 matching lines...) Expand all Loading... |
294 | 295 |
295 int response_code = source->GetResponseCode(); | 296 int response_code = source->GetResponseCode(); |
296 net::URLRequestStatus status = source->GetStatus(); | 297 net::URLRequestStatus status = source->GetStatus(); |
297 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( | 298 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
298 kUmaV4HashResponseMetricName, status, response_code); | 299 kUmaV4HashResponseMetricName, status, response_code); |
299 | 300 |
300 const FullHashCallback& callback = it->second; | 301 const FullHashCallback& callback = it->second; |
301 std::vector<SBFullHashResult> full_hashes; | 302 std::vector<SBFullHashResult> full_hashes; |
302 base::TimeDelta negative_cache_duration; | 303 base::TimeDelta negative_cache_duration; |
303 if (status.is_success() && response_code == net::HTTP_OK) { | 304 if (status.is_success() && response_code == net::HTTP_OK) { |
304 RecordGetHashResult(GET_HASH_STATUS_200); | 305 RecordGetHashResult(V4ProtocolManagerUtil::STATUS_200); |
305 ResetGetHashErrors(); | 306 ResetGetHashErrors(); |
306 std::string data; | 307 std::string data; |
307 source->GetResponseAsString(&data); | 308 source->GetResponseAsString(&data); |
308 if (!ParseHashResponse(data, &full_hashes, &negative_cache_duration)) { | 309 if (!ParseHashResponse(data, &full_hashes, &negative_cache_duration)) { |
309 full_hashes.clear(); | 310 full_hashes.clear(); |
310 RecordGetHashResult(GET_HASH_PARSE_ERROR); | 311 RecordGetHashResult(V4ProtocolManagerUtil::PARSE_ERROR); |
311 } | 312 } |
312 } else { | 313 } else { |
313 HandleGetHashError(Time::Now()); | 314 HandleGetHashError(Time::Now()); |
314 | 315 |
315 DVLOG(1) << "SafeBrowsing GetEncodedFullHashes request for: " | 316 DVLOG(1) << "SafeBrowsing GetEncodedFullHashes request for: " |
316 << source->GetURL() << " failed with error: " << status.error() | 317 << source->GetURL() << " failed with error: " << status.error() |
317 << " and response code: " << response_code; | 318 << " and response code: " << response_code; |
318 | 319 |
319 if (status.status() == net::URLRequestStatus::FAILED) { | 320 if (status.status() == net::URLRequestStatus::FAILED) { |
320 RecordGetHashResult(GET_HASH_NETWORK_ERROR); | 321 RecordGetHashResult(V4ProtocolManagerUtil::NETWORK_ERROR); |
321 } else { | 322 } else { |
322 RecordGetHashResult(GET_HASH_HTTP_ERROR); | 323 RecordGetHashResult(V4ProtocolManagerUtil::HTTP_ERROR); |
323 } | 324 } |
324 } | 325 } |
325 | 326 |
326 // Invoke the callback with full_hashes, even if there was a parse error or | 327 // Invoke the callback with full_hashes, even if there was a parse error or |
327 // an error response code (in which case full_hashes will be empty). The | 328 // an error response code (in which case full_hashes will be empty). The |
328 // caller can't be blocked indefinitely. | 329 // caller can't be blocked indefinitely. |
329 callback.Run(full_hashes, negative_cache_duration); | 330 callback.Run(full_hashes, negative_cache_duration); |
330 | 331 |
331 hash_requests_.erase(it); | 332 hash_requests_.erase(it); |
332 } | 333 } |
333 | 334 |
334 void V4GetHashProtocolManager::HandleGetHashError(const Time& now) { | 335 void V4GetHashProtocolManager::HandleGetHashError(const Time& now) { |
335 DCHECK(CalledOnValidThread()); | 336 DCHECK(CalledOnValidThread()); |
336 base::TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval( | 337 base::TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval( |
337 &gethash_error_count_, &gethash_back_off_mult_); | 338 &gethash_error_count_, &gethash_back_off_mult_); |
338 next_gethash_time_ = now + next; | 339 next_gethash_time_ = now + next; |
339 } | 340 } |
340 | 341 |
341 GURL V4GetHashProtocolManager::GetHashUrl(const std::string& req_base64) const { | 342 GURL V4GetHashProtocolManager::GetHashUrl(const std::string& req_base64) const { |
342 return V4ProtocolManagerUtil::GetRequestUrl(req_base64, "encodedFullHashes", | 343 return V4ProtocolManagerUtil::GetRequestUrl(req_base64, "encodedFullHashes", |
343 config_); | 344 config_); |
344 } | 345 } |
345 | 346 |
346 | 347 |
347 } // namespace safe_browsing | 348 } // namespace safe_browsing |
OLD | NEW |