Bug 1631676 - Part 1: add cookie behavior pref getter; r=baku

Differential Revision: https://phabricator.services.mozilla.com/D74049
This commit is contained in:
Liang-Heng Chen 2020-05-19 14:51:10 +00:00
parent 85a927675b
commit 0c371a2e76
4 changed files with 70 additions and 0 deletions

View File

@ -30,6 +30,20 @@
using namespace mozilla::dom; using namespace mozilla::dom;
// static
uint32_t nsICookieManager::GetCookieBehavior() {
bool isFirstPartyIsolated = OriginAttributes::IsFirstPartyEnabled();
uint32_t cookieBehavior =
mozilla::StaticPrefs::network_cookie_cookieBehavior();
if (isFirstPartyIsolated &&
cookieBehavior ==
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN) {
cookieBehavior = nsICookieService::BEHAVIOR_REJECT_TRACKER;
}
return cookieBehavior;
}
namespace mozilla { namespace mozilla {
namespace net { namespace net {
@ -249,6 +263,13 @@ CookieService::Observe(nsISupports* /*aSubject*/, const char* aTopic,
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
CookieService::GetCookieBehavior(uint32_t* aCookieBehavior) {
NS_ENSURE_ARG_POINTER(aCookieBehavior);
*aCookieBehavior = nsICookieManager::GetCookieBehavior();
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
CookieService::GetCookieStringForPrincipal(nsIPrincipal* aPrincipal, CookieService::GetCookieStringForPrincipal(nsIPrincipal* aPrincipal,
nsACString& aCookie) { nsACString& aCookie) {

View File

@ -46,6 +46,14 @@ interface nsICookieManager : nsISupports
*/ */
readonly attribute Array<nsICookie> sessionCookies; readonly attribute Array<nsICookie> sessionCookies;
/**
* Returns current effective value of the "network.cookie.cookieBehavior".
*/
readonly attribute uint32_t cookieBehavior;
%{C++
static uint32_t GetCookieBehavior();
%}
/** /**
* Called to remove an individual cookie from the cookie list, specified * Called to remove an individual cookie from the cookie list, specified
* by host, name, and path. If the cookie cannot be found, no exception * by host, name, and path. If the cookie cannot be found, no exception

View File

@ -0,0 +1,40 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Note: This test may cause intermittents if run at exactly midnight.
"use strict";
const PREF_FPI = "privacy.firstparty.isolate";
const PREF_COOKIE_BEHAVIOR = "network.cookie.cookieBehavior";
registerCleanupFunction(() => {
Services.prefs.clearUserPref(PREF_FPI);
Services.prefs.clearUserPref(PREF_COOKIE_BEHAVIOR);
});
add_task(function test_FPI_off() {
Services.prefs.setBoolPref(PREF_FPI, false);
for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR, i);
equal(Services.prefs.getIntPref(PREF_COOKIE_BEHAVIOR), i);
equal(Services.cookies.cookieBehavior, i);
}
});
add_task(function test_FPI_on() {
Services.prefs.setBoolPref(PREF_FPI, true);
for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR, i);
equal(Services.prefs.getIntPref(PREF_COOKIE_BEHAVIOR), i);
equal(
Services.cookies.cookieBehavior,
i == Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN
? Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
: i
);
}
});

View File

@ -1,6 +1,7 @@
[DEFAULT] [DEFAULT]
head = head.js ../../../../components/url-classifier/tests/unit/head_urlclassifier.js head = head.js ../../../../components/url-classifier/tests/unit/head_urlclassifier.js
[test_cookie_behavior.js]
[test_purge_trackers.js] [test_purge_trackers.js]
[test_tracking_db_service.js] [test_tracking_db_service.js]
[test_rejectForeignAllowList.js] [test_rejectForeignAllowList.js]