void CookieManager::update(CookieMap* targetMap, ParsedCookie* newCookie, BackingStoreRemovalPolicy postToBackingStore) { // 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 CookieLog("CookieManager - updating new cookie - %s.\n", newCookie->toString().utf8().data()); ParsedCookie* oldCookie = targetMap->updateCookie(newCookie); ASSERT(oldCookie); if (postToBackingStore == RemoveFromBackingStore) { bool newIsSession = newCookie->isSession(); bool oldIsSession = oldCookie->isSession(); if (!newIsSession && !oldIsSession) m_cookieBackingStore->update(newCookie); else if (newIsSession && !oldIsSession) { // Must manually decrease the counter because it was not counted when // the cookie was removed in cookieMap. removedCookie(); m_cookieBackingStore->remove(oldCookie); } else if (!newIsSession && oldIsSession) { // Must manually increase the counter because it was not counted when // the cookie was added in cookieMap. addedCookie(); m_cookieBackingStore->insert(newCookie); } } delete oldCookie; }
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; }