summaryrefslogtreecommitdiffstats
path: root/dom/base/test/browser_messagemanager_loadprocessscript.js
blob: af33da721d0ba36017f6d7476eb784802dd7a3aa (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
function getBaseNumberOfProcesses() {
  // We should have three processes for this test, the parent process and two
  // content processes for the tabs craeted by this test.
  let processCount = 3;

  // If we run WebExtensions out-of-process (see bug 1190679), there might be
  // additional processes for those, so let's add these to the base count to
  // not have system WebExtensions cause test failures.
  for (let i = 0; i < Services.ppmm.childCount; i++) {
    if (
      Services.ppmm.getChildAt(i).remoteType === E10SUtils.EXTENSION_REMOTE_TYPE
    ) {
      processCount += 1;
    }
  }

  return processCount;
}

function checkBaseProcessCount(description) {
  const baseProcessCount = getBaseNumberOfProcesses();
  const { childCount } = Services.ppmm;
  // With preloaded activity-stream, process count is a bit undeterministic, so
  // allow for some variation
  const extraCount = baseProcessCount + 1;
  ok(
    childCount === baseProcessCount || childCount === extraCount,
    `${description} (${baseProcessCount} or ${extraCount})`
  );
}

function processScript() {
  /* eslint-env mozilla/process-script */
  if (Services.cpmm !== this) {
    dump("Test failed: wrong global object\n");
    return;
  }

  this.cpmm = Services.cpmm;

  addMessageListener("ProcessTest:Reply", function listener(msg) {
    removeMessageListener("ProcessTest:Reply", listener);
    sendAsyncMessage("ProcessTest:Finished");
  });
  sendSyncMessage("ProcessTest:Loaded");
}
var processScriptURL = "data:,(" + processScript.toString() + ").call(this)";

function initTestScript() {
  /* eslint-env mozilla/process-script */
  let init = initialProcessData;
  if (init.test123 != "hello") {
    dump("Initial data incorrect\n");
    return;
  }

  sendAsyncMessage("ProcessTest:InitGood", init.test456.get("hi"));
}
var initTestScriptURL = "data:,(" + initTestScript.toString() + ")()";

var checkProcess = async function (mm) {
  let { target } = await promiseMessage(mm, "ProcessTest:Loaded");
  target.sendAsyncMessage("ProcessTest:Reply");
  await promiseMessage(target, "ProcessTest:Finished");
  ok(true, "Saw process finished");
};

function promiseMessage(messageManager, message) {
  return new Promise(resolve => {
    let listener = msg => {
      messageManager.removeMessageListener(message, listener);
      resolve(msg);
    };

    messageManager.addMessageListener(message, listener);
  });
}

add_task(async function () {
  // We want to count processes in this test, so let's disable the pre-allocated process manager.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.ipc.processPrelaunch.enabled", false]],
  });
});

add_task(async function () {
  // This test is only relevant in e10s.
  if (!gMultiProcessBrowser) {
    return;
  }

  Services.ppmm.releaseCachedProcesses();

  await SpecialPowers.pushPrefEnv({ set: [["dom.ipc.processCount", 5]] });
  await SpecialPowers.pushPrefEnv({
    set: [["dom.ipc.keepProcessesAlive.web", 5]],
  });

  let tabs = [];
  for (let i = 0; i < 3; i++) {
    tabs[i] = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      "about:blank"
    );
  }

  for (let i = 0; i < 3; i++) {
    // FIXME: This should wait for the tab removal gets reflected to the
    //        process count (bug 1446726).
    let sessionStorePromise = BrowserTestUtils.waitForSessionStoreUpdate(
      tabs[i]
    );
    BrowserTestUtils.removeTab(tabs[i]);
    await sessionStorePromise;
  }

  Services.ppmm.releaseCachedProcesses();
  checkBaseProcessCount(
    "Should get back to the base number of processes at this point"
  );
});

// Test that loading a process script loads in all existing processes
add_task(async function () {
  let checks = [];
  for (let i = 0; i < Services.ppmm.childCount; i++) {
    checks.push(checkProcess(Services.ppmm.getChildAt(i)));
  }

  Services.ppmm.loadProcessScript(processScriptURL, false);
  await Promise.all(checks);
});

// Test that loading a process script loads in new processes
add_task(async function () {
  // This test is only relevant in e10s
  if (!gMultiProcessBrowser) {
    return;
  }

  checkBaseProcessCount(
    "Should still be at the base number of processes at this point"
  );

  // Load something in the main process
  BrowserTestUtils.startLoadingURIString(
    gBrowser.selectedBrowser,
    "about:mozilla"
  );
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);

  let init = Services.ppmm.initialProcessData;
  init.test123 = "hello";
  init.test456 = new Map();
  init.test456.set("hi", "bye");

  // With no remote frames left we should be down to one process.
  // However, stuff like remote thumbnails can cause a content
  // process to exist nonetheless. This should be rare, though,
  // so the test is useful most of the time.
  if (Services.ppmm.childCount == 2) {
    let mainMM = Services.ppmm.getChildAt(0);

    let check = checkProcess(Services.ppmm);
    Services.ppmm.loadProcessScript(processScriptURL, true);

    // The main process should respond
    await check;

    check = checkProcess(Services.ppmm);
    // Reset the default browser to start a new child process
    gBrowser.updateBrowserRemoteness(gBrowser.selectedBrowser, {
      remoteType: E10SUtils.DEFAULT_REMOTE_TYPE,
    });
    BrowserTestUtils.startLoadingURIString(
      gBrowser.selectedBrowser,
      "about:blank"
    );
    await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);

    checkBaseProcessCount(
      "Should be back to the base number of processes at this point"
    );

    // The new process should have responded
    await check;

    Services.ppmm.removeDelayedProcessScript(processScriptURL);

    let childMM;
    childMM = Services.ppmm.getChildAt(2);

    childMM.loadProcessScript(initTestScriptURL, false);
    let msg = await promiseMessage(childMM, "ProcessTest:InitGood");
    is(msg.data, "bye", "initial process data was correct");
  } else {
    info("Unable to finish test entirely");
  }
});