summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/outOfProcess/head.js
blob: f7035ca7d15a2a58f964359e2f553ed581b633c9 (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
const OOP_BASE_PAGE_URI =
  "https://example.com/browser/browser/base/content/test/outOfProcess/file_base.html";

// The number of frames and subframes that exist for the basic OOP test. If frames are
// modified within file_base.html, update this value.
const TOTAL_FRAME_COUNT = 8;

// The frames are assigned different colors based on their process ids. If you add a
// frame you might need to add more colors to this list.
const FRAME_COLORS = ["white", "seashell", "lightcyan", "palegreen"];

/**
 * Set up a set of child frames for the given browser for testing
 * out of process frames. 'OOP_BASE_PAGE_URI' is the base page and subframes
 * contain pages from the same or other domains.
 *
 * @param browser browser containing frame hierarchy to set up
 * @param insertHTML HTML or function that returns what to insert into each frame
 * @returns array of all browsing contexts in depth-first order
 *
 * This function adds a browsing context and process id label to each
 * child subframe. It also sets the background color of each frame to
 * different colors based on the process id. The browser_basic_outofprocess.js
 * test verifies these colors to ensure that the frame/process hierarchy
 * has been set up as expected. Colors are used to help people visualize
 * the process setup.
 *
 * The insertHTML argument may be either a fixed string of HTML to insert
 * into each subframe, or a function that returns the string to insert. The
 * function takes one argument, the browsing context being processed.
 */
async function initChildFrames(browser, insertHTML) {
  let colors = FRAME_COLORS.slice();
  let colorMap = new Map();

  let browsingContexts = [];

  async function processBC(bc) {
    browsingContexts.push(bc);

    let pid = bc.currentWindowGlobal.osPid;
    let ident = "BrowsingContext: " + bc.id + "\nProcess: " + pid;

    let color = colorMap.get(pid);
    if (!color) {
      if (!colors.length) {
        ok(false, "ran out of available colors");
      }

      color = colors.shift();
      colorMap.set(pid, color);
    }

    let insertHTMLString = insertHTML;
    if (typeof insertHTML == "function") {
      insertHTMLString = insertHTML(bc);
    }

    await SpecialPowers.spawn(
      bc,
      [ident, color, insertHTMLString],
      (identChild, colorChild, insertHTMLChild) => {
        let root = content.document.documentElement;
        root.style = "background-color: " + colorChild;

        let pre = content.document.createElement("pre");
        pre.textContent = identChild;
        root.insertBefore(pre, root.firstChild);

        if (insertHTMLChild) {
          // eslint-disable-next-line no-unsanitized/property
          content.document.getElementById(
            "insertPoint"
          ).innerHTML = insertHTMLChild;
        }
      }
    );

    for (let childBC of bc.children) {
      await processBC(childBC);
    }
  }
  await processBC(browser.browsingContext);

  return browsingContexts;
}