OLD | NEW |
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 namespace net { | 105 namespace net { |
106 | 106 |
107 // See comments at declaration of these variables in cookie_monster.h | 107 // See comments at declaration of these variables in cookie_monster.h |
108 // for details. | 108 // for details. |
109 const size_t CookieMonster::kDomainMaxCookies = 180; | 109 const size_t CookieMonster::kDomainMaxCookies = 180; |
110 const size_t CookieMonster::kDomainPurgeCookies = 30; | 110 const size_t CookieMonster::kDomainPurgeCookies = 30; |
111 const size_t CookieMonster::kMaxCookies = 3300; | 111 const size_t CookieMonster::kMaxCookies = 3300; |
112 const size_t CookieMonster::kPurgeCookies = 300; | 112 const size_t CookieMonster::kPurgeCookies = 300; |
113 | 113 |
114 const double CookieMonster::kDomainCookiesQuotaLow = 0.2; | 114 const size_t CookieMonster::kDomainCookiesQuotaLow = 30; |
115 const double CookieMonster::kDomainCookiesQuotaMedium = 0.333333333333333333333; | 115 const size_t CookieMonster::kDomainCookiesQuotaMedium = 50; |
116 const double CookieMonster::kDomainCookiesQuotaHigh = | 116 const size_t CookieMonster::kDomainCookiesQuotaHigh = |
117 1 - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium; | 117 kDomainMaxCookies - kDomainPurgeCookies - kDomainCookiesQuotaLow - |
| 118 kDomainCookiesQuotaMedium; |
118 | 119 |
119 const int CookieMonster::kSafeFromGlobalPurgeDays = 30; | 120 const int CookieMonster::kSafeFromGlobalPurgeDays = 30; |
120 | 121 |
121 namespace { | 122 namespace { |
122 | 123 |
123 bool ContainsControlCharacter(const std::string& s) { | 124 bool ContainsControlCharacter(const std::string& s) { |
124 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { | 125 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { |
125 if ((*i >= 0) && (*i <= 31)) | 126 if ((*i >= 0) && (*i <= 31)) |
126 return true; | 127 return true; |
127 } | 128 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 CookieMonster::CookieItVector* non_secure_cookie_its) { | 243 CookieMonster::CookieItVector* non_secure_cookie_its) { |
243 DCHECK(secure_cookie_its && non_secure_cookie_its); | 244 DCHECK(secure_cookie_its && non_secure_cookie_its); |
244 for (const auto& curit : cookie_its) { | 245 for (const auto& curit : cookie_its) { |
245 if (curit->second->IsSecure()) | 246 if (curit->second->IsSecure()) |
246 secure_cookie_its->push_back(curit); | 247 secure_cookie_its->push_back(curit); |
247 else | 248 else |
248 non_secure_cookie_its->push_back(curit); | 249 non_secure_cookie_its->push_back(curit); |
249 } | 250 } |
250 } | 251 } |
251 | 252 |
| 253 // Predicate to support PartitionCookieByPriority(). |
| 254 struct CookiePriorityEqualsTo |
| 255 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> { |
| 256 explicit CookiePriorityEqualsTo(CookiePriority priority) |
| 257 : priority_(priority) {} |
| 258 |
| 259 bool operator()(const CookieMonster::CookieMap::iterator it) const { |
| 260 return it->second->Priority() == priority_; |
| 261 } |
| 262 |
| 263 const CookiePriority priority_; |
| 264 }; |
| 265 |
252 // For a CookieItVector iterator range [|it_begin|, |it_end|), | 266 // For a CookieItVector iterator range [|it_begin|, |it_end|), |
253 // moves all cookies with a given |priority| to the beginning of the list. | 267 // moves all cookies with a given |priority| to the beginning of the list. |
254 // Returns: An iterator in [it_begin, it_end) to the first element with | 268 // Returns: An iterator in [it_begin, it_end) to the first element with |
255 // priority != |priority|, or |it_end| if all have priority == |priority|. | 269 // priority != |priority|, or |it_end| if all have priority == |priority|. |
256 CookieMonster::CookieItVector::iterator PartitionCookieByPriority( | 270 CookieMonster::CookieItVector::iterator PartitionCookieByPriority( |
257 CookieMonster::CookieItVector::iterator it_begin, | 271 CookieMonster::CookieItVector::iterator it_begin, |
258 CookieMonster::CookieItVector::iterator it_end, | 272 CookieMonster::CookieItVector::iterator it_end, |
259 CookiePriority priority) { | 273 CookiePriority priority) { |
260 return std::partition( | 274 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority)); |
261 it_begin, it_end, | |
262 [priority](const CookieMonster::CookieMap::iterator& it) { | |
263 return it->second->Priority() == priority; | |
264 }); | |
265 } | 275 } |
266 | 276 |
267 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it, | 277 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it, |
268 const Time& access_date) { | 278 const Time& access_date) { |
269 return it->second->LastAccessDate() < access_date; | 279 return it->second->LastAccessDate() < access_date; |
270 } | 280 } |
271 | 281 |
272 // For a CookieItVector iterator range [|it_begin|, |it_end|) | 282 // For a CookieItVector iterator range [|it_begin|, |it_end|) |
273 // from a CookieItVector sorted by LastAccessDate(), returns the | 283 // from a CookieItVector sorted by LastAccessDate(), returns the |
274 // first iterator with access date >= |access_date|, or cookie_its_end if this | 284 // first iterator with access date >= |access_date|, or cookie_its_end if this |
(...skipping 1648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1923 bool enforce_strict_secure) { | 1933 bool enforce_strict_secure) { |
1924 lock_.AssertAcquired(); | 1934 lock_.AssertAcquired(); |
1925 | 1935 |
1926 size_t num_deleted = 0; | 1936 size_t num_deleted = 0; |
1927 Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays)); | 1937 Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays)); |
1928 | 1938 |
1929 // Collect garbage for this key, minding cookie priorities. | 1939 // Collect garbage for this key, minding cookie priorities. |
1930 if (cookies_.count(key) > kDomainMaxCookies) { | 1940 if (cookies_.count(key) > kDomainMaxCookies) { |
1931 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key; | 1941 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key; |
1932 | 1942 |
1933 CookieItVector cookie_its; | 1943 CookieItVector* cookie_its; |
1934 | 1944 |
1935 // First, we purge all expired cookies for this key (regardless of our | 1945 CookieItVector non_expired_cookie_its; |
1936 // target number of cookies, as we have no need to keep expired cookies | 1946 cookie_its = &non_expired_cookie_its; |
1937 // around). | |
1938 num_deleted += | 1947 num_deleted += |
1939 GarbageCollectExpired(current, cookies_.equal_range(key), &cookie_its); | 1948 GarbageCollectExpired(current, cookies_.equal_range(key), cookie_its); |
1940 | 1949 |
1941 if (cookie_its.size() > kDomainMaxCookies) { | 1950 CookieItVector secure_cookie_its; |
1942 // Then, if we're over the maximum allowed for this key, we'll remove | 1951 if (enforce_strict_secure && cookie_its->size() > kDomainMaxCookies) { |
1943 // cookies in the following order: | 1952 VLOG(kVlogGarbageCollection) << "Garbage collecting non-Secure cookies."; |
1944 // | 1953 num_deleted += |
1945 // 1. Low-priority non-secure cookies. | 1954 GarbageCollectNonSecure(non_expired_cookie_its, &secure_cookie_its); |
1946 // 2. Medium-priority non-secure cookies. | 1955 cookie_its = &secure_cookie_its; |
1947 // 3. High-priority non-secure cookies. | 1956 } |
1948 // 4. Low-priority secure cookies. | |
1949 // 5. Medium-priority secure cookies. | |
1950 // 6. High-priority secure cookies. | |
1951 // | |
1952 // Note that this implies that _all_ non-secure cookies will be removed | |
1953 // before _any_ secure cookie is removed, in accordance with | |
1954 // https://tools.ietf.org/html/draft-west-leave-secure-cookies-alone | |
1955 | 1957 |
| 1958 if (cookie_its->size() > kDomainMaxCookies) { |
1956 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain."; | 1959 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain."; |
1957 size_t purge_goal = | 1960 size_t purge_goal = |
1958 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies); | 1961 cookie_its->size() - (kDomainMaxCookies - kDomainPurgeCookies); |
1959 DCHECK(purge_goal > kDomainPurgeCookies); | 1962 DCHECK(purge_goal > kDomainPurgeCookies); |
1960 | 1963 |
1961 // To execute the above removals, we'll divide |cookies_its| into 6 | 1964 // Boundary iterators into |cookie_its| for different priorities. |
1962 // partitions, using 7 boundaries (note that the last boundary is off the | 1965 CookieItVector::iterator it_bdd[4]; |
1963 // end of the list): | 1966 // Intialize |it_bdd| while sorting |cookie_its| by priorities. |
1964 // | 1967 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries. |
1965 // If both non-secure and secure cookies are present, the list will look | 1968 it_bdd[0] = cookie_its->begin(); |
1966 // like this: | 1969 it_bdd[3] = cookie_its->end(); |
1967 // | 1970 it_bdd[1] = |
1968 // LLLLMMMMHHHHHLLLLMMMMHHHH | 1971 PartitionCookieByPriority(it_bdd[0], it_bdd[3], COOKIE_PRIORITY_LOW); |
1969 // ^ ^ ^ ^ ^ ^ ^ | 1972 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3], |
1970 // 0 1 2 3 4 5 6 | 1973 COOKIE_PRIORITY_MEDIUM); |
1971 // | 1974 size_t quota[3] = {kDomainCookiesQuotaLow, |
1972 // If only secure cookies are present, the list will look like this: | 1975 kDomainCookiesQuotaMedium, |
1973 // | 1976 kDomainCookiesQuotaHigh}; |
1974 // LLLLMMMMHHHHH | |
1975 // ^ ^ ^ ^ | |
1976 // 0 4 5 6 | |
1977 // 1 | |
1978 // 2 | |
1979 // 3 | |
1980 // | |
1981 // If only non-secure cookies are present, the list will look like this: | |
1982 // | |
1983 // LLLLMMMMHHHHH | |
1984 // ^ ^ ^ ^ | |
1985 // 0 1 2 3 | |
1986 // 4 | |
1987 // 5 | |
1988 // 6 | |
1989 CookieItVector::iterator it_bdd[7]; | |
1990 it_bdd[0] = cookie_its.begin(); | |
1991 it_bdd[1] = it_bdd[0]; | |
1992 it_bdd[2] = it_bdd[0]; | |
1993 it_bdd[3] = cookie_its.end(); | |
1994 it_bdd[4] = it_bdd[3]; | |
1995 it_bdd[5] = it_bdd[3]; | |
1996 it_bdd[6] = it_bdd[3]; | |
1997 | 1977 |
1998 size_t num_nonsecure = 0; | 1978 // Purge domain cookies in 3 rounds. |
| 1979 // Round 1: consider low-priority cookies only: evict least-recently |
| 1980 // accessed, while protecting quota[0] of these from deletion. |
| 1981 // Round 2: consider {low, medium}-priority cookies, evict least-recently |
| 1982 // accessed, while protecting quota[0] + quota[1]. |
| 1983 // Round 3: consider all cookies, evict least-recently accessed. |
| 1984 size_t accumulated_quota = 0; |
| 1985 CookieItVector::iterator it_purge_begin = it_bdd[0]; |
| 1986 for (int i = 0; i < 3 && purge_goal > 0; ++i) { |
| 1987 accumulated_quota += quota[i]; |
1999 | 1988 |
2000 // Move all non-secure cookies to the front of the list, and set boundary | 1989 size_t num_considered = it_bdd[i + 1] - it_purge_begin; |
2001 // #3 to the first secure cookie (or off the end of the list, in the case | 1990 if (num_considered <= accumulated_quota) |
2002 // where no secure cookies are present). | 1991 continue; |
2003 it_bdd[3] = std::partition(it_bdd[0], it_bdd[6], | |
2004 [](const CookieMap::iterator& it) { | |
2005 return !it->second->IsSecure(); | |
2006 }); | |
2007 | 1992 |
2008 // If we have non-secure cookies, partition them into priorities: | 1993 // Number of cookies that will be purged in this round. |
2009 if (it_bdd[3] > it_bdd[0]) { | 1994 size_t round_goal = |
2010 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3], | 1995 std::min(purge_goal, num_considered - accumulated_quota); |
2011 COOKIE_PRIORITY_LOW); | 1996 purge_goal -= round_goal; |
2012 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3], | 1997 |
2013 COOKIE_PRIORITY_MEDIUM); | 1998 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal); |
2014 num_nonsecure = it_bdd[3] - it_bdd[0]; | 1999 // Cookies accessed on or after |safe_date| would have been safe from |
| 2000 // global purge, and we want to keep track of this. |
| 2001 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal; |
| 2002 CookieItVector::iterator it_purge_middle = |
| 2003 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date); |
| 2004 // Delete cookies accessed before |safe_date|. |
| 2005 num_deleted += GarbageCollectDeleteRange( |
| 2006 current, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE, it_purge_begin, |
| 2007 it_purge_middle); |
| 2008 // Delete cookies accessed on or after |safe_date|. |
| 2009 num_deleted += GarbageCollectDeleteRange( |
| 2010 current, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE, it_purge_middle, |
| 2011 it_purge_end); |
| 2012 it_purge_begin = it_purge_end; |
2015 } | 2013 } |
2016 | |
2017 // Likewise, if we have secure cookies, partition them into priorities: | |
2018 if (it_bdd[3] < it_bdd[6]) { | |
2019 it_bdd[4] = PartitionCookieByPriority(it_bdd[3], it_bdd[6], | |
2020 COOKIE_PRIORITY_LOW); | |
2021 it_bdd[5] = PartitionCookieByPriority(it_bdd[4], it_bdd[6], | |
2022 COOKIE_PRIORITY_MEDIUM); | |
2023 } | |
2024 | |
2025 // Start with the non-secure cookies. | |
2026 if (purge_goal >= num_nonsecure) { | |
2027 // If we need to purge more cookies than we have non-secure, remove | |
2028 // them all, update |purge_goal| then purge the new |purge_goal| from | |
2029 // the secure cookies. | |
2030 num_deleted += GarbageCollectDeleteRange( | |
2031 current, DELETE_COOKIE_NON_SECURE, it_bdd[0], it_bdd[3]); | |
2032 num_deleted += GarbageCollectNumFromRangeWithQuota( | |
2033 current, safe_date, purge_goal - num_deleted, it_bdd, GC_SECURE); | |
2034 } else { | |
2035 num_deleted += GarbageCollectNumFromRangeWithQuota( | |
2036 current, safe_date, purge_goal, it_bdd, GC_NONSECURE); | |
2037 } | |
2038 purge_goal -= num_deleted; | |
2039 | |
2040 DCHECK_EQ(0U, purge_goal); | 2014 DCHECK_EQ(0U, purge_goal); |
2041 } | 2015 } |
2042 } | 2016 } |
2043 | 2017 |
2044 // Collect garbage for everything. With firefox style we want to preserve | 2018 // Collect garbage for everything. With firefox style we want to preserve |
2045 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict. | 2019 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict. |
2046 if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) { | 2020 if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) { |
2047 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything"; | 2021 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything"; |
2048 CookieItVector cookie_its; | 2022 CookieItVector cookie_its; |
2049 | 2023 |
(...skipping 27 matching lines...) Expand all Loading... |
2077 } else { | 2051 } else { |
2078 num_deleted += GarbageCollectLeastRecentlyAccessed( | 2052 num_deleted += GarbageCollectLeastRecentlyAccessed( |
2079 current, safe_date, purge_goal, cookie_its); | 2053 current, safe_date, purge_goal, cookie_its); |
2080 } | 2054 } |
2081 } | 2055 } |
2082 } | 2056 } |
2083 | 2057 |
2084 return num_deleted; | 2058 return num_deleted; |
2085 } | 2059 } |
2086 | 2060 |
2087 size_t CookieMonster::GarbageCollectNumFromRangeWithQuota( | |
2088 const Time& current, | |
2089 const Time& safe_date, | |
2090 size_t purge_goal, | |
2091 CookieItVector::iterator* it_bdd, | |
2092 GCType type) { | |
2093 size_t num_deleted = 0; | |
2094 size_t begin_partition = type == GC_SECURE ? 3 : 0; | |
2095 size_t num_cookies = it_bdd[begin_partition + 3] - it_bdd[begin_partition]; | |
2096 size_t num_to_keep = num_cookies - purge_goal; | |
2097 size_t quota[3] = {std::floor(num_to_keep * kDomainCookiesQuotaLow), | |
2098 std::floor(num_to_keep * kDomainCookiesQuotaMedium), | |
2099 std::floor(num_to_keep * kDomainCookiesQuotaHigh)}; | |
2100 | |
2101 // The quota calculation will be up to 3 fewer than the number of | |
2102 // cookies we can keep. Bump up the numbers to get the right answer: | |
2103 if (quota[0] + quota[1] + quota[2] < num_to_keep) | |
2104 quota[2] += 1; | |
2105 if (quota[0] + quota[1] + quota[2] < num_to_keep) | |
2106 quota[1] += 1; | |
2107 if (quota[0] + quota[1] + quota[2] < num_to_keep) | |
2108 quota[0] += 1; | |
2109 DCHECK_EQ(num_to_keep, quota[0] + quota[1] + quota[2]); | |
2110 | |
2111 // Purge domain cookies in 3 rounds. | |
2112 // Round 1: consider low-priority cookies only: evict least-recently | |
2113 // accessed, while protecting quota[0] of these from deletion. | |
2114 // Round 2: consider {low, medium}-priority cookies, evict least-recently | |
2115 // accessed, while protecting quota[0] + quota[1]. | |
2116 // Round 3: consider all cookies, evict least-recently accessed. | |
2117 size_t accumulated_quota = 0; | |
2118 CookieItVector::iterator it_purge_begin = it_bdd[begin_partition]; | |
2119 for (size_t i = 0; i < arraysize(quota) && purge_goal > 0; ++i) { | |
2120 accumulated_quota += quota[i]; | |
2121 | |
2122 size_t num_considered = it_bdd[i + begin_partition + 1] - it_purge_begin; | |
2123 if (num_considered <= accumulated_quota) | |
2124 continue; | |
2125 | |
2126 // Number of cookies that will be purged in this round. | |
2127 size_t round_goal = | |
2128 std::min(purge_goal, num_considered - accumulated_quota); | |
2129 purge_goal -= round_goal; | |
2130 | |
2131 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + begin_partition + 1], | |
2132 round_goal); | |
2133 // Cookies accessed on or after |safe_date| would have been safe from | |
2134 // global purge, and we want to keep track of this. | |
2135 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal; | |
2136 CookieItVector::iterator it_purge_middle = | |
2137 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date); | |
2138 // Delete cookies accessed before |safe_date|. | |
2139 num_deleted += GarbageCollectDeleteRange( | |
2140 current, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE, it_purge_begin, | |
2141 it_purge_middle); | |
2142 // Delete cookies accessed on or after |safe_date|. | |
2143 num_deleted += GarbageCollectDeleteRange( | |
2144 current, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE, it_purge_middle, | |
2145 it_purge_end); | |
2146 it_purge_begin = it_purge_end; | |
2147 } | |
2148 return num_deleted; | |
2149 } | |
2150 | |
2151 size_t CookieMonster::GarbageCollectExpired(const Time& current, | 2061 size_t CookieMonster::GarbageCollectExpired(const Time& current, |
2152 const CookieMapItPair& itpair, | 2062 const CookieMapItPair& itpair, |
2153 CookieItVector* cookie_its) { | 2063 CookieItVector* cookie_its) { |
2154 lock_.AssertAcquired(); | 2064 lock_.AssertAcquired(); |
2155 | 2065 |
2156 int num_deleted = 0; | 2066 int num_deleted = 0; |
2157 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { | 2067 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { |
2158 CookieMap::iterator curit = it; | 2068 CookieMap::iterator curit = it; |
2159 ++it; | 2069 ++it; |
2160 | 2070 |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2461 it != hook_map_.end(); ++it) { | 2371 it != hook_map_.end(); ++it) { |
2462 std::pair<GURL, std::string> key = it->first; | 2372 std::pair<GURL, std::string> key = it->first; |
2463 if (cookie.IncludeForRequestURL(key.first, opts) && | 2373 if (cookie.IncludeForRequestURL(key.first, opts) && |
2464 cookie.Name() == key.second) { | 2374 cookie.Name() == key.second) { |
2465 it->second->Notify(cookie, removed); | 2375 it->second->Notify(cookie, removed); |
2466 } | 2376 } |
2467 } | 2377 } |
2468 } | 2378 } |
2469 | 2379 |
2470 } // namespace net | 2380 } // namespace net |
OLD | NEW |