Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 1 | import {grantedOptPermissions, isPermissionsObjectEmpty, missingPermissions} from './optionsPermissions.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 2 | import optionsPrototype from './optionsPrototype.json5'; |
| 3 | import specialOptions from './specialOptions.json5'; |
| 4 | |
| 5 | export {optionsPrototype, specialOptions}; |
| 6 | |
| 7 | // Adds missing options with their default value. If |dryRun| is set to false, |
| 8 | // they are also saved to the sync storage area. |
| 9 | export function cleanUpOptions(options, dryRun = false) { |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 10 | console.debug('[cleanUpOptions] Previous options', JSON.stringify(options)); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 11 | |
| 12 | if (typeof options !== 'object' || options === null) options = {}; |
| 13 | |
| 14 | var ok = true; |
| 15 | for (const [opt, optMeta] of Object.entries(optionsPrototype)) { |
| 16 | if (!(opt in options)) { |
| 17 | ok = false; |
| 18 | options[opt] = optMeta['defaultValue']; |
| 19 | } |
| 20 | } |
| 21 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 22 | console.debug('[cleanUpOptions] New options', JSON.stringify(options)); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 23 | |
| 24 | if (!ok && !dryRun) { |
| 25 | chrome.storage.sync.set(options); |
| 26 | } |
| 27 | |
| 28 | return options; |
| 29 | } |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 30 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 31 | // This piece of code is used as part of the getOptions computation, and so |
| 32 | // isn't that useful. It's exported since we sometimes need to finish the |
| 33 | // computation in a service worker, where we have access to the |
| 34 | // chrome.permissions API. |
| 35 | // |
| 36 | // It accepts as an argument an object |items| with the same structure of the |
| 37 | // items saved in the sync storage area, and an array |permissionChecksFeatures| |
| 38 | // of features |
| 39 | export function disableItemsWithMissingPermissions( |
| 40 | items, permissionChecksFeatures) { |
| 41 | return grantedOptPermissions().then(grantedPerms => { |
| 42 | let permissionChecksPromises = []; |
| 43 | for (const f of permissionChecksFeatures) |
| 44 | permissionChecksPromises.push(missingPermissions(f, grantedPerms)); |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 45 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 46 | Promise.all(permissionChecksPromises).then(missingPerms => { |
| 47 | for (let i = 0; i < permissionChecksFeatures.length; i++) |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 48 | if (!isPermissionsObjectEmpty(missingPerms[i])) |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 49 | items[permissionChecksFeatures[i]] = false; |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 50 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 51 | return items; |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 52 | }); |
| 53 | }); |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 56 | // Returns a promise which returns the values of options |options| which are |
| 57 | // stored in the sync storage area. |
| 58 | // |
| 59 | // |requireOptionalPermissions| will determine whether to check if the required |
| 60 | // optional permissions have been granted or not to the options which have such |
| 61 | // requirements. If it is true, features with missing permissions will have |
| 62 | // their value set to false. |
| 63 | // |
| 64 | // When a kill switch is active, affected options always have their value set to |
| 65 | // false. |
| 66 | |
| 67 | // #!if !production |
| 68 | let timerId = 0; |
| 69 | let randomId = btoa(Math.random().toString()).substr(10, 5); |
| 70 | // #!endif |
| 71 | export function getOptions(options, requireOptionalPermissions = true) { |
| 72 | // #!if !production |
| 73 | let timeLabel = 'getOptions--' + randomId + '-' + (timerId++); |
| 74 | console.time(timeLabel); |
| 75 | // #!endif |
| 76 | // Once we only target MV3, this can be greatly simplified. |
| 77 | return new Promise((resolve, reject) => { |
| 78 | if (typeof options === 'string') |
| 79 | options = [options, '_forceDisabledFeatures']; |
| 80 | else if (Array.isArray(options)) |
| 81 | options = [...options, '_forceDisabledFeatures']; |
| 82 | else if (options !== null) |
| 83 | return reject(new Error( |
| 84 | 'Unexpected |options| parameter of type ' + (typeof options) + |
| 85 | ' (expected: string, array, or null).')); |
| 86 | |
| 87 | chrome.storage.sync.get(options, items => { |
| 88 | if (chrome.runtime.lastError) |
| 89 | return reject(chrome.runtime.lastError); |
| 90 | |
| 91 | // Handle applicable kill switches which force disable features |
| 92 | if (items?._forceDisabledFeatures) { |
| 93 | for (let feature of items?._forceDisabledFeatures) { |
| 94 | items[feature] = false; |
| 95 | } |
| 96 | |
| 97 | delete items._forceDisabledFeatures; |
| 98 | } |
| 99 | |
| 100 | if (!requireOptionalPermissions) return resolve(items); |
| 101 | |
| 102 | // Check whether some options have missing permissions which would |
| 103 | // force disable these features |
| 104 | let permissionChecksFeatures = []; |
| 105 | for (const [key, value] of Object.entries(items)) |
| 106 | if ((key in optionsPrototype) && value && |
| 107 | optionsPrototype[key].requiredOptPermissions?.length) |
| 108 | permissionChecksFeatures.push(key); |
| 109 | |
| 110 | if (permissionChecksFeatures.length == 0) return resolve(items); |
| 111 | |
| 112 | // If we don't have access to the chrome.permissions API (content |
| 113 | // scripts don't have access to it[1]), do the final piece of |
| 114 | // computation in the service worker/background script. |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 115 | // [1]: |
| 116 | // https://developer.chrome.com/docs/extensions/mv3/content_scripts/ |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 117 | |
| 118 | // #!if !production |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 119 | console.debug( |
| 120 | 'We are about to start checking granted permissions'); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 121 | console.timeLog(timeLabel); |
| 122 | // #!endif |
| 123 | if (!chrome.permissions) { |
| 124 | return chrome.runtime.sendMessage( |
| 125 | { |
| 126 | message: 'runDisableItemsWithMissingPermissions', |
| 127 | options: { |
| 128 | items, |
| 129 | permissionChecksFeatures, |
| 130 | }, |
| 131 | }, |
| 132 | response => { |
| 133 | if (response === undefined) |
| 134 | return reject(new Error( |
| 135 | 'An error ocurred while communicating with the service worker: ' + |
| 136 | chrome.runtime.lastError.message)); |
| 137 | |
| 138 | if (response.status == 'rejected') |
| 139 | return reject(response.error); |
| 140 | if (response.status == 'resolved') |
| 141 | return resolve(response.items); |
| 142 | return reject(new Error( |
| 143 | 'An unknown response was recieved from service worker.')); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | disableItemsWithMissingPermissions(items, permissionChecksFeatures) |
| 148 | .then(finalItems => resolve(finalItems)) |
| 149 | .catch(err => reject(err)); |
| 150 | }); |
| 151 | }) |
| 152 | // #!if !production |
| 153 | .then(items => { |
| 154 | console.group('getOptions(options); resolved; options: ', options); |
| 155 | console.timeEnd(timeLabel); |
| 156 | console.groupEnd(); |
| 157 | return items; |
| 158 | }) |
| 159 | .catch(err => { |
| 160 | console.group('getOptions(options); rejected; options: ', options); |
| 161 | console.timeEnd(timeLabel); |
| 162 | console.groupEnd(); |
| 163 | throw err; |
| 164 | }) |
| 165 | // #!endif |
| 166 | ; |
| 167 | } |
| 168 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 169 | // Returns a promise which returns whether the |option| option/feature is |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 170 | // currently enabled. If the feature requires optional permissions to work, |
| 171 | // |requireOptionalPermissions| will determine whether to check if the required |
| 172 | // optional permissions have been granted or not. |
| 173 | export function isOptionEnabled(option, requireOptionalPermissions = true) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 174 | return getOptions(option).then(options => { |
| 175 | return options?.[option] === true; |
| 176 | }); |
| 177 | } |