summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/tests/tabswitch/content/tabswitch-content-process.js
blob: b0cc7f47e3b7f2c25a0e341101018e36153014e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* eslint-env mozilla/process-script */

const { ComponentUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/ComponentUtils.sys.mjs"
);

const WEBEXTENSION_ID = "tabswitch-talos@mozilla.org";
const ABOUT_PAGE_NAME = "tabswitch";
const Registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
const UUID = "0f459ab4-b4ba-4741-ac89-ee47dea07adb";
const ABOUT_PATH_PATH = "content/test.html";

const { WebExtensionPolicy } = Cu.getGlobalForObject(Services);

const TPSProcessScript = {
  init() {
    let extensionPolicy = WebExtensionPolicy.getByID(WEBEXTENSION_ID);
    let aboutPageURI = extensionPolicy.getURL(ABOUT_PATH_PATH);

    class TabSwitchAboutModule {
      constructor() {
        this.QueryInterface = ChromeUtils.generateQI(["nsIAboutModule"]);
      }
      newChannel(aURI, aLoadInfo) {
        let uri = Services.io.newURI(aboutPageURI);
        let chan = Services.io.newChannelFromURIWithLoadInfo(uri, aLoadInfo);
        chan.originalURI = aURI;
        return chan;
      }
      getURIFlags() {
        return (
          Ci.nsIAboutModule.ALLOW_SCRIPT |
          Ci.nsIAboutModule.URI_MUST_LOAD_IN_CHILD |
          Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT
        );
      }
    }

    let factory = ComponentUtils.generateSingletonFactory(TabSwitchAboutModule);
    this._factory = factory;

    Registrar.registerFactory(
      Components.ID(UUID),
      "",
      `@mozilla.org/network/protocol/about;1?what=${ABOUT_PAGE_NAME}`,
      factory
    );

    this._hasSetup = true;
  },

  teardown() {
    if (!this._hasSetup) {
      return;
    }

    Registrar.unregisterFactory(Components.ID(UUID), this._factory);
    this._hasSetup = false;
    this._factory = null;
  },

  receiveMessage(msg) {
    if (msg.name == "Tabswitch:Teardown") {
      this.teardown();
    }
  },
};

TPSProcessScript.init();