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/xhrInterceptor/responseModifiers/index.js b/src/xhrInterceptor/responseModifiers/index.js
new file mode 100644
index 0000000..6a4573a
--- /dev/null
+++ b/src/xhrInterceptor/responseModifiers/index.js
@@ -0,0 +1,75 @@
+import MWOptionsWatcherClient from '../../common/mainWorldOptionsWatcher/Client.js';
+import {convertJSONToResponse, getResponseJSON} from '../utils.js';
+
+import demo from './demo.js';
+
+export const responseModifiers = [
+  demo,
+];
+
+// Content script target
+export const kCSTarget = 'TWPT-XHRInterceptorOptionsWatcher-CS';
+// Main world (AKA regular web page) target
+export const kMWTarget = 'TWPT-XHRInterceptorOptionsWatcher-MW';
+
+export default class ResponseModifier {
+  constructor() {
+    this.optionsWatcher = new MWOptionsWatcherClient(
+        Array.from(this.watchingFeatures()), kCSTarget, kMWTarget);
+  }
+
+  watchingFeatures(modifiers) {
+    if (!modifiers) modifiers = responseModifiers;
+
+    const union = new Set();
+    for (const m of modifiers) {
+      if (!m.featureGated) continue;
+      for (const feature of m.features) union.add(feature);
+    }
+    return union;
+  }
+
+  async #getMatchingModifiers(request) {
+    // First filter modifiers which match the request URL regex.
+    const urlModifiers = responseModifiers.filter(
+        modifier => request.$TWPTRequestURL.match(modifier.urlRegex));
+
+    // Now filter modifiers which require a certain feature to be enabled
+    // (feature-gated modifiers).
+    const featuresAreEnabled = await this.optionsWatcher.areEnabled(
+        Array.from(this.watchingFeatures(urlModifiers)));
+
+    // #!if !production
+    if (Object.keys(featuresAreEnabled).length > 0) {
+      console.info(
+          '[XHR Interceptor - Response Modifier] Requested features',
+          featuresAreEnabled, 'for request', request.$TWPTRequestURL);
+    }
+    // #!endif
+
+    return urlModifiers.filter(modifier => {
+      return !modifier.featureGated || modifier.isEnabled(featuresAreEnabled);
+    });
+  }
+
+  async intercept(request, response) {
+    const matchingModifiers = await this.#getMatchingModifiers(request);
+
+    // If we didn't find any matching modifiers, return the response right away.
+    if (matchingModifiers.length === 0) return response;
+
+    // Otherwise, apply the modifiers sequentially and set the new response.
+    let json = getResponseJSON({
+      responseType: request.xhr.responseType,
+      response: request.xhr.response,
+      $TWPTRequestURL: request.$TWPTRequestURL,
+      $isArrayProto: request.$isArrayProto,
+    });
+    for (const modifier of matchingModifiers) {
+      json = await modifier.interceptor(request, json);
+    }
+    response = convertJSONToResponse(request, json);
+    request.$newResponse = response;
+    request.$responseModified = true;
+  }
+}