| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 namespace net { | 69 namespace net { |
| 70 | 70 |
| 71 // Cookie garbage collection thresholds. Based off of the Mozilla defaults. | 71 // Cookie garbage collection thresholds. Based off of the Mozilla defaults. |
| 72 // It might seem scary to have a high purge value, but really it's not. You | 72 // It might seem scary to have a high purge value, but really it's not. You |
| 73 // just make sure that you increase the max to cover the increase in purge, | 73 // just make sure that you increase the max to cover the increase in purge, |
| 74 // and we would have been purging the same amount of cookies. We're just | 74 // and we would have been purging the same amount of cookies. We're just |
| 75 // going through the garbage collection process less often. | 75 // going through the garbage collection process less often. |
| 76 static const size_t kNumCookiesPerHost = 70; // ~50 cookies | 76 static const size_t kNumCookiesPerHost = 70; // ~50 cookies |
| 77 static const size_t kNumCookiesPerHostPurge = 20; | 77 static const size_t kNumCookiesPerHostPurge = 20; |
| 78 static const size_t kNumCookiesTotal = 1100; // ~1000 cookies | |
| 79 static const size_t kNumCookiesTotalPurge = 100; | |
| 80 | 78 |
| 81 // Default minimum delay after updating a cookie's LastAccessDate before we | 79 // Default minimum delay after updating a cookie's LastAccessDate before we |
| 82 // will update it again. | 80 // will update it again. |
| 83 static const int kDefaultAccessUpdateThresholdSeconds = 60; | 81 static const int kDefaultAccessUpdateThresholdSeconds = 60; |
| 84 | 82 |
| 85 // static | 83 // static |
| 86 bool CookieMonster::enable_file_scheme_ = false; | 84 bool CookieMonster::enable_file_scheme_ = false; |
| 87 | 85 |
| 88 // static | 86 // static |
| 89 void CookieMonster::EnableFileScheme() { | 87 void CookieMonster::EnableFileScheme() { |
| 90 enable_file_scheme_ = true; | 88 enable_file_scheme_ = true; |
| 91 } | 89 } |
| 92 | 90 |
| 93 CookieMonster::CookieMonster() | 91 CookieMonster::CookieMonster() |
| 94 : initialized_(false), | 92 : initialized_(false), |
| 95 store_(NULL), | 93 store_(NULL), |
| 96 last_access_threshold_( | 94 last_access_threshold_( |
| 97 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { | 95 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { |
| 98 } | 96 } |
| 99 | 97 |
| 100 CookieMonster::CookieMonster(PersistentCookieStore* store) | 98 CookieMonster::CookieMonster(PersistentCookieStore* store) |
| 101 : initialized_(false), | 99 : initialized_(false), |
| 102 store_(store), | 100 store_(store), |
| 103 last_access_threshold_( | 101 last_access_threshold_( |
| 104 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { | 102 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { |
| 105 } | 103 } |
| 106 | 104 |
| 107 CookieMonster::~CookieMonster() { | 105 CookieMonster::~CookieMonster() { |
| 106 GarbageCollectExpired(Time::Now(), |
| 107 CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 108 NULL); |
| 108 DeleteAll(false); | 109 DeleteAll(false); |
| 109 } | 110 } |
| 110 | 111 |
| 111 void CookieMonster::InitStore() { | 112 void CookieMonster::InitStore() { |
| 112 DCHECK(store_) << "Store must exist to initialize"; | 113 DCHECK(store_) << "Store must exist to initialize"; |
| 113 | 114 |
| 114 // Initialize the store and sync in any saved persistent cookies. We don't | 115 // Initialize the store and sync in any saved persistent cookies. We don't |
| 115 // care if it's expired, insert it so it can be garbage collected, removed, | 116 // care if it's expired, insert it so it can be garbage collected, removed, |
| 116 // and sync'd. | 117 // and sync'd. |
| 117 std::vector<KeyedCanonicalCookie> cookies; | 118 std::vector<KeyedCanonicalCookie> cookies; |
| 118 // Reserve space for the maximum amount of cookies a database should have. | |
| 119 // This prevents multiple vector growth / copies as we append cookies. | |
| 120 cookies.reserve(kNumCookiesTotal); | |
| 121 store_->Load(&cookies); | 119 store_->Load(&cookies); |
| 122 for (std::vector<KeyedCanonicalCookie>::const_iterator it = cookies.begin(); | 120 for (std::vector<KeyedCanonicalCookie>::const_iterator it = cookies.begin(); |
| 123 it != cookies.end(); ++it) { | 121 it != cookies.end(); ++it) { |
| 124 InternalInsertCookie(it->first, it->second, false); | 122 InternalInsertCookie(it->first, it->second, false); |
| 125 } | 123 } |
| 124 GarbageCollectExpired(Time::Now(), |
| 125 CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 126 NULL); |
| 126 } | 127 } |
| 127 | 128 |
| 128 // The system resolution is not high enough, so we can have multiple | 129 // The system resolution is not high enough, so we can have multiple |
| 129 // set cookies that result in the same system time. When this happens, we | 130 // set cookies that result in the same system time. When this happens, we |
| 130 // increment by one Time unit. Let's hope computers don't get too fast. | 131 // increment by one Time unit. Let's hope computers don't get too fast. |
| 131 Time CookieMonster::CurrentTime() { | 132 Time CookieMonster::CurrentTime() { |
| 132 return std::max(Time::Now(), | 133 return std::max(Time::Now(), |
| 133 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); | 134 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); |
| 134 } | 135 } |
| 135 | 136 |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 const std::string& key) { | 566 const std::string& key) { |
| 566 int num_deleted = 0; | 567 int num_deleted = 0; |
| 567 | 568 |
| 568 // Collect garbage for this key. | 569 // Collect garbage for this key. |
| 569 if (cookies_.count(key) > kNumCookiesPerHost) { | 570 if (cookies_.count(key) > kNumCookiesPerHost) { |
| 570 COOKIE_DLOG(INFO) << "GarbageCollect() key: " << key; | 571 COOKIE_DLOG(INFO) << "GarbageCollect() key: " << key; |
| 571 num_deleted += GarbageCollectRange(current, cookies_.equal_range(key), | 572 num_deleted += GarbageCollectRange(current, cookies_.equal_range(key), |
| 572 kNumCookiesPerHost, kNumCookiesPerHostPurge); | 573 kNumCookiesPerHost, kNumCookiesPerHostPurge); |
| 573 } | 574 } |
| 574 | 575 |
| 575 // Collect garbage for everything. | |
| 576 if (cookies_.size() > kNumCookiesTotal) { | |
| 577 COOKIE_DLOG(INFO) << "GarbageCollect() everything"; | |
| 578 num_deleted += GarbageCollectRange(current, | |
| 579 CookieMapItPair(cookies_.begin(), cookies_.end()), kNumCookiesTotal, | |
| 580 kNumCookiesTotalPurge); | |
| 581 } | |
| 582 | |
| 583 return num_deleted; | 576 return num_deleted; |
| 584 } | 577 } |
| 585 | 578 |
| 586 static bool LRUCookieSorter(const CookieMonster::CookieMap::iterator& it1, | 579 static bool LRUCookieSorter(const CookieMonster::CookieMap::iterator& it1, |
| 587 const CookieMonster::CookieMap::iterator& it2) { | 580 const CookieMonster::CookieMap::iterator& it2) { |
| 588 // Cookies accessed less recently should be deleted first. | 581 // Cookies accessed less recently should be deleted first. |
| 589 if (it1->second->LastAccessDate() != it2->second->LastAccessDate()) | 582 if (it1->second->LastAccessDate() != it2->second->LastAccessDate()) |
| 590 return it1->second->LastAccessDate() < it2->second->LastAccessDate(); | 583 return it1->second->LastAccessDate() < it2->second->LastAccessDate(); |
| 591 | 584 |
| 592 // In rare cases we might have two cookies with identical last access times. | 585 // In rare cases we might have two cookies with identical last access times. |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1113 } | 1106 } |
| 1114 | 1107 |
| 1115 std::string CookieMonster::CanonicalCookie::DebugString() const { | 1108 std::string CookieMonster::CanonicalCookie::DebugString() const { |
| 1116 return StringPrintf("name: %s value: %s path: %s creation: %llu", | 1109 return StringPrintf("name: %s value: %s path: %s creation: %llu", |
| 1117 name_.c_str(), value_.c_str(), path_.c_str(), | 1110 name_.c_str(), value_.c_str(), path_.c_str(), |
| 1118 creation_date_.ToTimeT()); | 1111 creation_date_.ToTimeT()); |
| 1119 } | 1112 } |
| 1120 | 1113 |
| 1121 } // namespace | 1114 } // namespace |
| 1122 | 1115 |
| OLD | NEW |