summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_extension_page_navigated.js
blob: e573595afb30d17cbbacc7e489e3271852c102b9 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

AddonTestUtils.init(this);
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "42",
  "42"
);

const server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] });

server.registerPathHandler("/", (request, response) => {
  response.write(`<!DOCTYPE html>
    <html>
      <head>
       <meta charset="utf-8">
       <title>test webpage</title>
      </head>
    </html>
  `);
});

function createTestExtPage({ script }) {
  return `<!DOCTYPE html>
    <html>
      <head>
       <meta charset="utf-8">
       <script src="${script}"></script>
      </head>
    </html>
  `;
}

function createTestExtPageScript(name) {
  return `(${async function (pageName) {
    browser.webRequest.onBeforeRequest.addListener(
      details => {
        browser.test.log(
          `${pageName} got a webRequest.onBeforeRequest event: ${details.url}`
        );
        browser.test.sendMessage(`event-received:${pageName}`);
      },
      { urls: ["http://example.com/request*"] }
    );

    // Calling an API implemented in the parent process to make sure
    // the webRequest.onBeforeRequest listener is got registered in
    // the parent process by the time the test is going to expect that
    // listener to intercept a test web request.
    await browser.runtime.getBrowserInfo();
    browser.test.sendMessage(`page-loaded:${pageName}`);
  }})("${name}");`;
}

const getExtensionContextIdAndURL = extensionId => {
  const { ExtensionProcessScript } = ChromeUtils.importESModule(
    "resource://gre/modules/ExtensionProcessScript.sys.mjs"
  );
  let extWindow = this.content.window;
  let extChild = ExtensionProcessScript.getExtensionChild(extensionId);

  let contextIds = [];
  let contextURLs = [];
  for (let ctx of extChild.views) {
    if (ctx.contentWindow === extWindow) {
      // Only one is expected, but we collect details from all
      // the ones that match to make sure the test will fails
      // in case there are unexpected multiple extension contexts
      // associated to the same contentWindow.
      contextIds.push(ctx.contextId);
      contextURLs.push(ctx.contentWindow.location.href);
    }
  }
  return { contextIds, contextURLs };
};

const getExtensionContextStatusByContextId = (
  extensionId,
  extPageContextId
) => {
  const { ExtensionProcessScript } = ChromeUtils.importESModule(
    "resource://gre/modules/ExtensionProcessScript.sys.mjs"
  );
  let extChild = ExtensionProcessScript.getExtensionChild(extensionId);

  let context;
  for (let ctx of extChild.views) {
    if (ctx.contextId === extPageContextId) {
      context = ctx;
    }
  }
  return context?.active;
};

add_task(async function test_extension_page_sameprocess_navigation() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["webRequest", "http://example.com/*"],
    },
    files: {
      "extpage1.html": createTestExtPage({ script: "extpage1.js" }),
      "extpage1.js": createTestExtPageScript("extpage1"),
      "extpage2.html": createTestExtPage({ script: "extpage2.js" }),
      "extpage2.js": createTestExtPageScript("extpage2"),
    },
  });

  await extension.startup();

  const policy = WebExtensionPolicy.getByID(extension.id);

  const extPageURL1 = policy.extension.baseURI.resolve("extpage1.html");
  const extPageURL2 = policy.extension.baseURI.resolve("extpage2.html");

  info("Opening extension page in a first browser element");
  const extPage = await ExtensionTestUtils.loadContentPage(extPageURL1);
  await extension.awaitMessage("page-loaded:extpage1");

  const { contextIds, contextURLs } = await extPage.spawn(
    [extension.id],
    getExtensionContextIdAndURL
  );

  Assert.deepEqual(
    contextURLs,
    [extPageURL1],
    `Found an extension context with the expected page url`
  );

  ok(
    contextIds[0],
    `Found an extension context with contextId ${contextIds[0]}`
  );
  ok(
    contextIds.length,
    `There should be only one extension context for a given content window, found ${contextIds.length}`
  );

  const [contextId] = contextIds;

  await ExtensionTestUtils.fetch(
    "http://example.com",
    "http://example.com/request1"
  );
  await extension.awaitMessage("event-received:extpage1");

  info("Load a second extension page in the same browser element");
  await extPage.loadURL(extPageURL2);
  await extension.awaitMessage("page-loaded:extpage2");

  let active;

  let { messages } = await AddonTestUtils.promiseConsoleOutput(async () => {
    // We only expect extpage2 to be able to receive API events.
    await ExtensionTestUtils.fetch(
      "http://example.com",
      "http://example.com/request2"
    );
    await extension.awaitMessage("event-received:extpage2");

    active = await extPage.spawn(
      [extension.id, contextId],
      getExtensionContextStatusByContextId
    );
  });

  if (
    Services.appinfo.sessionHistoryInParent &&
    WebExtensionPolicy.isExtensionProcess
  ) {
    // When the extension are running in the main process while the webpages run
    // in a separate child process, the extension page doesn't enter the BFCache
    // because nsFrameLoader::changeRemotenessCommon bails out due to retainPaint
    // being computed as true (see
    // https://searchfox.org/mozilla-central/rev/24c1cdc33ccce692612276cd0d3e9a44f6c22fd3/dom/base/nsFrameLoaderOwner.cpp#185-196
    // ).
    equal(active, undefined, "extension page context should not exist anymore");
  } else {
    equal(
      active,
      false,
      "extension page context is expected to be inactive while moved into the BFCache"
    );
  }

  if (typeof active === "boolean") {
    AddonTestUtils.checkMessages(
      messages,
      {
        forbidden: [
          // We should not have tried to deserialize the event data for the extension page
          // that got moved into the BFCache (See Bug 1499129).
          {
            message:
              /StructureCloneHolder.deserialize: Argument 1 is not an object/,
          },
        ],
        expected: [
          // If the extension page is expected to be in the BFCache, then we expect to see
          // a warning message logged for the ignored listener.
          {
            message:
              /Ignored listener for inactive context .* path=webRequest.onBeforeRequest/,
          },
        ],
      },
      "Expect no StructureCloneHolder error due to trying to send the event to inactive context"
    );
  }

  await extPage.close();
  await extension.unload();
});

add_task(async function test_extension_page_context_navigated_to_web_page() {
  const extension = ExtensionTestUtils.loadExtension({
    files: {
      "extpage.html": createTestExtPage({ script: "extpage.js" }),
      "extpage.js": function () {
        dump("loaded extension page\n");
        window.addEventListener(
          "pageshow",
          () => {
            browser.test.log("Extension page got a pageshow event");
            browser.test.sendMessage("extpage:pageshow");
          },
          { once: true }
        );
        window.addEventListener(
          "pagehide",
          () => {
            browser.test.log("Extension page got a pagehide event");
            browser.test.sendMessage("extpage:pagehide");
          },
          { once: true }
        );
      },
    },
  });

  await extension.startup();

  const policy = WebExtensionPolicy.getByID(extension.id);

  const extPageURL = policy.extension.baseURI.resolve("extpage.html");
  const webPageURL = "http://example.com/";

  info("Opening extension page in a browser element");
  const extPage = await ExtensionTestUtils.loadContentPage(extPageURL);
  await extension.awaitMessage("extpage:pageshow");

  const { contextIds, contextURLs } = await extPage.spawn(
    [extension.id],
    getExtensionContextIdAndURL
  );

  Assert.deepEqual(
    contextURLs,
    [extPageURL],
    `Found an extension context with the expected page url`
  );

  ok(
    contextIds[0],
    `Found an extension context with contextId ${contextIds[0]}`
  );
  ok(
    contextIds.length,
    `There should be only one extension context for a given content window, found ${contextIds.length}`
  );

  const [contextId] = contextIds;

  info("Load a webpage in the same browser element");
  await extPage.loadURL(webPageURL);
  await extension.awaitMessage("extpage:pagehide");

  info("Open extension page in a second browser element");
  const extPage2 = await ExtensionTestUtils.loadContentPage(extPageURL);
  await extension.awaitMessage("extpage:pageshow");

  let active = await extPage2.spawn(
    [extension.id, contextId],
    getExtensionContextStatusByContextId
  );

  if (WebExtensionPolicy.isExtensionProcess) {
    // When the extension are running in the main process while the webpages run
    // in a separate child process, the extension page doesn't enter the BFCache
    // because nsFrameLoader::changeRemotenessCommon bails out due to retainPaint
    // being computed as true (see
    // https://searchfox.org/mozilla-central/rev/24c1cdc33ccce692612276cd0d3e9a44f6c22fd3/dom/base/nsFrameLoaderOwner.cpp#185-196
    // ).
    equal(active, undefined, "extension page context should not exist anymore");
  } else if (Services.appinfo.sessionHistoryInParent) {
    // When SHIP is enabled and the extensions runs in their own child extension
    // process, the BFCache is managed entirely from the parent process and the
    // extension page is expected to be able to enter the BFCache.
    equal(
      active,
      false,
      "extension page context is expected to be inactive while moved into the BFCache"
    );
  } else {
    // With the extension running in a separate child process but fission disabled,
    // we expect the extension page to don't enter the BFCache.
    equal(active, undefined, "extension page context should not exist anymore");
  }

  if (active === false) {
    info(
      "Navigating to more web pages to confirm the extension page have been evicted from the BFCache"
    );
    for (let i = 2; i < 5; i++) {
      const url = `${webPageURL}/page${i}`;
      info(`Navigating to ${url}`);
      await extPage.loadURL(url);
    }
    equal(
      await extPage2.spawn(
        [extension.id, contextId],
        getExtensionContextStatusByContextId
      ),
      undefined,
      "extension page context should have been evicted"
    );
  }

  info("Cleanup and exit test");

  await Promise.all([
    extPage.close(),
    extPage2.close(),
    extension.awaitMessage("extpage:pagehide"),
  ]);
  await extension.unload();
});