Skip to content

Commit

Permalink
Request optional permissions without toolbar button.
Browse files Browse the repository at this point in the history
Original patch: piroor/treestyletab#2011
  • Loading branch information
piroor committed Aug 28, 2018
1 parent d91d275 commit b624f0b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
2 changes: 2 additions & 0 deletions webextensions/common/common.js
Expand Up @@ -89,6 +89,7 @@ export const configs = new Configs({
enableIntegrationWithTST: true,

requestingPermissions: null,
requestingPermissionsNatively: null,

shouldNotifyUpdatedFromLegacyVersion: false,
debug: false
Expand All @@ -100,6 +101,7 @@ export const configs = new Configs({
useWorkaroundForBug1272869
cachedExternalAddons
requestingPermissions
requestingPermissionsNatively
shouldNotifyUpdatedFromLegacyVersion
debug
`.trim().split('\n').map(key => key.trim()).filter(key => key && key.indexOf('//') != 0)
Expand Down
79 changes: 61 additions & 18 deletions webextensions/common/permissions.js
Expand Up @@ -21,22 +21,36 @@ export function clearRequest() {
}

export function isGranted(permissions) {
return browser.permissions.contains(permissions);
try {
return browser.permissions.contains(permissions);
}
catch(_e) {
return Promise.reject(new Error('unsupported permission'));
}
}

export function bindToCheckbox(permissions, checkbox, options = {}) {
isGranted(permissions).then(granted => {
checkbox.checked = granted;
});
isGranted(permissions)
.then(granted => {
checkbox.checked = granted;
})
.catch(_error => {
checkbox.setAttribute('readonly', true);
checkbox.setAttribute('disabled', true);
const label = checkbox.closest('label') || document.querySelector(`label[for=${checkbox.id}]`);
if (label)
label.setAttribute('disabled', true);
});

checkbox.addEventListener('change', _event => {
checkbox.requestPermissions()
});

browser.runtime.onMessage.addListener((message, _sender) => {
if (!message ||
!message.type ||
message.type != Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED ||
JSON.stringify(message.permissions) != JSON.stringify(permissions))
!message.type ||
message.type != Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED ||
JSON.stringify(message.permissions) != JSON.stringify(permissions))
return;
if (options.onChanged)
options.onChanged(true);
Expand Down Expand Up @@ -64,14 +78,37 @@ export function bindToCheckbox(permissions, checkbox, options = {}) {
return;
}

const granted = await isGranted(permissions);
checkbox.checked = false;
if (configs.requestingPermissionsNatively)
return;

// Following code will throw error on Firefox 60 and earlier (but not on Firefox ESR 60)
// due to https://bugzilla.mozilla.org/show_bug.cgi?id=1382953
// Also must not have used await before calling browser.permissions.request or it will throw an error.
let granted;
try {
configs.requestingPermissionsNatively = permissions;
granted = await browser.permissions.request(permissions);
}
catch (_error) {
}
finally {
configs.requestingPermissionsNatively = null;
}

if (granted === undefined)
granted = await isGranted(permissions);
else if (!granted)
return;

if (granted) {
options.onChanged(true);
checkbox.checked = true;
if (options.onChanged)
options.onChanged(true);
return;
}

configs.requestingPermissions = permissions;
checkbox.checked = false;
browser.browserAction.setBadgeText({ text: '!' });
browser.browserAction.setPopup({ popup: '' });

Expand Down Expand Up @@ -103,15 +140,21 @@ export function requestPostProcess() {

const permissions = configs.requestingPermissions;
configs.requestingPermissions = null;
configs.requestingPermissionsNatively = permissions;

browser.browserAction.setBadgeText({ text: '' });
browser.permissions.request(permissions).then(granted => {
log('permission requested: ', permissions, granted);
if (granted)
browser.runtime.sendMessage({
type: Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED,
permissions: permissions
});
});
browser.permissions.request(permissions)
.then(granted => {
log('permission requested: ', permissions, granted);
if (granted)
browser.runtime.sendMessage({
type: Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED,
permissions: permissions
});
})
.finally(() => {
configs.requestingPermissionsNatively = null;
});
return true;
}

Expand Down

0 comments on commit b624f0b

Please sign in to comment.