summaryrefslogtreecommitdiffstats
path: root/dom/workers/ScriptLoader.cpp
blob: 73997b2725715a3be52e8831b0d88d829a4357c8 (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
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "ScriptLoader.h"

#include <algorithm>
#include <type_traits>

#include "mozilla/dom/RequestBinding.h"
#include "nsIChannel.h"
#include "nsIContentPolicy.h"
#include "nsIContentSecurityPolicy.h"
#include "nsICookieJarSettings.h"
#include "nsIDocShell.h"
#include "nsIHttpChannel.h"
#include "nsIHttpChannelInternal.h"
#include "nsIIOService.h"
#include "nsIOService.h"
#include "nsIPrincipal.h"
#include "nsIProtocolHandler.h"
#include "nsIScriptError.h"
#include "nsIScriptSecurityManager.h"
#include "nsIStreamListenerTee.h"
#include "nsIThreadRetargetableRequest.h"
#include "nsIURI.h"
#include "nsIXPConnect.h"

#include "jsapi.h"
#include "jsfriendapi.h"
#include "js/CompilationAndEvaluation.h"
#include "js/Exception.h"
#include "js/SourceText.h"
#include "js/TypeDecls.h"
#include "nsError.h"
#include "nsComponentManagerUtils.h"
#include "nsContentSecurityManager.h"
#include "nsContentPolicyUtils.h"
#include "nsContentUtils.h"
#include "nsDocShellCID.h"
#include "nsJSEnvironment.h"
#include "nsNetUtil.h"
#include "nsIPipe.h"
#include "nsIOutputStream.h"
#include "nsPrintfCString.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsThreadUtils.h"
#include "nsXPCOM.h"
#include "xpcpublic.h"

#include "mozilla/AntiTrackingUtils.h"
#include "mozilla/ArrayAlgorithm.h"
#include "mozilla/Assertions.h"
#include "mozilla/Encoding.h"
#include "mozilla/LoadContext.h"
#include "mozilla/Maybe.h"
#include "mozilla/ipc/BackgroundUtils.h"
#include "mozilla/dom/ClientChannelHelper.h"
#include "mozilla/dom/ClientInfo.h"
#include "mozilla/dom/Exceptions.h"
#include "mozilla/dom/nsCSPService.h"
#include "mozilla/dom/nsCSPUtils.h"
#include "mozilla/dom/PerformanceStorage.h"
#include "mozilla/dom/Response.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/SerializedStackHolder.h"
#include "mozilla/dom/workerinternals/CacheLoadHandler.h"
#include "mozilla/dom/workerinternals/NetworkLoadHandler.h"
#include "mozilla/dom/workerinternals/ScriptResponseHeaderProcessor.h"
#include "mozilla/Result.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/UniquePtr.h"
#include "WorkerRunnable.h"
#include "WorkerScope.h"

#define MAX_CONCURRENT_SCRIPTS 1000

using JS::loader::ParserMetadata;
using JS::loader::ScriptKind;
using JS::loader::ScriptLoadRequest;
using mozilla::ipc::PrincipalInfo;

namespace mozilla::dom::workerinternals {
namespace {

nsresult ConstructURI(const nsAString& aScriptURL, nsIURI* baseURI,
                      const mozilla::Encoding* aDocumentEncoding,
                      nsIURI** aResult) {
  nsresult rv;
  // Only top level workers' main script use the document charset for the
  // script uri encoding. Otherwise, default encoding (UTF-8) is applied.
  if (aDocumentEncoding) {
    nsAutoCString charset;
    aDocumentEncoding->Name(charset);
    rv = NS_NewURI(aResult, aScriptURL, charset.get(), baseURI);
  } else {
    rv = NS_NewURI(aResult, aScriptURL, nullptr, baseURI);
  }

  if (NS_FAILED(rv)) {
    return NS_ERROR_DOM_SYNTAX_ERR;
  }
  return NS_OK;
}

nsresult ChannelFromScriptURL(
    nsIPrincipal* principal, Document* parentDoc, WorkerPrivate* aWorkerPrivate,
    nsILoadGroup* loadGroup, nsIIOService* ios,
    nsIScriptSecurityManager* secMan, nsIURI* aScriptURL,
    const Maybe<ClientInfo>& aClientInfo,
    const Maybe<ServiceWorkerDescriptor>& aController, bool aIsMainScript,
    WorkerScriptType aWorkerScriptType, nsContentPolicyType aContentPolicyType,
    nsLoadFlags aLoadFlags, uint32_t aSecFlags,
    nsICookieJarSettings* aCookieJarSettings, nsIReferrerInfo* aReferrerInfo,
    nsIChannel** aChannel) {
  AssertIsOnMainThread();

  nsresult rv;
  nsCOMPtr<nsIURI> uri = aScriptURL;

  // Only use the document when its principal matches the principal of the
  // current request. This means scripts fetched using the Workers' own
  // principal won't inherit properties of the document, in particular the CSP.
  if (parentDoc && parentDoc->NodePrincipal() != principal) {
    parentDoc = nullptr;
  }

  // The main service worker script should never be loaded over the network
  // in this path.  It should always be offlined by ServiceWorkerScriptCache.
  // We assert here since this error should also be caught by the runtime
  // check in CacheLoadHandler.
  //
  // Note, if we ever allow service worker scripts to be loaded from network
  // here we need to configure the channel properly.  For example, it must
  // not allow redirects.
  MOZ_DIAGNOSTIC_ASSERT(aContentPolicyType !=
                        nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER);

  nsCOMPtr<nsIChannel> channel;
  if (parentDoc) {
    // This is the path for top level dedicated worker scripts with a document
    rv = NS_NewChannel(getter_AddRefs(channel), uri, parentDoc, aSecFlags,
                       aContentPolicyType,
                       nullptr,  // aPerformanceStorage
                       loadGroup,
                       nullptr,  // aCallbacks
                       aLoadFlags, ios);
    NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
  } else {
    // This branch is used in the following cases:
    //    * Shared and ServiceWorkers (who do not have a doc)
    //    * Static Module Imports
    //    * ImportScripts

    // We must have a loadGroup with a load context for the principal to
    // traverse the channel correctly.

    MOZ_ASSERT(loadGroup);
    MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadGroup, principal));

    RefPtr<PerformanceStorage> performanceStorage;
    nsCOMPtr<nsICSPEventListener> cspEventListener;
    if (aWorkerPrivate && !aIsMainScript) {
      performanceStorage = aWorkerPrivate->GetPerformanceStorage();
      cspEventListener = aWorkerPrivate->CSPEventListener();
    }

    if (aClientInfo.isSome()) {
      // If we have an existing clientInfo (true for all modules and
      // importScripts), we will use this branch
      rv = NS_NewChannel(getter_AddRefs(channel), uri, principal,
                         aClientInfo.ref(), aController, aSecFlags,
                         aContentPolicyType, aCookieJarSettings,
                         performanceStorage, loadGroup, nullptr,  // aCallbacks
                         aLoadFlags, ios);
    } else {
      rv = NS_NewChannel(getter_AddRefs(channel), uri, principal, aSecFlags,
                         aContentPolicyType, aCookieJarSettings,
                         performanceStorage, loadGroup, nullptr,  // aCallbacks
                         aLoadFlags, ios);
    }

    NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);

    if (cspEventListener) {
      nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
      rv = loadInfo->SetCspEventListener(cspEventListener);
      NS_ENSURE_SUCCESS(rv, rv);
    }
  }

  if (aReferrerInfo) {
    nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel);
    if (httpChannel) {
      rv = httpChannel->SetReferrerInfo(aReferrerInfo);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return rv;
      }
    }
  }

  channel.forget(aChannel);
  return rv;
}

void LoadAllScripts(WorkerPrivate* aWorkerPrivate,
                    UniquePtr<SerializedStackHolder> aOriginStack,
                    const nsTArray<nsString>& aScriptURLs, bool aIsMainScript,
                    WorkerScriptType aWorkerScriptType, ErrorResult& aRv,
                    const mozilla::Encoding* aDocumentEncoding = nullptr) {
  aWorkerPrivate->AssertIsOnWorkerThread();
  NS_ASSERTION(!aScriptURLs.IsEmpty(), "Bad arguments!");

  AutoSyncLoopHolder syncLoop(aWorkerPrivate, Canceling);
  nsCOMPtr<nsISerialEventTarget> syncLoopTarget =
      syncLoop.GetSerialEventTarget();
  if (!syncLoopTarget) {
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
    return;
  }

  RefPtr<loader::WorkerScriptLoader> loader =
      loader::WorkerScriptLoader::Create(
          aWorkerPrivate, std::move(aOriginStack), syncLoopTarget,
          aWorkerScriptType, aRv);

  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  bool ok = loader->CreateScriptRequests(aScriptURLs, aDocumentEncoding,
                                         aIsMainScript);

  if (!ok) {
    return;
  }
  // Bug 1817259 - For now, we force loading the debugger script as Classic,
  // even if the debugged worker is a Module.
  if (aWorkerPrivate->WorkerType() == WorkerType::Module &&
      aWorkerScriptType != DebuggerScript) {
    if (!StaticPrefs::dom_workers_modules_enabled()) {
      aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
      return;
    }
    MOZ_ASSERT(aIsMainScript);
    // Module Load
    RefPtr<JS::loader::ScriptLoadRequest> mainScript = loader->GetMainScript();
    if (mainScript && mainScript->IsModuleRequest()) {
      if (NS_FAILED(mainScript->AsModuleRequest()->StartModuleLoad())) {
        return;
      }
      syncLoop.Run();
      return;
    }
  }

  if (loader->DispatchLoadScripts()) {
    syncLoop.Run();
  }
}

class ChannelGetterRunnable final : public WorkerMainThreadRunnable {
  const nsAString& mScriptURL;
  const WorkerType& mWorkerType;
  const RequestCredentials& mCredentials;
  const ClientInfo mClientInfo;
  WorkerLoadInfo& mLoadInfo;
  nsresult mResult;

 public:
  ChannelGetterRunnable(WorkerPrivate* aParentWorker,
                        const nsAString& aScriptURL,
                        const WorkerType& aWorkerType,
                        const RequestCredentials& aCredentials,
                        WorkerLoadInfo& aLoadInfo)
      : WorkerMainThreadRunnable(aParentWorker,
                                 "ScriptLoader :: ChannelGetter"_ns),
        mScriptURL(aScriptURL)
        // ClientInfo should always be present since this should not be called
        // if parent's status is greater than Running.
        ,
        mWorkerType(aWorkerType),
        mCredentials(aCredentials),
        mClientInfo(aParentWorker->GlobalScope()->GetClientInfo().ref()),
        mLoadInfo(aLoadInfo),
        mResult(NS_ERROR_FAILURE) {
    MOZ_ASSERT(aParentWorker);
    aParentWorker->AssertIsOnWorkerThread();
  }

  virtual bool MainThreadRun() override {
    AssertIsOnMainThread();

    // Initialize the WorkerLoadInfo principal to our triggering principal
    // before doing anything else.  Normally we do this in the WorkerPrivate
    // Constructor, but we can't do so off the main thread when creating
    // a nested worker.  So do it here instead.
    mLoadInfo.mLoadingPrincipal = mWorkerPrivate->GetPrincipal();
    MOZ_DIAGNOSTIC_ASSERT(mLoadInfo.mLoadingPrincipal);

    mLoadInfo.mPrincipal = mLoadInfo.mLoadingPrincipal;

    // Figure out our base URI.
    nsCOMPtr<nsIURI> baseURI = mWorkerPrivate->GetBaseURI();
    MOZ_ASSERT(baseURI);

    // May be null.
    nsCOMPtr<Document> parentDoc = mWorkerPrivate->GetDocument();

    mLoadInfo.mLoadGroup = mWorkerPrivate->GetLoadGroup();
    mLoadInfo.mCookieJarSettings = mWorkerPrivate->CookieJarSettings();

    // Nested workers use default uri encoding.
    nsCOMPtr<nsIURI> url;
    mResult = ConstructURI(mScriptURL, baseURI, nullptr, getter_AddRefs(url));
    NS_ENSURE_SUCCESS(mResult, true);

    Maybe<ClientInfo> clientInfo;
    clientInfo.emplace(mClientInfo);

    nsCOMPtr<nsIChannel> channel;
    nsCOMPtr<nsIReferrerInfo> referrerInfo =
        ReferrerInfo::CreateForFetch(mLoadInfo.mLoadingPrincipal, nullptr);
    mLoadInfo.mReferrerInfo =
        static_cast<ReferrerInfo*>(referrerInfo.get())
            ->CloneWithNewPolicy(mWorkerPrivate->GetReferrerPolicy());

    mResult = workerinternals::ChannelFromScriptURLMainThread(
        mLoadInfo.mLoadingPrincipal, parentDoc, mLoadInfo.mLoadGroup, url,
        mWorkerType, mCredentials, clientInfo,
        // Nested workers are always dedicated.
        nsIContentPolicy::TYPE_INTERNAL_WORKER, mLoadInfo.mCookieJarSettings,
        mLoadInfo.mReferrerInfo, getter_AddRefs(channel));
    NS_ENSURE_SUCCESS(mResult, true);

    mResult = mLoadInfo.SetPrincipalsAndCSPFromChannel(channel);
    NS_ENSURE_SUCCESS(mResult, true);

    mLoadInfo.mChannel = std::move(channel);
    return true;
  }

  nsresult GetResult() const { return mResult; }

 private:
  virtual ~ChannelGetterRunnable() = default;
};

nsresult GetCommonSecFlags(bool aIsMainScript, nsIURI* uri,
                           nsIPrincipal* principal,
                           WorkerScriptType aWorkerScriptType,
                           uint32_t& secFlags) {
  bool inheritAttrs = nsContentUtils::ChannelShouldInheritPrincipal(
      principal, uri, true /* aInheritForAboutBlank */,
      false /* aForceInherit */);

  bool isData = uri->SchemeIs("data");
  if (inheritAttrs && !isData) {
    secFlags |= nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL;
  }

  if (aWorkerScriptType == DebuggerScript) {
    // A DebuggerScript needs to be a local resource like chrome: or resource:
    bool isUIResource = false;
    nsresult rv = NS_URIChainHasFlags(
        uri, nsIProtocolHandler::URI_IS_UI_RESOURCE, &isUIResource);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    if (!isUIResource) {
      return NS_ERROR_DOM_SECURITY_ERR;
    }

    secFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
  }

  // Note: this is for backwards compatibility and goes against spec.
  // We should find a better solution.
  if (aIsMainScript && isData) {
    secFlags = nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL;
  }

  return NS_OK;
}

nsresult GetModuleSecFlags(bool aIsTopLevel, nsIPrincipal* principal,
                           WorkerScriptType aWorkerScriptType, nsIURI* aURI,
                           RequestCredentials aCredentials,
                           uint32_t& secFlags) {
  // Implements "To fetch a single module script,"
  // Step 9. If destination is "worker", "sharedworker", or "serviceworker",
  //         and the top-level module fetch flag is set, then set request's
  //         mode to "same-origin".

  // Step 8. Let request be a new request whose [...] mode is "cors" [...]
  secFlags = aIsTopLevel ? nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED
                         : nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT;

  // This implements the same Cookie settings as  nsContentSecurityManager's
  // ComputeSecurityFlags. The main difference is the line above, Step 9,
  // setting to same origin.

  if (aCredentials == RequestCredentials::Include) {
    secFlags |= nsILoadInfo::nsILoadInfo::SEC_COOKIES_INCLUDE;
  } else if (aCredentials == RequestCredentials::Same_origin) {
    secFlags |= nsILoadInfo::nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
  } else if (aCredentials == RequestCredentials::Omit) {
    secFlags |= nsILoadInfo::nsILoadInfo::SEC_COOKIES_OMIT;
  }

  return GetCommonSecFlags(aIsTopLevel, aURI, principal, aWorkerScriptType,
                           secFlags);
}

nsresult GetClassicSecFlags(bool aIsMainScript, nsIURI* uri,
                            nsIPrincipal* principal,
                            WorkerScriptType aWorkerScriptType,
                            uint32_t& secFlags) {
  secFlags = aIsMainScript
                 ? nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED
                 : nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT;

  return GetCommonSecFlags(aIsMainScript, uri, principal, aWorkerScriptType,
                           secFlags);
}

}  //  anonymous namespace

namespace loader {

class ScriptExecutorRunnable final : public MainThreadWorkerSyncRunnable {
  RefPtr<WorkerScriptLoader> mScriptLoader;
  const Span<RefPtr<ThreadSafeRequestHandle>> mLoadedRequests;

 public:
  ScriptExecutorRunnable(WorkerScriptLoader* aScriptLoader,
                         WorkerPrivate* aWorkerPrivate,
                         nsISerialEventTarget* aSyncLoopTarget,
                         Span<RefPtr<ThreadSafeRequestHandle>> aLoadedRequests);

 private:
  ~ScriptExecutorRunnable() = default;

  virtual bool IsDebuggerRunnable() const override;

  virtual bool PreRun(WorkerPrivate* aWorkerPrivate) override;

  bool ProcessModuleScript(JSContext* aCx, WorkerPrivate* aWorkerPrivate);

  bool ProcessClassicScripts(JSContext* aCx, WorkerPrivate* aWorkerPrivate);

  virtual bool WorkerRun(JSContext* aCx,
                         WorkerPrivate* aWorkerPrivate) override;

  nsresult Cancel() override;
};

static bool EvaluateSourceBuffer(JSContext* aCx, JS::Handle<JSScript*> aScript,
                                 JS::loader::ClassicScript* aClassicScript) {
  if (aClassicScript) {
    aClassicScript->AssociateWithScript(aScript);
  }

  JS::Rooted<JS::Value> unused(aCx);
  return JS_ExecuteScript(aCx, aScript, &unused);
}

WorkerScriptLoader::WorkerScriptLoader(
    UniquePtr<SerializedStackHolder> aOriginStack,
    nsISerialEventTarget* aSyncLoopTarget, WorkerScriptType aWorkerScriptType,
    ErrorResult& aRv)
    : mOriginStack(std::move(aOriginStack)),
      mSyncLoopTarget(aSyncLoopTarget),
      mWorkerScriptType(aWorkerScriptType),
      mRv(aRv),
      mLoadingModuleRequestCount(0),
      mCleanedUp(false),
      mCleanUpLock("cleanUpLock") {}

already_AddRefed<WorkerScriptLoader> WorkerScriptLoader::Create(
    WorkerPrivate* aWorkerPrivate,
    UniquePtr<SerializedStackHolder> aOriginStack,
    nsISerialEventTarget* aSyncLoopTarget, WorkerScriptType aWorkerScriptType,
    ErrorResult& aRv) {
  aWorkerPrivate->AssertIsOnWorkerThread();

  RefPtr<WorkerScriptLoader> self = new WorkerScriptLoader(
      std::move(aOriginStack), aSyncLoopTarget, aWorkerScriptType, aRv);

  RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
      aWorkerPrivate, "WorkerScriptLoader::Create", [self]() {
        // Requests that are in flight are covered by the worker references
        // in DispatchLoadScript(s), so we do not need to do additional
        // cleanup, but just in case we are ready/aborted we can try to
        // shutdown here, too.
        self->TryShutdown();
      });

  if (workerRef) {
    self->mWorkerRef = new ThreadSafeWorkerRef(workerRef);
  } else {
    self->mRv.Throw(NS_ERROR_FAILURE);
    return nullptr;
  }

  nsIGlobalObject* global = self->GetGlobal();
  self->mController = global->GetController();

  if (!StaticPrefs::dom_workers_modules_enabled()) {
    return self.forget();
  }

  // Set up the module loader, if it has not been initialzied yet.
  if (!aWorkerPrivate->IsServiceWorker()) {
    self->InitModuleLoader();
  }

  return self.forget();
}

ScriptLoadRequest* WorkerScriptLoader::GetMainScript() {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  ScriptLoadRequest* request = mLoadingRequests.getFirst();
  if (request->GetWorkerLoadContext()->IsTopLevel()) {
    return request;
  }
  return nullptr;
}

void WorkerScriptLoader::InitModuleLoader() {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  if (GetGlobal()->GetModuleLoader(nullptr)) {
    return;
  }
  RefPtr<WorkerModuleLoader> moduleLoader =
      new WorkerModuleLoader(this, GetGlobal());
  if (mWorkerScriptType == WorkerScript) {
    mWorkerRef->Private()->GlobalScope()->InitModuleLoader(moduleLoader);
    return;
  }
  mWorkerRef->Private()->DebuggerGlobalScope()->InitModuleLoader(moduleLoader);
}

bool WorkerScriptLoader::CreateScriptRequests(
    const nsTArray<nsString>& aScriptURLs,
    const mozilla::Encoding* aDocumentEncoding, bool aIsMainScript) {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  // If a worker has been loaded as a module worker, ImportScripts calls are
  // disallowed -- then the operation is invalid.
  //
  // 10.3.1 Importing scripts and libraries.
  // Step 1. If worker global scope's type is "module", throw a TypeError
  //         exception.
  //
  // Also, for now, the debugger script is always loaded as Classic,
  // even if the debugged worker is a Module. We still want to allow
  // it to use importScripts.
  if (mWorkerRef->Private()->WorkerType() == WorkerType::Module &&
      !aIsMainScript && !IsDebuggerScript()) {
    // This should only run for non-main scripts, as only these are
    // importScripts
    mRv.ThrowTypeError(
        "Using `ImportScripts` inside a Module Worker is "
        "disallowed.");
    return false;
  }
  for (const nsString& scriptURL : aScriptURLs) {
    RefPtr<ScriptLoadRequest> request =
        CreateScriptLoadRequest(scriptURL, aDocumentEncoding, aIsMainScript);
    if (!request) {
      return false;
    }
    mLoadingRequests.AppendElement(request);
  }

  return true;
}

nsTArray<RefPtr<ThreadSafeRequestHandle>> WorkerScriptLoader::GetLoadingList() {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  nsTArray<RefPtr<ThreadSafeRequestHandle>> list;
  for (ScriptLoadRequest* req = mLoadingRequests.getFirst(); req;
       req = req->getNext()) {
    RefPtr<ThreadSafeRequestHandle> handle =
        new ThreadSafeRequestHandle(req, mSyncLoopTarget.get());
    list.AppendElement(handle.forget());
  }
  return list;
}

bool WorkerScriptLoader::IsDynamicImport(ScriptLoadRequest* aRequest) {
  return aRequest->IsModuleRequest() &&
         aRequest->AsModuleRequest()->IsDynamicImport();
}

nsContentPolicyType WorkerScriptLoader::GetContentPolicyType(
    ScriptLoadRequest* aRequest) {
  if (aRequest->GetWorkerLoadContext()->IsTopLevel()) {
    // Implements https://html.spec.whatwg.org/#worker-processing-model
    // Step 13: Let destination be "sharedworker" if is shared is true, and
    // "worker" otherwise.
    return mWorkerRef->Private()->ContentPolicyType();
  }
  if (aRequest->IsModuleRequest()) {
    if (aRequest->AsModuleRequest()->IsDynamicImport()) {
      return nsIContentPolicy::TYPE_INTERNAL_MODULE;
    }

    // Implements the destination for Step 14 in
    // https://html.spec.whatwg.org/#worker-processing-model
    //
    // We need a special subresource type in order to correctly implement
    // the graph fetch, where the destination is set to "worker" or
    // "sharedworker".
    return nsIContentPolicy::TYPE_INTERNAL_WORKER_STATIC_MODULE;
  }
  // For script imported in worker's importScripts().
  return nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS;
}

already_AddRefed<ScriptLoadRequest> WorkerScriptLoader::CreateScriptLoadRequest(
    const nsString& aScriptURL, const mozilla::Encoding* aDocumentEncoding,
    bool aIsMainScript) {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  WorkerLoadContext::Kind kind =
      WorkerLoadContext::GetKind(aIsMainScript, IsDebuggerScript());

  Maybe<ClientInfo> clientInfo = GetGlobal()->GetClientInfo();

  // (For non-serviceworkers, this variable does not matter, but false best
  // captures their behavior.)
  bool onlyExistingCachedResourcesAllowed = false;
  if (mWorkerRef->Private()->IsServiceWorker()) {
    // https://w3c.github.io/ServiceWorker/#importscripts step 4:
    // > 4. If serviceWorker’s state is not "parsed" or "installing":
    // >    1. Return map[url] if it exists and a network error otherwise.
    //
    // So if our state is beyond installing, it's too late to make a request
    // that would perform a new fetch which would be cached.
    onlyExistingCachedResourcesAllowed =
        mWorkerRef->Private()->GetServiceWorkerDescriptor().State() >
        ServiceWorkerState::Installing;
  }
  RefPtr<WorkerLoadContext> loadContext = new WorkerLoadContext(
      kind, clientInfo, this, onlyExistingCachedResourcesAllowed);

  // Create ScriptLoadRequests for this WorkerScriptLoader
  ReferrerPolicy referrerPolicy = mWorkerRef->Private()->GetReferrerPolicy();

  // Only top level workers' main script use the document charset for the
  // script uri encoding. Otherwise, default encoding (UTF-8) is applied.
  MOZ_ASSERT_IF(bool(aDocumentEncoding),
                aIsMainScript && !mWorkerRef->Private()->GetParent());
  nsCOMPtr<nsIURI> baseURI = aIsMainScript ? GetInitialBaseURI() : GetBaseURI();
  nsCOMPtr<nsIURI> uri;
  bool setErrorResult = false;
  nsresult rv =
      ConstructURI(aScriptURL, baseURI, aDocumentEncoding, getter_AddRefs(uri));
  // If we failed to construct the URI, handle it in the LoadContext so it is
  // thrown in the right order.
  if (NS_WARN_IF(NS_FAILED(rv))) {
    setErrorResult = true;
    loadContext->mLoadResult = rv;
  }

  // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-worker-script
  // Step 2.5. Let script be the result [...] and the default classic script
  // fetch options.
  //
  // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-worklet/module-worker-script-graph
  // Step 1. Let options be a script fetch options whose cryptographic nonce is
  // the empty string, integrity metadata is the empty string, parser metadata
  // is "not-parser-inserted", credentials mode is credentials mode, referrer
  // policy is the empty string, and fetch priority is "auto".
  RefPtr<ScriptFetchOptions> fetchOptions = new ScriptFetchOptions(
      CORSMode::CORS_NONE, /* aNonce = */ u""_ns, RequestPriority::Auto,
      ParserMetadata::NotParserInserted, nullptr);

  RefPtr<ScriptLoadRequest> request = nullptr;
  // Bug 1817259 - For now the debugger scripts are always loaded a Classic.
  if (mWorkerRef->Private()->WorkerType() == WorkerType::Classic ||
      IsDebuggerScript()) {
    request = new ScriptLoadRequest(ScriptKind::eClassic, uri, referrerPolicy,
                                    fetchOptions, SRIMetadata(),
                                    nullptr,  // mReferrer
                                    loadContext);
  } else {
    // Implements part of "To fetch a worklet/module worker script graph"
    // including, setting up the request with a credentials mode,
    // destination.

    // Step 1. Let options be a script fetch options.
    // We currently don't track credentials in our ScriptFetchOptions
    // implementation, so we are defaulting the fetchOptions object defined
    // above. This behavior is handled fully in GetModuleSecFlags.

    if (!StaticPrefs::dom_workers_modules_enabled()) {
      mRv.ThrowTypeError("Modules in workers are currently disallowed.");
      return nullptr;
    }
    RefPtr<WorkerModuleLoader::ModuleLoaderBase> moduleLoader =
        GetGlobal()->GetModuleLoader(nullptr);

    // Implements the referrer for "To fetch a single module script"
    // Our implementation does not have a "client" as a referrer.
    // However, when client is resolved (per 8.3. Determine request’s
    // Referrer in
    // https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer)
    // This should result in the referrer source being the creation URL.
    //
    // In subresource modules, the referrer is the importing script.
    nsCOMPtr<nsIURI> referrer =
        mWorkerRef->Private()->GetReferrerInfo()->GetOriginalReferrer();

    // Part of Step 2. This sets the Top-level flag to true
    request = new ModuleLoadRequest(
        uri, referrerPolicy, fetchOptions, SRIMetadata(), referrer, loadContext,
        true,  /* is top level */
        false, /* is dynamic import */
        moduleLoader, ModuleLoadRequest::NewVisitedSetForTopLevelImport(uri),
        nullptr);
  }

  // Set the mURL, it will be used for error handling and debugging.
  request->mURL = NS_ConvertUTF16toUTF8(aScriptURL);

  if (setErrorResult) {
    request->SetPendingFetchingError();
  } else {
    request->NoCacheEntryFound();
  }

  return request.forget();
}

bool WorkerScriptLoader::DispatchLoadScript(ScriptLoadRequest* aRequest) {
  mWorkerRef->Private()->AssertIsOnWorkerThread();

  IncreaseLoadingModuleRequestCount();

  nsTArray<RefPtr<ThreadSafeRequestHandle>> scriptLoadList;
  RefPtr<ThreadSafeRequestHandle> handle =
      new ThreadSafeRequestHandle(aRequest, mSyncLoopTarget.get());
  scriptLoadList.AppendElement(handle.forget());

  RefPtr<ScriptLoaderRunnable> runnable =
      new ScriptLoaderRunnable(this, std::move(scriptLoadList));

  RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
      mWorkerRef->Private(), "WorkerScriptLoader::DispatchLoadScript",
      [runnable]() {
        NS_DispatchToMainThread(NewRunnableMethod(
            "ScriptLoaderRunnable::CancelMainThreadWithBindingAborted",
            runnable,
            &ScriptLoaderRunnable::CancelMainThreadWithBindingAborted));
      });

  if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
    NS_ERROR("Failed to dispatch!");
    mRv.Throw(NS_ERROR_FAILURE);
    return false;
  }
  return true;
}

bool WorkerScriptLoader::DispatchLoadScripts() {
  mWorkerRef->Private()->AssertIsOnWorkerThread();

  nsTArray<RefPtr<ThreadSafeRequestHandle>> scriptLoadList = GetLoadingList();

  RefPtr<ScriptLoaderRunnable> runnable =
      new ScriptLoaderRunnable(this, std::move(scriptLoadList));

  RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
      mWorkerRef->Private(), "WorkerScriptLoader::DispatchLoadScripts",
      [runnable]() {
        NS_DispatchToMainThread(NewRunnableMethod(
            "ScriptLoaderRunnable::CancelMainThreadWithBindingAborted",
            runnable,
            &ScriptLoaderRunnable::CancelMainThreadWithBindingAborted));
      });

  if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
    NS_ERROR("Failed to dispatch!");
    mRv.Throw(NS_ERROR_FAILURE);
    return false;
  }
  return true;
}

nsIURI* WorkerScriptLoader::GetInitialBaseURI() {
  MOZ_ASSERT(mWorkerRef->Private());
  nsIURI* baseURI;
  WorkerPrivate* parentWorker = mWorkerRef->Private()->GetParent();
  if (parentWorker) {
    baseURI = parentWorker->GetBaseURI();
  } else {
    // May be null.
    baseURI = mWorkerRef->Private()->GetBaseURI();
  }

  return baseURI;
}

nsIURI* WorkerScriptLoader::GetBaseURI() const {
  MOZ_ASSERT(mWorkerRef);
  nsIURI* baseURI;
  baseURI = mWorkerRef->Private()->GetBaseURI();
  NS_ASSERTION(baseURI, "Should have been set already!");

  return baseURI;
}

nsIGlobalObject* WorkerScriptLoader::GetGlobal() {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  return mWorkerScriptType == WorkerScript
             ? static_cast<nsIGlobalObject*>(
                   mWorkerRef->Private()->GlobalScope())
             : mWorkerRef->Private()->DebuggerGlobalScope();
}

void WorkerScriptLoader::MaybeMoveToLoadedList(ScriptLoadRequest* aRequest) {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  // Only set to ready for regular scripts. Module loader will set the script to
  // ready if it is a Module Request.
  if (!aRequest->IsModuleRequest()) {
    aRequest->SetReady();
  }

  // If the request is not in a list, we are in an illegal state.
  MOZ_RELEASE_ASSERT(aRequest->isInList());

  while (!mLoadingRequests.isEmpty()) {
    ScriptLoadRequest* request = mLoadingRequests.getFirst();
    // We need to move requests in post order. If prior requests have not
    // completed, delay execution.
    if (!request->IsFinished()) {
      break;
    }

    RefPtr<ScriptLoadRequest> req = mLoadingRequests.Steal(request);
    mLoadedRequests.AppendElement(req);
  }
}

bool WorkerScriptLoader::StoreCSP() {
  // We must be on the same worker as we started on.
  mWorkerRef->Private()->AssertIsOnWorkerThread();

  if (!mWorkerRef->Private()->GetJSContext()) {
    return false;
  }

  MOZ_ASSERT(!mRv.Failed());

  // Move the CSP from the workerLoadInfo in the corresponding Client
  // where the CSP code expects it!
  mWorkerRef->Private()->StoreCSPOnClient();
  return true;
}

bool WorkerScriptLoader::ProcessPendingRequests(JSContext* aCx) {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  // Don't run if something else has already failed.
  if (mExecutionAborted) {
    mLoadedRequests.CancelRequestsAndClear();
    TryShutdown();
    return true;
  }

  // If nothing else has failed, our ErrorResult better not be a failure
  // either.
  MOZ_ASSERT(!mRv.Failed(), "Who failed it and why?");

  // Slightly icky action at a distance, but there's no better place to stash
  // this value, really.
  JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
  MOZ_ASSERT(global);

  while (!mLoadedRequests.isEmpty()) {
    RefPtr<ScriptLoadRequest> req = mLoadedRequests.StealFirst();
    // We don't have a ProcessRequest method (like we do on the DOM), as there
    // isn't much processing that we need to do per request that isn't related
    // to evaluation (the processsing done for the DOM is handled in
    // DataRecievedFrom{Cache,Network} for workers.
    // So, this inner loop calls EvaluateScript directly. This will change
    // once modules are introduced as we will have some extra work to do.
    if (!EvaluateScript(aCx, req)) {
      req->Cancel();
      mExecutionAborted = true;
      WorkerLoadContext* loadContext = req->GetWorkerLoadContext();
      mMutedErrorFlag = loadContext->mMutedErrorFlag.valueOr(true);
      mLoadedRequests.CancelRequestsAndClear();
      break;
    }
  }

  TryShutdown();
  return true;
}

nsresult WorkerScriptLoader::LoadScript(
    ThreadSafeRequestHandle* aRequestHandle) {
  AssertIsOnMainThread();

  WorkerLoadContext* loadContext = aRequestHandle->GetContext();
  ScriptLoadRequest* request = aRequestHandle->GetRequest();
  MOZ_ASSERT_IF(loadContext->IsTopLevel(), !IsDebuggerScript());

  // The URL passed to us for loading was invalid, stop loading at this point.
  if (loadContext->mLoadResult != NS_ERROR_NOT_INITIALIZED) {
    return loadContext->mLoadResult;
  }

  WorkerPrivate* parentWorker = mWorkerRef->Private()->GetParent();

  // For JavaScript debugging, the devtools server must run on the same
  // thread as the debuggee, indicating the worker uses content principal.
  // However, in Bug 863246, web content will no longer be able to load
  // resource:// URIs by default, so we need system principal to load
  // debugger scripts.
  nsIPrincipal* principal = (IsDebuggerScript())
                                ? nsContentUtils::GetSystemPrincipal()
                                : mWorkerRef->Private()->GetPrincipal();

  nsCOMPtr<nsILoadGroup> loadGroup = mWorkerRef->Private()->GetLoadGroup();
  MOZ_DIAGNOSTIC_ASSERT(principal);

  NS_ENSURE_TRUE(NS_LoadGroupMatchesPrincipal(loadGroup, principal),
                 NS_ERROR_FAILURE);

  // May be null.
  nsCOMPtr<Document> parentDoc = mWorkerRef->Private()->GetDocument();

  nsCOMPtr<nsIChannel> channel;
  if (loadContext->IsTopLevel()) {
    // May be null.
    channel = mWorkerRef->Private()->ForgetWorkerChannel();
  }

  nsCOMPtr<nsIIOService> ios(do_GetIOService());

  nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
  NS_ASSERTION(secMan, "This should never be null!");

  nsresult& rv = loadContext->mLoadResult;

  nsLoadFlags loadFlags = mWorkerRef->Private()->GetLoadFlags();

  // Get the top-level worker.
  WorkerPrivate* topWorkerPrivate = mWorkerRef->Private();
  WorkerPrivate* parent = topWorkerPrivate->GetParent();
  while (parent) {
    topWorkerPrivate = parent;
    parent = topWorkerPrivate->GetParent();
  }

  // If the top-level worker is a dedicated worker and has a window, and the
  // window has a docshell, the caching behavior of this worker should match
  // that of that docshell.
  if (topWorkerPrivate->IsDedicatedWorker()) {
    nsCOMPtr<nsPIDOMWindowInner> window = topWorkerPrivate->GetWindow();
    if (window) {
      nsCOMPtr<nsIDocShell> docShell = window->GetDocShell();
      if (docShell) {
        nsresult rv = docShell->GetDefaultLoadFlags(&loadFlags);
        NS_ENSURE_SUCCESS(rv, rv);
      }
    }
  }

  if (!channel) {
    nsCOMPtr<nsIReferrerInfo> referrerInfo;
    uint32_t secFlags;
    if (request->IsModuleRequest()) {
      // https://fetch.spec.whatwg.org/#concept-main-fetch
      // Step 8. If request’s referrer policy is the empty string, then set
      //         request’s referrer policy to request’s policy container’s
      //         referrer policy.
      ReferrerPolicy policy =
          request->ReferrerPolicy() == ReferrerPolicy::_empty
              ? mWorkerRef->Private()->GetReferrerPolicy()
              : request->ReferrerPolicy();

      referrerInfo = new ReferrerInfo(request->mReferrer, policy);

      // https://html.spec.whatwg.org/multipage/webappapis.html#default-classic-script-fetch-options
      // The default classic script fetch options are a script fetch options
      // whose ... credentials mode is "same-origin", ....
      RequestCredentials credentials =
          mWorkerRef->Private()->WorkerType() == WorkerType::Classic
              ? RequestCredentials::Same_origin
              : mWorkerRef->Private()->WorkerCredentials();

      rv = GetModuleSecFlags(loadContext->IsTopLevel(), principal,
                             mWorkerScriptType, request->mURI, credentials,
                             secFlags);
    } else {
      referrerInfo = ReferrerInfo::CreateForFetch(principal, nullptr);
      if (parentWorker && !loadContext->IsTopLevel()) {
        referrerInfo =
            static_cast<ReferrerInfo*>(referrerInfo.get())
                ->CloneWithNewPolicy(parentWorker->GetReferrerPolicy());
      }
      rv = GetClassicSecFlags(loadContext->IsTopLevel(), request->mURI,
                              principal, mWorkerScriptType, secFlags);
    }

    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    nsContentPolicyType contentPolicyType = GetContentPolicyType(request);

    rv = ChannelFromScriptURL(
        principal, parentDoc, mWorkerRef->Private(), loadGroup, ios, secMan,
        request->mURI, loadContext->mClientInfo, mController,
        loadContext->IsTopLevel(), mWorkerScriptType, contentPolicyType,
        loadFlags, secFlags, mWorkerRef->Private()->CookieJarSettings(),
        referrerInfo, getter_AddRefs(channel));
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
  }

  // Associate any originating stack with the channel.
  if (!mOriginStackJSON.IsEmpty()) {
    NotifyNetworkMonitorAlternateStack(channel, mOriginStackJSON);
  }

  // We need to know which index we're on in OnStreamComplete so we know
  // where to put the result.
  RefPtr<NetworkLoadHandler> listener =
      new NetworkLoadHandler(this, aRequestHandle);

  RefPtr<ScriptResponseHeaderProcessor> headerProcessor = nullptr;

  // For each debugger script, a non-debugger script load of the same script
  // should have occured prior that processed the headers.
  if (!IsDebuggerScript()) {
    headerProcessor = MakeRefPtr<ScriptResponseHeaderProcessor>(
        mWorkerRef->Private(),
        loadContext->IsTopLevel() && !IsDynamicImport(request),
        GetContentPolicyType(request) ==
            nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS);
  }

  nsCOMPtr<nsIStreamLoader> loader;
  rv = NS_NewStreamLoader(getter_AddRefs(loader), listener, headerProcessor);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  if (loadContext->IsTopLevel()) {
    MOZ_DIAGNOSTIC_ASSERT(loadContext->mClientInfo.isSome());

    // In order to get the correct foreign partitioned prinicpal, we need to
    // set the `IsThirdPartyContextToTopWindow` to the channel's loadInfo.
    // This flag reflects the fact that if the worker is created under a
    // third-party context.
    nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
    loadInfo->SetIsThirdPartyContextToTopWindow(
        mWorkerRef->Private()->IsThirdPartyContextToTopWindow());

    Maybe<ClientInfo> clientInfo;
    clientInfo.emplace(loadContext->mClientInfo.ref());
    rv = AddClientChannelHelper(channel, std::move(clientInfo),
                                Maybe<ClientInfo>(),
                                mWorkerRef->Private()->HybridEventTarget());
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
  }

  if (StaticPrefs::browser_tabs_remote_useCrossOriginEmbedderPolicy()) {
    nsILoadInfo::CrossOriginEmbedderPolicy respectedCOEP =
        mWorkerRef->Private()->GetEmbedderPolicy();
    if (mWorkerRef->Private()->IsDedicatedWorker() &&
        respectedCOEP == nsILoadInfo::EMBEDDER_POLICY_NULL) {
      respectedCOEP = mWorkerRef->Private()->GetOwnerEmbedderPolicy();
    }

    nsCOMPtr<nsILoadInfo> channelLoadInfo = channel->LoadInfo();
    channelLoadInfo->SetLoadingEmbedderPolicy(respectedCOEP);
  }

  if (loadContext->mCacheStatus != WorkerLoadContext::ToBeCached) {
    rv = channel->AsyncOpen(loader);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
  } else {
    nsCOMPtr<nsIOutputStream> writer;

    // In case we return early.
    loadContext->mCacheStatus = WorkerLoadContext::Cancel;

    NS_NewPipe(getter_AddRefs(loadContext->mCacheReadStream),
               getter_AddRefs(writer), 0,
               UINT32_MAX,    // unlimited size to avoid writer WOULD_BLOCK case
               true, false);  // non-blocking reader, blocking writer

    nsCOMPtr<nsIStreamListenerTee> tee =
        do_CreateInstance(NS_STREAMLISTENERTEE_CONTRACTID);
    rv = tee->Init(loader, writer, listener);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    nsresult rv = channel->AsyncOpen(tee);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
  }

  loadContext->mChannel.swap(channel);

  return NS_OK;
}

nsresult WorkerScriptLoader::FillCompileOptionsForRequest(
    JSContext* cx, ScriptLoadRequest* aRequest, JS::CompileOptions* aOptions,
    JS::MutableHandle<JSScript*> aIntroductionScript) {
  // The full URL shouldn't be exposed to the debugger. See Bug 1634872
  aOptions->setFileAndLine(aRequest->mURL.get(), 1);
  aOptions->setNoScriptRval(true);

  aOptions->setMutedErrors(
      aRequest->GetWorkerLoadContext()->mMutedErrorFlag.value());

  if (aRequest->mSourceMapURL) {
    aOptions->setSourceMapURL(aRequest->mSourceMapURL->get());
  }

  return NS_OK;
}

bool WorkerScriptLoader::EvaluateScript(JSContext* aCx,
                                        ScriptLoadRequest* aRequest) {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  MOZ_ASSERT(!IsDynamicImport(aRequest));

  WorkerLoadContext* loadContext = aRequest->GetWorkerLoadContext();

  NS_ASSERTION(!loadContext->mChannel, "Should no longer have a channel!");
  NS_ASSERTION(aRequest->IsFinished(), "Should be scheduled!");

  MOZ_ASSERT(!mRv.Failed(), "Who failed it and why?");
  mRv.MightThrowJSException();
  if (NS_FAILED(loadContext->mLoadResult)) {
    ReportErrorToConsole(aRequest, loadContext->mLoadResult);
    return false;
  }

  // If this is a top level script that succeeded, then mark the
  // Client execution ready and possible controlled by a service worker.
  if (loadContext->IsTopLevel()) {
    if (mController.isSome()) {
      MOZ_ASSERT(mWorkerScriptType == WorkerScript,
                 "Debugger clients can't be controlled.");
      mWorkerRef->Private()->GlobalScope()->Control(mController.ref());
    }
    mWorkerRef->Private()->ExecutionReady();
  }

  if (aRequest->IsModuleRequest()) {
    // Only the top level module of the module graph will be executed from here,
    // the rest will be executed from SpiderMonkey as part of the execution of
    // the module graph.
    MOZ_ASSERT(aRequest->IsTopLevel());
    ModuleLoadRequest* request = aRequest->AsModuleRequest();
    if (!request->mModuleScript) {
      return false;
    }

    // https://html.spec.whatwg.org/#run-a-worker
    // if script's error to rethrow is non-null, then:
    //    Queue a global task on the DOM manipulation task source given worker's
    //    relevant global object to fire an event named error at worker.
    //
    // The event will be dispatched in CompileScriptRunnable.
    if (request->mModuleScript->HasParseError()) {
      // Here we assign an error code that is not a JS Exception, so
      // CompileRunnable can dispatch the event.
      mRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
      return false;
    }

    // Implements To fetch a worklet/module worker script graph
    // Step 5. Fetch the descendants of and link result.
    if (!request->InstantiateModuleGraph()) {
      return false;
    }

    if (request->mModuleScript->HasErrorToRethrow()) {
      // See the comments when we check HasParseError() above.
      mRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
      return false;
    }

    nsresult rv = request->EvaluateModule();
    return NS_SUCCEEDED(rv);
  }

  JS::CompileOptions options(aCx);
  // The introduction script is used by the DOM script loader as a way
  // to fill the Debugger Metadata for the JS Execution context. We don't use
  // the JS Execution context as we are not making use of async compilation
  // (delegation to another worker to produce bytecode or compile a string to a
  // JSScript), so it is not used in this context.
  JS::Rooted<JSScript*> unusedIntroductionScript(aCx);
  nsresult rv = FillCompileOptionsForRequest(aCx, aRequest, &options,
                                             &unusedIntroductionScript);

  MOZ_ASSERT(NS_SUCCEEDED(rv), "Filling compile options should not fail");

  // Our ErrorResult still shouldn't be a failure.
  MOZ_ASSERT(!mRv.Failed(), "Who failed it and why?");

  // Get the source text.
  ScriptLoadRequest::MaybeSourceText maybeSource;
  rv = aRequest->GetScriptSource(aCx, &maybeSource,
                                 aRequest->mLoadContext.get());
  if (NS_FAILED(rv)) {
    mRv.StealExceptionFromJSContext(aCx);
    return false;
  }

  RefPtr<JS::loader::ClassicScript> classicScript = nullptr;
  if (StaticPrefs::dom_workers_modules_enabled() &&
      !mWorkerRef->Private()->IsServiceWorker()) {
    // We need a LoadedScript to be associated with the JSScript in order to
    // correctly resolve the referencing private for dynamic imports. In turn
    // this allows us to correctly resolve the BaseURL.
    //
    // Dynamic import is disallowed on service workers.  Additionally, causes
    // crashes because the life cycle isn't completed for service workers.  To
    // keep things simple, we don't create a classic script for ServiceWorkers.
    // If this changes then we will need to ensure that the reference that is
    // held is released appropriately.
    nsCOMPtr<nsIURI> requestBaseURI;
    if (loadContext->mMutedErrorFlag.valueOr(false)) {
      NS_NewURI(getter_AddRefs(requestBaseURI), "about:blank"_ns);
    } else {
      requestBaseURI = aRequest->mBaseURL;
    }
    MOZ_ASSERT(aRequest->mLoadedScript->IsClassicScript());
    MOZ_ASSERT(aRequest->mLoadedScript->GetFetchOptions() ==
               aRequest->mFetchOptions);
    aRequest->mLoadedScript->SetBaseURL(requestBaseURI);
    classicScript = aRequest->mLoadedScript->AsClassicScript();
  }

  JS::Rooted<JSScript*> script(aCx);
  script = aRequest->IsUTF8Text()
               ? JS::Compile(aCx, options,
                             maybeSource.ref<JS::SourceText<Utf8Unit>>())
               : JS::Compile(aCx, options,
                             maybeSource.ref<JS::SourceText<char16_t>>());
  if (!script) {
    if (loadContext->IsTopLevel()) {
      // This is a top-level worker script,
      //
      // https://html.spec.whatwg.org/#run-a-worker
      // If script is null or if script's error to rethrow is non-null, then:
      //   Queue a global task on the DOM manipulation task source given
      //   worker's relevant global object to fire an event named error at
      //   worker.
      JS_ClearPendingException(aCx);
      mRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
    } else {
      // This is a script which is loaded by importScripts().
      //
      // https://html.spec.whatwg.org/#import-scripts-into-worker-global-scope
      // For each url in the resulting URL records:
      //   Fetch a classic worker-imported script given url and settings object,
      //   passing along performFetch if provided. If this succeeds, let script
      //   be the result. Otherwise, rethrow the exception.
      mRv.StealExceptionFromJSContext(aCx);
    }

    return false;
  }

  bool successfullyEvaluated = EvaluateSourceBuffer(aCx, script, classicScript);
  if (aRequest->IsCanceled()) {
    return false;
  }
  if (!successfullyEvaluated) {
    mRv.StealExceptionFromJSContext(aCx);
    return false;
  }
  // steal the loadContext so that the cycle is broken and cycle collector can
  // collect the scriptLoadRequest.
  return true;
}

void WorkerScriptLoader::TryShutdown() {
  {
    MutexAutoLock lock(CleanUpLock());
    if (CleanedUp()) {
      return;
    }
  }

  if (AllScriptsExecuted() && AllModuleRequestsLoaded()) {
    ShutdownScriptLoader(!mExecutionAborted, mMutedErrorFlag);
  }
}

void WorkerScriptLoader::ShutdownScriptLoader(bool aResult, bool aMutedError) {
  MOZ_ASSERT(AllScriptsExecuted());
  MOZ_ASSERT(AllModuleRequestsLoaded());
  mWorkerRef->Private()->AssertIsOnWorkerThread();

  if (!aResult) {
    // At this point there are two possibilities:
    //
    // 1) mRv.Failed().  In that case we just want to leave it
    //    as-is, except if it has a JS exception and we need to mute JS
    //    exceptions.  In that case, we log the exception without firing any
    //    events and then replace it on the ErrorResult with a NetworkError,
    //    per spec.
    //
    // 2) mRv succeeded.  As far as I can tell, this can only
    //    happen when loading the main worker script and
    //    GetOrCreateGlobalScope() fails or if ScriptExecutorRunnable::Cancel
    //    got called.  Does it matter what we throw in this case?  I'm not
    //    sure...
    if (mRv.Failed()) {
      if (aMutedError && mRv.IsJSException()) {
        LogExceptionToConsole(mWorkerRef->Private()->GetJSContext(),
                              mWorkerRef->Private());
        mRv.Throw(NS_ERROR_DOM_NETWORK_ERR);
      }
    } else {
      mRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
    }
  }

  // Lock, shutdown, and cleanup state. After this the Loader is closed.
  {
    MutexAutoLock lock(CleanUpLock());

    if (CleanedUp()) {
      return;
    }

    mWorkerRef->Private()->AssertIsOnWorkerThread();
    // Module loader doesn't use sync loop for dynamic import
    if (mSyncLoopTarget) {
      mWorkerRef->Private()->MaybeStopSyncLoop(
          mSyncLoopTarget, aResult ? NS_OK : NS_ERROR_FAILURE);
      mSyncLoopTarget = nullptr;
    }

    // Signal cleanup
    mCleanedUp = true;

    // Allow worker shutdown.
    mWorkerRef = nullptr;
  }
}

void WorkerScriptLoader::ReportErrorToConsole(ScriptLoadRequest* aRequest,
                                              nsresult aResult) const {
  nsAutoString url = NS_ConvertUTF8toUTF16(aRequest->mURL);
  workerinternals::ReportLoadError(mRv, aResult, url);
}

void WorkerScriptLoader::LogExceptionToConsole(JSContext* aCx,
                                               WorkerPrivate* aWorkerPrivate) {
  aWorkerPrivate->AssertIsOnWorkerThread();

  MOZ_ASSERT(mRv.IsJSException());

  JS::Rooted<JS::Value> exn(aCx);
  if (!ToJSValue(aCx, std::move(mRv), &exn)) {
    return;
  }

  // Now the exception state should all be in exn.
  MOZ_ASSERT(!JS_IsExceptionPending(aCx));
  MOZ_ASSERT(!mRv.Failed());

  JS::ExceptionStack exnStack(aCx, exn, nullptr);
  JS::ErrorReportBuilder report(aCx);
  if (!report.init(aCx, exnStack, JS::ErrorReportBuilder::WithSideEffects)) {
    JS_ClearPendingException(aCx);
    return;
  }

  RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
  xpcReport->Init(report.report(), report.toStringResult().c_str(),
                  aWorkerPrivate->IsChromeWorker(), aWorkerPrivate->WindowID());

  RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport);
  NS_DispatchToMainThread(r);
}

bool WorkerScriptLoader::AllModuleRequestsLoaded() const {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  return mLoadingModuleRequestCount == 0;
}

void WorkerScriptLoader::IncreaseLoadingModuleRequestCount() {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  ++mLoadingModuleRequestCount;
}

void WorkerScriptLoader::DecreaseLoadingModuleRequestCount() {
  mWorkerRef->Private()->AssertIsOnWorkerThread();
  --mLoadingModuleRequestCount;
}

NS_IMPL_ISUPPORTS(ScriptLoaderRunnable, nsIRunnable, nsINamed)

NS_IMPL_ISUPPORTS(WorkerScriptLoader, nsINamed)

ScriptLoaderRunnable::ScriptLoaderRunnable(
    WorkerScriptLoader* aScriptLoader,
    nsTArray<RefPtr<ThreadSafeRequestHandle>> aLoadingRequests)
    : mScriptLoader(aScriptLoader),
      mWorkerRef(aScriptLoader->mWorkerRef),
      mLoadingRequests(std::move(aLoadingRequests)),
      mCancelMainThread(Nothing()) {
  MOZ_ASSERT(aScriptLoader);
}

nsresult ScriptLoaderRunnable::Run() {
  AssertIsOnMainThread();

  // Convert the origin stack to JSON (which must be done on the main
  // thread) explicitly, so that we can use the stack to notify the net
  // monitor about every script we load. We do this, rather than pass
  // the stack directly to the netmonitor, in order to be able to use this
  // for all subsequent scripts.
  if (mScriptLoader->mOriginStack &&
      mScriptLoader->mOriginStackJSON.IsEmpty()) {
    ConvertSerializedStackToJSON(std::move(mScriptLoader->mOriginStack),
                                 mScriptLoader->mOriginStackJSON);
  }

  if (!mWorkerRef->Private()->IsServiceWorker() ||
      mScriptLoader->IsDebuggerScript()) {
    for (ThreadSafeRequestHandle* handle : mLoadingRequests) {
      handle->mRunnable = this;
    }

    for (ThreadSafeRequestHandle* handle : mLoadingRequests) {
      nsresult rv = mScriptLoader->LoadScript(handle);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        LoadingFinished(handle, rv);
        CancelMainThread(rv);
        return rv;
      }
    }

    return NS_OK;
  }

  MOZ_ASSERT(!mCacheCreator);
  mCacheCreator = new CacheCreator(mWorkerRef->Private());

  for (ThreadSafeRequestHandle* handle : mLoadingRequests) {
    handle->mRunnable = this;
    WorkerLoadContext* loadContext = handle->GetContext();
    mCacheCreator->AddLoader(MakeNotNull<RefPtr<CacheLoadHandler>>(
        mWorkerRef, handle, loadContext->IsTopLevel(),
        loadContext->mOnlyExistingCachedResourcesAllowed, mScriptLoader));
  }

  // The worker may have a null principal on first load, but in that case its
  // parent definitely will have one.
  nsIPrincipal* principal = mWorkerRef->Private()->GetPrincipal();
  if (!principal) {
    WorkerPrivate* parentWorker = mWorkerRef->Private()->GetParent();
    MOZ_ASSERT(parentWorker, "Must have a parent!");
    principal = parentWorker->GetPrincipal();
  }

  nsresult rv = mCacheCreator->Load(principal);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    CancelMainThread(rv);
    return rv;
  }

  return NS_OK;
}

nsresult ScriptLoaderRunnable::OnStreamComplete(
    ThreadSafeRequestHandle* aRequestHandle, nsresult aStatus) {
  AssertIsOnMainThread();

  LoadingFinished(aRequestHandle, aStatus);
  return NS_OK;
}

void ScriptLoaderRunnable::LoadingFinished(
    ThreadSafeRequestHandle* aRequestHandle, nsresult aRv) {
  AssertIsOnMainThread();

  WorkerLoadContext* loadContext = aRequestHandle->GetContext();

  loadContext->mLoadResult = aRv;
  MOZ_ASSERT(!loadContext->mLoadingFinished);
  loadContext->mLoadingFinished = true;

  if (loadContext->IsTopLevel() && NS_SUCCEEDED(aRv)) {
    MOZ_DIAGNOSTIC_ASSERT(
        mWorkerRef->Private()->PrincipalURIMatchesScriptURL());
  }

  MaybeExecuteFinishedScripts(aRequestHandle);
}

void ScriptLoaderRunnable::MaybeExecuteFinishedScripts(
    ThreadSafeRequestHandle* aRequestHandle) {
  AssertIsOnMainThread();

  // We execute the last step if we don't have a pending operation with the
  // cache and the loading is completed.
  WorkerLoadContext* loadContext = aRequestHandle->GetContext();
  if (!loadContext->IsAwaitingPromise()) {
    if (aRequestHandle->GetContext()->IsTopLevel()) {
      mWorkerRef->Private()->WorkerScriptLoaded();
    }
    DispatchProcessPendingRequests();
  }
}

void ScriptLoaderRunnable::CancelMainThreadWithBindingAborted() {
  AssertIsOnMainThread();
  CancelMainThread(NS_BINDING_ABORTED);
}

void ScriptLoaderRunnable::CancelMainThread(nsresult aCancelResult) {
  AssertIsOnMainThread();
  if (IsCancelled()) {
    return;
  }

  {
    MutexAutoLock lock(mScriptLoader->CleanUpLock());

    // Check if we have already cancelled, or if the worker has been killed
    // before we cancel.
    if (mScriptLoader->CleanedUp()) {
      return;
    }

    mCancelMainThread = Some(aCancelResult);

    for (ThreadSafeRequestHandle* handle : mLoadingRequests) {
      if (handle->IsEmpty()) {
        continue;
      }

      bool callLoadingFinished = true;

      WorkerLoadContext* loadContext = handle->GetContext();
      if (!loadContext) {
        continue;
      }

      if (loadContext->IsAwaitingPromise()) {
        MOZ_ASSERT(mWorkerRef->Private()->IsServiceWorker());
        loadContext->mCachePromise->MaybeReject(NS_BINDING_ABORTED);
        loadContext->mCachePromise = nullptr;
        callLoadingFinished = false;
      }
      if (loadContext->mChannel) {
        if (NS_SUCCEEDED(loadContext->mChannel->Cancel(aCancelResult))) {
          callLoadingFinished = false;
        } else {
          NS_WARNING("Failed to cancel channel!");
        }
      }
      if (callLoadingFinished && !loadContext->mLoadingFinished) {
        LoadingFinished(handle, aCancelResult);
      }
    }
    DispatchProcessPendingRequests();
  }
}

void ScriptLoaderRunnable::DispatchProcessPendingRequests() {
  AssertIsOnMainThread();

  const auto begin = mLoadingRequests.begin();
  const auto end = mLoadingRequests.end();
  using Iterator = decltype(begin);
  const auto maybeRangeToExecute =
      [begin, end]() -> Maybe<std::pair<Iterator, Iterator>> {
    // firstItToExecute is the first loadInfo where mExecutionScheduled is
    // unset.
    auto firstItToExecute = std::find_if(
        begin, end, [](const RefPtr<ThreadSafeRequestHandle>& requestHandle) {
          return !requestHandle->mExecutionScheduled;
        });

    if (firstItToExecute == end) {
      return Nothing();
    }

    // firstItUnexecutable is the first loadInfo that is not yet finished.
    // Update mExecutionScheduled on the ones we're about to schedule for
    // execution.
    const auto firstItUnexecutable =
        std::find_if(firstItToExecute, end,
                     [](RefPtr<ThreadSafeRequestHandle>& requestHandle) {
                       MOZ_ASSERT(!requestHandle->IsEmpty());
                       if (!requestHandle->Finished()) {
                         return true;
                       }

                       // We can execute this one.
                       requestHandle->mExecutionScheduled = true;

                       return false;
                     });

    return firstItUnexecutable == firstItToExecute
               ? Nothing()
               : Some(std::pair(firstItToExecute, firstItUnexecutable));
  }();

  // If there are no unexecutable load infos, we can unuse things before the
  // execution of the scripts and the stopping of the sync loop.
  if (maybeRangeToExecute) {
    if (maybeRangeToExecute->second == end) {
      mCacheCreator = nullptr;
    }

    RefPtr<ScriptExecutorRunnable> runnable = new ScriptExecutorRunnable(
        mScriptLoader, mWorkerRef->Private(), mScriptLoader->mSyncLoopTarget,
        Span<RefPtr<ThreadSafeRequestHandle>>{maybeRangeToExecute->first,
                                              maybeRangeToExecute->second});

    if (!runnable->Dispatch() && mScriptLoader->mSyncLoopTarget) {
      MOZ_ASSERT(false, "This should never fail!");
    }
  }
}

ScriptExecutorRunnable::ScriptExecutorRunnable(
    WorkerScriptLoader* aScriptLoader, WorkerPrivate* aWorkerPrivate,
    nsISerialEventTarget* aSyncLoopTarget,
    Span<RefPtr<ThreadSafeRequestHandle>> aLoadedRequests)
    : MainThreadWorkerSyncRunnable(aWorkerPrivate, aSyncLoopTarget,
                                   "ScriptExecutorRunnable"),
      mScriptLoader(aScriptLoader),
      mLoadedRequests(aLoadedRequests) {}

bool ScriptExecutorRunnable::IsDebuggerRunnable() const {
  // ScriptExecutorRunnable is used to execute both worker and debugger scripts.
  // In the latter case, the runnable needs to be dispatched to the debugger
  // queue.
  return mScriptLoader->IsDebuggerScript();
}

bool ScriptExecutorRunnable::PreRun(WorkerPrivate* aWorkerPrivate) {
  aWorkerPrivate->AssertIsOnWorkerThread();

  // We must be on the same worker as we started on.
  MOZ_ASSERT(
      mScriptLoader->mSyncLoopTarget == mSyncLoopTarget,
      "Unexpected SyncLoopTarget. Check if the sync loop was closed early");

  {
    // There is a possibility that we cleaned up while this task was waiting to
    // run. If this has happened, return and exit.
    MutexAutoLock lock(mScriptLoader->CleanUpLock());
    if (mScriptLoader->CleanedUp()) {
      return true;
    }

    const auto& requestHandle = mLoadedRequests[0];
    // Check if the request is still valid.
    if (requestHandle->IsEmpty() ||
        !requestHandle->GetContext()->IsTopLevel()) {
      return true;
    }
  }

  return mScriptLoader->StoreCSP();
}

bool ScriptExecutorRunnable::ProcessModuleScript(
    JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
  // We should only ever have one script when processing modules
  MOZ_ASSERT(mLoadedRequests.Length() == 1);
  RefPtr<ScriptLoadRequest> request;
  {
    // There is a possibility that we cleaned up while this task was waiting to
    // run. If this has happened, return and exit.
    MutexAutoLock lock(mScriptLoader->CleanUpLock());
    if (mScriptLoader->CleanedUp()) {
      return true;
    }

    MOZ_ASSERT(mLoadedRequests.Length() == 1);
    const auto& requestHandle = mLoadedRequests[0];
    // The request must be valid.
    MOZ_ASSERT(!requestHandle->IsEmpty());

    // Release the request to the worker. From this point on, the Request Handle
    // is empty.
    request = requestHandle->ReleaseRequest();

    // release lock. We will need it later if we cleanup.
  }

  MOZ_ASSERT(request->IsModuleRequest());

  WorkerLoadContext* loadContext = request->GetWorkerLoadContext();
  ModuleLoadRequest* moduleRequest = request->AsModuleRequest();

  // DecreaseLoadingModuleRequestCount must be called before OnFetchComplete.
  // OnFetchComplete will call ProcessPendingRequests, and in
  // ProcessPendingRequests it will try to shutdown if
  // AllModuleRequestsLoaded() returns true.
  mScriptLoader->DecreaseLoadingModuleRequestCount();
  moduleRequest->OnFetchComplete(loadContext->mLoadResult);

  if (NS_FAILED(loadContext->mLoadResult)) {
    if (moduleRequest->IsDynamicImport()) {
      if (request->isInList()) {
        moduleRequest->CancelDynamicImport(loadContext->mLoadResult);
        mScriptLoader->TryShutdown();
      }
    } else if (!moduleRequest->IsTopLevel()) {
      moduleRequest->Cancel();
      mScriptLoader->TryShutdown();
    } else {
      moduleRequest->LoadFailed();
    }
  }
  return true;
}

bool ScriptExecutorRunnable::ProcessClassicScripts(
    JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
  // There is a possibility that we cleaned up while this task was waiting to
  // run. If this has happened, return and exit.
  {
    MutexAutoLock lock(mScriptLoader->CleanUpLock());
    if (mScriptLoader->CleanedUp()) {
      return true;
    }

    for (const auto& requestHandle : mLoadedRequests) {
      // The request must be valid.
      MOZ_ASSERT(!requestHandle->IsEmpty());

      // Release the request to the worker. From this point on, the Request
      // Handle is empty.
      RefPtr<ScriptLoadRequest> request = requestHandle->ReleaseRequest();
      mScriptLoader->MaybeMoveToLoadedList(request);
    }
  }
  return mScriptLoader->ProcessPendingRequests(aCx);
}

bool ScriptExecutorRunnable::WorkerRun(JSContext* aCx,
                                       WorkerPrivate* aWorkerPrivate) {
  aWorkerPrivate->AssertIsOnWorkerThread();

  // We must be on the same worker as we started on.
  MOZ_ASSERT(
      mScriptLoader->mSyncLoopTarget == mSyncLoopTarget,
      "Unexpected SyncLoopTarget. Check if the sync loop was closed early");

  if (mLoadedRequests.begin()->get()->GetRequest()->IsModuleRequest()) {
    return ProcessModuleScript(aCx, aWorkerPrivate);
  }

  return ProcessClassicScripts(aCx, aWorkerPrivate);
}

nsresult ScriptExecutorRunnable::Cancel() {
  if (mScriptLoader->AllScriptsExecuted() &&
      mScriptLoader->AllModuleRequestsLoaded()) {
    mScriptLoader->ShutdownScriptLoader(false, false);
  }
  return NS_OK;
}

} /* namespace loader */

nsresult ChannelFromScriptURLMainThread(
    nsIPrincipal* aPrincipal, Document* aParentDoc, nsILoadGroup* aLoadGroup,
    nsIURI* aScriptURL, const WorkerType& aWorkerType,
    const RequestCredentials& aCredentials,
    const Maybe<ClientInfo>& aClientInfo,
    nsContentPolicyType aMainScriptContentPolicyType,
    nsICookieJarSettings* aCookieJarSettings, nsIReferrerInfo* aReferrerInfo,
    nsIChannel** aChannel) {
  AssertIsOnMainThread();

  nsCOMPtr<nsIIOService> ios(do_GetIOService());

  nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
  NS_ASSERTION(secMan, "This should never be null!");

  uint32_t secFlags;
  nsresult rv;
  if (aWorkerType == WorkerType::Module) {
    rv = GetModuleSecFlags(true, aPrincipal, WorkerScript, aScriptURL,
                           aCredentials, secFlags);
  } else {
    rv = GetClassicSecFlags(true, aScriptURL, aPrincipal, WorkerScript,
                            secFlags);
  }
  if (NS_FAILED(rv)) {
    return rv;
  }

  return ChannelFromScriptURL(
      aPrincipal, aParentDoc, nullptr, aLoadGroup, ios, secMan, aScriptURL,
      aClientInfo, Maybe<ServiceWorkerDescriptor>(), true, WorkerScript,
      aMainScriptContentPolicyType, nsIRequest::LOAD_NORMAL, secFlags,
      aCookieJarSettings, aReferrerInfo, aChannel);
}

nsresult ChannelFromScriptURLWorkerThread(
    JSContext* aCx, WorkerPrivate* aParent, const nsAString& aScriptURL,
    const WorkerType& aWorkerType, const RequestCredentials& aCredentials,
    WorkerLoadInfo& aLoadInfo) {
  aParent->AssertIsOnWorkerThread();

  RefPtr<ChannelGetterRunnable> getter = new ChannelGetterRunnable(
      aParent, aScriptURL, aWorkerType, aCredentials, aLoadInfo);

  ErrorResult rv;
  getter->Dispatch(Canceling, rv);
  if (rv.Failed()) {
    NS_ERROR("Failed to dispatch!");
    return rv.StealNSResult();
  }

  return getter->GetResult();
}

void ReportLoadError(ErrorResult& aRv, nsresult aLoadResult,
                     const nsAString& aScriptURL) {
  MOZ_ASSERT(!aRv.Failed());

  nsPrintfCString err("Failed to load worker script at \"%s\"",
                      NS_ConvertUTF16toUTF8(aScriptURL).get());

  switch (aLoadResult) {
    case NS_ERROR_FILE_NOT_FOUND:
    case NS_ERROR_NOT_AVAILABLE:
    case NS_ERROR_CORRUPTED_CONTENT:
      aRv.Throw(NS_ERROR_DOM_NETWORK_ERR);
      break;

    case NS_ERROR_MALFORMED_URI:
    case NS_ERROR_DOM_SYNTAX_ERR:
      aRv.ThrowSyntaxError(err);
      break;

    case NS_BINDING_ABORTED:
      // Note: we used to pretend like we didn't set an exception for
      // NS_BINDING_ABORTED, but then ShutdownScriptLoader did it anyway.  The
      // other callsite, in WorkerPrivate::Constructor, never passed in
      // NS_BINDING_ABORTED.  So just throw it directly here.  Consumers will
      // deal as needed.  But note that we do NOT want to use one of the
      // Throw*Error() methods on ErrorResult for this case, because that will
      // make it impossible for consumers to realize that our error was
      // NS_BINDING_ABORTED.
      aRv.Throw(aLoadResult);
      return;

    case NS_ERROR_DOM_BAD_URI:
      // This is actually a security error.
    case NS_ERROR_DOM_SECURITY_ERR:
      aRv.ThrowSecurityError(err);
      break;

    default:
      // For lack of anything better, go ahead and throw a NetworkError here.
      // We don't want to throw a JS exception, because for toplevel script
      // loads that would get squelched.
      aRv.ThrowNetworkError(nsPrintfCString(
          "Failed to load worker script at %s (nsresult = 0x%" PRIx32 ")",
          NS_ConvertUTF16toUTF8(aScriptURL).get(),
          static_cast<uint32_t>(aLoadResult)));
      return;
  }
}

void LoadMainScript(WorkerPrivate* aWorkerPrivate,
                    UniquePtr<SerializedStackHolder> aOriginStack,
                    const nsAString& aScriptURL,
                    WorkerScriptType aWorkerScriptType, ErrorResult& aRv,
                    const mozilla::Encoding* aDocumentEncoding) {
  nsTArray<nsString> scriptURLs;

  scriptURLs.AppendElement(aScriptURL);

  LoadAllScripts(aWorkerPrivate, std::move(aOriginStack), scriptURLs, true,
                 aWorkerScriptType, aRv, aDocumentEncoding);
}

void Load(WorkerPrivate* aWorkerPrivate,
          UniquePtr<SerializedStackHolder> aOriginStack,
          const nsTArray<nsString>& aScriptURLs,
          WorkerScriptType aWorkerScriptType, ErrorResult& aRv) {
  const uint32_t urlCount = aScriptURLs.Length();

  if (!urlCount) {
    return;
  }

  if (urlCount > MAX_CONCURRENT_SCRIPTS) {
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    return;
  }

  LoadAllScripts(aWorkerPrivate, std::move(aOriginStack), aScriptURLs, false,
                 aWorkerScriptType, aRv);
}

}  // namespace mozilla::dom::workerinternals