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

Side by Side Diff: net/cookies/cookie_monster.cc

Issue 1976073002: Making cookies eviction quotas match spec (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: removing deprecated comments 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
« no previous file with comments | « no previous file | net/cookies/cookie_monster_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // Portions of this code based on Mozilla: 5 // Portions of this code based on Mozilla:
6 // (netwerk/cookie/src/nsCookieService.cpp) 6 // (netwerk/cookie/src/nsCookieService.cpp)
7 /* ***** BEGIN LICENSE BLOCK ***** 7 /* ***** BEGIN LICENSE BLOCK *****
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9 * 9 *
10 * The contents of this file are subject to the Mozilla Public License Version 10 * The contents of this file are subject to the Mozilla Public License Version
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 // DELETE_COOKIE_LAST_ENTRY 302 // DELETE_COOKIE_LAST_ENTRY
303 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false}}; 303 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false}};
304 304
305 void RunAsync(scoped_refptr<base::TaskRunner> proxy, 305 void RunAsync(scoped_refptr<base::TaskRunner> proxy,
306 const CookieStore::CookieChangedCallback& callback, 306 const CookieStore::CookieChangedCallback& callback,
307 const CanonicalCookie& cookie, 307 const CanonicalCookie& cookie,
308 bool removed) { 308 bool removed) {
309 proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, removed)); 309 proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, removed));
310 } 310 }
311 311
312 size_t CountProtectedSecureCookiesAtPriority(
313 CookiePriority priority,
314 CookieMonster::CookieItVector* cookies) {
315 size_t num_protected_secure_cookies = 0;
316 for (const auto& cookie : *cookies) {
317 if (!cookie->second->IsSecure())
318 continue;
319 // 1) At low-priority, only low-priority secure cookies are protected as
320 // part of the quota.
321 // 2) At medium-priority, only medium-priority secure cookies are protected
322 // as part of the quota (low-priority secure cookies may be deleted).
323 // 3) At high-priority, medium-priority and high-priority secure cookies are
324 // protected as part of the quota (low-priority secure cookies may be
325 // deleted).
326 CookiePriority cookie_priority = cookie->second->Priority();
327 switch (cookie_priority) {
328 case COOKIE_PRIORITY_LOW:
329 if (priority == COOKIE_PRIORITY_LOW)
330 num_protected_secure_cookies++;
331 break;
332 case COOKIE_PRIORITY_MEDIUM:
333 case COOKIE_PRIORITY_HIGH:
334 if (cookie_priority <= priority)
335 num_protected_secure_cookies++;
336 break;
337 }
338 }
339
340 return num_protected_secure_cookies;
341 }
342
343 bool IsCookieEligibleForEviction(CookiePriority current_priority_level, 312 bool IsCookieEligibleForEviction(CookiePriority current_priority_level,
344 bool protect_secure_cookies, 313 bool protect_secure_cookies,
345 const CanonicalCookie* cookie) { 314 const CanonicalCookie* cookie) {
346 if (!cookie->IsSecure() || !protect_secure_cookies) 315 if (cookie->Priority() == current_priority_level && protect_secure_cookies)
347 return cookie->Priority() <= current_priority_level; 316 return !cookie->IsSecure();
348 317
349 // Special consideration has to be given for low-priority secure cookies since 318 return cookie->Priority() == current_priority_level;
350 // they are given lower prority than non-secure medium-priority and non-secure 319 }
351 // high-priority cookies. Thus, low-priority secure cookies may be evicted at
352 // a medium and high value of |current_priority_level|. Put another way,
353 // low-priority secure cookies are only protected when the current priority
354 // level is low.
355 if (current_priority_level == COOKIE_PRIORITY_LOW)
356 return false;
357 320
358 return cookie->Priority() == COOKIE_PRIORITY_LOW; 321 size_t CountCookiesForPossibleDeletion(
322 CookiePriority priority,
323 const CookieMonster::CookieItVector* cookies,
324 bool protect_secure_cookies) {
325 size_t cookies_count = 0U;
326 for (const auto& cookie : *cookies) {
327 if (cookie->second->Priority() == priority) {
328 if (!protect_secure_cookies || cookie->second->IsSecure())
329 cookies_count++;
330 }
331 }
332 return cookies_count;
359 } 333 }
360 334
361 } // namespace 335 } // namespace
362 336
363 CookieMonster::CookieMonster(PersistentCookieStore* store, 337 CookieMonster::CookieMonster(PersistentCookieStore* store,
364 CookieMonsterDelegate* delegate) 338 CookieMonsterDelegate* delegate)
365 : CookieMonster( 339 : CookieMonster(
366 store, 340 store,
367 delegate, 341 delegate,
368 base::TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) {} 342 base::TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) {}
(...skipping 1552 matching lines...) Expand 10 before | Expand all | Expand 10 after
1921 1895
1922 if (cookie_its->size() > kDomainMaxCookies) { 1896 if (cookie_its->size() > kDomainMaxCookies) {
1923 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain."; 1897 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1924 size_t purge_goal = 1898 size_t purge_goal =
1925 cookie_its->size() - (kDomainMaxCookies - kDomainPurgeCookies); 1899 cookie_its->size() - (kDomainMaxCookies - kDomainPurgeCookies);
1926 DCHECK(purge_goal > kDomainPurgeCookies); 1900 DCHECK(purge_goal > kDomainPurgeCookies);
1927 1901
1928 // Sort the cookies by access date, from least-recent to most-recent. 1902 // Sort the cookies by access date, from least-recent to most-recent.
1929 std::sort(cookie_its->begin(), cookie_its->end(), LRACookieSorter); 1903 std::sort(cookie_its->begin(), cookie_its->end(), LRACookieSorter);
1930 1904
1931 size_t additional_quota_low = kDomainCookiesQuotaLow;
1932 size_t additional_quota_medium = kDomainCookiesQuotaMedium;
1933 size_t additional_quota_high = kDomainCookiesQuotaHigh;
1934
1935 // Remove all but the kDomainCookiesQuotaLow most-recently accessed 1905 // Remove all but the kDomainCookiesQuotaLow most-recently accessed
1936 // cookies with low-priority. Then, if cookies still need to be removed, 1906 // cookies with low-priority. Then, if cookies still need to be removed,
1937 // bump the quota and remove low- and medium-priority. Then, if cookies 1907 // bump the quota and remove low- and medium-priority. Then, if cookies
1938 // _still_ need to be removed, bump the quota and remove cookies with 1908 // _still_ need to be removed, bump the quota and remove cookies with
1939 // any priority. 1909 // any priority.
1940 // 1910 //
1941 // 1. Low-priority non-secure cookies. 1911 // 1. Low-priority non-secure cookies.
1942 // 2. Low-priority secure cookies. 1912 // 2. Low-priority secure cookies.
1943 // 3. Medium-priority non-secure cookies. 1913 // 3. Medium-priority non-secure cookies.
1944 // 4. High-priority non-secure cookies. 1914 // 4. High-priority non-secure cookies.
(...skipping 17 matching lines...) Expand all
1962 {COOKIE_PRIORITY_HIGH, false}, 1932 {COOKIE_PRIORITY_HIGH, false},
1963 }; 1933 };
1964 1934
1965 size_t quota = 0; 1935 size_t quota = 0;
1966 for (const auto& purge_round : purge_rounds) { 1936 for (const auto& purge_round : purge_rounds) {
1967 // Only observe the non-secure purge rounds if strict secure cookies is 1937 // Only observe the non-secure purge rounds if strict secure cookies is
1968 // enabled. 1938 // enabled.
1969 if (!enforce_strict_secure && purge_round.protect_secure_cookies) 1939 if (!enforce_strict_secure && purge_round.protect_secure_cookies)
1970 continue; 1940 continue;
1971 1941
1972 // Only adjust the quota if the round is executing, otherwise it is 1942 // Adjust quota according to the priority of cookies. Each round should
1973 // necesary to delay quota adjustments until a later round. This is 1943 // protect certain number of cookies in order to avoid starvation.
1974 // because if the high priority, non-secure round is skipped, its quota 1944 // For example, when each round starts to remove cookies, the number of
1975 // should not count until the later high priority, full round later. 1945 // cookies of that priority are counted and a decision whether they
1976 size_t* additional_quota = nullptr; 1946 // should be deleted or not is made. If yes, some number of cookies of
1947 // that priority are deleted considering the quota.
1977 switch (purge_round.priority) { 1948 switch (purge_round.priority) {
1978 case COOKIE_PRIORITY_LOW: 1949 case COOKIE_PRIORITY_LOW:
1979 additional_quota = &additional_quota_low; 1950 quota = kDomainCookiesQuotaLow;
1980 break; 1951 break;
1981 case COOKIE_PRIORITY_MEDIUM: 1952 case COOKIE_PRIORITY_MEDIUM:
1982 additional_quota = &additional_quota_medium; 1953 quota = kDomainCookiesQuotaMedium;
1983 break; 1954 break;
1984 case COOKIE_PRIORITY_HIGH: 1955 case COOKIE_PRIORITY_HIGH:
1985 additional_quota = &additional_quota_high; 1956 quota = kDomainCookiesQuotaHigh;
1986 break; 1957 break;
1987 } 1958 }
1988 quota += *additional_quota;
1989 *additional_quota = 0u;
1990 size_t just_deleted = 0u; 1959 size_t just_deleted = 0u;
1991
1992 // Purge up to |purge_goal| for all cookies at the given priority. This 1960 // Purge up to |purge_goal| for all cookies at the given priority. This
1993 // path will always execute if strict secure cookies is disabled since 1961 // path will always execute if strict secure cookies is disabled since
1994 // |purge_goal| must be positive because of the for-loop guard. If 1962 // |purge_goal| must be positive because of the for-loop guard. If
1995 // strict secure cookies is enabled, this path will be taken only if the 1963 // strict secure cookies is enabled, this path will be taken only if the
1996 // initial non-secure purge did not evict enough cookies. 1964 // initial non-secure purge did not evict enough cookies.
1997 if (purge_goal > 0) { 1965 if (purge_goal > 0) {
1998 just_deleted = PurgeLeastRecentMatches( 1966 just_deleted = PurgeLeastRecentMatches(
1999 cookie_its, purge_round.priority, quota, purge_goal, 1967 cookie_its, purge_round.priority, quota, purge_goal,
2000 purge_round.protect_secure_cookies); 1968 purge_round.protect_secure_cookies);
2001 DCHECK_LE(just_deleted, purge_goal); 1969 DCHECK_LE(just_deleted, purge_goal);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
2051 return num_deleted; 2019 return num_deleted;
2052 } 2020 }
2053 2021
2054 size_t CookieMonster::PurgeLeastRecentMatches(CookieItVector* cookies, 2022 size_t CookieMonster::PurgeLeastRecentMatches(CookieItVector* cookies,
2055 CookiePriority priority, 2023 CookiePriority priority,
2056 size_t to_protect, 2024 size_t to_protect,
2057 size_t purge_goal, 2025 size_t purge_goal,
2058 bool protect_secure_cookies) { 2026 bool protect_secure_cookies) {
2059 DCHECK(thread_checker_.CalledOnValidThread()); 2027 DCHECK(thread_checker_.CalledOnValidThread());
2060 2028
2061 // Find the first protected cookie by walking down from the end of the list 2029 // 1. Count number of the cookies at |priority|
2062 // cookie list (most-recently accessed) until |to_protect| cookies that match 2030 size_t cookies_count_possibly_to_be_deleted = CountCookiesForPossibleDeletion(
2063 // |priority| are found. 2031 priority, cookies, false /* count all cookies */);
2064 // 2032
2065 // If |protect_secure_cookies| is true, do a first pass that counts eligible 2033 // 2. If |cookies_count_possibly_to_be_deleted| at |priority| is less than or
2066 // secure cookies at the specified priority as protected. 2034 // equal |to_protect|, skip round in order to preserve the quota. This
2035 // involves secure and non-secure cookies at |priority|.
2036 if (cookies_count_possibly_to_be_deleted <= to_protect)
2037 return 0u;
2038
2039 // 3. Calculate number of secure cookies at |priority|
2040 // and number of cookies at |priority| that can possibly be deleted.
2041 // It is guaranteed we do not delete more than |purge_goal| even if
2042 // |cookies_count_possibly_to_be_deleted| is higher.
2043 size_t secure_cookies = 0u;
2067 if (protect_secure_cookies) { 2044 if (protect_secure_cookies) {
2068 to_protect -= std::min( 2045 secure_cookies = CountCookiesForPossibleDeletion(
2069 to_protect, CountProtectedSecureCookiesAtPriority(priority, cookies)); 2046 priority, cookies, protect_secure_cookies /* count secure cookies */);
2047 cookies_count_possibly_to_be_deleted -=
2048 std::max(secure_cookies, to_protect - secure_cookies);
2049 } else {
2050 cookies_count_possibly_to_be_deleted -= to_protect;
2070 } 2051 }
2071 2052
2072 size_t protection_boundary = cookies->size(); 2053 size_t removed = 0u;
2073 while (to_protect > 0 && protection_boundary > 0) { 2054 size_t current = 0u;
2074 protection_boundary--; 2055 while ((removed < purge_goal && current < cookies->size()) &&
2075 if (cookies->at(protection_boundary)->second->Priority() <= priority) 2056 cookies_count_possibly_to_be_deleted > 0) {
2076 to_protect--;
2077 }
2078
2079 // Now, walk up from the beginning of the list (least-recently accessed) until
2080 // |purge_goal| cookies are removed, or the iterator hits
2081 // |protection_boundary|.
2082 size_t removed = 0;
2083 size_t current = 0;
2084 while (removed < purge_goal && current < protection_boundary) {
2085 const CanonicalCookie* current_cookie = cookies->at(current)->second; 2057 const CanonicalCookie* current_cookie = cookies->at(current)->second;
2086 // Only delete the current cookie if the priority is less than or equal to 2058 // Only delete the current cookie if the priority is equal to
2087 // the current level. If it is equal to the current level, and secure 2059 // the current level.
2088 // cookies are protected, only delete it if it is not secure.
2089 if (IsCookieEligibleForEviction(priority, protect_secure_cookies, 2060 if (IsCookieEligibleForEviction(priority, protect_secure_cookies,
2090 current_cookie)) { 2061 current_cookie)) {
2091 InternalDeleteCookie(cookies->at(current), true, 2062 InternalDeleteCookie(cookies->at(current), true,
2092 DELETE_COOKIE_EVICTED_DOMAIN); 2063 DELETE_COOKIE_EVICTED_DOMAIN);
2093 cookies->erase(cookies->begin() + current); 2064 cookies->erase(cookies->begin() + current);
2094 removed++; 2065 removed++;
2095 2066 cookies_count_possibly_to_be_deleted--;
2096 // The call to 'erase' above shifts the contents of the vector, but
2097 // doesn't shift |protection_boundary|. Decrement that here to ensure that
2098 // the correct set of cookies is protected.
2099 protection_boundary--;
2100 } else { 2067 } else {
2101 current++; 2068 current++;
2102 } 2069 }
2103 } 2070 }
2104 return removed; 2071 return removed;
2105 } 2072 }
2106 2073
2107 size_t CookieMonster::GarbageCollectExpired(const Time& current, 2074 size_t CookieMonster::GarbageCollectExpired(const Time& current,
2108 const CookieMapItPair& itpair, 2075 const CookieMapItPair& itpair,
2109 CookieItVector* cookie_its) { 2076 CookieItVector* cookie_its) {
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
2427 it != hook_map_.end(); ++it) { 2394 it != hook_map_.end(); ++it) {
2428 std::pair<GURL, std::string> key = it->first; 2395 std::pair<GURL, std::string> key = it->first;
2429 if (cookie.IncludeForRequestURL(key.first, opts) && 2396 if (cookie.IncludeForRequestURL(key.first, opts) &&
2430 cookie.Name() == key.second) { 2397 cookie.Name() == key.second) {
2431 it->second->Notify(cookie, removed); 2398 it->second->Notify(cookie, removed);
2432 } 2399 }
2433 } 2400 }
2434 } 2401 }
2435 2402
2436 } // namespace net 2403 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/cookies/cookie_monster_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698