Chromium Code Reviews| Index: net/cookies/cookie_monster.cc |
| diff --git a/net/cookies/cookie_monster.cc b/net/cookies/cookie_monster.cc |
| index fd97ab5c67e2bf489d4a1517ead6c2f3d117cf7d..fe9c415c7f84ffd1e462ad7e44f574ece87dd379 100644 |
| --- a/net/cookies/cookie_monster.cc |
| +++ b/net/cookies/cookie_monster.cc |
| @@ -326,13 +326,13 @@ size_t CountProtectedSecureCookiesAtPriority( |
| CookiePriority cookie_priority = cookie->second->Priority(); |
| switch (cookie_priority) { |
| case COOKIE_PRIORITY_LOW: |
| - if (priority == COOKIE_PRIORITY_LOW) |
| - num_protected_secure_cookies++; |
| + num_protected_secure_cookies++; |
| break; |
| case COOKIE_PRIORITY_MEDIUM: |
| + num_protected_secure_cookies++; |
| + break; |
| case COOKIE_PRIORITY_HIGH: |
| - if (cookie_priority <= priority) |
| - num_protected_secure_cookies++; |
| + num_protected_secure_cookies++; |
| break; |
| } |
| } |
| @@ -343,19 +343,26 @@ size_t CountProtectedSecureCookiesAtPriority( |
| bool IsCookieEligibleForEviction(CookiePriority current_priority_level, |
| bool protect_secure_cookies, |
| const CanonicalCookie* cookie) { |
| - if (!cookie->IsSecure() || !protect_secure_cookies) |
| - return cookie->Priority() <= current_priority_level; |
| - |
| - // Special consideration has to be given for low-priority secure cookies since |
| - // they are given lower prority than non-secure medium-priority and non-secure |
| - // high-priority cookies. Thus, low-priority secure cookies may be evicted at |
| - // a medium and high value of |current_priority_level|. Put another way, |
| - // low-priority secure cookies are only protected when the current priority |
| - // level is low. |
| - if (current_priority_level == COOKIE_PRIORITY_LOW) |
| + if (cookie->Priority() == current_priority_level && cookie->IsSecure() && |
|
jww
2016/05/23 23:19:03
These two conditionals can be simplified to:
if (
maksims (do not use this acc)
2016/05/24 06:24:57
Done.
|
| + protect_secure_cookies) |
| return false; |
| + if (cookie->Priority() == current_priority_level && !cookie->IsSecure() && |
| + protect_secure_cookies) |
| + return true; |
| - return cookie->Priority() == COOKIE_PRIORITY_LOW; |
| + return cookie->Priority() == current_priority_level; |
| +} |
| + |
| +size_t CountCookiesAtPriority(CookiePriority priority, |
| + CookieMonster::CookieItVector* cookies) { |
| + size_t num_of_priority_cookies = 0; |
| + size_t current = 0; |
| + while (current < cookies->size()) { |
| + if (cookies->at(current)->second->Priority() == priority) |
| + num_of_priority_cookies++; |
| + current++; |
| + } |
| + return num_of_priority_cookies; |
| } |
| } // namespace |
| @@ -1927,10 +1934,6 @@ size_t CookieMonster::GarbageCollect(const Time& current, |
| // Sort the cookies by access date, from least-recent to most-recent. |
| std::sort(cookie_its->begin(), cookie_its->end(), LRACookieSorter); |
| - size_t additional_quota_low = kDomainCookiesQuotaLow; |
| - size_t additional_quota_medium = kDomainCookiesQuotaMedium; |
| - size_t additional_quota_high = kDomainCookiesQuotaHigh; |
| - |
| // Remove all but the kDomainCookiesQuotaLow most-recently accessed |
| // cookies with low-priority. Then, if cookies still need to be removed, |
| // bump the quota and remove low- and medium-priority. Then, if cookies |
| @@ -1972,22 +1975,18 @@ size_t CookieMonster::GarbageCollect(const Time& current, |
| // necesary to delay quota adjustments until a later round. This is |
| // because if the high priority, non-secure round is skipped, its quota |
| // should not count until the later high priority, full round later. |
| - size_t* additional_quota = nullptr; |
| switch (purge_round.priority) { |
| case COOKIE_PRIORITY_LOW: |
| - additional_quota = &additional_quota_low; |
| + quota = kDomainCookiesQuotaLow; |
|
jww
2016/05/23 23:19:03
This is the change that I'm most confused about. D
maksims (do not use this acc)
2016/05/24 06:24:57
Not exactly. First, we take a quota at |priority|
jww
2016/05/26 03:46:39
Ok, I've got it. As mentioned before, can you upda
|
| break; |
| case COOKIE_PRIORITY_MEDIUM: |
| - additional_quota = &additional_quota_medium; |
| + quota = kDomainCookiesQuotaMedium; |
| break; |
| case COOKIE_PRIORITY_HIGH: |
| - additional_quota = &additional_quota_high; |
| + quota = kDomainCookiesQuotaHigh; |
| break; |
| } |
| - quota += *additional_quota; |
| - *additional_quota = 0u; |
| size_t just_deleted = 0u; |
| - |
| // Purge up to |purge_goal| for all cookies at the given priority. This |
| // path will always execute if strict secure cookies is disabled since |
| // |purge_goal| must be positive because of the for-loop guard. If |
| @@ -2056,46 +2055,43 @@ size_t CookieMonster::PurgeLeastRecentMatches(CookieItVector* cookies, |
| size_t purge_goal, |
| bool protect_secure_cookies) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| - |
| - // Find the first protected cookie by walking down from the end of the list |
| - // cookie list (most-recently accessed) until |to_protect| cookies that match |
| - // |priority| are found. |
| - // |
| - // If |protect_secure_cookies| is true, do a first pass that counts eligible |
| - // secure cookies at the specified priority as protected. |
| + // 1. Count number of the cookies at |priority| |
| + size_t cookies_count_possibly_to_be_deleted = |
|
jww
2016/05/23 23:19:03
While this logic is generally sound, it might be g
maksims (do not use this acc)
2016/05/24 06:24:57
Hmm, ok!
|
| + CountCookiesAtPriority(priority, cookies); |
| + // 2. If |cookies_count| at |priority| is less than or equal to_protect, skip. |
| + // This involves secure and non-secure cookies at |priority|. |
| + if (cookies_count_possibly_to_be_deleted <= to_protect) |
| + return 0u; |
| + |
| + // 3. Calculate number of secure cookies at |priority| |
| + // and Count number of cookies at |priority| that can possibly be deleted. |
| + // It is guaranteed we do not delete more than |purge_goal| even if |
| + // |priority_cookies_might_be_deleted| is higher. |
|
jww
2016/05/26 03:46:39
This variable name changed, correct?
|
| + size_t secure_cookies = 0u; |
| if (protect_secure_cookies) { |
| - to_protect -= std::min( |
| - to_protect, CountProtectedSecureCookiesAtPriority(priority, cookies)); |
| - } |
| - |
| - size_t protection_boundary = cookies->size(); |
| - while (to_protect > 0 && protection_boundary > 0) { |
| - protection_boundary--; |
| - if (cookies->at(protection_boundary)->second->Priority() <= priority) |
| - to_protect--; |
| + secure_cookies = CountProtectedSecureCookiesAtPriority(priority, cookies); |
| + cookies_count_possibly_to_be_deleted -= secure_cookies; |
|
jww
2016/05/23 23:19:03
What if secure_cookies < to_protect? Don't you sti
maksims (do not use this acc)
2016/05/24 06:24:57
It doesn't matter. |secure_cookies| are subtracted
jww
2016/05/26 03:46:39
What if there are 90 low priority, non-secure cook
|
| + } else { |
| + cookies_count_possibly_to_be_deleted -= to_protect; |
| } |
| - // Now, walk up from the beginning of the list (least-recently accessed) until |
| - // |purge_goal| cookies are removed, or the iterator hits |
| - // |protection_boundary|. |
| - size_t removed = 0; |
| - size_t current = 0; |
| - while (removed < purge_goal && current < protection_boundary) { |
| + size_t removed = 0u; |
| + size_t current = 0u; |
| + while (removed < purge_goal && current < cookies->size()) { |
| const CanonicalCookie* current_cookie = cookies->at(current)->second; |
| - // Only delete the current cookie if the priority is less than or equal to |
| - // the current level. If it is equal to the current level, and secure |
| - // cookies are protected, only delete it if it is not secure. |
| + // Only delete the current cookie if the priority is equal to |
| + // the current level. |
| if (IsCookieEligibleForEviction(priority, protect_secure_cookies, |
| - current_cookie)) { |
| + current_cookie) && |
| + cookies_count_possibly_to_be_deleted > 0) { |
| InternalDeleteCookie(cookies->at(current), true, |
| DELETE_COOKIE_EVICTED_DOMAIN); |
| cookies->erase(cookies->begin() + current); |
| removed++; |
| - |
| + cookies_count_possibly_to_be_deleted--; |
| // The call to 'erase' above shifts the contents of the vector, but |
| // doesn't shift |protection_boundary|. Decrement that here to ensure that |
| - // the correct set of cookies is protected. |
| - protection_boundary--; |
| + // the correct set of cookies is protected.; |
| } else { |
| current++; |
| } |