summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/pageloader/chrome/pageloader.js
blob: 6b1f89f71dc3aac546b9585ca4d3880f1566e6ec (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* import-globals-from report.js */

var { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);
var { E10SUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/E10SUtils.sys.mjs"
);
ChromeUtils.defineESModuleGetters(this, {
  TalosParentProfiler: "resource://talos-powers/TalosParentProfiler.sys.mjs",
});

var NUM_CYCLES = 5;
var numPageCycles = 1;

var numRetries = 0;
var maxRetries = 3;

var pageFilterRegexp = null;
var winWidth = 1024;
var winHeight = 768;

var pages;
var pageIndex;
var start_time;
var cycle;
var pageCycle;
var report;
var timeout = -1;
var delay = 250;
var running = false;
var forceCC = true;

var useMozAfterPaint = false;
var useFNBPaint = false;
var isFNBPaintPending = false;
var usePDFPaint = false;
var isPDFPaintPending = false;
var useHero = false;
var gPaintWindow = window;
var gPaintListener = false;
var loadNoCache = false;
var scrollTest = false;
var profilingInfo = false;
var baseVsRef = false;
var useBrowserChrome = false;
var useA11y = false;

var isIdleCallbackPending = false;

// when TEST_DOES_OWN_TIMING, we need to store the time from the page as MozAfterPaint can be slower than pageload
var gTime = -1;
var gStartTime = -1;
var gReference = -1;

var gBrowser;

// These are binary flags. Use 1/2/4/8/...
var TEST_DOES_OWN_TIMING = 1;
var EXECUTE_SCROLL_TEST = 2;

var browserWindow = null;

var recordedName = null;
var pageUrls;

/**
 * SingleTimeout class. Allow to register one and only one callback using
 * setTimeout at a time.
 */
var SingleTimeout = function () {
  this.timeoutEvent = undefined;
};

/**
 * Register a callback with the given timeout.
 *
 * If timeout is < 0, this is a no-op.
 *
 * If a callback was previously registered and has not been called yet, it is
 * first cleared with clear().
 */
SingleTimeout.prototype.register = function (callback, timeout) {
  if (timeout >= 0) {
    if (this.timeoutEvent !== undefined) {
      this.clear();
    }
    var that = this;
    this.timeoutEvent = setTimeout(function () {
      that.timeoutEvent = undefined;
      callback();
    }, timeout);
  }
};

/**
 * Clear a registered callback.
 */
SingleTimeout.prototype.clear = function () {
  if (this.timeoutEvent !== undefined) {
    clearTimeout(this.timeoutEvent);
    this.timeoutEvent = undefined;
  }
};

var failTimeout = new SingleTimeout();

async function plInit() {
  if (running) {
    return;
  }
  running = true;

  cycle = 0;
  pageCycle = 1;

  try {
    /*
     * Desktop firefox:
     * non-chrome talos runs - tp-cmdline will create and load pageloader
     * into the main window of the app which displays and tests content.
     * chrome talos runs - tp-cmdline does the same however pageloader
     * creates a new chromed browser window below for content.
     */

    var manifestURI = Services.prefs.getCharPref("talos.tpmanifest", null);
    if (manifestURI.length == null) {
      dumpLine("tp abort: talos.tpmanifest browser pref is not set");
      plStop(true);
    }

    NUM_CYCLES = Services.prefs.getIntPref("talos.tpcycles", 1);
    numPageCycles = Services.prefs.getIntPref("talos.tppagecycles", 1);
    timeout = Services.prefs.getIntPref("talos.tptimeout", -1);
    useMozAfterPaint = Services.prefs.getBoolPref(
      "talos.tpmozafterpaint",
      false
    );
    useHero = Services.prefs.getBoolPref("talos.tphero", false);
    useFNBPaint = Services.prefs.getBoolPref("talos.fnbpaint", false);
    usePDFPaint = Services.prefs.getBoolPref("talos.pdfpaint", false);
    loadNoCache = Services.prefs.getBoolPref("talos.tploadnocache", false);
    scrollTest = Services.prefs.getBoolPref("talos.tpscrolltest", false);
    useBrowserChrome = Services.prefs.getBoolPref("talos.tpchrome", false);
    useA11y = Services.prefs.getBoolPref("talos.a11y", false);

    // for pageloader tests the profiling info is found in an env variable
    // because it is not available early enough to set it as a browser pref
    if (Services.env.exists("TPPROFILINGINFO")) {
      profilingInfo = Services.env.get("TPPROFILINGINFO");
      if (profilingInfo !== null) {
        TalosParentProfiler.initFromObject(JSON.parse(profilingInfo));
      }
    }

    if (forceCC && !window.windowUtils.garbageCollect) {
      forceCC = false;
    }

    var fileURI = Services.io.newURI(manifestURI);
    pages = plLoadURLsFromURI(fileURI);

    if (!pages) {
      dumpLine("tp: could not load URLs, quitting");
      plStop(true);
    }

    if (!pages.length) {
      dumpLine("tp: no pages to test, quitting");
      plStop(true);
    }

    pageUrls = pages.map(function (p) {
      return p.url.spec.toString();
    });
    report = new Report();

    pageIndex = 0;
    if (profilingInfo) {
      TalosParentProfiler.beginTest(getCurrentPageShortName());
    }

    // Create a new chromed browser window for content
    var blank = Cc["@mozilla.org/supports-string;1"].createInstance(
      Ci.nsISupportsString
    );
    blank.data = "about:blank";

    let toolbars = "all";
    if (!useBrowserChrome) {
      toolbars = "titlebar,resizable";
    }

    browserWindow = Services.ww.openWindow(
      null,
      AppConstants.BROWSER_CHROME_URL,
      "_blank",
      `chrome,${toolbars},dialog=no,width=${winWidth},height=${winHeight}`,
      blank
    );

    gPaintWindow = browserWindow;
    // get our window out of the way
    window.resizeTo(10, 10);

    await new Promise(resolve => {
      browserWindow.addEventListener("load", resolve, {
        capture: true,
        once: true,
      });
    });

    // do this half a second after load, because we need to be
    // able to resize the window and not have it get clobbered
    // by the persisted values
    await new Promise(resolve => setTimeout(resolve, 500));

    browserWindow.resizeTo(winWidth, winHeight);
    browserWindow.moveTo(0, 0);
    browserWindow.focus();
    // This is hacky but pageloader has worked like this for a while...
    // eslint-disable-next-line no-global-assign
    gBrowser = browserWindow.gBrowser;

    // Since bug 1261842, the initial browser is remote unless it attempts
    // to browse to a URI that should be non-remote (landed at bug 1047603).
    //
    // However, when it loads a URI that requires a different remote type,
    // we lose the load listener and the injected tpRecordTime.remote,
    //
    // This listener will listen for when one of these process switches has
    // happened, and re-install these listeners and injected methods into
    // the new browser tab.
    //
    // It also probably means that per test (or, in fact, per pageloader browser
    // instance which adds the load listener and injects tpRecordTime), all the
    // pages should be able to load in the same mode as the initial page - due
    // to this reinitialization on the switch.
    let tab = gBrowser.selectedTab;
    tab.addEventListener("TabRemotenessChange", function () {
      loadFrameScripts(tab.linkedBrowser);
    });
    loadFrameScripts(tab.linkedBrowser);

    // Ensure that any webextensions that need to do setup have a chance
    // to do so. e.g. the 'tabswitch' talos test registers a about:tabswitch
    // handler during initialization, and if we don't wait for that, then
    // attempting to load that URL will result in an error and hang the
    // test.
    for (let extension of WebExtensionPolicy.getActiveExtensions()) {
      await extension.readyPromise;
    }
    plLoadPage();
  } catch (e) {
    dumpLine("pageloader exception: " + e);
    plStop(true);
  }
}

function plPageFlags() {
  return pages[pageIndex].flags;
}

var ContentListener = {
  receiveMessage(message) {
    switch (message.name) {
      case "PageLoader:LoadEvent":
        return plLoadHandlerMessage(message);
      case "PageLoader:Error":
        return plErrorMessage(message);
      case "PageLoader:RecordTime":
        return plRecordTimeMessage(message);
      case "PageLoader:IdleCallbackSet":
        return plIdleCallbackSet();
      case "PageLoader:IdleCallbackReceived":
        return plIdleCallbackReceived();
    }
    return undefined;
  },
};

// load the current page, start timing
var removeLastAddedMsgListener = null;
function plLoadPage() {
  if (profilingInfo) {
    TalosParentProfiler.beginTest(
      getCurrentPageShortName() + "_pagecycle_" + pageCycle
    );
  }

  var pageURL = pages[pageIndex].url;

  if (removeLastAddedMsgListener) {
    removeLastAddedMsgListener();
    removeLastAddedMsgListener = null;
  }

  let tab = gBrowser.selectedTab;
  tab.addEventListener("TabRemotenessChange", () => {
    addMsgListeners(tab.linkedBrowser);
  });
  addMsgListeners(tab.linkedBrowser);

  failTimeout.register(loadFail, timeout);
  // record which page we are about to open
  TalosParentProfiler.mark("Opening " + pages[pageIndex].url.pathQueryRef);

  if (useFNBPaint) {
    isFNBPaintPending = true;
  }

  if (usePDFPaint) {
    isPDFPaintPending = true;
  }

  startAndLoadURI(pageURL);
}

function addMsgListeners(browser) {
  let mm = browser.messageManager;
  // messages to watch for page load
  mm.addMessageListener("PageLoader:LoadEvent", ContentListener);
  mm.addMessageListener("PageLoader:RecordTime", ContentListener);
  mm.addMessageListener("PageLoader:IdleCallbackSet", ContentListener);
  mm.addMessageListener("PageLoader:IdleCallbackReceived", ContentListener);
  mm.addMessageListener("PageLoader:Error", ContentListener);

  removeLastAddedMsgListener = function () {
    mm.removeMessageListener("PageLoader:LoadEvent", ContentListener);
    mm.removeMessageListener("PageLoader:RecordTime", ContentListener);
    mm.removeMessageListener("PageLoader:IdleCallbackSet", ContentListener);
    mm.removeMessageListener(
      "PageLoader:IdleCallbackReceived",
      ContentListener
    );
    mm.removeMessageListener("PageLoader:Error", ContentListener);
  };
}

function loadFrameScripts(browser) {
  let mm = browser.messageManager;

  // Load our frame scripts.
  mm.loadFrameScript("chrome://pageloader/content/utils.js", false, true);

  // pick the right load handler
  if (useFNBPaint) {
    mm.loadFrameScript(
      "chrome://pageloader/content/lh_fnbpaint.js",
      false,
      true
    );
  } else if (useMozAfterPaint) {
    mm.loadFrameScript("chrome://pageloader/content/lh_moz.js", false, true);
  } else if (useHero) {
    mm.loadFrameScript("chrome://pageloader/content/lh_hero.js", false, true);
  } else if (usePDFPaint) {
    mm.loadFrameScript(
      "chrome://pageloader/content/lh_pdfpaint.js",
      false,
      true
    );
  } else {
    mm.loadFrameScript("chrome://pageloader/content/lh_dummy.js", false, true);
  }
  mm.loadFrameScript("chrome://pageloader/content/talos-content.js", false);
  mm.loadFrameScript(
    "resource://talos-powers/TalosContentProfiler.js",
    false,
    true
  );
  mm.loadFrameScript("chrome://pageloader/content/tscroll.js", false, true);
  mm.loadFrameScript("chrome://pageloader/content/Profiler.js", false, true);
  if (useA11y) {
    mm.loadFrameScript("chrome://pageloader/content/a11y.js", false, true);
  }
}

function startAndLoadURI(pageURL) {
  if (!(plPageFlags() & TEST_DOES_OWN_TIMING)) {
    // Indicate the subtest start if we're really measuring page load time.
    // If the test is doing its own timing, it'll also need to do its own
    // profiler subtestStart / subtestEnd.
    TalosParentProfiler.subtestStart("Starting to load URI " + pageURL.spec);
  }

  start_time = window.performance.now();
  if (loadNoCache) {
    gBrowser.loadURI(pageURL, {
      triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
      flags: Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE,
    });
  } else {
    gBrowser.loadURI(pageURL, {
      triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
    });
  }
}

function getTestName() {
  // returns tp5n
  var pageName = pages[pageIndex].url.spec;
  let parts = pageName.split("/");
  if (parts.length > 4) {
    return parts[4];
  }
  return "pageloader";
}

function getCurrentPageShortName() {
  // this is also used by gecko profiling for the profile
  // file name; so ensure it is valid on Windows/Linux/OSX
  var pageName = pages[pageIndex].url.spec;
  let parts = pageName.split("/");
  if (parts.length > 5) {
    // Tear off the first parts and concatenate the rest into a name.
    let remainingParts = parts.slice(5);
    let remainingAsString = remainingParts.join("_");
    if (remainingAsString.includes("?")) {
      // page name is something like 'tpaint.html?auto=1'
      remainingAsString = remainingAsString.split("?")[0];
    }
    return remainingAsString;
  }
  return "page_" + pageIndex;
}

function loadFail() {
  var pageName = pages[pageIndex].url.spec;
  numRetries++;

  if (numRetries >= maxRetries) {
    dumpLine("__FAILTimeout in " + getTestName() + "__FAIL");
    dumpLine(
      "__FAILTimeout (" +
        numRetries +
        "/" +
        maxRetries +
        ") exceeded on " +
        pageName +
        "__FAIL"
    );
    TalosParentProfiler.finishTest().then(() => {
      plStop(true);
    });
  } else {
    dumpLine(
      "__WARNTimeout (" +
        numRetries +
        "/" +
        maxRetries +
        ") exceeded on " +
        pageName +
        "__WARN"
    );
    // TODO: make this a cleaner cleanup
    pageCycle--;
    gBrowser.removeEventListener("load", plLoadHandler, true);
    gBrowser.removeEventListener("load", plLoadHandlerCapturing, true);
    gBrowser.removeEventListener("MozAfterPaint", plPaintedCapturing, true);
    gBrowser.removeEventListener("MozAfterPaint", plPainted, true);
    gPaintWindow.removeEventListener("MozAfterPaint", plPaintedCapturing, true);
    gPaintWindow.removeEventListener("MozAfterPaint", plPainted, true);
    removeLastAddedMsgListener = null;
    gPaintListener = false;

    // TODO: consider adding a tab and removing the old tab?!?
    setTimeout(plLoadPage, delay);
  }
}

var plNextPage = async function () {
  var doNextPage = false;

  // ensure we've receive idle-callback before proceeding
  if (isIdleCallbackPending) {
    dumpLine("Waiting for idle-callback");
    await waitForIdleCallback();
  }

  if (useFNBPaint) {
    // don't move to next page until we've received fnbpaint
    if (isFNBPaintPending) {
      dumpLine("Waiting for fnbpaint");
      await waitForFNBPaint();
    }
  }

  if (usePDFPaint) {
    // don't move to next page until we've received pdfpaint
    if (isPDFPaintPending) {
      dumpLine("Waiting for pdfpaint");
      await waitForPDFPaint();
    }
  }

  if (profilingInfo) {
    await TalosParentProfiler.finishTest();
  }

  if (pageCycle < numPageCycles) {
    pageCycle++;
    doNextPage = true;
  } else if (pageIndex < pages.length - 1) {
    pageIndex++;
    recordedName = null;
    pageCycle = 1;
    doNextPage = true;
  }

  if (doNextPage) {
    if (forceCC) {
      var tccstart = window.performance.now();
      window.windowUtils.garbageCollect();
      var tccend = window.performance.now();
      report.recordCCTime(tccend - tccstart);

      // Now asynchronously trigger GC / CC in the content process
      await forceContentGC();
    }

    setTimeout(plLoadPage, delay);
  } else {
    plStop(false);
  }
};

function waitForIdleCallback() {
  return new Promise(resolve => {
    function checkForIdleCallback() {
      if (!isIdleCallbackPending) {
        resolve();
      } else {
        setTimeout(checkForIdleCallback, 5);
      }
    }
    checkForIdleCallback();
  });
}

function plIdleCallbackSet() {
  if (!scrollTest) {
    isIdleCallbackPending = true;
  }
}

function plIdleCallbackReceived() {
  isIdleCallbackPending = false;
}

function waitForFNBPaint() {
  return new Promise(resolve => {
    function checkForFNBPaint() {
      if (!isFNBPaintPending) {
        resolve();
      } else {
        setTimeout(checkForFNBPaint, 200);
      }
    }
    checkForFNBPaint();
  });
}

function waitForPDFPaint() {
  return new Promise(resolve => {
    function checkForPDFPaint() {
      if (!isPDFPaintPending) {
        resolve();
      } else {
        setTimeout(checkForPDFPaint, 200);
      }
    }
    checkForPDFPaint();
  });
}

function forceContentGC() {
  return new Promise(resolve => {
    let mm = browserWindow.gBrowser.selectedBrowser.messageManager;
    mm.addMessageListener("Talos:ForceGC:OK", function onTalosContentForceGC() {
      mm.removeMessageListener("Talos:ForceGC:OK", onTalosContentForceGC);
      resolve();
    });
    mm.sendAsyncMessage("Talos:ForceGC");
  });
}

function plRecordTime(time) {
  var pageName = pages[pageIndex].url.spec;
  var i = pageIndex;
  if (i < pages.length - 1) {
    i++;
  } else {
    i = 0;
  }
  var nextName = pages[i].url.spec;
  if (!recordedName) {
    // when doing base vs ref type of test, add pre 'base' or 'ref' to reported page name;
    // this is necessary so that if multiple subtests use same reference page, results for
    // each ref page run will be kept separate for each base vs ref run, and not grouped
    // into just one set of results values for everytime that reference page was loaded
    if (baseVsRef) {
      recordedName = pages[pageIndex].pre + pageUrls[pageIndex];
    } else {
      recordedName = pageUrls[pageIndex];
    }
  }
  if (typeof time == "string") {
    var times = time.split(",");
    var names = recordedName.split(",");
    for (var t = 0; t < times.length; t++) {
      if (names.length == 1) {
        report.recordTime(names, times[t]);
      } else {
        report.recordTime(names[t], times[t]);
      }
    }
  } else {
    report.recordTime(recordedName, time);
  }
  dumpLine(
    "Cycle " +
      (cycle + 1) +
      "(" +
      pageCycle +
      "): loaded " +
      pageName +
      " (next: " +
      nextName +
      ")"
  );
}

function plLoadHandlerCapturing(evt) {
  // make sure we pick up the right load event
  if (evt.type != "load" || evt.originalTarget.defaultView.frameElement) {
    return;
  }

  // set the tpRecordTime function (called from test pages we load) to store a global time.
  gBrowser.contentWindow.wrappedJSObject.tpRecordTime = function (
    time,
    startTime,
    testName
  ) {
    gTime = time;
    gStartTime = startTime;
    recordedName = testName;
    setTimeout(plWaitForPaintingCapturing, 0);
  };

  gBrowser.contentWindow.wrappedJSObject.plGarbageCollect = function () {
    window.windowUtils.garbageCollect();
  };

  gBrowser.removeEventListener("load", plLoadHandlerCapturing, true);

  setTimeout(plWaitForPaintingCapturing, 0);
}

// Shim function this is really defined in tscroll.js
function sendScroll() {
  const SCROLL_TEST_STEP_PX = 10;
  const SCROLL_TEST_NUM_STEPS = 100;
  // The page doesn't really use tpRecordTime. Instead, we trigger the scroll test,
  // and the scroll test will call tpRecordTime which will take us to the next page
  let details = {
    target: "content",
    stepSize: SCROLL_TEST_STEP_PX,
    opt_numSteps: SCROLL_TEST_NUM_STEPS,
  };
  let mm = gBrowser.selectedBrowser.messageManager;
  mm.sendAsyncMessage("PageLoader:ScrollTest", { details });
}

function plWaitForPaintingCapturing() {
  if (gPaintListener) {
    return;
  }

  var utils = gPaintWindow.windowUtils;

  if (utils.isMozAfterPaintPending && useMozAfterPaint) {
    if (!gPaintListener) {
      gPaintWindow.addEventListener("MozAfterPaint", plPaintedCapturing, true);
    }
    gPaintListener = true;
    return;
  }

  _loadHandlerCapturing();
}

function plPaintedCapturing() {
  gPaintWindow.removeEventListener("MozAfterPaint", plPaintedCapturing, true);
  gPaintListener = false;
  _loadHandlerCapturing();
}

function _loadHandlerCapturing() {
  failTimeout.clear();

  if (!(plPageFlags() & TEST_DOES_OWN_TIMING)) {
    dumpLine(
      "tp: Capturing onload handler used with page that doesn't do its own timing?"
    );
    plStop(true);
  }

  if (useMozAfterPaint) {
    if (gStartTime != null && gStartTime >= 0) {
      gTime =
        window.performance.timing.navigationStart +
        window.performance.now() -
        gStartTime;
      gStartTime = -1;
    }
  }

  if (gTime !== -1) {
    plRecordTime(gTime);
    TalosParentProfiler.subtestEnd("Talos - capturing load handler fired");
    gTime = -1;
    recordedName = null;
    setTimeout(plNextPage, delay);
  }
}

// the onload handler
function plLoadHandler(evt) {
  // make sure we pick up the right load event
  if (evt.type != "load" || evt.originalTarget.defaultView.frameElement) {
    return;
  }

  gBrowser.removeEventListener("load", plLoadHandler, true);
  setTimeout(waitForPainted, 0);
}

// This is called after we have received a load event, now we wait for painted
function waitForPainted() {
  var utils = gPaintWindow.windowUtils;

  if (!utils.isMozAfterPaintPending || !useMozAfterPaint) {
    _loadHandler();
    return;
  }

  if (!gPaintListener) {
    gPaintWindow.addEventListener("MozAfterPaint", plPainted, true);
  }
  gPaintListener = true;
}

function plPainted() {
  gPaintWindow.removeEventListener("MozAfterPaint", plPainted, true);
  gPaintListener = false;
  _loadHandler();
}

function _loadHandler(paint_time = 0) {
  failTimeout.clear();
  var end_time = 0;

  if (paint_time !== 0) {
    // window.performance.timing.timeToNonBlankPaint is a timestamp
    // this may have a value for hero element (also a timestamp)

    let minDate = new Date("2001");

    if (paint_time < minDate) {
      //paint_time is a performance.now() value
      end_time = paint_time;
    } else {
      //paint_time is a UNIX timestamp
      end_time = paint_time - window.performance.timing.navigationStart;
    }
  } else {
    end_time = window.performance.now();
  }

  var duration;
  if (usePDFPaint) {
    // PDF paint uses performance.now(), so the time does not need to be
    // adjusted from the start time.
    duration = end_time;
  } else {
    duration = end_time - start_time;
  }
  TalosParentProfiler.subtestEnd("Bubbling load handler fired.");

  // does this page want to do its own timing?
  // if so, we shouldn't be here
  if (plPageFlags() & TEST_DOES_OWN_TIMING) {
    dumpLine(
      "tp: Bubbling onload handler used with page that does its own timing?"
    );
    plStop(true);
  }

  plRecordTime(duration);
  plNextPage();
}

// the core handler
function plLoadHandlerMessage(message) {
  let paint_time = 0;
  // XXX message.json.name contains the name
  // of the load handler, so in future versions
  // we can record several times per load.
  if (message.json.time !== undefined) {
    paint_time = message.json.time;
    if (message.json.name == "fnbpaint") {
      // we've received fnbpaint; no longer pending for this current pageload
      isFNBPaintPending = false;
    } else if (message.json.name == "pdfpaint") {
      isPDFPaintPending = false;
    }
  }

  failTimeout.clear();

  if (plPageFlags() & EXECUTE_SCROLL_TEST) {
    // Let the page settle down after its load event, then execute the scroll test.
    setTimeout(sendScroll, 500);
  } else if (plPageFlags() & TEST_DOES_OWN_TIMING) {
    var time;

    if (typeof gStartTime != "number") {
      gStartTime = Date.parse(gStartTime);
    }
    if (gTime !== -1) {
      if (useMozAfterPaint && gStartTime >= 0) {
        time = window.performance.now() - gStartTime;
        gStartTime = -1;
      } else if (!useMozAfterPaint) {
        time = gTime;
      }
      gTime = -1;
    }

    if (time !== undefined) {
      plRecordTime(time);
      plNextPage();
    }
  } else {
    _loadHandler(paint_time);
  }
}

// the record time handler
function plRecordTimeMessage(message) {
  gTime = message.json.time;
  gStartTime = message.json.startTime;
  recordedName = message.json.testName;

  if (useMozAfterPaint) {
    gStartTime = message.json.startTime;
  }
  _loadHandlerCapturing();
}

// error
function plErrorMessage(message) {
  if (message.json.msg) {
    dumpLine(message.json.msg);
  }
  plStop(true);
}

function plStop(force) {
  plStopAll(force);
}

function plStopAll(force) {
  try {
    if (!force) {
      pageIndex = 0;
      pageCycle = 1;
      if (cycle < NUM_CYCLES - 1) {
        cycle++;
        recordedName = null;
        setTimeout(plLoadPage, delay);
        return;
      }

      /* output report */
      dumpLine(report.getReport());
      dumpLine(report.getReportSummary());
    }
  } catch (e) {
    dumpLine(e);
  }

  if (gBrowser) {
    gBrowser.removeEventListener("load", plLoadHandlerCapturing, true);
    gBrowser.removeEventListener("load", plLoadHandler, true);

    if (useMozAfterPaint) {
      gBrowser.removeEventListener("MozAfterPaint", plPaintedCapturing, true);
      gBrowser.removeEventListener("MozAfterPaint", plPainted, true);
    }

    let mm = gBrowser.selectedBrowser.messageManager;
    mm.removeMessageListener("PageLoader:LoadEvent", ContentListener);
    mm.removeMessageListener("PageLoader:RecordTime", ContentListener);
    mm.removeMessageListener("PageLoader:Error", ContentListener);

    if (isIdleCallbackPending) {
      mm.removeMessageListener("PageLoader:IdleCallbackSet", ContentListener);
      mm.removeMessageListener(
        "PageLoader:IdleCallbackReceived",
        ContentListener
      );
    }
    mm.loadFrameScript(
      "data:,removeEventListener('load', _contentLoadHandler, true);",
      false,
      true
    );
  }

  if (MozillaFileLogger && MozillaFileLogger._foStream) {
    MozillaFileLogger.close();
  }

  goQuitApplication();
}

/* Returns array */
function plLoadURLsFromURI(manifestUri) {
  var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
    Ci.nsIFileInputStream
  );

  var uriFile = manifestUri.QueryInterface(Ci.nsIFileURL);

  if (uriFile.file.isFile() === false) {
    dumpLine("tp: invalid file: %s" % uriFile.file);
    return null;
  }

  try {
    fstream.init(uriFile.file, -1, 0, 0);
  } catch (ex) {
    dumpLine("tp: the file %s doesn't exist" % uriFile.file);
    return null;
  }

  var lstream = fstream.QueryInterface(Ci.nsILineInputStream);

  var url_array = [];

  var lineNo = 0;
  var line = { value: null };
  var baseVsRefIndex = 0;
  var more;
  do {
    lineNo++;
    more = lstream.readLine(line);
    var s = line.value;

    // strip comments (only leading ones)
    s = s.replace(/^#.*/, "");

    // strip leading and trailing whitespace
    s = s.replace(/^\s*/, "").replace(/\s*$/, "");

    if (!s) {
      continue;
    }

    var flags = 0;
    var urlspec = s;
    baseVsRefIndex += 1;

    // split on whitespace, and figure out if we have any flags
    var items = s.split(/\s+/);
    if (items[0] == "include") {
      if (items.length != 2) {
        dumpLine(
          "tp: Error on line " +
            lineNo +
            " in " +
            manifestUri.spec +
            ": include must be followed by the manifest to include!"
        );
        return null;
      }

      var subManifest = Services.io.newURI(items[1], null, manifestUri);
      if (subManifest == null) {
        dumpLine(
          "tp: invalid URI on line " +
            manifestUri.spec +
            ":" +
            lineNo +
            " : '" +
            line.value +
            "'"
        );
        return null;
      }

      var subItems = plLoadURLsFromURI(subManifest);
      if (subItems == null) {
        return null;
      }
      url_array = url_array.concat(subItems);
    } else {
      // For scrollTest flag, we accept "normal" pages but treat them as TEST_DOES_OWN_TIMING
      // together with EXECUTE_SCROLL_TEST which makes us run the scroll test on load.
      // We do this by artificially "injecting" the TEST_DOES_OWN_TIMING flag ("%") to the item
      // and then let the default flow for this flag run without further modifications
      // (other than calling the scroll test once the page is loaded).
      // Note that if we have the scrollTest flag but the item already has "%", then we do
      // nothing (the scroll test will not execute, and the page will report with its
      // own tpRecordTime and not the one from the scroll test).
      if (scrollTest && items.length == 1) {
        // scroll enabled and no "%"
        items.unshift("%");
        flags |= EXECUTE_SCROLL_TEST;
      }

      if (items.length == 2) {
        if (items[0].includes("%")) {
          flags |= TEST_DOES_OWN_TIMING;
        }

        urlspec = items[1];
      } else if (items.length == 3) {
        // base vs ref type of talos test
        // expect each manifest line to be in the format of:
        // & http://localhost/tests/perf-reftest/base-page.html, http://localhost/tests/perf-reftest/reference-page.html
        // test will run with the base page, then with the reference page; and ultimately the actual test results will
        // be the comparison values of those two pages; more than one line will result in base vs ref subtests
        if (items[0].includes("&")) {
          baseVsRef = true;
          flags |= TEST_DOES_OWN_TIMING;
          // for the base, must remove the comma on the end of the actual url
          var urlspecBase = items[1].slice(0, -1);
          var urlspecRef = items[2];
        } else {
          dumpLine(
            "tp: Error on line " +
              lineNo +
              " in " +
              manifestUri.spec +
              ": unknown manifest format!"
          );
          return null;
        }
      } else if (items.length != 1) {
        dumpLine(
          "tp: Error on line " +
            lineNo +
            " in " +
            manifestUri.spec +
            ": whitespace must be %-escaped!"
        );
        return null;
      }

      var url;

      if (!baseVsRef) {
        url = Services.io.newURI(urlspec, null, manifestUri);

        if (pageFilterRegexp && !pageFilterRegexp.test(url.spec)) {
          continue;
        }

        url_array.push({ url, flags });
      } else {
        // base vs ref type of talos test
        // we add a 'pre' prefix here indicating that this particular page is a base page or a reference
        // page; later on this 'pre' is used when recording the actual time value/result; because in
        // the results we use the url as the results key; but we might use the same test page as a reference
        // page in the same test suite, so we need to add a prefix so this results key is always unique
        url = Services.io.newURI(urlspecBase, null, manifestUri);
        if (pageFilterRegexp && !pageFilterRegexp.test(url.spec)) {
          continue;
        }
        var pre = "base_page_" + baseVsRefIndex + "_";
        url_array.push({ url, flags, pre });

        url = Services.io.newURI(urlspecRef, null, manifestUri);
        if (pageFilterRegexp && !pageFilterRegexp.test(url.spec)) {
          continue;
        }
        pre = "ref_page_" + baseVsRefIndex + "_";
        url_array.push({ url, flags, pre });
      }
    }
  } while (more);

  return url_array;
}

function dumpLine(str) {
  if (MozillaFileLogger && MozillaFileLogger._foStream) {
    MozillaFileLogger.log(str + "\n");
  }
  dump(str);
  dump("\n");
}