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::checkAndTreatCookie(ParsedCookie* cookie) { ASSERT(cookie->domain().length()); CookieMap* curMap = m_managerMap.get(cookie->domain()); // Check for cookie to remove in case it is not a session cookie and it has expired. if (cookie->hasExpired()) { // The cookie has expired so check we have a valid HashMap so try delete it. if (curMap) { // Check if we have a cookie to remove and update information accordingly. ParsedCookie* prevCookie = curMap->takePrevious(cookie); if (prevCookie) { cookieBackingStore().remove(cookie); removedCookie(); delete cookie; } } } else { if (!curMap) { curMap = new CookieMap(); m_managerMap.add(cookie->domain(), curMap); addCookieToMap(curMap, cookie); } else { // Check if there is a previous cookie. ParsedCookie* prevCookie = curMap->takePrevious(cookie); if (prevCookie) { update(curMap, prevCookie, cookie); delete prevCookie; } else addCookieToMap(curMap, cookie); } } }
void CookieManager::addCookieToMap(CookieMap* map, ParsedCookie* cookie) { // Check if we do not have reached the cookie's threshold. // FIXME : should split the case and remove one cookie among all the other if m_count >= max_count if (map->count() > s_maxCookieCountPerHost || m_count >= s_globalMaxCookieCount) { ParsedCookie* rmCookie = map->removeOldestCookie(); cookieBackingStore().remove(rmCookie); removedCookie(); delete rmCookie; } map->add(cookie); // Only add non session cookie to the backing store. if (!cookie->isSession()) cookieBackingStore().insert(cookie); m_count++; }
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; }