summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/file_slowed_document.sjs
blob: 8c42fcc96677acac0169e2ffc1c3664f7698a506 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80 ft=javascript: */
"use strict";

// This script slows the load of an HTML document so that we can reliably test
// all phases of the load cycle supported by the extension API.

/* eslint-disable no-unused-vars */

const URL = "file_slowed_document.sjs";

const DELAY = 2 * 1000; // Delay two seconds before completing the request.

let nsTimer = Components.Constructor(
  "@mozilla.org/timer;1",
  "nsITimer",
  "initWithCallback"
);

let timer;

function handleRequest(request, response) {
  response.processAsync();

  response.setHeader("Content-Type", "text/html", false);
  response.setHeader("Cache-Control", "no-cache", false);
  response.write(`<!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title></title>
    </head>
    <body>
  `);

  // Note: We need to store a reference to the timer to prevent it from being
  // canceled when it's GCed.
  timer = new nsTimer(
    () => {
      if (request.queryString.includes("with-iframe")) {
        response.write(`<iframe src="${URL}?r=${Math.random()}"></iframe>`);
      }
      response.write(`</body></html>`);
      response.finish();
    },
    DELAY,
    Ci.nsITimer.TYPE_ONE_SHOT
  );
}