Bug 580449 - Address nsCookieService.cpp review. r=jdm, a=fennec2.0+

This commit is contained in:
Dan Witte 2010-09-16 13:21:12 -07:00
parent e40712bedd
commit 75f1d1f405
3 changed files with 40 additions and 34 deletions

View File

@ -75,8 +75,8 @@ CookieServiceParent::RecvGetCookieString(const IPC::URI& aHost,
if (!hostURI) if (!hostURI)
return false; return false;
mCookieService->GetCookieInternal(hostURI, originatingURI, mCookieService->GetCookieStringInternal(hostURI, originatingURI,
aFromHttp, *aResult); aFromHttp, *aResult);
return true; return true;
} }

View File

@ -1075,16 +1075,7 @@ nsCookieService::GetCookieString(nsIURI *aHostURI,
nsIChannel *aChannel, nsIChannel *aChannel,
char **aCookie) char **aCookie)
{ {
NS_ENSURE_ARG(aHostURI); return GetCookieStringCommon(aHostURI, aChannel, false, aCookie);
NS_ENSURE_ARG(aCookie);
nsCOMPtr<nsIURI> originatingURI;
GetOriginatingURI(aChannel, getter_AddRefs(originatingURI));
nsCAutoString result;
GetCookieInternal(aHostURI, originatingURI, PR_FALSE, result);
*aCookie = result.IsEmpty() ? nsnull : ToNewCString(result);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -1092,6 +1083,15 @@ nsCookieService::GetCookieStringFromHttp(nsIURI *aHostURI,
nsIURI *aFirstURI, nsIURI *aFirstURI,
nsIChannel *aChannel, nsIChannel *aChannel,
char **aCookie) char **aCookie)
{
return GetCookieStringCommon(aHostURI, aChannel, true, aCookie);
}
nsresult
nsCookieService::GetCookieStringCommon(nsIURI *aHostURI,
nsIChannel *aChannel,
bool aHttpBound,
char** aCookie)
{ {
NS_ENSURE_ARG(aHostURI); NS_ENSURE_ARG(aHostURI);
NS_ENSURE_ARG(aCookie); NS_ENSURE_ARG(aCookie);
@ -1100,7 +1100,7 @@ nsCookieService::GetCookieStringFromHttp(nsIURI *aHostURI,
GetOriginatingURI(aChannel, getter_AddRefs(originatingURI)); GetOriginatingURI(aChannel, getter_AddRefs(originatingURI));
nsCAutoString result; nsCAutoString result;
GetCookieInternal(aHostURI, originatingURI, PR_TRUE, result); GetCookieStringInternal(aHostURI, originatingURI, aHttpBound, result);
*aCookie = result.IsEmpty() ? nsnull : ToNewCString(result); *aCookie = result.IsEmpty() ? nsnull : ToNewCString(result);
return NS_OK; return NS_OK;
} }
@ -1111,16 +1111,7 @@ nsCookieService::SetCookieString(nsIURI *aHostURI,
const char *aCookieHeader, const char *aCookieHeader,
nsIChannel *aChannel) nsIChannel *aChannel)
{ {
NS_ENSURE_ARG(aHostURI); return SetCookieStringCommon(aHostURI, aCookieHeader, NULL, aChannel, false);
NS_ENSURE_ARG(aCookieHeader);
nsCOMPtr<nsIURI> originatingURI;
GetOriginatingURI(aChannel, getter_AddRefs(originatingURI));
nsDependentCString cookieString(aCookieHeader);
SetCookieStringInternal(aHostURI, originatingURI,
cookieString, EmptyCString(), PR_FALSE);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -1130,6 +1121,17 @@ nsCookieService::SetCookieStringFromHttp(nsIURI *aHostURI,
const char *aCookieHeader, const char *aCookieHeader,
const char *aServerTime, const char *aServerTime,
nsIChannel *aChannel) nsIChannel *aChannel)
{
return SetCookieStringCommon(aHostURI, aCookieHeader, aServerTime, aChannel,
true);
}
nsresult
nsCookieService::SetCookieStringCommon(nsIURI *aHostURI,
const char *aCookieHeader,
const char *aServerTime,
nsIChannel *aChannel,
bool aFromHttp)
{ {
NS_ENSURE_ARG(aHostURI); NS_ENSURE_ARG(aHostURI);
NS_ENSURE_ARG(aCookieHeader); NS_ENSURE_ARG(aCookieHeader);
@ -1140,7 +1142,7 @@ nsCookieService::SetCookieStringFromHttp(nsIURI *aHostURI,
nsDependentCString cookieString(aCookieHeader); nsDependentCString cookieString(aCookieHeader);
nsDependentCString serverTime(aServerTime ? aServerTime : ""); nsDependentCString serverTime(aServerTime ? aServerTime : "");
SetCookieStringInternal(aHostURI, originatingURI, cookieString, SetCookieStringInternal(aHostURI, originatingURI, cookieString,
serverTime, PR_TRUE); serverTime, aFromHttp);
return NS_OK; return NS_OK;
} }
@ -1151,6 +1153,8 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
const nsCString &aServerTime, const nsCString &aServerTime,
PRBool aFromHttp) PRBool aFromHttp)
{ {
NS_ASSERTION(aHostURI, "null host!");
// get the base domain for the host URI. // get the base domain for the host URI.
// e.g. for "www.bbc.co.uk", this would be "bbc.co.uk". // e.g. for "www.bbc.co.uk", this would be "bbc.co.uk".
// file:// URI's (i.e. with an empty host) are allowed, but any other // file:// URI's (i.e. with an empty host) are allowed, but any other
@ -1879,15 +1883,12 @@ public:
}; };
void void
nsCookieService::GetCookieInternal(nsIURI *aHostURI, nsCookieService::GetCookieStringInternal(nsIURI *aHostURI,
nsIURI *aOriginatingURI, nsIURI *aOriginatingURI,
PRBool aHttpBound, PRBool aHttpBound,
nsCString &aCookieString) nsCString &aCookieString)
{ {
if (!aHostURI) { NS_ASSERTION(aHostURI, "null host!");
COOKIE_LOGFAILURE(GET_COOKIE, nsnull, nsnull, "host URI is null");
return;
}
// get the base domain, host, and path from the URI. // get the base domain, host, and path from the URI.
// e.g. for "www.bbc.co.uk", the base domain would be "bbc.co.uk". // e.g. for "www.bbc.co.uk", the base domain would be "bbc.co.uk".
@ -2073,6 +2074,8 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI,
PRInt64 aServerTime, PRInt64 aServerTime,
PRBool aFromHttp) PRBool aFromHttp)
{ {
NS_ASSERTION(aHostURI, "null host!");
// create a stack-based nsCookieAttributes, to store all the // create a stack-based nsCookieAttributes, to store all the
// attributes parsed from the cookie // attributes parsed from the cookie
nsCookieAttributes cookieAttributes; nsCookieAttributes cookieAttributes;
@ -2140,7 +2143,8 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI,
if (mPermissionService) { if (mPermissionService) {
PRBool permission; PRBool permission;
// Not passing an nsIChannel here means CanSetCookie will use the currently // Not passing an nsIChannel here means CanSetCookie will use the currently
// active window to display the prompt. This isn't exactly ideal... // active window to display the prompt. This isn't exactly ideal, but this
// code is going away. See bug 546746.
mPermissionService->CanSetCookie(aHostURI, mPermissionService->CanSetCookie(aHostURI,
nsnull, nsnull,
static_cast<nsICookie2*>(static_cast<nsCookie*>(cookie)), static_cast<nsICookie2*>(static_cast<nsCookie*>(cookie)),

View File

@ -228,7 +228,9 @@ class nsCookieService : public nsICookieService
nsresult NormalizeHost(nsCString &aHost); nsresult NormalizeHost(nsCString &aHost);
nsresult GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, PRBool &aRequireHostMatch); nsresult GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, PRBool &aRequireHostMatch);
nsresult GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain); nsresult GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain);
void GetCookieInternal(nsIURI *aHostURI, nsIURI *aOriginatingURI, PRBool aHttpBound, nsCString &aCookie); nsresult GetCookieStringCommon(nsIURI *aHostURI, nsIChannel *aChannel, bool aHttpBound, char** aCookie);
void GetCookieStringInternal(nsIURI *aHostURI, nsIURI *aOriginatingURI, PRBool aHttpBound, nsCString &aCookie);
nsresult SetCookieStringCommon(nsIURI *aHostURI, const char *aCookieHeader, const char *aServerTime, nsIChannel *aChannel, bool aFromHttp);
void SetCookieStringInternal(nsIURI *aHostURI, nsIURI *aOriginatingURI, const nsCString &aCookieHeader, const nsCString &aServerTime, PRBool aFromHttp); void SetCookieStringInternal(nsIURI *aHostURI, nsIURI *aOriginatingURI, const nsCString &aCookieHeader, const nsCString &aServerTime, PRBool aFromHttp);
PRBool SetCookieInternal(nsIURI *aHostURI, const nsCString& aBaseDomain, PRBool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, PRInt64 aServerTime, PRBool aFromHttp); PRBool SetCookieInternal(nsIURI *aHostURI, const nsCString& aBaseDomain, PRBool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, PRInt64 aServerTime, PRBool aFromHttp);
void AddInternal(const nsCString& aBaseDomain, nsCookie *aCookie, PRInt64 aCurrentTimeInUsec, nsIURI *aHostURI, const char *aCookieHeader, PRBool aFromHttp); void AddInternal(const nsCString& aBaseDomain, nsCookie *aCookie, PRInt64 aCurrentTimeInUsec, nsIURI *aHostURI, const char *aCookieHeader, PRBool aFromHttp);