avm99963 | 3a4946d | 2021-02-05 21:27:12 +0100 | [diff] [blame] | 1 | // IMPORTANT: keep this file in sync with background.js |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 2 | import XMLHttpRequest from 'sw-xhr'; |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 3 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame^] | 4 | import {cleanUpOptPermissions} from './common/optionsPermissions.js'; |
| 5 | import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/optionsUtils.js'; |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 6 | import KillSwitchMechanism from './killSwitch/index.js'; |
| 7 | |
| 8 | // XMLHttpRequest is not present in service workers and is required by the |
| 9 | // grpc-web package. Importing a shim to work around this. |
| 10 | // https://github.com/grpc/grpc-web/issues/1134 |
| 11 | self.XMLHttpRequest = XMLHttpRequest; |
| 12 | |
| 13 | chrome.action.onClicked.addListener(_ => { |
| 14 | chrome.runtime.openOptionsPage(); |
| 15 | }); |
| 16 | |
| 17 | const killSwitchMechanism = new KillSwitchMechanism(); |
| 18 | |
| 19 | chrome.alarms.create('updateKillSwitchStatus', { |
| 20 | periodInMinutes: PRODUCTION ? 30 : 1, |
| 21 | }); |
| 22 | |
| 23 | chrome.alarms.onAlarm.addListener(alarm => { |
| 24 | if (alarm.name === 'updateKillSwitchStatus') |
| 25 | killSwitchMechanism.updateKillSwitchStatus(); |
| 26 | }); |
| 27 | |
| 28 | // When the profile is first started, update the kill switch status. |
| 29 | chrome.runtime.onStartup.addListener(() => { |
| 30 | killSwitchMechanism.updateKillSwitchStatus(); |
| 31 | }); |
| 32 | |
| 33 | // When the extension is first installed or gets updated, set new options to |
| 34 | // their default value and update the kill switch status. |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 35 | chrome.runtime.onInstalled.addListener(details => { |
| 36 | if (details.reason == 'install' || details.reason == 'update') { |
| 37 | chrome.storage.sync.get(null, options => { |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 38 | cleanUpOptions(options, false); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 39 | }); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 40 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 41 | killSwitchMechanism.updateKillSwitchStatus(); |
| 42 | } |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 43 | }); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame^] | 44 | |
| 45 | // Clean up optional permissions and check that none are missing for enabled |
| 46 | // features as soon as the extension starts and when the options change. |
| 47 | cleanUpOptPermissions(); |
| 48 | |
| 49 | chrome.storage.sync.onChanged.addListener(() => { |
| 50 | cleanUpOptPermissions(); |
| 51 | }); |
| 52 | |
| 53 | chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { |
| 54 | if (sender.id !== chrome.runtime.id) |
| 55 | return console.warn( |
| 56 | 'An unknown sender (' + sender.id + |
| 57 | ') sent a message to the extension: ', |
| 58 | msg); |
| 59 | |
| 60 | console.assert(msg.message); |
| 61 | switch (msg.message) { |
| 62 | case 'runDisableItemsWithMissingPermissions': |
| 63 | console.assert( |
| 64 | msg.options?.items && msg.options?.permissionChecksFeatures); |
| 65 | disableItemsWithMissingPermissions( |
| 66 | msg.options?.items, msg.options?.permissionChecksFeatures) |
| 67 | .then(items => sendResponse({status: 'resolved', items})) |
| 68 | .catch(error => sendResponse({status: 'rejected', error})); |
| 69 | break; |
| 70 | |
| 71 | default: |
| 72 | console.warn('Unknown message "' + msg.message + '".'); |
| 73 | } |
| 74 | }); |