Bug 1452361 - Don't reset to default permissions for cookies in page info. r=nhnt11

This commit makes the page info window treat cookie permissions a little
differently, to reflect that the "default" value for cookies is a combination
of two prefs that doesn't strictly map onto the ALLOW/SESSION/DENY system of
permissions.

I also added some more general pageinfo permissions tests.

MozReview-Commit-ID: 80vd61Rv867

--HG--
extra : rebase_source : c079e47afb74b2c4b7da271efcaf20dd45d1eb60
This commit is contained in:
Johann Hofmann 2018-05-09 15:39:26 +02:00
parent 6c6cc479c4
commit 8c9cbbf7a1
4 changed files with 141 additions and 7 deletions

View File

@ -63,6 +63,27 @@ function initRow(aPartId) {
var {state, scope} = SitePermissions.get(gPermURI, aPartId);
let defaultState = SitePermissions.getDefault(aPartId);
// Since cookies preferences have many different possible configuration states
// we don't consider any permission except "no permission" to be default.
if (aPartId == "cookie") {
state = Services.perms.testPermissionFromPrincipal(gPermPrincipal, "cookie");
if (state == SitePermissions.UNKNOWN) {
checkbox.checked = true;
command.setAttribute("disabled", "true");
// Don't select any item in the radio group, as we can't
// confidently say that all cookies on the site will be allowed.
let radioGroup = document.getElementById("cookieRadioGroup");
radioGroup.selectedItem = null;
} else {
checkbox.checked = false;
command.removeAttribute("disabled");
}
setRadioState(aPartId, state);
return;
}
// When flash permission state is "Hide", we show it as "Always Ask" in page info.
if (aPartId.startsWith("plugin") && state == SitePermissions.PROMPT_HIDE) {
defaultState == SitePermissions.UNKNOWN ? state = defaultState : state = SitePermissions.PROMPT;
@ -146,8 +167,6 @@ function onCheckboxClick(aPartId) {
if (checkbox.checked) {
SitePermissions.remove(gPermURI, aPartId);
command.setAttribute("disabled", "true");
var perm = SitePermissions.getDefault(aPartId);
setRadioState(aPartId, perm);
} else {
onRadioClick(aPartId);
command.removeAttribute("disabled");
@ -156,7 +175,7 @@ function onCheckboxClick(aPartId) {
function onRadioClick(aPartId) {
var radioGroup = document.getElementById(aPartId + "RadioGroup");
var id = radioGroup.selectedItem.id;
var id = radioGroup.selectedItem ? radioGroup.selectedItem.id : "#1";
var permission = parseInt(id.split("#")[1]);
SitePermissions.set(gPermURI, aPartId, permission);
}

View File

@ -13,6 +13,7 @@ support-files =
[browser_pageinfo_image_info.js]
uses-unsafe-cpows = true
skip-if = (os == 'linux' && e10s) # bug 1161699
[browser_pageinfo_permissions.js]
[browser_pageinfo_security.js]
[browser_pageinfo_svg_image.js]
support-files =

View File

@ -0,0 +1,109 @@
ChromeUtils.import("resource:///modules/SitePermissions.jsm");
const TEST_ORIGIN = "https://example.com";
async function testPermissions(defaultPermission) {
await BrowserTestUtils.withNewTab(TEST_ORIGIN, async function(browser) {
let pageInfo = BrowserPageInfo(TEST_ORIGIN, "permTab");
await BrowserTestUtils.waitForEvent(pageInfo, "load");
let defaultCheckbox = await TestUtils.waitForCondition(() => pageInfo.document.getElementById("geoDef"));
let radioGroup = pageInfo.document.getElementById("geoRadioGroup");
let defaultRadioButton = pageInfo.document.getElementById("geo#" + defaultPermission);
let blockRadioButton = pageInfo.document.getElementById("geo#2");
ok(defaultCheckbox.checked, "The default checkbox should be checked.");
SitePermissions.set(gBrowser.currentURI, "geo", SitePermissions.BLOCK);
ok(!defaultCheckbox.checked, "The default checkbox should not be checked.");
defaultCheckbox.checked = true;
defaultCheckbox.dispatchEvent(new Event("command"));
is(SitePermissions.get(gBrowser.currentURI, "geo").state, defaultPermission,
"Checking the default checkbox should reset the permission.");
defaultCheckbox.checked = false;
defaultCheckbox.dispatchEvent(new Event("command"));
is(SitePermissions.get(gBrowser.currentURI, "geo").state, defaultPermission,
"Unchecking the default checkbox should pick the default permission.");
is(radioGroup.selectedItem, defaultRadioButton,
"The unknown radio button should be selected.");
radioGroup.selectedItem = blockRadioButton;
blockRadioButton.dispatchEvent(new Event("command"));
is(SitePermissions.get(gBrowser.currentURI, "geo").state, SitePermissions.BLOCK,
"Selecting a value in the radio group should set the corresponding permission");
radioGroup.selectedItem = defaultRadioButton;
defaultRadioButton.dispatchEvent(new Event("command"));
is(SitePermissions.get(gBrowser.currentURI, "geo").state, defaultPermission,
"Selecting the default value should reset the permission.");
ok(defaultCheckbox.checked, "The default checkbox should be checked.");
pageInfo.close();
SitePermissions.remove(gBrowser.currentURI, "geo");
});
}
// Test some standard operations in the permission tab.
add_task(async function test_geo_permission() {
await testPermissions(SitePermissions.UNKNOWN);
});
// Test some standard operations in the permission tab, falling back to a custom
// default permission instead of UNKNOWN.
add_task(async function test_default_geo_permission() {
await SpecialPowers.pushPrefEnv({set: [["permissions.default.geo", SitePermissions.ALLOW]]});
await testPermissions(SitePermissions.ALLOW);
});
// Test special behavior for cookie permissions.
add_task(async function test_cookie_permission() {
await BrowserTestUtils.withNewTab(TEST_ORIGIN, async function(browser) {
let pageInfo = BrowserPageInfo(TEST_ORIGIN, "permTab");
await BrowserTestUtils.waitForEvent(pageInfo, "load");
let defaultCheckbox = await TestUtils.waitForCondition(() => pageInfo.document.getElementById("cookieDef"));
let radioGroup = pageInfo.document.getElementById("cookieRadioGroup");
let allowRadioButton = pageInfo.document.getElementById("cookie#1");
let blockRadioButton = pageInfo.document.getElementById("cookie#2");
ok(defaultCheckbox.checked, "The default checkbox should be checked.");
defaultCheckbox.checked = false;
defaultCheckbox.dispatchEvent(new Event("command"));
is(Services.perms.testPermission(gBrowser.currentURI, "cookie"), SitePermissions.ALLOW,
"Unchecking the default checkbox should pick the default permission.");
is(radioGroup.selectedItem, allowRadioButton,
"The unknown radio button should be selected.");
radioGroup.selectedItem = blockRadioButton;
blockRadioButton.dispatchEvent(new Event("command"));
is(Services.perms.testPermission(gBrowser.currentURI, "cookie"), SitePermissions.BLOCK,
"Selecting a value in the radio group should set the corresponding permission");
radioGroup.selectedItem = allowRadioButton;
allowRadioButton.dispatchEvent(new Event("command"));
is(Services.perms.testPermission(gBrowser.currentURI, "cookie"), SitePermissions.ALLOW,
"Selecting a value in the radio group should set the corresponding permission");
ok(!defaultCheckbox.checked, "The default checkbox should not be checked.");
defaultCheckbox.checked = true;
defaultCheckbox.dispatchEvent(new Event("command"));
is(Services.perms.testPermission(gBrowser.currentURI, "cookie"), SitePermissions.UNKNOWN,
"Checking the default checkbox should reset the permission.");
is(radioGroup.selectedItem, null, "For cookies, no item should be selected when the checkbox is checked.");
pageInfo.close();
SitePermissions.remove(gBrowser.currentURI, "cookie");
});
});

View File

@ -405,8 +405,13 @@ var SitePermissions = {
*/
set(uri, permissionID, state, scope = this.SCOPE_PERSISTENT, browser = null) {
if (state == this.UNKNOWN || state == this.getDefault(permissionID)) {
this.remove(uri, permissionID, browser);
return;
// Because they are controlled by two prefs with many states that do not
// correspond to the classical ALLOW/DENY/PROMPT model, we want to always
// allow the user to add exceptions to their cookie rules without removing them.
if (permissionID != "cookie") {
this.remove(uri, permissionID, browser);
return;
}
}
if (state == this.ALLOW_COOKIES_FOR_SESSION && permissionID != "cookie") {
@ -617,10 +622,10 @@ var gPermissionObject = {
"cookie": {
states: [ SitePermissions.ALLOW, SitePermissions.ALLOW_COOKIES_FOR_SESSION, SitePermissions.BLOCK ],
getDefault() {
if (Services.prefs.getIntPref("network.cookie.cookieBehavior") == 2)
if (Services.prefs.getIntPref("network.cookie.cookieBehavior") == Ci.nsICookieService.BEHAVIOR_REJECT)
return SitePermissions.BLOCK;
if (Services.prefs.getIntPref("network.cookie.lifetimePolicy") == 2)
if (Services.prefs.getIntPref("network.cookie.lifetimePolicy") == Ci.nsICookieService.ACCEPT_SESSION)
return SitePermissions.ALLOW_COOKIES_FOR_SESSION;
return SitePermissions.ALLOW;