Skip to content

Commit

Permalink
Don't revoke <all_urls> permission until all related ocnfigs become f…
Browse files Browse the repository at this point in the history
…alse
  • Loading branch information
piroor committed Dec 3, 2024
1 parent 88e1707 commit 9e190a0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
33 changes: 13 additions & 20 deletions webextensions/background/browser-action-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2341,10 +2341,11 @@ if (browser.action/* Manifest V2 */ || browser.browserAction/* Manifest V3 */) {
if (item.permissions) {
Permissions.isGranted(item.permissions)
.then(async granted => {
if (item.checked == granted)
const checked = granted && (!('key' in item) || checkedFromConfigs);
if (checked == granted)
return;
item.checked = granted && (!('key' in item) || checkedFromConfigs);
await browser.menus.update(item.id, { checked: granted }).catch(ApiTabs.createErrorSuppressor());
item.checked = checked;
await browser.menus.update(item.id, { checked }).catch(ApiTabs.createErrorSuppressor());
await browser.menus.refresh().catch(ApiTabs.createErrorSuppressor());
});
delete params.checked;
Expand All @@ -2371,13 +2372,16 @@ if (browser.action/* Manifest V2 */ || browser.browserAction/* Manifest V3 */) {
browser.tabs.create({ url: item.url });
return;
}
if (item.key) {
if (info.checked)
configs[item.key] = 'value' in item ? item.value : true;
else if (!('value' in item))
configs[item.key] = false;
}
if (item.permissions) {
if (item.checked) {
if (item.canRevoke === false)
return;
browser.permissions.remove(item.permissions).catch(ApiTabs.createErrorSuppressor());
if (item.key && !('value' in item))
configs[item.key] = false;
if (!info.checked) {
if (item.canRevoke !== false)
browser.permissions.remove(item.permissions).catch(ApiTabs.createErrorSuppressor());
}
else {
browser.permissions.request(item.permissions)
Expand All @@ -2389,21 +2393,10 @@ if (browser.action/* Manifest V2 */ || browser.browserAction/* Manifest V3 */) {
type: Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED,
permissions: item.permissions
}).catch(_error => {});
if (item.key)
configs[item.key] = 'value' in item ? item.value : granted;
}
else {
if (item.key && !('value' in item))
configs[item.key] = false;
}
})
.catch(ApiTabs.createErrorHandler());
}
return;
}
if (item.key) {
configs[item.key] = 'value' in item ? item.value : !configs[item.key];
return;
}
});

Expand Down
45 changes: 29 additions & 16 deletions webextensions/common/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,28 @@ export function bindToCheckbox(permissions, checkbox, options = {}) {
const checkboxes = checkboxesForPermission.get(permissions);
try {
if (!checkbox.checked) {
const revoked = (
typeof options.canRevoke != 'function' ||
options.canRevoke()
);
if (!revoked)
if (checkbox.dataset.relatedConfigKey)
configs[checkbox.dataset.relatedConfigKey] = false;
if (options.onChanged)
options.onChanged(false);
const canRevoke = Array.from(checkboxes, checkbox => checkbox.dataset.relatedConfigKey ? configs[checkbox.dataset.relatedConfigKey] : null).filter(state => state !== null).every(state => !state);
if (!canRevoke)
return;
await browser.permissions.remove(permissions).catch(ApiTabs.createErrorSuppressor());
for (const checkbox of checkboxes) {
checkbox.checked = false;
for (const otherCheckbox of checkboxes) {
if (otherCheckbox != checkbox &&
otherCheckbox.dataset.relatedConfigKey)
continue;
otherCheckbox.checked = false;
}
if (options.onChanged)
options.onChanged(false);
return;
}

for (const checkbox of checkboxes) {
checkbox.checked = false;
for (const otherCheckbox of checkboxes) {
if (otherCheckbox != checkbox &&
otherCheckbox.dataset.relatedConfigKey)
continue;
otherCheckbox.checked = false;
}

if (configs.requestingPermissionsNatively)
Expand All @@ -190,11 +195,19 @@ export function bindToCheckbox(permissions, checkbox, options = {}) {
return;

if (granted) {
const checked = options.onChanged ?
options.onChanged(true) :
undefined;
for (const checkbox of checkboxes) {
checkbox.checked = checked !== undefined ? !!checked : true;
if (checkbox.dataset.relatedConfigKey)
configs[checkbox.dataset.relatedConfigKey] = true;
const configValue = !!checkbox.dataset.relatedConfigKey;
const onChangedResult = options.onChanged && options.onChanged(true);
const checked = configValue !== null ? configValue :
options.onChanged ?
onChangedResult :
undefined;
for (const otherCheckbox of checkboxes) {
if (otherCheckbox != checkbox &&
otherCheckbox.dataset.relatedConfigKey)
continue;
otherCheckbox.checked = checked !== undefined ? !!checked : true;
}
browser.runtime.sendMessage({
type: Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED,
Expand Down
14 changes: 1 addition & 13 deletions webextensions/options/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,27 +783,15 @@ function initPermissionOptions() {

Permissions.bindToCheckbox(
Permissions.ALL_URLS,
document.querySelector('#allUrlsPermissionGranted_tabPreviewTooltip'),
{
onChanged(granted) {
configs.tabPreviewTooltip = granted;
},
canRevoke() {
return !configs.tabPreviewTooltip && !configs.skipCollapsedTabsForTabSwitchingShortcuts;
},
}
document.querySelector('#allUrlsPermissionGranted_tabPreviewTooltip')
);
Permissions.bindToCheckbox(
Permissions.ALL_URLS,
document.querySelector('#allUrlsPermissionGranted_ctrlTabTracking'),
{
onChanged(granted) {
configs.skipCollapsedTabsForTabSwitchingShortcuts = granted;
updateCtrlTabSubItems(granted);
},
canRevoke() {
return !configs.tabPreviewTooltip && !configs.skipCollapsedTabsForTabSwitchingShortcuts;
},
}
);

Expand Down

0 comments on commit 9e190a0

Please sign in to comment.