Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Commit

Permalink
Bug 1260399 - Remove deprecated messages in nsICookieManager methods …
Browse files Browse the repository at this point in the history
…- part 3 - nsICookieManager.cookieExists, r=ehsan
  • Loading branch information
bakulf committed Sep 24, 2018
1 parent 266cc39 commit 8901816
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -121,17 +121,17 @@ 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();
addCookie(COOKIE);
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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
});
15 changes: 6 additions & 9 deletions browser/components/sessionstore/SessionCookies.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,21 @@ 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)}'.`);
}
if (!exists) {
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)}.`);
Expand Down
6 changes: 3 additions & 3 deletions extensions/cookie/test/unit/test_cookies_profile_close.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -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() {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
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() {
Expand Down Expand Up @@ -65,17 +65,17 @@
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();
addCookie(COOKIE);
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();
Expand All @@ -84,8 +84,8 @@
{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();
Expand Down
40 changes: 15 additions & 25 deletions netwerk/cookie/nsCookieService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}

Expand Down
29 changes: 17 additions & 12 deletions netwerk/cookie/nsICookieManager.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 8901816

Please sign in to comment.