Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 1 | import actionApi from './actionApi.js'; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 2 | import optionsPrototype from './optionsPrototype.json5'; |
| 3 | import {getOptions} from './optionsUtils.js'; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 4 | |
| 5 | // Required permissions, including host permissions. |
| 6 | // |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 7 | // IMPORTANT: This should be kept in sync with the "permissions", |
| 8 | // "host_permissions" and "content_scripts" keys in //templates/manifest.gjson. |
| 9 | const requiredPermissions = { |
| 10 | permissions: new Set([ |
| 11 | 'storage', 'alarms', |
| 12 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
| 13 | 'declarativeNetRequestWithHostAccess', |
| 14 | // #!endif |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 15 | // #!if browser_target == 'chromium_mv3' |
| 16 | 'scripting', |
| 17 | // #!endif |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 18 | ]), |
| 19 | origins: new Set([ |
| 20 | // Host permissions: |
| 21 | 'https://support.google.com/*', |
| 22 | |
| 23 | // Content scripts matches: |
| 24 | 'https://support.google.com/s/community*', |
| 25 | 'https://support.google.com/*/threads*', |
| 26 | 'https://support.google.com/*/thread/*', |
Adrià Vilanova Martínez | 0f508c9 | 2024-02-28 23:45:22 +0100 | [diff] [blame] | 27 | 'https://support.google.com/*/community-guide/*', |
| 28 | 'https://support.google.com/*/community-video/*', |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 29 | 'https://support.google.com/*/profile/*', |
| 30 | 'https://support.google.com/profile/*', |
| 31 | ]), |
| 32 | }; |
| 33 | |
| 34 | const permissionTypes = ['origins', 'permissions']; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 35 | |
| 36 | // Returns an array of optional permissions needed by |feature|. |
| 37 | export function requiredOptPermissions(feature) { |
| 38 | if (!(feature in optionsPrototype)) { |
| 39 | console.error('"' + feature + '" feature doesn\'t exist.'); |
| 40 | return []; |
| 41 | } |
| 42 | |
| 43 | return optionsPrototype[feature]?.requiredOptPermissions ?? []; |
| 44 | } |
| 45 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 46 | // Returns a promise resolving to the optional permissions needed by all the |
| 47 | // current enabled features. |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 48 | export function currentRequiredOptPermissions() { |
| 49 | return getOptions(null, /* requireOptionalPermissions = */ false) |
| 50 | .then(options => { |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 51 | let permissions = { |
| 52 | origins: [], |
| 53 | permissions: [], |
| 54 | }; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 55 | |
| 56 | // For each option |
| 57 | for (const [opt, optMeta] of Object.entries(optionsPrototype)) |
| 58 | // If the option is enabled |
| 59 | if (options[opt]) |
| 60 | // Add its required optional permissions to the list |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 61 | for (const type of permissionTypes) |
| 62 | permissions[type].push( |
| 63 | ...(optMeta.requiredOptPermissions?.[type] ?? [])); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 64 | |
| 65 | return permissions; |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | // Ensures that all the optional permissions required by |feature| are granted, |
| 70 | // and requests them otherwise. It returns a promise which resolves specifying |
| 71 | // whether the permissions were granted or not. |
| 72 | export function ensureOptPermissions(feature) { |
| 73 | return new Promise((resolve, reject) => { |
| 74 | let permissions = requiredOptPermissions(feature); |
| 75 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 76 | chrome.permissions.contains(permissions, isAlreadyGranted => { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 77 | if (isAlreadyGranted) return resolve(true); |
| 78 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 79 | chrome.permissions.request(permissions, granted => { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 80 | // If there was an error, reject the promise. |
| 81 | if (granted === undefined) |
| 82 | return reject(new Error( |
| 83 | chrome.runtime.lastError.message ?? |
| 84 | 'An unknown error occurred while requesting the permisisons')); |
| 85 | |
| 86 | // If the permission is granted we should maybe remove the warning |
| 87 | // badge. |
| 88 | if (granted) cleanUpOptPermissions(/* removeLeftoverPerms = */ false); |
| 89 | |
| 90 | return resolve(granted); |
| 91 | }); |
| 92 | }); |
| 93 | }); |
| 94 | } |
| 95 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 96 | // Returns a promise resolving to the currently granted optional permissions |
| 97 | // (i.e. excluding required permissions). |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 98 | export function grantedOptPermissions() { |
| 99 | return new Promise((resolve, reject) => { |
| 100 | chrome.permissions.getAll(response => { |
| 101 | if (response === undefined) |
| 102 | return reject(new Error( |
| 103 | chrome.runtime.lastError.message ?? |
| 104 | 'An unknown error occurred while calling chrome.permissions.getAll()')); |
| 105 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 106 | let optPermissions = {}; |
| 107 | for (const type of permissionTypes) |
| 108 | optPermissions[type] = |
| 109 | response[type].filter(p => !requiredPermissions[type].has(p)); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 110 | resolve(optPermissions); |
| 111 | }); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | // Returns a promise resolving to an object with 2 properties: |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 116 | // - missingPermissions: optional permissions which are required by enabled |
| 117 | // features and haven't been granted yet. |
| 118 | // - leftoverPermissions: optional permissions which are granted but are no |
| 119 | // longer needed. |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 120 | export function diffPermissions() { |
| 121 | return Promise |
| 122 | .all([ |
| 123 | grantedOptPermissions(), |
| 124 | currentRequiredOptPermissions(), |
| 125 | ]) |
| 126 | .then(perms => { |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 127 | let diff = { |
| 128 | missingPermissions: {}, |
| 129 | leftoverPermissions: {}, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 130 | }; |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 131 | for (const type of permissionTypes) { |
| 132 | diff.missingPermissions[type] = |
| 133 | perms[1][type].filter(p => !perms[0][type].includes(p)); |
| 134 | diff.leftoverPermissions[type] = |
| 135 | perms[0][type].filter(p => !perms[1][type].includes(p)); |
| 136 | } |
| 137 | return diff; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 138 | }) |
| 139 | .catch(cause => { |
| 140 | throw new Error( |
| 141 | 'Couldn\'t compute the missing and leftover permissions.', {cause}); |
| 142 | }); |
| 143 | } |
| 144 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 145 | // Returns a promise which resolves to the required optional permissions of |
| 146 | // |feature| which are missing. |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 147 | // |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 148 | // Accepts an argument |grantedPermissions| with the granted permissions, |
| 149 | // otherwise the function will call grantedOptPermissions() to retrieve them. |
| 150 | // This can be used to prevent calling chrome.permissions.getAll() repeteadly. |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 151 | export function missingPermissions(feature, grantedPermissions = null) { |
| 152 | let grantedOptPermissionsPromise; |
| 153 | if (grantedPermissions !== null) |
| 154 | grantedOptPermissionsPromise = new Promise((res, rej) => { |
| 155 | res(grantedPermissions); |
| 156 | }); |
| 157 | else |
| 158 | grantedOptPermissionsPromise = grantedOptPermissions(); |
| 159 | |
| 160 | return Promise |
| 161 | .all([ |
| 162 | grantedOptPermissionsPromise, |
| 163 | requiredOptPermissions(feature), |
| 164 | ]) |
| 165 | .then(perms => { |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 166 | let missingPerms = {}; |
| 167 | for (const type of permissionTypes) |
| 168 | missingPerms[type] = |
| 169 | perms[1][type].filter(p => !perms[0][type].includes(p)) |
| 170 | return missingPerms; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 171 | }) |
| 172 | .catch(cause => { |
| 173 | throw new Error( |
| 174 | 'Couldn\'t compute the missing permissions for "' + feature + '",', |
| 175 | {cause}); |
| 176 | }); |
| 177 | } |
| 178 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 179 | // Returns true if permissions (a chrome.permissions.Permissions object) is |
| 180 | // empty (that is, if their properties have empty arrays). |
| 181 | export function isPermissionsObjectEmpty(permissions) { |
| 182 | for (const type of permissionTypes) { |
| 183 | if ((permissions[type]?.length ?? 0) > 0) return false; |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 188 | // Deletes optional permissions which are no longer needed by the current |
| 189 | // set of enabled features (if |removeLeftoverPerms| is set to true), and sets a |
| 190 | // badge if some needed permissions are missing. |
| 191 | export function cleanUpOptPermissions(removeLeftoverPerms = true) { |
| 192 | return diffPermissions() |
| 193 | .then(perms => { |
| 194 | let {missingPermissions, leftoverPermissions} = perms; |
| 195 | |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 196 | if (!isPermissionsObjectEmpty(missingPermissions)) { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 197 | actionApi.setBadgeBackgroundColor({color: '#B71C1C'}); |
| 198 | actionApi.setBadgeText({text: '!'}); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 199 | actionApi.setTitle({ |
| 200 | title: chrome.i18n.getMessage('actionbadge_permissions_requested') |
| 201 | }); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 202 | } else { |
| 203 | actionApi.setBadgeText({text: ''}); |
| 204 | actionApi.setTitle({title: ''}); |
| 205 | } |
| 206 | |
| 207 | if (removeLeftoverPerms) { |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 208 | chrome.permissions.remove(leftoverPermissions); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 209 | } |
| 210 | }) |
| 211 | .catch(err => { |
| 212 | console.error( |
| 213 | 'An error ocurred while cleaning optional permissions: ', err); |
| 214 | }); |
| 215 | } |