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

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: Resolve merge conflicts 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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 // Case 2: The prefix is not in the cache. 159 // Case 2: The prefix is not in the cache.
160 // We need to send a request for full hashes. 160 // We need to send a request for full hashes.
161 // 161 //
162 // Eviction: 162 // Eviction:
163 // SBCachedFullHashResult entries can be removed from the cache only when 163 // SBCachedFullHashResult entries can be removed from the cache only when
164 // the negative cache expire time and the cache expire time of all full 164 // the negative cache expire time and the cache expire time of all full
165 // hash results for that prefix have expired. 165 // hash results for that prefix have expired.
166 // Individual full hash results can be removed from the prefix's 166 // Individual full hash results can be removed from the prefix's
167 // cache entry if they expire AND their expire time is after the negative 167 // cache entry if they expire AND their expire time is after the negative
168 // cache expire time. 168 // cache expire time.
169 //
170 // TODO(kcarattini): Implement cache eviction.
171 for (const SBFullHash& full_hash : full_hashes) { 169 for (const SBFullHash& full_hash : full_hashes) {
172 auto entry = v4_full_hash_cache_.find(full_hash.prefix); 170 auto entry = v4_full_hash_cache_.find(full_hash.prefix);
173 if (entry != v4_full_hash_cache_.end()) { 171 if (entry != v4_full_hash_cache_.end()) {
174 // Case 1. 172 // Case 1.
175 const SBCachedFullHashResult& cache_result = entry->second; 173 SBCachedFullHashResult& cache_result = entry->second;
176 174
177 const SBFullHashResult* found_full_hash = nullptr; 175 const SBFullHashResult* found_full_hash = nullptr;
176 size_t i = 0;
Nathan Parker 2016/05/31 22:28:31 how about something a bit more descriptive, like m
kcarattini 2016/06/01 10:58:36 Done.
178 for (const SBFullHashResult& hash_result : cache_result.full_hashes) { 177 for (const SBFullHashResult& hash_result : cache_result.full_hashes) {
179 if (SBFullHashEqual(full_hash, hash_result.hash)) { 178 if (SBFullHashEqual(full_hash, hash_result.hash)) {
180 found_full_hash = &hash_result; 179 found_full_hash = &hash_result;
181 break; 180 break;
182 } 181 }
182 ++i;
183 } 183 }
184 184
185 if (found_full_hash) { 185 if (found_full_hash) {
186 // Case a. 186 // Case a.
187 if (found_full_hash->cache_expire_after > now) { 187 if (found_full_hash->cache_expire_after > now) {
188 // Case i. 188 // Case i.
189 cached_results->push_back(*found_full_hash); 189 cached_results->push_back(*found_full_hash);
190 } else { 190 } else {
191 // Case ii. 191 // Case ii.
192 prefixes_needing_reqs->push_back(full_hash.prefix); 192 prefixes_needing_reqs->push_back(full_hash.prefix);
193 // If the negative cache expire time has passed, evict this full hash
194 // result from the cache.
195 if (cache_result.expire_after <= now) {
Nathan Parker 2016/05/31 23:32:04 What if there are positive cache entries that have
kcarattini 2016/06/01 10:58:36 Alex, correct me if I'm wrong, but I do not believ
woz 2016/06/02 15:38:26 That's right -- there's no guarantee that neg. cac
196 cache_result.full_hashes.erase(
197 cache_result.full_hashes.begin() + i);
198 // If there are no more full hashes, we can evict the entire entry.
199 if (cache_result.full_hashes.empty()) {
200 v4_full_hash_cache_.erase(entry);
201 }
202 }
193 } 203 }
194 } else { 204 } else {
195 // Case b. 205 // Case b.
196 if (cache_result.expire_after > now) { 206 if (cache_result.expire_after > now) {
197 // Case i. 207 // Case i.
198 } else { 208 } else {
199 // Case ii. 209 // Case ii.
200 prefixes_needing_reqs->push_back(full_hash.prefix); 210 prefixes_needing_reqs->push_back(full_hash.prefix);
201 } 211 }
202 } 212 }
203 } else { 213 } else {
204 // Case 2. 214 // Case 2.
205 prefixes_needing_reqs->push_back(full_hash.prefix); 215 prefixes_needing_reqs->push_back(full_hash.prefix);
206 } 216 }
207 } 217 }
208 218
209 // Multiple full hashes could share a prefix, remove duplicates. 219 // Multiple full hashes could share a prefix, remove duplicates.
210 std::sort(prefixes_needing_reqs->begin(), prefixes_needing_reqs->end()); 220 std::sort(prefixes_needing_reqs->begin(), prefixes_needing_reqs->end());
Nathan Parker 2016/05/31 22:28:31 an idea: prefixes_needing_req's could be a set (li
kcarattini 2016/06/01 10:58:36 I added a TODO. Since this change will require pip
211 prefixes_needing_reqs->erase(std::unique(prefixes_needing_reqs->begin(), 221 prefixes_needing_reqs->erase(std::unique(prefixes_needing_reqs->begin(),
212 prefixes_needing_reqs->end()), prefixes_needing_reqs->end()); 222 prefixes_needing_reqs->end()), prefixes_needing_reqs->end());
213 } 223 }
214 224
215 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults( 225 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults(
216 SafeBrowsingApiCheck* check, 226 SafeBrowsingApiCheck* check,
217 const std::vector<SBFullHashResult>& full_hash_results, 227 const std::vector<SBFullHashResult>& full_hash_results,
218 const base::Time& negative_cache_expire) { 228 const base::Time& negative_cache_expire) {
219 DCHECK_CURRENTLY_ON(BrowserThread::IO); 229 DCHECK_CURRENTLY_ON(BrowserThread::IO);
220 DCHECK(check); 230 DCHECK(check);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 const std::vector<SBFullHashResult>& cached_results, 290 const std::vector<SBFullHashResult>& cached_results,
281 Client* client) 291 Client* client)
282 : url_(url), prefixes_(prefixes), full_hashes_(full_hashes), 292 : url_(url), prefixes_(prefixes), full_hashes_(full_hashes),
283 cached_results_(cached_results), client_(client) { 293 cached_results_(cached_results), client_(client) {
284 } 294 }
285 295
286 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() { 296 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() {
287 } 297 }
288 298
289 } // namespace safe_browsing 299 } // 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