More accurate algorithm to account for permissions changes in the UI triggering automatic reloads.

This commit is contained in:
hackademix 2019-05-26 15:42:00 +02:00
parent b06ec5d1c8
commit 23fb55bf38
3 changed files with 27 additions and 9 deletions

View File

@ -238,7 +238,20 @@ var {Permissions, Policy, Sites} = (() => {
}
return enabled;
}
sameAs(otherPerms) {
let otherCaps = new Set(otherPerms.capabilities);
let theseCaps = this.capabilities;
for (let c of theseCaps) {
if (!otherCaps.delete(c)) return false;
}
for (let c of otherCaps) {
if (!theseCaps.has(c)) return false;
}
return true;
}
clone() {
return new Permissions(this.capabilities, this.temp, this.context);
}
get tempTwin() {
return this._tempTwin || (this._tempTwin = new Permissions(this.capabilities, true, this.contextual));
}

View File

@ -164,7 +164,7 @@ addEventListener("unload", e => {
sitesUI = new UI.Sites(document.getElementById("sites"));
sitesUI.onChange = (row) => {
pendingReload(!row.temp2perm);
pendingReload(sitesUI.anyPermissionsChanged());
if (optionsClosed) return;
browser.tabs.query({
url: browser.extension.getURL(

View File

@ -346,6 +346,11 @@ var UI = (() => {
allSiteRows() {
return this.table.querySelectorAll("tr.site");
}
anyPermissionsChanged() {
return Array.from(this.allSiteRows()).some(row => row.permissionsChanged);
}
clear() {
debug("Clearing list", this.table);
@ -399,17 +404,16 @@ var UI = (() => {
let tempToggle = preset.parentNode.querySelector("input.temp");
if (ev.type === "change") {
row.temp2perm = false;
row.permissionsChanged = false;
if (!row._originalPerms) {
row._originalPerms = row.perms.clone();
}
let policy = UI.policy;
let presetValue = preset.value;
let policyPreset = presetValue.startsWith("T_") ? policy[presetValue.substring(2)].tempTwin : policy[presetValue];
if (policyPreset) {
if (row.perms !== policyPreset) {
row.temp2perm = row.perms &&
(policyPreset.tempTwin === row.perms || policyPreset === row.perms._tempTwin);
row.perms = policyPreset;
}
if (policyPreset && row.perms !== policyPreset) {
row.perms = policyPreset;
}
if (preset.checked) {
row.dataset.preset = preset.value;
@ -443,6 +447,7 @@ var UI = (() => {
this.customize(perms, preset, row);
}
}
row.permissionsChanged = !row.perms.sameAs(row._originalPerms);
fireOnChange(this, row);
} else if (!(isCap || isTemp) && ev.type === "click") {
this.customize(row.perms, preset, row);