Bug 1267910 - Part 1: Make the API getCookiesFromHost of the nsICookieManager2 OriginAttributes-aware. r=jdm

--HG--
extra : rebase_source : 18945a3aac4c331a1513ac402553d725cf334b76
This commit is contained in:
Tim Huang 2016-05-25 11:54:21 +08:00
Родитель dab61c390e
Коммит cc903e433a
4 изменённых файлов: 136 добавлений и 32 удалений

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

@ -2260,6 +2260,45 @@ nsCookieService::GetEnumerator(nsISimpleEnumerator **aEnumerator)
return NS_NewArrayEnumerator(aEnumerator, cookieList);
}
static nsresult
InitializeOriginAttributes(NeckoOriginAttributes* aAttrs,
JS::HandleValue aOriginAttributes,
JSContext* aCx,
uint8_t aArgc,
const char16_t* aAPI,
const char16_t* aInterfaceSuffix)
{
MOZ_ASSERT(aAttrs);
MOZ_ASSERT(aCx);
MOZ_ASSERT(aAPI);
MOZ_ASSERT(aInterfaceSuffix);
if (aArgc == 0) {
const char16_t* params[] = {
aAPI,
aInterfaceSuffix
};
// This is supposed to be temporary and in 1 or 2 releases we want to
// have originAttributes param as mandatory. But for now, we don't want to
// break existing addons, so we write a console message to inform the addon
// developers about it.
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
NS_LITERAL_CSTRING("Cookie Manager"),
nullptr,
nsContentUtils::eNECKO_PROPERTIES,
"nsICookieManagerAPIDeprecated",
params, ArrayLength(params));
} else if (aArgc == 1) {
if (!aOriginAttributes.isObject() ||
!aAttrs->Init(aCx, aOriginAttributes)) {
return NS_ERROR_INVALID_ARG;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsCookieService::Add(const nsACString &aHost,
const nsACString &aPath,
@ -2268,8 +2307,41 @@ nsCookieService::Add(const nsACString &aHost,
bool aIsSecure,
bool aIsHttpOnly,
bool aIsSession,
int64_t aExpiry)
int64_t aExpiry,
JS::HandleValue aOriginAttributes,
JSContext* aCx,
uint8_t aArgc)
{
MOZ_ASSERT(aArgc == 0 || aArgc == 1);
NeckoOriginAttributes attrs;
nsresult rv = InitializeOriginAttributes(&attrs,
aOriginAttributes,
aCx,
aArgc,
MOZ_UTF16("nsICookieManager2.add()"),
MOZ_UTF16("2"));
NS_ENSURE_SUCCESS(rv, rv);
return AddNative(aHost, aPath, aName, aValue, aIsSecure, aIsHttpOnly,
aIsSession, aExpiry, &attrs);
}
NS_IMETHODIMP_(nsresult)
nsCookieService::AddNative(const nsACString &aHost,
const nsACString &aPath,
const nsACString &aName,
const nsACString &aValue,
bool aIsSecure,
bool aIsHttpOnly,
bool aIsSession,
int64_t aExpiry,
NeckoOriginAttributes* aOriginAttributes)
{
if (NS_WARN_IF(!aOriginAttributes)) {
return NS_ERROR_FAILURE;
}
if (!mDBState) {
NS_WARNING("No DBState! Profile already closed?");
return NS_ERROR_NOT_AVAILABLE;
@ -2287,7 +2359,7 @@ nsCookieService::Add(const nsACString &aHost,
NS_ENSURE_SUCCESS(rv, rv);
int64_t currentTimeInUsec = PR_Now();
nsCookieKey key = DEFAULT_APP_KEY(baseDomain);
nsCookieKey key = nsCookieKey(baseDomain, *aOriginAttributes);
RefPtr<nsCookie> cookie =
nsCookie::Create(aName, aValue, host, aPath,
@ -2370,24 +2442,16 @@ nsCookieService::Remove(const nsACString &aHost,
uint8_t aArgc)
{
MOZ_ASSERT(aArgc == 0 || aArgc == 1);
if (aArgc == 0) {
// This is supposed to be temporary and in 1 or 2 releases we want to
// have originAttributes param as mandatory. But for now, we don't want to
// break existing addons, so we write a console message to inform the addon
// developers about it.
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
NS_LITERAL_CSTRING("Cookie Manager"),
nullptr,
nsContentUtils::eNECKO_PROPERTIES,
"nsICookieManagerRemoveDeprecated");
}
NeckoOriginAttributes attrs;
if (aArgc == 1 &&
(!aOriginAttributes.isObject() ||
!attrs.Init(aCx, aOriginAttributes))) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv = InitializeOriginAttributes(&attrs,
aOriginAttributes,
aCx,
aArgc,
MOZ_UTF16("nsICookieManager.remove()"),
MOZ_UTF16(""));
NS_ENSURE_SUCCESS(rv, rv);
return RemoveNative(aHost, aName, aPath, aBlocked, &attrs);
}
@ -4300,8 +4364,13 @@ nsCookieService::CountCookiesFromHost(const nsACString &aHost,
// nsICookieManager2 interface.
NS_IMETHODIMP
nsCookieService::GetCookiesFromHost(const nsACString &aHost,
JS::HandleValue aOriginAttributes,
JSContext* aCx,
uint8_t aArgc,
nsISimpleEnumerator **aEnumerator)
{
MOZ_ASSERT(aArgc == 0 || aArgc == 1);
if (!mDBState) {
NS_WARNING("No DBState! Profile already closed?");
return NS_ERROR_NOT_AVAILABLE;
@ -4316,7 +4385,16 @@ nsCookieService::GetCookiesFromHost(const nsACString &aHost,
rv = GetBaseDomainFromHost(host, baseDomain);
NS_ENSURE_SUCCESS(rv, rv);
nsCookieKey key = DEFAULT_APP_KEY(baseDomain);
NeckoOriginAttributes attrs;
rv = InitializeOriginAttributes(&attrs,
aOriginAttributes,
aCx,
aArgc,
MOZ_UTF16("nsICookieManager2.getCookiesFromHost()"),
MOZ_UTF16("2"));
NS_ENSURE_SUCCESS(rv, rv);
nsCookieKey key = nsCookieKey(baseDomain, attrs);
EnsureReadDomain(key);
nsCookieEntry *entry = mDBState->hostTable.GetEntry(key);

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

@ -46,7 +46,10 @@ interface nsICookieManager2 : nsICookieManager
* expiration date, in seconds since midnight (00:00:00), January 1,
* 1970 UTC. note that expiry time will also be honored for session cookies;
* in this way, the more restrictive of the two will take effect.
* @param aOriginAttributes The originAttributes of this cookie. This
* attribute is optional to avoid breaking add-ons.
*/
[implicit_jscontext, optional_argc]
void add(in AUTF8String aHost,
in AUTF8String aPath,
in ACString aName,
@ -54,7 +57,19 @@ interface nsICookieManager2 : nsICookieManager
in boolean aIsSecure,
in boolean aIsHttpOnly,
in boolean aIsSession,
in int64_t aExpiry);
in int64_t aExpiry,
[optional] in jsval aOriginAttributes);
[notxpcom]
nsresult addNative(in AUTF8String aHost,
in AUTF8String aPath,
in ACString aName,
in ACString aValue,
in boolean aIsSecure,
in boolean aIsHttpOnly,
in boolean aIsSession,
in int64_t aExpiry,
in NeckoOriginAttributesPtr aOriginAttributes);
/**
* Find whether a given cookie already exists.
@ -92,12 +107,17 @@ interface nsICookieManager2 : nsICookieManager
* the host string to search for, e.g. "google.com". this should consist
* of only the host portion of a URI. see @add for a description of
* acceptable host strings.
* @param aOriginAttributes The originAttributes of cookies that would be
* retrived. This attribute is optional to avoid
* breaking add-ons.
*
* @return an nsISimpleEnumerator of nsICookie2 objects.
*
* @see countCookiesFromHost
*/
nsISimpleEnumerator getCookiesFromHost(in AUTF8String aHost);
[implicit_jscontext, optional_argc]
nsISimpleEnumerator getCookiesFromHost(in AUTF8String aHost,
[optional] in jsval aOriginAttributes);
/**
* Import an old-style cookie file. Imported cookies will be added to the

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

@ -44,5 +44,6 @@ TrackingUriBlocked=The resource at “%1$S” was blocked because tracking prote
# %1$S is the deprected API; %2$S is the API function that should be used.
APIDeprecationWarning=Warning: %1$S deprecated, please use %2$S
# LOCALIZATION NOTE (nsICookieManagerRemoveDeprecated): don't localize nsICookieManager.remove() and originAttributes.
nsICookieManagerRemoveDeprecated=“nsICookieManager.remove()” is changed. Update your code and pass the correct originAttributes. Read more on MDN: https://developer.mozilla.org/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICookieManager
# LOCALIZATION NOTE (nsICookieManagerDeprecated): don't localize originAttributes.
# %1$S is the deprecated API; %2$S is the interface suffix that the given deprecated API belongs to.
nsICookieManagerAPIDeprecated=“%1$S” is changed. Update your code and pass the correct originAttributes. Read more on MDN: https://developer.mozilla.org/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICookieManager%2$S

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

@ -597,33 +597,38 @@ main(int32_t argc, char *argv[])
nsCOMPtr<nsICookieManager2> cookieMgr2 = do_QueryInterface(cookieMgr);
if (!cookieMgr2) return -1;
mozilla::NeckoOriginAttributes attrs;
// first, ensure a clean slate
rv[0] = NS_SUCCEEDED(cookieMgr->RemoveAll());
// add some cookies
rv[1] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
rv[1] = NS_SUCCEEDED(cookieMgr2->AddNative(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
NS_LITERAL_CSTRING("/foo"), // path
NS_LITERAL_CSTRING("test1"), // name
NS_LITERAL_CSTRING("yes"), // value
false, // is secure
false, // is httponly
true, // is session
INT64_MAX)); // expiry time
rv[2] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
INT64_MAX, // expiry time
&attrs)); // originAttributes
rv[2] = NS_SUCCEEDED(cookieMgr2->AddNative(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
NS_LITERAL_CSTRING("/foo"), // path
NS_LITERAL_CSTRING("test2"), // name
NS_LITERAL_CSTRING("yes"), // value
false, // is secure
true, // is httponly
true, // is session
PR_Now() / PR_USEC_PER_SEC + 2)); // expiry time
rv[3] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("new.domain"), // domain
PR_Now() / PR_USEC_PER_SEC + 2, // expiry time
&attrs)); // originAttributes
rv[3] = NS_SUCCEEDED(cookieMgr2->AddNative(NS_LITERAL_CSTRING("new.domain"), // domain
NS_LITERAL_CSTRING("/rabbit"), // path
NS_LITERAL_CSTRING("test3"), // name
NS_LITERAL_CSTRING("yes"), // value
false, // is secure
false, // is httponly
true, // is session
INT64_MAX)); // expiry time
INT64_MAX, // expiry time
&attrs)); // originAttributes
// confirm using enumerator
nsCOMPtr<nsISimpleEnumerator> enumerator;
rv[4] = NS_SUCCEEDED(cookieMgr->GetEnumerator(getter_AddRefs(enumerator)));
@ -659,7 +664,6 @@ main(int32_t argc, char *argv[])
bool found;
rv[9] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && found;
mozilla::NeckoOriginAttributes attrs;
// remove the cookie, block it, and ensure it can't be added again
rv[10] = NS_SUCCEEDED(cookieMgr->RemoveNative(NS_LITERAL_CSTRING("new.domain"), // domain
@ -668,14 +672,15 @@ main(int32_t argc, char *argv[])
true, // is blocked
&attrs)); // originAttributes
rv[11] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
rv[12] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("new.domain"), // domain
rv[12] = NS_SUCCEEDED(cookieMgr2->AddNative(NS_LITERAL_CSTRING("new.domain"), // domain
NS_LITERAL_CSTRING("/rabbit"), // path
NS_LITERAL_CSTRING("test3"), // name
NS_LITERAL_CSTRING("yes"), // value
false, // is secure
false, // is httponly
true, // is session
INT64_MIN)); // expiry time
INT64_MIN, // expiry time
&attrs)); // originAttributes
rv[13] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
// sleep four seconds, to make sure the second cookie has expired
PR_Sleep(4 * PR_TicksPerSecond());