summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/tests/tabpaint/framescript.js
blob: 2d4d7149fc0849f977d4c1234dc5a241a765cb7e (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
/* eslint-env mozilla/frame-script */

(function () {
  addEventListener(
    "load",
    loadevt => {
      if (!content.location.pathname.endsWith("target.html")) {
        return;
      }

      /**
       * When a page is loaded, we expect a search string to be
       * appended with the "starting time" (in ms) of when the tab
       * was opened.
       *
       * Example: target.html?1457063506846
       */
      let opened = parseInt(content.location.search.substring(1), 10);

      addEventListener("MozAfterPaint", function onPaint(e) {
        // Bug 1371332 - sometimes, MozAfterPaint events fire
        // for "empty" paints, where nothing has actually been
        // painted. We can detect that by looking at the rect
        // for the region that has painted.
        let rect = e.boundingClientRect;
        if (!rect.width && !rect.height) {
          return;
        }

        removeEventListener("MozAfterPaint", onPaint);

        // The MozAfterPaint event comes with a paintTimeStamp
        // which tells us when in this content's lifetime the
        // paint actually occurred. Note that this is not a
        // measurement of when this paint occurred from
        // the UNIX epoch. This makes it a little tricky to
        // calculate when the paint actually occurred relative
        // to the starting time that's been appended to the
        // page's URL.
        //
        // Thankfully, the PerformanceTiming API gives us a
        // sense of when this page's lifetime started, relative
        // to the UNIX epoch - the "fetchStart". Taking that
        // time and adding the paintTimeStamp should give us
        // a pretty decent approximation of when since the
        // UNIX epoch the paint actually occurred for this
        // content.
        //
        // We can then subtract the starting time to get the
        // delta, which should now represent the time it took
        // from requesting that the tab be opened, to the
        // paint occurring within the tab.
        let fetchStart = content.performance.timing.fetchStart;
        let presented = fetchStart + e.paintTimeStamp;
        let delta = presented - opened;

        sendAsyncMessage("TabPaint:Painted", { delta });
      });
    },
    true
  );

  addEventListener(
    "TabPaint:Ping",
    e => {
      let evt = new content.CustomEvent("TabPaint:Pong", { bubbles: true });
      content.dispatchEvent(evt);
    },
    false,
    true
  );

  addEventListener(
    "TabPaint:Go",
    e => {
      sendAsyncMessage("TabPaint:Go", { target: e.detail.target });
    },
    false,
    true
  );

  addMessageListener("TabPaint:OpenFromContent", msg => {
    let evt = new content.CustomEvent("TabPaint:OpenFromContent", {
      bubbles: true,
    });
    content.dispatchEvent(evt);
  });

  addMessageListener("TabPaint:FinalResults", msg => {
    let evt = Cu.cloneInto(
      {
        bubbles: true,
        detail: msg.data,
      },
      content
    );

    content.dispatchEvent(
      new content.CustomEvent("TabPaint:FinalResults", evt)
    );
  });
})();