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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/memory/linked_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_offset_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "cc/layers/texture_layer.h"
#include "ppapi/c/dev/ppb_find_dev.h"
#include "ppapi/c/dev/ppb_zoom_dev.h"
#include "ppapi/c/dev/ppp_find_dev.h"
#include "ppapi/c/dev/ppp_selection_dev.h"
#include "ppapi/c/dev/ppp_text_input_dev.h"
#include "ppapi/c/dev/ppp_zoom_dev.h"
#include "ppapi/c/pp_rect.h"
#include "ppapi/c/ppb_audio_config.h"
#include "ppapi/c/ppb_core.h"
#include "ppapi/c/ppb_gamepad.h"
#include "ppapi/c/ppp_input_event.h"
#include "ppapi/c/ppp_instance.h"
#include "ppapi/c/ppp_messaging.h"
#include "ppapi/c/ppp_mouse_lock.h"
#include "ppapi/c/private/ppp_instance_private.h"
#include "ppapi/shared_impl/ppapi_permissions.h"
#include "ppapi/shared_impl/ppapi_preferences.h"
#include "ppapi/shared_impl/ppb_gamepad_shared.h"
#include "ppapi/shared_impl/ppb_input_event_shared.h"
#include "ppapi/shared_impl/ppb_url_util_shared.h"
#include "ppapi/shared_impl/ppb_view_shared.h"
#include "ppapi/shared_impl/ppp_instance_combined.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/scoped_pp_resource.h"
#include "ppapi/shared_impl/time_conversion.h"
#include "ppapi/shared_impl/url_request_info_data.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_buffer_api.h"
#include "printing/metafile.h"
#include "printing/metafile_skia_wrapper.h"
#include "printing/units.h"
#include "skia/ext/platform_canvas.h"
#include "skia/ext/platform_device.h"
#include "third_party/WebKit/public/platform/WebGamepads.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/public/platform/WebURLError.h"
#include "third_party/WebKit/public/platform/WebURLRequest.h"
#include "third_party/WebKit/public/web/WebBindings.h"
#include "third_party/WebKit/public/web/WebCompositionUnderline.h"
#include "third_party/WebKit/public/web/WebCursorInfo.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "third_party/WebKit/public/web/WebPluginContainer.h"
#include "third_party/WebKit/public/web/WebPrintParams.h"
#include "third_party/WebKit/public/web/WebPrintScalingOption.h"
#include "third_party/WebKit/public/web/WebScopedUserGesture.h"
#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
#include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/base/range/range.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#include "v8/include/v8.h"
#include "webkit/plugins/plugin_constants.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/content_decryptor_delegate.h"
#include "webkit/plugins/ppapi/event_conversion.h"
#include "webkit/plugins/ppapi/fullscreen_container.h"
#include "webkit/plugins/ppapi/gfx_conversion.h"
#include "webkit/plugins/ppapi/host_globals.h"
#include "webkit/plugins/ppapi/message_channel.h"
#include "webkit/plugins/ppapi/npapi_glue.h"
#include "webkit/plugins/ppapi/plugin_module.h"
#include "webkit/plugins/ppapi/plugin_object.h"
#include "webkit/plugins/ppapi/ppb_buffer_impl.h"
#include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
#include "webkit/plugins/ppapi/ppb_graphics_3d_impl.h"
#include "webkit/plugins/ppapi/ppb_image_data_impl.h"
#include "webkit/plugins/ppapi/ppp_pdf.h"
#include "webkit/plugins/ppapi/url_request_info_util.h"
#include "webkit/plugins/sad_plugin.h"
#include "webkit/renderer/compositor_bindings/web_layer_impl.h"
#if defined(OS_MACOSX)
#include "printing/metafile_impl.h"
#endif // defined(OS_MACOSX)
#if defined(OS_WIN)
#include "base/metrics/histogram.h"
#include "base/win/windows_version.h"
#include "skia/ext/platform_canvas.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/gdi_util.h"
#endif
using base::StringPrintf;
using ppapi::InputEventData;
using ppapi::PpapiGlobals;
using ppapi::PPB_InputEvent_Shared;
using ppapi::PPB_View_Shared;
using ppapi::PPP_Instance_Combined;
using ppapi::Resource;
using ppapi::ScopedPPResource;
using ppapi::StringVar;
using ppapi::TrackedCallback;
using ppapi::thunk::EnterResourceNoLock;
using ppapi::thunk::PPB_Buffer_API;
using ppapi::thunk::PPB_Gamepad_API;
using ppapi::thunk::PPB_Graphics2D_API;
using ppapi::thunk::PPB_Graphics3D_API;
using ppapi::thunk::PPB_ImageData_API;
using ppapi::Var;
using ppapi::ArrayBufferVar;
using ppapi::ViewData;
using WebKit::WebBindings;
using WebKit::WebCanvas;
using WebKit::WebCursorInfo;
using WebKit::WebDocument;
using WebKit::WebElement;
using WebKit::WebFrame;
using WebKit::WebInputEvent;
using WebKit::WebPlugin;
using WebKit::WebPluginContainer;
using WebKit::WebPrintParams;
using WebKit::WebPrintScalingOption;
using WebKit::WebScopedUserGesture;
using WebKit::WebString;
using WebKit::WebURLError;
using WebKit::WebURLLoader;
using WebKit::WebURLLoaderClient;
using WebKit::WebURLRequest;
using WebKit::WebURLResponse;
using WebKit::WebUserGestureIndicator;
using WebKit::WebUserGestureToken;
using WebKit::WebView;
namespace webkit {
namespace ppapi {
#if defined(OS_WIN)
// Exported by pdf.dll
typedef bool (*RenderPDFPageToDCProc)(
const unsigned char* pdf_buffer, int buffer_size, int page_number, HDC dc,
int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y,
int bounds_width, int bounds_height, bool fit_to_bounds,
bool stretch_to_bounds, bool keep_aspect_ratio, bool center_in_bounds,
bool autorotate);
void DrawEmptyRectangle(HDC dc) {
// TODO(sanjeevr): This is a temporary hack. If we output a JPEG
// to the EMF, the EnumEnhMetaFile call fails in the browser
// process. The failure also happens if we output nothing here.
// We need to investigate the reason for this failure and fix it.
// In the meantime this temporary hack of drawing an empty
// rectangle in the DC gets us by.
Rectangle(dc, 0, 0, 0, 0);
}
#endif // defined(OS_WIN)
namespace {
// Check PP_TextInput_Type and ui::TextInputType are kept in sync.
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_NONE) == \
int(PP_TEXTINPUT_TYPE_NONE), mismatching_enums);
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_TEXT) == \
int(PP_TEXTINPUT_TYPE_TEXT), mismatching_enums);
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_PASSWORD) == \
int(PP_TEXTINPUT_TYPE_PASSWORD), mismatching_enums);
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_SEARCH) == \
int(PP_TEXTINPUT_TYPE_SEARCH), mismatching_enums);
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_EMAIL) == \
int(PP_TEXTINPUT_TYPE_EMAIL), mismatching_enums);
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_NUMBER) == \
int(PP_TEXTINPUT_TYPE_NUMBER), mismatching_enums);
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_TELEPHONE) == \
int(PP_TEXTINPUT_TYPE_TELEPHONE), mismatching_enums);
COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_URL) == \
int(PP_TEXTINPUT_TYPE_URL), mismatching_enums);
// The default text input type is to regard the plugin always accept text input.
// This is for allowing users to use input methods even on completely-IME-
// unaware plugins (e.g., PPAPI Flash or PDF plugin for M16).
// Plugins need to explicitly opt out the text input mode if they know
// that they don't accept texts.
const ui::TextInputType kPluginDefaultTextInputType = ui::TEXT_INPUT_TYPE_TEXT;
#define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, np_name) \
COMPILE_ASSERT(static_cast<int>(WebCursorInfo::webkit_name) \
== static_cast<int>(np_name), \
mismatching_enums)
#define COMPILE_ASSERT_PRINT_SCALING_MATCHING_ENUM(webkit_name, pp_name) \
COMPILE_ASSERT(static_cast<int>(webkit_name) \
== static_cast<int>(pp_name), \
mismatching_enums)
// <embed>/<object> attributes.
const char kWidth[] = "width";
const char kHeight[] = "height";
const char kBorder[] = "border"; // According to w3c, deprecated.
const char kStyle[] = "style";
COMPILE_ASSERT_MATCHING_ENUM(TypePointer, PP_MOUSECURSOR_TYPE_POINTER);
COMPILE_ASSERT_MATCHING_ENUM(TypeCross, PP_MOUSECURSOR_TYPE_CROSS);
COMPILE_ASSERT_MATCHING_ENUM(TypeHand, PP_MOUSECURSOR_TYPE_HAND);
COMPILE_ASSERT_MATCHING_ENUM(TypeIBeam, PP_MOUSECURSOR_TYPE_IBEAM);
COMPILE_ASSERT_MATCHING_ENUM(TypeWait, PP_MOUSECURSOR_TYPE_WAIT);
COMPILE_ASSERT_MATCHING_ENUM(TypeHelp, PP_MOUSECURSOR_TYPE_HELP);
COMPILE_ASSERT_MATCHING_ENUM(TypeEastResize, PP_MOUSECURSOR_TYPE_EASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthResize, PP_MOUSECURSOR_TYPE_NORTHRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthEastResize,
PP_MOUSECURSOR_TYPE_NORTHEASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthWestResize,
PP_MOUSECURSOR_TYPE_NORTHWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthResize, PP_MOUSECURSOR_TYPE_SOUTHRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthEastResize,
PP_MOUSECURSOR_TYPE_SOUTHEASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthWestResize,
PP_MOUSECURSOR_TYPE_SOUTHWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeWestResize, PP_MOUSECURSOR_TYPE_WESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthSouthResize,
PP_MOUSECURSOR_TYPE_NORTHSOUTHRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeEastWestResize,
PP_MOUSECURSOR_TYPE_EASTWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthEastSouthWestResize,
PP_MOUSECURSOR_TYPE_NORTHEASTSOUTHWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthWestSouthEastResize,
PP_MOUSECURSOR_TYPE_NORTHWESTSOUTHEASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeColumnResize,
PP_MOUSECURSOR_TYPE_COLUMNRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeRowResize, PP_MOUSECURSOR_TYPE_ROWRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeMiddlePanning,
PP_MOUSECURSOR_TYPE_MIDDLEPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeEastPanning, PP_MOUSECURSOR_TYPE_EASTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthPanning,
PP_MOUSECURSOR_TYPE_NORTHPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthEastPanning,
PP_MOUSECURSOR_TYPE_NORTHEASTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthWestPanning,
PP_MOUSECURSOR_TYPE_NORTHWESTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthPanning,
PP_MOUSECURSOR_TYPE_SOUTHPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthEastPanning,
PP_MOUSECURSOR_TYPE_SOUTHEASTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthWestPanning,
PP_MOUSECURSOR_TYPE_SOUTHWESTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeWestPanning, PP_MOUSECURSOR_TYPE_WESTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeMove, PP_MOUSECURSOR_TYPE_MOVE);
COMPILE_ASSERT_MATCHING_ENUM(TypeVerticalText,
PP_MOUSECURSOR_TYPE_VERTICALTEXT);
COMPILE_ASSERT_MATCHING_ENUM(TypeCell, PP_MOUSECURSOR_TYPE_CELL);
COMPILE_ASSERT_MATCHING_ENUM(TypeContextMenu, PP_MOUSECURSOR_TYPE_CONTEXTMENU);
COMPILE_ASSERT_MATCHING_ENUM(TypeAlias, PP_MOUSECURSOR_TYPE_ALIAS);
COMPILE_ASSERT_MATCHING_ENUM(TypeProgress, PP_MOUSECURSOR_TYPE_PROGRESS);
COMPILE_ASSERT_MATCHING_ENUM(TypeNoDrop, PP_MOUSECURSOR_TYPE_NODROP);
COMPILE_ASSERT_MATCHING_ENUM(TypeCopy, PP_MOUSECURSOR_TYPE_COPY);
COMPILE_ASSERT_MATCHING_ENUM(TypeNone, PP_MOUSECURSOR_TYPE_NONE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNotAllowed, PP_MOUSECURSOR_TYPE_NOTALLOWED);
COMPILE_ASSERT_MATCHING_ENUM(TypeZoomIn, PP_MOUSECURSOR_TYPE_ZOOMIN);
COMPILE_ASSERT_MATCHING_ENUM(TypeZoomOut, PP_MOUSECURSOR_TYPE_ZOOMOUT);
COMPILE_ASSERT_MATCHING_ENUM(TypeGrab, PP_MOUSECURSOR_TYPE_GRAB);
COMPILE_ASSERT_MATCHING_ENUM(TypeGrabbing, PP_MOUSECURSOR_TYPE_GRABBING);
// Do not assert WebCursorInfo::TypeCustom == PP_CURSORTYPE_CUSTOM;
// PP_CURSORTYPE_CUSTOM is pinned to allow new cursor types.
COMPILE_ASSERT_PRINT_SCALING_MATCHING_ENUM(WebKit::WebPrintScalingOptionNone,
PP_PRINTSCALINGOPTION_NONE);
COMPILE_ASSERT_PRINT_SCALING_MATCHING_ENUM(
WebKit::WebPrintScalingOptionFitToPrintableArea,
PP_PRINTSCALINGOPTION_FIT_TO_PRINTABLE_AREA);
COMPILE_ASSERT_PRINT_SCALING_MATCHING_ENUM(
WebKit::WebPrintScalingOptionSourceSize, PP_PRINTSCALINGOPTION_SOURCE_SIZE);
// Sets |*security_origin| to be the WebKit security origin associated with the
// document containing the given plugin instance. On success, returns true. If
// the instance is invalid, returns false and |*security_origin| will be
// unchanged.
bool SecurityOriginForInstance(PP_Instance instance_id,
WebKit::WebSecurityOrigin* security_origin) {
PluginInstance* instance = HostGlobals::Get()->GetInstance(instance_id);
if (!instance)
return false;
WebElement plugin_element = instance->container()->element();
*security_origin = plugin_element.document().securityOrigin();
return true;
}
// Convert the given vector to an array of C-strings. The strings in the
// returned vector are only guaranteed valid so long as the vector of strings
// is not modified.
scoped_ptr<const char*[]> StringVectorToArgArray(
const std::vector<std::string>& vector) {
scoped_ptr<const char*[]> array(new const char*[vector.size()]);
for (size_t i = 0; i < vector.size(); ++i)
array[i] = vector[i].c_str();
return array.Pass();
}
} // namespace
// static
PluginInstance* PluginInstance::Create(PluginDelegate* delegate,
content::RenderView* render_view,
PluginModule* module,
WebPluginContainer* container,
const GURL& plugin_url) {
base::Callback<const void*(const char*)> get_plugin_interface_func =
base::Bind(&PluginModule::GetPluginInterface, module);
PPP_Instance_Combined* ppp_instance_combined =
PPP_Instance_Combined::Create(get_plugin_interface_func);
if (!ppp_instance_combined)
return NULL;
return new PluginInstance(delegate, render_view, module,
ppp_instance_combined, container, plugin_url);
}
PluginInstance::NaClDocumentLoader::NaClDocumentLoader()
: finished_loading_(false) {
}
PluginInstance::NaClDocumentLoader::~NaClDocumentLoader(){
}
void PluginInstance::NaClDocumentLoader::ReplayReceivedData(
WebURLLoaderClient* document_loader) {
for (std::list<std::string>::iterator it = data_.begin();
it != data_.end(); ++it) {
document_loader->didReceiveData(NULL, it->c_str(), it->length(),
0 /* encoded_data_length */);
}
if (finished_loading_) {
document_loader->didFinishLoading(NULL,
0 /* finish_time */);
}
if (error_.get()) {
document_loader->didFail(NULL, *error_);
}
}
void PluginInstance::NaClDocumentLoader::didReceiveData(
WebURLLoader* loader,
const char* data,
int data_length,
int encoded_data_length) {
data_.push_back(std::string(data, data_length));
}
void PluginInstance::NaClDocumentLoader::didFinishLoading(
WebURLLoader* loader,
double finish_time) {
DCHECK(!finished_loading_);
finished_loading_ = true;
}
void PluginInstance::NaClDocumentLoader::didFail(
WebURLLoader* loader,
const WebURLError& error) {
DCHECK(!error_.get());
error_.reset(new WebURLError(error));
}
PluginInstance::GamepadImpl::GamepadImpl(PluginDelegate* delegate)
: Resource(::ppapi::Resource::Untracked()),
delegate_(delegate) {
}
PPB_Gamepad_API* PluginInstance::GamepadImpl::AsPPB_Gamepad_API() {
return this;
}
void PluginInstance::GamepadImpl::Sample(PP_Instance /* instance */,
PP_GamepadsSampleData* data) {
WebKit::WebGamepads webkit_data;
delegate_->SampleGamepads(&webkit_data);
ConvertWebKitGamepadData(
*reinterpret_cast<const ::ppapi::WebKitGamepads*>(&webkit_data), data);
}
PluginInstance::PluginInstance(
PluginDelegate* delegate,
content::RenderView* render_view,
PluginModule* module,
::ppapi::PPP_Instance_Combined* instance_interface,
WebPluginContainer* container,
const GURL& plugin_url)
: delegate_(delegate),
render_view_(render_view),
module_(module),
instance_interface_(instance_interface),
pp_instance_(0),
container_(container),
layer_bound_to_fullscreen_(false),
plugin_url_(plugin_url),
full_frame_(false),
sent_initial_did_change_view_(false),
view_change_weak_ptr_factory_(this),
bound_graphics_2d_platform_(NULL),
has_webkit_focus_(false),
has_content_area_focus_(false),
find_identifier_(-1),
plugin_find_interface_(NULL),
plugin_input_event_interface_(NULL),
plugin_messaging_interface_(NULL),
plugin_mouse_lock_interface_(NULL),
plugin_pdf_interface_(NULL),
plugin_private_interface_(NULL),
plugin_selection_interface_(NULL),
plugin_textinput_interface_(NULL),
plugin_zoom_interface_(NULL),
checked_for_plugin_input_event_interface_(false),
checked_for_plugin_messaging_interface_(false),
checked_for_plugin_pdf_interface_(false),
gamepad_impl_(new GamepadImpl(delegate)),
plugin_print_interface_(NULL),
plugin_graphics_3d_interface_(NULL),
always_on_top_(false),
fullscreen_container_(NULL),
flash_fullscreen_(false),
desired_fullscreen_state_(false),
sad_plugin_(NULL),
input_event_mask_(0),
filtered_input_event_mask_(0),
text_input_type_(kPluginDefaultTextInputType),
text_input_caret_(0, 0, 0, 0),
text_input_caret_bounds_(0, 0, 0, 0),
text_input_caret_set_(false),
selection_caret_(0),
selection_anchor_(0),
pending_user_gesture_(0.0),
document_loader_(NULL),
nacl_document_load_(false),
npp_(new NPP_t),
isolate_(v8::Isolate::GetCurrent()) {
pp_instance_ = HostGlobals::Get()->AddInstance(this);
memset(¤t_print_settings_, 0, sizeof(current_print_settings_));
DCHECK(delegate);
module_->InstanceCreated(this);
delegate_->InstanceCreated(this);
view_data_.is_page_visible = delegate->IsPageVisible();
resource_creation_ = delegate_->CreateResourceCreationAPI(this);
// TODO(bbudge) remove this when the trusted NaCl plugin has been removed.
// We must defer certain plugin events for NaCl instances since we switch
// from the in-process to the out-of-process proxy after instantiating them.
if (module->name() == "Native Client")
nacl_document_load_ = true;
}
PluginInstance::~PluginInstance() {
DCHECK(!fullscreen_container_);
// Force-unbind any Graphics. In the case of Graphics2D, if the plugin
// leaks the graphics 2D, it may actually get cleaned up after our
// destruction, so we need its pointers to be up-to-date.
BindGraphics(pp_instance(), 0);
// Free all the plugin objects. This will automatically clear the back-
// pointer from the NPObject so WebKit can't call into the plugin any more.
//
// Swap out the set so we can delete from it (the objects will try to
// unregister themselves inside the delete call).
PluginObjectSet plugin_object_copy;
live_plugin_objects_.swap(plugin_object_copy);
for (PluginObjectSet::iterator i = plugin_object_copy.begin();
i != plugin_object_copy.end(); ++i)
delete *i;
if (TrackedCallback::IsPending(lock_mouse_callback_))
lock_mouse_callback_->Abort();
delegate_->InstanceDeleted(this);
module_->InstanceDeleted(this);
// If we switched from the NaCl plugin module, notify it too.
if (original_module_.get())
original_module_->InstanceDeleted(this);
// This should be last since some of the above "instance deleted" calls will
// want to look up in the global map to get info off of our object.
HostGlobals::Get()->InstanceDeleted(pp_instance_);
}
// NOTE: Any of these methods that calls into the plugin needs to take into
// account that the plugin may use Var to remove the <embed> from the DOM, which
// will make the WebPluginImpl drop its reference, usually the last one. If a
// method needs to access a member of the instance after the call has returned,
// then it needs to keep its own reference on the stack.
void PluginInstance::Delete() {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
// Force the MessageChannel to release its "passthrough object" which should
// release our last reference to the "InstanceObject" and will probably
// destroy it. We want to do this prior to calling DidDestroy in case the
// destructor of the instance object tries to use the instance.
message_channel_->SetPassthroughObject(NULL);
// If this is a NaCl plugin instance, shut down the NaCl plugin by calling
// its DidDestroy. Don't call DidDestroy on the untrusted plugin instance,
// since there is little that it can do at this point.
if (original_instance_interface_)
original_instance_interface_->DidDestroy(pp_instance());
else
instance_interface_->DidDestroy(pp_instance());
// Ensure we don't attempt to call functions on the destroyed instance.
original_instance_interface_.reset();
instance_interface_.reset();
if (fullscreen_container_) {
fullscreen_container_->Destroy();
fullscreen_container_ = NULL;
}
bound_graphics_3d_ = NULL;
UpdateLayer();
container_ = NULL;
}
void PluginInstance::Paint(WebCanvas* canvas,
const gfx::Rect& plugin_rect,
const gfx::Rect& paint_rect) {
TRACE_EVENT0("ppapi", "PluginInstance::Paint");
if (module()->is_crashed()) {
// Crashed plugin painting.
if (!sad_plugin_) // Lazily initialize bitmap.
sad_plugin_ = delegate_->GetSadPluginBitmap();
if (sad_plugin_)
webkit::PaintSadPlugin(canvas, plugin_rect, *sad_plugin_);
return;
}
PluginDelegate::PlatformGraphics2D* bound_graphics_2d = GetBoundGraphics2D();
if (bound_graphics_2d)
bound_graphics_2d->Paint(canvas, plugin_rect, paint_rect);
}
void PluginInstance::InvalidateRect(const gfx::Rect& rect) {
if (fullscreen_container_) {
if (rect.IsEmpty())
fullscreen_container_->Invalidate();
else
fullscreen_container_->InvalidateRect(rect);
} else {
if (!container_ ||
view_data_.rect.size.width == 0 || view_data_.rect.size.height == 0)
return; // Nothing to do.
if (rect.IsEmpty())
container_->invalidate();
else
container_->invalidateRect(rect);
}
}
void PluginInstance::ScrollRect(int dx, int dy, const gfx::Rect& rect) {
if (fullscreen_container_) {
fullscreen_container_->ScrollRect(dx, dy, rect);
} else {
if (full_frame_ && !IsViewAccelerated()) {
container_->scrollRect(dx, dy, rect);
} else {
// Can't do optimized scrolling since there could be other elements on top
// of us or the view renders via the accelerated compositor which is
// incompatible with the move and backfill scrolling model.
InvalidateRect(rect);
}
}
}
void PluginInstance::CommitBackingTexture() {
if (texture_layer_.get())
texture_layer_->SetNeedsDisplay();
}
void PluginInstance::InstanceCrashed() {
// Force free all resources and vars.
HostGlobals::Get()->InstanceCrashed(pp_instance());
// Free any associated graphics.
SetFullscreen(false);
FlashSetFullscreen(false, false);
// Unbind current 2D or 3D graphics context.
BindGraphics(pp_instance(), 0);
InvalidateRect(gfx::Rect());
delegate()->PluginCrashed(this);
}
static void SetGPUHistogram(const ::ppapi::Preferences& prefs,
const std::vector<std::string>& arg_names,
const std::vector<std::string>& arg_values) {
// Calculate a histogram to let us determine how likely people are to try to
// run Stage3D content on machines that have it blacklisted.
#if defined(OS_WIN)
bool needs_gpu = false;
bool is_xp = base::win::GetVersion() <= base::win::VERSION_XP;
for (size_t i = 0; i < arg_names.size(); i++) {
if (arg_names[i] == "wmode") {
// In theory content other than Flash could have a "wmode" argument,
// but that's pretty unlikely.
if (arg_values[i] == "direct" || arg_values[i] == "gpu")
needs_gpu = true;
break;
}
}
// 0 : No 3D content and GPU is blacklisted
// 1 : No 3D content and GPU is not blacklisted
// 2 : 3D content but GPU is blacklisted
// 3 : 3D content and GPU is not blacklisted
// 4 : No 3D content and GPU is blacklisted on XP
// 5 : No 3D content and GPU is not blacklisted on XP
// 6 : 3D content but GPU is blacklisted on XP
// 7 : 3D content and GPU is not blacklisted on XP
UMA_HISTOGRAM_ENUMERATION("Flash.UsesGPU",
is_xp * 4 + needs_gpu * 2 + prefs.is_webgl_supported, 8);
#endif
}
bool PluginInstance::Initialize(const std::vector<std::string>& arg_names,
const std::vector<std::string>& arg_values,
bool full_frame) {
message_channel_.reset(new MessageChannel(this));
full_frame_ = full_frame;
UpdateTouchEventRequest();
container_->setWantsWheelEvents(IsAcceptingWheelEvents());
SetGPUHistogram(delegate_->GetPreferences(), arg_names, arg_values);
argn_ = arg_names;
argv_ = arg_values;
scoped_ptr<const char*[]> argn_array(StringVectorToArgArray(argn_));
scoped_ptr<const char*[]> argv_array(StringVectorToArgArray(argv_));
bool success = PP_ToBool(instance_interface_->DidCreate(pp_instance(),
argn_.size(),
argn_array.get(),
argv_array.get()));
if (success)
message_channel_->StopQueueingJavaScriptMessages();
return success;
}
bool PluginInstance::HandleDocumentLoad(
const WebKit::WebURLResponse& response) {
DCHECK(!document_loader_);
if (!nacl_document_load_) {
if (module()->is_crashed()) {
// Don't create a resource for a crashed plugin.
container()->element().document().frame()->stopLoading();
return false;
}
delegate()->HandleDocumentLoad(this, response);
// If the load was not abandoned, document_loader_ will now be set. It's
// possible that the load was canceled by now and document_loader_ was
// already nulled out.
} else {
// The NaCl proxy isn't available, so save the response and record document
// load notifications for later replay.
nacl_document_response_ = response;
nacl_document_loader_.reset(new NaClDocumentLoader());
document_loader_ = nacl_document_loader_.get();
}
return true;
}
bool PluginInstance::SendCompositionEventToPlugin(PP_InputEvent_Type type,
const base::string16& text) {
std::vector<WebKit::WebCompositionUnderline> empty;
return SendCompositionEventWithUnderlineInformationToPlugin(
type, text, empty, static_cast<int>(text.size()),
static_cast<int>(text.size()));
}
bool PluginInstance::SendCompositionEventWithUnderlineInformationToPlugin(
PP_InputEvent_Type type,
const base::string16& text,
const std::vector<WebKit::WebCompositionUnderline>& underlines,
int selection_start,
int selection_end) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadInputEventInterface())
return false;
PP_InputEvent_Class event_class = PP_INPUTEVENT_CLASS_IME;
if (!(filtered_input_event_mask_ & event_class) &&
!(input_event_mask_ & event_class))
return false;
::ppapi::InputEventData event;
event.event_type = type;
event.event_time_stamp = ::ppapi::TimeTicksToPPTimeTicks(
base::TimeTicks::Now());
// Convert UTF16 text to UTF8 with offset conversion.
std::vector<size_t> utf16_offsets;
utf16_offsets.push_back(selection_start);
utf16_offsets.push_back(selection_end);
for (size_t i = 0; i < underlines.size(); ++i) {
utf16_offsets.push_back(underlines[i].startOffset);
utf16_offsets.push_back(underlines[i].endOffset);
}
std::vector<size_t> utf8_offsets(utf16_offsets);
event.character_text = base::UTF16ToUTF8AndAdjustOffsets(text, &utf8_offsets);
// Set the converted selection range.
event.composition_selection_start = (utf8_offsets[0] == std::string::npos ?
event.character_text.size() : utf8_offsets[0]);
event.composition_selection_end = (utf8_offsets[1] == std::string::npos ?
event.character_text.size() : utf8_offsets[1]);
// Set the converted segmentation points.
// Be sure to add 0 and size(), and remove duplication or errors.
std::set<size_t> offset_set(utf8_offsets.begin()+2, utf8_offsets.end());
offset_set.insert(0);
offset_set.insert(event.character_text.size());
offset_set.erase(std::string::npos);
event.composition_segment_offsets.assign(offset_set.begin(),
offset_set.end());
// Set the composition target.
for (size_t i = 0; i < underlines.size(); ++i) {
if (underlines[i].thick) {
std::vector<uint32_t>::iterator it =
std::find(event.composition_segment_offsets.begin(),
event.composition_segment_offsets.end(),
utf8_offsets[2*i+2]);
if (it != event.composition_segment_offsets.end()) {
event.composition_target_segment =
it - event.composition_segment_offsets.begin();
break;
}
}
}
// Send the event.
bool handled = false;
if (filtered_input_event_mask_ & event_class)
event.is_filtered = true;
else
handled = true; // Unfiltered events are assumed to be handled.
scoped_refptr<PPB_InputEvent_Shared> event_resource(
new PPB_InputEvent_Shared(::ppapi::OBJECT_IS_IMPL, pp_instance(), event));
handled |= PP_ToBool(plugin_input_event_interface_->HandleInputEvent(
pp_instance(), event_resource->pp_resource()));
return handled;
}
void PluginInstance::RequestInputEventsHelper(uint32_t event_classes) {
if (event_classes & PP_INPUTEVENT_CLASS_TOUCH)
UpdateTouchEventRequest();
if (event_classes & PP_INPUTEVENT_CLASS_WHEEL)
container_->setWantsWheelEvents(IsAcceptingWheelEvents());
}
bool PluginInstance::HandleCompositionStart(const base::string16& text) {
return SendCompositionEventToPlugin(PP_INPUTEVENT_TYPE_IME_COMPOSITION_START,
text);
}
bool PluginInstance::HandleCompositionUpdate(
const base::string16& text,
const std::vector<WebKit::WebCompositionUnderline>& underlines,
int selection_start,
int selection_end) {
return SendCompositionEventWithUnderlineInformationToPlugin(
PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE,
text, underlines, selection_start, selection_end);
}
bool PluginInstance::HandleCompositionEnd(const base::string16& text) {
return SendCompositionEventToPlugin(PP_INPUTEVENT_TYPE_IME_COMPOSITION_END,
text);
}
bool PluginInstance::HandleTextInput(const base::string16& text) {
return SendCompositionEventToPlugin(PP_INPUTEVENT_TYPE_IME_TEXT,
text);
}
void PluginInstance::GetSurroundingText(base::string16* text,
ui::Range* range) const {
std::vector<size_t> offsets;
offsets.push_back(selection_anchor_);
offsets.push_back(selection_caret_);
*text = base::UTF8ToUTF16AndAdjustOffsets(surrounding_text_, &offsets);
range->set_start(offsets[0] == base::string16::npos ? text->size()
: offsets[0]);
range->set_end(offsets[1] == base::string16::npos ? text->size()
: offsets[1]);
}
bool PluginInstance::IsPluginAcceptingCompositionEvents() const {
return (filtered_input_event_mask_ & PP_INPUTEVENT_CLASS_IME) ||
(input_event_mask_ & PP_INPUTEVENT_CLASS_IME);
}
gfx::Rect PluginInstance::GetCaretBounds() const {
if (!text_input_caret_set_) {
// If it is never set by the plugin, use the bottom left corner.
return gfx::Rect(view_data_.rect.point.x,
view_data_.rect.point.y + view_data_.rect.size.height,
0, 0);
}
// TODO(kinaba) Take CSS transformation into accont.
// TODO(kinaba) Take bounding_box into account. On some platforms, an
// "exclude rectangle" where candidate window must avoid the region can be
// passed to IME. Currently, we pass only the caret rectangle because
// it is the only information supported uniformly in Chromium.
gfx::Rect caret(text_input_caret_);
caret.Offset(view_data_.rect.point.x, view_data_.rect.point.y);
return caret;
}
bool PluginInstance::HandleInputEvent(const WebKit::WebInputEvent& event,
WebCursorInfo* cursor_info) {
TRACE_EVENT0("ppapi", "PluginInstance::HandleInputEvent");
if (WebInputEvent::isMouseEventType(event.type))
delegate()->DidReceiveMouseEvent(this);
// Don't dispatch input events to crashed plugins.
if (module()->is_crashed())
return false;
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
bool rv = false;
if (LoadInputEventInterface()) {
PP_InputEvent_Class event_class = ClassifyInputEvent(event.type);
if (!event_class)
return false;
if ((filtered_input_event_mask_ & event_class) ||
(input_event_mask_ & event_class)) {
// Actually send the event.
std::vector< ::ppapi::InputEventData > events;
CreateInputEventData(event, &events);
// Allow the user gesture to be pending after the plugin handles the
// event. This allows out-of-process plugins to respond to the user
// gesture after processing has finished here.
if (WebUserGestureIndicator::isProcessingUserGesture()) {
pending_user_gesture_ =
::ppapi::EventTimeToPPTimeTicks(event.timeStampSeconds);
pending_user_gesture_token_ =
WebUserGestureIndicator::currentUserGestureToken();
pending_user_gesture_token_.setOutOfProcess();
}
// Each input event may generate more than one PP_InputEvent.
for (size_t i = 0; i < events.size(); i++) {
if (filtered_input_event_mask_ & event_class)
events[i].is_filtered = true;
else
rv = true; // Unfiltered events are assumed to be handled.
scoped_refptr<PPB_InputEvent_Shared> event_resource(
new PPB_InputEvent_Shared(::ppapi::OBJECT_IS_IMPL,
pp_instance(), events[i]));
rv |= PP_ToBool(plugin_input_event_interface_->HandleInputEvent(
pp_instance(), event_resource->pp_resource()));
}
}
}
if (cursor_)
*cursor_info = *cursor_;
return rv;
}
void PluginInstance::HandleMessage(PP_Var message) {
TRACE_EVENT0("ppapi", "PluginInstance::HandleMessage");
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadMessagingInterface())
return;
plugin_messaging_interface_->HandleMessage(pp_instance(), message);
}
PP_Var PluginInstance::GetInstanceObject() {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
// If the plugin supports the private instance interface, try to retrieve its
// instance object.
if (LoadPrivateInterface())
return plugin_private_interface_->GetInstanceObject(pp_instance());
return PP_MakeUndefined();
}
void PluginInstance::ViewChanged(const gfx::Rect& position,
const gfx::Rect& clip,
const std::vector<gfx::Rect>& cut_outs_rects) {
// WebKit can give weird (x,y) positions for empty clip rects (since the
// position technically doesn't matter). But we want to make these
// consistent since this is given to the plugin, so force everything to 0
// in the "everything is clipped" case.
gfx::Rect new_clip;
if (!clip.IsEmpty())
new_clip = clip;
cut_outs_rects_ = cut_outs_rects;
view_data_.rect = PP_FromGfxRect(position);
view_data_.clip_rect = PP_FromGfxRect(clip);
view_data_.device_scale = container_->deviceScaleFactor();
view_data_.css_scale = container_->pageZoomFactor() *
container_->pageScaleFactor();
if (desired_fullscreen_state_ || view_data_.is_fullscreen) {
WebElement element = container_->element();
WebDocument document = element.document();
bool is_fullscreen_element = (element == document.fullScreenElement());
if (!view_data_.is_fullscreen && desired_fullscreen_state_ &&
delegate()->IsInFullscreenMode() && is_fullscreen_element) {
// Entered fullscreen. Only possible via SetFullscreen().
view_data_.is_fullscreen = true;
} else if (view_data_.is_fullscreen && !is_fullscreen_element) {
// Exited fullscreen. Possible via SetFullscreen() or F11/link,
// so desired_fullscreen_state might be out-of-date.
desired_fullscreen_state_ = false;
view_data_.is_fullscreen = false;
// This operation will cause the plugin to re-layout which will send more
// DidChangeView updates. Schedule an asynchronous update and suppress
// notifications until that completes to avoid sending intermediate sizes
// to the plugins.
ScheduleAsyncDidChangeView();
// Reset the size attributes that we hacked to fill in the screen and
// retrigger ViewChanged. Make sure we don't forward duplicates of
// this view to the plugin.
ResetSizeAttributesAfterFullscreen();
return;
}
}
UpdateFlashFullscreenState(fullscreen_container_ != NULL);
SendDidChangeView();
}
void PluginInstance::SetWebKitFocus(bool has_focus) {
if (has_webkit_focus_ == has_focus)
return;
bool old_plugin_focus = PluginHasFocus();
has_webkit_focus_ = has_focus;
if (PluginHasFocus() != old_plugin_focus)
SendFocusChangeNotification();
}
void PluginInstance::SetContentAreaFocus(bool has_focus) {
if (has_content_area_focus_ == has_focus)
return;
bool old_plugin_focus = PluginHasFocus();
has_content_area_focus_ = has_focus;
if (PluginHasFocus() != old_plugin_focus)
SendFocusChangeNotification();
}
void PluginInstance::PageVisibilityChanged(bool is_visible) {
if (is_visible == view_data_.is_page_visible)
return; // Nothing to do.
view_data_.is_page_visible = is_visible;
// If the initial DidChangeView notification hasn't been sent to the plugin,
// let it pass the visibility state for us, instead of sending a notification
// immediately. It is possible that PluginInstance::ViewChanged() hasn't been
// called for the first time. In that case, most of the fields in |view_data_|
// haven't been properly initialized.
if (sent_initial_did_change_view_)
SendDidChangeView();
}
void PluginInstance::ViewWillInitiatePaint() {
if (GetBoundGraphics2D())
GetBoundGraphics2D()->ViewWillInitiatePaint();
else if (bound_graphics_3d_.get())
bound_graphics_3d_->ViewWillInitiatePaint();
}
void PluginInstance::ViewInitiatedPaint() {
if (GetBoundGraphics2D())
GetBoundGraphics2D()->ViewInitiatedPaint();
else if (bound_graphics_3d_.get())
bound_graphics_3d_->ViewInitiatedPaint();
}
void PluginInstance::ViewFlushedPaint() {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (GetBoundGraphics2D())
GetBoundGraphics2D()->ViewFlushedPaint();
else if (bound_graphics_3d_.get())
bound_graphics_3d_->ViewFlushedPaint();
}
bool PluginInstance::GetBitmapForOptimizedPluginPaint(
const gfx::Rect& paint_bounds,
TransportDIB** dib,
gfx::Rect* location,
gfx::Rect* clip,
float* scale_factor) {
if (!always_on_top_)
return false;
if (!GetBoundGraphics2D() || !GetBoundGraphics2D()->IsAlwaysOpaque())
return false;
// We specifically want to compare against the area covered by the backing
// store when seeing if we cover the given paint bounds, since the backing
// store could be smaller than the declared plugin area.
PPB_ImageData_Impl* image_data = GetBoundGraphics2D()->ImageData();
// ImageDatas created by NaCl don't have a PlatformImage, so can't be
// optimized this way.
if (!image_data->PlatformImage())
return false;
gfx::Point plugin_origin = PP_ToGfxPoint(view_data_.rect.point);
gfx::Vector2d plugin_offset = plugin_origin.OffsetFromOrigin();
// Convert |paint_bounds| to be relative to the left-top corner of the plugin.
gfx::Rect relative_paint_bounds(paint_bounds);
relative_paint_bounds.Offset(-plugin_offset);
gfx::Rect pixel_plugin_backing_store_rect(
0, 0, image_data->width(), image_data->height());
float scale = GetBoundGraphics2D()->GetScale();
gfx::Rect plugin_backing_store_rect = gfx::ToEnclosedRect(
gfx::ScaleRect(pixel_plugin_backing_store_rect, scale));
gfx::Rect clip_page = PP_ToGfxRect(view_data_.clip_rect);
gfx::Rect plugin_paint_rect =
gfx::IntersectRects(plugin_backing_store_rect, clip_page);
if (!plugin_paint_rect.Contains(relative_paint_bounds))
return false;
// Don't do optimized painting if the area to paint intersects with the
// cut-out rects, otherwise we will paint over them.
for (std::vector<gfx::Rect>::const_iterator iter = cut_outs_rects_.begin();
iter != cut_outs_rects_.end(); ++iter) {
if (relative_paint_bounds.Intersects(*iter))
return false;
}
*dib = image_data->PlatformImage()->GetTransportDIB();
plugin_backing_store_rect.Offset(plugin_offset);
*location = plugin_backing_store_rect;
clip_page.Offset(plugin_offset);
*clip = clip_page;
// The plugin scale factor is inverted, e.g. for a device scale factor of 2x
// the plugin scale factor is 0.5.
*scale_factor = 1.0 / scale;
return true;
}
base::string16 PluginInstance::GetSelectedText(bool html) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadSelectionInterface())
return base::string16();
PP_Var rv = plugin_selection_interface_->GetSelectedText(pp_instance(),
PP_FromBool(html));
StringVar* string = StringVar::FromPPVar(rv);
base::string16 selection;
if (string)
selection = UTF8ToUTF16(string->value());
// Release the ref the plugin transfered to us.
HostGlobals::Get()->GetVarTracker()->ReleaseVar(rv);
return selection;
}
base::string16 PluginInstance::GetLinkAtPosition(const gfx::Point& point) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadPdfInterface())
return base::string16();
PP_Point p;
p.x = point.x();
p.y = point.y();
PP_Var rv = plugin_pdf_interface_->GetLinkAtPosition(pp_instance(), p);
StringVar* string = StringVar::FromPPVar(rv);
base::string16 link;
if (string)
link = UTF8ToUTF16(string->value());
// Release the ref the plugin transfered to us.
PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(rv);
return link;
}
void PluginInstance::RequestSurroundingText(
size_t desired_number_of_characters) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadTextInputInterface())
return;
plugin_textinput_interface_->RequestSurroundingText(
pp_instance(), desired_number_of_characters);
}
void PluginInstance::Zoom(double factor, bool text_only) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadZoomInterface())
return;
plugin_zoom_interface_->Zoom(pp_instance(), factor, PP_FromBool(text_only));
}
bool PluginInstance::StartFind(const base::string16& search_text,
bool case_sensitive,
int identifier) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadFindInterface())
return false;
find_identifier_ = identifier;
return PP_ToBool(
plugin_find_interface_->StartFind(
pp_instance(),
UTF16ToUTF8(search_text.c_str()).c_str(),
PP_FromBool(case_sensitive)));
}
void PluginInstance::SelectFindResult(bool forward) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (LoadFindInterface())
plugin_find_interface_->SelectFindResult(pp_instance(),
PP_FromBool(forward));
}
void PluginInstance::StopFind() {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadFindInterface())
return;
find_identifier_ = -1;
plugin_find_interface_->StopFind(pp_instance());
}
bool PluginInstance::LoadFindInterface() {
if (!plugin_find_interface_) {
plugin_find_interface_ =
static_cast<const PPP_Find_Dev*>(module_->GetPluginInterface(
PPP_FIND_DEV_INTERFACE));
}
return !!plugin_find_interface_;
}
bool PluginInstance::LoadInputEventInterface() {
if (!checked_for_plugin_input_event_interface_) {
checked_for_plugin_input_event_interface_ = true;
plugin_input_event_interface_ =
static_cast<const PPP_InputEvent*>(module_->GetPluginInterface(
PPP_INPUT_EVENT_INTERFACE));
}
return !!plugin_input_event_interface_;
}
bool PluginInstance::LoadMessagingInterface() {
if (!checked_for_plugin_messaging_interface_) {
checked_for_plugin_messaging_interface_ = true;
plugin_messaging_interface_ =
static_cast<const PPP_Messaging*>(module_->GetPluginInterface(
PPP_MESSAGING_INTERFACE));
}
return !!plugin_messaging_interface_;
}
bool PluginInstance::LoadMouseLockInterface() {
if (!plugin_mouse_lock_interface_) {
plugin_mouse_lock_interface_ =
static_cast<const PPP_MouseLock*>(module_->GetPluginInterface(
PPP_MOUSELOCK_INTERFACE));
}
return !!plugin_mouse_lock_interface_;
}
bool PluginInstance::LoadPdfInterface() {
if (!checked_for_plugin_pdf_interface_) {
checked_for_plugin_pdf_interface_ = true;
plugin_pdf_interface_ =
static_cast<const PPP_Pdf_1*>(module_->GetPluginInterface(
PPP_PDF_INTERFACE_1));
}
return !!plugin_pdf_interface_;
}
bool PluginInstance::LoadPrintInterface() {
// Only check for the interface if the plugin has dev permission.
if (!module_->permissions().HasPermission(::ppapi::PERMISSION_DEV))
return false;
if (!plugin_print_interface_) {
plugin_print_interface_ = static_cast<const PPP_Printing_Dev*>(
module_->GetPluginInterface(PPP_PRINTING_DEV_INTERFACE));
}
return !!plugin_print_interface_;
}
bool PluginInstance::LoadPrivateInterface() {
// Only check for the interface if the plugin has private permission.
if (!module_->permissions().HasPermission(::ppapi::PERMISSION_PRIVATE))
return false;
if (!plugin_private_interface_) {
plugin_private_interface_ = static_cast<const PPP_Instance_Private*>(
module_->GetPluginInterface(PPP_INSTANCE_PRIVATE_INTERFACE));
}
return !!plugin_private_interface_;
}
bool PluginInstance::LoadSelectionInterface() {
if (!plugin_selection_interface_) {
plugin_selection_interface_ =
static_cast<const PPP_Selection_Dev*>(module_->GetPluginInterface(
PPP_SELECTION_DEV_INTERFACE));
}
return !!plugin_selection_interface_;
}
bool PluginInstance::LoadTextInputInterface() {
if (!plugin_textinput_interface_) {
plugin_textinput_interface_ =
static_cast<const PPP_TextInput_Dev*>(module_->GetPluginInterface(
PPP_TEXTINPUT_DEV_INTERFACE));
}
return !!plugin_textinput_interface_;
}
bool PluginInstance::LoadZoomInterface() {
if (!plugin_zoom_interface_) {
plugin_zoom_interface_ =
static_cast<const PPP_Zoom_Dev*>(module_->GetPluginInterface(
PPP_ZOOM_DEV_INTERFACE));
}
return !!plugin_zoom_interface_;
}
bool PluginInstance::PluginHasFocus() const {
return flash_fullscreen_ || (has_webkit_focus_ && has_content_area_focus_);
}
void PluginInstance::SendFocusChangeNotification() {
// This call can happen during PluginInstance destruction, because WebKit
// informs the plugin it's losing focus. See crbug.com/236574
if (!delegate_ || !instance_interface_)
return;
bool has_focus = PluginHasFocus();
delegate()->PluginFocusChanged(this, has_focus);
instance_interface_->DidChangeFocus(pp_instance(), PP_FromBool(has_focus));
}
void PluginInstance::UpdateTouchEventRequest() {
bool raw_touch = (filtered_input_event_mask_ & PP_INPUTEVENT_CLASS_TOUCH) ||
(input_event_mask_ & PP_INPUTEVENT_CLASS_TOUCH);
container_->requestTouchEventType(raw_touch ?
WebKit::WebPluginContainer::TouchEventRequestTypeRaw :
WebKit::WebPluginContainer::TouchEventRequestTypeSynthesizedMouse);
}
bool PluginInstance::IsAcceptingWheelEvents() const {
return (filtered_input_event_mask_ & PP_INPUTEVENT_CLASS_WHEEL) ||
(input_event_mask_ & PP_INPUTEVENT_CLASS_WHEEL);
}
void PluginInstance::ScheduleAsyncDidChangeView() {
if (view_change_weak_ptr_factory_.HasWeakPtrs())
return; // Already scheduled.
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&PluginInstance::SendAsyncDidChangeView,
view_change_weak_ptr_factory_.GetWeakPtr()));
}
void PluginInstance::SendAsyncDidChangeView() {
// The bound callback that owns the weak pointer is still valid until after
// this function returns. SendDidChangeView checks HasWeakPtrs, so we need to
// invalidate them here.
// NOTE: If we ever want to have more than one pending callback, it should
// use a different factory, or we should have a different strategy here.
view_change_weak_ptr_factory_.InvalidateWeakPtrs();
SendDidChangeView();
}
void PluginInstance::SendDidChangeView() {
// Don't send DidChangeView to crashed plugins.
if (module()->is_crashed())
return;
if (view_change_weak_ptr_factory_.HasWeakPtrs() ||
(sent_initial_did_change_view_ &&
last_sent_view_data_.Equals(view_data_)))
return; // Nothing to update.
const PP_Size& size = view_data_.rect.size;
// Avoid sending a notification with a huge rectangle.
if (size.width < 0 || size.width > kMaxPluginSideLength ||
size.height < 0 || size.height > kMaxPluginSideLength ||
// We know this won't overflow due to above checks.
static_cast<uint32>(size.width) * static_cast<uint32>(size.height) >
kMaxPluginSize) {
return;
}
sent_initial_did_change_view_ = true;
last_sent_view_data_ = view_data_;
ScopedPPResource resource(
ScopedPPResource::PassRef(),
(new PPB_View_Shared(::ppapi::OBJECT_IS_IMPL,
pp_instance(), view_data_))->GetReference());
instance_interface_->DidChangeView(pp_instance(), resource,
&view_data_.rect,
&view_data_.clip_rect);
}
void PluginInstance::ReportGeometry() {
// If this call was delayed, we may have transitioned back to fullscreen in
// the mean time, so only report the geometry if we are actually in normal
// mode.
if (container_ && !fullscreen_container_ && !flash_fullscreen_)
container_->reportGeometry();
}
bool PluginInstance::GetPreferredPrintOutputFormat(
PP_PrintOutputFormat_Dev* format) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!LoadPrintInterface())
return false;
uint32_t supported_formats =
plugin_print_interface_->QuerySupportedFormats(pp_instance());
if (supported_formats & PP_PRINTOUTPUTFORMAT_PDF) {
*format = PP_PRINTOUTPUTFORMAT_PDF;
return true;
}
return false;
}
bool PluginInstance::SupportsPrintInterface() {
PP_PrintOutputFormat_Dev format;
return GetPreferredPrintOutputFormat(&format);
}
bool PluginInstance::IsPrintScalingDisabled() {
DCHECK(plugin_print_interface_);
if (!plugin_print_interface_)
return false;
return plugin_print_interface_->IsScalingDisabled(pp_instance()) == PP_TRUE;
}
int PluginInstance::PrintBegin(const WebPrintParams& print_params) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
PP_PrintOutputFormat_Dev format;
if (!GetPreferredPrintOutputFormat(&format)) {
// PrintBegin should not have been called since SupportsPrintInterface
// would have returned false;
NOTREACHED();
return 0;
}
int num_pages = 0;
PP_PrintSettings_Dev print_settings;
print_settings.printable_area = PP_FromGfxRect(print_params.printableArea);
print_settings.content_area = PP_FromGfxRect(print_params.printContentArea);
print_settings.paper_size = PP_FromGfxSize(print_params.paperSize);
print_settings.dpi = print_params.printerDPI;
print_settings.orientation = PP_PRINTORIENTATION_NORMAL;
print_settings.grayscale = PP_FALSE;
print_settings.print_scaling_option = static_cast<PP_PrintScalingOption_Dev>(
print_params.printScalingOption);
print_settings.format = format;
num_pages = plugin_print_interface_->Begin(pp_instance(),
&print_settings);
if (!num_pages)
return 0;
current_print_settings_ = print_settings;
canvas_.clear();
ranges_.clear();
return num_pages;
}
bool PluginInstance::PrintPage(int page_number, WebKit::WebCanvas* canvas) {
#if defined(ENABLE_PRINTING)
DCHECK(plugin_print_interface_);
PP_PrintPageNumberRange_Dev page_range;
page_range.first_page_number = page_range.last_page_number = page_number;
// The canvas only has a metafile on it for print preview.
bool save_for_later =
(printing::MetafileSkiaWrapper::GetMetafileFromCanvas(*canvas) != NULL);
#if defined(OS_MACOSX) || defined(OS_WIN)
save_for_later = save_for_later && skia::IsPreviewMetafile(*canvas);
#endif
if (save_for_later) {
ranges_.push_back(page_range);
canvas_ = skia::SharePtr(canvas);
return true;
} else {
return PrintPageHelper(&page_range, 1, canvas);
}
#else // defined(ENABLED_PRINTING)
return false;
#endif
}
bool PluginInstance::PrintPageHelper(PP_PrintPageNumberRange_Dev* page_ranges,
int num_ranges,
WebKit::WebCanvas* canvas) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
DCHECK(plugin_print_interface_);
if (!plugin_print_interface_)
return false;
PP_Resource print_output = plugin_print_interface_->PrintPages(
pp_instance(), page_ranges, num_ranges);
if (!print_output)
return false;
bool ret = false;
if (current_print_settings_.format == PP_PRINTOUTPUTFORMAT_PDF)
ret = PrintPDFOutput(print_output, canvas);
// Now we need to release the print output resource.
PluginModule::GetCore()->ReleaseResource(print_output);
return ret;
}
void PluginInstance::PrintEnd() {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
if (!ranges_.empty())
PrintPageHelper(&(ranges_.front()), ranges_.size(), canvas_.get());
canvas_.clear();
ranges_.clear();
DCHECK(plugin_print_interface_);
if (plugin_print_interface_)
plugin_print_interface_->End(pp_instance());
memset(¤t_print_settings_, 0, sizeof(current_print_settings_));
#if defined(OS_MACOSX)
last_printed_page_ = NULL;
#endif // defined(OS_MACOSX)
}
bool PluginInstance::CanRotateView() {
if (!LoadPdfInterface())
return false;
return true;
}
void PluginInstance::SetBoundGraphics2DForTest(
PluginDelegate::PlatformGraphics2D* graphics) {
BindGraphics(pp_instance(), 0); // Unbind any old stuff.
if (graphics) {
bound_graphics_2d_platform_ = graphics;
bound_graphics_2d_platform_->BindToInstance(this);
}
}
void PluginInstance::RotateView(WebPlugin::RotationType type) {
if (!LoadPdfInterface())
return;
PP_PrivatePageTransformType transform_type =
type == WebPlugin::RotationType90Clockwise ?
PP_PRIVATEPAGETRANSFORMTYPE_ROTATE_90_CW :
PP_PRIVATEPAGETRANSFORMTYPE_ROTATE_90_CCW;
plugin_pdf_interface_->Transform(pp_instance(), transform_type);
// NOTE: plugin instance may have been deleted.
}
bool PluginInstance::FlashIsFullscreenOrPending() {
return fullscreen_container_ != NULL;
}
bool PluginInstance::IsFullscreenOrPending() {
return desired_fullscreen_state_;
}
bool PluginInstance::SetFullscreen(bool fullscreen) {
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
// Check whether we are trying to switch to the state we're already going
// to (i.e. if we're already switching to fullscreen but the fullscreen
// container isn't ready yet, don't do anything more).
if (fullscreen == IsFullscreenOrPending())
return false;
// Check whether we are trying to switch while the state is in transition.
// The 2nd request gets dropped while messing up the internal state, so
// disallow this.
if (view_data_.is_fullscreen != desired_fullscreen_state_)
return false;
if (fullscreen && !IsProcessingUserGesture())
return false;
VLOG(1) << "Setting fullscreen to " << (fullscreen ? "on" : "off");
desired_fullscreen_state_ = fullscreen;
if (fullscreen) {
// Create the user gesture in case we're processing one that's pending.
WebScopedUserGesture user_gesture(CurrentUserGestureToken());
// WebKit does not resize the plugin to fill the screen in fullscreen mode,
// so we will tweak plugin's attributes to support the expected behavior.
KeepSizeAttributesBeforeFullscreen();
SetSizeAttributesForFullscreen();
container_->element().requestFullScreen();
} else {
container_->element().document().cancelFullScreen();
}
return true;
}
void PluginInstance::FlashSetFullscreen(bool fullscreen, bool delay_report) {
TRACE_EVENT0("ppapi", "PluginInstance::FlashSetFullscreen");
// Keep a reference on the stack. See NOTE above.
scoped_refptr<PluginInstance> ref(this);
// We check whether we are trying to switch to the state we're already going
// to (i.e. if we're already switching to fullscreen but the fullscreen
// container isn't ready yet, don't do anything more).
if (fullscreen == FlashIsFullscreenOrPending())
return;
// Unbind current 2D or 3D graphics context.
VLOG(1) << "Setting fullscreen to " << (fullscreen ? "on" : "off");
if (fullscreen) {
DCHECK(!fullscreen_container_);
fullscreen_container_ = delegate_->CreateFullscreenContainer(this);
UpdateLayer();
} else {
DCHECK(fullscreen_container_);
fullscreen_container_->Destroy();
fullscreen_container_ = NULL;
UpdateFlashFullscreenState(false);
if (!delay_report) {
ReportGeometry();
} else {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&PluginInstance::ReportGeometry, this));
}
}
}
void PluginInstance::UpdateFlashFullscreenState(bool flash_fullscreen) {
bool is_mouselock_pending = TrackedCallback::IsPending(lock_mouse_callback_);
if (flash_fullscreen == flash_fullscreen_) {
// Manually clear callback when fullscreen fails with mouselock pending.
if (!flash_fullscreen && is_mouselock_pending)
lock_mouse_callback_->Run(PP_ERROR_FAILED);
return;
}
PPB_Graphics3D_Impl* graphics_3d = bound_graphics_3d_.get();
if (graphics_3d)
UpdateLayer();
bool old_plugin_focus = PluginHasFocus();
flash_fullscreen_ = flash_fullscreen;
if (is_mouselock_pending && !delegate()->IsMouseLocked(this)) {
if (!IsProcessingUserGesture() &&
!module_->permissions().HasPermission(
::ppapi::PERMISSION_BYPASS_USER_GESTURE)) {
lock_mouse_callback_->Run(PP_ERROR_NO_USER_GESTURE);
} else {
// Open a user gesture here so the Webkit user gesture checks will succeed
// for out-of-process plugins.
WebScopedUserGesture user_gesture(CurrentUserGestureToken());
if (!delegate()->LockMouse(this))
lock_mouse_callback_->Run(PP_ERROR_FAILED);
}
}
if (PluginHasFocus() != old_plugin_focus)
SendFocusChangeNotification();
}
int32_t PluginInstance::Navigate(const ::ppapi::URLRequestInfoData& request,
const char* target,
bool from_user_action) {
if (!container_)
return PP_ERROR_FAILED;
WebDocument document = container_->element().document();
WebFrame* frame = document.frame();
if (!frame)
return PP_ERROR_FAILED;
::ppapi::URLRequestInfoData completed_request = request;
WebURLRequest web_request;
if (!CreateWebURLRequest(&completed_request, frame, &web_request))
return PP_ERROR_FAILED;
web_request.setFirstPartyForCookies(document.firstPartyForCookies());
web_request.setHasUserGesture(from_user_action);
GURL gurl(web_request.url());
if (gurl.SchemeIs("javascript")) {
// In imitation of the NPAPI implementation, only |target_frame == frame| is
// allowed for security reasons.
WebFrame* target_frame =
frame->view()->findFrameByName(WebString::fromUTF8(target), frame);
if (target_frame != frame)
return PP_ERROR_NOACCESS;
// TODO(viettrungluu): NPAPI sends the result back to the plugin -- do we
// need that?
WebString result = container_->executeScriptURL(gurl, from_user_action);
return result.isNull() ? PP_ERROR_FAILED : PP_OK;
}
// Only GETs and POSTs are supported.
if (web_request.httpMethod() != "GET" &&
web_request.httpMethod() != "POST")
return PP_ERROR_BADARGUMENT;
WebString target_str = WebString::fromUTF8(target);
container_->loadFrameRequest(web_request, target_str, false, NULL);
return PP_OK;
}
bool PluginInstance::IsRectTopmost(const gfx::Rect& rect) {
if (flash_fullscreen_)
return true;
return container_->isRectTopmost(rect);
}
bool PluginInstance::IsViewAccelerated() {
if (!container_)
return false;
WebDocument document = container_->element().document();
WebFrame* frame = document.frame();
if (!frame)
return false;
WebView* view = frame->view();
if (!view)
return false;
return view->isAcceleratedCompositingActive();
}
PluginDelegate::PlatformContext3D* PluginInstance::CreateContext3D() {
return delegate_->CreateContext3D();
}
bool PluginInstance::PrintPDFOutput(PP_Resource print_output,
WebKit::WebCanvas* canvas) {
#if defined(ENABLE_PRINTING)
::ppapi::thunk::EnterResourceNoLock<PPB_Buffer_API> enter(print_output, true);
if (enter.failed())
return false;
BufferAutoMapper mapper(enter.object());
if (!mapper.data() || !mapper.size()) {
NOTREACHED();
return false;
}
#if defined(OS_WIN)
// For Windows, we need the PDF DLL to render the output PDF to a DC.
HMODULE pdf_module = GetModuleHandle(L"pdf.dll");
if (!pdf_module)
return false;
RenderPDFPageToDCProc render_proc =
reinterpret_cast<RenderPDFPageToDCProc>(
GetProcAddress(pdf_module, "RenderPDFPageToDC"));
if (!render_proc)
return false;
#endif // defined(OS_WIN)
bool ret = false;
#if defined(OS_LINUX) || defined(OS_MACOSX)
// On Linux we just set the final bits in the native metafile
// (NativeMetafile and PreviewMetafile must have compatible formats,
// i.e. both PDF for this to work).
printing::Metafile* metafile =
printing::MetafileSkiaWrapper::GetMetafileFromCanvas(*canvas);
DCHECK(metafile != NULL);
if (metafile)
ret = metafile->InitFromData(mapper.data(), mapper.size());
#elif defined(OS_WIN)
printing::Metafile* metafile =
printing::MetafileSkiaWrapper::GetMetafileFromCanvas(*canvas);
if (metafile) {
// We only have a metafile when doing print preview, so we just want to
// pass the PDF off to preview.
ret = metafile->InitFromData(mapper.data(), mapper.size());
} else {
// On Windows, we now need to render the PDF to the DC that backs the
// supplied canvas.
HDC dc = skia::BeginPlatformPaint(canvas);
DrawEmptyRectangle(dc);
gfx::Size size_in_pixels;
size_in_pixels.set_width(printing::ConvertUnit(
current_print_settings_.printable_area.size.width,
static_cast<int>(printing::kPointsPerInch),
current_print_settings_.dpi));
size_in_pixels.set_height(printing::ConvertUnit(
current_print_settings_.printable_area.size.height,
static_cast<int>(printing::kPointsPerInch),
current_print_settings_.dpi));
// We need to scale down DC to fit an entire page into DC available area.
// First, we'll try to use default scaling based on the 72dpi that is
// used in webkit for printing.
// If default scaling is not enough to fit the entire PDF without
// Current metafile is based on screen DC and have current screen size.
// Writing outside of those boundaries will result in the cut-off output.
// On metafiles (this is the case here), scaling down will still record
// original coordinates and we'll be able to print in full resolution.
// Before playback we'll need to counter the scaling up that will happen
// in the browser (printed_document_win.cc).
double dynamic_scale = gfx::CalculatePageScale(dc, size_in_pixels.width(),
size_in_pixels.height());
double page_scale = static_cast<double>(printing::kPointsPerInch) /
static_cast<double>(current_print_settings_.dpi);
if (dynamic_scale < page_scale) {
page_scale = dynamic_scale;
printing::MetafileSkiaWrapper::SetCustomScaleOnCanvas(*canvas,
page_scale);
}
gfx::ScaleDC(dc, page_scale);
ret = render_proc(static_cast<unsigned char*>(mapper.data()), mapper.size(),
0, dc, current_print_settings_.dpi,
current_print_settings_.dpi, 0, 0, size_in_pixels.width(),
size_in_pixels.height(), true, false, true, true, true);
skia::EndPlatformPaint(canvas);
}
#endif // defined(OS_WIN)
return ret;
#else // defined(ENABLE_PRINTING)
return false;
#endif
}
PluginDelegate::PlatformGraphics2D* PluginInstance::GetBoundGraphics2D() const {
return bound_graphics_2d_platform_;
}
static void IgnoreCallback(unsigned, bool) {}
void PluginInstance::UpdateLayer() {
if (!container_)
return;
gpu::Mailbox mailbox;
if (bound_graphics_3d_.get()) {
PluginDelegate::PlatformContext3D* context =
bound_graphics_3d_->platform_context();
context->GetBackingMailbox(&mailbox);
}
bool want_layer = !mailbox.IsZero();
if (want_layer == !!texture_layer_.get() &&
layer_bound_to_fullscreen_ == !!fullscreen_container_)
return;
if (texture_layer_.get()) {
if (!layer_bound_to_fullscreen_)
container_->setWebLayer(NULL);
else if (fullscreen_container_)
fullscreen_container_->SetLayer(NULL);
web_layer_.reset();
texture_layer_ = NULL;
}
if (want_layer) {
DCHECK(bound_graphics_3d_.get());
texture_layer_ = cc::TextureLayer::CreateForMailbox(NULL);
web_layer_.reset(new webkit::WebLayerImpl(texture_layer_));
if (fullscreen_container_) {
fullscreen_container_->SetLayer(web_layer_.get());
// Ignore transparency in fullscreen, since that's what Flash always
// wants to do, and that lets it not recreate a context if
// wmode=transparent was specified.
texture_layer_->SetContentsOpaque(true);
} else {
container_->setWebLayer(web_layer_.get());
texture_layer_->SetContentsOpaque(bound_graphics_3d_->IsOpaque());
}
texture_layer_->SetTextureMailbox(
cc::TextureMailbox(mailbox, base::Bind(&IgnoreCallback), 0));
}
layer_bound_to_fullscreen_ = !!fullscreen_container_;
}
void PluginInstance::AddPluginObject(PluginObject* plugin_object) {
DCHECK(live_plugin_objects_.find(plugin_object) ==
live_plugin_objects_.end());
live_plugin_objects_.insert(plugin_object);
}
void PluginInstance::RemovePluginObject(PluginObject* plugin_object) {
// Don't actually verify that the object is in the set since during module
// deletion we'll be in the process of freeing them.
live_plugin_objects_.erase(plugin_object);
}
bool PluginInstance::IsFullPagePlugin() const {
WebFrame* frame = container()->element().document().frame();
return frame->view()->mainFrame()->document().isPluginDocument();
}
bool PluginInstance::IsProcessingUserGesture() {
PP_TimeTicks now =
::ppapi::TimeTicksToPPTimeTicks(base::TimeTicks::Now());
// Give a lot of slack so tests won't be flaky.
const PP_TimeTicks kUserGestureDurationInSeconds = 10.0;
return pending_user_gesture_token_.hasGestures() &&
(now - pending_user_gesture_ < kUserGestureDurationInSeconds);
}
WebUserGestureToken PluginInstance::CurrentUserGestureToken() {
if (!IsProcessingUserGesture())
pending_user_gesture_token_ = WebUserGestureToken();
return pending_user_gesture_token_;
}
void PluginInstance::OnLockMouseACK(bool succeeded) {
if (TrackedCallback::IsPending(lock_mouse_callback_))
lock_mouse_callback_->Run(succeeded ? PP_OK : PP_ERROR_FAILED);
}
void PluginInstance::OnMouseLockLost() {
if (LoadMouseLockInterface())
plugin_mouse_lock_interface_->MouseLockLost(pp_instance());
}
void PluginInstance::HandleMouseLockedInputEvent(
const WebKit::WebMouseEvent& event) {
// |cursor_info| is ignored since it is hidden when the mouse is locked.
WebKit::WebCursorInfo cursor_info;
HandleInputEvent(event, &cursor_info);
}
void PluginInstance::SimulateInputEvent(const InputEventData& input_event) {
WebView* web_view = container()->element().document().frame()->view();
if (!web_view) {
NOTREACHED();
return;
}
bool handled = SimulateIMEEvent(input_event);
if (handled)
return;
std::vector<linked_ptr<WebInputEvent> > events =
CreateSimulatedWebInputEvents(
input_event,
view_data_.rect.point.x + view_data_.rect.size.width / 2,
view_data_.rect.point.y + view_data_.rect.size.height / 2);
for (std::vector<linked_ptr<WebInputEvent> >::iterator it = events.begin();
it != events.end(); ++it) {
web_view->handleInputEvent(*it->get());
}
}
bool PluginInstance::SimulateIMEEvent(const InputEventData& input_event) {
switch (input_event.event_type) {
case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START:
case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE:
SimulateImeSetCompositionEvent(input_event);
break;
case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END:
DCHECK(input_event.character_text.empty());
SimulateImeSetCompositionEvent(input_event);
break;
case PP_INPUTEVENT_TYPE_IME_TEXT:
delegate()->SimulateImeConfirmComposition(
UTF8ToUTF16(input_event.character_text));
break;
default:
return false;
}
return true;
}
void PluginInstance::SimulateImeSetCompositionEvent(
const InputEventData& input_event) {
std::vector<size_t> offsets;
offsets.push_back(input_event.composition_selection_start);
offsets.push_back(input_event.composition_selection_end);
offsets.insert(offsets.end(),
input_event.composition_segment_offsets.begin(),
input_event.composition_segment_offsets.end());
base::string16 utf16_text =
base::UTF8ToUTF16AndAdjustOffsets(input_event.character_text, &offsets);
std::vector<WebKit::WebCompositionUnderline> underlines;
for (size_t i = 2; i + 1 < offsets.size(); ++i) {
WebKit::WebCompositionUnderline underline;
underline.startOffset = offsets[i];
underline.endOffset = offsets[i + 1];
if (input_event.composition_target_segment == static_cast<int32_t>(i - 2))
underline.thick = true;
underlines.push_back(underline);
}
delegate()->SimulateImeSetComposition(
utf16_text, underlines, offsets[0], offsets[1]);
}
ContentDecryptorDelegate* PluginInstance::GetContentDecryptorDelegate() {
if (content_decryptor_delegate_)
return content_decryptor_delegate_.get();
const PPP_ContentDecryptor_Private* plugin_decryption_interface =
static_cast<const PPP_ContentDecryptor_Private*>(
module_->GetPluginInterface(
PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE));
if (!plugin_decryption_interface)
return NULL;
content_decryptor_delegate_.reset(
new ContentDecryptorDelegate(pp_instance_, plugin_decryption_interface));
return content_decryptor_delegate_.get();
}
PP_Bool PluginInstance::BindGraphics(PP_Instance instance,
PP_Resource device) {
TRACE_EVENT0("ppapi", "PluginInstance::BindGraphics");
// The Graphics3D instance can't be destroyed until we call
// UpdateLayer().
scoped_refptr< ::ppapi::Resource> old_graphics = bound_graphics_3d_.get();
if (bound_graphics_3d_.get()) {
bound_graphics_3d_->BindToInstance(false);
bound_graphics_3d_ = NULL;
}
if (bound_graphics_2d_platform_) {
GetBoundGraphics2D()->BindToInstance(NULL);
bound_graphics_2d_platform_ = NULL;
}
// Special-case clearing the current device.
if (!device) {
UpdateLayer();
InvalidateRect(gfx::Rect());
return PP_TRUE;
}
// Refuse to bind if in transition to fullscreen with PPB_FlashFullscreen or
// to/from fullscreen with PPB_Fullscreen.
if ((fullscreen_container_ && !flash_fullscreen_) ||
desired_fullscreen_state_ != view_data_.is_fullscreen)
return PP_FALSE;
PluginDelegate::PlatformGraphics2D* graphics_2d =
delegate_->GetGraphics2D(this, device);
EnterResourceNoLock<PPB_Graphics3D_API> enter_3d(device, false);
PPB_Graphics3D_Impl* graphics_3d = enter_3d.succeeded() ?
static_cast<PPB_Graphics3D_Impl*>(enter_3d.object()) : NULL;
if (graphics_2d) {
if (graphics_2d->BindToInstance(this)) {
bound_graphics_2d_platform_ = graphics_2d;
UpdateLayer();
return PP_TRUE;
}
} else if (graphics_3d) {
// Make sure graphics can only be bound to the instance it is
// associated with.
if (graphics_3d->pp_instance() == pp_instance() &&
graphics_3d->BindToInstance(true)) {
bound_graphics_3d_ = graphics_3d;
UpdateLayer();
return PP_TRUE;
}
}
// The instance cannot be bound or the device is not a valid resource type.
return PP_FALSE;
}
PP_Bool PluginInstance::IsFullFrame(PP_Instance instance) {
return PP_FromBool(full_frame());
}
const ViewData* PluginInstance::GetViewData(PP_Instance instance) {
return &view_data_;
}
PP_Bool PluginInstance::FlashIsFullscreen(PP_Instance instance) {
return PP_FromBool(flash_fullscreen_);
}
PP_Var PluginInstance::GetWindowObject(PP_Instance instance) {
if (!container_)
return PP_MakeUndefined();
WebFrame* frame = container_->element().document().frame();
if (!frame)
return PP_MakeUndefined();
return NPObjectToPPVar(this, frame->windowObject());
}
PP_Var PluginInstance::GetOwnerElementObject(PP_Instance instance) {
if (!container_)
return PP_MakeUndefined();
return NPObjectToPPVar(this, container_->scriptableObjectForElement());
}
PP_Var PluginInstance::ExecuteScript(PP_Instance instance,
PP_Var script,
PP_Var* exception) {
// Executing the script may remove the plugin from the DOM, so we need to keep
// a reference to ourselves so that we can still process the result after the
// WebBindings::evaluate() below.
scoped_refptr<PluginInstance> ref(this);
TryCatch try_catch(exception);
if (try_catch.has_exception())
return PP_MakeUndefined();
// Convert the script into an inconvenient NPString object.
StringVar* script_string = StringVar::FromPPVar(script);
if (!script_string) {
try_catch.SetException("Script param to ExecuteScript must be a string.");
return PP_MakeUndefined();
}
NPString np_script;
np_script.UTF8Characters = script_string->value().c_str();
np_script.UTF8Length = script_string->value().length();
// Get the current frame to pass to the evaluate function.
WebFrame* frame = container_->element().document().frame();
if (!frame) {
try_catch.SetException("No frame to execute script in.");
return PP_MakeUndefined();
}
NPVariant result;
bool ok = false;
if (IsProcessingUserGesture()) {
WebKit::WebScopedUserGesture user_gesture(CurrentUserGestureToken());
ok = WebBindings::evaluate(NULL, frame->windowObject(), &np_script,
&result);
} else {
ok = WebBindings::evaluate(NULL, frame->windowObject(), &np_script,
&result);
}
if (!ok) {
// TryCatch doesn't catch the exceptions properly. Since this is only for
// a trusted API, just set to a general exception message.
try_catch.SetException("Exception caught");
WebBindings::releaseVariantValue(&result);
return PP_MakeUndefined();
}
PP_Var ret = NPVariantToPPVar(this, &result);
WebBindings::releaseVariantValue(&result);
return ret;
}
uint32_t PluginInstance::GetAudioHardwareOutputSampleRate(
PP_Instance instance) {
return delegate()->GetAudioHardwareOutputSampleRate();
}
uint32_t PluginInstance::GetAudioHardwareOutputBufferSize(
PP_Instance instance) {
return delegate()->GetAudioHardwareOutputBufferSize();
}
PP_Var PluginInstance::GetDefaultCharSet(PP_Instance instance) {
std::string encoding = delegate()->GetDefaultEncoding();
return StringVar::StringToPPVar(encoding);
}
// These PPB_ContentDecryptor_Private calls are responses to
// PPP_ContentDecryptor_Private calls made on |content_decryptor_delegate_|.
// Therefore, |content_decryptor_delegate_| must have been initialized when
// the following methods are called.
void PluginInstance::NeedKey(PP_Instance instance,
PP_Var key_system_var,
PP_Var session_id_var,
PP_Var init_data_var) {
content_decryptor_delegate_->NeedKey(
key_system_var, session_id_var, init_data_var);
}
void PluginInstance::KeyAdded(PP_Instance instance,
PP_Var key_system_var,
PP_Var session_id_var) {
content_decryptor_delegate_->KeyAdded(key_system_var, session_id_var);
}
void PluginInstance::KeyMessage(PP_Instance instance,
PP_Var key_system_var,
PP_Var session_id_var,
PP_Var message_var,
PP_Var default_url_var) {
content_decryptor_delegate_->KeyMessage(
key_system_var, session_id_var, message_var, default_url_var);
}
void PluginInstance::KeyError(PP_Instance instance,
PP_Var key_system_var,
PP_Var session_id_var,
int32_t media_error,
int32_t system_code) {
content_decryptor_delegate_->KeyError(
key_system_var, session_id_var, media_error, system_code);
}
void PluginInstance::DeliverBlock(PP_Instance instance,
PP_Resource decrypted_block,
const PP_DecryptedBlockInfo* block_info) {
content_decryptor_delegate_->DeliverBlock(decrypted_block, block_info);
}
void PluginInstance::DecoderInitializeDone(PP_Instance instance,
PP_DecryptorStreamType decoder_type,
uint32_t request_id,
PP_Bool success) {
content_decryptor_delegate_->DecoderInitializeDone(
decoder_type, request_id, success);
}
void PluginInstance::DecoderDeinitializeDone(
PP_Instance instance,
PP_DecryptorStreamType decoder_type,
uint32_t request_id) {
content_decryptor_delegate_->DecoderDeinitializeDone(decoder_type,
request_id);
}
void PluginInstance::DecoderResetDone(PP_Instance instance,
PP_DecryptorStreamType decoder_type,
uint32_t request_id) {
content_decryptor_delegate_->DecoderResetDone(decoder_type, request_id);
}
void PluginInstance::DeliverFrame(PP_Instance instance,
PP_Resource decrypted_frame,
const PP_DecryptedFrameInfo* frame_info) {
content_decryptor_delegate_->DeliverFrame(decrypted_frame, frame_info);
}
void PluginInstance::DeliverSamples(PP_Instance instance,
PP_Resource audio_frames,
const PP_DecryptedBlockInfo* block_info) {
content_decryptor_delegate_->DeliverSamples(audio_frames, block_info);
}
void PluginInstance::NumberOfFindResultsChanged(PP_Instance instance,
int32_t total,
PP_Bool final_result) {
DCHECK_NE(find_identifier_, -1);
delegate_->NumberOfFindResultsChanged(find_identifier_, total,
PP_ToBool(final_result));
}
void PluginInstance::SelectedFindResultChanged(PP_Instance instance,
int32_t index) {
DCHECK_NE(find_identifier_, -1);
delegate_->SelectedFindResultChanged(find_identifier_, index);
}
PP_Bool PluginInstance::IsFullscreen(PP_Instance instance) {
return PP_FromBool(view_data_.is_fullscreen);
}
PP_Bool PluginInstance::SetFullscreen(PP_Instance instance,
PP_Bool fullscreen) {
return PP_FromBool(SetFullscreen(PP_ToBool(fullscreen)));
}
PP_Bool PluginInstance::GetScreenSize(PP_Instance instance, PP_Size* size) {
gfx::Size screen_size = delegate()->GetScreenSize();
*size = PP_MakeSize(screen_size.width(), screen_size.height());
return PP_TRUE;
}
::ppapi::Resource* PluginInstance::GetSingletonResource(
PP_Instance instance,
::ppapi::SingletonResourceID id) {
// Flash APIs and some others aren't implemented in-process.
switch (id) {
case ::ppapi::BROKER_SINGLETON_ID:
case ::ppapi::BROWSER_FONT_SINGLETON_ID:
case ::ppapi::CRX_FILESYSTEM_SINGLETON_ID:
case ::ppapi::EXTENSIONS_COMMON_SINGLETON_ID:
case ::ppapi::FLASH_CLIPBOARD_SINGLETON_ID:
case ::ppapi::FLASH_FILE_SINGLETON_ID:
case ::ppapi::FLASH_FULLSCREEN_SINGLETON_ID:
case ::ppapi::FLASH_SINGLETON_ID:
case ::ppapi::NETWORK_PROXY_SINGLETON_ID:
case ::ppapi::PDF_SINGLETON_ID:
case ::ppapi::TRUETYPE_FONT_SINGLETON_ID:
NOTIMPLEMENTED();
return NULL;
case ::ppapi::GAMEPAD_SINGLETON_ID:
return gamepad_impl_.get();
}
NOTREACHED();
return NULL;
}
int32_t PluginInstance::RequestInputEvents(PP_Instance instance,
uint32_t event_classes) {
input_event_mask_ |= event_classes;
filtered_input_event_mask_ &= ~(event_classes);
RequestInputEventsHelper(event_classes);
return ValidateRequestInputEvents(false, event_classes);
}
int32_t PluginInstance::RequestFilteringInputEvents(PP_Instance instance,
uint32_t event_classes) {
filtered_input_event_mask_ |= event_classes;
input_event_mask_ &= ~(event_classes);
RequestInputEventsHelper(event_classes);
return ValidateRequestInputEvents(true, event_classes);
}
void PluginInstance::ClearInputEventRequest(PP_Instance instance,
uint32_t event_classes) {
input_event_mask_ &= ~(event_classes);
filtered_input_event_mask_ &= ~(event_classes);
RequestInputEventsHelper(event_classes);
}
void PluginInstance::ZoomChanged(PP_Instance instance, double factor) {
// We only want to tell the page to change its zoom if the whole page is the
// plugin. If we're in an iframe, then don't do anything.
if (!IsFullPagePlugin())
return;
container()->zoomLevelChanged(WebView::zoomFactorToZoomLevel(factor));
}
void PluginInstance::ZoomLimitsChanged(PP_Instance instance,
double minimum_factor,
double maximium_factor) {
if (minimum_factor > maximium_factor) {
NOTREACHED();
return;
}
delegate()->ZoomLimitsChanged(minimum_factor, maximium_factor);
}
void PluginInstance::PostMessage(PP_Instance instance, PP_Var message) {
message_channel_->PostMessageToJavaScript(message);
}
PP_Bool PluginInstance::SetCursor(PP_Instance instance,
PP_MouseCursor_Type type,
PP_Resource image,
const PP_Point* hot_spot) {
if (!ValidateSetCursorParams(type, image, hot_spot))
return PP_FALSE;
if (type != PP_MOUSECURSOR_TYPE_CUSTOM) {
DoSetCursor(new WebCursorInfo(static_cast<WebCursorInfo::Type>(type)));
return PP_TRUE;
}
EnterResourceNoLock<PPB_ImageData_API> enter(image, true);
if (enter.failed())
return PP_FALSE;
PPB_ImageData_Impl* image_data =
static_cast<PPB_ImageData_Impl*>(enter.object());
ImageDataAutoMapper auto_mapper(image_data);
if (!auto_mapper.is_valid())
return PP_FALSE;
scoped_ptr<WebCursorInfo> custom_cursor(
new WebCursorInfo(WebCursorInfo::TypeCustom));
custom_cursor->hotSpot.x = hot_spot->x;
custom_cursor->hotSpot.y = hot_spot->y;
const SkBitmap* bitmap = image_data->GetMappedBitmap();
// Make a deep copy, so that the cursor remains valid even after the original
// image data gets freed.
if (!bitmap->copyTo(&custom_cursor->customImage.getSkBitmap(),
bitmap->config())) {
return PP_FALSE;
}
DoSetCursor(custom_cursor.release());
return PP_TRUE;
}
int32_t PluginInstance::LockMouse(PP_Instance instance,
scoped_refptr<TrackedCallback> callback) {
if (TrackedCallback::IsPending(lock_mouse_callback_))
return PP_ERROR_INPROGRESS;
if (delegate()->IsMouseLocked(this))
return PP_OK;
if (!CanAccessMainFrame())
return PP_ERROR_NOACCESS;
if (!IsProcessingUserGesture())
return PP_ERROR_NO_USER_GESTURE;
// Attempt mouselock only if Flash isn't waiting on fullscreen, otherwise
// we wait and call LockMouse() in UpdateFlashFullscreenState().
if (!FlashIsFullscreenOrPending() || flash_fullscreen()) {
// Open a user gesture here so the Webkit user gesture checks will succeed
// for out-of-process plugins.
WebScopedUserGesture user_gesture(CurrentUserGestureToken());
if (!delegate()->LockMouse(this))
return PP_ERROR_FAILED;
}
// Either mouselock succeeded or a Flash fullscreen is pending.
lock_mouse_callback_ = callback;
return PP_OK_COMPLETIONPENDING;
}
void PluginInstance::UnlockMouse(PP_Instance instance) {
delegate()->UnlockMouse(this);
}
void PluginInstance::SetTextInputType(PP_Instance instance,
PP_TextInput_Type type) {
int itype = type;
if (itype < 0 || itype > ui::TEXT_INPUT_TYPE_URL)
itype = ui::TEXT_INPUT_TYPE_NONE;
text_input_type_ = static_cast<ui::TextInputType>(itype);
delegate()->PluginTextInputTypeChanged(this);
}
void PluginInstance::UpdateCaretPosition(PP_Instance instance,
const PP_Rect& caret,
const PP_Rect& bounding_box) {
text_input_caret_ = PP_ToGfxRect(caret);
text_input_caret_bounds_ = PP_ToGfxRect(bounding_box);
text_input_caret_set_ = true;
delegate()->PluginCaretPositionChanged(this);
}
void PluginInstance::CancelCompositionText(PP_Instance instance) {
delegate()->PluginRequestedCancelComposition(this);
}
void PluginInstance::SelectionChanged(PP_Instance instance) {
// TODO(kinaba): currently the browser always calls RequestSurroundingText.
// It can be optimized so that it won't call it back until the information
// is really needed.
// Avoid calling in nested context or else this will reenter the plugin. This
// uses a weak pointer rather than exploiting the fact that this class is
// refcounted because we don't actually want this operation to affect the
// lifetime of the instance.
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&PluginInstance::RequestSurroundingText,
AsWeakPtr(),
static_cast<size_t>(kExtraCharsForTextInput)));
}
void PluginInstance::UpdateSurroundingText(PP_Instance instance,
const char* text,
uint32_t caret,
uint32_t anchor) {
surrounding_text_ = text;
selection_caret_ = caret;
selection_anchor_ = anchor;
delegate()->PluginSelectionChanged(this);
}
PP_Var PluginInstance::ResolveRelativeToDocument(
PP_Instance instance,
PP_Var relative,
PP_URLComponents_Dev* components) {
StringVar* relative_string = StringVar::FromPPVar(relative);
if (!relative_string)
return PP_MakeNull();
WebElement plugin_element = container()->element();
GURL document_url = plugin_element.document().baseURL();
return ::ppapi::PPB_URLUtil_Shared::GenerateURLReturn(
document_url.Resolve(relative_string->value()),
components);
}
PP_Bool PluginInstance::DocumentCanRequest(PP_Instance instance, PP_Var url) {
StringVar* url_string = StringVar::FromPPVar(url);
if (!url_string)
return PP_FALSE;
WebKit::WebSecurityOrigin security_origin;
if (!SecurityOriginForInstance(instance, &security_origin))
return PP_FALSE;
GURL gurl(url_string->value());
if (!gurl.is_valid())
return PP_FALSE;
return BoolToPPBool(security_origin.canRequest(gurl));
}
PP_Bool PluginInstance::DocumentCanAccessDocument(PP_Instance instance,
PP_Instance target) {
WebKit::WebSecurityOrigin our_origin;
if (!SecurityOriginForInstance(instance, &our_origin))
return PP_FALSE;
WebKit::WebSecurityOrigin target_origin;
if (!SecurityOriginForInstance(instance, &target_origin))
return PP_FALSE;
return BoolToPPBool(our_origin.canAccess(target_origin));
}
PP_Var PluginInstance::GetDocumentURL(PP_Instance instance,
PP_URLComponents_Dev* components) {
WebKit::WebDocument document = container()->element().document();
return ::ppapi::PPB_URLUtil_Shared::GenerateURLReturn(document.url(),
components);
}
PP_Var PluginInstance::GetPluginInstanceURL(
PP_Instance instance,
PP_URLComponents_Dev* components) {
return ::ppapi::PPB_URLUtil_Shared::GenerateURLReturn(plugin_url_,
components);
}
PP_ExternalPluginResult PluginInstance::ResetAsProxied(
scoped_refptr<PluginModule> module) {
// Save the original module and switch over to the new one now that this
// plugin is using the IPC-based proxy.
original_module_ = module_;
module_ = module;
// Don't send any messages to the plugin until DidCreate() has finished.
message_channel_->QueueJavaScriptMessages();
// For NaCl instances, remember the NaCl plugin instance interface, so we
// can shut it down by calling its DidDestroy in our Delete() method.
original_instance_interface_.reset(instance_interface_.release());
base::Callback<const void*(const char*)> get_plugin_interface_func =
base::Bind(&PluginModule::GetPluginInterface, module_.get());
PPP_Instance_Combined* ppp_instance_combined =
PPP_Instance_Combined::Create(get_plugin_interface_func);
if (!ppp_instance_combined) {
// The proxy must support at least one usable PPP_Instance interface.
// While this could be a failure to implement the interface in the NaCl
// module, it is more likely that the NaCl process has crashed. Either
// way, report that module initialization failed.
return PP_EXTERNAL_PLUGIN_ERROR_MODULE;
}
instance_interface_.reset(ppp_instance_combined);
// Clear all PPP interfaces we may have cached.
plugin_find_interface_ = NULL;
plugin_input_event_interface_ = NULL;
checked_for_plugin_input_event_interface_ = false;
plugin_messaging_interface_ = NULL;
checked_for_plugin_messaging_interface_ = false;
plugin_mouse_lock_interface_ = NULL;
plugin_pdf_interface_ = NULL;
checked_for_plugin_pdf_interface_ = false;
plugin_private_interface_ = NULL;
plugin_selection_interface_ = NULL;
plugin_textinput_interface_ = NULL;
plugin_zoom_interface_ = NULL;
// Re-send the DidCreate event via the proxy.
scoped_ptr<const char*[]> argn_array(StringVectorToArgArray(argn_));
scoped_ptr<const char*[]> argv_array(StringVectorToArgArray(argv_));
if (!instance_interface_->DidCreate(pp_instance(), argn_.size(),
argn_array.get(), argv_array.get()))
return PP_EXTERNAL_PLUGIN_ERROR_INSTANCE;
message_channel_->StopQueueingJavaScriptMessages();
// Clear sent_initial_did_change_view_ and cancel any pending DidChangeView
// event. This way, SendDidChangeView will send the "current" view
// immediately (before other events like HandleDocumentLoad).
sent_initial_did_change_view_ = false;
view_change_weak_ptr_factory_.InvalidateWeakPtrs();
SendDidChangeView();
DCHECK(nacl_document_load_);
nacl_document_load_ = false;
if (!nacl_document_response_.isNull()) {
document_loader_ = NULL;
// Pass the response to the new proxy.
HandleDocumentLoad(nacl_document_response_);
nacl_document_response_ = WebKit::WebURLResponse();
// Replay any document load events we've received to the real loader.
nacl_document_loader_->ReplayReceivedData(document_loader_);
nacl_document_loader_.reset(NULL);
}
return PP_EXTERNAL_PLUGIN_OK;
}
bool PluginInstance::IsValidInstanceOf(PluginModule* module) {
DCHECK(module);
return module == module_.get() ||
module == original_module_.get();
}
NPP PluginInstance::instanceNPP() {
return npp_.get();
}
v8::Isolate* PluginInstance::GetIsolate() const {
return isolate_;
}
PluginInstance* PluginInstance::Get(PP_Instance instance_id) {
return HostGlobals::Get()->GetInstance(instance_id);
}
::ppapi::VarTracker* PluginInstance::GetVarTracker() {
return HostGlobals::Get()->GetVarTracker();
}
PP_Resource PluginInstance::CreateExternalFileReference(
const base::FilePath& external_file_path) {
webkit::ppapi::PPB_FileRef_Impl* ref =
webkit::ppapi::PPB_FileRef_Impl::CreateExternal(
pp_instance(), external_file_path, "");
return ref->GetReference();
}
PP_Resource PluginInstance::CreateImage(gfx::ImageSkia* source_image,
float scale) {
ui::ScaleFactor scale_factor = ui::GetScaleFactorFromScale(scale);
gfx::ImageSkiaRep image_skia_rep = source_image->GetRepresentation(
scale_factor);
if (image_skia_rep.is_null() || image_skia_rep.scale_factor() != scale_factor)
return 0;
scoped_refptr<webkit::ppapi::PPB_ImageData_Impl> image_data(
new webkit::ppapi::PPB_ImageData_Impl(
pp_instance(),
webkit::ppapi::PPB_ImageData_Impl::PLATFORM));
if (!image_data->Init(
webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(),
image_skia_rep.pixel_width(),
image_skia_rep.pixel_height(),
false)) {
return 0;
}
webkit::ppapi::ImageDataAutoMapper mapper(image_data.get());
if (!mapper.is_valid())
return 0;
skia::PlatformCanvas* canvas = image_data->GetPlatformCanvas();
// Note: Do not SkBitmap::copyTo the canvas bitmap directly because it will
// ignore the allocated pixels in shared memory and re-allocate a new buffer.
canvas->writePixels(image_skia_rep.sk_bitmap(), 0, 0);
return image_data->GetReference();
}
base::FilePath PluginInstance::GetModulePath() {
return module_->path();
}
PP_ExternalPluginResult PluginInstance::SwitchToOutOfProcessProxy(
const base::FilePath& file_path,
::ppapi::PpapiPermissions permissions,
const IPC::ChannelHandle& channel_handle,
base::ProcessId plugin_pid,
int plugin_child_id) {
// Create a new module for each instance of the external plugin that is using
// the IPC based out-of-process proxy. We can't use the existing module,
// because it is configured for the in-process plugin, and we must keep it
// that way to allow the page to create other instances.
scoped_refptr<webkit::ppapi::PluginModule> external_plugin_module(
module_->CreateModuleForExternalPluginInstance());
content::RendererPpapiHost* renderer_ppapi_host =
delegate_->CreateExternalPluginModule(
external_plugin_module,
file_path,
permissions,
channel_handle,
plugin_pid,
plugin_child_id);
if (!renderer_ppapi_host) {
DLOG(ERROR) << "CreateExternalPluginModule() failed";
return PP_EXTERNAL_PLUGIN_ERROR_MODULE;
}
// Finally, switch the instance to the proxy.
return external_plugin_module->InitAsProxiedExternalPlugin(this);
}
void PluginInstance::DoSetCursor(WebCursorInfo* cursor) {
cursor_.reset(cursor);
if (fullscreen_container_) {
fullscreen_container_->DidChangeCursor(*cursor);
} else {
delegate()->DidChangeCursor(this, *cursor);
}
}
bool PluginInstance::CanAccessMainFrame() const {
if (!container_)
return false;
WebKit::WebDocument containing_document = container_->element().document();
if (!containing_document.frame() ||
!containing_document.frame()->view() ||
!containing_document.frame()->view()->mainFrame()) {
return false;
}
WebKit::WebDocument main_document =
containing_document.frame()->view()->mainFrame()->document();
return containing_document.securityOrigin().canAccess(
main_document.securityOrigin());
}
void PluginInstance::KeepSizeAttributesBeforeFullscreen() {
WebElement element = container_->element();
width_before_fullscreen_ = element.getAttribute(WebString::fromUTF8(kWidth));
height_before_fullscreen_ =
element.getAttribute(WebString::fromUTF8(kHeight));
border_before_fullscreen_ =
element.getAttribute(WebString::fromUTF8(kBorder));
style_before_fullscreen_ = element.getAttribute(WebString::fromUTF8(kStyle));
}
void PluginInstance::SetSizeAttributesForFullscreen() {
screen_size_for_fullscreen_ = delegate()->GetScreenSize();
std::string width = StringPrintf("%d", screen_size_for_fullscreen_.width());
std::string height = StringPrintf("%d", screen_size_for_fullscreen_.height());
WebElement element = container_->element();
element.setAttribute(WebString::fromUTF8(kWidth), WebString::fromUTF8(width));
element.setAttribute(WebString::fromUTF8(kHeight),
WebString::fromUTF8(height));
element.setAttribute(WebString::fromUTF8(kBorder), WebString::fromUTF8("0"));
// There should be no style settings that matter in fullscreen mode,
// so just replace them instead of appending.
// NOTE: "position: fixed" and "display: block" reset the plugin and
// using %% settings might not work without them (e.g. if the plugin is a
// child of a container element).
std::string style;
style += StringPrintf("width: %s !important; ", width.c_str());
style += StringPrintf("height: %s !important; ", height.c_str());
style += "margin: 0 !important; padding: 0 !important; border: 0 !important";
container_->element().setAttribute(kStyle, WebString::fromUTF8(style));
}
void PluginInstance::ResetSizeAttributesAfterFullscreen() {
screen_size_for_fullscreen_ = gfx::Size();
WebElement element = container_->element();
element.setAttribute(WebString::fromUTF8(kWidth), width_before_fullscreen_);
element.setAttribute(WebString::fromUTF8(kHeight), height_before_fullscreen_);
element.setAttribute(WebString::fromUTF8(kBorder), border_before_fullscreen_);
element.setAttribute(WebString::fromUTF8(kStyle), style_before_fullscreen_);
}
} // namespace ppapi
} // namespace webkit
|