summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_context_isolation.js
blob: 7794a66d5739ea80d5190111090e973c247b4de5 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";

/* globals exportFunction */
/* eslint-disable mozilla/balanced-listeners */

const server = createHttpServer({ hosts: ["example.com", "example.org"] });

server.registerPathHandler("/dummy", (request, response) => {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/html", false);
  response.write("<!DOCTYPE html><html></html>");
});

server.registerPathHandler("/bfcachetestpage", (request, response) => {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/html;charset=utf-8", false);
  response.write(`<!DOCTYPE html>
<script>
  window.addEventListener("pageshow", (event) => {
    event.stopImmediatePropagation();
    if (window.browserTestSendMessage) {
      browserTestSendMessage("content-script-show");
    }
  });
  window.addEventListener("pagehide", (event) => {
    event.stopImmediatePropagation();
    if (window.browserTestSendMessage) {
      if (event.persisted) {
        browserTestSendMessage("content-script-hide");
      } else {
        browserTestSendMessage("content-script-unload");
      }
    }
  }, true);
</script>`);
});

add_task(async function test_contentscript_context_isolation() {
  function contentScript() {
    browser.test.sendMessage("content-script-ready");

    exportFunction(browser.test.sendMessage, window, {
      defineAs: "browserTestSendMessage",
    });

    window.addEventListener("pageshow", () => {
      browser.test.fail(
        "pageshow should have been suppressed by stopImmediatePropagation"
      );
    });
    window.addEventListener(
      "pagehide",
      () => {
        browser.test.fail(
          "pagehide should have been suppressed by stopImmediatePropagation"
        );
      },
      true
    );
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        {
          matches: ["http://example.com/bfcachetestpage"],
          js: ["content_script.js"],
        },
      ],
    },

    files: {
      "content_script.js": contentScript,
    },
  });

  let contentPage = await ExtensionTestUtils.loadContentPage(
    "http://example.com/bfcachetestpage"
  );
  await extension.startup();
  await extension.awaitMessage("content-script-ready");

  // Get the content script context and check that it points to the correct window.
  await contentPage.legacySpawn(extension.id, async extensionId => {
    const { ExtensionContent } = ChromeUtils.importESModule(
      "resource://gre/modules/ExtensionContent.sys.mjs"
    );
    this.context = ExtensionContent.getContextByExtensionId(
      extensionId,
      this.content
    );

    Assert.ok(this.context, "Got content script context");

    Assert.equal(
      this.context.contentWindow,
      this.content,
      "Context's contentWindow property is correct"
    );

    // Navigate so that the content page is hidden in the bfcache.

    this.content.location = "http://example.org/dummy?noscripthere1";
  });

  await extension.awaitMessage("content-script-hide");

  await contentPage.legacySpawn(null, async () => {
    Assert.equal(
      this.context.contentWindow,
      null,
      "Context's contentWindow property is null"
    );
    Assert.ok(this.context.sandbox, "Context's sandbox exists");

    // Navigate back so the content page is resurrected from the bfcache.
    this.content.history.back();
  });

  await extension.awaitMessage("content-script-show");

  async function testWithoutBfcache() {
    return contentPage.legacySpawn(null, async () => {
      Assert.equal(
        this.context.contentWindow,
        this.content,
        "Context's contentWindow property is correct"
      );
      Assert.ok(this.context.sandbox, "Context's sandbox exists before unload");

      let contextUnloadedPromise = new Promise(resolve => {
        this.context.callOnClose({ close: resolve });
      });

      // Now add an "unload" event listener, which should prevent a page from entering the bfcache.
      await new Promise(resolve => {
        this.content.addEventListener("unload", () => {
          Assert.equal(
            this.context.contentWindow,
            this.content,
            "Context's contentWindow property should be non-null at unload"
          );
          resolve();
        });
        this.content.location = "http://example.org/dummy?noscripthere2";
      });

      await contextUnloadedPromise;
    });
  }
  await runWithPrefs(
    [["docshell.shistory.bfcache.allow_unload_listeners", false]],
    testWithoutBfcache
  );

  await extension.awaitMessage("content-script-unload");

  await contentPage.legacySpawn(null, async () => {
    Assert.equal(
      this.context.sandbox,
      null,
      "Context's sandbox has been destroyed after unload"
    );
  });

  await contentPage.close();
  await extension.unload();
});