Skip to content

Commit

Permalink
Bug 1631676 - Part 1: add cookie behavior pref getter; r=baku
Browse files Browse the repository at this point in the history
  • Loading branch information
xeonchen committed May 19, 2020
1 parent 85a9276 commit 0c371a2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
21 changes: 21 additions & 0 deletions netwerk/cookie/CookieService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@

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 net {

Expand Down Expand Up @@ -249,6 +263,13 @@ CookieService::Observe(nsISupports* /*aSubject*/, const char* aTopic,
return NS_OK;
}

NS_IMETHODIMP
CookieService::GetCookieBehavior(uint32_t* aCookieBehavior) {
NS_ENSURE_ARG_POINTER(aCookieBehavior);
*aCookieBehavior = nsICookieManager::GetCookieBehavior();
return NS_OK;
}

NS_IMETHODIMP
CookieService::GetCookieStringForPrincipal(nsIPrincipal* aPrincipal,
nsACString& aCookie) {
Expand Down
8 changes: 8 additions & 0 deletions netwerk/cookie/nsICookieManager.idl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ interface nsICookieManager : nsISupports
*/
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
* by host, name, and path. If the cookie cannot be found, no exception
Expand Down
Original file line number Diff line number Diff line change
@@ -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
);
}
});
1 change: 1 addition & 0 deletions toolkit/components/antitracking/test/xpcshell/xpcshell.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[DEFAULT]
head = head.js ../../../../components/url-classifier/tests/unit/head_urlclassifier.js

[test_cookie_behavior.js]
[test_purge_trackers.js]
[test_tracking_db_service.js]
[test_rejectForeignAllowList.js]
Expand Down

0 comments on commit 0c371a2

Please sign in to comment.