summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/browser/browser_ext_process_crash_handling.js
blob: c33727b96bbd87cc5104a32b78d530e86db04be4 (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
169
170
171
172
173
174
175
176
177
178
179
180
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);

const { ExtensionProcessCrashObserver, Management } =
  ChromeUtils.importESModule("resource://gre/modules/Extension.sys.mjs");

AddonTestUtils.initMochitest(this);

add_task(async function test_ExtensionProcessCrashObserver() {
  await SpecialPowers.pushPrefEnv({
    // This test triggers a crash and so it will be restarting all builtin
    // extensions persistent background pages as a side effect (and that would
    // be make this test to hit failures due to the builtin background pages
    // still in the process of being restarted being detected as shutdown leaks).
    set: [["extensions.background.disableRestartPersistentAfterCrash", true]],
  });
  let mv2Extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      manifest_version: 2,
    },
    background() {
      browser.test.sendMessage("background_running");
    },
  });

  await mv2Extension.startup();
  await mv2Extension.awaitMessage("background_running");

  let {
    currentProcessChildID,
    lastCrashedProcessChildID,
    processSpawningDisabled,
    lastCrashTimestamps,
  } = ExtensionProcessCrashObserver;

  Assert.notEqual(
    currentProcessChildID,
    undefined,
    "Expect ExtensionProcessCrashObserver.currentProcessChildID to be set"
  );

  Assert.equal(
    ChromeUtils.getAllDOMProcesses().find(
      pp => pp.childID == currentProcessChildID
    )?.remoteType,
    "extension",
    "Expect a child process with remoteType extension to be found for the process childID set"
  );

  Assert.notEqual(
    lastCrashedProcessChildID,
    currentProcessChildID,
    "Expect lastCrashedProcessChildID to not be set to the same value that currentProcessChildID is set"
  );

  Assert.equal(
    processSpawningDisabled,
    false,
    "Expect process spawning to be enabled"
  );

  Assert.deepEqual(lastCrashTimestamps, [], "Expect no crash timestamps");

  let mv3Extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      manifest_version: 3,
    },
    background() {
      browser.test.sendMessage("background_running");
    },
  });

  const waitForExtensionBrowserInserted = () =>
    new Promise(resolve => {
      const listener = (_eventName, browser) => {
        if (!browser.getAttribute("webextension-view-type") === "background") {
          return;
        }
        Management.off("extension-browser-inserted", listener);
        resolve(browser);
      };
      Management.on("extension-browser-inserted", listener);
    });

  const waitForExtensionProcessCrashNotified = () =>
    new Promise(resolve => {
      Management.once("extension-process-crash", (_evt, data) => resolve(data));
    });

  const promiseBackgroundBrowser = waitForExtensionBrowserInserted();

  const promiseExtensionProcessCrashNotified =
    waitForExtensionProcessCrashNotified();

  await mv3Extension.startup();
  await mv3Extension.awaitMessage("background_running");
  const bgPageBrowser = await promiseBackgroundBrowser;

  Assert.ok(
    Glean.extensions.processEvent.created_fg.testGetValue() > 0,
    "Expect glean processEvent.created_fg to be set."
  );
  Assert.equal(
    undefined,
    Glean.extensions.processEvent.created_bg.testGetValue(),
    "Creating in the background is not expected on desktop."
  );

  info("Force extension process crash");
  // Clear any existing telemetry data, so that we can be sure we can
  // assert the glean process_event metric labels values to be strictly
  // equal to 1 after the extension process crashed.
  Services.fog.testResetFOG();
  // NOTE: shouldShowTabCrashPage option needs to be set to false
  // to make sure crashFrame method resolves without waiting for a
  // tab crash page (which is not going to be shown for a background
  // page browser element).
  await BrowserTestUtils.crashFrame(
    bgPageBrowser,
    /* shouldShowTabCrashPage */ false
  );

  info("Verify ExtensionProcessCrashObserver after extension process crash");
  Assert.equal(
    ExtensionProcessCrashObserver.lastCrashedProcessChildID,
    currentProcessChildID,
    "Expect ExtensionProcessCrashObserver.lastCrashedProcessChildID to be set to the expected childID"
  );

  Assert.equal(
    ExtensionProcessCrashObserver.processSpawningDisabled,
    false,
    "Expect process spawning to still be enabled"
  );
  Assert.equal(
    ExtensionProcessCrashObserver.lastCrashTimestamps.length,
    1,
    "Expect a crash timestamp"
  );

  info("Expect the same childID to have been notified as a Management event");
  Assert.deepEqual(
    await promiseExtensionProcessCrashNotified,
    {
      childID: currentProcessChildID,
      processSpawningDisabled: false,
      // This boolean flag is expected to be always true on Desktop builds.
      appInForeground: true,
    },
    "Got the expected childID notified as part of the extension-process-crash Management event"
  );

  Assert.ok(
    Glean.extensions.processEvent.crashed_fg.testGetValue() > 0,
    "Expect glean processEvent.crashed_fg to be set"
  );
  Assert.equal(
    undefined,
    Glean.extensions.processEvent.crashed_bg.testGetValue(),
    "Crashing in the background is not expected on desktop."
  );

  info("Wait for mv3 extension shutdown");
  await mv3Extension.unload();
  info("Wait for mv2 extension shutdown");
  await mv2Extension.unload();

  // Reset this array to prevent TV failures.
  ExtensionProcessCrashObserver.lastCrashTimestamps = [];

  await SpecialPowers.popPrefEnv();
});