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
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "base/basictypes.h"
/* This must occur *after* layers/PLayerTransaction.h to avoid typedefs conflicts. */
#include "mozilla/ArrayUtils.h"
#include "pratom.h"
#include "prmem.h"
#include "prenv.h"
#include "prclist.h"
#include "jsfriendapi.h"
#include "nsPluginHost.h"
#include "nsNPAPIPlugin.h"
#include "nsNPAPIPluginInstance.h"
#include "nsNPAPIPluginStreamListener.h"
#include "nsPluginStreamListenerPeer.h"
#include "nsIServiceManager.h"
#include "nsThreadUtils.h"
#include "mozilla/Preferences.h"
#include "nsPluginInstanceOwner.h"
#include "nsPluginsDir.h"
#include "nsPluginLogging.h"
#include "nsIDOMElement.h"
#include "nsPIDOMWindow.h"
#include "nsGlobalWindow.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsIIDNService.h"
#include "nsIScriptGlobalObject.h"
#include "nsIScriptContext.h"
#include "nsDOMJSUtils.h"
#include "nsIPrincipal.h"
#include "nsWildCard.h"
#include "nsContentUtils.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsIXULRuntime.h"
#include "nsIXPConnect.h"
#include "nsIObserverService.h"
#include <prinrval.h>
#ifdef MOZ_WIDGET_COCOA
#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>
#include <OpenGL/OpenGL.h>
#include "nsCocoaFeatures.h"
#include "PluginUtilsOSX.h"
#endif
// needed for nppdf plugin
#if (MOZ_WIDGET_GTK)
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#if (MOZ_WIDGET_GTK == 2)
#include "gtk2xtbin.h"
#endif
#endif
#include "nsJSUtils.h"
#include "nsJSNPRuntime.h"
#include "nsIHttpAuthManager.h"
#include "nsICookieService.h"
#include "nsILoadContext.h"
#include "nsIDocShell.h"
#include "nsNetUtil.h"
#include "nsNetCID.h"
#include "mozilla/Mutex.h"
#include "mozilla/PluginLibrary.h"
using mozilla::PluginLibrary;
#include "mozilla/PluginPRLibrary.h"
using mozilla::PluginPRLibrary;
#include "mozilla/plugins/PluginModuleParent.h"
using mozilla::plugins::PluginModuleChromeParent;
using mozilla::plugins::PluginModuleContentParent;
#ifdef MOZ_X11
#include "mozilla/X11Util.h"
#endif
#ifdef XP_WIN
#include <windows.h>
#include "mozilla/WindowsVersion.h"
#ifdef ACCESSIBILITY
#include "mozilla/a11y/Compatibility.h"
#endif
#endif
#ifdef MOZ_WIDGET_ANDROID
#include <android/log.h>
#include "android_npapi.h"
#include "ANPBase.h"
#include "GeneratedJNIWrappers.h"
#undef LOG
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GeckoPlugins" , ## args)
#endif
#include "nsIAudioChannelAgent.h"
#include "AudioChannelService.h"
using namespace mozilla;
using namespace mozilla::plugins::parent;
// We should make this const...
static NPNetscapeFuncs sBrowserFuncs = {
sizeof(sBrowserFuncs),
(NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR,
_geturl,
_posturl,
_requestread,
_newstream,
_write,
_destroystream,
_status,
_useragent,
_memalloc,
_memfree,
_memflush,
_reloadplugins,
_getJavaEnv,
_getJavaPeer,
_geturlnotify,
_posturlnotify,
_getvalue,
_setvalue,
_invalidaterect,
_invalidateregion,
_forceredraw,
_getstringidentifier,
_getstringidentifiers,
_getintidentifier,
_identifierisstring,
_utf8fromidentifier,
_intfromidentifier,
_createobject,
_retainobject,
_releaseobject,
_invoke,
_invokeDefault,
_evaluate,
_getproperty,
_setproperty,
_removeproperty,
_hasproperty,
_hasmethod,
_releasevariantvalue,
_setexception,
_pushpopupsenabledstate,
_poppopupsenabledstate,
_enumerate,
_pluginthreadasynccall,
_construct,
_getvalueforurl,
_setvalueforurl,
_getauthenticationinfo,
_scheduletimer,
_unscheduletimer,
_popupcontextmenu,
_convertpoint,
nullptr, // handleevent, unimplemented
nullptr, // unfocusinstance, unimplemented
_urlredirectresponse,
_initasyncsurface,
_finalizeasyncsurface,
_setcurrentasyncsurface
};
static Mutex *sPluginThreadAsyncCallLock = nullptr;
static PRCList sPendingAsyncCalls = PR_INIT_STATIC_CLIST(&sPendingAsyncCalls);
// POST/GET stream type
enum eNPPStreamTypeInternal {
eNPPStreamTypeInternal_Get,
eNPPStreamTypeInternal_Post
};
void NS_NotifyBeginPluginCall(NSPluginCallReentry aReentryState)
{
nsNPAPIPluginInstance::BeginPluginCall(aReentryState);
}
void NS_NotifyPluginCall(NSPluginCallReentry aReentryState)
{
nsNPAPIPluginInstance::EndPluginCall(aReentryState);
}
static void CheckClassInitialized()
{
static bool initialized = false;
if (initialized)
return;
if (!sPluginThreadAsyncCallLock)
sPluginThreadAsyncCallLock = new Mutex("nsNPAPIPlugin.sPluginThreadAsyncCallLock");
initialized = true;
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,("NPN callbacks initialized\n"));
}
nsNPAPIPlugin::nsNPAPIPlugin()
{
memset((void*)&mPluginFuncs, 0, sizeof(mPluginFuncs));
mPluginFuncs.size = sizeof(mPluginFuncs);
mPluginFuncs.version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
mLibrary = nullptr;
}
nsNPAPIPlugin::~nsNPAPIPlugin()
{
delete mLibrary;
mLibrary = nullptr;
}
void
nsNPAPIPlugin::PluginCrashed(const nsAString& pluginDumpID,
const nsAString& browserDumpID)
{
RefPtr<nsPluginHost> host = nsPluginHost::GetInst();
host->PluginCrashed(this, pluginDumpID, browserDumpID);
}
bool
nsNPAPIPlugin::RunPluginOOP(const nsPluginTag *aPluginTag)
{
#ifdef MOZ_WIDGET_ANDROID
return false;
#else
return true;
#endif
}
inline PluginLibrary*
GetNewPluginLibrary(nsPluginTag *aPluginTag)
{
PROFILER_LABEL_FUNC(js::ProfileEntry::Category::OTHER);
if (!aPluginTag) {
return nullptr;
}
if (XRE_IsContentProcess()) {
return PluginModuleContentParent::LoadModule(aPluginTag->mId, aPluginTag);
}
if (nsNPAPIPlugin::RunPluginOOP(aPluginTag)) {
return PluginModuleChromeParent::LoadModule(aPluginTag->mFullPath.get(), aPluginTag->mId, aPluginTag);
}
return new PluginPRLibrary(aPluginTag->mFullPath.get(), aPluginTag->mLibrary);
}
// Creates an nsNPAPIPlugin object. One nsNPAPIPlugin object exists per plugin (not instance).
nsresult
nsNPAPIPlugin::CreatePlugin(nsPluginTag *aPluginTag, nsNPAPIPlugin** aResult)
{
PROFILER_LABEL_FUNC(js::ProfileEntry::Category::OTHER);
*aResult = nullptr;
if (!aPluginTag) {
return NS_ERROR_FAILURE;
}
CheckClassInitialized();
RefPtr<nsNPAPIPlugin> plugin = new nsNPAPIPlugin();
PluginLibrary* pluginLib = GetNewPluginLibrary(aPluginTag);
if (!pluginLib) {
return NS_ERROR_FAILURE;
}
#if defined(XP_MACOSX) || defined(MOZ_WIDGET_ANDROID)
if (!pluginLib->HasRequiredFunctions()) {
NS_WARNING("Not all necessary functions exposed by plugin, it will not load.");
delete pluginLib;
return NS_ERROR_FAILURE;
}
#endif
plugin->mLibrary = pluginLib;
pluginLib->SetPlugin(plugin);
// Exchange NPAPI entry points.
#if defined(XP_WIN)
// NP_GetEntryPoints must be called before NP_Initialize on Windows.
NPError pluginCallError;
nsresult rv = pluginLib->NP_GetEntryPoints(&plugin->mPluginFuncs, &pluginCallError);
if (rv != NS_OK || pluginCallError != NPERR_NO_ERROR) {
return NS_ERROR_FAILURE;
}
// NP_Initialize must be called after NP_GetEntryPoints on Windows.
rv = pluginLib->NP_Initialize(&sBrowserFuncs, &pluginCallError);
if (rv != NS_OK || pluginCallError != NPERR_NO_ERROR) {
return NS_ERROR_FAILURE;
}
#elif defined(XP_MACOSX)
// NP_Initialize must be called before NP_GetEntryPoints on Mac OS X.
// We need to match WebKit's behavior.
NPError pluginCallError;
nsresult rv = pluginLib->NP_Initialize(&sBrowserFuncs, &pluginCallError);
if (rv != NS_OK || pluginCallError != NPERR_NO_ERROR) {
return NS_ERROR_FAILURE;
}
rv = pluginLib->NP_GetEntryPoints(&plugin->mPluginFuncs, &pluginCallError);
if (rv != NS_OK || pluginCallError != NPERR_NO_ERROR) {
return NS_ERROR_FAILURE;
}
#else
NPError pluginCallError;
nsresult rv = pluginLib->NP_Initialize(&sBrowserFuncs, &plugin->mPluginFuncs, &pluginCallError);
if (rv != NS_OK || pluginCallError != NPERR_NO_ERROR) {
return NS_ERROR_FAILURE;
}
#endif
plugin.forget(aResult);
return NS_OK;
}
PluginLibrary*
nsNPAPIPlugin::GetLibrary()
{
return mLibrary;
}
NPPluginFuncs*
nsNPAPIPlugin::PluginFuncs()
{
return &mPluginFuncs;
}
nsresult
nsNPAPIPlugin::Shutdown()
{
NPP_PLUGIN_LOG(PLUGIN_LOG_BASIC,
("NPP Shutdown to be called: this=%p\n", this));
NPError shutdownError;
mLibrary->NP_Shutdown(&shutdownError);
return NS_OK;
}
nsresult
nsNPAPIPlugin::RetainStream(NPStream *pstream, nsISupports **aRetainedPeer)
{
if (!aRetainedPeer)
return NS_ERROR_NULL_POINTER;
*aRetainedPeer = nullptr;
if (!pstream || !pstream->ndata)
return NS_ERROR_NULL_POINTER;
nsNPAPIStreamWrapper* streamWrapper = static_cast<nsNPAPIStreamWrapper*>(pstream->ndata);
nsNPAPIPluginStreamListener* listener = streamWrapper->GetStreamListener();
if (!listener) {
return NS_ERROR_NULL_POINTER;
}
nsIStreamListener* streamListener = listener->GetStreamListenerPeer();
if (!streamListener) {
return NS_ERROR_NULL_POINTER;
}
*aRetainedPeer = streamListener;
NS_ADDREF(*aRetainedPeer);
return NS_OK;
}
// Create a new NPP GET or POST (given in the type argument) url
// stream that may have a notify callback
NPError
MakeNewNPAPIStreamInternal(NPP npp, const char *relativeURL, const char *target,
eNPPStreamTypeInternal type,
bool bDoNotify = false,
void *notifyData = nullptr, uint32_t len = 0,
const char *buf = nullptr, NPBool file = false)
{
if (!npp)
return NPERR_INVALID_INSTANCE_ERROR;
PluginDestructionGuard guard(npp);
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *) npp->ndata;
if (!inst || !inst->IsRunning())
return NPERR_INVALID_INSTANCE_ERROR;
nsCOMPtr<nsIPluginHost> pluginHostCOM = do_GetService(MOZ_PLUGIN_HOST_CONTRACTID);
nsPluginHost *pluginHost = static_cast<nsPluginHost*>(pluginHostCOM.get());
if (!pluginHost) {
return NPERR_GENERIC_ERROR;
}
RefPtr<nsNPAPIPluginStreamListener> listener;
// Set aCallNotify here to false. If pluginHost->GetURL or PostURL fail,
// the listener's destructor will do the notification while we are about to
// return a failure code.
// Call SetCallNotify(true) below after we are sure we cannot return a failure
// code.
if (!target) {
inst->NewStreamListener(relativeURL, notifyData,
getter_AddRefs(listener));
if (listener) {
listener->SetCallNotify(false);
}
}
switch (type) {
case eNPPStreamTypeInternal_Get:
{
if (NS_FAILED(pluginHost->GetURL(inst, relativeURL, target, listener,
nullptr, nullptr, false)))
return NPERR_GENERIC_ERROR;
break;
}
case eNPPStreamTypeInternal_Post:
{
if (NS_FAILED(pluginHost->PostURL(inst, relativeURL, len, buf, file,
target, listener, nullptr, nullptr,
false, 0, nullptr)))
return NPERR_GENERIC_ERROR;
break;
}
default:
NS_ERROR("how'd I get here");
}
if (listener) {
// SetCallNotify(bDoNotify) here, see comment above.
listener->SetCallNotify(bDoNotify);
}
return NPERR_NO_ERROR;
}
#if defined(MOZ_MEMORY_WINDOWS)
extern "C" size_t malloc_usable_size(const void *ptr);
#endif
namespace {
static char *gNPPException;
class nsPluginThreadRunnable : public Runnable,
public PRCList
{
public:
nsPluginThreadRunnable(NPP instance, PluginThreadCallback func,
void *userData);
virtual ~nsPluginThreadRunnable();
NS_IMETHOD Run();
bool IsForInstance(NPP instance)
{
return (mInstance == instance);
}
void Invalidate()
{
mFunc = nullptr;
}
bool IsValid()
{
return (mFunc != nullptr);
}
private:
NPP mInstance;
PluginThreadCallback mFunc;
void *mUserData;
};
static nsIDocument *
GetDocumentFromNPP(NPP npp)
{
NS_ENSURE_TRUE(npp, nullptr);
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)npp->ndata;
NS_ENSURE_TRUE(inst, nullptr);
PluginDestructionGuard guard(inst);
RefPtr<nsPluginInstanceOwner> owner = inst->GetOwner();
NS_ENSURE_TRUE(owner, nullptr);
nsCOMPtr<nsIDocument> doc;
owner->GetDocument(getter_AddRefs(doc));
return doc;
}
static already_AddRefed<nsIChannel>
GetChannelFromNPP(NPP npp)
{
nsCOMPtr<nsIDocument> doc = GetDocumentFromNPP(npp);
if (!doc)
return nullptr;
nsCOMPtr<nsPIDOMWindowOuter> domwindow = doc->GetWindow();
nsCOMPtr<nsIChannel> channel;
if (domwindow) {
nsCOMPtr<nsIDocShell> docShell = domwindow->GetDocShell();
if (docShell) {
docShell->GetCurrentDocumentChannel(getter_AddRefs(channel));
}
}
return channel.forget();
}
static NPIdentifier
doGetIdentifier(JSContext *cx, const NPUTF8* name)
{
NS_ConvertUTF8toUTF16 utf16name(name);
JSString *str = ::JS_AtomizeAndPinUCStringN(cx, utf16name.get(), utf16name.Length());
if (!str)
return nullptr;
return StringToNPIdentifier(cx, str);
}
#if defined(MOZ_MEMORY_WINDOWS)
BOOL
InHeap(HANDLE hHeap, LPVOID lpMem)
{
BOOL success = FALSE;
PROCESS_HEAP_ENTRY he;
he.lpData = nullptr;
while (HeapWalk(hHeap, &he) != 0) {
if (he.lpData == lpMem) {
success = TRUE;
break;
}
}
HeapUnlock(hHeap);
return success;
}
#endif
} /* anonymous namespace */
NPPExceptionAutoHolder::NPPExceptionAutoHolder()
: mOldException(gNPPException)
{
gNPPException = nullptr;
}
NPPExceptionAutoHolder::~NPPExceptionAutoHolder()
{
NS_ASSERTION(!gNPPException, "NPP exception not properly cleared!");
gNPPException = mOldException;
}
nsPluginThreadRunnable::nsPluginThreadRunnable(NPP instance,
PluginThreadCallback func,
void *userData)
: mInstance(instance), mFunc(func), mUserData(userData)
{
if (!sPluginThreadAsyncCallLock) {
// Failed to create lock, not much we can do here then...
mFunc = nullptr;
return;
}
PR_INIT_CLIST(this);
{
MutexAutoLock lock(*sPluginThreadAsyncCallLock);
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
if (!inst || !inst->IsRunning()) {
// The plugin was stopped, ignore this async call.
mFunc = nullptr;
return;
}
PR_APPEND_LINK(this, &sPendingAsyncCalls);
}
}
nsPluginThreadRunnable::~nsPluginThreadRunnable()
{
if (!sPluginThreadAsyncCallLock) {
return;
}
{
MutexAutoLock lock(*sPluginThreadAsyncCallLock);
PR_REMOVE_LINK(this);
}
}
NS_IMETHODIMP
nsPluginThreadRunnable::Run()
{
if (mFunc) {
PluginDestructionGuard guard(mInstance);
NS_TRY_SAFE_CALL_VOID(mFunc(mUserData), nullptr,
NS_PLUGIN_CALL_SAFE_TO_REENTER_GECKO);
}
return NS_OK;
}
void
OnPluginDestroy(NPP instance)
{
if (!sPluginThreadAsyncCallLock) {
return;
}
{
MutexAutoLock lock(*sPluginThreadAsyncCallLock);
if (PR_CLIST_IS_EMPTY(&sPendingAsyncCalls)) {
return;
}
nsPluginThreadRunnable *r =
(nsPluginThreadRunnable *)PR_LIST_HEAD(&sPendingAsyncCalls);
do {
if (r->IsForInstance(instance)) {
r->Invalidate();
}
r = (nsPluginThreadRunnable *)PR_NEXT_LINK(r);
} while (r != &sPendingAsyncCalls);
}
}
void
OnShutdown()
{
NS_ASSERTION(PR_CLIST_IS_EMPTY(&sPendingAsyncCalls),
"Pending async plugin call list not cleaned up!");
if (sPluginThreadAsyncCallLock) {
delete sPluginThreadAsyncCallLock;
sPluginThreadAsyncCallLock = nullptr;
}
}
AsyncCallbackAutoLock::AsyncCallbackAutoLock()
{
if (sPluginThreadAsyncCallLock) {
sPluginThreadAsyncCallLock->Lock();
}
}
AsyncCallbackAutoLock::~AsyncCallbackAutoLock()
{
if (sPluginThreadAsyncCallLock) {
sPluginThreadAsyncCallLock->Unlock();
}
}
NPP NPPStack::sCurrentNPP = nullptr;
const char *
PeekException()
{
return gNPPException;
}
void
PopException()
{
NS_ASSERTION(gNPPException, "Uh, no NPP exception to pop!");
if (gNPPException) {
free(gNPPException);
gNPPException = nullptr;
}
}
//
// Static callbacks that get routed back through the new C++ API
//
namespace mozilla {
namespace plugins {
namespace parent {
NPError
_geturl(NPP npp, const char* relativeURL, const char* target)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_geturl called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_GetURL: npp=%p, target=%s, url=%s\n", (void *)npp, target,
relativeURL));
PluginDestructionGuard guard(npp);
// Block Adobe Acrobat from loading URLs that are not http:, https:,
// or ftp: URLs if the given target is null.
if (!target && relativeURL &&
(strncmp(relativeURL, "http:", 5) != 0) &&
(strncmp(relativeURL, "https:", 6) != 0) &&
(strncmp(relativeURL, "ftp:", 4) != 0)) {
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *) npp->ndata;
const char *name = nullptr;
RefPtr<nsPluginHost> host = nsPluginHost::GetInst();
host->GetPluginName(inst, &name);
if (name && strstr(name, "Adobe") && strstr(name, "Acrobat")) {
return NPERR_NO_ERROR;
}
}
return MakeNewNPAPIStreamInternal(npp, relativeURL, target,
eNPPStreamTypeInternal_Get);
}
NPError
_geturlnotify(NPP npp, const char* relativeURL, const char* target,
void* notifyData)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_geturlnotify called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_GetURLNotify: npp=%p, target=%s, notify=%p, url=%s\n", (void*)npp,
target, notifyData, relativeURL));
PluginDestructionGuard guard(npp);
return MakeNewNPAPIStreamInternal(npp, relativeURL, target,
eNPPStreamTypeInternal_Get, true,
notifyData);
}
NPError
_posturlnotify(NPP npp, const char *relativeURL, const char *target,
uint32_t len, const char *buf, NPBool file, void *notifyData)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_posturlnotify called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
if (!buf)
return NPERR_INVALID_PARAM;
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_PostURLNotify: npp=%p, target=%s, len=%d, file=%d, "
"notify=%p, url=%s, buf=%s\n",
(void*)npp, target, len, file, notifyData, relativeURL,
buf));
PluginDestructionGuard guard(npp);
return MakeNewNPAPIStreamInternal(npp, relativeURL, target,
eNPPStreamTypeInternal_Post, true,
notifyData, len, buf, file);
}
NPError
_posturl(NPP npp, const char *relativeURL, const char *target,
uint32_t len, const char *buf, NPBool file)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_posturl called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_PostURL: npp=%p, target=%s, file=%d, len=%d, url=%s, "
"buf=%s\n",
(void*)npp, target, file, len, relativeURL, buf));
PluginDestructionGuard guard(npp);
return MakeNewNPAPIStreamInternal(npp, relativeURL, target,
eNPPStreamTypeInternal_Post, false, nullptr,
len, buf, file);
}
NPError
_newstream(NPP npp, NPMIMEType type, const char* target, NPStream* *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_newstream called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_NewStream: npp=%p, type=%s, target=%s\n", (void*)npp,
(const char *)type, target));
NPError err = NPERR_INVALID_INSTANCE_ERROR;
if (npp && npp->ndata) {
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance*)npp->ndata;
PluginDestructionGuard guard(inst);
nsCOMPtr<nsIOutputStream> stream;
if (NS_SUCCEEDED(inst->NewStreamFromPlugin((const char*) type, target,
getter_AddRefs(stream)))) {
nsNPAPIStreamWrapper* wrapper = new nsNPAPIStreamWrapper(stream, nullptr);
if (wrapper) {
(*result) = &wrapper->mNPStream;
err = NPERR_NO_ERROR;
} else {
err = NPERR_OUT_OF_MEMORY_ERROR;
}
} else {
err = NPERR_GENERIC_ERROR;
}
}
return err;
}
int32_t
_write(NPP npp, NPStream *pstream, int32_t len, void *buffer)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_write called from the wrong thread\n"));
return 0;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_Write: npp=%p, url=%s, len=%d, buffer=%s\n", (void*)npp,
pstream->url, len, (char*)buffer));
// negative return indicates failure to the plugin
if (!npp)
return -1;
PluginDestructionGuard guard(npp);
nsNPAPIStreamWrapper* wrapper = static_cast<nsNPAPIStreamWrapper*>(pstream->ndata);
if (!wrapper) {
return -1;
}
nsIOutputStream* stream = wrapper->GetOutputStream();
if (!stream) {
return -1;
}
uint32_t count = 0;
nsresult rv = stream->Write((char *)buffer, len, &count);
if (NS_FAILED(rv)) {
return -1;
}
return (int32_t)count;
}
NPError
_destroystream(NPP npp, NPStream *pstream, NPError reason)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_destroystream called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_DestroyStream: npp=%p, url=%s, reason=%d\n", (void*)npp,
pstream->url, (int)reason));
if (!npp)
return NPERR_INVALID_INSTANCE_ERROR;
PluginDestructionGuard guard(npp);
nsNPAPIStreamWrapper *streamWrapper = static_cast<nsNPAPIStreamWrapper*>(pstream->ndata);
if (!streamWrapper) {
return NPERR_INVALID_PARAM;
}
nsNPAPIPluginStreamListener *listener = streamWrapper->GetStreamListener();
if (listener) {
// This type of stream is going from the browser to the plugin. It's either the
// initial src/data stream or another stream resulting from NPN_GetURL* or
// NPN_PostURL*.
//
// Calling OnStopBinding on the listener may cause it to be deleted due to the
// releasing of its last references.
listener->OnStopBinding(nullptr, NS_BINDING_ABORTED);
} else {
// This type of stream (NPStream) was created via NPN_NewStream. The plugin holds
// the reference until it is to be deleted here. Deleting the wrapper will
// release the wrapped nsIOutputStream.
//
// The NPStream the plugin references should always be a sub-object of its own
// 'ndata', which is our nsNPAPIStramWrapper. See bug 548441.
NS_ASSERTION((char*)streamWrapper <= (char*)pstream &&
((char*)pstream) + sizeof(*pstream)
<= ((char*)streamWrapper) + sizeof(*streamWrapper),
"pstream is not a subobject of wrapper");
delete streamWrapper;
}
// 'listener' and/or 'streamWrapper' may be invalid (deleted) at this point. Don't
// touch them again!
return NPERR_NO_ERROR;
}
void
_status(NPP npp, const char *message)
{
// NPN_Status is no longer supported.
}
void
_memfree (void *ptr)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_memfree called from the wrong thread\n"));
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY, ("NPN_MemFree: ptr=%p\n", ptr));
if (ptr)
free(ptr);
}
uint32_t
_memflush(uint32_t size)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_memflush called from the wrong thread\n"));
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY, ("NPN_MemFlush: size=%d\n", size));
nsMemory::HeapMinimize(true);
return 0;
}
void
_reloadplugins(NPBool reloadPages)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_reloadplugins called from the wrong thread\n"));
return;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_ReloadPlugins: reloadPages=%d\n", reloadPages));
nsCOMPtr<nsIPluginHost> pluginHost(do_GetService(MOZ_PLUGIN_HOST_CONTRACTID));
if (!pluginHost)
return;
pluginHost->ReloadPlugins();
}
void
_invalidaterect(NPP npp, NPRect *invalidRect)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_invalidaterect called from the wrong thread\n"));
return;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_InvalidateRect: npp=%p, top=%d, left=%d, bottom=%d, "
"right=%d\n", (void *)npp, invalidRect->top,
invalidRect->left, invalidRect->bottom, invalidRect->right));
if (!npp || !npp->ndata) {
NS_WARNING("_invalidaterect: npp or npp->ndata == 0");
return;
}
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance*)npp->ndata;
PluginDestructionGuard guard(inst);
inst->InvalidateRect((NPRect *)invalidRect);
}
void
_invalidateregion(NPP npp, NPRegion invalidRegion)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_invalidateregion called from the wrong thread\n"));
return;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPN_InvalidateRegion: npp=%p, region=%p\n", (void*)npp,
(void*)invalidRegion));
if (!npp || !npp->ndata) {
NS_WARNING("_invalidateregion: npp or npp->ndata == 0");
return;
}
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance*)npp->ndata;
PluginDestructionGuard guard(inst);
inst->InvalidateRegion((NPRegion)invalidRegion);
}
void
_forceredraw(NPP npp)
{
}
NPObject*
_getwindowobject(NPP npp)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getwindowobject called from the wrong thread\n"));
return nullptr;
}
// The window want to return here is the outer window, *not* the inner (since
// we don't know what the plugin will do with it).
nsIDocument* doc = GetDocumentFromNPP(npp);
NS_ENSURE_TRUE(doc, nullptr);
nsCOMPtr<nsPIDOMWindowOuter> outer = doc->GetWindow();
NS_ENSURE_TRUE(outer, nullptr);
JS::Rooted<JSObject*> global(dom::RootingCx(),
nsGlobalWindow::Cast(outer)->GetGlobalJSObject());
return nsJSObjWrapper::GetNewOrUsed(npp, global);
}
NPObject*
_getpluginelement(NPP npp)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getpluginelement called from the wrong thread\n"));
return nullptr;
}
nsNPAPIPluginInstance* inst = static_cast<nsNPAPIPluginInstance*>(npp->ndata);
if (!inst)
return nullptr;
nsCOMPtr<nsIDOMElement> element;
inst->GetDOMElement(getter_AddRefs(element));
if (!element)
return nullptr;
nsIDocument *doc = GetDocumentFromNPP(npp);
if (NS_WARN_IF(!doc)) {
return nullptr;
}
dom::AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(doc->GetInnerWindow()))) {
return nullptr;
}
JSContext* cx = jsapi.cx();
nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID()));
NS_ENSURE_TRUE(xpc, nullptr);
JS::RootedObject obj(cx);
xpc->WrapNative(cx, ::JS::CurrentGlobalOrNull(cx), element,
NS_GET_IID(nsIDOMElement), obj.address());
NS_ENSURE_TRUE(obj, nullptr);
return nsJSObjWrapper::GetNewOrUsed(npp, obj);
}
NPIdentifier
_getstringidentifier(const NPUTF8* name)
{
if (!name) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS, ("NPN_getstringidentifier: passed null name"));
return nullptr;
}
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getstringidentifier called from the wrong thread\n"));
}
AutoSafeJSContext cx;
return doGetIdentifier(cx, name);
}
void
_getstringidentifiers(const NPUTF8** names, int32_t nameCount,
NPIdentifier *identifiers)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getstringidentifiers called from the wrong thread\n"));
}
AutoSafeJSContext cx;
for (int32_t i = 0; i < nameCount; ++i) {
if (names[i]) {
identifiers[i] = doGetIdentifier(cx, names[i]);
} else {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS, ("NPN_getstringidentifiers: passed null name"));
identifiers[i] = nullptr;
}
}
}
NPIdentifier
_getintidentifier(int32_t intid)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getstringidentifier called from the wrong thread\n"));
}
return IntToNPIdentifier(intid);
}
NPUTF8*
_utf8fromidentifier(NPIdentifier id)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_utf8fromidentifier called from the wrong thread\n"));
}
if (!id)
return nullptr;
if (!NPIdentifierIsString(id)) {
return nullptr;
}
JSString *str = NPIdentifierToString(id);
nsAutoString autoStr;
AssignJSFlatString(autoStr, JS_ASSERT_STRING_IS_FLAT(str));
return ToNewUTF8String(autoStr);
}
int32_t
_intfromidentifier(NPIdentifier id)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_intfromidentifier called from the wrong thread\n"));
}
if (!NPIdentifierIsInt(id)) {
return INT32_MIN;
}
return NPIdentifierToInt(id);
}
bool
_identifierisstring(NPIdentifier id)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_identifierisstring called from the wrong thread\n"));
}
return NPIdentifierIsString(id);
}
NPObject*
_createobject(NPP npp, NPClass* aClass)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_createobject called from the wrong thread\n"));
return nullptr;
}
if (!npp) {
NS_ERROR("Null npp passed to _createobject()!");
return nullptr;
}
PluginDestructionGuard guard(npp);
if (!aClass) {
NS_ERROR("Null class passed to _createobject()!");
return nullptr;
}
NPPAutoPusher nppPusher(npp);
NPObject *npobj;
if (aClass->allocate) {
npobj = aClass->allocate(npp, aClass);
} else {
npobj = (NPObject *)PR_Malloc(sizeof(NPObject));
}
if (npobj) {
npobj->_class = aClass;
npobj->referenceCount = 1;
NS_LOG_ADDREF(npobj, 1, "BrowserNPObject", sizeof(NPObject));
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("Created NPObject %p, NPClass %p\n", npobj, aClass));
return npobj;
}
NPObject*
_retainobject(NPObject* npobj)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_retainobject called from the wrong thread\n"));
}
if (npobj) {
#ifdef NS_BUILD_REFCNT_LOGGING
int32_t refCnt =
#endif
PR_ATOMIC_INCREMENT((int32_t*)&npobj->referenceCount);
NS_LOG_ADDREF(npobj, refCnt, "BrowserNPObject", sizeof(NPObject));
}
return npobj;
}
void
_releaseobject(NPObject* npobj)
{
// If nothing is passed, just return, even if we're on the wrong thread.
if (!npobj) {
return;
}
// THIS IS A KNOWN LEAK. SEE BUG 1221448.
// If releaseobject is called off the main thread and we have a valid pointer,
// we at least know it was created on the main thread (see _createobject
// implementation). However, forwarding the deletion back to the main thread
// without careful checking could cause bad memory management races. So, for
// now, we leak by warning and then just returning early. But it should fix
// java 7 crashes.
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_releaseobject called from the wrong thread\n"));
return;
}
int32_t refCnt = PR_ATOMIC_DECREMENT((int32_t*)&npobj->referenceCount);
NS_LOG_RELEASE(npobj, refCnt, "BrowserNPObject");
if (refCnt == 0) {
nsNPObjWrapper::OnDestroy(npobj);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("Deleting NPObject %p, refcount hit 0\n", npobj));
if (npobj->_class && npobj->_class->deallocate) {
npobj->_class->deallocate(npobj);
} else {
PR_Free(npobj);
}
}
}
bool
_invoke(NPP npp, NPObject* npobj, NPIdentifier method, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_invoke called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class || !npobj->_class->invoke)
return false;
PluginDestructionGuard guard(npp);
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_Invoke(npp %p, npobj %p, method %p, args %d\n", npp,
npobj, method, argCount));
return npobj->_class->invoke(npobj, method, args, argCount, result);
}
bool
_invokeDefault(NPP npp, NPObject* npobj, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_invokedefault called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class || !npobj->_class->invokeDefault)
return false;
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_InvokeDefault(npp %p, npobj %p, args %d\n", npp,
npobj, argCount));
return npobj->_class->invokeDefault(npobj, args, argCount, result);
}
bool
_evaluate(NPP npp, NPObject* npobj, NPString *script, NPVariant *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_evaluate called from the wrong thread\n"));
return false;
}
if (!npp)
return false;
NPPAutoPusher nppPusher(npp);
nsIDocument *doc = GetDocumentFromNPP(npp);
NS_ENSURE_TRUE(doc, false);
nsGlobalWindow* win = nsGlobalWindow::Cast(doc->GetInnerWindow());
if (NS_WARN_IF(!win || !win->FastGetGlobalJSObject())) {
return false;
}
nsAutoMicroTask mt;
dom::AutoEntryScript aes(win, "NPAPI NPN_evaluate");
JSContext* cx = aes.cx();
JS::Rooted<JSObject*> obj(cx, nsNPObjWrapper::GetNewOrUsed(npp, cx, npobj));
if (!obj) {
return false;
}
obj = js::ToWindowIfWindowProxy(obj);
MOZ_ASSERT(obj, "ToWindowIfWindowProxy should never return null");
if (result) {
// Initialize the out param to void
VOID_TO_NPVARIANT(*result);
}
if (!script || !script->UTF8Length || !script->UTF8Characters) {
// Nothing to evaluate.
return true;
}
NS_ConvertUTF8toUTF16 utf16script(script->UTF8Characters,
script->UTF8Length);
nsIPrincipal *principal = doc->NodePrincipal();
nsAutoCString specStr;
const char *spec;
nsCOMPtr<nsIURI> uri;
principal->GetURI(getter_AddRefs(uri));
if (uri) {
uri->GetSpec(specStr);
spec = specStr.get();
} else {
// No URI in a principal means it's the system principal. If the
// document URI is a chrome:// URI, pass that in as the URI of the
// script, else pass in null for the filename as there's no way to
// know where this document really came from. Passing in null here
// also means that the script gets treated by XPConnect as if it
// needs additional protection, which is what we want for unknown
// chrome code anyways.
uri = doc->GetDocumentURI();
bool isChrome = false;
if (uri && NS_SUCCEEDED(uri->SchemeIs("chrome", &isChrome)) && isChrome) {
uri->GetSpec(specStr);
spec = specStr.get();
} else {
spec = nullptr;
}
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_Evaluate(npp %p, npobj %p, script <<<%s>>>) called\n",
npp, npobj, script->UTF8Characters));
JS::CompileOptions options(cx);
options.setFileAndLine(spec, 0)
.setVersion(JSVERSION_DEFAULT);
JS::Rooted<JS::Value> rval(cx);
nsJSUtils::EvaluateOptions evalOptions(cx);
if (obj != js::GetGlobalForObjectCrossCompartment(obj) &&
!evalOptions.scopeChain.append(obj)) {
return false;
}
obj = js::GetGlobalForObjectCrossCompartment(obj);
nsresult rv = nsJSUtils::EvaluateString(cx, utf16script, obj, options,
evalOptions, &rval);
return NS_SUCCEEDED(rv) &&
(!result || JSValToNPVariant(npp, cx, rval, result));
}
bool
_getproperty(NPP npp, NPObject* npobj, NPIdentifier property,
NPVariant *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getproperty called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class || !npobj->_class->getProperty)
return false;
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_GetProperty(npp %p, npobj %p, property %p) called\n",
npp, npobj, property));
if (!npobj->_class->getProperty(npobj, property, result))
return false;
// If a Java plugin tries to get the document.URL or document.documentURI
// property from us, don't pass back a value that Java won't be able to
// understand -- one that will make the URL(String) constructor throw a
// MalformedURL exception. Passing such a value causes Java Plugin2 to
// crash (to throw a RuntimeException in Plugin2Manager.getDocumentBase()).
// Also don't pass back a value that Java is likely to mishandle.
nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*) npp->ndata;
if (!inst)
return false;
nsNPAPIPlugin* plugin = inst->GetPlugin();
if (!plugin)
return false;
RefPtr<nsPluginHost> host = nsPluginHost::GetInst();
nsPluginTag* pluginTag = host->TagForPlugin(plugin);
if (!pluginTag->mIsJavaPlugin)
return true;
if (!NPVARIANT_IS_STRING(*result))
return true;
NPUTF8* propertyName = _utf8fromidentifier(property);
if (!propertyName)
return true;
bool notURL =
(PL_strcasecmp(propertyName, "URL") &&
PL_strcasecmp(propertyName, "documentURI"));
_memfree(propertyName);
if (notURL)
return true;
NPObject* window_obj = _getwindowobject(npp);
if (!window_obj)
return true;
NPVariant doc_v;
NPObject* document_obj = nullptr;
NPIdentifier doc_id = _getstringidentifier("document");
bool ok = npobj->_class->getProperty(window_obj, doc_id, &doc_v);
_releaseobject(window_obj);
if (ok) {
if (NPVARIANT_IS_OBJECT(doc_v)) {
document_obj = NPVARIANT_TO_OBJECT(doc_v);
} else {
_releasevariantvalue(&doc_v);
return true;
}
} else {
return true;
}
_releaseobject(document_obj);
if (document_obj != npobj)
return true;
NPString urlnp = NPVARIANT_TO_STRING(*result);
nsXPIDLCString url;
url.Assign(urlnp.UTF8Characters, urlnp.UTF8Length);
bool javaCompatible = false;
if (NS_FAILED(NS_CheckIsJavaCompatibleURLString(url, &javaCompatible)))
javaCompatible = false;
if (javaCompatible)
return true;
// If Java won't be able to interpret the original value of document.URL or
// document.documentURI, or is likely to mishandle it, pass back something
// that Java will understand but won't be able to use to access the network,
// and for which same-origin checks will always fail.
if (inst->mFakeURL.IsVoid()) {
// Abort (do an error return) if NS_MakeRandomInvalidURLString() fails.
if (NS_FAILED(NS_MakeRandomInvalidURLString(inst->mFakeURL))) {
_releasevariantvalue(result);
return false;
}
}
_releasevariantvalue(result);
char* fakeurl = (char *) _memalloc(inst->mFakeURL.Length() + 1);
strcpy(fakeurl, inst->mFakeURL);
STRINGZ_TO_NPVARIANT(fakeurl, *result);
return true;
}
bool
_setproperty(NPP npp, NPObject* npobj, NPIdentifier property,
const NPVariant *value)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_setproperty called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class || !npobj->_class->setProperty)
return false;
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_SetProperty(npp %p, npobj %p, property %p) called\n",
npp, npobj, property));
return npobj->_class->setProperty(npobj, property, value);
}
bool
_removeproperty(NPP npp, NPObject* npobj, NPIdentifier property)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_removeproperty called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class || !npobj->_class->removeProperty)
return false;
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_RemoveProperty(npp %p, npobj %p, property %p) called\n",
npp, npobj, property));
return npobj->_class->removeProperty(npobj, property);
}
bool
_hasproperty(NPP npp, NPObject* npobj, NPIdentifier propertyName)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_hasproperty called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class || !npobj->_class->hasProperty)
return false;
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_HasProperty(npp %p, npobj %p, property %p) called\n",
npp, npobj, propertyName));
return npobj->_class->hasProperty(npobj, propertyName);
}
bool
_hasmethod(NPP npp, NPObject* npobj, NPIdentifier methodName)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_hasmethod called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class || !npobj->_class->hasMethod)
return false;
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_HasMethod(npp %p, npobj %p, property %p) called\n",
npp, npobj, methodName));
return npobj->_class->hasMethod(npobj, methodName);
}
bool
_enumerate(NPP npp, NPObject *npobj, NPIdentifier **identifier,
uint32_t *count)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_enumerate called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class)
return false;
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPN_Enumerate(npp %p, npobj %p) called\n", npp, npobj));
if (!NP_CLASS_STRUCT_VERSION_HAS_ENUM(npobj->_class) ||
!npobj->_class->enumerate) {
*identifier = 0;
*count = 0;
return true;
}
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
return npobj->_class->enumerate(npobj, identifier, count);
}
bool
_construct(NPP npp, NPObject* npobj, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_construct called from the wrong thread\n"));
return false;
}
if (!npp || !npobj || !npobj->_class ||
!NP_CLASS_STRUCT_VERSION_HAS_CTOR(npobj->_class) ||
!npobj->_class->construct) {
return false;
}
NPPExceptionAutoHolder nppExceptionHolder;
NPPAutoPusher nppPusher(npp);
return npobj->_class->construct(npobj, args, argCount, result);
}
void
_releasevariantvalue(NPVariant* variant)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_releasevariantvalue called from the wrong thread\n"));
}
switch (variant->type) {
case NPVariantType_Void :
case NPVariantType_Null :
case NPVariantType_Bool :
case NPVariantType_Int32 :
case NPVariantType_Double :
break;
case NPVariantType_String :
{
const NPString *s = &NPVARIANT_TO_STRING(*variant);
if (s->UTF8Characters) {
#if defined(MOZ_MEMORY_WINDOWS)
if (malloc_usable_size((void *)s->UTF8Characters) != 0) {
PR_Free((void *)s->UTF8Characters);
} else {
void *p = (void *)s->UTF8Characters;
DWORD nheaps = 0;
AutoTArray<HANDLE, 50> heaps;
nheaps = GetProcessHeaps(0, heaps.Elements());
heaps.AppendElements(nheaps);
GetProcessHeaps(nheaps, heaps.Elements());
for (DWORD i = 0; i < nheaps; i++) {
if (InHeap(heaps[i], p)) {
HeapFree(heaps[i], 0, p);
break;
}
}
}
#else
free((void *)s->UTF8Characters);
#endif
}
break;
}
case NPVariantType_Object:
{
NPObject *npobj = NPVARIANT_TO_OBJECT(*variant);
if (npobj)
_releaseobject(npobj);
break;
}
default:
NS_ERROR("Unknown NPVariant type!");
}
VOID_TO_NPVARIANT(*variant);
}
void
_setexception(NPObject* npobj, const NPUTF8 *message)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_setexception called from the wrong thread\n"));
return;
}
if (!message) return;
if (gNPPException) {
// If a plugin throws multiple exceptions, we'll only report the
// last one for now.
free(gNPPException);
}
gNPPException = strdup(message);
}
NPError
_getvalue(NPP npp, NPNVariable variable, void *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getvalue called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("NPN_GetValue: npp=%p, var=%d\n",
(void*)npp, (int)variable));
nsresult res;
PluginDestructionGuard guard(npp);
// Cast NPNVariable enum to int to avoid warnings about including switch
// cases for android_npapi.h's non-standard ANPInterface values.
switch (static_cast<int>(variable)) {
#if defined(XP_UNIX) && !defined(XP_MACOSX)
case NPNVxDisplay : {
#if defined(MOZ_X11)
if (npp) {
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *) npp->ndata;
bool windowless = false;
inst->IsWindowless(&windowless);
// The documentation on the types for many variables in NP(N|P)_GetValue
// is vague. Often boolean values are NPBool (1 byte), but
// https://developer.mozilla.org/en/XEmbed_Extension_for_Mozilla_Plugins
// treats NPPVpluginNeedsXEmbed as PRBool (int), and
// on x86/32-bit, flash stores to this using |movl 0x1,&needsXEmbed|.
// thus we can't use NPBool for needsXEmbed, or the three bytes above
// it on the stack would get clobbered. so protect with the larger bool.
int needsXEmbed = 0;
if (!windowless) {
res = inst->GetValueFromPlugin(NPPVpluginNeedsXEmbed, &needsXEmbed);
// If the call returned an error code make sure we still use our default value.
if (NS_FAILED(res)) {
needsXEmbed = 0;
}
}
if (windowless || needsXEmbed) {
(*(Display **)result) = mozilla::DefaultXDisplay();
return NPERR_NO_ERROR;
}
}
#if (MOZ_WIDGET_GTK == 2)
// adobe nppdf calls XtGetApplicationNameAndClass(display,
// &instance, &class) we have to init Xt toolkit before get
// XtDisplay just call gtk_xtbin_new(w,0) once
static GtkWidget *gtkXtBinHolder = 0;
if (!gtkXtBinHolder) {
gtkXtBinHolder = gtk_xtbin_new(gdk_get_default_root_window(),0);
// it crashes on destroy, let it leak
// gtk_widget_destroy(gtkXtBinHolder);
}
(*(Display **)result) = GTK_XTBIN(gtkXtBinHolder)->xtdisplay;
return NPERR_NO_ERROR;
#endif
#endif
return NPERR_GENERIC_ERROR;
}
case NPNVxtAppContext:
return NPERR_GENERIC_ERROR;
#endif
#if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
case NPNVnetscapeWindow: {
if (!npp || !npp->ndata)
return NPERR_INVALID_INSTANCE_ERROR;
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *) npp->ndata;
RefPtr<nsPluginInstanceOwner> owner = inst->GetOwner();
NS_ENSURE_TRUE(owner, NPERR_NO_ERROR);
if (NS_SUCCEEDED(owner->GetNetscapeWindow(result))) {
return NPERR_NO_ERROR;
}
return NPERR_GENERIC_ERROR;
}
#endif
case NPNVjavascriptEnabledBool: {
*(NPBool*)result = false;
bool js = false;
res = Preferences::GetBool("javascript.enabled", &js);
if (NS_SUCCEEDED(res)) {
*(NPBool*)result = js;
}
return NPERR_NO_ERROR;
}
case NPNVasdEnabledBool:
*(NPBool*)result = false;
return NPERR_NO_ERROR;
case NPNVisOfflineBool: {
bool offline = false;
nsCOMPtr<nsIIOService> ioservice =
do_GetService(NS_IOSERVICE_CONTRACTID, &res);
if (NS_SUCCEEDED(res))
res = ioservice->GetOffline(&offline);
if (NS_FAILED(res))
return NPERR_GENERIC_ERROR;
*(NPBool*)result = offline;
return NPERR_NO_ERROR;
}
case NPNVToolkit: {
#ifdef MOZ_WIDGET_GTK
*((NPNToolkitType*)result) = NPNVGtk2;
#endif
if (*(NPNToolkitType*)result)
return NPERR_NO_ERROR;
return NPERR_GENERIC_ERROR;
}
case NPNVSupportsXEmbedBool: {
#ifdef MOZ_WIDGET_GTK
*(NPBool*)result = true;
#else
*(NPBool*)result = false;
#endif
return NPERR_NO_ERROR;
}
case NPNVWindowNPObject: {
*(NPObject **)result = _getwindowobject(npp);
return *(NPObject **)result ? NPERR_NO_ERROR : NPERR_GENERIC_ERROR;
}
case NPNVPluginElementNPObject: {
*(NPObject **)result = _getpluginelement(npp);
return *(NPObject **)result ? NPERR_NO_ERROR : NPERR_GENERIC_ERROR;
}
case NPNVSupportsWindowless: {
#if defined(XP_WIN) || defined(XP_MACOSX) || \
(defined(MOZ_X11) && defined(MOZ_WIDGET_GTK))
*(NPBool*)result = true;
#else
*(NPBool*)result = false;
#endif
return NPERR_NO_ERROR;
}
case NPNVprivateModeBool: {
bool privacy;
nsNPAPIPluginInstance *inst = static_cast<nsNPAPIPluginInstance*>(npp->ndata);
if (!inst)
return NPERR_GENERIC_ERROR;
nsresult rv = inst->IsPrivateBrowsing(&privacy);
if (NS_FAILED(rv))
return NPERR_GENERIC_ERROR;
*(NPBool*)result = (NPBool)privacy;
return NPERR_NO_ERROR;
}
case NPNVdocumentOrigin: {
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)npp->ndata;
if (!inst) {
return NPERR_GENERIC_ERROR;
}
nsCOMPtr<nsIDOMElement> element;
inst->GetDOMElement(getter_AddRefs(element));
if (!element) {
return NPERR_GENERIC_ERROR;
}
nsCOMPtr<nsIContent> content(do_QueryInterface(element));
if (!content) {
return NPERR_GENERIC_ERROR;
}
nsIPrincipal* principal = content->NodePrincipal();
nsAutoString utf16Origin;
res = nsContentUtils::GetUTFOrigin(principal, utf16Origin);
if (NS_FAILED(res)) {
return NPERR_GENERIC_ERROR;
}
nsCOMPtr<nsIIDNService> idnService = do_GetService(NS_IDNSERVICE_CONTRACTID);
if (!idnService) {
return NPERR_GENERIC_ERROR;
}
// This is a bit messy: we convert to UTF-8 here, but then
// nsIDNService::Normalize will convert back to UTF-16 for processing,
// and back to UTF-8 again to return the result.
// Alternative: perhaps we should add a NormalizeUTF16 version of the API,
// and just convert to UTF-8 for the final return (resulting in one
// encoding form conversion instead of three).
NS_ConvertUTF16toUTF8 utf8Origin(utf16Origin);
nsAutoCString normalizedUTF8Origin;
res = idnService->Normalize(utf8Origin, normalizedUTF8Origin);
if (NS_FAILED(res)) {
return NPERR_GENERIC_ERROR;
}
*(char**)result = ToNewCString(normalizedUTF8Origin);
return *(char**)result ? NPERR_NO_ERROR : NPERR_GENERIC_ERROR;
}
#ifdef XP_MACOSX
case NPNVpluginDrawingModel: {
if (npp) {
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance*)npp->ndata;
if (inst) {
NPDrawingModel drawingModel;
inst->GetDrawingModel((int32_t*)&drawingModel);
*(NPDrawingModel*)result = drawingModel;
return NPERR_NO_ERROR;
}
}
return NPERR_GENERIC_ERROR;
}
#ifndef NP_NO_QUICKDRAW
case NPNVsupportsQuickDrawBool: {
*(NPBool*)result = false;
return NPERR_NO_ERROR;
}
#endif
case NPNVsupportsCoreGraphicsBool: {
*(NPBool*)result = true;
return NPERR_NO_ERROR;
}
case NPNVsupportsCoreAnimationBool: {
*(NPBool*)result = true;
return NPERR_NO_ERROR;
}
case NPNVsupportsInvalidatingCoreAnimationBool: {
*(NPBool*)result = true;
return NPERR_NO_ERROR;
}
case NPNVsupportsCompositingCoreAnimationPluginsBool: {
*(NPBool*)result = PR_TRUE;
return NPERR_NO_ERROR;
}
#ifndef NP_NO_CARBON
case NPNVsupportsCarbonBool: {
*(NPBool*)result = false;
return NPERR_NO_ERROR;
}
#endif
case NPNVsupportsCocoaBool: {
*(NPBool*)result = true;
return NPERR_NO_ERROR;
}
case NPNVsupportsUpdatedCocoaTextInputBool: {
*(NPBool*)result = true;
return NPERR_NO_ERROR;
}
#endif
#if defined(XP_MACOSX) || defined(XP_WIN)
case NPNVcontentsScaleFactor: {
nsNPAPIPluginInstance *inst =
(nsNPAPIPluginInstance *) (npp ? npp->ndata : nullptr);
double scaleFactor = inst ? inst->GetContentsScaleFactor() : 1.0;
*(double*)result = scaleFactor;
return NPERR_NO_ERROR;
}
#endif
case NPNVCSSZoomFactor: {
nsNPAPIPluginInstance *inst =
(nsNPAPIPluginInstance *) (npp ? npp->ndata : nullptr);
double scaleFactor = inst ? inst->GetCSSZoomFactor() : 1.0;
*(double*)result = scaleFactor;
return NPERR_NO_ERROR;
}
#ifdef MOZ_WIDGET_ANDROID
case kLogInterfaceV0_ANPGetValue: {
LOG("get log interface");
ANPLogInterfaceV0 *i = (ANPLogInterfaceV0 *) result;
InitLogInterface(i);
return NPERR_NO_ERROR;
}
case kBitmapInterfaceV0_ANPGetValue: {
LOG("get bitmap interface");
return NPERR_GENERIC_ERROR;
}
case kMatrixInterfaceV0_ANPGetValue: {
LOG("get matrix interface");
return NPERR_GENERIC_ERROR;
}
case kPathInterfaceV0_ANPGetValue: {
LOG("get path interface");
return NPERR_GENERIC_ERROR;
}
case kTypefaceInterfaceV0_ANPGetValue: {
LOG("get typeface interface");
ANPTypefaceInterfaceV0 *i = (ANPTypefaceInterfaceV0 *) result;
InitTypeFaceInterface(i);
return NPERR_NO_ERROR;
}
case kPaintInterfaceV0_ANPGetValue: {
LOG("get paint interface");
ANPPaintInterfaceV0 *i = (ANPPaintInterfaceV0 *) result;
InitPaintInterface(i);
return NPERR_NO_ERROR;
}
case kCanvasInterfaceV0_ANPGetValue: {
LOG("get canvas interface");
ANPCanvasInterfaceV0 *i = (ANPCanvasInterfaceV0 *) result;
InitCanvasInterface(i);
return NPERR_NO_ERROR;
}
case kWindowInterfaceV0_ANPGetValue: {
LOG("get window interface");
ANPWindowInterfaceV0 *i = (ANPWindowInterfaceV0 *) result;
InitWindowInterface(i);
return NPERR_NO_ERROR;
}
case kAudioTrackInterfaceV0_ANPGetValue: {
LOG("get audio interface");
ANPAudioTrackInterfaceV0 *i = (ANPAudioTrackInterfaceV0 *) result;
InitAudioTrackInterfaceV0(i);
return NPERR_NO_ERROR;
}
case kEventInterfaceV0_ANPGetValue: {
LOG("get event interface");
ANPEventInterfaceV0 *i = (ANPEventInterfaceV0 *) result;
InitEventInterface(i);
return NPERR_NO_ERROR;
}
case kSystemInterfaceV0_ANPGetValue: {
LOG("get system interface");
return NPERR_GENERIC_ERROR;
}
case kSurfaceInterfaceV0_ANPGetValue: {
LOG("get surface interface");
ANPSurfaceInterfaceV0 *i = (ANPSurfaceInterfaceV0 *) result;
InitSurfaceInterface(i);
return NPERR_NO_ERROR;
}
case kSupportedDrawingModel_ANPGetValue: {
LOG("get supported drawing model");
return NPERR_GENERIC_ERROR;
}
case kJavaContext_ANPGetValue: {
LOG("get java context");
auto ret = java::GeckoAppShell::GetContext();
if (!ret)
return NPERR_GENERIC_ERROR;
*static_cast<jobject*>(result) = ret.Forget();
return NPERR_NO_ERROR;
}
case kAudioTrackInterfaceV1_ANPGetValue: {
LOG("get audio interface v1");
ANPAudioTrackInterfaceV1 *i = (ANPAudioTrackInterfaceV1 *) result;
InitAudioTrackInterfaceV1(i);
return NPERR_NO_ERROR;
}
case kNativeWindowInterfaceV0_ANPGetValue: {
LOG("get native window interface v0");
ANPNativeWindowInterfaceV0* i = (ANPNativeWindowInterfaceV0 *) result;
InitNativeWindowInterface(i);
return NPERR_NO_ERROR;
}
case kOpenGLInterfaceV0_ANPGetValue: {
LOG("get openGL interface");
return NPERR_GENERIC_ERROR;
}
case kWindowInterfaceV1_ANPGetValue: {
LOG("get Window interface V1");
return NPERR_GENERIC_ERROR;
}
case kWindowInterfaceV2_ANPGetValue: {
LOG("get Window interface V2");
ANPWindowInterfaceV2 *i = (ANPWindowInterfaceV2 *) result;
InitWindowInterfaceV2(i);
return NPERR_NO_ERROR;
}
case kVideoInterfaceV0_ANPGetValue: {
LOG("get video interface V0");
return NPERR_GENERIC_ERROR;
}
case kVideoInterfaceV1_ANPGetValue: {
LOG("get video interface V1");
ANPVideoInterfaceV1 *i = (ANPVideoInterfaceV1*) result;
InitVideoInterfaceV1(i);
return NPERR_NO_ERROR;
}
case kSystemInterfaceV1_ANPGetValue: {
LOG("get system interface v1");
ANPSystemInterfaceV1* i = reinterpret_cast<ANPSystemInterfaceV1*>(result);
InitSystemInterfaceV1(i);
return NPERR_NO_ERROR;
}
case kSystemInterfaceV2_ANPGetValue: {
LOG("get system interface v2");
ANPSystemInterfaceV2* i = reinterpret_cast<ANPSystemInterfaceV2*>(result);
InitSystemInterfaceV2(i);
return NPERR_NO_ERROR;
}
#endif
// we no longer hand out any XPCOM objects
case NPNVDOMElement:
case NPNVDOMWindow:
case NPNVserviceManager:
// old XPCOM objects, no longer supported, but null out the out
// param to avoid crashing plugins that still try to use this.
*(nsISupports**)result = nullptr;
MOZ_FALLTHROUGH;
default:
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("NPN_getvalue unhandled get value: %d\n", variable));
return NPERR_GENERIC_ERROR;
}
}
NPError
_setvalue(NPP npp, NPPVariable variable, void *result)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_setvalue called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("NPN_SetValue: npp=%p, var=%d\n",
(void*)npp, (int)variable));
if (!npp)
return NPERR_INVALID_INSTANCE_ERROR;
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *) npp->ndata;
NS_ASSERTION(inst, "null instance");
if (!inst)
return NPERR_INVALID_INSTANCE_ERROR;
PluginDestructionGuard guard(inst);
// Cast NPNVariable enum to int to avoid warnings about including switch
// cases for android_npapi.h's non-standard ANPInterface values.
switch (static_cast<int>(variable)) {
// we should keep backward compatibility with NPAPI where the
// actual pointer value is checked rather than its content
// when passing booleans
case NPPVpluginWindowBool: {
#ifdef XP_MACOSX
// This setting doesn't apply to OS X (only to Windows and Unix/Linux).
// See https://developer.mozilla.org/En/NPN_SetValue#section_5. Return
// NPERR_NO_ERROR here to conform to other browsers' behavior on OS X
// (e.g. Safari and Opera).
return NPERR_NO_ERROR;
#else
NPBool bWindowless = (result == nullptr);
return inst->SetWindowless(bWindowless);
#endif
}
case NPPVpluginTransparentBool: {
NPBool bTransparent = (result != nullptr);
return inst->SetTransparent(bTransparent);
}
case NPPVjavascriptPushCallerBool: {
return NPERR_NO_ERROR;
}
case NPPVpluginKeepLibraryInMemory: {
NPBool bCached = (result != nullptr);
inst->SetCached(bCached);
return NPERR_NO_ERROR;
}
case NPPVpluginUsesDOMForCursorBool: {
bool useDOMForCursor = (result != nullptr);
return inst->SetUsesDOMForCursor(useDOMForCursor);
}
case NPPVpluginIsPlayingAudio: {
bool isMuted = !result;
nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*) npp->ndata;
MOZ_ASSERT(inst);
if (isMuted && !inst->HasAudioChannelAgent()) {
return NPERR_NO_ERROR;
}
nsCOMPtr<nsIAudioChannelAgent> agent;
nsresult rv = inst->GetOrCreateAudioChannelAgent(getter_AddRefs(agent));
if (NS_WARN_IF(NS_FAILED(rv))) {
return NPERR_NO_ERROR;
}
MOZ_ASSERT(agent);
if (isMuted) {
rv = agent->NotifyStoppedPlaying();
if (NS_WARN_IF(NS_FAILED(rv))) {
return NPERR_NO_ERROR;
}
} else {
dom::AudioPlaybackConfig config;
rv = agent->NotifyStartedPlaying(&config,
dom::AudioChannelService::AudibleState::eAudible);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NPERR_NO_ERROR;
}
rv = inst->WindowVolumeChanged(config.mVolume, config.mMuted);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NPERR_NO_ERROR;
}
// Since we only support for muting now, the implementation of suspend
// is equal to muting. Therefore, if we have already muted the plugin,
// then we don't need to call WindowSuspendChanged() again.
if (config.mMuted) {
return NPERR_NO_ERROR;
}
rv = inst->WindowSuspendChanged(config.mSuspend);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NPERR_NO_ERROR;
}
}
return NPERR_NO_ERROR;
}
#ifndef MOZ_WIDGET_ANDROID
// On android, their 'drawing model' uses the same constant!
case NPPVpluginDrawingModel: {
if (inst) {
inst->SetDrawingModel((NPDrawingModel)NS_PTR_TO_INT32(result));
return NPERR_NO_ERROR;
}
else {
return NPERR_GENERIC_ERROR;
}
}
#endif
#ifdef XP_MACOSX
case NPPVpluginEventModel: {
if (inst) {
inst->SetEventModel((NPEventModel)NS_PTR_TO_INT32(result));
return NPERR_NO_ERROR;
}
else {
return NPERR_GENERIC_ERROR;
}
}
#endif
#ifdef MOZ_WIDGET_ANDROID
case kRequestDrawingModel_ANPSetValue:
if (inst)
inst->SetANPDrawingModel(NS_PTR_TO_INT32(result));
return NPERR_NO_ERROR;
case kAcceptEvents_ANPSetValue:
return NPERR_NO_ERROR;
#endif
default:
return NPERR_GENERIC_ERROR;
}
}
NPError
_requestread(NPStream *pstream, NPByteRange *rangeList)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_requestread called from the wrong thread\n"));
return NPERR_INVALID_PARAM;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("NPN_RequestRead: stream=%p\n",
(void*)pstream));
#ifdef PLUGIN_LOGGING
for(NPByteRange * range = rangeList; range != nullptr; range = range->next)
MOZ_LOG(nsPluginLogging::gNPNLog,PLUGIN_LOG_NOISY,
("%i-%i", range->offset, range->offset + range->length - 1));
MOZ_LOG(nsPluginLogging::gNPNLog,PLUGIN_LOG_NOISY, ("\n\n"));
PR_LogFlush();
#endif
if (!pstream || !rangeList || !pstream->ndata)
return NPERR_INVALID_PARAM;
nsNPAPIStreamWrapper* streamWrapper = static_cast<nsNPAPIStreamWrapper*>(pstream->ndata);
nsNPAPIPluginStreamListener* streamlistener = streamWrapper->GetStreamListener();
if (!streamlistener) {
return NPERR_GENERIC_ERROR;
}
int32_t streamtype = NP_NORMAL;
streamlistener->GetStreamType(&streamtype);
if (streamtype != NP_SEEK)
return NPERR_STREAM_NOT_SEEKABLE;
if (!streamlistener->mStreamListenerPeer)
return NPERR_GENERIC_ERROR;
nsresult rv = streamlistener->mStreamListenerPeer->RequestRead((NPByteRange *)rangeList);
if (NS_FAILED(rv))
return NPERR_GENERIC_ERROR;
return NPERR_NO_ERROR;
}
// Deprecated, only stubbed out
void* /* OJI type: JRIEnv* */
_getJavaEnv()
{
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("NPN_GetJavaEnv\n"));
return nullptr;
}
const char *
_useragent(NPP npp)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_useragent called from the wrong thread\n"));
return nullptr;
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("NPN_UserAgent: npp=%p\n", (void*)npp));
nsCOMPtr<nsIPluginHost> pluginHostCOM(do_GetService(MOZ_PLUGIN_HOST_CONTRACTID));
nsPluginHost *pluginHost = static_cast<nsPluginHost*>(pluginHostCOM.get());
if (!pluginHost) {
return nullptr;
}
const char *retstr;
nsresult rv = pluginHost->UserAgent(&retstr);
if (NS_FAILED(rv))
return nullptr;
return retstr;
}
void *
_memalloc (uint32_t size)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL,("NPN_memalloc called from the wrong thread\n"));
}
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY, ("NPN_MemAlloc: size=%d\n", size));
return moz_xmalloc(size);
}
// Deprecated, only stubbed out
void* /* OJI type: jref */
_getJavaPeer(NPP npp)
{
NPN_PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("NPN_GetJavaPeer: npp=%p\n", (void*)npp));
return nullptr;
}
void
_pushpopupsenabledstate(NPP npp, NPBool enabled)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_pushpopupsenabledstate called from the wrong thread\n"));
return;
}
nsNPAPIPluginInstance *inst = npp ? (nsNPAPIPluginInstance *)npp->ndata : nullptr;
if (!inst)
return;
inst->PushPopupsEnabledState(enabled);
}
void
_poppopupsenabledstate(NPP npp)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_poppopupsenabledstate called from the wrong thread\n"));
return;
}
nsNPAPIPluginInstance *inst = npp ? (nsNPAPIPluginInstance *)npp->ndata : nullptr;
if (!inst)
return;
inst->PopPopupsEnabledState();
}
void
_pluginthreadasynccall(NPP instance, PluginThreadCallback func, void *userData)
{
if (NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,("NPN_pluginthreadasynccall called from the main thread\n"));
} else {
NPN_PLUGIN_LOG(PLUGIN_LOG_NOISY,("NPN_pluginthreadasynccall called from a non main thread\n"));
}
RefPtr<nsPluginThreadRunnable> evt =
new nsPluginThreadRunnable(instance, func, userData);
if (evt && evt->IsValid()) {
NS_DispatchToMainThread(evt);
}
}
NPError
_getvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
char **value, uint32_t *len)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getvalueforurl called from the wrong thread\n"));
return NPERR_GENERIC_ERROR;
}
if (!instance) {
return NPERR_INVALID_PARAM;
}
if (!url || !*url || !len) {
return NPERR_INVALID_URL;
}
*len = 0;
switch (variable) {
case NPNURLVProxy:
{
nsCOMPtr<nsIPluginHost> pluginHostCOM(do_GetService(MOZ_PLUGIN_HOST_CONTRACTID));
nsPluginHost *pluginHost = static_cast<nsPluginHost*>(pluginHostCOM.get());
if (pluginHost && NS_SUCCEEDED(pluginHost->FindProxyForURL(url, value))) {
*len = *value ? strlen(*value) : 0;
return NPERR_NO_ERROR;
}
break;
}
case NPNURLVCookie:
{
nsCOMPtr<nsICookieService> cookieService =
do_GetService(NS_COOKIESERVICE_CONTRACTID);
if (!cookieService)
return NPERR_GENERIC_ERROR;
// Make an nsURI from the url argument
nsCOMPtr<nsIURI> uri;
if (NS_FAILED(NS_NewURI(getter_AddRefs(uri), nsDependentCString(url)))) {
return NPERR_GENERIC_ERROR;
}
nsCOMPtr<nsIChannel> channel = GetChannelFromNPP(instance);
if (NS_FAILED(cookieService->GetCookieString(uri, channel, value)) ||
!*value) {
return NPERR_GENERIC_ERROR;
}
*len = strlen(*value);
return NPERR_NO_ERROR;
}
default:
// Fall through and return an error...
;
}
return NPERR_GENERIC_ERROR;
}
NPError
_setvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
const char *value, uint32_t len)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_setvalueforurl called from the wrong thread\n"));
return NPERR_GENERIC_ERROR;
}
if (!instance) {
return NPERR_INVALID_PARAM;
}
if (!url || !*url) {
return NPERR_INVALID_URL;
}
switch (variable) {
case NPNURLVCookie:
{
if (!value || 0 == len)
return NPERR_INVALID_PARAM;
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsIIOService> ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv))
return NPERR_GENERIC_ERROR;
nsCOMPtr<nsICookieService> cookieService = do_GetService(NS_COOKIESERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv))
return NPERR_GENERIC_ERROR;
nsCOMPtr<nsIURI> uriIn;
rv = ioService->NewURI(nsDependentCString(url), nullptr, nullptr, getter_AddRefs(uriIn));
if (NS_FAILED(rv))
return NPERR_GENERIC_ERROR;
nsCOMPtr<nsIChannel> channel = GetChannelFromNPP(instance);
char *cookie = (char*)value;
char c = cookie[len];
cookie[len] = '\0';
rv = cookieService->SetCookieString(uriIn, nullptr, cookie, channel);
cookie[len] = c;
if (NS_SUCCEEDED(rv))
return NPERR_NO_ERROR;
}
break;
case NPNURLVProxy:
// We don't support setting proxy values, fall through...
default:
// Fall through and return an error...
;
}
return NPERR_GENERIC_ERROR;
}
NPError
_getauthenticationinfo(NPP instance, const char *protocol, const char *host,
int32_t port, const char *scheme, const char *realm,
char **username, uint32_t *ulen, char **password,
uint32_t *plen)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_getauthenticationinfo called from the wrong thread\n"));
return NPERR_GENERIC_ERROR;
}
if (!instance || !protocol || !host || !scheme || !realm || !username ||
!ulen || !password || !plen)
return NPERR_INVALID_PARAM;
*username = nullptr;
*password = nullptr;
*ulen = 0;
*plen = 0;
nsDependentCString proto(protocol);
if (!proto.LowerCaseEqualsLiteral("http") &&
!proto.LowerCaseEqualsLiteral("https"))
return NPERR_GENERIC_ERROR;
nsCOMPtr<nsIHttpAuthManager> authManager =
do_GetService("@mozilla.org/network/http-auth-manager;1");
if (!authManager)
return NPERR_GENERIC_ERROR;
nsNPAPIPluginInstance *inst = static_cast<nsNPAPIPluginInstance*>(instance->ndata);
if (!inst)
return NPERR_GENERIC_ERROR;
bool authPrivate = false;
if (NS_FAILED(inst->IsPrivateBrowsing(&authPrivate)))
return NPERR_GENERIC_ERROR;
nsIDocument *doc = GetDocumentFromNPP(instance);
NS_ENSURE_TRUE(doc, NPERR_GENERIC_ERROR);
nsIPrincipal *principal = doc->NodePrincipal();
nsAutoString unused, uname16, pwd16;
if (NS_FAILED(authManager->GetAuthIdentity(proto, nsDependentCString(host),
port, nsDependentCString(scheme),
nsDependentCString(realm),
EmptyCString(), unused, uname16,
pwd16, authPrivate, principal))) {
return NPERR_GENERIC_ERROR;
}
NS_ConvertUTF16toUTF8 uname8(uname16);
NS_ConvertUTF16toUTF8 pwd8(pwd16);
*username = ToNewCString(uname8);
*ulen = *username ? uname8.Length() : 0;
*password = ToNewCString(pwd8);
*plen = *password ? pwd8.Length() : 0;
return NPERR_NO_ERROR;
}
uint32_t
_scheduletimer(NPP instance, uint32_t interval, NPBool repeat, PluginTimerFunc timerFunc)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_scheduletimer called from the wrong thread\n"));
return 0;
}
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
if (!inst)
return 0;
return inst->ScheduleTimer(interval, repeat, timerFunc);
}
void
_unscheduletimer(NPP instance, uint32_t timerID)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_unscheduletimer called from the wrong thread\n"));
return;
}
#ifdef MOZ_WIDGET_ANDROID
// Sometimes Flash calls this with a dead NPP instance. Ensure the one we have
// here is valid and maps to a nsNPAPIPluginInstance.
nsNPAPIPluginInstance *inst = nsNPAPIPluginInstance::GetFromNPP(instance);
#else
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
#endif
if (!inst)
return;
inst->UnscheduleTimer(timerID);
}
NPError
_popupcontextmenu(NPP instance, NPMenu* menu)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_popupcontextmenu called from the wrong thread\n"));
return 0;
}
#ifdef MOZ_WIDGET_COCOA
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
double pluginX, pluginY;
double screenX, screenY;
const NPCocoaEvent* currentEvent = static_cast<NPCocoaEvent*>(inst->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,
nullptr,
nullptr);
} 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)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_convertpoint called from the wrong thread\n"));
return 0;
}
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
if (!inst)
return false;
return inst->ConvertPoint(sourceX, sourceY, sourceSpace, destX, destY, destSpace);
}
void
_urlredirectresponse(NPP instance, void* notifyData, NPBool allow)
{
if (!NS_IsMainThread()) {
NPN_PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("NPN_convertpoint called from the wrong thread\n"));
return;
}
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
if (!inst) {
return;
}
inst->URLRedirectResponse(notifyData, allow);
}
NPError
_initasyncsurface(NPP instance, NPSize *size, NPImageFormat format, void *initData, NPAsyncSurface *surface)
{
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
if (!inst) {
return NPERR_GENERIC_ERROR;
}
return inst->InitAsyncSurface(size, format, initData, surface);
}
NPError
_finalizeasyncsurface(NPP instance, NPAsyncSurface *surface)
{
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
if (!inst) {
return NPERR_GENERIC_ERROR;
}
return inst->FinalizeAsyncSurface(surface);
}
void
_setcurrentasyncsurface(NPP instance, NPAsyncSurface *surface, NPRect *changed)
{
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)instance->ndata;
if (!inst) {
return;
}
inst->SetCurrentAsyncSurface(surface, changed);
}
} /* namespace parent */
} /* namespace plugins */
} /* namespace mozilla */
|