Create mainWorldContentScriptBridge

These classes are the base of a communication bridge between the main
world and content scripts, which has been extracted from the main world
options watcher.

This will be used by the main world i18n component.

Bug: twpowertools:157
Change-Id: I08553b05648ad8453203ab08ecf01e824af15fea
diff --git a/src/common/mainWorldOptionsWatcher/Server.js b/src/common/mainWorldOptionsWatcher/Server.js
index 0d30aac..353c63d 100644
--- a/src/common/mainWorldOptionsWatcher/Server.js
+++ b/src/common/mainWorldOptionsWatcher/Server.js
@@ -1,46 +1,38 @@
 import OptionsWatcher from '../../common/optionsWatcher.js';
+import MainWorldContentScriptBridgeServer from '../mainWorldContentScriptBridge/Server.js';
 
 // Main World OptionsWatcher server (used in content scripts to be able to serve
 // the options to Main World (MW) scripts).
-export default class MWOptionsWatcherServer {
+export default class MWOptionsWatcherServer extends
+    MainWorldContentScriptBridgeServer {
   constructor(CSTarget, MWTarget) {
-    if (!CSTarget || !MWTarget)
-      throw new Error(
-          `[MWOptionsWatcherServer] CSTarget and MWTarget are compulsory.`);
-
+    super(CSTarget, MWTarget);
     this.optionsWatcher = null;
-    this.CSTarget = CSTarget;
-    this.MWTarget = MWTarget;
-
-    window.addEventListener('message', e => this.handleMessage(e));
+    this.setUpHandler(this.handleMessage);
   }
 
-  handleMessage(e) {
-    const uuid = e.data?.uuid;
-    if (e.source !== window || e.data?.target !== this.CSTarget || !uuid)
-      return;
-
-    if (e.data?.action === 'setUp') {
-      this.optionsWatcher = new OptionsWatcher(e.data?.request?.options);
+  handleMessage(uuid, action, request) {
+    if (action === 'setUp') {
+      this.optionsWatcher = new OptionsWatcher(request?.options);
       return;
     }
 
     if (!this.optionsWatcher) {
       console.warn(`[MWOptionsWatcherServer] Action '${
-          e.data?.action}' called before setting up options watcher.`);
+          action}' called before setting up options watcher.`);
       return;
     }
 
-    switch (e.data?.action) {
+    switch (action) {
       case 'getOption':
-        this.optionsWatcher.getOption(e.data?.request?.option).then(value => {
-          this.respond(uuid, value);
+        this.optionsWatcher.getOption(request?.option).then(value => {
+          this._respond(uuid, value);
         });
         return;
 
       case 'getOptions':
         var promises = [];
-        var options = e.data?.request?.options ?? [];
+        var options = request?.options ?? [];
         for (const option of options) {
           promises.push(this.optionsWatcher.getOption(option));
         }
@@ -49,19 +41,19 @@
           for (let i = 0; i < responses.length; i++) {
             response[options[i]] = responses[i];
           }
-          this.respond(uuid, response);
+          this._respond(uuid, response);
         });
         return;
 
       case 'isEnabled':
-        this.optionsWatcher.isEnabled(e.data?.request?.option).then(value => {
-          this.respond(uuid, value);
+        this.optionsWatcher.isEnabled(request?.option).then(value => {
+          this._respond(uuid, value);
         });
         return;
 
       case 'areEnabled':
         var promises = [];
-        var options = e.data?.request?.options ?? [];
+        var options = request?.options ?? [];
         for (const option of options) {
           promises.push(this.optionsWatcher.isEnabled(option));
         }
@@ -70,22 +62,13 @@
           for (let i = 0; i < responses.length; i++) {
             response[options[i]] = responses[i];
           }
-          this.respond(uuid, response);
+          this._respond(uuid, response);
         });
         return;
 
       default:
-        console.error(`[MWOptionsWatcherServer] Invalid action received (${
-            e.data?.action})`);
+        console.error(
+            `[MWOptionsWatcherServer] Invalid action received (${action})`);
     }
   }
-
-  respond(uuid, response) {
-    const data = {
-      target: this.MWTarget,
-      uuid,
-      response,
-    };
-    window.postMessage(data, window.origin);
-  }
 }