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/base64url.h" | 9 #include "base/base64url.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
12 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
13 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
15 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
16 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
17 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
18 | 19 |
19 using base::Time; | 20 using base::Time; |
20 using base::TimeDelta; | 21 using base::TimeDelta; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 const V4ProtocolConfig& config) | 113 const V4ProtocolConfig& config) |
113 : gethash_error_count_(0), | 114 : gethash_error_count_(0), |
114 gethash_back_off_mult_(1), | 115 gethash_back_off_mult_(1), |
115 next_gethash_time_(Time::FromDoubleT(0)), | 116 next_gethash_time_(Time::FromDoubleT(0)), |
116 config_(config), | 117 config_(config), |
117 request_context_getter_(request_context_getter), | 118 request_context_getter_(request_context_getter), |
118 url_fetcher_id_(0), | 119 url_fetcher_id_(0), |
119 clock_(new base::DefaultClock()) {} | 120 clock_(new base::DefaultClock()) {} |
120 | 121 |
121 V4GetHashProtocolManager::~V4GetHashProtocolManager() { | 122 V4GetHashProtocolManager::~V4GetHashProtocolManager() { |
122 // Delete in-progress SafeBrowsing requests. | |
123 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), | |
124 hash_requests_.end()); | |
125 hash_requests_.clear(); | |
126 } | 123 } |
127 | 124 |
128 // static | 125 // static |
129 void V4GetHashProtocolManager::RegisterFactory( | 126 void V4GetHashProtocolManager::RegisterFactory( |
130 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) { | 127 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) { |
131 if (factory_) | 128 if (factory_) |
132 delete factory_; | 129 delete factory_; |
133 factory_ = factory.release(); | 130 factory_ = factory.release(); |
134 } | 131 } |
135 | 132 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 std::vector<SBFullHashResult> full_hashes; | 313 std::vector<SBFullHashResult> full_hashes; |
317 callback.Run(full_hashes, base::Time()); | 314 callback.Run(full_hashes, base::Time()); |
318 return; | 315 return; |
319 } | 316 } |
320 | 317 |
321 std::string req_base64 = GetHashRequest(prefixes, platforms, threat_type); | 318 std::string req_base64 = GetHashRequest(prefixes, platforms, threat_type); |
322 GURL gethash_url; | 319 GURL gethash_url; |
323 net::HttpRequestHeaders headers; | 320 net::HttpRequestHeaders headers; |
324 GetHashUrlAndHeaders(req_base64, &gethash_url, &headers); | 321 GetHashUrlAndHeaders(req_base64, &gethash_url, &headers); |
325 | 322 |
326 net::URLFetcher* fetcher = | 323 std::unique_ptr<net::URLFetcher> owned_fetcher = net::URLFetcher::Create( |
327 net::URLFetcher::Create(url_fetcher_id_++, gethash_url, | 324 url_fetcher_id_++, gethash_url, net::URLFetcher::GET, this); |
328 net::URLFetcher::GET, this) | 325 net::URLFetcher* fetcher = owned_fetcher.get(); |
329 .release(); | |
330 fetcher->SetExtraRequestHeaders(headers.ToString()); | 326 fetcher->SetExtraRequestHeaders(headers.ToString()); |
331 hash_requests_[fetcher] = callback; | 327 hash_requests_[fetcher] = std::make_pair(std::move(owned_fetcher), callback); |
332 | 328 |
333 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 329 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
334 fetcher->SetRequestContext(request_context_getter_.get()); | 330 fetcher->SetRequestContext(request_context_getter_.get()); |
335 fetcher->Start(); | 331 fetcher->Start(); |
336 } | 332 } |
337 | 333 |
338 void V4GetHashProtocolManager::GetFullHashesWithApis( | 334 void V4GetHashProtocolManager::GetFullHashesWithApis( |
339 const std::vector<SBPrefix>& prefixes, | 335 const std::vector<SBPrefix>& prefixes, |
340 FullHashCallback callback) { | 336 FullHashCallback callback) { |
341 std::vector<PlatformType> platform = {CHROME_PLATFORM}; | 337 std::vector<PlatformType> platform = {CHROME_PLATFORM}; |
342 GetFullHashes(prefixes, platform, API_ABUSE, callback); | 338 GetFullHashes(prefixes, platform, API_ABUSE, callback); |
343 } | 339 } |
344 | 340 |
345 void V4GetHashProtocolManager::SetClockForTests( | 341 void V4GetHashProtocolManager::SetClockForTests( |
346 std::unique_ptr<base::Clock> clock) { | 342 std::unique_ptr<base::Clock> clock) { |
347 clock_ = std::move(clock); | 343 clock_ = std::move(clock); |
348 } | 344 } |
349 | 345 |
350 // net::URLFetcherDelegate implementation ---------------------------------- | 346 // net::URLFetcherDelegate implementation ---------------------------------- |
351 | 347 |
352 // SafeBrowsing request responses are handled here. | 348 // SafeBrowsing request responses are handled here. |
353 void V4GetHashProtocolManager::OnURLFetchComplete( | 349 void V4GetHashProtocolManager::OnURLFetchComplete( |
354 const net::URLFetcher* source) { | 350 const net::URLFetcher* source) { |
355 DCHECK(CalledOnValidThread()); | 351 DCHECK(CalledOnValidThread()); |
356 | 352 |
357 HashRequests::iterator it = hash_requests_.find(source); | 353 HashRequests::iterator it = hash_requests_.find(source); |
358 DCHECK(it != hash_requests_.end()) << "Request not found"; | 354 DCHECK(it != hash_requests_.end()) << "Request not found"; |
359 | 355 |
360 // FindFullHashes response. | |
361 // Reset the scoped pointer so the fetcher gets destroyed properly. | |
362 std::unique_ptr<const net::URLFetcher> fetcher(it->first); | |
363 | |
364 int response_code = source->GetResponseCode(); | 356 int response_code = source->GetResponseCode(); |
365 net::URLRequestStatus status = source->GetStatus(); | 357 net::URLRequestStatus status = source->GetStatus(); |
366 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( | 358 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
367 kUmaV4HashResponseMetricName, status, response_code); | 359 kUmaV4HashResponseMetricName, status, response_code); |
368 | 360 |
369 const FullHashCallback& callback = it->second; | 361 const FullHashCallback& callback = it->second.second; |
370 std::vector<SBFullHashResult> full_hashes; | 362 std::vector<SBFullHashResult> full_hashes; |
371 base::Time negative_cache_expire; | 363 base::Time negative_cache_expire; |
372 if (status.is_success() && response_code == net::HTTP_OK) { | 364 if (status.is_success() && response_code == net::HTTP_OK) { |
373 RecordGetHashResult(V4OperationResult::STATUS_200); | 365 RecordGetHashResult(V4OperationResult::STATUS_200); |
374 ResetGetHashErrors(); | 366 ResetGetHashErrors(); |
375 std::string data; | 367 std::string data; |
376 source->GetResponseAsString(&data); | 368 source->GetResponseAsString(&data); |
377 if (!ParseHashResponse(data, &full_hashes, &negative_cache_expire)) { | 369 if (!ParseHashResponse(data, &full_hashes, &negative_cache_expire)) { |
378 full_hashes.clear(); | 370 full_hashes.clear(); |
379 RecordGetHashResult(V4OperationResult::PARSE_ERROR); | 371 RecordGetHashResult(V4OperationResult::PARSE_ERROR); |
(...skipping 29 matching lines...) Expand all Loading... |
409 | 401 |
410 void V4GetHashProtocolManager::GetHashUrlAndHeaders( | 402 void V4GetHashProtocolManager::GetHashUrlAndHeaders( |
411 const std::string& req_base64, | 403 const std::string& req_base64, |
412 GURL* gurl, | 404 GURL* gurl, |
413 net::HttpRequestHeaders* headers) const { | 405 net::HttpRequestHeaders* headers) const { |
414 V4ProtocolManagerUtil::GetRequestUrlAndHeaders(req_base64, "fullHashes:find", | 406 V4ProtocolManagerUtil::GetRequestUrlAndHeaders(req_base64, "fullHashes:find", |
415 config_, gurl, headers); | 407 config_, gurl, headers); |
416 } | 408 } |
417 | 409 |
418 } // namespace safe_browsing | 410 } // namespace safe_browsing |
OLD | NEW |