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 #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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 const V4ProtocolConfig& config) override { | 84 const V4ProtocolConfig& config) override { |
| 84 return new V4GetHashProtocolManager(request_context_getter, config); | 85 return new V4GetHashProtocolManager(request_context_getter, config); |
| 85 } | 86 } |
| 86 | 87 |
| 87 private: | 88 private: |
| 88 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactoryImpl); | 89 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactoryImpl); |
| 89 }; | 90 }; |
| 90 | 91 |
| 91 // V4GetHashProtocolManager implementation -------------------------------- | 92 // V4GetHashProtocolManager implementation -------------------------------- |
| 92 | 93 |
| 94 struct V4GetHashProtocolManager::FetcherAndCallback { | |
| 95 FetcherAndCallback(std::unique_ptr<net::URLFetcher> fetcher, | |
| 96 FullHashCallback callback) | |
|
mattm
2016/08/27 01:43:25
const ref
Avi (use Gerrit)
2016/08/29 18:04:23
moot
| |
| 97 : fetcher(std::move(fetcher)), callback(callback) {} | |
| 98 | |
| 99 std::unique_ptr<net::URLFetcher> fetcher; | |
| 100 FullHashCallback callback; | |
| 101 }; | |
| 102 | |
| 93 // static | 103 // static |
| 94 V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL; | 104 V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL; |
| 95 | 105 |
| 96 // static | 106 // static |
| 97 V4GetHashProtocolManager* V4GetHashProtocolManager::Create( | 107 V4GetHashProtocolManager* V4GetHashProtocolManager::Create( |
| 98 net::URLRequestContextGetter* request_context_getter, | 108 net::URLRequestContextGetter* request_context_getter, |
| 99 const V4ProtocolConfig& config) { | 109 const V4ProtocolConfig& config) { |
| 100 if (!factory_) | 110 if (!factory_) |
| 101 factory_ = new V4GetHashProtocolManagerFactoryImpl(); | 111 factory_ = new V4GetHashProtocolManagerFactoryImpl(); |
| 102 return factory_->CreateProtocolManager(request_context_getter, config); | 112 return factory_->CreateProtocolManager(request_context_getter, config); |
| 103 } | 113 } |
| 104 | 114 |
| 105 void V4GetHashProtocolManager::ResetGetHashErrors() { | 115 void V4GetHashProtocolManager::ResetGetHashErrors() { |
| 106 gethash_error_count_ = 0; | 116 gethash_error_count_ = 0; |
| 107 gethash_back_off_mult_ = 1; | 117 gethash_back_off_mult_ = 1; |
| 108 } | 118 } |
| 109 | 119 |
| 110 V4GetHashProtocolManager::V4GetHashProtocolManager( | 120 V4GetHashProtocolManager::V4GetHashProtocolManager( |
| 111 net::URLRequestContextGetter* request_context_getter, | 121 net::URLRequestContextGetter* request_context_getter, |
| 112 const V4ProtocolConfig& config) | 122 const V4ProtocolConfig& config) |
| 113 : gethash_error_count_(0), | 123 : gethash_error_count_(0), |
| 114 gethash_back_off_mult_(1), | 124 gethash_back_off_mult_(1), |
| 115 next_gethash_time_(Time::FromDoubleT(0)), | 125 next_gethash_time_(Time::FromDoubleT(0)), |
| 116 config_(config), | 126 config_(config), |
| 117 request_context_getter_(request_context_getter), | 127 request_context_getter_(request_context_getter), |
| 118 url_fetcher_id_(0), | 128 url_fetcher_id_(0), |
| 119 clock_(new base::DefaultClock()) {} | 129 clock_(new base::DefaultClock()) {} |
| 120 | 130 |
| 121 V4GetHashProtocolManager::~V4GetHashProtocolManager() { | 131 V4GetHashProtocolManager::~V4GetHashProtocolManager() { |
| 122 // Delete in-progress SafeBrowsing requests. | |
| 123 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), | |
| 124 hash_requests_.end()); | |
| 125 hash_requests_.clear(); | |
| 126 } | 132 } |
| 127 | 133 |
| 128 // static | 134 // static |
| 129 void V4GetHashProtocolManager::RegisterFactory( | 135 void V4GetHashProtocolManager::RegisterFactory( |
| 130 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) { | 136 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) { |
| 131 if (factory_) | 137 if (factory_) |
| 132 delete factory_; | 138 delete factory_; |
| 133 factory_ = factory.release(); | 139 factory_ = factory.release(); |
| 134 } | 140 } |
| 135 | 141 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 std::string req_base64 = GetHashRequest(prefixes, platforms, threat_type); | 327 std::string req_base64 = GetHashRequest(prefixes, platforms, threat_type); |
| 322 GURL gethash_url; | 328 GURL gethash_url; |
| 323 net::HttpRequestHeaders headers; | 329 net::HttpRequestHeaders headers; |
| 324 GetHashUrlAndHeaders(req_base64, &gethash_url, &headers); | 330 GetHashUrlAndHeaders(req_base64, &gethash_url, &headers); |
| 325 | 331 |
| 326 net::URLFetcher* fetcher = | 332 net::URLFetcher* fetcher = |
| 327 net::URLFetcher::Create(url_fetcher_id_++, gethash_url, | 333 net::URLFetcher::Create(url_fetcher_id_++, gethash_url, |
| 328 net::URLFetcher::GET, this) | 334 net::URLFetcher::GET, this) |
| 329 .release(); | 335 .release(); |
| 330 fetcher->SetExtraRequestHeaders(headers.ToString()); | 336 fetcher->SetExtraRequestHeaders(headers.ToString()); |
| 331 hash_requests_[fetcher] = callback; | 337 hash_requests_[fetcher] = |
| 338 base::MakeUnique<FetcherAndCallback>(base::WrapUnique(fetcher), callback); | |
| 332 | 339 |
| 333 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 340 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 334 fetcher->SetRequestContext(request_context_getter_.get()); | 341 fetcher->SetRequestContext(request_context_getter_.get()); |
| 335 fetcher->Start(); | 342 fetcher->Start(); |
| 336 } | 343 } |
| 337 | 344 |
| 338 void V4GetHashProtocolManager::GetFullHashesWithApis( | 345 void V4GetHashProtocolManager::GetFullHashesWithApis( |
| 339 const std::vector<SBPrefix>& prefixes, | 346 const std::vector<SBPrefix>& prefixes, |
| 340 FullHashCallback callback) { | 347 FullHashCallback callback) { |
| 341 std::vector<PlatformType> platform = {CHROME_PLATFORM}; | 348 std::vector<PlatformType> platform = {CHROME_PLATFORM}; |
| 342 GetFullHashes(prefixes, platform, API_ABUSE, callback); | 349 GetFullHashes(prefixes, platform, API_ABUSE, callback); |
| 343 } | 350 } |
| 344 | 351 |
| 345 void V4GetHashProtocolManager::SetClockForTests( | 352 void V4GetHashProtocolManager::SetClockForTests( |
| 346 std::unique_ptr<base::Clock> clock) { | 353 std::unique_ptr<base::Clock> clock) { |
| 347 clock_ = std::move(clock); | 354 clock_ = std::move(clock); |
| 348 } | 355 } |
| 349 | 356 |
| 350 // net::URLFetcherDelegate implementation ---------------------------------- | 357 // net::URLFetcherDelegate implementation ---------------------------------- |
| 351 | 358 |
| 352 // SafeBrowsing request responses are handled here. | 359 // SafeBrowsing request responses are handled here. |
| 353 void V4GetHashProtocolManager::OnURLFetchComplete( | 360 void V4GetHashProtocolManager::OnURLFetchComplete( |
| 354 const net::URLFetcher* source) { | 361 const net::URLFetcher* source) { |
| 355 DCHECK(CalledOnValidThread()); | 362 DCHECK(CalledOnValidThread()); |
| 356 | 363 |
| 357 HashRequests::iterator it = hash_requests_.find(source); | 364 HashRequests::iterator it = hash_requests_.find(source); |
| 358 DCHECK(it != hash_requests_.end()) << "Request not found"; | 365 DCHECK(it != hash_requests_.end()) << "Request not found"; |
| 359 | 366 |
| 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(); | 367 int response_code = source->GetResponseCode(); |
| 365 net::URLRequestStatus status = source->GetStatus(); | 368 net::URLRequestStatus status = source->GetStatus(); |
| 366 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( | 369 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
| 367 kUmaV4HashResponseMetricName, status, response_code); | 370 kUmaV4HashResponseMetricName, status, response_code); |
| 368 | 371 |
| 369 const FullHashCallback& callback = it->second; | 372 const FullHashCallback& callback = it->second->callback; |
| 370 std::vector<SBFullHashResult> full_hashes; | 373 std::vector<SBFullHashResult> full_hashes; |
| 371 base::Time negative_cache_expire; | 374 base::Time negative_cache_expire; |
| 372 if (status.is_success() && response_code == net::HTTP_OK) { | 375 if (status.is_success() && response_code == net::HTTP_OK) { |
| 373 RecordGetHashResult(V4OperationResult::STATUS_200); | 376 RecordGetHashResult(V4OperationResult::STATUS_200); |
| 374 ResetGetHashErrors(); | 377 ResetGetHashErrors(); |
| 375 std::string data; | 378 std::string data; |
| 376 source->GetResponseAsString(&data); | 379 source->GetResponseAsString(&data); |
| 377 if (!ParseHashResponse(data, &full_hashes, &negative_cache_expire)) { | 380 if (!ParseHashResponse(data, &full_hashes, &negative_cache_expire)) { |
| 378 full_hashes.clear(); | 381 full_hashes.clear(); |
| 379 RecordGetHashResult(V4OperationResult::PARSE_ERROR); | 382 RecordGetHashResult(V4OperationResult::PARSE_ERROR); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 409 | 412 |
| 410 void V4GetHashProtocolManager::GetHashUrlAndHeaders( | 413 void V4GetHashProtocolManager::GetHashUrlAndHeaders( |
| 411 const std::string& req_base64, | 414 const std::string& req_base64, |
| 412 GURL* gurl, | 415 GURL* gurl, |
| 413 net::HttpRequestHeaders* headers) const { | 416 net::HttpRequestHeaders* headers) const { |
| 414 V4ProtocolManagerUtil::GetRequestUrlAndHeaders(req_base64, "fullHashes:find", | 417 V4ProtocolManagerUtil::GetRequestUrlAndHeaders(req_base64, "fullHashes:find", |
| 415 config_, gurl, headers); | 418 config_, gurl, headers); |
| 416 } | 419 } |
| 417 | 420 |
| 418 } // namespace safe_browsing | 421 } // namespace safe_browsing |
| OLD | NEW |