summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_new_tab_in_privilegedabout_process_pref.js
blob: 6ab6ce198eddf83e7e91cced8f9276b5aab83298 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/**
 * Tests to ensure that Activity Stream loads in the privileged about:
 * content process. Normal http web pages should load in the web content
 * process.
 * Ref: Bug 1469072.
 */

const ABOUT_BLANK = "about:blank";
const ABOUT_HOME = "about:home";
const ABOUT_NEWTAB = "about:newtab";
const ABOUT_WELCOME = "about:welcome";
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
const TEST_HTTP = "http://example.org/";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.newtab.preload", false],
      ["browser.tabs.remote.separatePrivilegedContentProcess", true],
      ["dom.ipc.processCount.privilegedabout", 1],
      ["dom.ipc.keepProcessesAlive.privilegedabout", 1],
    ],
  });
});

/*
 * Test to ensure that the Activity Stream tabs open in privileged about: content
 * process. We will first open an about:newtab page that acts as a reference to
 * the privileged about:  content process. With the reference, we can then open
 * Activity Stream links in a new tab and ensure that the new tab opens in the same
 * privileged about: content process as our reference.
 */
add_task(async function activity_stream_in_privileged_content_process() {
  Services.ppmm.releaseCachedProcesses();

  await BrowserTestUtils.withNewTab(ABOUT_NEWTAB, async function (browser1) {
    checkBrowserRemoteType(browser1, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE);

    // Note the processID for about:newtab for comparison later.
    let privilegedPid = browser1.frameLoader.remoteTab.osPid;

    for (let url of [
      ABOUT_NEWTAB,
      ABOUT_WELCOME,
      ABOUT_HOME,
      `${ABOUT_NEWTAB}#foo`,
      `${ABOUT_WELCOME}#bar`,
      `${ABOUT_HOME}#baz`,
      `${ABOUT_NEWTAB}?q=foo`,
      `${ABOUT_WELCOME}?q=bar`,
      `${ABOUT_HOME}?q=baz`,
    ]) {
      await BrowserTestUtils.withNewTab(url, async function (browser2) {
        is(
          browser2.frameLoader.remoteTab.osPid,
          privilegedPid,
          "Check that about:newtab tabs are in the same privileged about: content process."
        );
      });
    }
  });

  Services.ppmm.releaseCachedProcesses();
});

/*
 * Test to ensure that a process switch occurs when navigating between normal
 * web pages and Activity Stream pages in the same tab.
 */
add_task(async function process_switching_through_loading_in_the_same_tab() {
  Services.ppmm.releaseCachedProcesses();

  await BrowserTestUtils.withNewTab(TEST_HTTP, async function (browser) {
    checkBrowserRemoteType(browser, E10SUtils.WEB_REMOTE_TYPE);

    for (let [url, remoteType] of [
      [ABOUT_NEWTAB, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [ABOUT_BLANK, E10SUtils.WEB_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [ABOUT_HOME, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [ABOUT_WELCOME, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [ABOUT_BLANK, E10SUtils.WEB_REMOTE_TYPE],
      [`${ABOUT_NEWTAB}#foo`, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [`${ABOUT_WELCOME}#bar`, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [`${ABOUT_HOME}#baz`, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [`${ABOUT_NEWTAB}?q=foo`, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [`${ABOUT_WELCOME}?q=bar`, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
      [`${ABOUT_HOME}?q=baz`, E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE],
      [TEST_HTTP, E10SUtils.WEB_REMOTE_TYPE],
    ]) {
      BrowserTestUtils.loadURIString(browser, url);
      await BrowserTestUtils.browserLoaded(browser, false, url);
      checkBrowserRemoteType(browser, remoteType);
    }
  });

  Services.ppmm.releaseCachedProcesses();
});

/*
 * Test to ensure that a process switch occurs when navigating between normal
 * web pages and Activity Stream pages using the browser's navigation features
 * such as history and location change.
 */
add_task(async function process_switching_through_navigation_features() {
  Services.ppmm.releaseCachedProcesses();

  await BrowserTestUtils.withNewTab(
    ABOUT_NEWTAB,
    async function (initialBrowser) {
      checkBrowserRemoteType(
        initialBrowser,
        E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE
      );

      // Note the processID for about:newtab for comparison later.
      let privilegedPid = initialBrowser.frameLoader.remoteTab.osPid;

      function assertIsPrivilegedProcess(browser, desc) {
        is(
          browser.messageManager.remoteType,
          E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE,
          `Check that ${desc} is loaded in privileged about: content process.`
        );
        is(
          browser.frameLoader.remoteTab.osPid,
          privilegedPid,
          `Check that ${desc} is loaded in original privileged process.`
        );
      }

      // Check that about:newtab opened from JS in about:newtab page is in the same process.
      let promiseTabOpened = BrowserTestUtils.waitForNewTab(
        gBrowser,
        ABOUT_NEWTAB,
        true
      );
      await SpecialPowers.spawn(initialBrowser, [ABOUT_NEWTAB], uri => {
        content.open(uri, "_blank");
      });
      let newTab = await promiseTabOpened;
      registerCleanupFunction(async function () {
        BrowserTestUtils.removeTab(newTab);
      });
      let browser = newTab.linkedBrowser;
      assertIsPrivilegedProcess(browser, "new tab opened from about:newtab");

      // Check that reload does not break the privileged about: content process affinity.
      BrowserReload();
      await BrowserTestUtils.browserLoaded(browser, false, ABOUT_NEWTAB);
      assertIsPrivilegedProcess(browser, "about:newtab after reload");

      // Load http webpage
      BrowserTestUtils.loadURIString(browser, TEST_HTTP);
      await BrowserTestUtils.browserLoaded(browser, false, TEST_HTTP);
      checkBrowserRemoteType(browser, E10SUtils.WEB_REMOTE_TYPE);

      // Check that using the history back feature switches back to privileged about: content process.
      let promiseLocation = BrowserTestUtils.waitForLocationChange(
        gBrowser,
        ABOUT_NEWTAB
      );
      browser.goBack();
      await promiseLocation;
      // We will need to ensure that the process flip has fully completed so that
      // the navigation history data will be available when we do browser.goForward();
      await BrowserTestUtils.browserLoaded(browser);
      assertIsPrivilegedProcess(browser, "about:newtab after history goBack");

      // Check that using the history forward feature switches back to the web content process.
      promiseLocation = BrowserTestUtils.waitForLocationChange(
        gBrowser,
        TEST_HTTP
      );
      browser.goForward();
      await promiseLocation;
      // We will need to ensure that the process flip has fully completed so that
      // the navigation history data will be available when we do browser.gotoIndex(0);
      await BrowserTestUtils.browserLoaded(browser);
      checkBrowserRemoteType(
        browser,
        E10SUtils.WEB_REMOTE_TYPE,
        "Check that tab runs in the web content process after using history goForward."
      );

      // Check that goto history index does not break the affinity.
      promiseLocation = BrowserTestUtils.waitForLocationChange(
        gBrowser,
        ABOUT_NEWTAB
      );
      browser.gotoIndex(0);
      await promiseLocation;
      is(
        browser.frameLoader.remoteTab.osPid,
        privilegedPid,
        "Check that about:newtab is in privileged about: content process after history gotoIndex."
      );
      assertIsPrivilegedProcess(
        browser,
        "about:newtab after history goToIndex"
      );

      BrowserTestUtils.loadURIString(browser, TEST_HTTP);
      await BrowserTestUtils.browserLoaded(browser, false, TEST_HTTP);
      checkBrowserRemoteType(browser, E10SUtils.WEB_REMOTE_TYPE);

      // Check that location change causes a change in process type as well.
      await SpecialPowers.spawn(browser, [ABOUT_NEWTAB], uri => {
        content.location = uri;
      });
      await BrowserTestUtils.browserLoaded(browser, false, ABOUT_NEWTAB);
      assertIsPrivilegedProcess(browser, "about:newtab after location change");
    }
  );

  Services.ppmm.releaseCachedProcesses();
});