blob: 7dc5fafa90bbfd99995273a92138f9f352a84193 [file] [log] [blame]
avm999633a4946d2021-02-05 21:27:12 +01001// IMPORTANT: keep this file in sync with sw.js
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +01002import {cleanUpOptPermissions} from './common/optionsPermissions.js';
3import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/optionsUtils.js';
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02004import KillSwitchMechanism from './killSwitch/index.js';
avm999631a1d2b72020-08-20 03:15:20 +02005
6chrome.browserAction.onClicked.addListener(function() {
7 chrome.runtime.openOptionsPage();
8});
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02009
10const killSwitchMechanism = new KillSwitchMechanism();
11
12chrome.alarms.create('updateKillSwitchStatus', {
13 periodInMinutes: PRODUCTION ? 30 : 1,
14});
15
16chrome.alarms.onAlarm.addListener(alarm => {
17 if (alarm.name === 'updateKillSwitchStatus')
18 killSwitchMechanism.updateKillSwitchStatus();
19});
20
21// When the profile is first started, update the kill switch status.
22chrome.runtime.onStartup.addListener(() => {
23 killSwitchMechanism.updateKillSwitchStatus();
24});
25
26// When the extension is first installed or gets updated, set new options to
27// their default value and update the kill switch status.
28chrome.runtime.onInstalled.addListener(details => {
29 if (details.reason == 'install' || details.reason == 'update') {
30 chrome.storage.sync.get(null, options => {
31 cleanUpOptions(options, false);
32 });
33
34 killSwitchMechanism.updateKillSwitchStatus();
35 }
36});
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +010037
38// Clean up optional permissions and check that none are missing for enabled
39// features as soon as the extension starts and when the options change.
40cleanUpOptPermissions();
41
42chrome.storage.sync.onChanged.addListener(() => {
43 cleanUpOptPermissions();
44});
45
46chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
47 if (sender.id !== chrome.runtime.id)
48 return console.warn(
49 'An unknown sender (' + sender.id +
50 ') sent a message to the extension: ',
51 msg);
52
53 console.assert(msg.message);
54 switch (msg.message) {
55 case 'runDisableItemsWithMissingPermissions':
56 console.assert(
57 msg.options?.items && msg.options?.permissionChecksFeatures);
58 disableItemsWithMissingPermissions(
59 msg.options?.items, msg.options?.permissionChecksFeatures)
60 .then(items => sendResponse({status: 'resolved', items}))
61 .catch(error => sendResponse({status: 'rejected', error}));
62 break;
63
64 default:
65 console.warn('Unknown message "' + msg.message + '".');
66 }
67});