| 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/memory/ptr_util.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 15 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 16 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 19 | 20 |
| 20 using base::Time; | 21 using base::Time; |
| 21 using base::TimeDelta; | 22 using base::TimeDelta; |
| 23 using content::BrowserThread; |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 // Record a GetHash result. | 27 // Record a GetHash result. |
| 26 void RecordGetHashResult(safe_browsing::V4OperationResult result) { | 28 void RecordGetHashResult(safe_browsing::V4OperationResult result) { |
| 27 UMA_HISTOGRAM_ENUMERATION( | 29 UMA_HISTOGRAM_ENUMERATION( |
| 28 "SafeBrowsing.GetV4HashResult", result, | 30 "SafeBrowsing.GetV4HashResult", result, |
| 29 safe_browsing::V4OperationResult::OPERATION_RESULT_MAX); | 31 safe_browsing::V4OperationResult::OPERATION_RESULT_MAX); |
| 30 } | 32 } |
| 31 | 33 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 250 |
| 249 void V4GetHashProtocolManager::ClearCache() { | 251 void V4GetHashProtocolManager::ClearCache() { |
| 250 DCHECK(CalledOnValidThread()); | 252 DCHECK(CalledOnValidThread()); |
| 251 full_hash_cache_.clear(); | 253 full_hash_cache_.clear(); |
| 252 } | 254 } |
| 253 | 255 |
| 254 void V4GetHashProtocolManager::GetFullHashes( | 256 void V4GetHashProtocolManager::GetFullHashes( |
| 255 const FullHashToStoreAndHashPrefixesMap& | 257 const FullHashToStoreAndHashPrefixesMap& |
| 256 full_hash_to_store_and_hash_prefixes, | 258 full_hash_to_store_and_hash_prefixes, |
| 257 FullHashCallback callback) { | 259 FullHashCallback callback) { |
| 260 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 258 DCHECK(CalledOnValidThread()); | 261 DCHECK(CalledOnValidThread()); |
| 259 DCHECK(!full_hash_to_store_and_hash_prefixes.empty()); | 262 DCHECK(!full_hash_to_store_and_hash_prefixes.empty()); |
| 260 | 263 |
| 261 std::vector<HashPrefix> prefixes_to_request; | 264 std::vector<HashPrefix> prefixes_to_request; |
| 262 std::vector<FullHashInfo> cached_full_hash_infos; | 265 std::vector<FullHashInfo> cached_full_hash_infos; |
| 263 GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, Time::Now(), | 266 GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, Time::Now(), |
| 264 &prefixes_to_request, &cached_full_hash_infos); | 267 &prefixes_to_request, &cached_full_hash_infos); |
| 265 | 268 |
| 266 if (prefixes_to_request.empty()) { | 269 if (prefixes_to_request.empty()) { |
| 267 // 100% cache hits (positive or negative) so we can call the callback right | 270 // 100% cache hits (positive or negative) so we can call the callback right |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 | 302 |
| 300 fetcher->SetExtraRequestHeaders(headers.ToString()); | 303 fetcher->SetExtraRequestHeaders(headers.ToString()); |
| 301 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 304 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 302 fetcher->SetRequestContext(request_context_getter_.get()); | 305 fetcher->SetRequestContext(request_context_getter_.get()); |
| 303 fetcher->Start(); | 306 fetcher->Start(); |
| 304 } | 307 } |
| 305 | 308 |
| 306 void V4GetHashProtocolManager::GetFullHashesWithApis( | 309 void V4GetHashProtocolManager::GetFullHashesWithApis( |
| 307 const GURL& url, | 310 const GURL& url, |
| 308 ThreatMetadataForApiCallback api_callback) { | 311 ThreatMetadataForApiCallback api_callback) { |
| 312 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 309 DCHECK(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme)); | 313 DCHECK(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme)); |
| 310 | 314 |
| 311 std::unordered_set<FullHash> full_hashes; | 315 std::unordered_set<FullHash> full_hashes; |
| 312 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); | 316 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); |
| 313 | 317 |
| 314 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | 318 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; |
| 315 for (const FullHash& full_hash : full_hashes) { | 319 for (const FullHash& full_hash : full_hashes) { |
| 316 HashPrefix prefix; | 320 HashPrefix prefix; |
| 317 bool result = | 321 bool result = |
| 318 V4ProtocolManagerUtil::FullHashToSmallestHashPrefix(full_hash, &prefix); | 322 V4ProtocolManagerUtil::FullHashToSmallestHashPrefix(full_hash, &prefix); |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 | 626 |
| 623 void V4GetHashProtocolManager::SetClockForTests( | 627 void V4GetHashProtocolManager::SetClockForTests( |
| 624 std::unique_ptr<base::Clock> clock) { | 628 std::unique_ptr<base::Clock> clock) { |
| 625 clock_ = std::move(clock); | 629 clock_ = std::move(clock); |
| 626 } | 630 } |
| 627 | 631 |
| 628 void V4GetHashProtocolManager::UpdateCache( | 632 void V4GetHashProtocolManager::UpdateCache( |
| 629 const std::vector<HashPrefix>& prefixes_requested, | 633 const std::vector<HashPrefix>& prefixes_requested, |
| 630 const std::vector<FullHashInfo>& full_hash_infos, | 634 const std::vector<FullHashInfo>& full_hash_infos, |
| 631 const Time& negative_cache_expire) { | 635 const Time& negative_cache_expire) { |
| 636 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 637 |
| 632 // If negative_cache_expire is null, don't cache the results since it's not | 638 // If negative_cache_expire is null, don't cache the results since it's not |
| 633 // clear till what time they should be considered valid. | 639 // clear till what time they should be considered valid. |
| 634 if (negative_cache_expire.is_null()) { | 640 if (negative_cache_expire.is_null()) { |
| 635 return; | 641 return; |
| 636 } | 642 } |
| 637 | 643 |
| 638 for (const HashPrefix& prefix : prefixes_requested) { | 644 for (const HashPrefix& prefix : prefixes_requested) { |
| 639 // Create or reset the cached result for this prefix. | 645 // Create or reset the cached result for this prefix. |
| 640 CachedHashPrefixInfo& chpi = full_hash_cache_[prefix]; | 646 CachedHashPrefixInfo& chpi = full_hash_cache_[prefix]; |
| 641 chpi.full_hash_infos.clear(); | 647 chpi.full_hash_infos.clear(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 RecordV4GetHashCheckResult(GET_HASH_CHECK_MISS); | 683 RecordV4GetHashCheckResult(GET_HASH_CHECK_MISS); |
| 678 } | 684 } |
| 679 } | 685 } |
| 680 | 686 |
| 681 // net::URLFetcherDelegate implementation ---------------------------------- | 687 // net::URLFetcherDelegate implementation ---------------------------------- |
| 682 | 688 |
| 683 // SafeBrowsing request responses are handled here. | 689 // SafeBrowsing request responses are handled here. |
| 684 void V4GetHashProtocolManager::OnURLFetchComplete( | 690 void V4GetHashProtocolManager::OnURLFetchComplete( |
| 685 const net::URLFetcher* source) { | 691 const net::URLFetcher* source) { |
| 686 DCHECK(CalledOnValidThread()); | 692 DCHECK(CalledOnValidThread()); |
| 693 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 687 | 694 |
| 688 PendingHashRequests::iterator it = pending_hash_requests_.find(source); | 695 PendingHashRequests::iterator it = pending_hash_requests_.find(source); |
| 689 DCHECK(it != pending_hash_requests_.end()) << "Request not found"; | 696 DCHECK(it != pending_hash_requests_.end()) << "Request not found"; |
| 690 | 697 |
| 691 int response_code = source->GetResponseCode(); | 698 int response_code = source->GetResponseCode(); |
| 692 net::URLRequestStatus status = source->GetStatus(); | 699 net::URLRequestStatus status = source->GetStatus(); |
| 693 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( | 700 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
| 694 kUmaV4HashResponseMetricName, status, response_code); | 701 kUmaV4HashResponseMetricName, status, response_code); |
| 695 | 702 |
| 696 std::vector<FullHashInfo> full_hash_infos; | 703 std::vector<FullHashInfo> full_hash_infos; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 732 std::ostream& operator<<(std::ostream& os, const FullHashInfo& fhi) { | 739 std::ostream& operator<<(std::ostream& os, const FullHashInfo& fhi) { |
| 733 os << "{full_hash: " << fhi.full_hash << "; list_id: " << fhi.list_id | 740 os << "{full_hash: " << fhi.full_hash << "; list_id: " << fhi.list_id |
| 734 << "; positive_expiry: " << fhi.positive_expiry | 741 << "; positive_expiry: " << fhi.positive_expiry |
| 735 << "; metadata.api_permissions.size(): " | 742 << "; metadata.api_permissions.size(): " |
| 736 << fhi.metadata.api_permissions.size() << "}"; | 743 << fhi.metadata.api_permissions.size() << "}"; |
| 737 return os; | 744 return os; |
| 738 } | 745 } |
| 739 #endif | 746 #endif |
| 740 | 747 |
| 741 } // namespace safe_browsing | 748 } // namespace safe_browsing |
| OLD | NEW |