Add kill switch mechanism

This code implements the kill switch mechanism in the extension. This is
explained in //src/killSwitch/README.md and in the design doc:
https://docs.google.com/document/d/1O5YV6_WcxwrUyz-lwHOSTfZ3oyIFWj2EQee0VuKkhaA/edit.

Bug: twpowertools:64

Change-Id: Ia993c78035bba7038aafd53d156f20954217e86f
diff --git a/src/sw.js b/src/sw.js
index b8dccf2..73efa06 100644
--- a/src/sw.js
+++ b/src/sw.js
@@ -1,15 +1,42 @@
 // IMPORTANT: keep this file in sync with background.js
-import {cleanUpOptions} from './common/optionsUtils.js'
+import XMLHttpRequest from 'sw-xhr';
 
-// When the extension gets updated, set new options to their default value.
+import {cleanUpOptions} from './common/optionsUtils.js';
+import KillSwitchMechanism from './killSwitch/index.js';
+
+// XMLHttpRequest is not present in service workers and is required by the
+// grpc-web package. Importing a shim to work around this.
+// https://github.com/grpc/grpc-web/issues/1134
+self.XMLHttpRequest = XMLHttpRequest;
+
+chrome.action.onClicked.addListener(_ => {
+  chrome.runtime.openOptionsPage();
+});
+
+const killSwitchMechanism = new KillSwitchMechanism();
+
+chrome.alarms.create('updateKillSwitchStatus', {
+  periodInMinutes: PRODUCTION ? 30 : 1,
+});
+
+chrome.alarms.onAlarm.addListener(alarm => {
+  if (alarm.name === 'updateKillSwitchStatus')
+    killSwitchMechanism.updateKillSwitchStatus();
+});
+
+// When the profile is first started, update the kill switch status.
+chrome.runtime.onStartup.addListener(() => {
+  killSwitchMechanism.updateKillSwitchStatus();
+});
+
+// When the extension is first installed or gets updated, set new options to
+// their default value and update the kill switch status.
 chrome.runtime.onInstalled.addListener(details => {
   if (details.reason == 'install' || details.reason == 'update') {
     chrome.storage.sync.get(null, options => {
       cleanUpOptions(options, false);
     });
-  }
-});
 
-chrome.action.onClicked.addListener(_ => {
-  chrome.runtime.openOptionsPage();
+    killSwitchMechanism.updateKillSwitchStatus();
+  }
 });