Bug 1260399 - Remove deprecated messages in nsICookieManager methods - part 3 - nsICookieManager.cookieExists, r=ehsan

This commit is contained in:
Andrea Marchesini 2018-09-24 11:37:57 +02:00
parent 42c515aa1c
commit b958c6f170
9 changed files with 91 additions and 92 deletions

View File

@ -25,7 +25,7 @@ let since, oldCookie;
function addCookie(cookie) {
Services.cookies.add(cookie.host, cookie.path, cookie.name, "test", false, false, false, Date.now() / 1000 + 10000, {}, Ci.nsICookie2.SAMESITE_UNSET);
ok(Services.cookies.cookieExists(cookie), `Cookie ${cookie.name} was created.`);
ok(Services.cookies.cookieExists(cookie.host, cookie.path, cookie.name, {}), `Cookie ${cookie.name} was created.`);
}
async function setUpCookies() {
@ -121,8 +121,8 @@ add_task(async function testCookies() {
extension.sendMessage(method, {since});
await extension.awaitMessage("cookiesRemoved");
ok(Services.cookies.cookieExists(oldCookie), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE), "Recent cookie was removed.");
ok(Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
// Clear cookies with an old since value.
await setUpCookies();
@ -130,8 +130,8 @@ add_task(async function testCookies() {
extension.sendMessage(method, {since: since - 100000});
await extension.awaitMessage("cookiesRemoved");
ok(!Services.cookies.cookieExists(oldCookie), "Old cookie was removed.");
ok(!Services.cookies.cookieExists(COOKIE), "Recent cookie was removed.");
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was removed.");
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
// Clear cookies with no since value and valid originTypes.
await setUpCookies();
@ -140,8 +140,8 @@ add_task(async function testCookies() {
{originTypes: {unprotectedWeb: true, protectedWeb: false}});
await extension.awaitMessage("cookiesRemoved");
ok(!Services.cookies.cookieExists(COOKIE), `Cookie ${COOKIE.name} was removed.`);
ok(!Services.cookies.cookieExists(oldCookie), `Cookie ${oldCookie.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), `Cookie ${COOKIE.name} was removed.`);
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), `Cookie ${oldCookie.name} was removed.`);
}
await extension.startup();
@ -176,8 +176,8 @@ add_task(async function testCacheAndCookies() {
await awaitNotification;
await extension.awaitMessage("cacheAndCookiesRemoved");
ok(Services.cookies.cookieExists(oldCookie), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE), "Recent cookie was removed.");
ok(Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
// Clear cache and cookies with an old since value.
await setUpCookies();
@ -186,8 +186,8 @@ add_task(async function testCacheAndCookies() {
await awaitNotification;
await extension.awaitMessage("cacheAndCookiesRemoved");
ok(!Services.cookies.cookieExists(oldCookie), "Old cookie was removed.");
ok(!Services.cookies.cookieExists(COOKIE), "Recent cookie was removed.");
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was removed.");
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
// Clear cache and cookies with hostnames value.
await setUpCookies();
@ -196,9 +196,9 @@ add_task(async function testCacheAndCookies() {
await awaitNotification;
await extension.awaitMessage("cacheAndCookiesRemoved");
ok(Services.cookies.cookieExists(COOKIE), `Cookie ${COOKIE.name} was not removed.`);
ok(!Services.cookies.cookieExists(COOKIE_NET), `Cookie ${COOKIE_NET.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE_ORG), `Cookie ${COOKIE_ORG.name} was removed.`);
ok(Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), `Cookie ${COOKIE.name} was not removed.`);
ok(!Services.cookies.cookieExists(COOKIE_NET.host, COOKIE_NET.path, COOKIE_NET.name, {}), `Cookie ${COOKIE_NET.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE_ORG.host, COOKIE_ORG.path, COOKIE_ORG.name, {}), `Cookie ${COOKIE_ORG.name} was removed.`);
// Clear cache and cookies with (empty) hostnames value.
await setUpCookies();
@ -207,9 +207,9 @@ add_task(async function testCacheAndCookies() {
await awaitNotification;
await extension.awaitMessage("cacheAndCookiesRemoved");
ok(Services.cookies.cookieExists(COOKIE), `Cookie ${COOKIE.name} was not removed.`);
ok(Services.cookies.cookieExists(COOKIE_NET), `Cookie ${COOKIE_NET.name} was not removed.`);
ok(Services.cookies.cookieExists(COOKIE_ORG), `Cookie ${COOKIE_ORG.name} was not removed.`);
ok(Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), `Cookie ${COOKIE.name} was not removed.`);
ok(Services.cookies.cookieExists(COOKIE_NET.host, COOKIE_NET.path, COOKIE_NET.name, {}), `Cookie ${COOKIE_NET.name} was not removed.`);
ok(Services.cookies.cookieExists(COOKIE_ORG.host, COOKIE_ORG.path, COOKIE_ORG.name, {}), `Cookie ${COOKIE_ORG.name} was not removed.`);
// Clear cache and cookies with both hostnames and since values.
await setUpCookies();
@ -218,10 +218,10 @@ add_task(async function testCacheAndCookies() {
await awaitNotification;
await extension.awaitMessage("cacheAndCookiesRemoved");
ok(Services.cookies.cookieExists(oldCookie), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE), "Recent cookie was removed.");
ok(Services.cookies.cookieExists(COOKIE_NET), "Cookie with different hostname was not removed");
ok(Services.cookies.cookieExists(COOKIE_ORG), "Cookie with different hostname was not removed");
ok(Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
ok(Services.cookies.cookieExists(COOKIE_NET.host, COOKIE_NET.path, COOKIE_NET.name, {}), "Cookie with different hostname was not removed");
ok(Services.cookies.cookieExists(COOKIE_ORG.host, COOKIE_ORG.path, COOKIE_ORG.name, {}), "Cookie with different hostname was not removed");
// Clear cache and cookies with no since or hostnames value.
await setUpCookies();
@ -230,10 +230,10 @@ add_task(async function testCacheAndCookies() {
await awaitNotification;
await extension.awaitMessage("cacheAndCookiesRemoved");
ok(!Services.cookies.cookieExists(COOKIE), `Cookie ${COOKIE.name} was removed.`);
ok(!Services.cookies.cookieExists(oldCookie), `Cookie ${oldCookie.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE_NET), `Cookie ${COOKIE_NET.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE_ORG), `Cookie ${COOKIE_ORG.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), `Cookie ${COOKIE.name} was removed.`);
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), `Cookie ${oldCookie.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE_NET.host, COOKIE_NET.path, COOKIE_NET.name, {}), `Cookie ${COOKIE_NET.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE_ORG.host, COOKIE_ORG.path, COOKIE_ORG.name, {}), `Cookie ${COOKIE_ORG.name} was removed.`);
await extension.unload();
});

View File

@ -49,16 +49,12 @@ var SessionCookiesInternal = {
restore(cookies) {
for (let cookie of cookies) {
let expiry = "expiry" in cookie ? cookie.expiry : MAX_EXPIRY;
let cookieObj = {
host: cookie.host,
path: cookie.path || "",
name: cookie.name || "",
};
let originAttributes = cookie.originAttributes || {};
let exists = false;
try {
exists = Services.cookies.cookieExists(cookieObj, originAttributes);
exists = Services.cookies.cookieExists(cookie.host,
cookie.path || "",
cookie.name || "",
cookie.originAttributes || {});
} catch (ex) {
Cu.reportError(`nsCookieService::CookieExists failed with error '${ex}' for '${JSON.stringify(cookie)}'.`);
}
@ -66,7 +62,8 @@ var SessionCookiesInternal = {
try {
Services.cookies.add(cookie.host, cookie.path || "", cookie.name || "",
cookie.value, !!cookie.secure, !!cookie.httponly,
/* isSession = */ true, expiry, originAttributes,
/* isSession = */ true, expiry,
cookie.originAttributes || {},
Ci.nsICookie2.SAMESITE_UNSET);
} catch (ex) {
Cu.reportError(`nsCookieService::Add failed with error '${ex}' for cookie ${JSON.stringify(cookie)}.`);

View File

@ -32,7 +32,7 @@ function* do_run_test() {
Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
let enumerator = Services.cookiemgr.enumerator;
Assert.ok(enumerator.hasMoreElements());
let cookie = enumerator.getNext();
let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
Assert.ok(!enumerator.hasMoreElements());
// Fire 'profile-before-change'.
@ -69,7 +69,7 @@ function* do_run_test() {
}, Cr.NS_ERROR_NOT_AVAILABLE);
do_check_throws(function() {
Services.cookiemgr.cookieExists(cookie);
Services.cookiemgr.cookieExists(cookie.host, cookie.path, cookie.name, {});
}, Cr.NS_ERROR_NOT_AVAILABLE);
do_check_throws(function() {
@ -86,7 +86,7 @@ function* do_run_test() {
// Load the profile and check that the API is available.
do_load_profile();
Assert.ok(Services.cookiemgr.cookieExists(cookie));
Assert.ok(Services.cookiemgr.cookieExists(cookie.host, cookie.path, cookie.name, {}));
finish_test();
}

View File

@ -25,7 +25,7 @@ let since, oldCookie;
function addCookie(cookie) {
let expiry = Date.now() / 1000 + 10000;
Services.cookies.add(cookie.host, cookie.path, cookie.name, "test", false, false, false, expiry, {}, Ci.nsICookie2.SAMESITE_UNSET);
ok(Services.cookies.cookieExists(cookie), `Cookie ${cookie.name} was created.`);
ok(Services.cookies.cookieExists(cookie.host, cookie.path, cookie.name, {}), `Cookie ${cookie.name} was created.`);
}
async function setUpCookies() {
@ -65,8 +65,8 @@ add_task(async function testCookies() {
extension.sendMessage(method, {since});
await extension.awaitMessage("cookiesRemoved");
ok(Services.cookies.cookieExists(oldCookie), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE), "Recent cookie was removed.");
ok(Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was not removed.");
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
// Clear cookies with an old since value.
await setUpCookies();
@ -74,8 +74,8 @@ add_task(async function testCookies() {
extension.sendMessage(method, {since: since - 100000});
await extension.awaitMessage("cookiesRemoved");
ok(!Services.cookies.cookieExists(oldCookie), "Old cookie was removed.");
ok(!Services.cookies.cookieExists(COOKIE), "Recent cookie was removed.");
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was removed.");
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
// Clear cookies with no since value and valid originTypes.
await setUpCookies();
@ -84,8 +84,8 @@ add_task(async function testCookies() {
{originTypes: {unprotectedWeb: true, protectedWeb: false}});
await extension.awaitMessage("cookiesRemoved");
ok(!Services.cookies.cookieExists(COOKIE), `Cookie ${COOKIE.name} was removed.`);
ok(!Services.cookies.cookieExists(oldCookie), `Cookie ${oldCookie.name} was removed.`);
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), `Cookie ${COOKIE.name} was removed.`);
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), `Cookie ${oldCookie.name} was removed.`);
}
await extension.startup();

View File

@ -4736,35 +4736,31 @@ nsCookieService::PurgeCookies(int64_t aCurrentTimeInUsec)
// find whether a given cookie has been previously set. this is provided by the
// nsICookieManager interface.
NS_IMETHODIMP
nsCookieService::CookieExists(nsICookie2* aCookie,
nsCookieService::CookieExists(const nsACString& aHost,
const nsACString& aPath,
const nsACString& aName,
JS::HandleValue aOriginAttributes,
JSContext* aCx,
uint8_t aArgc,
bool* aFoundCookie)
{
NS_ENSURE_ARG_POINTER(aCookie);
NS_ENSURE_ARG_POINTER(aCx);
NS_ENSURE_ARG_POINTER(aFoundCookie);
MOZ_ASSERT(aArgc == 0 || aArgc == 1);
OriginAttributes attrs;
nsresult rv = InitializeOriginAttributes(&attrs,
aOriginAttributes,
aCx,
aArgc,
u"nsICookieManager.cookieExists()",
u"2");
NS_ENSURE_SUCCESS(rv, rv);
return CookieExistsNative(aCookie, &attrs, aFoundCookie);
if (!aOriginAttributes.isObject() ||
!attrs.Init(aCx, aOriginAttributes)) {
return NS_ERROR_INVALID_ARG;
}
return CookieExistsNative(aHost, aPath, aName, &attrs, aFoundCookie);
}
NS_IMETHODIMP_(nsresult)
nsCookieService::CookieExistsNative(nsICookie2* aCookie,
nsCookieService::CookieExistsNative(const nsACString& aHost,
const nsACString& aPath,
const nsACString& aName,
OriginAttributes* aOriginAttributes,
bool* aFoundCookie)
{
NS_ENSURE_ARG_POINTER(aCookie);
NS_ENSURE_ARG_POINTER(aOriginAttributes);
NS_ENSURE_ARG_POINTER(aFoundCookie);
@ -4778,21 +4774,15 @@ nsCookieService::CookieExistsNative(nsICookie2* aCookie,
AutoRestore<DBState*> savePrevDBState(mDBState);
mDBState = (aOriginAttributes->mPrivateBrowsingId > 0) ? mPrivateDBState : mDefaultDBState;
nsAutoCString host, name, path;
nsresult rv = aCookie->GetHost(host);
NS_ENSURE_SUCCESS(rv, rv);
rv = aCookie->GetName(name);
NS_ENSURE_SUCCESS(rv, rv);
rv = aCookie->GetPath(path);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString baseDomain;
rv = GetBaseDomainFromHost(mTLDService, host, baseDomain);
nsresult rv = GetBaseDomainFromHost(mTLDService, aHost, baseDomain);
NS_ENSURE_SUCCESS(rv, rv);
nsListIter iter;
*aFoundCookie = FindCookie(nsCookieKey(baseDomain, *aOriginAttributes),
host, name, path, iter);
PromiseFlatCString(aHost),
PromiseFlatCString(aName),
PromiseFlatCString(aPath), iter);
return NS_OK;
}

View File

@ -147,23 +147,28 @@ interface nsICookieManager : nsISupports
/**
* Find whether a given cookie already exists.
*
* @param aCookie
* the cookie to look for
* @param aHost
* the cookie's host to look for
* @param aPath
* the cookie's path to look for
* @param aName
* the cookie's name to look for
* @param aOriginAttributes
* nsICookie2 contains an originAttributes but if nsICookie2 is
* implemented in JS, we can't retrieve its originAttributes because
* the getter is marked [implicit_jscontext]. This optional parameter
* is a workaround.
* the cookie's originAttributes to look for
*
* @return true if a cookie was found which matches the host, path, and name
* fields of aCookie
* @return true if a cookie was found which matches the host, path, name and
* originAttributes fields of aCookie
*/
[implicit_jscontext, optional_argc]
boolean cookieExists(in nsICookie2 aCookie,
[optional] in jsval aOriginAttributes);
[implicit_jscontext]
boolean cookieExists(in AUTF8String aHost,
in AUTF8String aPath,
in ACString aName,
in jsval aOriginAttributes);
[notxpcom]
nsresult cookieExistsNative(in nsICookie2 aCookie,
nsresult cookieExistsNative(in AUTF8String aHost,
in AUTF8String aPath,
in ACString aName,
in OriginAttributesPtr aOriginAttributes,
out boolean aExists);

View File

@ -715,7 +715,10 @@ TEST(TestCookie,TestCookieMain)
EXPECT_EQ(hostCookies, 2u);
// check CookieExistsNative() using the third cookie
bool found;
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(newDomainCookie, &attrs, &found)));
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(NS_LITERAL_CSTRING("new.domain"),
NS_LITERAL_CSTRING("/rabbit"),
NS_LITERAL_CSTRING("test3"),
&attrs, &found)));
EXPECT_TRUE(found);
@ -725,7 +728,10 @@ TEST(TestCookie,TestCookieMain)
NS_LITERAL_CSTRING("/rabbit"), // path
true, // is blocked
&attrs))); // originAttributes
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(newDomainCookie, &attrs, &found)));
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(NS_LITERAL_CSTRING("new.domain"),
NS_LITERAL_CSTRING("/rabbit"),
NS_LITERAL_CSTRING("test3"),
&attrs, &found)));
EXPECT_FALSE(found);
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->AddNative(NS_LITERAL_CSTRING("new.domain"), // domain
NS_LITERAL_CSTRING("/rabbit"), // path
@ -737,7 +743,10 @@ TEST(TestCookie,TestCookieMain)
INT64_MIN, // expiry time
&attrs, // originAttributes
nsICookie2::SAMESITE_UNSET)));
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(newDomainCookie, &attrs, &found)));
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(NS_LITERAL_CSTRING("new.domain"),
NS_LITERAL_CSTRING("/rabbit"),
NS_LITERAL_CSTRING("test3"),
&attrs, &found)));
EXPECT_FALSE(found);
// sleep four seconds, to make sure the second cookie has expired
PR_Sleep(4 * PR_TicksPerSecond());
@ -745,7 +754,10 @@ TEST(TestCookie,TestCookieMain)
// expired cookie
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CountCookiesFromHost(NS_LITERAL_CSTRING("cookiemgr.test"), &hostCookies)));
EXPECT_EQ(hostCookies, 2u);
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(expiredCookie, &attrs, &found)));
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr2->CookieExistsNative(NS_LITERAL_CSTRING("cookiemgr.test"),
NS_LITERAL_CSTRING("/foo"),
NS_LITERAL_CSTRING("test2"),
&attrs, &found)));
EXPECT_TRUE(found);
// double-check RemoveAll() using the enumerator
EXPECT_TRUE(NS_SUCCEEDED(cookieMgr->RemoveAll()));

View File

@ -28,7 +28,7 @@ function run_test() {
}
};
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
Assert.ok(!cm.cookieExists(cookie));
Assert.ok(!cm.cookieExists(cookie.host, cookie.path, cookie.name, {}));
// if the above line does not crash, the test was successful
do_test_finished();
}

View File

@ -56,12 +56,7 @@ function add_cookie(aDomain) {
* True if the cookie should exist, false otherwise.
*/
function check_cookie_exists(aDomain, aExists) {
let cookie = {
host: aDomain,
name: COOKIE_NAME,
path: COOKIE_PATH,
};
Assert.equal(aExists, Services.cookies.cookieExists(cookie));
Assert.equal(aExists, Services.cookies.cookieExists(aDomain, COOKIE_PATH, COOKIE_NAME, {}));
}
/**