implement CountCookiesFromHost() in cookie backend for perf reasons, and update consumers to use it.bug 379239, r=mvl, sr=biesi

This commit is contained in:
dwitte@stanford.edu 2007-05-05 14:09:54 -07:00
Родитель c251746b06
Коммит 1852ac4c86
5 изменённых файлов: 50 добавлений и 43 удалений

Просмотреть файл

@ -299,23 +299,9 @@ function hostHasCookies(hostName) {
return false;
var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"]
.getService(Components.interfaces.nsICookieManager);
.getService(Components.interfaces.nsICookieManager2);
var iter = cookieManager.enumerator;
while (iter.hasMoreElements()){
var cookie = iter.getNext().QueryInterface(Components.interfaces.nsICookie);
if (!cookie)
continue;
// A direct match works whether it's a domain cookie or not
if (cookie.host == hostName)
return true;
// Domain cookies just need to end with our target hostname
if (cookie.isDomain && endsWith(hostName, cookie.host))
return true;
}
return false;
return cookieManager.countCookiesFromHost(hostName) > 0;
}
/**

Просмотреть файл

@ -361,11 +361,17 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
// get some useful information to present to the user:
// whether a previous cookie already exists, and how many cookies this host
// has set
PRBool foundCookie;
PRBool foundCookie = PR_FALSE;
PRUint32 countFromHost;
nsCOMPtr<nsICookieManager2> cookieManager = do_GetService(NS_COOKIEMANAGER_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
rv = cookieManager->FindMatchingCookie(aCookie, &countFromHost, &foundCookie);
if (NS_SUCCEEDED(rv)) {
nsCAutoString rawHost;
aCookie->GetRawHost(rawHost);
rv = cookieManager->CountCookiesFromHost(rawHost, &countFromHost);
if (NS_SUCCEEDED(rv) && countFromHost > 0)
rv = cookieManager->CookieExists(aCookie, &foundCookie);
}
if (NS_FAILED(rv)) return rv;
// check if the cookie we're trying to set is already expired, and return;

Просмотреть файл

@ -43,7 +43,7 @@ interface nsICookie2;
* Additions to the frozen nsICookieManager
*/
[scriptable, uuid(3E73FF5F-154E-494f-B640-3C654BA2CC2B)]
[scriptable, uuid(9facbb48-c05d-470b-b4da-897c8d613e00)]
interface nsICookieManager2 : nsICookieManager
{
/**
@ -77,19 +77,24 @@ interface nsICookieManager2 : nsICookieManager
in PRInt64 aExpiry);
/**
* Find whether a matching cookie already exists, and how many cookies
* a given host has already set. This is useful when e.g. prompting the
* user whether to accept a given cookie.
* Find whether a given cookie already exists.
*
* @param aCookie
* the cookie to look for
* @param aCountFromHost
* the number of cookies found whose hosts are the same as, or
* subdomains of, the host field of aCookie
*
* @return true if a cookie was found which matches the host, path, and name
* fields of aCookie
*/
boolean findMatchingCookie(in nsICookie2 aCookie,
out unsigned long aCountFromHost);
boolean cookieExists(in nsICookie2 aCookie);
/**
* Count how many cookies a given host has already set.
*
* @param aHost
* the raw host string to look for, e.g. "google.com"
*
* @return the number of cookies found whose hosts are the same as, or
* subdomains of, aHost
*/
unsigned long countCookiesFromHost(in ACString aHost);
};

Просмотреть файл

@ -1432,7 +1432,7 @@ nsCookieService::AddInternal(nsCookie *aCookie,
// check if we have to delete an old cookie.
nsEnumerationData data(aCurrentTime, LL_MAXINT);
if (CountCookiesFromHost(aCookie, data) >= mMaxCookiesPerHost) {
if (CountCookiesFromHostInternal(aCookie->RawHost(), data) >= mMaxCookiesPerHost) {
// remove the oldest cookie from host
oldCookie = data.iter.current;
RemoveCookieFromList(data.iter);
@ -2147,34 +2147,31 @@ nsCookieService::RemoveExpiredCookies(nsInt64 aCurrentTime)
mHostTable.EnumerateEntries(removeExpiredCallback, &aCurrentTime);
}
// find whether a previous cookie has been set, and count the number of cookies from
// this host, for prompting purposes. this is provided by the nsICookieManager2
// interface.
// find whether a given cookie has been previously set. this is provided by the
// nsICookieManager2 interface.
NS_IMETHODIMP
nsCookieService::FindMatchingCookie(nsICookie2 *aCookie,
PRUint32 *aCountFromHost,
PRBool *aFoundCookie)
nsCookieService::CookieExists(nsICookie2 *aCookie,
PRBool *aFoundCookie)
{
NS_ENSURE_ARG_POINTER(aCookie);
// we don't care about finding the oldest cookie here, so disable the search
// just a placeholder
nsEnumerationData data(NOW_IN_SECONDS, LL_MININT);
nsCookie *cookie = NS_STATIC_CAST(nsCookie*, aCookie);
*aCountFromHost = CountCookiesFromHost(cookie, data);
*aFoundCookie = FindCookie(cookie->Host(), cookie->Name(), cookie->Path(), data.iter);
return NS_OK;
}
// count the number of cookies from this host, and find the oldest cookie
// from this host.
// count the number of cookies from a given host, and simultaneously find the
// oldest cookie from the host.
PRUint32
nsCookieService::CountCookiesFromHost(nsCookie *aCookie,
nsEnumerationData &aData)
nsCookieService::CountCookiesFromHostInternal(const nsACString &aHost,
nsEnumerationData &aData)
{
PRUint32 countFromHost = 0;
nsCAutoString hostWithDot(NS_LITERAL_CSTRING(".") + aCookie->RawHost());
nsCAutoString hostWithDot(NS_LITERAL_CSTRING(".") + aHost);
const char *currentDot = hostWithDot.get();
const char *nextDot = currentDot + 1;
@ -2202,6 +2199,19 @@ nsCookieService::CountCookiesFromHost(nsCookie *aCookie,
return countFromHost;
}
// count the number of cookies stored by a particular host. this is provided by the
// nsICookieManager2 interface.
NS_IMETHODIMP
nsCookieService::CountCookiesFromHost(const nsACString &aHost,
PRUint32 *aCountFromHost)
{
// we don't care about finding the oldest cookie here, so disable the search
nsEnumerationData data(NOW_IN_SECONDS, LL_MININT);
*aCountFromHost = CountCookiesFromHostInternal(aHost, data);
return NS_OK;
}
// find an exact previous match.
PRBool
nsCookieService::FindCookie(const nsAFlatCString &aHost,

Просмотреть файл

@ -190,7 +190,7 @@ class nsCookieService : public nsICookieServiceInternal
void RemoveExpiredCookies(nsInt64 aCurrentTime);
PRBool FindCookie(const nsAFlatCString &aHost, const nsAFlatCString &aName, const nsAFlatCString &aPath, nsListIter &aIter);
void FindOldestCookie(nsEnumerationData &aData);
PRUint32 CountCookiesFromHost(nsCookie *aCookie, nsEnumerationData &aData);
PRUint32 CountCookiesFromHostInternal(const nsACString &aHost, nsEnumerationData &aData);
void NotifyRejected(nsIURI *aHostURI);
void NotifyChanged(nsICookie2 *aCookie, const PRUnichar *aData);