summaryrefslogtreecommitdiffstats
path: root/dom/plugins/ipc/PluginModuleChild.cpp
blob: 81eb8467d468de25bf946171a9a6acde178288dd (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
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=4 et : */
/* 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 "mozilla/plugins/PluginModuleChild.h"

/* This must occur *after* plugins/PluginModuleChild.h to avoid typedefs
 * conflicts. */
#include "mozilla/ArrayUtils.h"

#include "mozilla/ipc/MessageChannel.h"

#ifdef MOZ_WIDGET_GTK
#  include <gtk/gtk.h>
#  include <gdk/gdkx.h>
#endif

#include "nsIFile.h"

#include "pratom.h"
#include "nsDebug.h"
#include "nsCOMPtr.h"
#include "nsPluginsDir.h"
#include "nsXULAppAPI.h"

#ifdef MOZ_X11
#  include "nsX11ErrorHandler.h"
#  include "mozilla/X11Util.h"
#endif

#include "mozilla/ipc/CrashReporterClient.h"
#include "mozilla/ipc/Endpoint.h"
#include "mozilla/ipc/ProcessChild.h"
#include "mozilla/plugins/PluginInstanceChild.h"
#include "mozilla/plugins/StreamNotifyChild.h"
#include "mozilla/plugins/BrowserStreamChild.h"
#include "mozilla/Sprintf.h"
#include "mozilla/Unused.h"

#include "nsNPAPIPlugin.h"
#include "FunctionHook.h"
#include "FunctionBrokerChild.h"

#ifdef XP_WIN
#  include "mozilla/widget/AudioSession.h"
#  include <knownfolders.h>
#endif

#ifdef MOZ_WIDGET_COCOA
#  include "PluginInterposeOSX.h"
#  include "PluginUtilsOSX.h"
#endif

#ifdef MOZ_GECKO_PROFILER
#  include "ChildProfilerController.h"
#endif

#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
#  include "mozilla/Sandbox.h"
#endif

using namespace mozilla;
using namespace mozilla::ipc;
using namespace mozilla::plugins;
using namespace mozilla::widget;

#if defined(XP_WIN)
const wchar_t* kFlashFullscreenClass = L"ShockwaveFlashFullScreen";
#  if defined(MOZ_SANDBOX)
std::wstring sRoamingPath;
#  endif
#endif

namespace {
// see PluginModuleChild::GetChrome()
PluginModuleChild* gChromeInstance = nullptr;
}  // namespace

#ifdef XP_WIN
// Used with fix for flash fullscreen window loosing focus.
static bool gDelayFlashFocusReplyUntilEval = false;
#endif

/* static */
bool PluginModuleChild::CreateForContentProcess(
    Endpoint<PPluginModuleChild>&& aEndpoint) {
  auto* child = new PluginModuleChild(false);
  return child->InitForContent(std::move(aEndpoint));
}

PluginModuleChild::PluginModuleChild(bool aIsChrome)
    : mLibrary(0),
      mPluginFilename(""),
      mQuirks(QUIRKS_NOT_INITIALIZED),
      mIsChrome(aIsChrome),
      mHasShutdown(false),
      mShutdownFunc(0),
      mInitializeFunc(0)
#if defined(OS_WIN) || defined(OS_MACOSX)
      ,
      mGetEntryPointsFunc(0)
#elif defined(MOZ_WIDGET_GTK)
      ,
      mNestedLoopTimerId(0)
#endif
#ifdef OS_WIN
      ,
      mNestedEventHook(nullptr),
      mGlobalCallWndProcHook(nullptr),
      mAsyncRenderSupport(false)
#endif
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
      ,
      mFlashSandboxLevel(0),
      mEnableFlashSandboxLogging(false)
#endif
{
  memset(&mFunctions, 0, sizeof(mFunctions));
  if (mIsChrome) {
    MOZ_ASSERT(!gChromeInstance);
    gChromeInstance = this;
  }

#ifdef XP_MACOSX
  if (aIsChrome) {
    mac_plugin_interposing::child::SetUpCocoaInterposing();
  }
#endif
}

PluginModuleChild::~PluginModuleChild() {
  if (mIsChrome) {
    MOZ_ASSERT(gChromeInstance == this);

    // We don't unload the plugin library in case it uses atexit handlers or
    // other similar hooks.

    DeinitGraphics();
    PluginScriptableObjectChild::ClearIdentifiers();

    gChromeInstance = nullptr;
  }
}

// static
PluginModuleChild* PluginModuleChild::GetChrome() {
  // A special PluginModuleChild instance that talks to the chrome process
  // during startup and shutdown. Synchronous messages to or from this actor
  // should be avoided because they may lead to hangs.
  MOZ_ASSERT(gChromeInstance);
  return gChromeInstance;
}

void PluginModuleChild::CommonInit() {
  PLUGIN_LOG_DEBUG_METHOD;

  // Request Windows message deferral behavior on our channel. This
  // applies to the top level and all sub plugin protocols since they
  // all share the same channel.
  // Bug 1090573 - Don't do this for connections to content processes.
  GetIPCChannel()->SetChannelFlags(
      MessageChannel::REQUIRE_DEFERRED_MESSAGE_PROTECTION);

  memset((void*)&mFunctions, 0, sizeof(mFunctions));
  mFunctions.size = sizeof(mFunctions);
  mFunctions.version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
}

bool PluginModuleChild::InitForContent(
    Endpoint<PPluginModuleChild>&& aEndpoint) {
  CommonInit();

  if (!aEndpoint.Bind(this)) {
    return false;
  }

  mLibrary = GetChrome()->mLibrary;
  mFunctions = GetChrome()->mFunctions;

  return true;
}

mozilla::ipc::IPCResult PluginModuleChild::RecvInitProfiler(
    Endpoint<mozilla::PProfilerChild>&& aEndpoint) {
#ifdef MOZ_GECKO_PROFILER
  mProfilerController = ChildProfilerController::Create(std::move(aEndpoint));
#endif
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::RecvDisableFlashProtectedMode() {
  MOZ_ASSERT(mIsChrome);
#ifdef XP_WIN
  FunctionHook::HookProtectedMode();
#else
  MOZ_ASSERT(false, "Should not be called");
#endif
  return IPC_OK();
}

#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
void PluginModuleChild::EnableFlashSandbox(int aLevel,
                                           bool aShouldEnableLogging) {
  mFlashSandboxLevel = aLevel;
  mEnableFlashSandboxLogging = aShouldEnableLogging;
}
#endif

#if defined(OS_WIN) && defined(MOZ_SANDBOX)
/* static */
void PluginModuleChild::SetFlashRoamingPath(const std::wstring& aRoamingPath) {
  MOZ_ASSERT(sRoamingPath.empty());
  sRoamingPath = aRoamingPath;
}

/* static */ std::wstring PluginModuleChild::GetFlashRoamingPath() {
  return sRoamingPath;
}
#endif

bool PluginModuleChild::InitForChrome(const std::string& aPluginFilename,
                                      base::ProcessId aParentPid,
                                      MessageLoop* aIOLoop,
                                      UniquePtr<IPC::Channel> aChannel) {
  NS_ASSERTION(aChannel, "need a channel");

#if defined(OS_WIN) && defined(MOZ_SANDBOX)
  MOZ_ASSERT(!sRoamingPath.empty(),
             "Should have already called SetFlashRoamingPath");
#endif

  if (!InitGraphics()) return false;

  mPluginFilename = aPluginFilename.c_str();
  nsCOMPtr<nsIFile> localFile;
  NS_NewLocalFile(NS_ConvertUTF8toUTF16(mPluginFilename), true,
                  getter_AddRefs(localFile));

  if (!localFile) return false;

  bool exists;
  localFile->Exists(&exists);
  NS_ASSERTION(exists, "plugin file ain't there");

  nsPluginFile pluginFile(localFile);

  nsPluginInfo info = nsPluginInfo();
  if (NS_FAILED(pluginFile.GetPluginInfo(info, &mLibrary))) {
    return false;
  }

#if defined(XP_WIN)
  // XXX quirks isn't initialized yet
  mAsyncRenderSupport = info.fSupportsAsyncRender;
#endif
#if defined(MOZ_X11)
  constexpr auto flash10Head = "Shockwave Flash 10."_ns;
  if (StringBeginsWith(nsDependentCString(info.fDescription), flash10Head)) {
    AddQuirk(QUIRK_FLASH_EXPOSE_COORD_TRANSLATION);
  }
#endif
#if defined(XP_MACOSX)
  const char* namePrefix = "Plugin Content";
  char nameBuffer[80];
  SprintfLiteral(nameBuffer, "%s (%s)", namePrefix, info.fName);
  mozilla::plugins::PluginUtilsOSX::SetProcessName(nameBuffer);
#endif
  pluginFile.FreePluginInfo(info);
#if defined(MOZ_X11) || defined(XP_MACOSX)
  if (!mLibrary)
#endif
  {
    nsresult rv = pluginFile.LoadPlugin(&mLibrary);
    if (NS_FAILED(rv)) return false;
  }
  NS_ASSERTION(mLibrary, "couldn't open shared object");

  CommonInit();

  if (!Open(std::move(aChannel), aParentPid, aIOLoop)) {
    return false;
  }

  GetIPCChannel()->SetAbortOnError(true);

#if defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
  mShutdownFunc =
      (NP_PLUGINSHUTDOWN)PR_FindFunctionSymbol(mLibrary, "NP_Shutdown");

  // create the new plugin handler

  mInitializeFunc =
      (NP_PLUGINUNIXINIT)PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
  NS_ASSERTION(mInitializeFunc, "couldn't find NP_Initialize()");

#elif defined(OS_WIN) || defined(OS_MACOSX)
  mShutdownFunc =
      (NP_PLUGINSHUTDOWN)PR_FindFunctionSymbol(mLibrary, "NP_Shutdown");

  mGetEntryPointsFunc =
      (NP_GETENTRYPOINTS)PR_FindSymbol(mLibrary, "NP_GetEntryPoints");
  NS_ENSURE_TRUE(mGetEntryPointsFunc, false);

  mInitializeFunc =
      (NP_PLUGININIT)PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
  NS_ENSURE_TRUE(mInitializeFunc, false);
#else

#  error Please copy the initialization code from nsNPAPIPlugin.cpp

#endif

#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
  if (mFlashSandboxLevel > 0) {
    MacSandboxInfo flashSandboxInfo;
    flashSandboxInfo.type = MacSandboxType_Flash;
    flashSandboxInfo.pluginBinaryPath = aPluginFilename;
    flashSandboxInfo.level = mFlashSandboxLevel;
    flashSandboxInfo.shouldLog = mEnableFlashSandboxLogging;

    std::string sbError;
    if (!mozilla::StartMacSandbox(flashSandboxInfo, sbError)) {
      fprintf(stderr, "Failed to start sandbox:\n%s\n", sbError.c_str());
      return false;
    }
  }
#endif

  return true;
}

#if defined(MOZ_WIDGET_GTK)

typedef void (*GObjectDisposeFn)(GObject*);
typedef gboolean (*GtkWidgetScrollEventFn)(GtkWidget*, GdkEventScroll*);
typedef void (*GtkPlugEmbeddedFn)(GtkPlug*);

static GObjectDisposeFn real_gtk_plug_dispose;
static GtkPlugEmbeddedFn real_gtk_plug_embedded;

static void undo_bogus_unref(gpointer data, GObject* object,
                             gboolean is_last_ref) {
  if (!is_last_ref)  // recursion in g_object_ref
    return;

  g_object_ref(object);
}

static void wrap_gtk_plug_dispose(GObject* object) {
  // Work around Flash Player bug described in bug 538914.
  //
  // This function is called during gtk_widget_destroy and/or before
  // the object's last reference is removed.  A reference to the
  // object is held during the call so the ref count should not drop
  // to zero.  However, Flash Player tries to destroy the GtkPlug
  // using g_object_unref instead of gtk_widget_destroy.  The
  // reference that Flash is removing actually belongs to the
  // GtkPlug.  During real_gtk_plug_dispose, the GtkPlug removes its
  // reference.
  //
  // A toggle ref is added to prevent premature deletion of the object
  // caused by Flash Player's extra unref, and to detect when there are
  // unexpectedly no other references.
  g_object_add_toggle_ref(object, undo_bogus_unref, nullptr);
  (*real_gtk_plug_dispose)(object);
  g_object_remove_toggle_ref(object, undo_bogus_unref, nullptr);
}

static gboolean gtk_plug_scroll_event(GtkWidget* widget,
                                      GdkEventScroll* gdk_event) {
  if (!gtk_widget_is_toplevel(widget))  // in same process as its GtkSocket
    return FALSE;  // event not handled; propagate to GtkSocket

  GdkWindow* socket_window = gtk_plug_get_socket_window(GTK_PLUG(widget));
  if (!socket_window) return FALSE;

  // Propagate the event to the embedder.
  GdkScreen* screen = gdk_window_get_screen(socket_window);
  GdkWindow* plug_window = gtk_widget_get_window(widget);
  GdkWindow* event_window = gdk_event->window;
  gint x = gdk_event->x;
  gint y = gdk_event->y;
  unsigned int button;
  unsigned int button_mask = 0;
  XEvent xevent;
  Display* dpy = GDK_WINDOW_XDISPLAY(socket_window);

  /* Translate the event coordinates to the plug window,
   * which should be aligned with the socket window.
   */
  while (event_window != plug_window) {
    gint dx, dy;

    gdk_window_get_position(event_window, &dx, &dy);
    x += dx;
    y += dy;

    event_window = gdk_window_get_parent(event_window);
    if (!event_window) return FALSE;
  }

  switch (gdk_event->direction) {
    case GDK_SCROLL_UP:
      button = 4;
      button_mask = Button4Mask;
      break;
    case GDK_SCROLL_DOWN:
      button = 5;
      button_mask = Button5Mask;
      break;
    case GDK_SCROLL_LEFT:
      button = 6;
      break;
    case GDK_SCROLL_RIGHT:
      button = 7;
      break;
    default:
      return FALSE;  // unknown GdkScrollDirection
  }

  memset(&xevent, 0, sizeof(xevent));
  xevent.xbutton.type = ButtonPress;
  xevent.xbutton.window = gdk_x11_window_get_xid(socket_window);
  xevent.xbutton.root =
      gdk_x11_window_get_xid(gdk_screen_get_root_window(screen));
  xevent.xbutton.subwindow = gdk_x11_window_get_xid(plug_window);
  xevent.xbutton.time = gdk_event->time;
  xevent.xbutton.x = x;
  xevent.xbutton.y = y;
  xevent.xbutton.x_root = gdk_event->x_root;
  xevent.xbutton.y_root = gdk_event->y_root;
  xevent.xbutton.state = gdk_event->state;
  xevent.xbutton.button = button;
  xevent.xbutton.same_screen = X11True;

  gdk_error_trap_push();

  XSendEvent(dpy, xevent.xbutton.window, X11True, ButtonPressMask, &xevent);

  xevent.xbutton.type = ButtonRelease;
  xevent.xbutton.state |= button_mask;
  XSendEvent(dpy, xevent.xbutton.window, X11True, ButtonReleaseMask, &xevent);

  gdk_display_sync(gdk_screen_get_display(screen));
  gdk_error_trap_pop();

  return TRUE;  // event handled
}

static void wrap_gtk_plug_embedded(GtkPlug* plug) {
  GdkWindow* socket_window = gtk_plug_get_socket_window(plug);
  if (socket_window) {
    if (gtk_check_version(2, 18, 7) != nullptr  // older
        && g_object_get_data(G_OBJECT(socket_window),
                             "moz-existed-before-set-window")) {
      // Add missing reference for
      // https://bugzilla.gnome.org/show_bug.cgi?id=607061
      g_object_ref(socket_window);
    }

    // Ensure the window exists to make this GtkPlug behave like an
    // in-process GtkPlug for Flash Player.  (Bugs 561308 and 539138).
    gtk_widget_realize(GTK_WIDGET(plug));
  }

  if (*real_gtk_plug_embedded) {
    (*real_gtk_plug_embedded)(plug);
  }
}

//
// The next four constants are knobs that can be tuned.  They trade
// off potential UI lag from delayed event processing with CPU time.
//
static const gint kNestedLoopDetectorPriority = G_PRIORITY_HIGH_IDLE;
// 90ms so that we can hopefully break livelocks before the user
// notices UI lag (100ms)
static const guint kNestedLoopDetectorIntervalMs = 90;

static const gint kBrowserEventPriority = G_PRIORITY_HIGH_IDLE;
static const guint kBrowserEventIntervalMs = 10;

// static
gboolean PluginModuleChild::DetectNestedEventLoop(gpointer data) {
  PluginModuleChild* pmc = static_cast<PluginModuleChild*>(data);

  MOZ_ASSERT(0 != pmc->mNestedLoopTimerId, "callback after descheduling");
  MOZ_ASSERT(pmc->mTopLoopDepth < g_main_depth(),
             "not canceled before returning to main event loop!");

  PLUGIN_LOG_DEBUG(("Detected nested glib event loop"));

  // just detected a nested loop; start a timer that will
  // periodically rpc-call back into the browser and process some
  // events
  pmc->mNestedLoopTimerId = g_timeout_add_full(
      kBrowserEventPriority, kBrowserEventIntervalMs,
      PluginModuleChild::ProcessBrowserEvents, data, nullptr);
  // cancel the nested-loop detection timer
  return FALSE;
}

// static
gboolean PluginModuleChild::ProcessBrowserEvents(gpointer data) {
  PluginModuleChild* pmc = static_cast<PluginModuleChild*>(data);

  MOZ_ASSERT(pmc->mTopLoopDepth < g_main_depth(),
             "not canceled before returning to main event loop!");

  pmc->CallProcessSomeEvents();

  return TRUE;
}

void PluginModuleChild::EnteredCxxStack() {
  MOZ_ASSERT(0 == mNestedLoopTimerId, "previous timer not descheduled");

  mNestedLoopTimerId = g_timeout_add_full(
      kNestedLoopDetectorPriority, kNestedLoopDetectorIntervalMs,
      PluginModuleChild::DetectNestedEventLoop, this, nullptr);

#  ifdef DEBUG
  mTopLoopDepth = g_main_depth();
#  endif
}

void PluginModuleChild::ExitedCxxStack() {
  MOZ_ASSERT(0 < mNestedLoopTimerId, "nested loop timeout not scheduled");

  g_source_remove(mNestedLoopTimerId);
  mNestedLoopTimerId = 0;
}

#endif

mozilla::ipc::IPCResult PluginModuleChild::RecvSetParentHangTimeout(
    const uint32_t& aSeconds) {
#ifdef XP_WIN
  SetReplyTimeoutMs(((aSeconds > 0) ? (1000 * aSeconds) : 0));
#endif
  return IPC_OK();
}

bool PluginModuleChild::ShouldContinueFromReplyTimeout() {
#ifdef XP_WIN
  MOZ_CRASH("terminating child process");
#endif
  return true;
}

bool PluginModuleChild::InitGraphics() {
#if defined(MOZ_WIDGET_GTK)
  // Work around plugins that don't interact well with GDK
  // client-side windows.
  PR_SetEnv("GDK_NATIVE_WINDOWS=1");

  gtk_init(0, 0);

  // GtkPlug is a static class so will leak anyway but this ref makes sure.
  gpointer gtk_plug_class = g_type_class_ref(GTK_TYPE_PLUG);

  // The dispose method is a good place to hook into the destruction process
  // because the reference count should be 1 the last time dispose is
  // called.  (Toggle references wouldn't detect if the reference count
  // might be higher.)
  GObjectDisposeFn* dispose = &G_OBJECT_CLASS(gtk_plug_class)->dispose;
  MOZ_ASSERT(*dispose != wrap_gtk_plug_dispose, "InitGraphics called twice");
  real_gtk_plug_dispose = *dispose;
  *dispose = wrap_gtk_plug_dispose;

  // If we ever stop setting GDK_NATIVE_WINDOWS, we'll also need to
  // gtk_widget_add_events GDK_SCROLL_MASK or GDK client-side windows will
  // not tell us about the scroll events that it intercepts.  With native
  // windows, this is called when GDK intercepts the events; if GDK doesn't
  // intercept the events, then the X server will instead send them directly
  // to an ancestor (embedder) window.
  GtkWidgetScrollEventFn* scroll_event =
      &GTK_WIDGET_CLASS(gtk_plug_class)->scroll_event;
  if (!*scroll_event) {
    *scroll_event = gtk_plug_scroll_event;
  }

  GtkPlugEmbeddedFn* embedded = &GTK_PLUG_CLASS(gtk_plug_class)->embedded;
  real_gtk_plug_embedded = *embedded;
  *embedded = wrap_gtk_plug_embedded;

#else
  // may not be necessary on all platforms
#endif
#ifdef MOZ_X11
  // Do this after initializing GDK, or GDK will install its own handler.
  InstallX11ErrorHandler();
#endif
  return true;
}

void PluginModuleChild::DeinitGraphics() {
#if defined(MOZ_X11) && defined(NS_FREE_PERMANENT_DATA)
  // We free some data off of XDisplay close hooks, ensure they're
  // run.  Closing the display is pretty scary, so we only do it to
  // silence leak checkers.
  XCloseDisplay(DefaultXDisplay());
#endif
}

NPError PluginModuleChild::NP_Shutdown() {
  AssertPluginThread();
  MOZ_ASSERT(mIsChrome);

  if (mHasShutdown) {
    return NPERR_NO_ERROR;
  }

#if defined XP_WIN
  mozilla::widget::StopAudioSession();
#endif

  // the PluginModuleParent shuts down this process after this interrupt
  // call pops off its stack

  NPError rv = mShutdownFunc ? mShutdownFunc() : NPERR_NO_ERROR;

  // weakly guard against re-entry after NP_Shutdown
  memset(&mFunctions, 0, sizeof(mFunctions));

#ifdef OS_WIN
  ResetEventHooks();
#endif

  GetIPCChannel()->SetAbortOnError(false);

  mHasShutdown = true;

  return rv;
}

mozilla::ipc::IPCResult PluginModuleChild::AnswerNP_Shutdown(NPError* rv) {
  *rv = NP_Shutdown();
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::AnswerOptionalFunctionsSupported(
    bool* aURLRedirectNotify, bool* aClearSiteData, bool* aGetSitesWithData) {
  *aURLRedirectNotify = !!mFunctions.urlredirectnotify;
  *aClearSiteData = !!mFunctions.clearsitedata;
  *aGetSitesWithData = !!mFunctions.getsiteswithdata;
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::RecvNPP_ClearSiteData(
    const nsCString& aSite, const uint64_t& aFlags, const uint64_t& aMaxAge,
    const uint64_t& aCallbackId) {
  NPError result =
      mFunctions.clearsitedata(NullableStringGet(aSite), aFlags, aMaxAge);
  SendReturnClearSiteData(result, aCallbackId);
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::RecvNPP_GetSitesWithData(
    const uint64_t& aCallbackId) {
  char** result = mFunctions.getsiteswithdata();
  nsTArray<nsCString> array;
  if (!result) {
    SendReturnSitesWithData(array, aCallbackId);
    return IPC_OK();
  }
  char** iterator = result;
  while (*iterator) {
    array.AppendElement(*iterator);
    free(*iterator);
    ++iterator;
  }
  SendReturnSitesWithData(array, aCallbackId);
  free(result);
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::RecvSetAudioSessionData(
    const nsID& aId, const nsString& aDisplayName, const nsString& aIconPath) {
#if !defined XP_WIN
  MOZ_CRASH("Not Reached!");
#else
  nsresult rv =
      mozilla::widget::RecvAudioSessionData(aId, aDisplayName, aIconPath);
  NS_ENSURE_SUCCESS(rv, IPC_OK());  // Bail early if this fails

  // Ignore failures here; we can't really do anything about them
  mozilla::widget::StartAudioSession();
  return IPC_OK();
#endif
}

mozilla::ipc::IPCResult PluginModuleChild::RecvInitPluginModuleChild(
    Endpoint<PPluginModuleChild>&& aEndpoint) {
  if (!CreateForContentProcess(std::move(aEndpoint))) {
    return IPC_FAIL(this, "CreateForContentProcess failed");
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::RecvInitPluginFunctionBroker(
    Endpoint<PFunctionBrokerChild>&& aEndpoint) {
#if defined(XP_WIN)
  MOZ_ASSERT(mIsChrome);
  if (!FunctionBrokerChild::Initialize(std::move(aEndpoint))) {
    return IPC_FAIL(
        this, "InitPluginFunctionBroker failed to initialize broker child.");
  }
  return IPC_OK();
#else
  return IPC_FAIL(this,
                  "InitPluginFunctionBroker not supported on this platform.");
#endif
}

mozilla::ipc::IPCResult PluginModuleChild::AnswerInitCrashReporter(
    mozilla::dom::NativeThreadId* aOutId) {
  CrashReporterClient::InitSingleton();
  *aOutId = CrashReporter::CurrentThreadId();

  return IPC_OK();
}

void PluginModuleChild::ActorDestroy(ActorDestroyReason why) {
#ifdef MOZ_GECKO_PROFILER
  if (mProfilerController) {
    mProfilerController->Shutdown();
    mProfilerController = nullptr;
  }
#endif

  if (!mIsChrome) {
    PluginModuleChild* chromeInstance = PluginModuleChild::GetChrome();
    if (chromeInstance) {
      chromeInstance->SendNotifyContentModuleDestroyed();
    }

    // Destroy ourselves once we finish other teardown activities.
    RefPtr<DeleteTask<PluginModuleChild>> task =
        new DeleteTask<PluginModuleChild>(this);
    MessageLoop::current()->PostTask(task.forget());
    return;
  }

  if (AbnormalShutdown == why) {
    NS_WARNING("shutting down early because of crash!");
    ProcessChild::QuickExit();
  }

  if (!mHasShutdown) {
    MOZ_ASSERT(gChromeInstance == this);
    NP_Shutdown();
  }

#if defined(XP_WIN)
  FunctionBrokerChild::Destroy();
  FunctionHook::ClearDllInterceptorCache();
#endif

  // doesn't matter why we're being destroyed; it's up to us to
  // initiate (clean) shutdown
  CrashReporterClient::DestroySingleton();

  XRE_ShutdownChildProcess();
}

void PluginModuleChild::CleanUp() {}

const char* PluginModuleChild::GetUserAgent() {
  return NullableStringGet(Settings().userAgent());
}

//-----------------------------------------------------------------------------
// FIXME/cjones: just getting this out of the way for the moment ...

namespace mozilla::plugins::child {

static NPError _requestread(NPStream* pstream, NPByteRange* rangeList);

static NPError _geturlnotify(NPP aNPP, const char* relativeURL,
                             const char* target, void* notifyData);

static NPError _getvalue(NPP aNPP, NPNVariable variable, void* r_value);

static NPError _setvalue(NPP aNPP, NPPVariable variable, void* r_value);

static NPError _geturl(NPP aNPP, const char* relativeURL, const char* target);

static NPError _posturlnotify(NPP aNPP, const char* relativeURL,
                              const char* target, uint32_t len, const char* buf,
                              NPBool file, void* notifyData);

static NPError _posturl(NPP aNPP, const char* relativeURL, const char* target,
                        uint32_t len, const char* buf, NPBool file);

static void _status(NPP aNPP, const char* message);

static void _memfree(void* ptr);

static uint32_t _memflush(uint32_t size);

static void _reloadplugins(NPBool reloadPages);

static void _invalidaterect(NPP aNPP, NPRect* invalidRect);

static void _invalidateregion(NPP aNPP, NPRegion invalidRegion);

static void _forceredraw(NPP aNPP);

static const char* _useragent(NPP aNPP);

static void* _memalloc(uint32_t size);

// Deprecated entry points for the old Java plugin.
static void* /* OJI type: JRIEnv* */
_getjavaenv(void);

// Deprecated entry points for the old Java plugin.
static void* /* OJI type: jref */
_getjavapeer(NPP aNPP);

static bool _invoke(NPP aNPP, NPObject* npobj, NPIdentifier method,
                    const NPVariant* args, uint32_t argCount,
                    NPVariant* result);

static bool _invokedefault(NPP aNPP, NPObject* npobj, const NPVariant* args,
                           uint32_t argCount, NPVariant* result);

static bool _evaluate(NPP aNPP, NPObject* npobj, NPString* script,
                      NPVariant* result);

static bool _getproperty(NPP aNPP, NPObject* npobj, NPIdentifier property,
                         NPVariant* result);

static bool _setproperty(NPP aNPP, NPObject* npobj, NPIdentifier property,
                         const NPVariant* value);

static bool _removeproperty(NPP aNPP, NPObject* npobj, NPIdentifier property);

static bool _hasproperty(NPP aNPP, NPObject* npobj, NPIdentifier propertyName);

static bool _hasmethod(NPP aNPP, NPObject* npobj, NPIdentifier methodName);

static bool _enumerate(NPP aNPP, NPObject* npobj, NPIdentifier** identifier,
                       uint32_t* count);

static bool _construct(NPP aNPP, NPObject* npobj, const NPVariant* args,
                       uint32_t argCount, NPVariant* result);

static void _releasevariantvalue(NPVariant* variant);

static void _setexception(NPObject* npobj, const NPUTF8* message);

static void _pushpopupsenabledstate(NPP aNPP, NPBool enabled);

static void _poppopupsenabledstate(NPP aNPP);

static NPError _getvalueforurl(NPP npp, NPNURLVariable variable,
                               const char* url, char** value, uint32_t* len);

static NPError _setvalueforurl(NPP npp, NPNURLVariable variable,
                               const char* url, const char* value,
                               uint32_t len);

static uint32_t _scheduletimer(NPP instance, uint32_t interval, NPBool repeat,
                               void (*timerFunc)(NPP npp, uint32_t timerID));

static void _unscheduletimer(NPP instance, uint32_t timerID);

static NPError _popupcontextmenu(NPP instance, NPMenu* menu);

static NPBool _convertpoint(NPP instance, double sourceX, double sourceY,
                            NPCoordinateSpace sourceSpace, double* destX,
                            double* destY, NPCoordinateSpace destSpace);

static void _urlredirectresponse(NPP instance, void* notifyData, NPBool allow);

static NPError _initasyncsurface(NPP instance, NPSize* size,
                                 NPImageFormat format, void* initData,
                                 NPAsyncSurface* surface);

static NPError _finalizeasyncsurface(NPP instance, NPAsyncSurface* surface);

static void _setcurrentasyncsurface(NPP instance, NPAsyncSurface* surface,
                                    NPRect* changed);

}  // namespace mozilla::plugins::child

const NPNetscapeFuncs PluginModuleChild::sBrowserFuncs = {
    sizeof(sBrowserFuncs),
    (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR,
    mozilla::plugins::child::_geturl,
    mozilla::plugins::child::_posturl,
    mozilla::plugins::child::_requestread,
    nullptr,  // _newstream, unimplemented
    nullptr,  // _write, unimplemented
    nullptr,  // _destroystream, unimplemented
    mozilla::plugins::child::_status,
    mozilla::plugins::child::_useragent,
    mozilla::plugins::child::_memalloc,
    mozilla::plugins::child::_memfree,
    mozilla::plugins::child::_memflush,
    mozilla::plugins::child::_reloadplugins,
    mozilla::plugins::child::_getjavaenv,
    mozilla::plugins::child::_getjavapeer,
    mozilla::plugins::child::_geturlnotify,
    mozilla::plugins::child::_posturlnotify,
    mozilla::plugins::child::_getvalue,
    mozilla::plugins::child::_setvalue,
    mozilla::plugins::child::_invalidaterect,
    mozilla::plugins::child::_invalidateregion,
    mozilla::plugins::child::_forceredraw,
    PluginModuleChild::NPN_GetStringIdentifier,
    PluginModuleChild::NPN_GetStringIdentifiers,
    PluginModuleChild::NPN_GetIntIdentifier,
    PluginModuleChild::NPN_IdentifierIsString,
    PluginModuleChild::NPN_UTF8FromIdentifier,
    PluginModuleChild::NPN_IntFromIdentifier,
    PluginModuleChild::NPN_CreateObject,
    PluginModuleChild::NPN_RetainObject,
    PluginModuleChild::NPN_ReleaseObject,
    mozilla::plugins::child::_invoke,
    mozilla::plugins::child::_invokedefault,
    mozilla::plugins::child::_evaluate,
    mozilla::plugins::child::_getproperty,
    mozilla::plugins::child::_setproperty,
    mozilla::plugins::child::_removeproperty,
    mozilla::plugins::child::_hasproperty,
    mozilla::plugins::child::_hasmethod,
    mozilla::plugins::child::_releasevariantvalue,
    mozilla::plugins::child::_setexception,
    mozilla::plugins::child::_pushpopupsenabledstate,
    mozilla::plugins::child::_poppopupsenabledstate,
    mozilla::plugins::child::_enumerate,
    nullptr,  // pluginthreadasynccall, not used
    mozilla::plugins::child::_construct,
    mozilla::plugins::child::_getvalueforurl,
    mozilla::plugins::child::_setvalueforurl,
    nullptr,  // NPN GetAuthenticationInfo, not supported
    mozilla::plugins::child::_scheduletimer,
    mozilla::plugins::child::_unscheduletimer,
    mozilla::plugins::child::_popupcontextmenu,
    mozilla::plugins::child::_convertpoint,
    nullptr,  // handleevent, unimplemented
    nullptr,  // unfocusinstance, unimplemented
    mozilla::plugins::child::_urlredirectresponse,
    mozilla::plugins::child::_initasyncsurface,
    mozilla::plugins::child::_finalizeasyncsurface,
    mozilla::plugins::child::_setcurrentasyncsurface,
};

PluginInstanceChild* InstCast(NPP aNPP) {
  MOZ_ASSERT(!!(aNPP->ndata), "nil instance");
  return static_cast<PluginInstanceChild*>(aNPP->ndata);
}

namespace mozilla::plugins::child {

NPError _requestread(NPStream* aStream, NPByteRange* aRangeList) {
  return NPERR_STREAM_NOT_SEEKABLE;
}

NPError _geturlnotify(NPP aNPP, const char* aRelativeURL, const char* aTarget,
                      void* aNotifyData) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(NPERR_INVALID_PARAM);

  if (!aNPP)  // nullptr check for nspluginwrapper (bug 561690)
    return NPERR_INVALID_INSTANCE_ERROR;

  nsCString url = NullableString(aRelativeURL);
  auto* sn = new StreamNotifyChild(url);

  NPError err;
  if (!InstCast(aNPP)->CallPStreamNotifyConstructor(
          sn, url, NullableString(aTarget), false, nsCString(), false, &err)) {
    return NPERR_GENERIC_ERROR;
  }

  if (NPERR_NO_ERROR == err) {
    // If NPN_PostURLNotify fails, the parent will immediately send us
    // a PStreamNotifyDestructor, which should not call NPP_URLNotify.
    sn->SetValid(aNotifyData);
  }

  return err;
}

NPError _getvalue(NPP aNPP, NPNVariable aVariable, void* aValue) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(NPERR_INVALID_PARAM);

  switch (aVariable) {
    // Copied from nsNPAPIPlugin.cpp
    case NPNVToolkit:
#if defined(MOZ_WIDGET_GTK)
      *static_cast<NPNToolkitType*>(aValue) = NPNVGtk2;
      return NPERR_NO_ERROR;
#endif
      return NPERR_GENERIC_ERROR;

    case NPNVjavascriptEnabledBool:
      *(NPBool*)aValue =
          PluginModuleChild::GetChrome()->Settings().javascriptEnabled();
      return NPERR_NO_ERROR;
    case NPNVasdEnabledBool:
      *(NPBool*)aValue =
          PluginModuleChild::GetChrome()->Settings().asdEnabled();
      return NPERR_NO_ERROR;
    case NPNVisOfflineBool:
      *(NPBool*)aValue = PluginModuleChild::GetChrome()->Settings().isOffline();
      return NPERR_NO_ERROR;
    case NPNVSupportsXEmbedBool:
      // We don't support windowed xembed any more. But we still deliver
      // events based on X/GTK, not Xt, so we continue to return true
      // (and Flash requires that we return true).
      *(NPBool*)aValue = true;
      return NPERR_NO_ERROR;
    case NPNVSupportsWindowless:
      *(NPBool*)aValue = true;
      return NPERR_NO_ERROR;
#if defined(MOZ_WIDGET_GTK)
    case NPNVxDisplay: {
      if (!aNPP) {
        return NPERR_INVALID_INSTANCE_ERROR;
      }
      return InstCast(aNPP)->NPN_GetValue(aVariable, aValue);
    }
    case NPNVxtAppContext:
      return NPERR_GENERIC_ERROR;
#endif
    default: {
      if (aNPP) {
        return InstCast(aNPP)->NPN_GetValue(aVariable, aValue);
      }

      NS_WARNING("Null NPP!");
      return NPERR_INVALID_INSTANCE_ERROR;
    }
  }

  MOZ_ASSERT_UNREACHABLE("Shouldn't get here!");
  return NPERR_GENERIC_ERROR;
}

NPError _setvalue(NPP aNPP, NPPVariable aVariable, void* aValue) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(NPERR_INVALID_PARAM);
  return InstCast(aNPP)->NPN_SetValue(aVariable, aValue);
}

NPError _geturl(NPP aNPP, const char* aRelativeURL, const char* aTarget) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(NPERR_INVALID_PARAM);

  NPError err;
  InstCast(aNPP)->CallNPN_GetURL(NullableString(aRelativeURL),
                                 NullableString(aTarget), &err);
  return err;
}

NPError _posturlnotify(NPP aNPP, const char* aRelativeURL, const char* aTarget,
                       uint32_t aLength, const char* aBuffer, NPBool aIsFile,
                       void* aNotifyData) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(NPERR_INVALID_PARAM);

  if (!aBuffer) return NPERR_INVALID_PARAM;

  if (aIsFile) {
    PLUGIN_LOG_DEBUG(
        ("NPN_PostURLNotify with file=true is no longer supported"));
    return NPERR_GENERIC_ERROR;
  }

  nsCString url = NullableString(aRelativeURL);
  auto* sn = new StreamNotifyChild(url);

  NPError err;
  if (!InstCast(aNPP)->CallPStreamNotifyConstructor(
          sn, url, NullableString(aTarget), true, nsCString(aBuffer, aLength),
          aIsFile, &err)) {
    return NPERR_GENERIC_ERROR;
  }

  if (NPERR_NO_ERROR == err) {
    // If NPN_PostURLNotify fails, the parent will immediately send us
    // a PStreamNotifyDestructor, which should not call NPP_URLNotify.
    sn->SetValid(aNotifyData);
  }

  return err;
}

NPError _posturl(NPP aNPP, const char* aRelativeURL, const char* aTarget,
                 uint32_t aLength, const char* aBuffer, NPBool aIsFile) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(NPERR_INVALID_PARAM);

  if (aIsFile) {
    PLUGIN_LOG_DEBUG(("NPN_PostURL with file=true is no longer supported"));
    return NPERR_GENERIC_ERROR;
  }
  NPError err;
  // FIXME what should happen when |aBuffer| is null?
  InstCast(aNPP)->CallNPN_PostURL(
      NullableString(aRelativeURL), NullableString(aTarget),
      nsDependentCString(aBuffer, aLength), aIsFile, &err);
  return err;
}

void _status(NPP aNPP, const char* aMessage) {
  // NPN_Status is no longer supported.
}

void _memfree(void* aPtr) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  free(aPtr);
}

uint32_t _memflush(uint32_t aSize) { return 0; }

void _reloadplugins(NPBool aReloadPages) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD_VOID();

  // Send the reload message to all modules. Chrome will need to reload from
  // disk and content will need to request a new list of plugin tags from
  // chrome.
  PluginModuleChild::GetChrome()->SendNPN_ReloadPlugins(!!aReloadPages);
}

void _invalidaterect(NPP aNPP, NPRect* aInvalidRect) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD_VOID();
  // nullptr check for nspluginwrapper (bug 548434)
  if (aNPP) {
    InstCast(aNPP)->InvalidateRect(aInvalidRect);
  }
}

void _invalidateregion(NPP aNPP, NPRegion aInvalidRegion) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD_VOID();
  NS_WARNING("Not yet implemented!");
}

void _forceredraw(NPP aNPP) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD_VOID();

  // We ignore calls to NPN_ForceRedraw. Such calls should
  // never be necessary.
}

const char* _useragent(NPP aNPP) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(nullptr);
  return PluginModuleChild::GetChrome()->GetUserAgent();
}

void* _memalloc(uint32_t aSize) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  return moz_xmalloc(aSize);
}

// Deprecated entry points for the old Java plugin.
void* /* OJI type: JRIEnv* */
_getjavaenv(void) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  return 0;
}

void* /* OJI type: jref */
_getjavapeer(NPP aNPP) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  return 0;
}

bool _invoke(NPP aNPP, NPObject* aNPObj, NPIdentifier aMethod,
             const NPVariant* aArgs, uint32_t aArgCount, NPVariant* aResult) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class || !aNPObj->_class->invoke)
    return false;

  return aNPObj->_class->invoke(aNPObj, aMethod, aArgs, aArgCount, aResult);
}

bool _invokedefault(NPP aNPP, NPObject* aNPObj, const NPVariant* aArgs,
                    uint32_t aArgCount, NPVariant* aResult) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class || !aNPObj->_class->invokeDefault)
    return false;

  return aNPObj->_class->invokeDefault(aNPObj, aArgs, aArgCount, aResult);
}

bool _evaluate(NPP aNPP, NPObject* aObject, NPString* aScript,
               NPVariant* aResult) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!(aNPP && aObject && aScript && aResult)) {
    NS_ERROR("Bad arguments!");
    return false;
  }

  PluginScriptableObjectChild* actor =
      InstCast(aNPP)->GetActorForNPObject(aObject);
  if (!actor) {
    NS_ERROR("Failed to create actor?!");
    return false;
  }

#ifdef XP_WIN
  if (gDelayFlashFocusReplyUntilEval) {
    ReplyMessage(0);
    gDelayFlashFocusReplyUntilEval = false;
  }
#endif

  return actor->Evaluate(aScript, aResult);
}

bool _getproperty(NPP aNPP, NPObject* aNPObj, NPIdentifier aPropertyName,
                  NPVariant* aResult) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class || !aNPObj->_class->getProperty)
    return false;

  return aNPObj->_class->getProperty(aNPObj, aPropertyName, aResult);
}

bool _setproperty(NPP aNPP, NPObject* aNPObj, NPIdentifier aPropertyName,
                  const NPVariant* aValue) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class || !aNPObj->_class->setProperty)
    return false;

  return aNPObj->_class->setProperty(aNPObj, aPropertyName, aValue);
}

bool _removeproperty(NPP aNPP, NPObject* aNPObj, NPIdentifier aPropertyName) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class || !aNPObj->_class->removeProperty)
    return false;

  return aNPObj->_class->removeProperty(aNPObj, aPropertyName);
}

bool _hasproperty(NPP aNPP, NPObject* aNPObj, NPIdentifier aPropertyName) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class || !aNPObj->_class->hasProperty)
    return false;

  return aNPObj->_class->hasProperty(aNPObj, aPropertyName);
}

bool _hasmethod(NPP aNPP, NPObject* aNPObj, NPIdentifier aMethodName) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class || !aNPObj->_class->hasMethod)
    return false;

  return aNPObj->_class->hasMethod(aNPObj, aMethodName);
}

bool _enumerate(NPP aNPP, NPObject* aNPObj, NPIdentifier** aIdentifiers,
                uint32_t* aCount) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class) return false;

  if (!NP_CLASS_STRUCT_VERSION_HAS_ENUM(aNPObj->_class) ||
      !aNPObj->_class->enumerate) {
    *aIdentifiers = 0;
    *aCount = 0;
    return true;
  }

  return aNPObj->_class->enumerate(aNPObj, aIdentifiers, aCount);
}

bool _construct(NPP aNPP, NPObject* aNPObj, const NPVariant* aArgs,
                uint32_t aArgCount, NPVariant* aResult) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(false);

  if (!aNPP || !aNPObj || !aNPObj->_class ||
      !NP_CLASS_STRUCT_VERSION_HAS_CTOR(aNPObj->_class) ||
      !aNPObj->_class->construct) {
    return false;
  }

  return aNPObj->_class->construct(aNPObj, aArgs, aArgCount, aResult);
}

void _releasevariantvalue(NPVariant* aVariant) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  // Only assert plugin thread here for consistency with in-process plugins.
  AssertPluginThread();

  if (NPVARIANT_IS_STRING(*aVariant)) {
    NPString str = NPVARIANT_TO_STRING(*aVariant);
    free(const_cast<NPUTF8*>(str.UTF8Characters));
  } else if (NPVARIANT_IS_OBJECT(*aVariant)) {
    NPObject* object = NPVARIANT_TO_OBJECT(*aVariant);
    if (object) {
      PluginModuleChild::NPN_ReleaseObject(object);
    }
  }
  VOID_TO_NPVARIANT(*aVariant);
}

void _setexception(NPObject* aNPObj, const NPUTF8* aMessage) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD_VOID();

  // Do nothing. We no longer support this API.
}

void _pushpopupsenabledstate(NPP aNPP, NPBool aEnabled) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD_VOID();

  InstCast(aNPP)->CallNPN_PushPopupsEnabledState(aEnabled ? true : false);
}

void _poppopupsenabledstate(NPP aNPP) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD_VOID();

  InstCast(aNPP)->CallNPN_PopPopupsEnabledState();
}

NPError _getvalueforurl(NPP npp, NPNURLVariable variable, const char* url,
                        char** value, uint32_t* len) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();

  if (!url) return NPERR_INVALID_URL;

  if (!npp || !value || !len) return NPERR_INVALID_PARAM;

  if (variable == NPNURLVProxy) {
    nsCString v;
    NPError result;
    InstCast(npp)->CallNPN_GetValueForURL(variable, nsCString(url), &v,
                                          &result);
    if (NPERR_NO_ERROR == result) {
      *value = ToNewCString(v);
      *len = v.Length();
    }
    return result;
  }

  return NPERR_INVALID_PARAM;
}

NPError _setvalueforurl(NPP npp, NPNURLVariable variable, const char* url,
                        const char* value, uint32_t len) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();

  if (!value) return NPERR_INVALID_PARAM;

  if (!url) return NPERR_INVALID_URL;

  if (variable == NPNURLVProxy) {
    NPError result;
    InstCast(npp)->CallNPN_SetValueForURL(
        variable, nsCString(url), nsDependentCString(value, len), &result);
    return result;
  }

  return NPERR_INVALID_PARAM;
}

uint32_t _scheduletimer(NPP npp, uint32_t interval, NPBool repeat,
                        void (*timerFunc)(NPP npp, uint32_t timerID)) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();
  return InstCast(npp)->ScheduleTimer(interval, repeat, timerFunc);
}

void _unscheduletimer(NPP npp, uint32_t timerID) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();
  InstCast(npp)->UnscheduleTimer(timerID);
}

#ifdef OS_MACOSX
static void ProcessBrowserEvents(void* pluginModule) {
  PluginModuleChild* pmc = static_cast<PluginModuleChild*>(pluginModule);

  if (!pmc) return;

  pmc->CallProcessSomeEvents();
}
#endif

NPError _popupcontextmenu(NPP instance, NPMenu* menu) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();

#ifdef MOZ_WIDGET_COCOA
  double pluginX, pluginY;
  double screenX, screenY;

  const NPCocoaEvent* currentEvent = InstCast(instance)->getCurrentEvent();
  if (!currentEvent) {
    return NPERR_GENERIC_ERROR;
  }

  // Ensure that the events has an x/y value.
  if (currentEvent->type != NPCocoaEventMouseDown &&
      currentEvent->type != NPCocoaEventMouseUp &&
      currentEvent->type != NPCocoaEventMouseMoved &&
      currentEvent->type != NPCocoaEventMouseEntered &&
      currentEvent->type != NPCocoaEventMouseExited &&
      currentEvent->type != NPCocoaEventMouseDragged) {
    return NPERR_GENERIC_ERROR;
  }

  pluginX = currentEvent->data.mouse.pluginX;
  pluginY = currentEvent->data.mouse.pluginY;

  if ((pluginX < 0.0) || (pluginY < 0.0)) return NPERR_GENERIC_ERROR;

  NPBool success =
      _convertpoint(instance, pluginX, pluginY, NPCoordinateSpacePlugin,
                    &screenX, &screenY, NPCoordinateSpaceScreen);

  if (success) {
    return mozilla::plugins::PluginUtilsOSX::ShowCocoaContextMenu(
        menu, screenX, screenY, InstCast(instance)->Manager(),
        ProcessBrowserEvents);
  } else {
    NS_WARNING("Convertpoint failed, could not created contextmenu.");
    return NPERR_GENERIC_ERROR;
  }

#else
  NS_WARNING("Not supported on this platform!");
  return NPERR_GENERIC_ERROR;
#endif
}

NPBool _convertpoint(NPP instance, double sourceX, double sourceY,
                     NPCoordinateSpace sourceSpace, double* destX,
                     double* destY, NPCoordinateSpace destSpace) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  if (!IsPluginThread()) {
    NS_WARNING("Not running on the plugin's main thread!");
    return false;
  }

  double rDestX = 0;
  bool ignoreDestX = !destX;
  double rDestY = 0;
  bool ignoreDestY = !destY;
  bool result = false;
  InstCast(instance)->CallNPN_ConvertPoint(sourceX, ignoreDestX, sourceY,
                                           ignoreDestY, sourceSpace, destSpace,
                                           &rDestX, &rDestY, &result);
  if (result) {
    if (destX) *destX = rDestX;
    if (destY) *destY = rDestY;
  }

  return result;
}

void _urlredirectresponse(NPP instance, void* notifyData, NPBool allow) {
  InstCast(instance)->NPN_URLRedirectResponse(notifyData, allow);
}

NPError _initasyncsurface(NPP instance, NPSize* size, NPImageFormat format,
                          void* initData, NPAsyncSurface* surface) {
  return InstCast(instance)->NPN_InitAsyncSurface(size, format, initData,
                                                  surface);
}

NPError _finalizeasyncsurface(NPP instance, NPAsyncSurface* surface) {
  return InstCast(instance)->NPN_FinalizeAsyncSurface(surface);
}

void _setcurrentasyncsurface(NPP instance, NPAsyncSurface* surface,
                             NPRect* changed) {
  InstCast(instance)->NPN_SetCurrentAsyncSurface(surface, changed);
}

}  // namespace mozilla::plugins::child

//-----------------------------------------------------------------------------

mozilla::ipc::IPCResult PluginModuleChild::RecvSettingChanged(
    const PluginSettings& aSettings) {
  mCachedSettings = aSettings;
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::AnswerNP_GetEntryPoints(
    NPError* _retval) {
  PLUGIN_LOG_DEBUG_METHOD;
  AssertPluginThread();
  MOZ_ASSERT(mIsChrome);

#if defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
  return IPC_OK();
#elif defined(OS_WIN) || defined(OS_MACOSX)
  *_retval = mGetEntryPointsFunc(&mFunctions);
  return IPC_OK();
#else
#  error Please implement me for your platform
#endif
}

mozilla::ipc::IPCResult PluginModuleChild::AnswerNP_Initialize(
    const PluginSettings& aSettings, NPError* rv) {
  *rv = DoNP_Initialize(aSettings);
  return IPC_OK();
}

NPError PluginModuleChild::DoNP_Initialize(const PluginSettings& aSettings) {
  PLUGIN_LOG_DEBUG_METHOD;
  AssertPluginThread();
  MOZ_ASSERT(mIsChrome);

  mCachedSettings = aSettings;

#ifdef OS_WIN
  SetEventHooks();
#endif

#ifdef MOZ_X11
#  ifdef MOZ_WIDGET_GTK
  if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())) {
    // We don't support NPAPI plugins on Wayland.
    return NPERR_GENERIC_ERROR;
  }
#  endif
  // Send the parent our X socket to act as a proxy reference for our X
  // resources.
  int xSocketFd = ConnectionNumber(DefaultXDisplay());
  SendBackUpXResources(FileDescriptor(xSocketFd));
#endif

  NPError result;
#if defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
  result = mInitializeFunc(&sBrowserFuncs, &mFunctions);
#elif defined(OS_WIN) || defined(OS_MACOSX)
  result = mInitializeFunc(&sBrowserFuncs);
#else
#  error Please implement me for your platform
#endif

  return result;
}

PPluginInstanceChild* PluginModuleChild::AllocPPluginInstanceChild(
    const nsCString& aMimeType, const nsTArray<nsCString>& aNames,
    const nsTArray<nsCString>& aValues) {
  PLUGIN_LOG_DEBUG_METHOD;
  AssertPluginThread();

  // In e10s, gChromeInstance hands out quirks to instances, but never
  // allocates an instance on its own. Make sure it gets the latest copy
  // of quirks once we have them. Also note, with process-per-tab, we may
  // have multiple PluginModuleChilds in the same plugin process, so only
  // initialize this once in gChromeInstance, which is a singleton.
  GetChrome()->InitQuirksModes(aMimeType);
  mQuirks = GetChrome()->mQuirks;

#ifdef XP_WIN
  FunctionHook::HookFunctions(mQuirks);
#endif

  return new PluginInstanceChild(&mFunctions, aMimeType, aNames, aValues);
}

void PluginModuleChild::InitQuirksModes(const nsCString& aMimeType) {
  if (mQuirks != QUIRKS_NOT_INITIALIZED) {
    return;
  }

  mQuirks = GetQuirksFromMimeTypeAndFilename(aMimeType, mPluginFilename);
}

mozilla::ipc::IPCResult PluginModuleChild::AnswerModuleSupportsAsyncRender(
    bool* aResult) {
#if defined(XP_WIN)
  *aResult = gChromeInstance->mAsyncRenderSupport;
  return IPC_OK();
#else
  MOZ_ASSERT_UNREACHABLE("Shouldn't get here!");
  return IPC_FAIL_NO_REASON(this);
#endif
}

mozilla::ipc::IPCResult PluginModuleChild::RecvPPluginInstanceConstructor(
    PPluginInstanceChild* aActor, const nsCString& aMimeType,
    nsTArray<nsCString>&& aNames, nsTArray<nsCString>&& aValues) {
  PLUGIN_LOG_DEBUG_METHOD;
  AssertPluginThread();

  NS_ASSERTION(aActor, "Null actor!");
  return IPC_OK();
}

mozilla::ipc::IPCResult PluginModuleChild::AnswerSyncNPP_New(
    PPluginInstanceChild* aActor, NPError* rv) {
  PLUGIN_LOG_DEBUG_METHOD;
  PluginInstanceChild* childInstance =
      reinterpret_cast<PluginInstanceChild*>(aActor);
  AssertPluginThread();
  *rv = childInstance->DoNPP_New();
  return IPC_OK();
}

bool PluginModuleChild::DeallocPPluginInstanceChild(
    PPluginInstanceChild* aActor) {
  PLUGIN_LOG_DEBUG_METHOD;
  AssertPluginThread();

  delete aActor;

  return true;
}

NPObject* PluginModuleChild::NPN_CreateObject(NPP aNPP, NPClass* aClass) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  ENSURE_PLUGIN_THREAD(nullptr);

  PluginInstanceChild* i = InstCast(aNPP);
  if (i->mDeletingHash) {
    NS_ERROR("Plugin used NPP after NPP_Destroy");
    return nullptr;
  }

  NPObject* newObject;
  if (aClass && aClass->allocate) {
    newObject = aClass->allocate(aNPP, aClass);
  } else {
    newObject = reinterpret_cast<NPObject*>(child::_memalloc(sizeof(NPObject)));
  }

  if (newObject) {
    newObject->_class = aClass;
    newObject->referenceCount = 1;
    NS_LOG_ADDREF(newObject, 1, "NPObject", sizeof(NPObject));
  }

  PluginScriptableObjectChild::RegisterObject(newObject, i);

  return newObject;
}

NPObject* PluginModuleChild::NPN_RetainObject(NPObject* aNPObj) {
  AssertPluginThread();

  if (NS_WARN_IF(!aNPObj)) {
    return nullptr;
  }

#ifdef NS_BUILD_REFCNT_LOGGING
  int32_t refCnt =
#endif
      PR_ATOMIC_INCREMENT((int32_t*)&aNPObj->referenceCount);
  NS_LOG_ADDREF(aNPObj, refCnt, "NPObject", sizeof(NPObject));

  return aNPObj;
}

void PluginModuleChild::NPN_ReleaseObject(NPObject* aNPObj) {
  AssertPluginThread();

  PluginInstanceChild* instance =
      PluginScriptableObjectChild::GetInstanceForNPObject(aNPObj);
  if (!instance) {
    // The PluginInstanceChild was destroyed
    return;
  }

  DeletingObjectEntry* doe = nullptr;
  if (instance->mDeletingHash) {
    doe = instance->mDeletingHash->GetEntry(aNPObj);
    if (!doe) {
      NS_ERROR(
          "An object for a destroyed instance isn't in the instance deletion "
          "hash");
      return;
    }
    if (doe->mDeleted) return;
  }

  int32_t refCnt = PR_ATOMIC_DECREMENT((int32_t*)&aNPObj->referenceCount);
  NS_LOG_RELEASE(aNPObj, refCnt, "NPObject");

  if (refCnt == 0) {
    DeallocNPObject(aNPObj);
    if (doe) doe->mDeleted = true;
  }
}

void PluginModuleChild::DeallocNPObject(NPObject* aNPObj) {
  if (aNPObj->_class && aNPObj->_class->deallocate) {
    aNPObj->_class->deallocate(aNPObj);
  } else {
    child::_memfree(aNPObj);
  }

  PluginScriptableObjectChild* actor =
      PluginScriptableObjectChild::GetActorForNPObject(aNPObj);
  if (actor) actor->NPObjectDestroyed();

  PluginScriptableObjectChild::UnregisterObject(aNPObj);
}

NPIdentifier PluginModuleChild::NPN_GetStringIdentifier(const NPUTF8* aName) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();

  if (!aName) return 0;

  nsDependentCString name(aName);
  PluginIdentifier ident(name);
  PluginScriptableObjectChild::StackIdentifier stackID(ident);
  stackID.MakePermanent();
  return stackID.ToNPIdentifier();
}

void PluginModuleChild::NPN_GetStringIdentifiers(const NPUTF8** aNames,
                                                 int32_t aNameCount,
                                                 NPIdentifier* aIdentifiers) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();

  if (!(aNames && aNameCount > 0 && aIdentifiers)) {
    MOZ_CRASH("Bad input! Headed for a crash!");
  }

  for (int32_t index = 0; index < aNameCount; ++index) {
    if (!aNames[index]) {
      aIdentifiers[index] = 0;
      continue;
    }
    nsDependentCString name(aNames[index]);
    PluginIdentifier ident(name);
    PluginScriptableObjectChild::StackIdentifier stackID(ident);
    stackID.MakePermanent();
    aIdentifiers[index] = stackID.ToNPIdentifier();
  }
}

bool PluginModuleChild::NPN_IdentifierIsString(NPIdentifier aIdentifier) {
  PLUGIN_LOG_DEBUG_FUNCTION;

  PluginScriptableObjectChild::StackIdentifier stack(aIdentifier);
  return stack.IsString();
}

NPIdentifier PluginModuleChild::NPN_GetIntIdentifier(int32_t aIntId) {
  PLUGIN_LOG_DEBUG_FUNCTION;
  AssertPluginThread();

  PluginIdentifier ident(aIntId);
  PluginScriptableObjectChild::StackIdentifier stackID(ident);
  stackID.MakePermanent();
  return stackID.ToNPIdentifier();
}

NPUTF8* PluginModuleChild::NPN_UTF8FromIdentifier(NPIdentifier aIdentifier) {
  PLUGIN_LOG_DEBUG_FUNCTION;

  PluginScriptableObjectChild::StackIdentifier stackID(aIdentifier);
  if (stackID.IsString()) {
    return ToNewCString(stackID.GetString());
  }
  return nullptr;
}

int32_t PluginModuleChild::NPN_IntFromIdentifier(NPIdentifier aIdentifier) {
  PLUGIN_LOG_DEBUG_FUNCTION;

  PluginScriptableObjectChild::StackIdentifier stackID(aIdentifier);
  if (!stackID.IsString()) {
    return stackID.GetInt();
  }
  return INT32_MIN;
}

#ifdef OS_WIN
void PluginModuleChild::EnteredCall() { mIncallPumpingStack.AppendElement(); }

void PluginModuleChild::ExitedCall() {
  NS_ASSERTION(mIncallPumpingStack.Length(), "mismatched entered/exited");
  const IncallFrame& f = mIncallPumpingStack.LastElement();
  if (f._spinning)
    MessageLoop::current()->SetNestableTasksAllowed(
        f._savedNestableTasksAllowed);

  // XXX Is RemoveLastElement intentionally called only after calling
  // SetNestableTasksAllowed? Otherwise, PopLastElement could be used above.
  mIncallPumpingStack.RemoveLastElement();
}

LRESULT CALLBACK PluginModuleChild::CallWindowProcHook(int nCode, WPARAM wParam,
                                                       LPARAM lParam) {
  // Trap and reply to anything we recognize as the source of a
  // potential send message deadlock.
  if (nCode >= 0 &&
      (InSendMessageEx(nullptr) & (ISMEX_REPLIED | ISMEX_SEND)) == ISMEX_SEND) {
    CWPSTRUCT* pCwp = reinterpret_cast<CWPSTRUCT*>(lParam);
    if (pCwp->message == WM_KILLFOCUS) {
      // Fix for flash fullscreen window loosing focus. On single
      // core systems, sync killfocus events need to be handled
      // after the flash fullscreen window procedure processes this
      // message, otherwise fullscreen focus will not work correctly.
      wchar_t szClass[26];
      if (GetClassNameW(pCwp->hwnd, szClass,
                        sizeof(szClass) / sizeof(char16_t)) &&
          !wcscmp(szClass, kFlashFullscreenClass)) {
        gDelayFlashFocusReplyUntilEval = true;
      }
    }
  }

  return CallNextHookEx(nullptr, nCode, wParam, lParam);
}

LRESULT CALLBACK PluginModuleChild::NestedInputEventHook(int nCode,
                                                         WPARAM wParam,
                                                         LPARAM lParam) {
  PluginModuleChild* self = GetChrome();
  uint32_t len = self->mIncallPumpingStack.Length();
  if (nCode >= 0 && len && !self->mIncallPumpingStack[len - 1]._spinning) {
    MessageLoop* loop = MessageLoop::current();
    self->SendProcessNativeEventsInInterruptCall();
    IncallFrame& f = self->mIncallPumpingStack[len - 1];
    f._spinning = true;
    f._savedNestableTasksAllowed = loop->NestableTasksAllowed();
    loop->SetNestableTasksAllowed(true);
    loop->set_os_modal_loop(true);
  }

  return CallNextHookEx(nullptr, nCode, wParam, lParam);
}

void PluginModuleChild::SetEventHooks() {
  NS_ASSERTION(
      !mNestedEventHook,
      "mNestedEventHook already setup in call to SetNestedInputEventHook?");
  NS_ASSERTION(
      !mGlobalCallWndProcHook,
      "mGlobalCallWndProcHook already setup in call to CallWindowProcHook?");

  PLUGIN_LOG_DEBUG(("%s", FULLFUNCTION));

  // WH_MSGFILTER event hook for detecting modal loops in the child.
  mNestedEventHook = SetWindowsHookEx(WH_MSGFILTER, NestedInputEventHook,
                                      nullptr, GetCurrentThreadId());

  // WH_CALLWNDPROC event hook for trapping sync messages sent from
  // parent that can cause deadlocks.
  mGlobalCallWndProcHook = SetWindowsHookEx(WH_CALLWNDPROC, CallWindowProcHook,
                                            nullptr, GetCurrentThreadId());
}

void PluginModuleChild::ResetEventHooks() {
  PLUGIN_LOG_DEBUG(("%s", FULLFUNCTION));
  if (mNestedEventHook) UnhookWindowsHookEx(mNestedEventHook);
  mNestedEventHook = nullptr;
  if (mGlobalCallWndProcHook) UnhookWindowsHookEx(mGlobalCallWndProcHook);
  mGlobalCallWndProcHook = nullptr;
}
#endif

mozilla::ipc::IPCResult
PluginModuleChild::RecvProcessNativeEventsInInterruptCall() {
  PLUGIN_LOG_DEBUG(("%s", FULLFUNCTION));
#if defined(OS_WIN)
  ProcessNativeEventsInInterruptCall();
  return IPC_OK();
#else
  MOZ_CRASH(
      "PluginModuleChild::RecvProcessNativeEventsInInterruptCall not "
      "implemented!");
  return IPC_FAIL_NO_REASON(this);
#endif
}

#ifdef MOZ_WIDGET_COCOA
void PluginModuleChild::ProcessNativeEvents() { CallProcessSomeEvents(); }
#endif

NPError PluginModuleChild::PluginRequiresAudioDeviceChanges(
    PluginInstanceChild* aInstance, NPBool aShouldRegister) {
#ifdef XP_WIN
  // Maintain a set of PluginInstanceChildren that we need to tell when the
  // default audio device has changed.
  NPError rv = NPERR_NO_ERROR;
  if (aShouldRegister) {
    if (mAudioNotificationSet.IsEmpty()) {
      // We are registering the first plugin.  Notify the PluginModuleParent
      // that it needs to start sending us audio device notifications.
      if (!CallNPN_SetValue_NPPVpluginRequiresAudioDeviceChanges(
              aShouldRegister, &rv)) {
        return NPERR_GENERIC_ERROR;
      }
    }
    if (rv == NPERR_NO_ERROR) {
      mAudioNotificationSet.PutEntry(aInstance);
    }
  } else if (!mAudioNotificationSet.IsEmpty()) {
    mAudioNotificationSet.RemoveEntry(aInstance);
    if (mAudioNotificationSet.IsEmpty()) {
      // We released the last plugin.  Unregister from the PluginModuleParent.
      if (!CallNPN_SetValue_NPPVpluginRequiresAudioDeviceChanges(
              aShouldRegister, &rv)) {
        return NPERR_GENERIC_ERROR;
      }
    }
  }
  return rv;
#else
  MOZ_CRASH(
      "PluginRequiresAudioDeviceChanges is not available on this platform.");
#endif  // XP_WIN
}

mozilla::ipc::IPCResult
PluginModuleChild::RecvNPP_SetValue_NPNVaudioDeviceChangeDetails(
    const NPAudioDeviceChangeDetailsIPC& detailsIPC) {
#if defined(XP_WIN)
  NPAudioDeviceChangeDetails details;
  details.flow = detailsIPC.flow;
  details.role = detailsIPC.role;
  details.defaultDevice = detailsIPC.defaultDevice.c_str();
  for (auto iter = mAudioNotificationSet.ConstIter(); !iter.Done();
       iter.Next()) {
    PluginInstanceChild* pluginInst = iter.Get()->GetKey();
    pluginInst->DefaultAudioDeviceChanged(details);
  }
  return IPC_OK();
#else
  MOZ_CRASH(
      "NPP_SetValue_NPNVaudioDeviceChangeDetails is a Windows-only message");
#endif
}

mozilla::ipc::IPCResult
PluginModuleChild::RecvNPP_SetValue_NPNVaudioDeviceStateChanged(
    const NPAudioDeviceStateChangedIPC& aDeviceStateIPC) {
#if defined(XP_WIN)
  NPAudioDeviceStateChanged stateChange;
  stateChange.newState = aDeviceStateIPC.state;
  stateChange.device = aDeviceStateIPC.device.c_str();
  for (auto iter = mAudioNotificationSet.ConstIter(); !iter.Done();
       iter.Next()) {
    PluginInstanceChild* pluginInst = iter.Get()->GetKey();
    pluginInst->AudioDeviceStateChanged(stateChange);
  }
  return IPC_OK();
#else
  MOZ_CRASH("NPP_SetValue_NPNVaudioDeviceRemoved is a Windows-only message");
#endif
}