Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: components/safe_browsing_db/database_manager.cc

Issue 2009133005: SafeBrowsing: Implement cache eviction for API full hash results. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@osb-cache
Patch Set: Rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/database_manager.h" 5 #include "components/safe_browsing_db/database_manager.h"
6 6
7 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 7 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
8 #include "content/public/browser/browser_thread.h" 8 #include "content/public/browser/browser_thread.h"
9 #include "net/url_request/url_request_context_getter.h" 9 #include "net/url_request/url_request_context_getter.h"
10 #include "url/gurl.h" 10 #include "url/gurl.h"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 // Case 2: The prefix is not in the cache. 160 // Case 2: The prefix is not in the cache.
161 // We need to send a request for full hashes. 161 // We need to send a request for full hashes.
162 // 162 //
163 // Eviction: 163 // Eviction:
164 // SBCachedFullHashResult entries can be removed from the cache only when 164 // SBCachedFullHashResult entries can be removed from the cache only when
165 // the negative cache expire time and the cache expire time of all full 165 // the negative cache expire time and the cache expire time of all full
166 // hash results for that prefix have expired. 166 // hash results for that prefix have expired.
167 // Individual full hash results can be removed from the prefix's 167 // Individual full hash results can be removed from the prefix's
168 // cache entry if they expire AND their expire time is after the negative 168 // cache entry if they expire AND their expire time is after the negative
169 // cache expire time. 169 // cache expire time.
170 //
171 // TODO(kcarattini): Implement cache eviction.
172 for (const SBFullHash& full_hash : full_hashes) { 170 for (const SBFullHash& full_hash : full_hashes) {
173 auto entry = v4_full_hash_cache_[threat_type].find(full_hash.prefix); 171 auto entry = v4_full_hash_cache_[threat_type].find(full_hash.prefix);
174 if (entry != v4_full_hash_cache_[threat_type].end()) { 172 if (entry != v4_full_hash_cache_[threat_type].end()) {
175 // Case 1. 173 // Case 1.
176 const SBCachedFullHashResult& cache_result = entry->second; 174 SBCachedFullHashResult& cache_result = entry->second;
177 175
178 const SBFullHashResult* found_full_hash = nullptr; 176 const SBFullHashResult* found_full_hash = nullptr;
177 size_t matched_idx = 0;
179 for (const SBFullHashResult& hash_result : cache_result.full_hashes) { 178 for (const SBFullHashResult& hash_result : cache_result.full_hashes) {
180 if (SBFullHashEqual(full_hash, hash_result.hash)) { 179 if (SBFullHashEqual(full_hash, hash_result.hash)) {
181 found_full_hash = &hash_result; 180 found_full_hash = &hash_result;
182 break; 181 break;
183 } 182 }
183 ++matched_idx;
184 } 184 }
185 185
186 if (found_full_hash) { 186 if (found_full_hash) {
187 // Case a. 187 // Case a.
188 if (found_full_hash->cache_expire_after > now) { 188 if (found_full_hash->cache_expire_after > now) {
189 // Case i. 189 // Case i.
190 cached_results->push_back(*found_full_hash); 190 cached_results->push_back(*found_full_hash);
191 } else { 191 } else {
192 // Case ii. 192 // Case ii.
193 prefixes_needing_reqs->push_back(full_hash.prefix); 193 prefixes_needing_reqs->push_back(full_hash.prefix);
194 // If the negative cache expire time has passed, evict this full hash
195 // result from the cache.
196 if (cache_result.expire_after <= now) {
197 cache_result.full_hashes.erase(
198 cache_result.full_hashes.begin() + matched_idx);
199 // If there are no more full hashes, we can evict the entire entry.
200 if (cache_result.full_hashes.empty()) {
201 v4_full_hash_cache_[threat_type].erase(entry);
202 }
203 }
194 } 204 }
195 } else { 205 } else {
196 // Case b. 206 // Case b.
197 if (cache_result.expire_after > now) { 207 if (cache_result.expire_after > now) {
198 // Case i. 208 // Case i.
199 } else { 209 } else {
200 // Case ii. 210 // Case ii.
201 prefixes_needing_reqs->push_back(full_hash.prefix); 211 prefixes_needing_reqs->push_back(full_hash.prefix);
202 } 212 }
203 } 213 }
204 } else { 214 } else {
205 // Case 2. 215 // Case 2.
206 prefixes_needing_reqs->push_back(full_hash.prefix); 216 prefixes_needing_reqs->push_back(full_hash.prefix);
207 } 217 }
208 } 218 }
209 219
210 // Multiple full hashes could share a prefix, remove duplicates. 220 // Multiple full hashes could share a prefix, remove duplicates.
221 // TODO(kcarattini): Make |prefixes_needing_reqs| a set.
211 std::sort(prefixes_needing_reqs->begin(), prefixes_needing_reqs->end()); 222 std::sort(prefixes_needing_reqs->begin(), prefixes_needing_reqs->end());
212 prefixes_needing_reqs->erase(std::unique(prefixes_needing_reqs->begin(), 223 prefixes_needing_reqs->erase(std::unique(prefixes_needing_reqs->begin(),
213 prefixes_needing_reqs->end()), prefixes_needing_reqs->end()); 224 prefixes_needing_reqs->end()), prefixes_needing_reqs->end());
214 } 225 }
215 226
216 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults( 227 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults(
217 SafeBrowsingApiCheck* check, 228 SafeBrowsingApiCheck* check,
218 const std::vector<SBFullHashResult>& full_hash_results, 229 const std::vector<SBFullHashResult>& full_hash_results,
219 const base::Time& negative_cache_expire) { 230 const base::Time& negative_cache_expire) {
220 DCHECK_CURRENTLY_ON(BrowserThread::IO); 231 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 const std::vector<SBFullHashResult>& cached_results, 293 const std::vector<SBFullHashResult>& cached_results,
283 Client* client) 294 Client* client)
284 : url_(url), prefixes_(prefixes), full_hashes_(full_hashes), 295 : url_(url), prefixes_(prefixes), full_hashes_(full_hashes),
285 cached_results_(cached_results), client_(client) { 296 cached_results_(cached_results), client_(client) {
286 } 297 }
287 298
288 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() { 299 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() {
289 } 300 }
290 301
291 } // namespace safe_browsing 302 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/database_manager.h ('k') | components/safe_browsing_db/database_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698