Permissions requesting fixes (#1334)

* Fix permissions requests not occurring during user interaction

* Update toggle after changing permissions

* Update functions

* Update clipboard requests
This commit is contained in:
toasted-nutbread 2021-01-30 15:05:08 -05:00 committed by GitHub
parent a802666d8d
commit 828c4b11c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 23 deletions

View File

@ -36,21 +36,47 @@ async function isAllowedFileSchemeAccess() {
return await new Promise((resolve) => chrome.extension.isAllowedFileSchemeAccess(resolve)); return await new Promise((resolve) => chrome.extension.isAllowedFileSchemeAccess(resolve));
} }
async function hasPermissions(permissions) { function hasPermissions(permissions) {
return await new Promise((resolve) => chrome.permissions.contains({permissions}, resolve)); return new Promise((resolve) => chrome.permissions.contains({permissions}, (result) => {
const e = chrome.runtime.lastError;
resolve(!e && result);
}));
} }
async function setPermissionsGranted(permissions, shouldHave) { function setPermissionsGranted(permissions, shouldHave) {
const has = await hasPermissions(permissions); return (
if (shouldHave === has) { return has; }
return await (
shouldHave ? shouldHave ?
new Promise((resolve) => chrome.permissions.request({permissions}, resolve)) : new Promise((resolve, reject) => chrome.permissions.request({permissions}, (result) => {
new Promise((resolve) => chrome.permissions.remove({permissions}, (v) => resolve(!v))) const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(result);
}
})) :
new Promise((resolve, reject) => chrome.permissions.remove({permissions}, (result) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(!result);
}
}))
); );
} }
function setupPermissionCheckbox(checkbox, permissions) {
checkbox.addEventListener('change', (e) => {
updatePermissionCheckbox(checkbox, permissions, e.currentTarget.checked);
}, false);
}
async function updatePermissionCheckbox(checkbox, permissions, value) {
checkbox.checked = !value;
const hasPermission = await setPermissionsGranted(permissions, value);
checkbox.checked = hasPermission;
}
(async () => { (async () => {
try { try {
const documentFocusController = new DocumentFocusController(); const documentFocusController = new DocumentFocusController();
@ -79,9 +105,7 @@ async function setPermissionsGranted(permissions, shouldHave) {
permissionsCheckboxes[i].checked = permissions[i]; permissionsCheckboxes[i].checked = permissions[i];
} }
permissionsCheckboxes[0].addEventListener('change', (e) => { setupPermissionCheckbox(permissionsCheckboxes[0], ['clipboardRead']);
setPermissionsGranted(['clipboardRead'], e.currentTarget.checked);
});
await promiseTimeout(100); await promiseTimeout(100);

View File

@ -339,14 +339,7 @@ class AnkiController {
} }
async _requestClipboardReadPermission() { async _requestClipboardReadPermission() {
const permissions = ['clipboardRead']; return await this._settingsController.setPermissionsGranted(['clipboardRead'], true);
if (await new Promise((resolve) => chrome.permissions.contains({permissions}, resolve))) {
// Already has permission
return;
}
return await new Promise((resolve) => chrome.permissions.request({permissions}, resolve));
} }
_getFieldMarkers(fieldValue) { _getFieldMarkers(fieldValue) {

View File

@ -58,9 +58,7 @@ class ClipboardPopupsController {
let value = checkbox.checked; let value = checkbox.checked;
if (value) { if (value) {
value = await new Promise((resolve) => { value = await this._settingsController.setPermissionsGranted(['clipboardRead'], true);
chrome.permissions.request({permissions: ['clipboardRead']}, resolve);
});
checkbox.checked = value; checkbox.checked = value;
} }

View File

@ -132,6 +132,39 @@ class SettingsController extends EventDispatcher {
return optionsFull; return optionsFull;
} }
hasPermissions(permissions) {
return new Promise((resolve, reject) => chrome.permissions.contains({permissions}, (result) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(result);
}
}));
}
setPermissionsGranted(permissions, shouldHave) {
return (
shouldHave ?
new Promise((resolve, reject) => chrome.permissions.request({permissions}, (result) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(result);
}
})) :
new Promise((resolve, reject) => chrome.permissions.remove({permissions}, (result) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(!result);
}
}))
);
}
// Private // Private
_setProfileIndex(value) { _setProfileIndex(value) {