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

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

Issue 1767513004: Clean up cookie eviction. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mmenke@ Created 4 years, 9 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 | « net/cookies/cookie_monster.h ('k') | 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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 // sorts by creation time (oldest first). 155 // sorts by creation time (oldest first).
156 // The RFC says the sort order for the domain attribute is undefined. 156 // The RFC says the sort order for the domain attribute is undefined.
157 bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) { 157 bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
158 if (cc1->Path().length() == cc2->Path().length()) 158 if (cc1->Path().length() == cc2->Path().length())
159 return cc1->CreationDate() < cc2->CreationDate(); 159 return cc1->CreationDate() < cc2->CreationDate();
160 return cc1->Path().length() > cc2->Path().length(); 160 return cc1->Path().length() > cc2->Path().length();
161 } 161 }
162 162
163 bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1, 163 bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
164 const CookieMonster::CookieMap::iterator& it2) { 164 const CookieMonster::CookieMap::iterator& it2) {
165 // Cookies accessed less recently should be deleted first.
166 if (it1->second->LastAccessDate() != it2->second->LastAccessDate()) 165 if (it1->second->LastAccessDate() != it2->second->LastAccessDate())
167 return it1->second->LastAccessDate() < it2->second->LastAccessDate(); 166 return it1->second->LastAccessDate() < it2->second->LastAccessDate();
168 167
169 // In rare cases we might have two cookies with identical last access times. 168 // Ensure stability for == last access times by falling back to creation.
170 // To preserve the stability of the sort, in these cases prefer to delete
171 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
172 return it1->second->CreationDate() < it2->second->CreationDate(); 169 return it1->second->CreationDate() < it2->second->CreationDate();
173 } 170 }
174 171
175 // Compare cookies using name, domain and path, so that "equivalent" cookies 172 // Compare cookies using name, domain and path, so that "equivalent" cookies
176 // (per RFC 2965) are equal to each other. 173 // (per RFC 2965) are equal to each other.
177 bool PartialDiffCookieSorter(const CanonicalCookie& a, 174 bool PartialDiffCookieSorter(const CanonicalCookie& a,
178 const CanonicalCookie& b) { 175 const CanonicalCookie& b) {
179 return a.PartialCompare(b); 176 return a.PartialCompare(b);
180 } 177 }
181 178
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 CookieMonster::CookieItVector* non_secure_cookie_its) { 240 CookieMonster::CookieItVector* non_secure_cookie_its) {
244 DCHECK(secure_cookie_its && non_secure_cookie_its); 241 DCHECK(secure_cookie_its && non_secure_cookie_its);
245 for (const auto& curit : cookie_its) { 242 for (const auto& curit : cookie_its) {
246 if (curit->second->IsSecure()) 243 if (curit->second->IsSecure())
247 secure_cookie_its->push_back(curit); 244 secure_cookie_its->push_back(curit);
248 else 245 else
249 non_secure_cookie_its->push_back(curit); 246 non_secure_cookie_its->push_back(curit);
250 } 247 }
251 } 248 }
252 249
253 // Predicate to support PartitionCookieByPriority().
254 struct CookiePriorityEqualsTo {
255 explicit CookiePriorityEqualsTo(CookiePriority priority)
256 : priority_(priority) {}
257
258 bool operator()(const CookieMonster::CookieMap::iterator it) const {
259 return it->second->Priority() == priority_;
260 }
261
262 const CookiePriority priority_;
263 };
264
265 // For a CookieItVector iterator range [|it_begin|, |it_end|),
266 // moves all cookies with a given |priority| to the beginning of the list.
267 // Returns: An iterator in [it_begin, it_end) to the first element with
268 // priority != |priority|, or |it_end| if all have priority == |priority|.
269 CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
270 CookieMonster::CookieItVector::iterator it_begin,
271 CookieMonster::CookieItVector::iterator it_end,
272 CookiePriority priority) {
273 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
274 }
275
276 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it, 250 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it,
277 const Time& access_date) { 251 const Time& access_date) {
278 return it->second->LastAccessDate() < access_date; 252 return it->second->LastAccessDate() < access_date;
279 } 253 }
280 254
281 // For a CookieItVector iterator range [|it_begin|, |it_end|) 255 // For a CookieItVector iterator range [|it_begin|, |it_end|)
282 // from a CookieItVector sorted by LastAccessDate(), returns the 256 // from a CookieItVector sorted by LastAccessDate(), returns the
283 // first iterator with access date >= |access_date|, or cookie_its_end if this 257 // first iterator with access date >= |access_date|, or cookie_its_end if this
284 // holds for all. 258 // holds for all.
285 CookieMonster::CookieItVector::iterator LowerBoundAccessDate( 259 CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
(...skipping 1605 matching lines...) Expand 10 before | Expand all | Expand 10 after
1891 if (cookies_.count(key) > kDomainMaxCookies) { 1865 if (cookies_.count(key) > kDomainMaxCookies) {
1892 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key; 1866 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
1893 1867
1894 CookieItVector* cookie_its; 1868 CookieItVector* cookie_its;
1895 1869
1896 CookieItVector non_expired_cookie_its; 1870 CookieItVector non_expired_cookie_its;
1897 cookie_its = &non_expired_cookie_its; 1871 cookie_its = &non_expired_cookie_its;
1898 num_deleted += 1872 num_deleted +=
1899 GarbageCollectExpired(current, cookies_.equal_range(key), cookie_its); 1873 GarbageCollectExpired(current, cookies_.equal_range(key), cookie_its);
1900 1874
1875 // TODO(mkwst): Soften this.
1901 CookieItVector secure_cookie_its; 1876 CookieItVector secure_cookie_its;
1902 if (enforce_strict_secure && cookie_its->size() > kDomainMaxCookies) { 1877 if (enforce_strict_secure && cookie_its->size() > kDomainMaxCookies) {
1903 VLOG(kVlogGarbageCollection) << "Garbage collecting non-Secure cookies."; 1878 VLOG(kVlogGarbageCollection) << "Garbage collecting non-Secure cookies.";
1904 num_deleted += 1879 num_deleted +=
1905 GarbageCollectNonSecure(non_expired_cookie_its, &secure_cookie_its); 1880 GarbageCollectNonSecure(non_expired_cookie_its, &secure_cookie_its);
1906 cookie_its = &secure_cookie_its; 1881 cookie_its = &secure_cookie_its;
1907 } 1882 }
1908 1883
1909 if (cookie_its->size() > kDomainMaxCookies) { 1884 if (cookie_its->size() > kDomainMaxCookies) {
1910 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain."; 1885 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1911 size_t purge_goal = 1886 size_t purge_goal =
1912 cookie_its->size() - (kDomainMaxCookies - kDomainPurgeCookies); 1887 cookie_its->size() - (kDomainMaxCookies - kDomainPurgeCookies);
1913 DCHECK(purge_goal > kDomainPurgeCookies); 1888 DCHECK(purge_goal > kDomainPurgeCookies);
1914 1889
1915 // Boundary iterators into |cookie_its| for different priorities. 1890 // Sort the cookies by access date, from least-recent to most-recent.
1916 CookieItVector::iterator it_bdd[4]; 1891 std::sort(cookie_its->begin(), cookie_its->end(), LRACookieSorter);
1917 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1918 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1919 it_bdd[0] = cookie_its->begin();
1920 it_bdd[3] = cookie_its->end();
1921 it_bdd[1] =
1922 PartitionCookieByPriority(it_bdd[0], it_bdd[3], COOKIE_PRIORITY_LOW);
1923 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
1924 COOKIE_PRIORITY_MEDIUM);
1925 size_t quota[3] = {kDomainCookiesQuotaLow,
1926 kDomainCookiesQuotaMedium,
1927 kDomainCookiesQuotaHigh};
1928 1892
1929 // Purge domain cookies in 3 rounds. 1893 // Remove all but the kDomainCookiesQuotaLow most-recently accessed
1930 // Round 1: consider low-priority cookies only: evict least-recently 1894 // cookies with low-priority. Then, if cookies still need to be removed,
1931 // accessed, while protecting quota[0] of these from deletion. 1895 // bump the quota and remove low- and medium-priority. Then, if cookies
1932 // Round 2: consider {low, medium}-priority cookies, evict least-recently 1896 // _still_ need to be removed, bump the quota and remove cookies with
1933 // accessed, while protecting quota[0] + quota[1]. 1897 // any priority.
1934 // Round 3: consider all cookies, evict least-recently accessed. 1898 const size_t kQuotas[3] = {kDomainCookiesQuotaLow,
1935 size_t accumulated_quota = 0; 1899 kDomainCookiesQuotaMedium,
1936 CookieItVector::iterator it_purge_begin = it_bdd[0]; 1900 kDomainCookiesQuotaHigh};
1937 for (int i = 0; i < 3 && purge_goal > 0; ++i) { 1901 size_t quota = 0;
1938 accumulated_quota += quota[i]; 1902 for (size_t i = 0; i < arraysize(kQuotas) && purge_goal > 0; i++) {
1903 quota += kQuotas[i];
1904 size_t just_deleted =
1905 PurgeLeastRecentMatches(cookie_its, static_cast<CookiePriority>(i),
1906 quota, purge_goal, safe_date);
1907 DCHECK_LE(just_deleted, purge_goal);
1908 purge_goal -= just_deleted;
1909 num_deleted += just_deleted;
1910 }
1939 1911
1940 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1941 if (num_considered <= accumulated_quota)
1942 continue;
1943
1944 // Number of cookies that will be purged in this round.
1945 size_t round_goal =
1946 std::min(purge_goal, num_considered - accumulated_quota);
1947 purge_goal -= round_goal;
1948
1949 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1950 // Cookies accessed on or after |safe_date| would have been safe from
1951 // global purge, and we want to keep track of this.
1952 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1953 CookieItVector::iterator it_purge_middle =
1954 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1955 // Delete cookies accessed before |safe_date|.
1956 num_deleted += GarbageCollectDeleteRange(
1957 current, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE, it_purge_begin,
1958 it_purge_middle);
1959 // Delete cookies accessed on or after |safe_date|.
1960 num_deleted += GarbageCollectDeleteRange(
1961 current, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE, it_purge_middle,
1962 it_purge_end);
1963 it_purge_begin = it_purge_end;
1964 }
1965 DCHECK_EQ(0U, purge_goal); 1912 DCHECK_EQ(0U, purge_goal);
1966 } 1913 }
1967 } 1914 }
1968 1915
1969 // Collect garbage for everything. With firefox style we want to preserve 1916 // Collect garbage for everything. With firefox style we want to preserve
1970 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict. 1917 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
1971 if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) { 1918 if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) {
1972 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything"; 1919 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
1973 CookieItVector cookie_its; 1920 CookieItVector cookie_its;
1974 1921
(...skipping 27 matching lines...) Expand all
2002 } else { 1949 } else {
2003 num_deleted += GarbageCollectLeastRecentlyAccessed( 1950 num_deleted += GarbageCollectLeastRecentlyAccessed(
2004 current, safe_date, purge_goal, cookie_its); 1951 current, safe_date, purge_goal, cookie_its);
2005 } 1952 }
2006 } 1953 }
2007 } 1954 }
2008 1955
2009 return num_deleted; 1956 return num_deleted;
2010 } 1957 }
2011 1958
1959 size_t CookieMonster::PurgeLeastRecentMatches(CookieItVector* cookies,
1960 CookiePriority priority,
1961 size_t to_protect,
1962 size_t purge_goal,
1963 const base::Time& safe_date) {
1964 DCHECK(thread_checker_.CalledOnValidThread());
1965
1966 // Find the first protected cookie by walking down from the end of the list
1967 // cookie list (most-recently accessed) until |to_protect| cookies that match
1968 // |priority| are found.
1969 size_t protection_boundary = cookies->size();
1970 while (to_protect > 0 && protection_boundary > 0) {
1971 protection_boundary--;
1972 if (cookies->at(protection_boundary)->second->Priority() <= priority)
1973 to_protect--;
1974 }
1975
1976 // Now, walk up from the beginning of the list (least-recently accessed) until
1977 // |purge_goal| cookies are removed, or the iterator hits
1978 // |protection_boundary|.
1979 size_t removed = 0;
1980 size_t current = 0;
1981 while (removed < purge_goal && current < protection_boundary) {
1982 if (cookies->at(current)->second->Priority() <= priority) {
1983 InternalDeleteCookie(
1984 cookies->at(current), true,
1985 cookies->at(current)->second->LastAccessDate() > safe_date
1986 ? DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
1987 : DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE);
1988 cookies->erase(cookies->begin() + current);
1989 removed++;
1990
1991 // The call to 'erase' above shifts the contents of the vector, but
1992 // doesn't shift |protection_boundary|. Decrement that here to ensure that
1993 // the correct set of cookies is protected.
1994 protection_boundary--;
1995 } else {
1996 current++;
1997 }
1998 }
1999 return removed;
2000 }
2001
2012 size_t CookieMonster::GarbageCollectExpired(const Time& current, 2002 size_t CookieMonster::GarbageCollectExpired(const Time& current,
2013 const CookieMapItPair& itpair, 2003 const CookieMapItPair& itpair,
2014 CookieItVector* cookie_its) { 2004 CookieItVector* cookie_its) {
2015 DCHECK(thread_checker_.CalledOnValidThread()); 2005 DCHECK(thread_checker_.CalledOnValidThread());
2016 2006
2017 int num_deleted = 0; 2007 int num_deleted = 0;
2018 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { 2008 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2019 CookieMap::iterator curit = it; 2009 CookieMap::iterator curit = it;
2020 ++it; 2010 ++it;
2021 2011
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
2349 it != hook_map_.end(); ++it) { 2339 it != hook_map_.end(); ++it) {
2350 std::pair<GURL, std::string> key = it->first; 2340 std::pair<GURL, std::string> key = it->first;
2351 if (cookie.IncludeForRequestURL(key.first, opts) && 2341 if (cookie.IncludeForRequestURL(key.first, opts) &&
2352 cookie.Name() == key.second) { 2342 cookie.Name() == key.second) {
2353 it->second->Notify(cookie, removed); 2343 it->second->Notify(cookie, removed);
2354 } 2344 }
2355 } 2345 }
2356 } 2346 }
2357 2347
2358 } // namespace net 2348 } // namespace net
OLDNEW
« no previous file with comments | « net/cookies/cookie_monster.h ('k') | net/cookies/cookie_monster_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698