summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/helper_largeAllocation.js
blob: ec5b4b3158e9d26e9fa8fddcc68e716f2db45175 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const TEST_URI =
  "http://example.com/browser/dom/tests/browser/test_largeAllocation.html";
const TEST_URI_2 =
  "http://example.com/browser/dom/tests/browser/test_largeAllocation2.html";

function expectProcessCreated() {
  let os = Services.obs;
  let kill; // A kill function which will disable the promise.
  let promise = new Promise((resolve, reject) => {
    let topic = "ipc:content-created";
    function observer() {
      os.removeObserver(observer, topic);
      ok(true, "Expect process created");
      resolve();
    }
    os.addObserver(observer, topic);
    kill = () => {
      os.removeObserver(observer, topic);
      ok(true, "Expect process created killed");
      reject();
    };
  });
  promise.kill = kill;
  return promise;
}

function getPID(aBrowser) {
  return SpecialPowers.spawn(aBrowser, [], () => {
    return Services.appinfo.processID;
  });
}

function getInLAProc(aBrowser) {
  return SpecialPowers.spawn(aBrowser, [], () => {
    return Services.appinfo.remoteType == "webLargeAllocation";
  });
}

async function largeAllocSuccessTests() {
  // I'm terrible and put this set of tests into a single file, so I need a longer timeout
  requestLongerTimeout(4);

  // Check if we are on win32
  let isWin32 =
    /Windows/.test(navigator.userAgent) && !/x64/.test(navigator.userAgent);

  await SpecialPowers.pushPrefEnv({
    set: [
      // Enable the header if it is disabled
      ["dom.largeAllocationHeader.enabled", true],
      // Force-enable process creation with large-allocation, such that non
      // win32 builds can test the behavior.
      ["dom.largeAllocation.forceEnable", !isWin32],
      // Increase processCount.webLargeAllocation to avoid any races where
      // processes aren't being cleaned up quickly enough.
      ["dom.ipc.processCount.webLargeAllocation", 20],
    ],
  });

  // A toplevel tab should be able to navigate cross process!
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 0");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    // Wait for the new process to be created
    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(
      pid1,
      pid2,
      "The pids should be different between the initial load and the new load"
    );
    is(true, await getInLAProc(aBrowser));
  });

  // When a Large-Allocation document is loaded in an iframe, the header should
  // be ignored, and the tab should stay in the current process.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 1");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      // eslint-disable-next-line no-unsanitized/property
      content.document.body.innerHTML = `<iframe src='${TEST_URI}'></iframe>`;

      return new Promise(resolve => {
        content.document.body.querySelector("iframe").onload = () => {
          ok(true, "Iframe finished loading");
          resolve();
        };
      });
    });

    let pid2 = await getPID(aBrowser);

    is(pid1, pid2, "The PID should not have changed");
    is(false, await getInLAProc(aBrowser));
  });

  // If you have an opener cross process navigation shouldn't work
  await BrowserTestUtils.withNewTab("http://example.com", async function(
    aBrowser
  ) {
    info("Starting test 2");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      info(TEST_URI);
      content.document.body.innerHTML = "<button>CLICK ME</button>";

      return new Promise(resolve => {
        let w = content.window.open(TEST_URI, "_blank");
        w.onload = () => {
          ok(true, "Window finished loading");
          w.close();
          resolve();
        };
      });
    });

    let pid2 = await getPID(aBrowser);

    is(pid1, pid2, "The PID should not have changed");
    is(false, await getInLAProc(aBrowser));
  });

  // Load Large-Allocation twice with example.com load in between
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 3");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2);
    is(true, await getInLAProc(aBrowser));

    await SpecialPowers.spawn(aBrowser, [], () => {
      content.document.location = "http://example.com";
    });

    await BrowserTestUtils.browserLoaded(aBrowser);

    // We should have been kicked out of the large-allocation process by the
    // load, meaning we're back in a non-fresh process
    is(false, await getInLAProc(aBrowser));

    ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid4 = await getPID(aBrowser);

    isnot(pid1, pid4);
    isnot(pid2, pid4);
    is(true, await getInLAProc(aBrowser));
  });

  // Load Large-Allocation then example.com load, then back button press should load from bfcache.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 4");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    // Switch to about:blank, so we can navigate back
    await SpecialPowers.spawn(aBrowser, [], () => {
      content.document.location = "http://example.com";
    });

    await BrowserTestUtils.browserLoaded(aBrowser);

    let pid3 = await getPID(aBrowser);

    // We should have been kicked out of the large-allocation process by the
    // load, meaning we're back in a non-large-allocation process.
    is(false, await getInLAProc(aBrowser));

    ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    // Navigate back to the previous page. As the large alloation process was
    // left, it won't be in bfcache and will have to be loaded fresh.
    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.window.history.back();
    });

    await ready;

    let pid4 = await getPID(aBrowser);

    isnot(pid1, pid4, "PID 4 shouldn't match PID 1");
    isnot(pid2, pid4, "PID 4 shouldn't match PID 2");
    isnot(pid3, pid4, "PID 4 shouldn't match PID 3");
    is(true, await getInLAProc(aBrowser));
  });

  // Two consecutive large-allocation loads should create two processes.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 5");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI_2], TEST_URI_2 => {
      content.document.location = TEST_URI_2;
    });

    await ready;

    let pid3 = await getPID(aBrowser);

    isnot(pid1, pid3, "PIDs 1 and 3 should not match");
    isnot(pid2, pid3, "PIDs 2 and 3 should not match");
    is(true, await getInLAProc(aBrowser));
  });

  // Opening a window from the large-allocation window should prevent the process switch.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 6");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    let promiseTabOpened = BrowserTestUtils.waitForNewTab(
      gBrowser,
      "about:blank"
    );
    await SpecialPowers.spawn(aBrowser, [], () => {
      content.window.open("about:blank");
      content.document.location = "http://example.com";
    });

    await BrowserTestUtils.browserLoaded(aBrowser);

    let pid3 = await getPID(aBrowser);

    is(pid3, pid2, "PIDs 2 and 3 should match");
    is(true, await getInLAProc(aBrowser));

    BrowserTestUtils.removeTab(await promiseTabOpened);
  });

  // Opening a window from the large-allocation window should prevent the
  // process switch with reload.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 6a");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    let firstTab = gBrowser.selectedTab;
    let promiseTabOpened = BrowserTestUtils.waitForNewTab(
      gBrowser,
      "about:blank"
    );
    await SpecialPowers.spawn(aBrowser, [], () => {
      content.window.open("about:blank");
    });
    let newTab = await promiseTabOpened;

    if (firstTab != gBrowser.selectedTab) {
      firstTab = await BrowserTestUtils.switchTab(gBrowser, firstTab);
      aBrowser = firstTab.linkedBrowser;
    }
    let promiseLoad = BrowserTestUtils.browserLoaded(aBrowser);
    document.getElementById("reload-button").doCommand();
    await promiseLoad;

    let pid3 = await getPID(aBrowser);

    is(pid3, pid2, "PIDs 2 and 3 should match");
    is(true, await getInLAProc(aBrowser));

    BrowserTestUtils.removeTab(newTab);
  });

  // Try dragging the tab into a new window when not at the maximum number of
  // Large-Allocation processes.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 7");

    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);
    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    let newWindow = await BrowserTestUtils.openNewBrowserWindow();

    newWindow.gBrowser.adoptTab(
      gBrowser.getTabForBrowser(aBrowser),
      0,
      null,
      Services.scriptSecurityManager.getSystemPrincipal()
    );
    let newTab = newWindow.gBrowser.tabs[0];

    is(newTab.linkedBrowser.currentURI.spec, TEST_URI);
    is(newTab.linkedBrowser.remoteType, "webLargeAllocation");
    let pid3 = await getPID(newTab.linkedBrowser);

    is(pid2, pid3, "PIDs 2 and 3 should match");
    is(true, await getInLAProc(newTab.linkedBrowser));

    await BrowserTestUtils.closeWindow(newWindow);
  });

  // Try opening a new Large-Allocation document when at the max number of large
  // allocation processes.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 8");
    await SpecialPowers.pushPrefEnv({
      set: [["dom.ipc.processCount.webLargeAllocation", 1]],
    });

    // Loading the first Large-Allocation tab should succeed as normal
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
      // The second one should load in a non-LA proc because the
      // webLargeAllocation processes have been exhausted.
      is(false, await getInLAProc(aBrowser));

      let ready = Promise.all([BrowserTestUtils.browserLoaded(aBrowser)]);
      await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
        content.document.location = TEST_URI;
      });
      await ready;

      is(false, await getInLAProc(aBrowser));
    });
  });

  // XXX: Important - reset the process count, as it was set to 1 by the
  // previous test.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.ipc.processCount.webLargeAllocation", 20]],
  });

  // view-source tabs should not be considered to be in a large-allocation process.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 9");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    await SpecialPowers.spawn(aBrowser, [], () => {
      content.document.location = "view-source:http://example.com";
    });

    await BrowserTestUtils.browserLoaded(aBrowser);

    let pid2 = await getPID(aBrowser);

    is(pid1, pid2, "The PID should not have changed");
    is(false, await getInLAProc(aBrowser));
  });

  // Try dragging tab into new window when at the max number of large allocation
  // processes.
  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 10");
    await SpecialPowers.pushPrefEnv({
      set: [["dom.ipc.processCount.webLargeAllocation", 1]],
    });

    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);
    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    let newWindow = await BrowserTestUtils.openNewBrowserWindow();

    newWindow.gBrowser.adoptTab(gBrowser.getTabForBrowser(aBrowser), 0);
    let newTab = newWindow.gBrowser.tabs[0];

    is(newTab.linkedBrowser.currentURI.spec, TEST_URI);
    is(newTab.linkedBrowser.remoteType, "webLargeAllocation");
    let pid3 = await getPID(newTab.linkedBrowser);

    is(pid2, pid3, "PIDs 2 and 3 should match");
    is(true, await getInLAProc(newTab.linkedBrowser));

    await BrowserTestUtils.closeWindow(newWindow);
  });

  // XXX: Important - reset the process count, as it was set to 1 by the
  // previous test.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.ipc.processCount.webLargeAllocation", 20]],
  });

  await BrowserTestUtils.withNewTab("about:blank", async function(aBrowser) {
    info("Starting test 11");

    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    let ready = Promise.all([
      expectProcessCreated(),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);
    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await ready;

    let pid2 = await getPID(aBrowser);

    isnot(pid1, pid2, "PIDs 1 and 2 should not match");
    is(true, await getInLAProc(aBrowser));

    await Promise.all([
      SpecialPowers.spawn(aBrowser, [], () => {
        content.document.querySelector("#submit").click();
      }),
      BrowserTestUtils.browserLoaded(aBrowser),
    ]);

    let innerText = await SpecialPowers.spawn(aBrowser, [], () => {
      return content.document.body.innerText;
    });
    isnot(innerText, "FAIL", "We should not have sent a get request!");
    is(
      innerText,
      "textarea=default+value&button=submit",
      "The post data should be received by the callee"
    );
  });

  // XXX: Make sure to reset dom.ipc.processCount.webLargeAllocation if adding a
  // test after the above test.
}

async function largeAllocFailTests() {
  await BrowserTestUtils.withNewTab("http://example.com", async function(
    aBrowser
  ) {
    info("Starting test 1");
    let pid1 = await getPID(aBrowser);
    is(false, await getInLAProc(aBrowser));

    await SpecialPowers.spawn(aBrowser, [TEST_URI], TEST_URI => {
      content.document.location = TEST_URI;
    });

    await BrowserTestUtils.browserLoaded(aBrowser);

    let pid2 = await getPID(aBrowser);

    is(pid1, pid2, "The PID should not have changed");
    is(false, await getInLAProc(aBrowser));
  });
}