| 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" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 "SafeBrowsing.GetV4HashHttpResponseOrErrorCode"; | 133 "SafeBrowsing.GetV4HashHttpResponseOrErrorCode"; |
| 134 | 134 |
| 135 // The default V4GetHashProtocolManagerFactory. | 135 // The default V4GetHashProtocolManagerFactory. |
| 136 class V4GetHashProtocolManagerFactoryImpl | 136 class V4GetHashProtocolManagerFactoryImpl |
| 137 : public V4GetHashProtocolManagerFactory { | 137 : public V4GetHashProtocolManagerFactory { |
| 138 public: | 138 public: |
| 139 V4GetHashProtocolManagerFactoryImpl() {} | 139 V4GetHashProtocolManagerFactoryImpl() {} |
| 140 ~V4GetHashProtocolManagerFactoryImpl() override {} | 140 ~V4GetHashProtocolManagerFactoryImpl() override {} |
| 141 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager( | 141 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager( |
| 142 net::URLRequestContextGetter* request_context_getter, | 142 net::URLRequestContextGetter* request_context_getter, |
| 143 const base::hash_set<UpdateListIdentifier>& stores_to_request, | 143 const std::unordered_set<UpdateListIdentifier>& stores_to_request, |
| 144 const V4ProtocolConfig& config) override { | 144 const V4ProtocolConfig& config) override { |
| 145 return base::WrapUnique(new V4GetHashProtocolManager( | 145 return base::WrapUnique(new V4GetHashProtocolManager( |
| 146 request_context_getter, stores_to_request, config)); | 146 request_context_getter, stores_to_request, config)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 private: | 149 private: |
| 150 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactoryImpl); | 150 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactoryImpl); |
| 151 }; | 151 }; |
| 152 | 152 |
| 153 // ---------------------------------------------------------------- | 153 // ---------------------------------------------------------------- |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 202 } |
| 203 | 203 |
| 204 // V4GetHashProtocolManager implementation -------------------------------- | 204 // V4GetHashProtocolManager implementation -------------------------------- |
| 205 | 205 |
| 206 // static | 206 // static |
| 207 V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL; | 207 V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL; |
| 208 | 208 |
| 209 // static | 209 // static |
| 210 std::unique_ptr<V4GetHashProtocolManager> V4GetHashProtocolManager::Create( | 210 std::unique_ptr<V4GetHashProtocolManager> V4GetHashProtocolManager::Create( |
| 211 net::URLRequestContextGetter* request_context_getter, | 211 net::URLRequestContextGetter* request_context_getter, |
| 212 const base::hash_set<UpdateListIdentifier>& stores_to_request, | 212 const std::unordered_set<UpdateListIdentifier>& stores_to_request, |
| 213 const V4ProtocolConfig& config) { | 213 const V4ProtocolConfig& config) { |
| 214 if (!factory_) | 214 if (!factory_) |
| 215 factory_ = new V4GetHashProtocolManagerFactoryImpl(); | 215 factory_ = new V4GetHashProtocolManagerFactoryImpl(); |
| 216 return factory_->CreateProtocolManager(request_context_getter, | 216 return factory_->CreateProtocolManager(request_context_getter, |
| 217 stores_to_request, config); | 217 stores_to_request, config); |
| 218 } | 218 } |
| 219 | 219 |
| 220 // static | 220 // static |
| 221 void V4GetHashProtocolManager::RegisterFactory( | 221 void V4GetHashProtocolManager::RegisterFactory( |
| 222 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) { | 222 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) { |
| 223 if (factory_) | 223 if (factory_) |
| 224 delete factory_; | 224 delete factory_; |
| 225 factory_ = factory.release(); | 225 factory_ = factory.release(); |
| 226 } | 226 } |
| 227 | 227 |
| 228 V4GetHashProtocolManager::V4GetHashProtocolManager( | 228 V4GetHashProtocolManager::V4GetHashProtocolManager( |
| 229 net::URLRequestContextGetter* request_context_getter, | 229 net::URLRequestContextGetter* request_context_getter, |
| 230 const base::hash_set<UpdateListIdentifier>& stores_to_request, | 230 const std::unordered_set<UpdateListIdentifier>& stores_to_request, |
| 231 const V4ProtocolConfig& config) | 231 const V4ProtocolConfig& config) |
| 232 : gethash_error_count_(0), | 232 : gethash_error_count_(0), |
| 233 gethash_back_off_mult_(1), | 233 gethash_back_off_mult_(1), |
| 234 next_gethash_time_(Time::FromDoubleT(0)), | 234 next_gethash_time_(Time::FromDoubleT(0)), |
| 235 config_(config), | 235 config_(config), |
| 236 request_context_getter_(request_context_getter), | 236 request_context_getter_(request_context_getter), |
| 237 url_fetcher_id_(0), | 237 url_fetcher_id_(0), |
| 238 clock_(new base::DefaultClock()) { | 238 clock_(new base::DefaultClock()) { |
| 239 DCHECK(!stores_to_request.empty()); | 239 DCHECK(!stores_to_request.empty()); |
| 240 for (const UpdateListIdentifier& store : stores_to_request) { | 240 for (const UpdateListIdentifier& store : stores_to_request) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 301 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 302 fetcher->SetRequestContext(request_context_getter_.get()); | 302 fetcher->SetRequestContext(request_context_getter_.get()); |
| 303 fetcher->Start(); | 303 fetcher->Start(); |
| 304 } | 304 } |
| 305 | 305 |
| 306 void V4GetHashProtocolManager::GetFullHashesWithApis( | 306 void V4GetHashProtocolManager::GetFullHashesWithApis( |
| 307 const GURL& url, | 307 const GURL& url, |
| 308 ThreatMetadataForApiCallback api_callback) { | 308 ThreatMetadataForApiCallback api_callback) { |
| 309 DCHECK(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme)); | 309 DCHECK(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme)); |
| 310 | 310 |
| 311 base::hash_set<FullHash> full_hashes; | 311 std::unordered_set<FullHash> full_hashes; |
| 312 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); | 312 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); |
| 313 | 313 |
| 314 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | 314 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; |
| 315 for (const FullHash& full_hash : full_hashes) { | 315 for (const FullHash& full_hash : full_hashes) { |
| 316 HashPrefix prefix; | 316 HashPrefix prefix; |
| 317 bool result = | 317 bool result = |
| 318 V4ProtocolManagerUtil::FullHashToSmallestHashPrefix(full_hash, &prefix); | 318 V4ProtocolManagerUtil::FullHashToSmallestHashPrefix(full_hash, &prefix); |
| 319 DCHECK(result); | 319 DCHECK(result); |
| 320 full_hash_to_store_and_hash_prefixes[full_hash].emplace_back( | 320 full_hash_to_store_and_hash_prefixes[full_hash].emplace_back( |
| 321 GetChromeUrlApiId(), prefix); | 321 GetChromeUrlApiId(), prefix); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 // We need to send a request for full hashes. | 358 // We need to send a request for full hashes. |
| 359 // | 359 // |
| 360 // Note on eviction: | 360 // Note on eviction: |
| 361 // CachedHashPrefixInfo entries can be removed from the cache only when | 361 // CachedHashPrefixInfo entries can be removed from the cache only when |
| 362 // the negative cache expire time and the cache expire time of all full | 362 // the negative cache expire time and the cache expire time of all full |
| 363 // hash results for that prefix have expired. | 363 // hash results for that prefix have expired. |
| 364 // Individual full hash results can be removed from the prefix's | 364 // Individual full hash results can be removed from the prefix's |
| 365 // cache entry if they expire AND their expire time is after the negative | 365 // cache entry if they expire AND their expire time is after the negative |
| 366 // cache expire time. | 366 // cache expire time. |
| 367 | 367 |
| 368 base::hash_set<HashPrefix> unique_prefixes_to_request; | 368 std::unordered_set<HashPrefix> unique_prefixes_to_request; |
| 369 for (const auto& it : full_hash_to_store_and_hash_prefixes) { | 369 for (const auto& it : full_hash_to_store_and_hash_prefixes) { |
| 370 const FullHash& full_hash = it.first; | 370 const FullHash& full_hash = it.first; |
| 371 const StoreAndHashPrefixes& matched = it.second; | 371 const StoreAndHashPrefixes& matched = it.second; |
| 372 for (const StoreAndHashPrefix& matched_it : matched) { | 372 for (const StoreAndHashPrefix& matched_it : matched) { |
| 373 const UpdateListIdentifier& list_id = matched_it.list_id; | 373 const UpdateListIdentifier& list_id = matched_it.list_id; |
| 374 const HashPrefix& prefix = matched_it.hash_prefix; | 374 const HashPrefix& prefix = matched_it.hash_prefix; |
| 375 auto prefix_entry = full_hash_cache_.find(prefix); | 375 auto prefix_entry = full_hash_cache_.find(prefix); |
| 376 if (prefix_entry != full_hash_cache_.end()) { | 376 if (prefix_entry != full_hash_cache_.end()) { |
| 377 // Case 1. | 377 // Case 1. |
| 378 const CachedHashPrefixInfo& cached_prefix_info = prefix_entry->second; | 378 const CachedHashPrefixInfo& cached_prefix_info = prefix_entry->second; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 | 457 |
| 458 void V4GetHashProtocolManager::HandleGetHashError(const Time& now) { | 458 void V4GetHashProtocolManager::HandleGetHashError(const Time& now) { |
| 459 DCHECK(CalledOnValidThread()); | 459 DCHECK(CalledOnValidThread()); |
| 460 TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval( | 460 TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval( |
| 461 &gethash_error_count_, &gethash_back_off_mult_); | 461 &gethash_error_count_, &gethash_back_off_mult_); |
| 462 next_gethash_time_ = now + next; | 462 next_gethash_time_ = now + next; |
| 463 } | 463 } |
| 464 | 464 |
| 465 void V4GetHashProtocolManager::OnFullHashForApi( | 465 void V4GetHashProtocolManager::OnFullHashForApi( |
| 466 const ThreatMetadataForApiCallback& api_callback, | 466 const ThreatMetadataForApiCallback& api_callback, |
| 467 const base::hash_set<FullHash>& full_hashes, | 467 const std::unordered_set<FullHash>& full_hashes, |
| 468 const std::vector<FullHashInfo>& full_hash_infos) { | 468 const std::vector<FullHashInfo>& full_hash_infos) { |
| 469 ThreatMetadata md; | 469 ThreatMetadata md; |
| 470 for (const FullHashInfo& full_hash_info : full_hash_infos) { | 470 for (const FullHashInfo& full_hash_info : full_hash_infos) { |
| 471 DCHECK_EQ(GetChromeUrlApiId(), full_hash_info.list_id); | 471 DCHECK_EQ(GetChromeUrlApiId(), full_hash_info.list_id); |
| 472 DCHECK(full_hashes.find(full_hash_info.full_hash) != full_hashes.end()); | 472 DCHECK(full_hashes.find(full_hash_info.full_hash) != full_hashes.end()); |
| 473 md.api_permissions.insert(full_hash_info.metadata.api_permissions.begin(), | 473 md.api_permissions.insert(full_hash_info.metadata.api_permissions.begin(), |
| 474 full_hash_info.metadata.api_permissions.end()); | 474 full_hash_info.metadata.api_permissions.end()); |
| 475 } | 475 } |
| 476 | 476 |
| 477 api_callback.Run(md); | 477 api_callback.Run(md); |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 730 std::ostream& operator<<(std::ostream& os, const FullHashInfo& fhi) { | 730 std::ostream& operator<<(std::ostream& os, const FullHashInfo& fhi) { |
| 731 os << "{full_hash: " << fhi.full_hash << "; list_id: " << fhi.list_id | 731 os << "{full_hash: " << fhi.full_hash << "; list_id: " << fhi.list_id |
| 732 << "; positive_expiry: " << fhi.positive_expiry | 732 << "; positive_expiry: " << fhi.positive_expiry |
| 733 << "; metadata.api_permissions.size(): " | 733 << "; metadata.api_permissions.size(): " |
| 734 << fhi.metadata.api_permissions.size() << "}"; | 734 << fhi.metadata.api_permissions.size() << "}"; |
| 735 return os; | 735 return os; |
| 736 } | 736 } |
| 737 #endif | 737 #endif |
| 738 | 738 |
| 739 } // namespace safe_browsing | 739 } // namespace safe_browsing |
| OLD | NEW |