blob: fce78d26ab96933e46ed32799d5b23222ad6d18b [file] [log] [blame]
avm999633a4946d2021-02-05 21:27:12 +01001// IMPORTANT: keep this file in sync with background.js
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02002import XMLHttpRequest from 'sw-xhr';
avm99963bbc88c62020-12-25 03:44:41 +01003
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +01004import {cleanUpOptPermissions} from './common/optionsPermissions.js';
5import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/optionsUtils.js';
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02006import 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
11self.XMLHttpRequest = XMLHttpRequest;
12
13chrome.action.onClicked.addListener(_ => {
14 chrome.runtime.openOptionsPage();
15});
16
17const killSwitchMechanism = new KillSwitchMechanism();
18
19chrome.alarms.create('updateKillSwitchStatus', {
20 periodInMinutes: PRODUCTION ? 30 : 1,
21});
22
23chrome.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.
29chrome.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.
avm99963bbc88c62020-12-25 03:44:41 +010035chrome.runtime.onInstalled.addListener(details => {
36 if (details.reason == 'install' || details.reason == 'update') {
37 chrome.storage.sync.get(null, options => {
avm99963bf8eece2021-04-22 00:27:03 +020038 cleanUpOptions(options, false);
avm99963bbc88c62020-12-25 03:44:41 +010039 });
avm99963bbc88c62020-12-25 03:44:41 +010040
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020041 killSwitchMechanism.updateKillSwitchStatus();
42 }
avm99963bbc88c62020-12-25 03:44:41 +010043});
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +010044
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.
47cleanUpOptPermissions();
48
49chrome.storage.sync.onChanged.addListener(() => {
50 cleanUpOptPermissions();
51});
52
53chrome.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});