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);
+    });
+  }
+}