diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-02-08 17:52:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-08 17:52:41 -0500 |
commit | 849e4fabe1dffc2851fcb338dae8400d6c8e46ca (patch) | |
tree | e49dbd86ec20a2a11fc8938b8f6a4e1a9c57501e /ext/bg/js/permissions-main.js | |
parent | 008809e0e73665249b7b3dd7c14a1761aa06bd39 (diff) |
Native messaging optional permission (#1348)
* Refactor PermissionsToggleController to not require a setting
* Update nativeMessaging to be optional on Chrome
* Update parsing.enableMecabParser setting to request permissions
* Update permissions page to use PermissionsToggleController
* Update permissions documentation
* Disable toggle for permissions which are not optional
Diffstat (limited to 'ext/bg/js/permissions-main.js')
-rw-r--r-- | ext/bg/js/permissions-main.js | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/ext/bg/js/permissions-main.js b/ext/bg/js/permissions-main.js index 366a057b..5b17a5dd 100644 --- a/ext/bg/js/permissions-main.js +++ b/ext/bg/js/permissions-main.js @@ -17,6 +17,8 @@ /* global * DocumentFocusController + * PermissionsToggleController + * SettingsController * api */ @@ -36,45 +38,24 @@ async function isAllowedFileSchemeAccess() { return await new Promise((resolve) => chrome.extension.isAllowedFileSchemeAccess(resolve)); } -function hasPermissions(permissions) { - return new Promise((resolve) => chrome.permissions.contains({permissions}, (result) => { - const e = chrome.runtime.lastError; - resolve(!e && result); - })); -} - -function 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); - } - })) - ); -} +function setupPermissionsToggles() { + const manifest = chrome.runtime.getManifest(); + let optionalPermissions = manifest.optional_permissions; + if (!Array.isArray(optionalPermissions)) { optionalPermissions = []; } + optionalPermissions = new Set(optionalPermissions); -function setupPermissionCheckbox(checkbox, permissions) { - checkbox.addEventListener('change', (e) => { - updatePermissionCheckbox(checkbox, permissions, e.currentTarget.checked); - }, false); -} + const hasAllPermisions = (set, values) => { + for (const value of values) { + if (!set.has(value)) { return false; } + } + return true; + }; -async function updatePermissionCheckbox(checkbox, permissions, value) { - checkbox.checked = !value; - const hasPermission = await setPermissionsGranted(permissions, value); - checkbox.checked = hasPermission; + for (const toggle of document.querySelectorAll('.permissions-toggle')) { + let permissions = toggle.dataset.requiredPermissions; + permissions = (typeof permissions === 'string' && permissions.length > 0 ? permissions.split(' ') : []); + toggle.disabled = !hasAllPermisions(optionalPermissions, permissions); + } } (async () => { @@ -82,6 +63,8 @@ async function updatePermissionCheckbox(checkbox, permissions, value) { const documentFocusController = new DocumentFocusController(); documentFocusController.prepare(); + setupPermissionsToggles(); + for (const node of document.querySelectorAll('.extension-id-example')) { node.textContent = chrome.runtime.getURL('/'); } @@ -92,13 +75,11 @@ async function updatePermissionCheckbox(checkbox, permissions, value) { setupEnvironmentInfo(); const permissionsCheckboxes = [ - document.querySelector('#permission-checkbox-clipboard-read'), document.querySelector('#permission-checkbox-allow-in-private-windows'), document.querySelector('#permission-checkbox-allow-file-url-access') ]; const permissions = await Promise.all([ - hasPermissions(['clipboardRead']), isAllowedIncognitoAccess(), isAllowedFileSchemeAccess() ]); @@ -107,7 +88,11 @@ async function updatePermissionCheckbox(checkbox, permissions, value) { permissionsCheckboxes[i].checked = permissions[i]; } - setupPermissionCheckbox(permissionsCheckboxes[0], ['clipboardRead']); + const settingsController = new SettingsController(0); + settingsController.prepare(); + + const permissionsToggleController = new PermissionsToggleController(settingsController); + permissionsToggleController.prepare(); await promiseTimeout(100); |