void CookieManager::cookieLimitCleanUp(Timer<CookieManager>* timer) { ASSERT_UNUSED(timer, timer == &m_limitTimer); CookieLimitLog("CookieManager - Starting cookie clean up"); size_t numberOfCookiesOverLimit = (m_count > s_globalMaxCookieCount) ? m_count - s_globalMaxCookieCount : 0; size_t amountToDelete = s_cookiesToDeleteWhenLimitReached + numberOfCookiesOverLimit; CookieLimitLog("CookieManager - Excess: %d Amount to Delete: %d", numberOfCookiesOverLimit, amountToDelete); // Call the database to delete 'amountToDelete' of cookies Vector<ParsedCookie*> cookiesToDelete; cookiesToDelete.reserveInitialCapacity(amountToDelete); CookieLimitLog("CookieManager - Calling database to clean up"); m_cookieBackingStore->getCookiesFromDatabase(cookiesToDelete, amountToDelete); // Cookies are ordered in ASC order by lastAccessed for (size_t i = 0; i < amountToDelete; ++i) { // Expire them and call checkandtreat to delete them from memory and database ParsedCookie* newCookie = cookiesToDelete[i]; CookieLimitLog("CookieManager - Expire cookie: %s and delete", newCookie->toString().utf8().data()); newCookie->forceExpire(); checkAndTreatCookie(newCookie, RemoveFromBackingStore); } CookieLimitLog("CookieManager - Cookie clean up complete."); }
void CookieManager::addCookieToMap(CookieMap* targetMap, ParsedCookie* candidateCookie, BackingStoreRemovalPolicy postToBackingStore) { ParsedCookie* oldestCookie = 0; // Check if we have not reached the per cookie domain limit. // If that is not true, we check if the global limit has been reached if backingstore mode is on // Two points: // 1) We only do a global check if backingstore mode is on because the global cookie limit only // counts session cookies that are saved in the database. If the user goes over the limit // when they are in private mode, we know that the total cookie limit will be under the limit // once the user goes back to normal mode (memory deleted and reloaded from the database) // 2) We use else if for this statement because if we remove a cookie in the 1st statement // then it means the global count will never exceed the limit CookieLimitLog("CookieManager - local count: %d global count: %d", targetMap->count(), m_count); if (targetMap->count() >= s_maxCookieCountPerHost) { CookieLog("CookieManager - deleting oldest cookie from this map due to domain count.\n"); oldestCookie = targetMap->removeOldestCookie(); } else if (m_count >= s_globalMaxCookieCount && (postToBackingStore != DoNotRemoveFromBackingStore)) { CookieLimitLog("CookieManager - Global limit reached, initiate cookie limit clean up."); initiateCookieLimitCleanUp(); } CookieLog("CookieManager - adding new cookie - %s.\n", candidateCookie->toString().utf8().data()); targetMap->addCookie(candidateCookie); // Only add non session cookie to the backing store. if (postToBackingStore == RemoveFromBackingStore) { if (oldestCookie && !oldestCookie->isSession()) { CookieLog("CookieManager - oldestCookie exists, deleting it from backingstore and destructing.\n"); m_cookieBackingStore->remove(oldestCookie); } if (!candidateCookie->isSession()) m_cookieBackingStore->insert(candidateCookie); } if (oldestCookie) delete oldestCookie; }
void CookieManager::addCookieToMap(CookieMap* targetMap, ParsedCookie* candidateCookie, BackingStoreRemovalPolicy postToBackingStore, CookieFilter filter) { ParsedCookie* replacedCookie = 0; if (!targetMap->addOrReplaceCookie(candidateCookie, &replacedCookie, filter)) { CookieLog("CookieManager - rejecting new cookie - %s.\n", candidateCookie->toString().utf8().data()); delete candidateCookie; return; } if (replacedCookie) { CookieLog("CookieManager - updating new cookie - %s.\n", candidateCookie->toString().utf8().data()); // A cookie was replaced in targetMap. // If old cookie is non-session and new one is, we have to delete it from backingstore // If new cookie is non-session and old one is, we have to add it to backingstore // If both sessions are non-session, then we update it in the backingstore bool newIsSession = candidateCookie->isSession(); bool oldIsSession = replacedCookie->isSession(); if (postToBackingStore == RemoveFromBackingStore) { if (!newIsSession && !oldIsSession) m_cookieBackingStore->update(candidateCookie); else if (newIsSession && !oldIsSession) { // Must manually decrease the counter because it was not counted when // the cookie was removed in cookieVector. removedCookie(); m_cookieBackingStore->remove(replacedCookie); } else if (!newIsSession && oldIsSession) { // Must manually increase the counter because it was not counted when // the cookie was added in cookieVector. addedCookie(); m_cookieBackingStore->insert(candidateCookie); } } delete replacedCookie; return; } CookieLog("CookieManager - adding new cookie - %s.\n", candidateCookie->toString().utf8().data()); ParsedCookie* oldestCookie = 0; // Check if we have not reached the per cookie domain limit. // If that is not true, we check if the global limit has been reached if backingstore mode is on // Two points: // 1) We only do a global check if backingstore mode is on because the global cookie limit only // counts session cookies that are saved in the database. If the user goes over the limit // when they are in private mode, we know that the total cookie limit will be under the limit // once the user goes back to normal mode (memory deleted and reloaded from the database) // 2) We use else if for this statement because if we remove a cookie in the 1st statement // then it means the global count will never exceed the limit CookieLimitLog("CookieManager - local count: %d global count: %d", targetMap->count(), m_count); if (targetMap->count() > s_maxCookieCountPerHost) { CookieLog("CookieManager - deleting oldest cookie from this map due to domain count.\n"); oldestCookie = targetMap->removeOldestCookie(); } else if (m_count > s_globalMaxCookieCount && (postToBackingStore != DoNotRemoveFromBackingStore)) { CookieLimitLog("CookieManager - Global limit reached, initiate cookie limit clean up."); initiateCookieLimitCleanUp(); } // Only add non session cookie to the backing store. if (postToBackingStore == RemoveFromBackingStore) { if (oldestCookie && !oldestCookie->isSession()) { CookieLog("CookieManager - oldestCookie exists, deleting it from backingstore and destructing.\n"); m_cookieBackingStore->remove(oldestCookie); } if (!candidateCookie->isSession()) m_cookieBackingStore->insert(candidateCookie); } if (oldestCookie) delete oldestCookie; }