summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_contentscript_in_parent.js
blob: 5a8bbcb589a0c773ca3eeb688d5012d4e89aceb0 (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
"use strict";

const TELEMETRY_EVENT = "security#javascriptLoad#parentProcess";

add_task(async function test_contentscript_telemetry() {
  // Turn on telemetry and reset it to the previous state once the test is completed.
  // const telemetryCanRecordBase = Services.telemetry.canRecordBase;
  // Services.telemetry.canRecordBase = true;
  // SimpleTest.registerCleanupFunction(() => {
  //   Services.telemetry.canRecordBase = telemetryCanRecordBase;
  // });

  function background() {
    browser.test.onMessage.addListener(async msg => {
      if (msg !== "execute") {
        return;
      }
      browser.tabs.executeScript({
        file: "execute_script.js",
        allFrames: true,
        matchAboutBlank: true,
        runAt: "document_start",
      });

      await browser.userScripts.register({
        js: [{ file: "user_script.js" }],
        matches: ["<all_urls>"],
        matchAboutBlank: true,
        allFrames: true,
        runAt: "document_start",
      });

      await browser.contentScripts.register({
        js: [{ file: "content_script.js" }],
        matches: ["<all_urls>"],
        matchAboutBlank: true,
        allFrames: true,
        runAt: "document_start",
      });

      browser.test.sendMessage("executed");
    });
  }

  let extensionData = {
    manifest: {
      permissions: ["tabs", "<all_urls>"],
      user_scripts: {},
    },
    background,
    files: {
      // Fail if this ever executes.
      "execute_script.js": 'browser.test.fail("content-script-run");',
      "user_script.js": 'browser.test.fail("content-script-run");',
      "content_script.js": 'browser.test.fail("content-script-run");',
    },
  };

  function getSecurityEventCount() {
    let snap = Services.telemetry.getSnapshotForKeyedScalars();
    return snap.parent["telemetry.event_counts"][TELEMETRY_EVENT] || 0;
  }

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:preferences"
  );
  is(
    getSecurityEventCount(),
    0,
    `No events recorded before startup: ${TELEMETRY_EVENT}.`
  );

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  await extension.startup();
  is(
    getSecurityEventCount(),
    0,
    `No events recorded after startup: ${TELEMETRY_EVENT}.`
  );

  extension.sendMessage("execute");
  await extension.awaitMessage("executed");

  // Do another load.
  BrowserTestUtils.removeTab(tab);
  tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:preferences"
  );

  BrowserTestUtils.removeTab(tab);
  await extension.unload();

  is(
    getSecurityEventCount(),
    0,
    `No events recorded after executeScript: ${TELEMETRY_EVENT}.`
  );
});