Add XHR response modifier

- Add ResponseModifier class which is responsible for handling the
  modification of the responses intercepted by XHRProxy. The
  modifications occur by processing requests via several individual
  response modifiers sequentially, which are applied if the URL matches
  a RegEx condition and, if applicable, passes a condition based on the
  features enabled by the user.

- A sample response modifier called "demo" is included, to show how a
  simple modifier would work.

- Add mainWorldOptionsWatcher with 2 components: a server and a client,
  so main world scripts (clients) can retrieve the extension options via
  their corresponding content scripts (the servers) using an
  OptionsWatcher class instantiated by the server. This is used by the
  ResponseModifier class since it's injected into the main world because
  it's the only way to intercept requests.

Bug: twpowertools:153
Change-Id: I8a9767e1eadd60d3a0f1054669e1e1f5e7a49fbb
diff --git a/src/common/mainWorldOptionsWatcher/Client.js b/src/common/mainWorldOptionsWatcher/Client.js
new file mode 100644
index 0000000..3e5a9f9
--- /dev/null
+++ b/src/common/mainWorldOptionsWatcher/Client.js
@@ -0,0 +1,76 @@
+export const kDefaultTimeout = 10 * 1000;  // 10 seconds
+
+// Main World OptionsWatcher client (used in scripts injected into the Main
+// World (MW) to get the options).
+export default class MWOptionsWatcherClient {
+  constructor(options, CSTarget, MWTarget, timeout) {
+    if (!CSTarget || !MWTarget)
+      throw new Error(
+          `[MWOptionsWatcherClient] CSTarget and MWTarget are compulsory.`);
+
+    this.CSTarget = CSTarget;
+    this.MWTarget = MWTarget;
+    this.timeout = timeout ?? kDefaultTimeout;
+    this.#setUp(options);
+  }
+
+  #setUp(options) {
+    this.#sendRequestWithoutCallback('setUp', {options});
+  }
+
+  async getOption(option) {
+    if (!option) return null;
+    return this.#sendRequest('getOption', {option});
+  }
+
+  async getOptions(options) {
+    if (!options || options?.length === 0) return [];
+    return this.#sendRequest('getOptions', {options});
+  }
+
+  async isEnabled(option) {
+    if (!option) return null;
+    return this.#sendRequest('isEnabled', {option});
+  }
+
+  async areEnabled(options) {
+    if (!options || options?.length === 0) return [];
+    return this.#sendRequest('areEnabled', {options});
+  }
+
+  #sendRequestWithoutCallback(action, request, uuid) {
+    if (!uuid) uuid = self.crypto.randomUUID();
+    const data = {
+      target: this.CSTarget,
+      uuid,
+      action,
+      request,
+    };
+    window.postMessage(data, '*');
+  }
+
+  #sendRequest(action, request) {
+    return new Promise((res, rej) => {
+      const uuid = self.crypto.randomUUID();
+
+      let timeoutId;
+      let listener = e => {
+        if (e.source !== window || e.data?.target !== this.MWTarget ||
+            e.data?.uuid !== uuid)
+          return;
+
+        window.removeEventListener('message', listener);
+        clearTimeout(timeoutId);
+        res(e.data?.response);
+      };
+      window.addEventListener('message', listener);
+
+      timeoutId = setTimeout(() => {
+        window.removeEventListener('message', listener);
+        rej(new Error('Timed out while waiting response.'));
+      }, this.timeout);
+
+      this.#sendRequestWithoutCallback(action, request, uuid);
+    });
+  }
+}
diff --git a/src/common/mainWorldOptionsWatcher/Server.js b/src/common/mainWorldOptionsWatcher/Server.js
new file mode 100644
index 0000000..0d30aac
--- /dev/null
+++ b/src/common/mainWorldOptionsWatcher/Server.js
@@ -0,0 +1,91 @@
+import OptionsWatcher from '../../common/optionsWatcher.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 {
+  constructor(CSTarget, MWTarget) {
+    if (!CSTarget || !MWTarget)
+      throw new Error(
+          `[MWOptionsWatcherServer] CSTarget and MWTarget are compulsory.`);
+
+    this.optionsWatcher = null;
+    this.CSTarget = CSTarget;
+    this.MWTarget = MWTarget;
+
+    window.addEventListener('message', e => this.handleMessage(e));
+  }
+
+  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);
+      return;
+    }
+
+    if (!this.optionsWatcher) {
+      console.warn(`[MWOptionsWatcherServer] Action '${
+          e.data?.action}' called before setting up options watcher.`);
+      return;
+    }
+
+    switch (e.data?.action) {
+      case 'getOption':
+        this.optionsWatcher.getOption(e.data?.request?.option).then(value => {
+          this.respond(uuid, value);
+        });
+        return;
+
+      case 'getOptions':
+        var promises = [];
+        var options = e.data?.request?.options ?? [];
+        for (const option of options) {
+          promises.push(this.optionsWatcher.getOption(option));
+        }
+        Promise.all(promises).then(responses => {
+          const response = {};
+          for (let i = 0; i < responses.length; i++) {
+            response[options[i]] = responses[i];
+          }
+          this.respond(uuid, response);
+        });
+        return;
+
+      case 'isEnabled':
+        this.optionsWatcher.isEnabled(e.data?.request?.option).then(value => {
+          this.respond(uuid, value);
+        });
+        return;
+
+      case 'areEnabled':
+        var promises = [];
+        var options = e.data?.request?.options ?? [];
+        for (const option of options) {
+          promises.push(this.optionsWatcher.isEnabled(option));
+        }
+        Promise.all(promises).then(responses => {
+          const response = {};
+          for (let i = 0; i < responses.length; i++) {
+            response[options[i]] = responses[i];
+          }
+          this.respond(uuid, response);
+        });
+        return;
+
+      default:
+        console.error(`[MWOptionsWatcherServer] Invalid action received (${
+            e.data?.action})`);
+    }
+  }
+
+  respond(uuid, response) {
+    const data = {
+      target: this.MWTarget,
+      uuid,
+      response,
+    };
+    window.postMessage(data, window.origin);
+  }
+}
diff --git a/src/common/protojs.js b/src/common/protojs.js
index 026559b..a2e2d06 100644
--- a/src/common/protojs.js
+++ b/src/common/protojs.js
@@ -11,3 +11,26 @@
   }
   return object;
 }
+
+// The inverse function.
+export function inverseCorrectArrayKeys(input) {
+  if (Array.isArray(input)) {
+    if (input[0] === null || input[0] === undefined) {
+      // Make a copy of the input array so we don't modify the original one.
+      input = Array.from(input);
+      input.shift();
+    }
+    for (let i = 0; i < input.length; ++i) {
+      input[i] = inverseCorrectArrayKeys(input[i]);
+    }
+    return input;
+  }
+
+  if (typeof input !== 'object' || input === null) return input;
+
+  let array = [];
+  Object.entries(input).forEach(entry => {
+    array[entry[0] - 1] = inverseCorrectArrayKeys(entry[1]);
+  });
+  return array;
+}