blob: 73efa0627aa7465097ffe1ac9bcb57e886cb9f67 [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ínez413cb442021-09-06 00:30:45 +02004import {cleanUpOptions} from './common/optionsUtils.js';
5import KillSwitchMechanism from './killSwitch/index.js';
6
7// XMLHttpRequest is not present in service workers and is required by the
8// grpc-web package. Importing a shim to work around this.
9// https://github.com/grpc/grpc-web/issues/1134
10self.XMLHttpRequest = XMLHttpRequest;
11
12chrome.action.onClicked.addListener(_ => {
13 chrome.runtime.openOptionsPage();
14});
15
16const killSwitchMechanism = new KillSwitchMechanism();
17
18chrome.alarms.create('updateKillSwitchStatus', {
19 periodInMinutes: PRODUCTION ? 30 : 1,
20});
21
22chrome.alarms.onAlarm.addListener(alarm => {
23 if (alarm.name === 'updateKillSwitchStatus')
24 killSwitchMechanism.updateKillSwitchStatus();
25});
26
27// When the profile is first started, update the kill switch status.
28chrome.runtime.onStartup.addListener(() => {
29 killSwitchMechanism.updateKillSwitchStatus();
30});
31
32// When the extension is first installed or gets updated, set new options to
33// their default value and update the kill switch status.
avm99963bbc88c62020-12-25 03:44:41 +010034chrome.runtime.onInstalled.addListener(details => {
35 if (details.reason == 'install' || details.reason == 'update') {
36 chrome.storage.sync.get(null, options => {
avm99963bf8eece2021-04-22 00:27:03 +020037 cleanUpOptions(options, false);
avm99963bbc88c62020-12-25 03:44:41 +010038 });
avm99963bbc88c62020-12-25 03:44:41 +010039
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020040 killSwitchMechanism.updateKillSwitchStatus();
41 }
avm99963bbc88c62020-12-25 03:44:41 +010042});