Merge background.js and sw.js into bg.js

Now that we have included webpack-preprocessor-loader into Webpack, we
can merge the two files and hide code behind conditional statements
which will be preprocessed at compile time.

Change-Id: I99c87ca33fcb231d04c51ff9651e0cf10443e60b
diff --git a/playbooks/lint/run.yaml b/playbooks/lint/run.yaml
index fd6ff96..a82cfc6 100644
--- a/playbooks/lint/run.yaml
+++ b/playbooks/lint/run.yaml
@@ -4,4 +4,3 @@
       make_target: build_test_extension
     - role: web-ext-lint
       source_dir: "dist/gecko"
-      extra_lint_flags: "--ignore-files sw.js"
diff --git a/src/background.js b/src/background.js
deleted file mode 100644
index 7dc5faf..0000000
--- a/src/background.js
+++ /dev/null
@@ -1,67 +0,0 @@
-// IMPORTANT: keep this file in sync with sw.js
-import {cleanUpOptPermissions} from './common/optionsPermissions.js';
-import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/optionsUtils.js';
-import KillSwitchMechanism from './killSwitch/index.js';
-
-chrome.browserAction.onClicked.addListener(function() {
-  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);
-    });
-
-    killSwitchMechanism.updateKillSwitchStatus();
-  }
-});
-
-// Clean up optional permissions and check that none are missing for enabled
-// features as soon as the extension starts and when the options change.
-cleanUpOptPermissions();
-
-chrome.storage.sync.onChanged.addListener(() => {
-  cleanUpOptPermissions();
-});
-
-chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
-  if (sender.id !== chrome.runtime.id)
-    return console.warn(
-        'An unknown sender (' + sender.id +
-            ') sent a message to the extension: ',
-        msg);
-
-  console.assert(msg.message);
-  switch (msg.message) {
-    case 'runDisableItemsWithMissingPermissions':
-      console.assert(
-          msg.options?.items && msg.options?.permissionChecksFeatures);
-      disableItemsWithMissingPermissions(
-          msg.options?.items, msg.options?.permissionChecksFeatures)
-          .then(items => sendResponse({status: 'resolved', items}))
-          .catch(error => sendResponse({status: 'rejected', error}));
-      break;
-
-    default:
-      console.warn('Unknown message "' + msg.message + '".');
-  }
-});
diff --git a/src/sw.js b/src/bg.js
similarity index 89%
rename from src/sw.js
rename to src/bg.js
index fce78d2..ed18aae 100644
--- a/src/sw.js
+++ b/src/bg.js
@@ -1,16 +1,20 @@
-// IMPORTANT: keep this file in sync with background.js
+// #!if browser_target == 'chromium_mv3'
 import XMLHttpRequest from 'sw-xhr';
+// #!endif
 
+import actionApi from './common/actionApi.js';
 import {cleanUpOptPermissions} from './common/optionsPermissions.js';
 import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/optionsUtils.js';
 import KillSwitchMechanism from './killSwitch/index.js';
 
-// XMLHttpRequest is not present in service workers and is required by the
+// #!if browser_target == 'chromium_mv3'
+// XMLHttpRequest is not present in service workers (MV3) 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;
+// #!endif
 
-chrome.action.onClicked.addListener(_ => {
+actionApi.onClicked.addListener(() => {
   chrome.runtime.openOptionsPage();
 });
 
diff --git a/src/common/actionApi.js b/src/common/actionApi.js
new file mode 100644
index 0000000..69c68b8
--- /dev/null
+++ b/src/common/actionApi.js
@@ -0,0 +1,5 @@
+// #!if browser_target == 'chromium_mv3'
+export default chrome.action;
+// #!else
+export default chrome.browserAction;
+// #!endif
diff --git a/src/common/optionsPermissions.js b/src/common/optionsPermissions.js
index 9a998ad..93ba4cf 100644
--- a/src/common/optionsPermissions.js
+++ b/src/common/optionsPermissions.js
@@ -1,11 +1,6 @@
 import optionsPrototype from './optionsPrototype.json5';
 import {getOptions} from './optionsUtils.js';
-
-// #!if browser_target == 'chromium_mv3'
-const actionApi = chrome.action;
-// #!else
-const actionApi = chrome.browserAction;
-// #!endif
+import actionApi from './actionApi.js';
 
 // Required permissions, including host permissions.
 //
diff --git a/templates/manifest.gjson b/templates/manifest.gjson
index 20999d8..2faf7fb 100644
--- a/templates/manifest.gjson
+++ b/templates/manifest.gjson
@@ -103,10 +103,10 @@
 #if defined(CHROMIUM)
     "persistent": false,
 #endif
-    "scripts": ["background.bundle.js"]
+    "scripts": ["bg.bundle.js"]
 #endif
 #if defined(CHROMIUM_MV3)
-    "service_worker": "sw.bundle.js"
+    "service_worker": "bg.bundle.js"
 #endif
   },
 #if defined(GECKO)
diff --git a/webpack.config.js b/webpack.config.js
index 660d631..24d3876 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -52,10 +52,7 @@
   };
 
   // Background script (or service worker for MV3)
-  if (env.browser_target == 'chromium_mv3')
-    entry.sw = './src/sw.js';
-  else
-    entry.background = './src/background.js';
+  entry.bg = './src/bg.js';
 
   let outputPath = path.join(__dirname, 'dist', env.browser_target);