File: pdf_extension_test.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (4680 lines) | stat: -rw-r--r-- 195,569 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include <algorithm>
#include <string>
#include <tuple>
#include <variant>
#include <vector>

#include "base/feature_list.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/hash/hash.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/test/with_feature_override.h"
#include "base/thread_annotations.h"
#include "base/threading/thread_restrictions.h"
#include "base/unguessable_token.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "cc/input/scroll_utils.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/pdf/pdf_extension_test_base.h"
#include "chrome/browser/pdf/pdf_extension_test_util.h"
#include "chrome/browser/pdf/pdf_viewer_stream_manager.h"
#include "chrome/browser/pdf/test_pdf_viewer_stream_manager.h"
#include "chrome/browser/plugins/plugin_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/download/public/common/download_item.h"
#include "components/guest_view/browser/test_guest_view_manager.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/pdf/browser/pdf_frame_util.h"
#include "components/pdf/common/constants.h"
#include "components/pdf/common/pdf_util.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/zoom/page_zoom.h"
#include "components/zoom/test/zoom_test_utils.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/context_menu_interceptor.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/hit_test_region_observer.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_frame_navigation_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/text_input_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "extensions/browser/api/file_system/file_system_api.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/extension_web_contents_observer.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_attach_helper.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view_guest.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "pdf/pdf_features.h"
#include "services/network/public/cpp/features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/context_menu_data/untrustworthy_context_menu_params.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"
#include "third_party/blink/public/common/messaging/transferable_message.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/clipboard_monitor.h"
#include "ui/base/clipboard/clipboard_observer.h"
#include "ui/base/clipboard/test/test_clipboard.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/native_theme/features/native_theme_features.h"
#include "url/gurl.h"

#if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC)
#include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
#endif

namespace {

using ::content::WebContents;
using ::extensions::MimeHandlerViewGuest;
using ::extensions::TestMimeHandlerViewGuest;
using ::guest_view::GuestViewManager;
using ::guest_view::TestGuestViewManager;
using ::guest_view::TestGuestViewManagerFactory;
using ::pdf_extension_test_util::ConvertPageCoordToScreenCoord;
using ::pdf_extension_test_util::GetPdfPluginFrames;
using ::pdf_extension_test_util::SetInputFocusOnPlugin;
using ::testing::IsEmpty;
using ::testing::StartsWith;

const int kNumberLoadTestParts = 10;

#if BUILDFLAG(IS_MAC)
const int kDefaultKeyModifier = blink::WebInputEvent::kMetaKey;
#else
const int kDefaultKeyModifier = blink::WebInputEvent::kControlKey;
#endif

// Javascript evaluation used to check if inner PDF frames can be accessed.
constexpr char kNestedWindowFramesUndefinedCheck[] =
    "window.frames[0][0] === undefined";

struct PDFExtensionLoadTestPassToString {
  std::string operator()(
      const ::testing::TestParamInfo<std::tuple<int, bool>>& i) const {
    return std::string(std::get<1>(i.param) ? "OOPIF_" : "GUESTVIEW_") +
           base::NumberToString(std::get<0>(i.param));
  }
};

struct PDFExtensionIsolatedContentTestPassToString {
  std::string operator()(
      const ::testing::TestParamInfo<std::tuple<bool, bool>>& i) const {
    return std::string(std::get<1>(i.param) ? "OOPIF_" : "GUESTVIEW_") +
           std::string(std::get<0>(i.param) ? "SITE_ISOLATED"
                                            : "SITE_UNISOLATED");
  }
};

// Calling PluginService::GetPlugins ensures that LoadPlugins is called
// internally. This is an asynchronous task and this method uses a run loop to
// wait for the loading task to complete.
void WaitForPluginServiceToLoad() {
  base::RunLoop run_loop;
  content::PluginService::GetPluginsCallback callback = base::BindOnce(
      [](base::RepeatingClosure quit,
         const std::vector<content::WebPluginInfo>& unused) { quit.Run(); },
      run_loop.QuitClosure());
  content::PluginService::GetInstance()->GetPlugins(std::move(callback));
  run_loop.Run();
}

}  // namespace

class PDFExtensionTest : public base::test::WithFeatureOverride,
                         public PDFExtensionTestBase {
 public:
  PDFExtensionTest()
      : base::test::WithFeatureOverride(chrome_pdf::features::kPdfOopif) {}

  bool UseOopif() const override { return GetParam(); }
};

using PDFExtensionTestWithoutOopifOverride = PDFExtensionTestBase;

class PDFExtensionTestWithPartialLoading : public PDFExtensionTest {
 protected:
  std::vector<base::test::FeatureRefAndParams> GetEnabledFeatures()
      const override {
    auto enabled = PDFExtensionTest::GetEnabledFeatures();
    enabled.push_back({chrome_pdf::features::kPdfIncrementalLoading, {}});
    enabled.push_back({chrome_pdf::features::kPdfPartialLoading, {}});
    return enabled;
  }
};

// Historically, https://crrev.com/352991 focused the PDF embed element when it
// was created. To preserve this behavior, make sure the extension frame has
// focus for a full page PDF viewer on creation.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, FullPagePdfHasFocus) {
  // Load test PDF, and verify the text area has focus.
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));

  ASSERT_EQ(GetActiveWebContents()->GetFocusedFrame(), extension_host);
}

// For GuestView PDF, this test verifies that when a PDF is loaded, the embedder
// WebContents' html consists of a single <embed> tag with appropriate
// properties. It also verifies that the guest WebContents finishes loading. For
// OOPIF PDF, this test verifies that extension frame finished loading. For
// both, the WebContents and the extension frame should have the correct URL for
// the PDF extension.
// TODO(wjmaclean): Are there any attributes we can/should test with respect to
// the extension's loaded html?
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, PdfExtensionLoaded) {
  // Load test PDF.
  const GURL main_url(embedded_test_server()->GetURL("/pdf/test.pdf"));
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(main_url);
  ASSERT_TRUE(extension_host);
  auto* web_contents = GetActiveWebContents();
  auto* primary_main_frame = web_contents->GetPrimaryMainFrame();

  // Verify we loaded the extension.
  const GURL extension_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html");
  EXPECT_EQ(extension_url, extension_host->GetLastCommittedURL());
  EXPECT_EQ(main_url, primary_main_frame->GetLastCommittedURL());

  // Make sure the embedder has the correct html boilerplate. GuestView PDF
  // only, as OOPIF PDF hides the html using shadow DOM.
  if (!UseOopif()) {
    EXPECT_EQ(
        1, content::EvalJs(primary_main_frame, "document.body.children.length;")
               .ExtractInt());
    EXPECT_EQ("EMBED", content::EvalJs(primary_main_frame,
                                       "document.body.firstChild.tagName;")
                           .ExtractString());
    EXPECT_EQ(
        "application/pdf",
        content::EvalJs(primary_main_frame, "document.body.firstChild.type;")
            .ExtractString());
    EXPECT_EQ("about:blank", content::EvalJs(primary_main_frame,
                                             "document.body.firstChild.src;")
                                 .ExtractString());
    EXPECT_TRUE(
        content::EvalJs(primary_main_frame,
                        "document.body.firstChild.hasAttribute('internalid');")
            .ExtractBool());
  }
}

// The pdf extension's frame should not allow cross-origin navigations. They
// should be blocked, but not crash.
// https://crbug.com/394513280
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       NoCrossOriginNavigationFromPdfExtensionFrame) {
  if (UseOopif()) {
    // This test only applies to MimeHandlerViewGuest PDF.
    GTEST_SKIP();
  }

  const GURL main_url(embedded_test_server()->GetURL("/pdf/test.pdf"));
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(main_url);
  ASSERT_TRUE(extension_host);

  content::TestFrameNavigationObserver frame_nav_observer(extension_host);
  ASSERT_TRUE(content::ExecJs(extension_host,
                              "window.location = 'https://example.com';"));
  frame_nav_observer.Wait();

  EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT,
            frame_nav_observer.last_net_error_code());
  const GURL extension_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html");
  EXPECT_EQ(extension_url, extension_host->GetLastCommittedURL());
}

// A test to verify that the presence of a pending NavigationEntry on the
// NavigationController of a WebContents hosting a PDF does not affect the
// value returned by WebContentsImpl::GetPendingZoomLevel() when called on
// the PDF extension frame's RenderWidgetHost.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       ZoomNotAffectedByPendingNavigationEntry) {
  // Set default zoom factor to 200%.
  auto* web_contents = GetActiveWebContents();
  content::HostZoomMap::GetForWebContents(web_contents)
      ->SetDefaultZoomLevel(blink::ZoomFactorToZoomLevel(2.0));

  // Load PDF.
  const GURL main_url(embedded_test_server()->GetURL("/pdf/test.pdf"));
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(main_url);
  ASSERT_TRUE(extension_host);

  // Verify PDF extension frame has zoom factor 100%.
  EXPECT_EQ(
      blink::ZoomFactorToZoomLevel(1.0),
      content::GetPendingZoomLevel(extension_host->GetRenderWidgetHost()));

  // Navigate and immediately check GetPendingZoomLevel stays at factor 100%.
  auto& controller = web_contents->GetController();
  controller.LoadURLWithParams(content::NavigationController::LoadURLParams(
      embedded_test_server()->GetURL("/title1.html")));
  EXPECT_EQ(
      blink::ZoomFactorToZoomLevel(1.0),
      content::GetPendingZoomLevel(extension_host->GetRenderWidgetHost()));
  // Verify the navigation is still pending. This gives assurance that the
  // preceding call to GetPendingZoomLevel did encounter the condition being
  // tested.
  EXPECT_NE(nullptr, controller.GetPendingEntry());
}

// Helper class to allow pausing the asynchronous attachment of an inner
// WebContents between MimeHandlerViewAttachHelper's AttachToOuterWebContents()
// and ResumeAttachOrDestroy().  This corresponds to the point where the inner
// WebContents has been created but not yet attached or navigated.
class InnerWebContentsAttachDelayer {
 public:
  explicit InnerWebContentsAttachDelayer(
      content::RenderFrameHost* outer_frame) {
    auto* mime_handler_view_helper =
        extensions::MimeHandlerViewAttachHelper::Get(
            outer_frame->GetProcess()->GetDeprecatedID());
    mime_handler_view_helper->set_resume_attach_callback_for_testing(
        base::BindOnce(&InnerWebContentsAttachDelayer::ResumeAttachCallback,
                       base::Unretained(this)));
  }

  void ResumeAttachCallback(base::OnceClosure resume_closure) {
    // Called to continue in the test while the attachment is paused. The
    // attachment will continue when the test calls ResumeAttach.
    resume_closure_ = std::move(resume_closure);
    run_loop_.Quit();
  }

  void WaitForAttachStart() { run_loop_.Run(); }

  void ResumeAttach() {
    ASSERT_TRUE(resume_closure_);
    std::move(resume_closure_).Run();
  }

 private:
  base::OnceClosure resume_closure_;
  base::RunLoop run_loop_;
};

// Ensure that when the only other PDF instance closes in the middle of
// attaching an inner WebContents for a PDF, the inner WebContents can still
// successfully complete its attachment and subsequent navigation.  See
// https://crbug.com/1295431.
// See PDFExtensionOopifTest.PdfExtensionLoadedWhileOldPdfCloses for the OOPIF
// PDF version.
IN_PROC_BROWSER_TEST_F(PDFExtensionTestWithoutOopifOverride,
                       PdfExtensionLoadedWhileOldPdfCloses) {
  // Load test PDF in first tab.
  const GURL main_url(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));
  auto* primary_main_frame = GetActiveWebContents()->GetPrimaryMainFrame();

  // Verify the PDF has loaded.
  auto* guest_view = GetGuestViewManager()->WaitForSingleGuestViewCreated();
  ASSERT_TRUE(guest_view);
  EXPECT_NE(primary_main_frame, guest_view->GetGuestMainFrame());
  TestMimeHandlerViewGuest::WaitForGuestLoadStartThenStop(guest_view);

  // Verify we loaded the extension.
  const GURL extension_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html");
  EXPECT_EQ(extension_url,
            guest_view->GetGuestMainFrame()->GetLastCommittedURL());
  EXPECT_EQ(main_url, primary_main_frame->GetLastCommittedURL());

  // Open another tab and navigate it to a same-site non-PDF URL.
  ui_test_utils::TabAddedWaiter add_tab1(browser());
  chrome::NewTab(browser());
  add_tab1.Wait();
  ASSERT_EQ(2, browser()->tab_strip_model()->count());
  WebContents* new_web_contents =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  ASSERT_EQ(new_web_contents, GetActiveWebContents());
  const GURL non_pdf_url(embedded_test_server()->GetURL("/title1.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), non_pdf_url));

  // Start loading another PDF in the new tab, but pause during guest attach.
  // It is important that the PDF navigation uses the same RFH as `delayer`.
  InnerWebContentsAttachDelayer delayer(
      new_web_contents->GetPrimaryMainFrame());
  content::TestNavigationObserver navigation_observer(new_web_contents);
  new_web_contents->GetController().LoadURLWithParams(
      content::NavigationController::LoadURLParams(main_url));
  delayer.WaitForAttachStart();

  // Close the first tab, destroying the first PDF while the second PDF is in
  // the middle of initialization. In https://crbug.com/1295431, the extension
  // process exited here and caused a crash when the second PDF resumed.
  EXPECT_EQ(2U, GetGuestViewManager()->GetCurrentGuestCount());
  ASSERT_EQ(2, browser()->tab_strip_model()->count());
  browser()->tab_strip_model()->CloseWebContentsAt(
      0, TabCloseTypes::CLOSE_USER_GESTURE);
  ASSERT_EQ(1, browser()->tab_strip_model()->count());
  // `TestGuestViewManager` manages the guests by the order of creation.
  GetGuestViewManager()->WaitForFirstGuestDeleted();
  EXPECT_EQ(1U, GetGuestViewManager()->GetCurrentGuestCount());
  primary_main_frame = new_web_contents->GetPrimaryMainFrame();

  // Now resume the guest attachment and ensure the second PDF loads without
  // crashing.
  delayer.ResumeAttach();
  navigation_observer.Wait();
  auto* guest_view2 = GetGuestViewManager()->WaitForSingleGuestViewCreated();
  ASSERT_TRUE(guest_view2);
  EXPECT_NE(primary_main_frame, guest_view2->GetGuestMainFrame());
  TestMimeHandlerViewGuest::WaitForGuestLoadStartThenStop(guest_view2);

  // Verify we loaded the extension.
  EXPECT_EQ(extension_url,
            guest_view2->GetGuestMainFrame()->GetLastCommittedURL());
  EXPECT_EQ(main_url, primary_main_frame->GetLastCommittedURL());
}

// This test verifies that when a PDF is served with a restrictive
// Content-Security-Policy, the embed tag is still sized correctly.
// Regression test for https://crbug.com/271452.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, CSPDoesNotBlockEmbedStyles) {
  const GURL main_url(embedded_test_server()->GetURL("/pdf/test-csp.pdf"));
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(main_url);
  ASSERT_TRUE(extension_host);
  auto* primary_main_frame = GetActiveWebContents()->GetPrimaryMainFrame();

  // Verify the extension was loaded.
  const GURL extension_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html");
  EXPECT_EQ(extension_url, extension_host->GetLastCommittedURL());
  EXPECT_EQ(main_url, primary_main_frame->GetLastCommittedURL());

  // Verify that the plugin occupies all of the page area.
  const gfx::Rect embedder_rect =
      primary_main_frame->GetView()->GetViewBounds();
  const gfx::Rect extension_rect = extension_host->GetView()->GetViewBounds();
  EXPECT_EQ(embedder_rect, extension_rect);
}

// This test verifies that when a PDF is served with
// Content-Security-Policy: sandbox, this is ignored and the PDF is displayed.
// Regression test for https://crbug.com/1187122.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, CSPWithSandboxDoesNotBlockPDF) {
  const GURL main_url(
      embedded_test_server()->GetURL("/pdf/test-csp-sandbox.pdf"));
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(main_url);
  ASSERT_TRUE(extension_host);

  // Verify the extension was loaded.
  const GURL extension_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html");
  EXPECT_EQ(extension_url, extension_host->GetLastCommittedURL());
  EXPECT_EQ(
      main_url,
      GetActiveWebContents()->GetPrimaryMainFrame()->GetLastCommittedURL());
}

// This test verifies that Content-Security-Policy's frame-ancestors 'none'
// directive is effective on a PDF response.
// Regression test for https://crbug.com/1107535.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, CSPFrameAncestorsCanBlockEmbedding) {
  WebContents* web_contents = GetActiveWebContents();
  content::WebContentsConsoleObserver console_observer(web_contents);
  console_observer.SetPattern(
      "*because an ancestor violates the following Content Security Policy "
      "directive: \"frame-ancestors 'none'*");

  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL(
                     "/pdf/frame-test-csp-frame-ancestors-none.html")));

  ASSERT_TRUE(console_observer.Wait());

  EXPECT_EQ(0, CountPDFProcesses());
}

// This test verifies that Content-Security-Policy's frame-ancestors directive
// overrides an X-Frame-Options header on a PDF response.
// Regression test for https://crbug.com/1107535.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       CSPFrameAncestorsOverridesXFrameOptions) {
  const GURL main_url(
      embedded_test_server()->GetURL("/pdf/frame-test-csp-and-xfo.html"));

  content::RenderFrameHost* extension_host;
  if (UseOopif()) {
    extension_host = LoadPdfInFirstChildGetExtensionHost(main_url);
  } else {
    // The URL uses an iframe element to embed the PDF, so
    // `LoadPdfInFirstChild()` can't be used here.
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));

    // Verify the pdf has loaded.
    auto* guest_view = GetGuestViewManager()->WaitForSingleGuestViewCreated();
    ASSERT_TRUE(guest_view);
    TestMimeHandlerViewGuest::WaitForGuestLoadStartThenStop(guest_view);
    extension_host = guest_view->GetGuestMainFrame();
  }
  ASSERT_TRUE(extension_host);

  auto* primary_main_frame = GetActiveWebContents()->GetPrimaryMainFrame();
  EXPECT_NE(primary_main_frame, extension_host);

  // Verify the extension was loaded.
  const GURL extension_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html");
  EXPECT_EQ(extension_url, extension_host->GetLastCommittedURL());
  EXPECT_EQ(
      main_url,
      GetActiveWebContents()->GetPrimaryMainFrame()->GetLastCommittedURL());
}

class PDFExtensionLoadTest
    : public PDFExtensionTestWithoutOopifOverride,
      public testing::WithParamInterface<std::tuple<int, bool>> {
 protected:
  int load_test_part() const { return std::get<0>(GetParam()); }

  bool UseOopif() const override { return std::get<1>(GetParam()); }

  // Load all the PDFs contained in chrome/test/data/<dir_name>. The files are
  // sharded across kNumberLoadTestParts using base::PersistentHash(filename).
  void LoadAllPdfsTest(const std::string& dir_name) {
    base::ScopedAllowBlockingForTesting allow_blocking;
    base::FilePath test_data_dir;
    ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
    base::FileEnumerator file_enumerator(test_data_dir.AppendASCII(dir_name),
                                         false, base::FileEnumerator::FILES,
                                         FILE_PATH_LITERAL("*.pdf"));

    size_t count = 0;
    for (base::FilePath file_path = file_enumerator.Next(); !file_path.empty();
         file_path = file_enumerator.Next()) {
      std::string filename = file_path.BaseName().MaybeAsASCII();
      ASSERT_FALSE(filename.empty());

      std::string pdf_file = dir_name + "/" + filename;
      SCOPED_TRACE(pdf_file);
      if (base::PersistentHash(filename) % kNumberLoadTestParts ==
          load_test_part()) {
        LOG(INFO) << "Loading: " << pdf_file;
        testing::AssertionResult success =
            LoadPdf(embedded_test_server()->GetURL("/" + pdf_file));
        if (pdf_file == "pdf_private/cfuzz5.pdf")
          continue;
        EXPECT_EQ(PdfIsExpectedToLoad(pdf_file), success) << pdf_file;
      }
      ++count;
    }
    // Assume that there is at least 1 pdf in the directory to guard against
    // someone deleting the directory and silently making the test pass.
    ASSERT_GE(count, 1u);
  }
};

#if BUILDFLAG(IS_LINUX)
#define MAYBE_Load DISABLED_Load
#else
#define MAYBE_Load Load
#endif
IN_PROC_BROWSER_TEST_P(PDFExtensionLoadTest, MAYBE_Load) {
  LoadAllPdfsTest("pdf");
}

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
IN_PROC_BROWSER_TEST_P(PDFExtensionLoadTest, LoadPrivate) {
  LoadAllPdfsTest("pdf_private");
}
#endif

// We break PDFExtensionLoadTest up into kNumberLoadTestParts.
INSTANTIATE_TEST_SUITE_P(
    PDFTestFiles,
    PDFExtensionLoadTest,
    testing::Combine(testing::Range(0, kNumberLoadTestParts), testing::Bool()),
    PDFExtensionLoadTestPassToString());

using PDFExtensionBlobNavigationTest = PDFExtensionTest;

IN_PROC_BROWSER_TEST_P(PDFExtensionBlobNavigationTest, NewTab) {
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(),
      embedded_test_server()->GetURL("/pdf/blob_navigation_new_tab.html")));

  // Calling `window.open` without a user gesture will be blocked by the popup
  // blocker. `ExecJs()` emulates a user gesture which bypasses the restriction.
  content::TestNavigationObserver navigation_observer(nullptr);
  navigation_observer.StartWatchingNewWebContents();
  ASSERT_TRUE(content::ExecJs(GetActiveWebContents(), "openBlobPdfInNewTab()"));
  navigation_observer.Wait();

  ASSERT_EQ(browser()->tab_strip_model()->count(), 2);
  WebContents* new_tab_contents =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  EXPECT_TRUE(EnsureFullPagePDFHasLoadedWithValidFrameTree(
      new_tab_contents, /*allow_multiple_frames=*/false));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionBlobNavigationTest,
                       NewTabWithCrossOriginEmbedderPolicy) {
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL(
                     "/pdf/test-coep-blob-navigation-new-tab.html")));

  content::TestNavigationObserver navigation_observer(nullptr);
  navigation_observer.StartWatchingNewWebContents();
  ASSERT_TRUE(content::ExecJs(GetActiveWebContents(), "openBlobPdfInNewTab()"));
  navigation_observer.Wait();

  ASSERT_EQ(browser()->tab_strip_model()->count(), 2);
  EXPECT_TRUE(EnsureFullPagePDFHasLoadedWithValidFrameTree(
      browser()->tab_strip_model()->GetWebContentsAt(1),
      /*allow_multiple_frames=*/false));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionBlobNavigationTest, SameTab) {
  ASSERT_TRUE(ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
      browser(),
      embedded_test_server()->GetURL("/pdf/blob_navigation_same_tab.html"),
      /*number_of_navigations=*/2));
  EXPECT_TRUE(EnsureFullPagePDFHasLoadedWithValidFrameTree(
      GetActiveWebContents(), /*allow_multiple_frames=*/false));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, LoadInPlatformApp) {
  // TODO(crbug.com/40268279): Remove this once the test passes for OOPIF PDF.
  if (UseOopif()) {
    GTEST_SKIP();
  }

  extensions::TestExtensionDir dir;
  dir.WriteManifest(R"(
    {
      "name": "PDFExtensionTest App",
      "version": "1.0",
      "manifest_version": 2,
      "app": {
        "background": {
          "scripts": ["background_script.js"]
        }
      }
    }
  )");

  dir.WriteFile(FILE_PATH_LITERAL("background_script.js"), R"(
    chrome.app.runtime.onLaunched.addListener(() => {
      chrome.app.window.create('test.pdf', () => {
        chrome.test.notifyPass();
      });
    });
  )");

  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    std::string pdf_contents;
    ASSERT_TRUE(base::ReadFileToString(
        GetTestResourcesParentDir().AppendASCII("pdf").AppendASCII("test.pdf"),
        &pdf_contents));
    dir.WriteFile(FILE_PATH_LITERAL("test.pdf"), pdf_contents);
  }

  extensions::ResultCatcher result_catcher;
  ASSERT_TRUE(LoadAndLaunchApp(dir.UnpackedPath(), /*uses_guest_view=*/true));
  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();

  auto* app_registry = extensions::AppWindowRegistry::Get(browser()->profile());
  ASSERT_TRUE(app_registry);
  const extensions::AppWindowRegistry::AppWindowList& app_windows =
      app_registry->app_windows();
  ASSERT_EQ(app_windows.size(), 1u);
  extensions::AppWindow* window = app_windows.front();
  ASSERT_TRUE(window);
  content::WebContents* app_contents = window->web_contents();

  ASSERT_TRUE(content::WaitForLoadStop(app_contents));
  EXPECT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(app_contents));
}

class DownloadAwaiter : public content::DownloadManager::Observer {
 public:
  DownloadAwaiter() = default;
  ~DownloadAwaiter() override = default;

  const GURL& GetLastUrl() {
    // Wait until the download has been created.
    download_run_loop_.Run();
    return last_url_;
  }

  // content::DownloadManager::Observer implementation.
  void OnDownloadCreated(content::DownloadManager* manager,
                         download::DownloadItem* item) override {
    last_url_ = item->GetURL();
    download_run_loop_.Quit();
  }

 private:
  base::RunLoop download_run_loop_;
  GURL last_url_;
};

// Tests behavior when the PDF plugin is disabled in preferences.
class PDFPluginDisabledTest : public PDFExtensionTest {
 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    PDFExtensionTest::SetUpCommandLine(command_line);

    command_line->AppendSwitch(switches::kEnablePluginPlaceholderTesting);
  }

  void SetUpOnMainThread() override {
    PDFExtensionTest::SetUpOnMainThread();

    content::BrowserContext* browser_context =
        GetActiveWebContents()->GetBrowserContext();
    Profile* profile = Profile::FromBrowserContext(browser_context);
    profile->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysOpenPdfExternally,
                                    true);

    content::DownloadManager* download_manager =
        browser_context->GetDownloadManager();
    download_awaiter_ = std::make_unique<DownloadAwaiter>();
    download_manager->AddObserver(download_awaiter_.get());
  }

  void TearDownOnMainThread() override {
    content::BrowserContext* browser_context =
        GetActiveWebContents()->GetBrowserContext();
    content::DownloadManager* download_manager =
        browser_context->GetDownloadManager();
    download_manager->RemoveObserver(download_awaiter_.get());

    // Cancel all downloads to shut down cleanly.
    std::vector<raw_ptr<download::DownloadItem, VectorExperimental>> downloads;
    download_manager->GetAllDownloads(&downloads);
    for (download::DownloadItem* item : downloads) {
      item->Cancel(false);
    }

    PDFExtensionTest::TearDownOnMainThread();
  }

  void ClickOpenButtonInIframe() {
    content::RenderFrameHost* iframe_render_frame_host =
        ChildFrameAt(GetActiveWebContents(), 0);
    ASSERT_TRUE(iframe_render_frame_host);
    ASSERT_TRUE(
        content::ExecJs(iframe_render_frame_host,
                        "document.getElementById('open-button').click();"));
  }

  void ValidateSingleSuccessfulDownloadAndNoPDFPluginLaunch() {
    // Validate that we downloaded a single PDF and didn't launch the PDF
    // plugin.
    EXPECT_EQ(embedded_test_server()->GetURL("/pdf/test.pdf"),
              AwaitAndGetLastDownloadedUrl());
    EXPECT_EQ(1u, GetNumberOfDownloads());
    EXPECT_EQ(0, CountPDFProcesses());
  }

 private:
  size_t GetNumberOfDownloads() {
    content::BrowserContext* browser_context =
        GetActiveWebContents()->GetBrowserContext();
    content::DownloadManager* download_manager =
        browser_context->GetDownloadManager();

    std::vector<raw_ptr<download::DownloadItem, VectorExperimental>> downloads;
    download_manager->GetAllDownloads(&downloads);
    return downloads.size();
  }

  const GURL& AwaitAndGetLastDownloadedUrl() {
    return download_awaiter_->GetLastUrl();
  }

  std::unique_ptr<DownloadAwaiter> download_awaiter_;
};

IN_PROC_BROWSER_TEST_P(PDFPluginDisabledTest, DirectNavigationToPDF) {
  // Navigate to a PDF and test that it is downloaded.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/test.pdf")));

  ValidateSingleSuccessfulDownloadAndNoPDFPluginLaunch();
}

// TODO(crbug.com/40762344): fix flakiness and reenable. Also, that test
// became flaky on Windows, see crbug.com/1323701.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || \
    BUILDFLAG(IS_WIN)
#define MAYBE_EmbedPdfPlaceholderWithCSP DISABLED_EmbedPdfPlaceholderWithCSP
#else
#define MAYBE_EmbedPdfPlaceholderWithCSP EmbedPdfPlaceholderWithCSP
#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) ||
        // BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_P(PDFPluginDisabledTest,
                       MAYBE_EmbedPdfPlaceholderWithCSP) {
  // Navigate to a page with CSP that uses <embed> to embed a PDF as a plugin.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/pdf_embed_csp.html")));
  PluginTestUtils::WaitForPlaceholderReady(GetActiveWebContents(), "pdf_embed");

  // Fake a click on the <embed>, then press Enter to trigger the download.
  gfx::Point point_in_pdf(100, 100);
  content::SimulateMouseClickAt(GetActiveWebContents(), kDefaultKeyModifier,
                                blink::WebMouseEvent::Button::kLeft,
                                point_in_pdf);
  content::SimulateKeyPress(GetActiveWebContents(), ui::DomKey::ENTER,
                            ui::DomCode::ENTER, ui::VKEY_RETURN, false, false,
                            false, false);

  ValidateSingleSuccessfulDownloadAndNoPDFPluginLaunch();
}

#if BUILDFLAG(IS_CHROMEOS)
// TODO(crbug.com/40923384): Deflake and reenable the test.
#define MAYBE_IframePdfPlaceholderWithCSP DISABLED_IframePdfPlaceholderWithCSP
#else
#define MAYBE_IframePdfPlaceholderWithCSP IframePdfPlaceholderWithCSP
#endif
IN_PROC_BROWSER_TEST_P(PDFPluginDisabledTest,
                       MAYBE_IframePdfPlaceholderWithCSP) {
  // Navigate to a page that uses <iframe> to embed a PDF as a plugin.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/pdf_iframe_csp.html")));

  ClickOpenButtonInIframe();
  ValidateSingleSuccessfulDownloadAndNoPDFPluginLaunch();
}

IN_PROC_BROWSER_TEST_P(PDFPluginDisabledTest,
                       IframePlaceholderInjectedIntoNewWindow) {
  // This is an unusual test to verify crbug.com/924823. We are injecting the
  // HTML for a PDF IFRAME into a newly created popup with an undefined URL.
  ASSERT_TRUE(
      content::EvalJs(
          GetActiveWebContents(),
          content::JsReplace(
              "new Promise((resolve) => {"
              "  var popup = window.open();"
              "  popup.document.writeln("
              "      '<iframe id=\"pdf_iframe\" src=\"' + $1 + '\"></iframe>');"
              "  var iframe = popup.document.getElementById('pdf_iframe');"
              "  iframe.onload = () => resolve(true);"
              "});",
              embedded_test_server()->GetURL("/pdf/test.pdf").spec()))
          .ExtractBool());

  ClickOpenButtonInIframe();
  ValidateSingleSuccessfulDownloadAndNoPDFPluginLaunch();
}

// Test that if the plugin tries to load a URL that redirects then it will fail
// to load. This is to avoid the source origin of the document changing during
// the redirect, which can have security implications. https://crbug.com/653749.
//
// Note that this can happen only during partial loading, as the initial URL
// load is handled by MimeHandlerView, and the plugin only gets the response.
IN_PROC_BROWSER_TEST_P(PDFExtensionTestWithPartialLoading,
                       PartialRedirectsFailInPlugin) {
  // Should match values used by `chrome_pdf::DocumentLoaderImpl`.
  constexpr size_t kDefaultRequestSize = 65536;
  constexpr size_t kChunkCloseDistance = 10;

  std::string pdf_contents;
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    base::FilePath test_data_dir;
    ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
    ASSERT_TRUE(
        base::ReadFileToString(test_data_dir.AppendASCII("pdf").AppendASCII(
                                   "test-ranges-linearized.pdf"),
                               &pdf_contents));
  }
  ASSERT_GT(pdf_contents.size(),
            kDefaultRequestSize * (kChunkCloseDistance + 2));

  // Use an additional test server, to allow customizing the response handling.
  net::test_server::EmbeddedTestServer test_server;

  constexpr char kActualPdf[] = "/pdf/test-ranges-linearized.pdf";
  constexpr char kSimulatedPdf[] = "/simulated/test-ranges-linearized.pdf";
  net::test_server::ControllableHttpResponse initial_response(&test_server,
                                                              kSimulatedPdf);
  net::test_server::ControllableHttpResponse followup_response(&test_server,
                                                               kSimulatedPdf);
  auto handle = test_server.StartAndReturnHandle();

  WebContents* contents = GetActiveWebContents();
  content::TestNavigationObserver navigation_observer(contents);
  contents->GetController().LoadURLWithParams(
      content::NavigationController::LoadURLParams(
          test_server.GetURL(kSimulatedPdf)));

  {
    SCOPED_TRACE("Waiting for initial request");
    initial_response.WaitForRequest();
  }
  initial_response.Send("HTTP/1.1 200 OK\r\n");
  initial_response.Send("Accept-Ranges: bytes\r\n");
  initial_response.Send(
      base::StringPrintf("Content-Length: %zu\r\n", pdf_contents.size()));
  initial_response.Send("Content-Type: application/pdf\r\n");
  initial_response.Send("\r\n");
  initial_response.Send(pdf_contents.substr(0, kDefaultRequestSize));

  navigation_observer.Wait();
  ASSERT_TRUE(navigation_observer.last_navigation_succeeded());

  {
    SCOPED_TRACE("Waiting for follow-up request");
    followup_response.WaitForRequest();
  }
  followup_response.Send("HTTP/1.1 301 Moved Permanently\r\n");
  followup_response.Send(base::StringPrintf(
      "Location: %s\r\n",
      embedded_test_server()->GetURL(kActualPdf).spec().c_str()));
  followup_response.Send("\r\n");
  followup_response.Done();

  // TODO(crbug.com/40189769): Load success or failure is non-deterministic
  // currently, due to races between viewport messages and loading. For this
  // test, we only care that loading terminated, not about success or failure.
  std::ignore = pdf_extension_test_util::EnsurePDFHasLoaded(contents);
}

// Ensure that the internal PDF plugin application/x-google-chrome-pdf won't be
// loaded if it's not loaded in the chrome extension page.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, EnsureInternalPluginDisabled) {
  std::string url = embedded_test_server()->GetURL("/pdf/test.pdf").spec();
  std::string data_url =
      "data:text/html,"
      "<html><body>"
      "<embed type=\"application/x-google-chrome-pdf\" src=\"" +
      url +
      "\">"
      "</body></html>";
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL(data_url)));
  WebContents* web_contents = GetActiveWebContents();
  ASSERT_EQ(false,
            content::EvalJs(
                web_contents,
                "var plugin_loaded = "
                "    document.getElementsByTagName('embed')[0].postMessage !== "
                "undefined;"
                "plugin_loaded;"));
}

// Ensure cross-origin replies won't work for getSelectedText.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, EnsureCrossOriginRepliesBlocked) {
  std::string url = embedded_test_server()->GetURL("/pdf/test.pdf").spec();
  std::string data_url =
      "data:text/html,"
      "<html><body>"
      "<embed type=\"application/pdf\" src=\"" +
      url +
      "\">"
      "</body></html>";

  content::RenderFrameHost* extension_host =
      LoadPdfInFirstChildGetExtensionHost(GURL(data_url));
  ASSERT_TRUE(extension_host);

  TestGetSelectedTextReply(extension_host, false);
}

// Ensure same-origin replies do work for getSelectedText.
// The full page PDF embedder frame can't post messages to the PDF extension
// frame, so it can't post a getSelectedText message.
IN_PROC_BROWSER_TEST_F(PDFExtensionTestWithoutOopifOverride,
                       EnsureSameOriginRepliesAllowed) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  TestGetSelectedTextReply(extension_host, true);
}

// TODO(crbug.com/40647731): Should be allowed?
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, EnsureOpaqueOriginRepliesBlocked) {
  content::RenderFrameHost* extension_host =
      LoadPdfInFirstChildGetExtensionHost(
          embedded_test_server()->GetURL("/pdf/data_url_rectangles.html"));
  ASSERT_TRUE(extension_host);

  TestGetSelectedTextReply(extension_host, false);
}

// Ensure that the PDF component extension cannot be loaded directly.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, BlockDirectAccess) {
  const std::string pattern =
      UseOopif()
          ? "*Failed to get StreamContainer*"
          : "*Streams are only available from a mime handler view guest.*";

  WebContents* web_contents = GetActiveWebContents();

  content::WebContentsConsoleObserver console_observer(web_contents);
  console_observer.SetPattern(pattern);
  GURL forbidden_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?"
      "https://example.com/notrequested.pdf");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), forbidden_url));

  ASSERT_TRUE(console_observer.Wait());

  EXPECT_EQ(0, CountPDFProcesses());
}

// This test ensures that PDF can be loaded from local file
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, EnsurePDFFromLocalFileLoads) {
  GURL test_pdf_url;
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    base::FilePath test_data_dir;
    ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
    test_data_dir = test_data_dir.Append(FILE_PATH_LITERAL("pdf"));
    base::FilePath test_data_file = test_data_dir.AppendASCII("test.pdf");
    ASSERT_TRUE(PathExists(test_data_file));
    test_pdf_url = GURL("file://" + test_data_file.MaybeAsASCII());
  }
  EXPECT_TRUE(LoadPdf(test_pdf_url));

  EXPECT_EQ(1, CountPDFProcesses());
}

// Tests that PDF with no filename extension can be loaded from local file.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, ExtensionlessPDFLocalFileLoads) {
  GURL test_pdf_url;
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    base::FilePath test_data_dir;
    ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
    test_data_dir = test_data_dir.AppendASCII("pdf");
    base::FilePath test_data_file = test_data_dir.AppendASCII("imgpdf");
    ASSERT_TRUE(PathExists(test_data_file));
    test_pdf_url = GURL("file://" + test_data_file.MaybeAsASCII());
  }
  EXPECT_TRUE(LoadPdf(test_pdf_url));

  EXPECT_EQ(1, CountPDFProcesses());
}

// This test ensures that link permissions are enforced properly in PDFs.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, LinkPermissions) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  // chrome://favicon links should be allowed for PDFs, while chrome://settings
  // links should not.
  GURL valid_link_url(std::string(chrome::kChromeUIFaviconURL) +
                      "https://www.google.ca/");
  GURL invalid_link_url(chrome::kChromeUISettingsURL);

  GURL unfiltered_valid_link_url(valid_link_url);
  content::RenderProcessHost* rph = extension_host->GetProcess();
  rph->FilterURL(true, &valid_link_url);
  rph->FilterURL(true, &invalid_link_url);

  // Invalid link URLs should be changed to "about:blank#blocked" when filtered.
  EXPECT_EQ(unfiltered_valid_link_url, valid_link_url);
  EXPECT_EQ(GURL(content::kBlockedURL), invalid_link_url);
}

// This test ensures that titles are set properly for PDFs without /Title.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, TabTitleWithNoTitle) {
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));

  const std::u16string kExpectedTitle = u"test.pdf";
  EXPECT_EQ(kExpectedTitle, GetEmbedderWebContents()
                                ->GetController()
                                .GetLastCommittedEntry()
                                ->GetTitleForDisplay());
  EXPECT_EQ(kExpectedTitle, GetActiveWebContents()->GetTitle());
}

// This test ensures that titles are set properly for PDFs with /Title.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, TabTitleWithTitle) {
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test-title.pdf")));

  const std::u16string kExpectedTitle = u"PDF title test";
  EXPECT_EQ(kExpectedTitle, GetEmbedderWebContents()
                                ->GetController()
                                .GetLastCommittedEntry()
                                ->GetTitleForDisplay());
  EXPECT_EQ(kExpectedTitle, GetActiveWebContents()->GetTitle());
}

// This test ensures that titles are set properly for embedded PDFs (using data
// URL). PDF /Title should be ignored for embedded PDFs.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, TabTitleWithEmbeddedPdfDataUrl) {
  std::string url =
      embedded_test_server()->GetURL("/pdf/test-title.pdf").spec();
  std::string data_url =
      "data:text/html,"
      "<html><head><title>TabTitleWithEmbeddedPdf</title></head><body>"
      "<embed type=\"application/pdf\" src=\"" +
      url +
      "\"></body></html>";
  ASSERT_TRUE(LoadPdfInFirstChild(GURL(data_url)));
  EXPECT_EQ(u"TabTitleWithEmbeddedPdf", GetActiveWebContents()->GetTitle());
}

// This test ensures that tab titles are set properly for embedded PDFs.
// PDF /Title should be ignored for embedded PDFs.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, TabTitleWithEmbeddedPdf) {
  // Load page with embedded PDF and make sure it succeeds.
  ASSERT_TRUE(LoadPdfInFirstChild(
      embedded_test_server()->GetURL("/pdf/pdf_embed.html")));
  WebContents* web_contents = GetActiveWebContents();

  EXPECT_EQ(u"TabWithEmbeddedPdf", web_contents->GetTitle());
}

// Tests that PDF MIME type is not set for non-PDF `WebContents`.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, IsContentsMimeTypePdfNonPdf) {
  // Navigate to a non-PDF page.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/title1.html")));

  // Verify that MIME type associated with non-PDF `WebContents` is not
  // `application/pdf`.
  EXPECT_NE(pdf::kPDFMimeType, GetActiveWebContents()->GetContentsMimeType());
}

// Tests that PDF MIME type is not set for embedded PDF `WebContents`.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, IsContentsMimeTypePdfEmbedPdf) {
  // Load page with embedded PDF and make sure it succeeds.
  ASSERT_TRUE(LoadPdfInFirstChild(
      embedded_test_server()->GetURL("/pdf/pdf_embed.html")));

  // Verify that MIME type associated with embedded PDF `WebContents` is not
  // `application/pdf`.
  EXPECT_NE(pdf::kPDFMimeType, GetEmbedderWebContents()->GetContentsMimeType());
}

// Tests that PDF MIME type is set for full-page PDF `WebContents`.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, IsContentsMimeTypePdfFullPagePdf) {
  // Load a full-page PDF and make sure it succeeds.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/test.pdf")));

  // Verify that MIME type associated with full-page PDF `WebContents` is
  // `application/pdf`.
  EXPECT_EQ(pdf::kPDFMimeType, GetEmbedderWebContents()->GetContentsMimeType());
}

// Tests that PDF MIME type is set for full-page PDF `WebContents` with MIME
// type params.
// NOTE: This test is not parameterized as PDF MIME type validation for OOPIF
// variant is already being done in
// PDFExtensionOopifTest.FindFullPagePdfExtensionHostFullPagePdfWithMimeTypeParam.
IN_PROC_BROWSER_TEST_F(PDFExtensionTestWithoutOopifOverride,
                       IsContentsMimeTypePdfFullPagePdfWithMimeTypeParam) {
  // Load a full-page PDF with MIME type param and make sure it succeeds.
  const char kPdfFullPageWithMimeTypeParam[] =
      "data:application/pdf;charset=iso-8859-5;base64,"
      "JVBERi0xLjcKJaDypPQKMSAwIG9iaiA8PAogIC9UeXBlIC9DYXRhbG9nCiAgL1BhZ2VzIDIg"
      "MCBSCj4+CmVuZG9iagoyIDAgb2JqIDw8CiAgL1R5cGUgL1BhZ2VzCiAgL0NvdW50IDEKICAv"
      "S2lkcyBbMyAwIFJdCiAgL1Jlc291cmNlcyA8PCA+Pgo+PgplbmRvYmoKMyAwIG9iaiA8PAog"
      "IC9UeXBlIC9QYWdlIAogIC9QYXJlbnQgMiAwIFIKICAvTWVkaWFCb3ggWzAgMCAxMDAgNTBd"
      "Cj4+CmVuZG9iagp4cmVmCjAgNAowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMTUgMDAw"
      "MDAgbiAKMDAwMDAwMDA2OCAwMDAwMCBuIAowMDAwMDAwMTUwIDAwMDAwIG4gCnRyYWlsZXIg"
      "PDwKICAvUm9vdCAxIDAgUgogIC9TaXplIDQKPj4Kc3RhcnR4cmVmCjIyNwolJUVPRgo=";
  EXPECT_TRUE(LoadPdf(GURL(kPdfFullPageWithMimeTypeParam)));

  // Verify that MIME type associated with full-page PDF `WebContents` is
  // `application/pdf`, irrespective of MIME type params (Ex: charset in this
  // test).
  EXPECT_EQ(pdf::kPDFMimeType, GetEmbedderWebContents()->GetContentsMimeType());
}

// Flaky, http://crbug.com/767427
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#define MAYBE_PdfZoomWithoutBubble DISABLED_PdfZoomWithoutBubble
#else
#define MAYBE_PdfZoomWithoutBubble PdfZoomWithoutBubble
#endif
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, MAYBE_PdfZoomWithoutBubble) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);
  WebContents* web_contents = GetActiveWebContents();

  // Here we look at the presets to find the next zoom level above 0. Ideally
  // we should look at the zoom levels from the PDF viewer javascript, but we
  // assume they'll always match the browser presets, which are easier to
  // access. In the script below, we zoom to 100% (0), then wait for this to be
  // picked up by the browser zoom, then zoom to the next zoom level. This
  // ensures the test passes regardless of the initial default zoom level.
  std::vector<double> preset_zoom_levels = zoom::PageZoom::PresetZoomLevels(0);
  auto it = std::ranges::find(preset_zoom_levels, 0);
  ASSERT_NE(it, preset_zoom_levels.end());
  it++;
  ASSERT_NE(it, preset_zoom_levels.end());
  double new_zoom_level = *it;

  auto* zoom_controller = zoom::ZoomController::FromWebContents(web_contents);
  // We expect a ZoomChangedEvent with can_show_bubble == false if the PDF
  // extension behaviour is properly picked up. The test times out otherwise.
  zoom::ZoomChangedWatcher watcher(
      zoom_controller,
      zoom::ZoomController::ZoomChangedEventData(
          web_contents,
          web_contents->GetPrimaryMainFrame()->GetFrameTreeNodeId(), 0,
          new_zoom_level, zoom::ZoomController::ZOOM_MODE_MANUAL, false));

  // Zoom PDF via script.
#if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC)
  EXPECT_FALSE(ZoomBubbleView::GetZoomBubble());
#endif
  ASSERT_TRUE(content::ExecJs(extension_host,
                              "while (viewer.viewport.getZoom() < 1) {"
                              "  viewer.viewport.zoomIn();"
                              "}"
                              "setTimeout(() => {"
                              "  viewer.viewport.zoomIn();"
                              "}, 1);"));

  watcher.Wait();
#if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC)
  EXPECT_FALSE(ZoomBubbleView::GetZoomBubble());
#endif
}

class PDFExtensionScrollTest : public PDFExtensionTest {
 public:
  void SetUpOnMainThread() override {
    PDFExtensionTest::SetUpOnMainThread();

    GetActiveWebContents()->Resize({0, 0, 1024, 768});
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    PDFExtensionTest::SetUpCommandLine(command_line);

    // Smooth scrolling confuses the test cases that reads the scroll bar
    // position.
    command_line->AppendSwitch(switches::kDisableSmoothScrolling);
  }

 protected:
  class ScrollEventWaiter {
   public:
    explicit ScrollEventWaiter(content::ToRenderFrameHost extension_host)
        : message_queue_(extension_host.render_frame_host()) {
      content::ExecuteScriptAsync(
          extension_host,
          R"(viewer.shadowRoot.querySelector('#scroller').onscroll = () => {
            if (viewer.viewport.scrollContent_.unackedScrollsToRemote_ === 0) {
              window.domAutomationController.send('dispatchedScrollEvent');
            }
          };
          if (viewer.viewport.scrollContent_.unackedScrollsToRemote_ === 0) {
            window.domAutomationController.send('dispatchedScrollEvent');
          })");

      // Wait for pending scroll-to-remotes to complete.
      Wait();
    }

    void Reset() { message_queue_.ClearQueue(); }

    void Wait() {
      std::string message;
      ASSERT_TRUE(message_queue_.WaitForMessage(&message));
      EXPECT_EQ("\"dispatchedScrollEvent\"", message);
    }

   private:
    content::DOMMessageQueue message_queue_;
  };

  // Scroll increment in CSS pixels. Should match `SCROLL_INCREMENT` in
  // //chrome/browser/resources/pdf/viewport.js.
  static constexpr int kScrollIncrement = 40;

  // Scrolling by a fraction of the viewport height may introduce slight
  // position differences on various platforms due to rounding. Tolerate this
  // difference.
  static constexpr float kScrollPositionEpsilon = 2.0f;

  static int GetViewportHeight(content::RenderFrameHost* extension_host) {
    return content::EvalJs(extension_host, "viewer.viewport.size.height;")
        .ExtractInt();
  }

  static int GetViewportScrollPositionX(
      content::RenderFrameHost* extension_host) {
    return content::EvalJs(extension_host, "viewer.viewport.position.x;")
        .ExtractInt();
  }

  static int GetViewportScrollPositionY(
      content::RenderFrameHost* extension_host) {
    return content::EvalJs(extension_host, "viewer.viewport.position.y;")
        .ExtractInt();
  }
};

IN_PROC_BROWSER_TEST_P(PDFExtensionScrollTest, WithSpace) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf"));
  ASSERT_TRUE(extension_host);

  SetInputFocusOnPlugin(extension_host, GetEmbedderWebContents());
  ASSERT_EQ(0, GetViewportScrollPositionY(extension_host));

  // Get the viewport height, since the scroll distance is based on it.
  const int viewport_height = GetViewportHeight(extension_host);
  ASSERT_GT(viewport_height, 0);

  // For web content, page down / page up scrolling only scrolls by a fraction
  // of the viewport height. The PDF Viewer should match that behavior.
  const float scroll_height =
      viewport_height * cc::kMinFractionToStepWhenPaging;

  // Press Space to scroll down.
  ScrollEventWaiter scroll_waiter(extension_host);
  content::SimulateKeyPress(GetActiveWebContents(),
                            ui::DomKey::FromCharacter(' '), ui::DomCode::SPACE,
                            ui::VKEY_SPACE,
                            /*control=*/false, /*shift=*/false, /*alt=*/false,
                            /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_NEAR(scroll_height, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);

  // Press Space to scroll down again.
  scroll_waiter.Reset();
  content::SimulateKeyPress(GetActiveWebContents(),
                            ui::DomKey::FromCharacter(' '), ui::DomCode::SPACE,
                            ui::VKEY_SPACE,
                            /*control=*/false, /*shift=*/false, /*alt=*/false,
                            /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_NEAR(scroll_height * 2, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);

  // Press Shift+Space to scroll up.
  scroll_waiter.Reset();
  content::SimulateKeyPress(GetActiveWebContents(),
                            ui::DomKey::FromCharacter(' '), ui::DomCode::SPACE,
                            ui::VKEY_SPACE,
                            /*control=*/false, /*shift=*/true, /*alt=*/false,
                            /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_NEAR(scroll_height, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);
}

IN_PROC_BROWSER_TEST_P(PDFExtensionScrollTest, WithPageDownUp) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf"));
  ASSERT_TRUE(extension_host);

  SetInputFocusOnPlugin(extension_host, GetEmbedderWebContents());
  ASSERT_EQ(0, GetViewportScrollPositionY(extension_host));

  // Get the viewport height, since the scroll distance is based on it.
  const int viewport_height = GetViewportHeight(extension_host);
  ASSERT_GT(viewport_height, 0);

  // For web content, page down / page up scrolling only scrolls by a fraction
  // of the viewport height. The PDF Viewer should match that behavior.
  const float scroll_height =
      viewport_height * cc::kMinFractionToStepWhenPaging;

  // Press PageDown to scroll down.
  ScrollEventWaiter scroll_waiter(extension_host);
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::PAGE_DOWN,
                                       ui::DomCode::PAGE_DOWN, ui::VKEY_NEXT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_NEAR(scroll_height, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);

  // Press PageDown to scroll down again.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::PAGE_DOWN,
                                       ui::DomCode::PAGE_DOWN, ui::VKEY_NEXT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_NEAR(scroll_height * 2, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);

  // Press PageUp to scroll up.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(
      GetActiveWebContents(), ui::DomKey::PAGE_UP, ui::DomCode::PAGE_UP,
      ui::VKEY_PRIOR,
      /*control=*/false, /*shift=*/false, /*alt=*/false,
      /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_NEAR(scroll_height, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);
}

IN_PROC_BROWSER_TEST_P(PDFExtensionScrollTest, WithArrowLeftRight) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf#zoom=200"));
  ASSERT_TRUE(extension_host);

  SetInputFocusOnPlugin(extension_host, GetEmbedderWebContents());
  ASSERT_EQ(0, GetViewportScrollPositionY(extension_host));

  // Press ArrowRight to scroll right.
  ScrollEventWaiter scroll_waiter(extension_host);
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_RIGHT,
                                       ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionX(extension_host));

  // Press ArrowRight to scroll right again.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_RIGHT,
                                       ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_EQ(kScrollIncrement * 2, GetViewportScrollPositionX(extension_host));

  // Press ArrowLeft to scroll left.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_LEFT,
                                       ui::DomCode::ARROW_LEFT, ui::VKEY_LEFT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionX(extension_host));
}

// TODO(crbug.com/369947144): Fix flakiness and reenable the test.
#if BUILDFLAG(IS_MAC)
#define MAYBE_WithArrowLeftRightScrollToPage \
  DISABLED_WithArrowLeftRightScrollToPage
#else
#define MAYBE_WithArrowLeftRightScrollToPage WithArrowLeftRightScrollToPage
#endif  // BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_P(PDFExtensionScrollTest,
                       MAYBE_WithArrowLeftRightScrollToPage) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf"));
  ASSERT_TRUE(extension_host);

  SetInputFocusOnPlugin(extension_host, GetEmbedderWebContents());
  ASSERT_EQ(0, GetViewportScrollPositionY(extension_host));

  // Press ArrowRight to scroll to next page.
  ScrollEventWaiter scroll_waiter(extension_host);
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_RIGHT,
                                       ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
#if BUILDFLAG(IS_WIN)
  const int kFirstPosition = ui::IsFluentScrollbarEnabled() ? 917 : 915;
#elif BUILDFLAG(IS_CHROMEOS)
  constexpr int kFirstPosition = 937;
#else
  constexpr int kFirstPosition = 918;
#endif
  EXPECT_NEAR(kFirstPosition, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);

  // Press ArrowRight to scroll to next page again.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_RIGHT,
                                       ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
#if BUILDFLAG(IS_WIN)
  const int kSecondPosition = ui::IsFluentScrollbarEnabled() ? 1834 : 1831;
#elif BUILDFLAG(IS_CHROMEOS)
  constexpr int kSecondPosition = 1875;
#else
  constexpr int kSecondPosition = 1836;
#endif
  EXPECT_NEAR(kSecondPosition, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);

  // Press ArrowLeft to scroll to previous page.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_LEFT,
                                       ui::DomCode::ARROW_LEFT, ui::VKEY_LEFT,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_NEAR(kFirstPosition, GetViewportScrollPositionY(extension_host),
              kScrollPositionEpsilon);
}

IN_PROC_BROWSER_TEST_P(PDFExtensionScrollTest, WithArrowDownUp) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf"));
  ASSERT_TRUE(extension_host);

  SetInputFocusOnPlugin(extension_host, GetEmbedderWebContents());
  ASSERT_EQ(0, GetViewportScrollPositionY(extension_host));

  // Press ArrowDown to scroll down.
  ScrollEventWaiter scroll_waiter(extension_host);
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_DOWN,
                                       ui::DomCode::ARROW_DOWN, ui::VKEY_DOWN,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionY(extension_host));

  // Press ArrowDown to scroll down again.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_DOWN,
                                       ui::DomCode::ARROW_DOWN, ui::VKEY_DOWN,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_EQ(kScrollIncrement * 2, GetViewportScrollPositionY(extension_host));

  // Press ArrowUp to scroll up.
  scroll_waiter.Reset();
  content::SimulateKeyPressWithoutChar(GetActiveWebContents(),
                                       ui::DomKey::ARROW_UP,
                                       ui::DomCode::ARROW_UP, ui::VKEY_UP,
                                       /*control=*/false, /*shift=*/false,
                                       /*alt=*/false,
                                       /*command=*/false);
  ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
  EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionY(extension_host));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, SelectAllShortcut) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  content::RenderFrameHost* frame =
      pdf_frame_util::FindPdfChildFrame(extension_host);
  content::RenderWidgetHostView* view = frame->GetView();
  EXPECT_THAT(view->GetSelectedText(), IsEmpty());

  base::RunLoop run_loop;
  content::TextInputManagerTester input_tester(GetActiveWebContents());
  input_tester.SetOnTextSelectionChangedCallback(run_loop.QuitClosure());

  bool control = false;
  bool command = false;
#if BUILDFLAG(IS_MAC)
  command = true;
#else
  control = true;
#endif
  content::SimulateKeyPress(GetActiveWebContents(),
                            ui::DomKey::FromCharacter('a'), ui::DomCode::US_A,
                            ui::VKEY_A, control,
                            /*shift=*/false,
                            /*alt=*/false, command);
  run_loop.Run();

#if BUILDFLAG(IS_WIN)
  constexpr char kExpectedText[] = "this is some text\r\nsome more text";
#else
  constexpr char kExpectedText[] = "this is some text\nsome more text";
#endif
  EXPECT_EQ(base::UTF16ToUTF8(view->GetSelectedText()), kExpectedText);
}

// TODO(crbug.com/40793934): Add tests for using space and shift+space shortcuts
// for scrolling PDFs.

// Test that even if a different tab is selected when a navigation occurs,
// the correct tab still gets navigated (see crbug.com/672563).
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, NavigationOnCorrectTab) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);
  WebContents* web_contents = GetActiveWebContents();

  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
          ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_NE(web_contents, active_web_contents);

  content::TestNavigationObserver active_navigation_observer(
      active_web_contents);
  content::TestNavigationObserver navigation_observer(web_contents);
  ASSERT_TRUE(content::ExecJs(extension_host,
                              "viewer.navigator_.navigate("
                              "    'www.example.com',"
                              "    WindowOpenDisposition.CURRENT_TAB);"));
  navigation_observer.Wait();

  EXPECT_FALSE(navigation_observer.last_navigation_url().is_empty());
  EXPECT_TRUE(active_navigation_observer.last_navigation_url().is_empty());
  EXPECT_FALSE(active_web_contents->GetController().GetPendingEntry());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, MultipleDomains) {
  ASSERT_TRUE(LoadPdfInNewTab(
      embedded_test_server()->GetURL("a.com", "/pdf/test.pdf")));
  ASSERT_TRUE(LoadPdfInNewTab(
      embedded_test_server()->GetURL("b.com", "/pdf/test.pdf")));
  EXPECT_EQ(2, CountPDFProcesses());
}

namespace {

class PDFExtensionIsolatedContentTest
    : public PDFExtensionTestWithoutOopifOverride,
      public testing::WithParamInterface<std::tuple<bool, bool>> {
 protected:
  bool site_isolated() const { return std::get<0>(GetParam()); }

  bool UseOopif() const override { return std::get<1>(GetParam()); }

  std::vector<base::test::FeatureRefAndParams> GetEnabledFeatures()
      const override {
    std::vector<base::test::FeatureRefAndParams> enabled =
        PDFExtensionTestWithoutOopifOverride::GetEnabledFeatures();
    if (site_isolated()) {
      enabled.push_back({features::kSitePerProcess, {}});
    }
    return enabled;
  }

  std::vector<base::test::FeatureRef> GetDisabledFeatures() const override {
    std::vector<base::test::FeatureRef> disabled =
        PDFExtensionTestWithoutOopifOverride::GetDisabledFeatures();
    if (!site_isolated()) {
      disabled.push_back(features::kSitePerProcess);
    }
    return disabled;
  }
};

INSTANTIATE_TEST_SUITE_P(All,
                         PDFExtensionIsolatedContentTest,
                         testing::Combine(testing::Bool(), testing::Bool()),
                         PDFExtensionIsolatedContentTestPassToString());

}  // namespace

// Makes sure `PDFExtensionIsolatedContentTest` runs with and without Site
// Isolation enabled (see crbug.com/1298269).
//
// This is a separate test because fatal assertions in `SetUpInMainThread()`
// don't terminate early, so there's no point asserting before every test.
IN_PROC_BROWSER_TEST_P(PDFExtensionIsolatedContentTest, ExpectSiteIsolation) {
  EXPECT_EQ(site_isolated(),
            content::SiteIsolationPolicy::UseDedicatedProcessesForAllSites());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionIsolatedContentTest, PdfAndHtml) {
  content::RenderProcessHost::SetMaxRendererProcessCount(1);

  // Load a page with an embedded PDF and an HTML iframe, both of the same
  // origin.
  const GURL main_url(
      embedded_test_server()->GetURL("/pdf/embed_pdf_and_html.html"));

  if (UseOopif()) {
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));

    // The PDF embed is the second child of the HTML page.
    content::RenderFrameHost* pdf_embed =
        ChildFrameAt(GetActiveWebContents(), 1);
    ASSERT_TRUE(GetTestPdfViewerStreamManager(GetActiveWebContents())
                    ->WaitUntilPdfLoaded(pdf_embed));
  } else {
    ASSERT_TRUE(LoadPdf(main_url));
  }

  // The PDF plugin frame and the iframe should not share renderer processes
  // even though they share origins.
  std::vector<content::RenderFrameHost*> plugin_frames =
      GetPdfPluginFrames(GetActiveWebContents());
  ASSERT_EQ(plugin_frames.size(), 1u);

  content::RenderFrameHost* iframe = ChildFrameAt(GetActiveWebContents(), 0);
  ASSERT_TRUE(iframe);
  EXPECT_EQ(iframe->GetLastCommittedURL(),
            embedded_test_server()->GetURL("/title1.html"));

  EXPECT_EQ(plugin_frames[0]->GetLastCommittedOrigin(),
            iframe->GetLastCommittedOrigin());
  EXPECT_NE(plugin_frames[0]->GetProcess(), iframe->GetProcess());
  EXPECT_EQ(
      content::SiteIsolationPolicy::AreOriginKeyedProcessesEnabledByDefault(),
      content::HasOriginKeyedProcess(plugin_frames[0]));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionIsolatedContentTest, DataNavigation) {
  content::RenderFrameHost* extension_host =
      LoadPdfInFirstChildGetExtensionHost(
          embedded_test_server()->GetURL("/pdf/data_url_rectangles.html"));
  ASSERT_TRUE(extension_host);

  // The PDF plugin frame and the extension main frame should not share renderer
  // processes even though the extension triggers a data: navigation when
  // loading its plugin.
  std::vector<content::RenderFrameHost*> plugin_frames =
      GetPdfPluginFrames(GetActiveWebContents());
  ASSERT_EQ(plugin_frames.size(), 1u);
  EXPECT_NE(plugin_frames[0]->GetProcess(), extension_host->GetProcess());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionIsolatedContentTest, HistoryNavigation) {
  // Navigating to a PDF should spawn a PDF renderer process.
  EXPECT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));
  EXPECT_EQ(CountPDFProcesses(), 1);

  // Navigating to non-PDF content should remove the PDF renderer process.
  EXPECT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/title1.html")));
  EXPECT_EQ(CountPDFProcesses(), 0);

  // Navigating back to the PDF should once again spawn a PDF renderer process.
  WebContents* web_contents = GetActiveWebContents();
  web_contents->GetController().GoBack();
  EXPECT_TRUE(content::WaitForLoadStop(web_contents));
  EXPECT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(web_contents));
  EXPECT_EQ(CountPDFProcesses(), 1);
}

IN_PROC_BROWSER_TEST_P(PDFExtensionIsolatedContentTest, Jitless) {
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));

  // PDF content should always be in JIT-less processes.
  std::vector<content::RenderFrameHost*> plugin_frames =
      GetPdfPluginFrames(GetActiveWebContents());
  ASSERT_EQ(plugin_frames.size(), 1u);
  EXPECT_TRUE(plugin_frames[0]->GetProcess()->IsJitDisabled());
}

class PDFExtensionLinkClickTest : public PDFExtensionTest {
 public:
  PDFExtensionLinkClickTest() = default;
  ~PDFExtensionLinkClickTest() override = default;

 protected:
  // The rectangle of the link in test-link.pdf is [72 706 164 719] in PDF user
  // space. To calculate a position inside this rectangle, several
  // transformations have to be applied:
  // [a] (110, 110) in Blink page coordinates ->
  // [b] (219, 169) in Blink screen coordinates ->
  // [c] (115, 169) in PDF Device space coordinates ->
  // [d] (82.5, 709.5) in PDF user space coordinates.
  // This performs the [a] to [b] transformation, since that is the coordinate
  // space with respect to guest that SimulateMouseClickAt() needs.
  gfx::Point GetLinkPosition(content::RenderFrameHost* extension_host) {
    return ConvertPageCoordToScreenCoord(extension_host, {110, 110});
  }
};

IN_PROC_BROWSER_TEST_P(PDFExtensionLinkClickTest, CtrlLeft) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-link.pdf"));
  ASSERT_TRUE(extension_host);

  WebContents* web_contents = GetActiveWebContents();

  SimulateMouseClickAt(extension_host, GetEmbedderWebContents(),
                       kDefaultKeyModifier, blink::WebMouseEvent::Button::kLeft,
                       GetLinkPosition(extension_host));
  ui_test_utils::TabAddedWaiter(browser()).Wait();

  int tab_count = browser()->tab_strip_model()->count();
  ASSERT_EQ(2, tab_count);

  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_EQ(web_contents, active_web_contents);

  WebContents* new_web_contents =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  ASSERT_TRUE(new_web_contents);
  ASSERT_NE(web_contents, new_web_contents);

  const GURL& url = new_web_contents->GetVisibleURL();
  EXPECT_EQ("http://www.example.com/", url.spec());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionLinkClickTest, Middle) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-link.pdf"));
  ASSERT_TRUE(extension_host);

  WebContents* web_contents = GetActiveWebContents();

  SimulateMouseClickAt(extension_host, GetEmbedderWebContents(), 0,
                       blink::WebMouseEvent::Button::kMiddle,
                       GetLinkPosition(extension_host));
  ui_test_utils::TabAddedWaiter(browser()).Wait();

  int tab_count = browser()->tab_strip_model()->count();
  ASSERT_EQ(2, tab_count);

  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_EQ(web_contents, active_web_contents);

  WebContents* new_web_contents =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  ASSERT_TRUE(new_web_contents);
  ASSERT_NE(web_contents, new_web_contents);

  const GURL& url = new_web_contents->GetVisibleURL();
  EXPECT_EQ("http://www.example.com/", url.spec());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionLinkClickTest, CtrlShiftLeft) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-link.pdf"));
  ASSERT_TRUE(extension_host);

  WebContents* web_contents = GetActiveWebContents();

  const int modifiers = blink::WebInputEvent::kShiftKey | kDefaultKeyModifier;

  SimulateMouseClickAt(extension_host, GetEmbedderWebContents(), modifiers,
                       blink::WebMouseEvent::Button::kLeft,
                       GetLinkPosition(extension_host));
  ui_test_utils::TabAddedWaiter(browser()).Wait();

  int tab_count = browser()->tab_strip_model()->count();
  ASSERT_EQ(2, tab_count);

  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_NE(web_contents, active_web_contents);

  const GURL& url = active_web_contents->GetVisibleURL();
  EXPECT_EQ("http://www.example.com/", url.spec());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionLinkClickTest, ShiftMiddle) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-link.pdf"));
  ASSERT_TRUE(extension_host);

  WebContents* web_contents = GetActiveWebContents();

  SimulateMouseClickAt(
      extension_host, GetEmbedderWebContents(), blink::WebInputEvent::kShiftKey,
      blink::WebMouseEvent::Button::kMiddle, GetLinkPosition(extension_host));
  ui_test_utils::TabAddedWaiter(browser()).Wait();

  int tab_count = browser()->tab_strip_model()->count();
  ASSERT_EQ(2, tab_count);

  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_NE(web_contents, active_web_contents);

  const GURL& url = active_web_contents->GetVisibleURL();
  EXPECT_EQ("http://www.example.com/", url.spec());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionLinkClickTest, ShiftLeft) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-link.pdf"));
  ASSERT_TRUE(extension_host);

  ASSERT_EQ(1U, chrome::GetTotalBrowserCount());

  WebContents* web_contents = GetActiveWebContents();

  SimulateMouseClickAt(
      extension_host, GetEmbedderWebContents(), blink::WebInputEvent::kShiftKey,
      blink::WebMouseEvent::Button::kLeft, GetLinkPosition(extension_host));
  Browser* new_browser = ui_test_utils::WaitForBrowserToOpen();
  ui_test_utils::WaitUntilBrowserBecomeActive(new_browser);

  ASSERT_EQ(2U, chrome::GetTotalBrowserCount());

  WebContents* active_web_contents =
      new_browser->tab_strip_model()->GetActiveWebContents();
  ASSERT_NE(web_contents, active_web_contents);

  const GURL& url = active_web_contents->GetVisibleURL();
  EXPECT_EQ("http://www.example.com/", url.spec());
}

// This test opens a PDF by clicking a link via javascript and verifies that
// the PDF is loaded and functional by clicking a link in the PDF. The link
// click in the PDF opens a new tab. The main page handles the pageShow event
// and updates the history state.
IN_PROC_BROWSER_TEST_P(PDFExtensionLinkClickTest, OpenPDFWithReplaceState) {
  // Navigate to the main page.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(),
      embedded_test_server()->GetURL("/pdf/pdf_href_replace_state.html")));
  WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  // Click on the link which opens the PDF via JS.
  content::TestNavigationObserver navigation_observer(web_contents);
  const char kPdfLinkClick[] = "document.getElementById('link').click();";
  ASSERT_TRUE(content::ExecJs(web_contents, kPdfLinkClick));
  navigation_observer.Wait();
  const GURL& current_url = web_contents->GetLastCommittedURL();
  ASSERT_EQ("/pdf/test-link.pdf", current_url.path());

  ASSERT_TRUE(EnsureFullPagePDFHasLoadedWithValidFrameTree(
      web_contents, /*allow_multiple_frames=*/false));

  content::RenderFrameHost* extension_host =
      GetOnlyPdfExtensionHostEnsureValid();
  ASSERT_TRUE(extension_host);

  // Now click on the link to example.com in the PDF. This should open up a new
  // tab.
  SimulateMouseClickAt(extension_host, GetEmbedderWebContents(),
                       kDefaultKeyModifier, blink::WebMouseEvent::Button::kLeft,
                       GetLinkPosition(extension_host));

  ui_test_utils::TabAddedWaiter(browser()).Wait();

  // We should have two tabs now. One with the PDF and the second for
  // example.com
  int tab_count = browser()->tab_strip_model()->count();
  ASSERT_EQ(2, tab_count);

  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_EQ(web_contents, active_web_contents);

  WebContents* new_web_contents =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  ASSERT_TRUE(new_web_contents);
  ASSERT_NE(web_contents, new_web_contents);

  const GURL& url = new_web_contents->GetVisibleURL();
  EXPECT_EQ("http://www.example.com/", url.spec());
}

namespace {
// Fails the test if a navigation is started in the given WebContents.
class FailOnNavigation : public content::WebContentsObserver {
 public:
  explicit FailOnNavigation(WebContents* contents)
      : content::WebContentsObserver(contents) {}

  // content::WebContentsObserver:
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override {
    ADD_FAILURE() << "Unexpected navigation";
  }
};
}  // namespace

// If the PDF viewer can't navigate the tab using a tab id, make sure it doesn't
// try to navigate the extension frame.
// Regression test for https://crbug.com/1158381
IN_PROC_BROWSER_TEST_P(PDFExtensionLinkClickTest, LinkClickInPdfInNonTab) {
  // For ease of testing, we'll still load the PDF in a tab, but we clobber the
  // tab id in the viewer to make it think it's not in a tab.
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-link.pdf"));
  ASSERT_TRUE(extension_host);
  ASSERT_TRUE(
      content::ExecJs(extension_host,
                      "window.viewer.browserApi.getStreamInfo().tabId = "
                      "    chrome.tabs.TAB_ID_NONE;"));

  content::WebContents* target_contents = GetActiveWebContents();
  if (!UseOopif()) {
    MimeHandlerViewGuest* guest =
        pdf_extension_test_util::GetOnlyMimeHandlerView(target_contents);
    ASSERT_TRUE(guest);
    target_contents = guest->web_contents();
    EXPECT_NE(GetActiveWebContents(), target_contents);
  }

  FailOnNavigation fail_if_mimehandler_navigates(target_contents);
  SimulateMouseClickAt(extension_host, GetEmbedderWebContents(),
                       blink::WebInputEvent::kNoModifiers,
                       blink::WebMouseEvent::Button::kLeft,
                       GetLinkPosition(extension_host));

  // The PDF extension frame must not navigate away. If
  // `fail_if_mimehandler_navigates` doesn't see a navigation, we consider the
  // test to have passed.
  base::RunLoop run_loop;
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, run_loop.QuitClosure(), TestTimeouts::tiny_timeout());
  run_loop.Run();
}

class PDFExtensionInternalLinkClickTest : public PDFExtensionTest {
 public:
  PDFExtensionInternalLinkClickTest() = default;
  ~PDFExtensionInternalLinkClickTest() override = default;

 protected:
  gfx::Point GetLinkPosition(content::RenderFrameHost* extension_host) {
    // The whole first page is a link.
    return ConvertPageCoordToScreenCoord(extension_host, {100, 100});
  }
};

IN_PROC_BROWSER_TEST_P(PDFExtensionInternalLinkClickTest, CtrlLeft) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-internal-link.pdf"));
  ASSERT_TRUE(extension_host);

  WebContents* web_contents = GetActiveWebContents();

  SimulateMouseClickAt(extension_host, GetEmbedderWebContents(),
                       kDefaultKeyModifier, blink::WebMouseEvent::Button::kLeft,
                       GetLinkPosition(extension_host));
  ui_test_utils::TabAddedWaiter(browser()).Wait();

  int tab_count = browser()->tab_strip_model()->count();
  ASSERT_EQ(2, tab_count);

  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_EQ(web_contents, active_web_contents);

  WebContents* new_web_contents =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  ASSERT_TRUE(new_web_contents);
  ASSERT_NE(web_contents, new_web_contents);

  const GURL& url = new_web_contents->GetVisibleURL();
  EXPECT_EQ("/pdf/test-internal-link.pdf", url.path());
  EXPECT_EQ("page=2&zoom=100,0,200", url.ref());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionInternalLinkClickTest, Middle) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-internal-link.pdf"));
  ASSERT_TRUE(extension_host);

  WebContents* web_contents = GetActiveWebContents();

  SimulateMouseClickAt(extension_host, GetEmbedderWebContents(), 0,
                       blink::WebMouseEvent::Button::kMiddle,
                       GetLinkPosition(extension_host));
  ui_test_utils::TabAddedWaiter(browser()).Wait();

  int tab_count = browser()->tab_strip_model()->count();
  ASSERT_EQ(2, tab_count);

  WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_EQ(web_contents, active_web_contents);

  WebContents* new_web_contents =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  ASSERT_TRUE(new_web_contents);
  ASSERT_NE(web_contents, new_web_contents);

  const GURL& url = new_web_contents->GetVisibleURL();
  EXPECT_EQ("/pdf/test-internal-link.pdf", url.path());
  EXPECT_EQ("page=2&zoom=100,0,200", url.ref());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionInternalLinkClickTest, ShiftLeft) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test-internal-link.pdf"));
  ASSERT_TRUE(extension_host);

  ASSERT_EQ(1U, chrome::GetTotalBrowserCount());

  WebContents* web_contents = GetActiveWebContents();

  SimulateMouseClickAt(
      extension_host, GetEmbedderWebContents(), blink::WebInputEvent::kShiftKey,
      blink::WebMouseEvent::Button::kLeft, GetLinkPosition(extension_host));
  Browser* new_browser = ui_test_utils::WaitForBrowserToOpen();
  ui_test_utils::WaitUntilBrowserBecomeActive(new_browser);

  ASSERT_EQ(2U, chrome::GetTotalBrowserCount());

  WebContents* active_web_contents =
      new_browser->tab_strip_model()->GetActiveWebContents();
  ASSERT_NE(web_contents, active_web_contents);

  const GURL& url = active_web_contents->GetVisibleURL();
  EXPECT_EQ("/pdf/test-internal-link.pdf", url.path());
  EXPECT_EQ("page=2&zoom=100,0,200", url.ref());
}

class PDFExtensionComboBoxTest : public PDFExtensionTest {
 public:
  // Returns a point near the left edge of the editable combo box in
  // combobox_form.pdf, inside the combo box rect. The point is in Blink screen
  // coordinates.
  //
  // The combo box's rect is [100 50 200 80] in PDF user space. (136, 318) in
  // Blink page coordinates corresponds to approximately (102, 62) in PDF user
  // space coordinates. See PDFExtensionLinkClickTest::GetLinkPosition() for
  // more information on all the coordinate systems involved.
  gfx::Point GetEditableComboBoxLeftPosition(
      content::RenderFrameHost* extension_host) {
    return ConvertPageCoordToScreenCoord(extension_host, {136, 318});
  }

  void ClickLeftSideOfEditableComboBox(
      content::RenderFrameHost* extension_host) {
    SimulateMouseClickAt(extension_host, GetEmbedderWebContents(), 0,
                         blink::WebMouseEvent::Button::kLeft,
                         GetEditableComboBoxLeftPosition(extension_host));

    // Make sure mouse events are sent completely before proceeding, in order to
    // avoid races with subsequent keyboard events.
    content::InputEventAckWaiter mouse_waiter(
        pdf_frame_util::FindPdfChildFrame(extension_host)
            ->GetRenderWidgetHost(),
        blink::WebInputEvent::Type::kMouseUp);
    mouse_waiter.Wait();
  }

  void TypeHello(content::RenderFrameHost* extension_host) {
    struct KeyData {
      char ch;
      ui::DomCode code;
      ui::KeyboardCode key_code;
    };

    constexpr KeyData kData[] = {
        {'H', ui::DomCode::US_H, ui::VKEY_H},
        {'E', ui::DomCode::US_E, ui::VKEY_E},
        {'L', ui::DomCode::US_L, ui::VKEY_L},
        {'L', ui::DomCode::US_L, ui::VKEY_L},
        {'O', ui::DomCode::US_O, ui::VKEY_O},
    };

    content::RenderFrameHost* plugin_frame =
        pdf_frame_util::FindPdfChildFrame(extension_host);
    // Make sure that the plugin frame of guest has focus.
    ASSERT_EQ(GetActiveWebContents()->GetFocusedFrame(), plugin_frame);
    for (const auto& data : kData) {
      content::SimulateKeyPress(GetEmbedderWebContents(),
                                ui::DomKey::FromCharacter(data.ch), data.code,
                                data.key_code, /*control=*/false,
                                /*shift=*/false, /*alt=*/false,
                                /*command=*/false);
      content::InputEventAckWaiter key_waiter(
          plugin_frame->GetRenderWidgetHost(),
          blink::WebInputEvent::Type::kKeyUp);
      key_waiter.Wait();
    }
  }

  // Presses the left arrow key.
  void PressLeftArrow(content::RenderFrameHost* extension_host) {
    // Make sure that the plugin frame of guest has focus.
    ASSERT_EQ(GetActiveWebContents()->GetFocusedFrame(),
              pdf_frame_util::FindPdfChildFrame(extension_host));
    content::SimulateKeyPressWithoutChar(
        GetEmbedderWebContents(), ui::DomKey::ARROW_LEFT,
        ui::DomCode::ARROW_LEFT, ui::VKEY_LEFT, false, false, false, false);
  }

  // Presses down shift, presses the left arrow, and lets go of shift.
  void PressShiftLeftArrow(content::RenderFrameHost* extension_host) {
    // Make sure that the plugin frame of guest has focus.
    ASSERT_EQ(GetActiveWebContents()->GetFocusedFrame(),
              pdf_frame_util::FindPdfChildFrame(extension_host));
    content::SimulateKeyPressWithoutChar(GetEmbedderWebContents(),
                                         ui::DomKey::ARROW_LEFT,
                                         ui::DomCode::ARROW_LEFT, ui::VKEY_LEFT,
                                         false, /*shift=*/true, false, false);
  }

  // Presses the right arrow key.
  void PressRightArrow(content::RenderFrameHost* extension_host) {
    // Make sure that the plugin frame of guest has focus.
    ASSERT_EQ(GetActiveWebContents()->GetFocusedFrame(),
              pdf_frame_util::FindPdfChildFrame(extension_host));
    content::SimulateKeyPressWithoutChar(
        GetEmbedderWebContents(), ui::DomKey::ARROW_RIGHT,
        ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT, false, false, false, false);
  }

  // Presses down shift, presses the right arrow, and lets go of shift.
  void PressShiftRightArrow(content::RenderFrameHost* extension_host) {
    // Make sure that the plugin frame of guest has focus.
    ASSERT_EQ(GetActiveWebContents()->GetFocusedFrame(),
              pdf_frame_util::FindPdfChildFrame(extension_host));
    content::SimulateKeyPressWithoutChar(
        GetEmbedderWebContents(), ui::DomKey::ARROW_RIGHT,
        ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT, false, /*shift=*/true, false,
        false);
  }
};

class PDFExtensionSaveTest : public PDFExtensionComboBoxTest {
 public:
  void SetUpOnMainThread() override {
    PDFExtensionComboBoxTest::SetUpOnMainThread();

    base::ScopedAllowBlockingForTesting allow_blocking;
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  }

  void SaveEditedPdf(content::RenderFrameHost* extension_host) {
    ASSERT_TRUE(content::ExecJs(
        extension_host,
        "var viewer = document.getElementById('viewer');"
        "var toolbar = viewer.shadowRoot.getElementById('toolbar');"
        "var downloads = toolbar.shadowRoot.getElementById('downloads');"
        "downloads.shadowRoot.getElementById('download-edited').click();"));
  }

  void WaitForSavedPdf(const base::FilePath& path) {
    while (!base::PathExists(path))
      content::RunAllTasksUntilIdle();
  }

  base::FilePath GetDownloadDir() const { return temp_dir_.GetPath(); }

 private:
  base::ScopedTempDir temp_dir_;
};

// Flaky, https://crbug.com/1269103, https://crbug.com/1520715
IN_PROC_BROWSER_TEST_P(PDFExtensionSaveTest, DISABLED_Save) {
  base::ScopedAllowBlockingForTesting allow_blocking;

  base::FilePath save_path = GetDownloadDir().AppendASCII("edited.pdf");
  ASSERT_FALSE(base::PathExists(save_path));

  using extensions::FileSystemChooseEntryFunction;
  const FileSystemChooseEntryFunction::TestOptions test_options{
      .path_to_be_picked = &save_path};
  auto auto_reset_options =
      FileSystemChooseEntryFunction::SetOptionsForTesting(test_options);

  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  ClickLeftSideOfEditableComboBox(extension_host);
  TypeHello(extension_host);
  SaveEditedPdf(extension_host);
  WaitForSavedPdf(save_path);
}

class PDFExtensionSaveWithPolicyTest : public PDFExtensionSaveTest {
 public:
  // InProcessBrowserTest:
  void SetUpInProcessBrowserTestFixture() override {
    policy_provider_.SetDefaultReturns(
        /*is_initialization_complete_return=*/true,
        /*is_first_policy_load_complete_return=*/true);
    policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
        &policy_provider_);
  }

  void SetDownloadPolicyManagedPath(const base::FilePath& path) {
    policy::PolicyMap policies;
    policies.Set(policy::key::kDownloadDirectory,
                 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
                 policy::POLICY_SOURCE_CLOUD, base::Value(path.AsUTF8Unsafe()),
                 nullptr);
    policy_provider_.UpdateChromePolicy(policies);
    base::RunLoop().RunUntilIdle();
  }

  void CreateConflictingFilenames(const base::FilePath& path, int count) {
    for (int i = 0; i < count; ++i) {
      base::FilePath unique_path = base::GetUniquePath(path);
      ASSERT_TRUE(!unique_path.empty());
      ASSERT_TRUE(base::WriteFile(unique_path, ""));
    }
  }

  int CountPdfFilesInDir(const base::FilePath& dir) {
    base::FileEnumerator file_enumerator(dir,
                                         /*recursive=*/false,
                                         base::FileEnumerator::FILES,
                                         FILE_PATH_LITERAL("*.pdf"));

    int count = 0;
    for (base::FilePath file_path = file_enumerator.Next(); !file_path.empty();
         file_path = file_enumerator.Next()) {
      ++count;
    }
    return count;
  }

 private:
  testing::NiceMock<policy::MockConfigurationPolicyProvider> policy_provider_;
};

// Flaky, https://crbug.com/1269103, https://crbug.com/1520715
IN_PROC_BROWSER_TEST_P(PDFExtensionSaveWithPolicyTest,
                       DISABLED_SaveWithPolicy) {
  base::ScopedAllowBlockingForTesting allow_blocking;

  base::FilePath save_path = GetDownloadDir().AppendASCII("combobox_form.pdf");
  ASSERT_FALSE(base::PathExists(save_path));

  SetDownloadPolicyManagedPath(GetDownloadDir());
  DownloadPrefs::FromBrowserContext(profile())
      ->SkipSanitizeDownloadTargetPathForTesting();

  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  ClickLeftSideOfEditableComboBox(extension_host);
  TypeHello(extension_host);
  SaveEditedPdf(extension_host);
  WaitForSavedPdf(save_path);
}

// Flaky, https://crbug.com/1269103, https://crbug.com/1520715
IN_PROC_BROWSER_TEST_P(PDFExtensionSaveWithPolicyTest,
                       DISABLED_SaveWithPolicyUniqueNumberSuffix) {
  base::ScopedAllowBlockingForTesting allow_blocking;

  CreateConflictingFilenames(GetDownloadDir().AppendASCII("combobox_form.pdf"),
                             5);
  EXPECT_EQ(5, CountPdfFilesInDir(GetDownloadDir()));

  base::FilePath save_path =
      GetDownloadDir().AppendASCII("combobox_form (5).pdf");
  ASSERT_FALSE(base::PathExists(save_path));

  SetDownloadPolicyManagedPath(GetDownloadDir());
  DownloadPrefs::FromBrowserContext(profile())
      ->SkipSanitizeDownloadTargetPathForTesting();

  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  ClickLeftSideOfEditableComboBox(extension_host);
  TypeHello(extension_host);
  SaveEditedPdf(extension_host);
  WaitForSavedPdf(save_path);
}

// TODO(crbug.com/40803991): Make this test non-flaky.
IN_PROC_BROWSER_TEST_P(PDFExtensionSaveWithPolicyTest,
                       DISABLED_SaveWithPolicyUniqueTimeSuffix) {
  base::ScopedAllowBlockingForTesting allow_blocking;

  CreateConflictingFilenames(GetDownloadDir().AppendASCII("combobox_form.pdf"),
                             101);
  EXPECT_EQ(101, CountPdfFilesInDir(GetDownloadDir()));

  SetDownloadPolicyManagedPath(GetDownloadDir());
  DownloadPrefs::FromBrowserContext(profile())
      ->SkipSanitizeDownloadTargetPathForTesting();

  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  ClickLeftSideOfEditableComboBox(extension_host);
  TypeHello(extension_host);
  SaveEditedPdf(extension_host);
  while (CountPdfFilesInDir(GetDownloadDir()) != 102)
    content::RunAllTasksUntilIdle();
}

class PDFExtensionClipboardTest : public PDFExtensionComboBoxTest,
                                  public ui::ClipboardObserver {
 public:
  // PDFExtensionTest:
  void SetUpOnMainThread() override {
    PDFExtensionTest::SetUpOnMainThread();
    ui::TestClipboard::CreateForCurrentThread();
  }
  void TearDownOnMainThread() override {
    ui::Clipboard::DestroyClipboardForCurrentThread();
    PDFExtensionTest::TearDownOnMainThread();
  }

  // ui::ClipboardObserver:
  void OnClipboardDataChanged() override {
    DCHECK(!clipboard_changed_);
    clipboard_changed_ = true;
    std::move(clipboard_quit_closure_).Run();
  }

  // Runs `action` and checks the Linux selection clipboard contains `expected`.
  void DoActionAndCheckSelectionClipboard(base::OnceClosure action,
                                          const std::string& expected) {
    if (ui::Clipboard::IsSupportedClipboardBuffer(
            ui::ClipboardBuffer::kSelection)) {
      DoActionAndCheckClipboard(std::move(action),
                                ui::ClipboardBuffer::kSelection, expected);
    } else {
      // Even though there is no selection clipboard to check, `action` still
      // needs to run.
      std::move(action).Run();
    }
  }

  // Sends a copy command and checks the copy/paste clipboard.
  // Note: Trying to send ctrl+c does not work correctly with
  // SimulateKeyPress(). Using IDC_COPY does not work on Mac in browser_tests.
  void SendCopyCommandAndCheckCopyPasteClipboard(const std::string& expected) {
    DoActionAndCheckClipboard(
        base::BindLambdaForTesting([&]() { GetEmbedderWebContents()->Copy(); }),
        ui::ClipboardBuffer::kCopyPaste, expected);
  }

 private:
  // Runs `action` and checks `clipboard_buffer` contains `expected`.
  void DoActionAndCheckClipboard(base::OnceClosure action,
                                 ui::ClipboardBuffer clipboard_buffer,
                                 const std::string& expected) {
    ASSERT_FALSE(clipboard_quit_closure_);

    ui::ClipboardMonitor::GetInstance()->AddObserver(this);
    EXPECT_FALSE(clipboard_changed_);
    clipboard_changed_ = false;

    base::RunLoop run_loop;
    clipboard_quit_closure_ = run_loop.QuitClosure();
    std::move(action).Run();
    run_loop.Run();
    EXPECT_FALSE(clipboard_quit_closure_);

    EXPECT_TRUE(clipboard_changed_);
    clipboard_changed_ = false;
    ui::ClipboardMonitor::GetInstance()->RemoveObserver(this);

    auto* clipboard = ui::Clipboard::GetForCurrentThread();
    std::string clipboard_data;
    clipboard->ReadAsciiText(clipboard_buffer, /* data_dst=*/nullptr,
                             &clipboard_data);
    EXPECT_EQ(expected, clipboard_data);
  }

  base::RepeatingClosure clipboard_quit_closure_;
  bool clipboard_changed_ = false;
};

// TODO(crbug.com/40715498): Fix flakiness.
// TODO(crbug.com/41493691): Fix flakiness.
IN_PROC_BROWSER_TEST_P(PDFExtensionClipboardTest,
                       DISABLED_IndividualShiftRightArrowPresses) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  // Give the editable combo box focus.
  ClickLeftSideOfEditableComboBox(extension_host);

  TypeHello(extension_host);

  // Put the cursor back to the left side of the combo box.
  ClickLeftSideOfEditableComboBox(extension_host);

  // Press shift + right arrow 3 times. Letting go of shift in between.
  auto action = base::BindLambdaForTesting(
      [&]() { PressShiftRightArrow(extension_host); });
  DoActionAndCheckSelectionClipboard(action, "H");
  DoActionAndCheckSelectionClipboard(action, "HE");
  DoActionAndCheckSelectionClipboard(action, "HEL");
  SendCopyCommandAndCheckCopyPasteClipboard("HEL");
}

// TODO(crbug.com/40599189): test is flaky.
IN_PROC_BROWSER_TEST_P(PDFExtensionClipboardTest,
                       DISABLED_IndividualShiftLeftArrowPresses) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  // Give the editable combo box focus.
  ClickLeftSideOfEditableComboBox(extension_host);

  TypeHello(extension_host);

  // Put the cursor back to the left side of the combo box.
  ClickLeftSideOfEditableComboBox(extension_host);

  for (int i = 0; i < 3; ++i)
    PressRightArrow(extension_host);

  // Press shift + left arrow 2 times. Letting go of shift in between.
  auto action = base::BindLambdaForTesting(
      [&]() { PressShiftLeftArrow(extension_host); });
  DoActionAndCheckSelectionClipboard(action, "L");
  DoActionAndCheckSelectionClipboard(action, "EL");
  SendCopyCommandAndCheckCopyPasteClipboard("EL");

  // Press shift + left arrow 2 times. Letting go of shift in between.
  DoActionAndCheckSelectionClipboard(action, "HEL");
  DoActionAndCheckSelectionClipboard(action, "HEL");
  SendCopyCommandAndCheckCopyPasteClipboard("HEL");
}

// Flaky, https://crbug.com/1121446, https://crbug.com/1520715
IN_PROC_BROWSER_TEST_P(PDFExtensionClipboardTest,
                       DISABLED_CombinedShiftRightArrowPresses) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  // Give the editable combo box focus.
  ClickLeftSideOfEditableComboBox(extension_host);

  TypeHello(extension_host);

  // Put the cursor back to the left side of the combo box.
  ClickLeftSideOfEditableComboBox(extension_host);

  // Press shift + right arrow 3 times. Holding down shift in between.
  {
    content::ScopedSimulateModifierKeyPress hold_shift(
        GetEmbedderWebContents(), false, true, false, false);
    auto action = base::BindLambdaForTesting([&]() {
      hold_shift.KeyPressWithoutChar(ui::DomKey::ARROW_RIGHT,
                                     ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT);
    });
    DoActionAndCheckSelectionClipboard(action, "H");
    DoActionAndCheckSelectionClipboard(action, "HE");
    DoActionAndCheckSelectionClipboard(action, "HEL");
  }
  SendCopyCommandAndCheckCopyPasteClipboard("HEL");
}

// Flaky on multiple platforms (https://crbug.com/1121446)
IN_PROC_BROWSER_TEST_P(PDFExtensionClipboardTest,
                       DISABLED_CombinedShiftArrowPresses) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));
  ASSERT_TRUE(extension_host);

  // Give the editable combo box focus.
  ClickLeftSideOfEditableComboBox(extension_host);

  TypeHello(extension_host);

  // Put the cursor back to the left side of the combo box.
  ClickLeftSideOfEditableComboBox(extension_host);

  for (int i = 0; i < 3; ++i)
    PressRightArrow(extension_host);

  // Press shift + left arrow 3 times. Holding down shift in between.
  {
    content::ScopedSimulateModifierKeyPress hold_shift(
        GetEmbedderWebContents(), false, true, false, false);
    auto action = base::BindLambdaForTesting([&]() {
      hold_shift.KeyPressWithoutChar(ui::DomKey::ARROW_LEFT,
                                     ui::DomCode::ARROW_LEFT, ui::VKEY_LEFT);
    });
    DoActionAndCheckSelectionClipboard(action, "L");
    DoActionAndCheckSelectionClipboard(action, "EL");
    DoActionAndCheckSelectionClipboard(action, "HEL");
  }
  SendCopyCommandAndCheckCopyPasteClipboard("HEL");

  // Press shift + right arrow 2 times. Holding down shift in between.
  {
    content::ScopedSimulateModifierKeyPress hold_shift(
        GetEmbedderWebContents(), false, true, false, false);
    auto action = base::BindLambdaForTesting([&]() {
      hold_shift.KeyPressWithoutChar(ui::DomKey::ARROW_RIGHT,
                                     ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT);
    });
    DoActionAndCheckSelectionClipboard(action, "EL");
    DoActionAndCheckSelectionClipboard(action, "L");
  }
  SendCopyCommandAndCheckCopyPasteClipboard("L");
}

// Verifies that an <embed> of size zero will still instantiate a guest and post
// message to the <embed> is correctly forwarded to the extension. This is for
// catching future regression in docs/ and slides/ pages (see
// https://crbug.com/763812).
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, PostMessageForZeroSizedEmbed) {
  content::DOMMessageQueue queue(
      browser()->tab_strip_model()->GetActiveWebContents());
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL(
                     "/pdf/post_message_zero_sized_embed.html")));
  std::string message;
  EXPECT_TRUE(queue.WaitForMessage(&message));
  EXPECT_EQ("\"POST_MESSAGE_OK\"", message);
}

// In response to the events sent in |send_events|, ensures the PDF viewer zooms
// in and that the viewer's custom pinch zooming mechanism is used to do so.
void EnsureCustomPinchZoomInvoked(content::RenderFrameHost* guest_mainframe,
                                  WebContents* contents,
                                  base::OnceClosure send_events) {
  constexpr char kListenPinchUpdate[] = R"(
      const gestureDetector = viewer.viewport.getGestureDetectorForTesting();
      var updatePromise = new Promise((resolve) => {
        gestureDetector.getEventTarget().addEventListener('pinchupdate',
                                                          resolve);
      });
  )";
  ASSERT_TRUE(content::ExecJs(guest_mainframe, kListenPinchUpdate));

  zoom::ZoomChangedWatcher zoom_watcher(
      contents,
      base::BindRepeating(
          [](const zoom::ZoomController::ZoomChangedEventData& event) {
            return event.new_zoom_level > event.old_zoom_level &&
                   event.zoom_mode == zoom::ZoomController::ZOOM_MODE_MANUAL &&
                   !event.can_show_bubble;
          }));

  std::move(send_events).Run();

  EXPECT_EQ(true, content::EvalJs(guest_mainframe,
                                  "updatePromise.then((update) => !!update);"));

  zoom_watcher.Wait();

  // Check that the browser's native pinch zoom was prevented.
  EXPECT_DOUBLE_EQ(
      1.0,
      content::EvalJs(contents, "window.visualViewport.scale").ExtractDouble());
}

// Ensure that touchpad pinch events are handled by the PDF viewer.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, TouchpadPinchInvokesCustomZoom) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  content::WaitForHitTestData(extension_host);

  base::OnceClosure send_pinch = base::BindOnce(
      [](content::RenderFrameHost* extension_host) {
        const gfx::Rect extension_host_rect =
            extension_host->GetView()->GetViewBounds();
        const gfx::Point mouse_position(extension_host_rect.width() / 2,
                                        extension_host_rect.height() / 2);
        content::SimulateGesturePinchSequence(
            extension_host->GetRenderWidgetHost(), mouse_position, 1.23,
            blink::WebGestureDevice::kTouchpad);
      },
      extension_host);

  EnsureCustomPinchZoomInvoked(extension_host, GetActiveWebContents(),
                               std::move(send_pinch));
}

#if !BUILDFLAG(IS_MAC)
// Ensure that ctrl-wheel events are handled by the PDF viewer.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, CtrlWheelInvokesCustomZoom) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  content::WaitForHitTestData(extension_host);

  base::OnceClosure send_ctrl_wheel = base::BindOnce(
      [](content::RenderFrameHost* extension_host) {
        const gfx::Rect extension_host_rect =
            extension_host->GetView()->GetViewBounds();
        const gfx::Point mouse_position(extension_host_rect.width() / 2,
                                        extension_host_rect.height() / 2);
        content::SimulateMouseWheelCtrlZoomEvent(
            extension_host->GetRenderWidgetHost(), mouse_position, true,
            blink::WebMouseWheelEvent::kPhaseBegan);
      },
      extension_host);

  EnsureCustomPinchZoomInvoked(extension_host, GetActiveWebContents(),
                               std::move(send_ctrl_wheel));
}

// Flaky on ChromeOS (https://crbug.com/922974)
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_TouchscreenPinchInvokesCustomZoom \
  DISABLED_TouchscreenPinchInvokesCustomZoom
#else
#define MAYBE_TouchscreenPinchInvokesCustomZoom \
  TouchscreenPinchInvokesCustomZoom
#endif
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       MAYBE_TouchscreenPinchInvokesCustomZoom) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  content::WaitForHitTestData(extension_host);

  base::OnceClosure send_touchscreen_pinch = base::BindOnce(
      [](WebContents* web_contents, content::RenderFrameHost* extension_host) {
        const gfx::Rect extension_host_rect =
            extension_host->GetView()->GetViewBounds();
        const gfx::PointF anchor_position(extension_host_rect.width() / 2,
                                          extension_host_rect.height() / 2);
        base::RunLoop run_loop;
        content::SimulateTouchscreenPinch(web_contents, anchor_position, 1.2f,
                                          run_loop.QuitClosure());
        run_loop.Run();
      },
      GetActiveWebContents(), extension_host);

  EnsureCustomPinchZoomInvoked(extension_host, GetActiveWebContents(),
                               std::move(send_touchscreen_pinch));
}

#endif  // !BUILDFLAG(IS_MAC)

using PDFExtensionHitTestTest = PDFExtensionTest;

// Flaky in nearly all configurations; see https://crbug.com/856169.
IN_PROC_BROWSER_TEST_P(PDFExtensionHitTestTest, DISABLED_MouseLeave) {
  // Load page with embedded PDF and make sure it succeeds.
  content::RenderFrameHost* extension_host =
      LoadPdfInFirstChildGetExtensionHost(
          embedded_test_server()->GetURL("/pdf/pdf_embed.html"));
  ASSERT_TRUE(extension_host);

  WebContents* embedder_contents = GetActiveWebContents();

  content::WaitForHitTestData(extension_host);

  gfx::Point point_in_parent(250, 25);
  gfx::Point point_in_pdf(250, 250);

  // Inject script to count MouseLeaves in the PDF.
  ASSERT_TRUE(
      content::ExecJs(extension_host,
                      "var enter_count = 0;\n"
                      "var leave_count = 0;\n"
                      "document.addEventListener('mouseenter', function (){\n"
                      "  enter_count++;"
                      "});\n"
                      "document.addEventListener('mouseleave', function (){\n"
                      "  leave_count++;"
                      "});"));

  // Inject some MouseMoves to invoke a MouseLeave in the PDF.
  content::SimulateMouseEvent(embedder_contents,
                              blink::WebInputEvent::Type::kMouseMove,
                              point_in_parent);
  content::SimulateMouseEvent(
      embedder_contents, blink::WebInputEvent::Type::kMouseMove, point_in_pdf);
  content::SimulateMouseEvent(embedder_contents,
                              blink::WebInputEvent::Type::kMouseMove,
                              point_in_parent);

  // Verify MouseEnter, MouseLeave received.
  int leave_count = 0;
  do {
    leave_count = EvalJs(extension_host, "leave_count;").ExtractInt();
  } while (!leave_count);
  EXPECT_EQ(1, EvalJs(extension_host, "enter_count;"));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionHitTestTest, ContextMenuCoordinates) {
  // Load page with embedded PDF and make sure it succeeds.
  content::RenderFrameHost* extension_host =
      LoadPdfInFirstChildGetExtensionHost(
          embedded_test_server()->GetURL("/pdf/pdf_embed.html"));
  ASSERT_TRUE(extension_host);

  WebContents* embedder_contents = GetActiveWebContents();

  content::WaitForHitTestData(extension_host);

  // Observe context menu IPC.
  content::RenderFrameHost* plugin_frame =
      pdf_frame_util::FindPdfChildFrame(extension_host);
  content::ContextMenuInterceptor context_menu_interceptor(plugin_frame);

  ContextMenuWaiter menu_observer;

  // Send mouse right-click to activate context menu.
  gfx::Point context_menu_position(80, 130);
  content::SimulateMouseClickAt(embedder_contents, kDefaultKeyModifier,
                                blink::WebMouseEvent::Button::kRight,
                                context_menu_position);

  // We expect the context menu, invoked via the RenderFrameHost, to be using
  // root view coordinates.
  menu_observer.WaitForMenuOpenAndClose();
  ASSERT_EQ(context_menu_position.x(), menu_observer.params().x);
  ASSERT_EQ(context_menu_position.y(), menu_observer.params().y);

  // We expect the IPC, received from the renderer, to be using local coords.
  context_menu_interceptor.Wait();
  blink::UntrustworthyContextMenuParams params =
      context_menu_interceptor.get_params();
  gfx::Point received_context_menu_position =
      plugin_frame->GetRenderWidgetHost()
          ->GetView()
          ->TransformPointToRootCoordSpace({params.x, params.y});
  EXPECT_EQ(context_menu_position, received_context_menu_position);

  // TODO(wjmaclean): If it ever becomes possible to filter outgoing IPCs from
  // the RenderProcessHost, we should verify the blink.mojom.PluginActionAt
  // message is sent with the same coordinates as in the
  // UntrustworthyContextMenuParams.
}

// The plugin document and the mime handler should both use the same background
// color.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, BackgroundColor) {
  // The background color for plugins is injected when the first response
  // is intercepted, at which point not all the plugins have loaded. This line
  // ensures that the PDF plugin has loaded and the right background color is
  // beign used.
  WaitForPluginServiceToLoad();
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  const std::string script =
      "window.getComputedStyle(document.body, null)."
      "getPropertyValue('background-color')";
  std::string outer =
      content::EvalJs(GetActiveWebContents(), script).ExtractString();
  std::string inner = content::EvalJs(extension_host, script).ExtractString();
  EXPECT_EQ(inner, outer);
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, DefaultFocusForEmbeddedPDF) {
  content::RenderFrameHost* extension_host =
      LoadPdfInFirstChildGetExtensionHost(
          embedded_test_server()->GetURL("/pdf/pdf_embed.html"));
  ASSERT_TRUE(extension_host);

  // Verify that current focus state is body element.
  const std::string script =
      "const is_plugin_focused = document.activeElement === "
      "document.body;"
      "is_plugin_focused;";

  ASSERT_EQ(true, content::EvalJs(extension_host, script));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, DefaultFocusForNonEmbeddedPDF) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  // Verify that current focus state is document element.
  const std::string script =
      "const is_plugin_focused = document.activeElement === "
      "document.body;"
      "is_plugin_focused;";

  ASSERT_EQ(true, content::EvalJs(extension_host, script));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, PdfVisibility) {
  GURL url = embedded_test_server()->GetURL("/pdf/pdf_embed.html");
  EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_EQ(content::Visibility::VISIBLE, web_contents->GetVisibility());

  web_contents->WasHidden();
  EXPECT_EQ(content::Visibility::HIDDEN, web_contents->GetVisibility());

  auto inner_contents = web_contents->GetInnerWebContents();
  if (base::FeatureList::IsEnabled(chrome_pdf::features::kPdfOopif)) {
    // No inner WebContents, nothing to do.
    EXPECT_EQ(0u, inner_contents.size());
    return;
  }

  ASSERT_EQ(1u, inner_contents.size());
  WebContents* inner = inner_contents[0];
  EXPECT_EQ(content::Visibility::HIDDEN, inner->GetVisibility());

  web_contents->WasShown();
  EXPECT_EQ(content::Visibility::VISIBLE, web_contents->GetVisibility());
  EXPECT_EQ(content::Visibility::VISIBLE, inner->GetVisibility());
}

// A helper for waiting for the first request for |url_to_intercept|.
class RequestWaiter {
 public:
  // Start intercepting requests to |url_to_intercept|.
  explicit RequestWaiter(const GURL& url_to_intercept)
      : url_to_intercept_(url_to_intercept),
        interceptor_(base::BindRepeating(&RequestWaiter::InterceptorCallback,
                                         base::Unretained(this))) {
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
    DCHECK(url_to_intercept.is_valid());
  }

  RequestWaiter(const RequestWaiter&) = delete;
  RequestWaiter& operator=(const RequestWaiter&) = delete;

  void WaitForRequest() {
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
    if (!IsAlreadyIntercepted())
      run_loop_.Run();
    DCHECK(IsAlreadyIntercepted());
  }

 private:
  bool InterceptorCallback(
      content::URLLoaderInterceptor::RequestParams* params) {
    // This method may be called either on the IO or UI thread.
    DCHECK(params);

    base::AutoLock lock(lock_);
    if (url_to_intercept_ != params->url_request.url || already_intercepted_)
      return false;

    already_intercepted_ = true;
    run_loop_.Quit();
    return false;
  }

  bool IsAlreadyIntercepted() {
    base::AutoLock lock(lock_);
    return already_intercepted_;
  }

  const GURL url_to_intercept_;
  content::URLLoaderInterceptor interceptor_;
  base::RunLoop run_loop_;

  base::Lock lock_;
  bool already_intercepted_ GUARDED_BY(lock_) = false;
};

// This is a regression test for a problem where DidStopLoading didn't get
// propagated from a remote frame into the main frame.  See also
// https://crbug.com/964364.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, DidStopLoading) {
  // Prepare to wait for requests for the main page of the MimeHandlerView for
  // PDFs.
  RequestWaiter interceptor(
      GURL("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html"));

  // Navigate to a page with:
  //   <embed type="application/pdf" src="test.pdf"></embed>
  //   <iframe src="/hung"></iframe>
  // Afterwards, the main page should be still loading because of the hung
  // subframe (but the subframe for the OOPIF-based PDF MimeHandlerView might or
  // might not be created at this point).
  ui_test_utils::NavigateToURLWithDisposition(
      browser(),
      embedded_test_server()->GetURL(
          "/pdf/pdf_embed_with_hung_sibling_subframe.html"),
      WindowOpenDisposition::CURRENT_TAB,
      ui_test_utils::BROWSER_TEST_NO_WAIT);  // Don't wait for completion.

  // Wait for the request for the MimeHandlerView extension.  Afterwards, the
  // main page should be still loading because of
  // 1) the MimeHandlerView frame is loading
  // 2) the hung subframe is loading.
  interceptor.WaitForRequest();

  // Remove the hung subframe.  Afterwards the main page should stop loading as
  // soon as the MimeHandlerView frame stops loading (assumming we have not bugs
  // similar to https://crbug.com/964364).
  WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(content::ExecJs(
      web_contents, "document.getElementById('hung_subframe').remove();"));

  // MAIN VERIFICATION: Wait for the main frame to report that is has stopped
  // loading.
  EXPECT_TRUE(content::WaitForLoadStop(web_contents));
}

// This test verifies that it is possible to add an <embed src=pdf> element into
// a new popup window when using document.write.  See also
// https://crbug.com/1041880.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, DocumentWriteIntoNewPopup) {
  // Navigate to an empty/boring test page.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/title1.html")));

  // Open a new popup and call document.write to add an embedded PDF.
  const GURL pdf_url(embedded_test_server()->GetURL("/pdf/test.pdf"));

  content::TestNavigationObserver navigation_observer(pdf_url);
  navigation_observer.StartWatchingNewWebContents();

  WebContents* popup = nullptr;
  {
    const char kScriptTemplate[] = R"(
      const url = $1;
      const html = '<embed type="application/pdf" src="' + url + '">';

      const popup = window.open('', '_blank');
      popup.document.write(html);
  )";
    content::WebContentsAddedObserver popup_observer;
    ASSERT_TRUE(content::ExecJs(GetActiveWebContents(),
                                content::JsReplace(kScriptTemplate, pdf_url)));
    popup = popup_observer.GetWebContents();
  }

  // Wait for the PDF navigation to finish.
  navigation_observer.Wait();

  // Verify the PDF loaded successfully.
  if (UseOopif()) {
    EXPECT_TRUE(
        GetTestPdfViewerStreamManager(popup)->WaitUntilPdfLoadedInFirstChild());
  } else {
    EXPECT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(popup));
  }
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, LoadPdfFromExtension) {
  const extensions::Extension* test_extension = LoadExtension(
      GetTestResourcesParentDir().AppendASCII("pdf/extension_with_pdf"));
  ASSERT_TRUE(test_extension);

  EXPECT_TRUE(LoadPdf(test_extension->ResolveExtensionURL("test.pdf")));
}

// Tests that the PDF extension loads in the presence of an extension that, on
// the completion of document loading, adds an <iframe> to the body element.
// See crbug.com/40671023.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       PdfLoadsWithExtensionThatInjectsFrame) {
  const extensions::Extension* test_extension = LoadExtension(
      GetTestResourcesParentDir().AppendASCII("pdf/extension_injects_iframe"));
  ASSERT_TRUE(test_extension);

  EXPECT_TRUE(LoadPdfAllowMultipleFrames(
      embedded_test_server()->GetURL("/pdf/test.pdf")));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionTest, Metrics) {
  base::HistogramTester histograms;
  base::UserActionTester actions;

  ASSERT_TRUE(
      LoadPdf(embedded_test_server()->GetURL("/pdf/combobox_form.pdf")));

  metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting();

  // Histograms.
  // Duplicating some constants to avoid reaching into pdf/ internals.
  constexpr int kAcroForm = 1;
  constexpr int k1_7 = 8;
  histograms.ExpectUniqueSample("PDF.FormType", kAcroForm, 1);
  histograms.ExpectUniqueSample("PDF.Version", k1_7, 1);
  histograms.ExpectUniqueSample("PDF.HasAttachment", 0, 1);

  // Custom histograms.
  histograms.ExpectUniqueSample("PDF.PageCount", 1, 1);

  // User actions.
  EXPECT_EQ(1, actions.GetActionCount("PDF.LoadSuccess"));
}

// Test that the PDF.LoadStatus2 metric is incremented correctly when the PDF is
// loaded with PDFium.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       MetricsPDFLoadStatusLoadedPdfWithPdfium) {
  const char kPdfLoadStatusMetric[] = "PDF.LoadStatus2";
  base::HistogramTester histograms;

  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedFullPagePdfWithPdfium, 0);
  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedEmbeddedPdfWithPdfium, 0);

  EXPECT_TRUE(
      LoadPdf(embedded_test_server()->GetURL("/pdf/combobox_form.pdf")));

  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedFullPagePdfWithPdfium, 1);
  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedEmbeddedPdfWithPdfium, 0);

  EXPECT_TRUE(LoadPdfInFirstChild(
      embedded_test_server()->GetURL("/pdf/pdf_embed.html")));

  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedFullPagePdfWithPdfium, 1);
  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedEmbeddedPdfWithPdfium, 1);

  // All other buckets should not be incremented.
  histograms.ExpectBucketCount(
      kPdfLoadStatusMetric,
      PDFLoadStatus::kShowedDisabledPluginPlaceholderForEmbeddedPdf, 0);
  histograms.ExpectBucketCount(
      kPdfLoadStatusMetric, PDFLoadStatus::kTriggeredNoGestureDriveByDownload,
      0);
  histograms.ExpectBucketCount(
      kPdfLoadStatusMetric, PDFLoadStatus::kLoadedIframePdfWithNoPdfViewer, 0);
  histograms.ExpectBucketCount(
      kPdfLoadStatusMetric,
      PDFLoadStatus::kViewPdfClickedInPdfPluginPlaceholder, 0);
}

// Flaky. See https://crbug.com/1101514.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest, DISABLED_TabInAndOutOfPDFPlugin) {
  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  // Set focus on last toolbar element (zoom-out-button).
  ASSERT_TRUE(content::ExecJs(extension_host,
                              R"(viewer.shadowRoot.querySelector('#zoomToolbar')
         .$['zoom-out-button']
         .$$('cr-icon-button')
         .focus();)"));

  // The script will ensure we return the the focused element on focus.
  const char kScript[] = R"(
    const plugin = viewer.shadowRoot.querySelector('#plugin');
    plugin.addEventListener('focus', () => {
      window.domAutomationController.send('plugin');
    });

    const button = viewer.shadowRoot.querySelector('#zoomToolbar')
                   .$['zoom-out-button']
                   .$$('cr-icon-button');
    button.addEventListener('focus', () => {
      window.domAutomationController.send('zoom-out-button');
    });
  )";
  ASSERT_TRUE(content::ExecJs(extension_host, kScript));

  // Helper to simulate a tab press and wait for a focus message.
  auto press_tab_and_wait_for_message = [extension_host, this](bool reverse) {
    content::DOMMessageQueue msg_queue(extension_host);
    std::string reply;
    SimulateKeyPress(GetActiveWebContents(), ui::DomKey::TAB, ui::DomCode::TAB,
                     ui::VKEY_TAB, false, /*shift=*/reverse, false, false);
    EXPECT_TRUE(msg_queue.WaitForMessage(&reply));
    return reply;
  };

  // Press <tab> and ensure that PDF document receives focus.
  EXPECT_EQ("\"plugin\"", press_tab_and_wait_for_message(false));
  // Press <shift-tab> and ensure that last toolbar element (zoom-out-button)
  // receives focus.
  EXPECT_EQ("\"zoom-out-button\"", press_tab_and_wait_for_message(true));
}

// Test that a PDF with COEP: require-corp header can load successfully.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       CrossOriginEmbedderPolicyRequireCorpPdf) {
  EXPECT_TRUE(LoadPdf(
      embedded_test_server()->GetURL("/pdf/test-coep-require-corp.pdf")));
}

// Test that a PDF with COEP: credentialless header can load successfully.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       CrossOriginEmbedderPolicyCredentiallessPdf) {
  EXPECT_TRUE(LoadPdf(
      embedded_test_server()->GetURL("/pdf/test-coep-credentialless.pdf")));
}

// Test that a PDF without the COEP: require-corp header fails to load when
// embedded in a page that has the header.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       CrossOriginEmbedderPolicyRequireCorpIframe) {
  EXPECT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/test-coep-iframe.html")));

  // The PDF failed to load if there aren't any PDF extension hosts or PDF
  // plugin frames.
  WebContents* web_contents = GetActiveWebContents();
  EXPECT_TRUE(
      pdf_extension_test_util::GetPdfExtensionHosts(web_contents).empty());
  EXPECT_TRUE(
      pdf_extension_test_util::GetPdfPluginFrames(web_contents).empty());
  EXPECT_EQ(0u, pdf_extension_test_util::CountPdfPluginProcesses(browser()));
}

// Test that a data: URL PDF embed can load successfully when embedded in a page
// that has the COEP: require-corp header.
IN_PROC_BROWSER_TEST_P(PDFExtensionTest,
                       CrossOriginEmbedderPolicyDataUrlPdfIframe) {
  EXPECT_TRUE(LoadPdfInFirstChild(
      embedded_test_server()->GetURL("/pdf/test-coep-data-pdf-embed.html")));
}

class PDFExtensionPrerenderTest : public PDFExtensionTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    PDFExtensionTest::SetUpCommandLine(command_line);
    // |prerender_helper_| has a ScopedFeatureList so we needed to delay its
    // creation until now because PDFExtensionTest also uses a ScopedFeatureList
    // and initialization order matters.
    prerender_helper_ = std::make_unique<content::test::PrerenderTestHelper>(
        base::BindRepeating(&PDFExtensionPrerenderTest::GetActiveWebContents,
                            base::Unretained(this)));
  }

  void SetUpOnMainThread() override {
    prerender_helper_->RegisterServerRequestMonitor(embedded_test_server());
    PDFExtensionTest::SetUpOnMainThread();
  }

 protected:
  void PrerenderAndExpectCancellation(const GURL& prerender_url) {
    content::test::PrerenderHostObserver observer(*GetActiveWebContents(),
                                                  prerender_url);
    prerender_helper().AddPrerenderAsync(prerender_url);
    observer.WaitForDestroyed();
  }

  content::test::PrerenderTestHelper& prerender_helper() {
    return *prerender_helper_;
  }

 private:
  std::unique_ptr<content::test::PrerenderTestHelper> prerender_helper_;
};

// TODO(crbug.com/40180674): The PDF viewer cannot currently be prerendered
// correctly. This tests that prerendering is cancelled. Once we're able to
// support this, this test should be replaced with one that prerenders the PDF
// viewer.
IN_PROC_BROWSER_TEST_P(PDFExtensionPrerenderTest, CancelPrerender) {
  const GURL initial_url =
      embedded_test_server()->GetURL("a.test", "/empty.html");
  const GURL pdf_url =
      embedded_test_server()->GetURL("a.test", "/pdf/test.pdf");
  WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), initial_url));

  PrerenderAndExpectCancellation(pdf_url);
  if (UseOopif()) {
    EXPECT_FALSE(pdf::PdfViewerStreamManager::FromWebContents(web_contents));
  } else {
    EXPECT_EQ(0U, GetGuestViewManager()->num_guests_created());
  }

  prerender_helper().NavigatePrimaryPage(pdf_url);
  ASSERT_EQ(web_contents->GetLastCommittedURL(), pdf_url);
  EXPECT_TRUE(EnsureFullPagePDFHasLoadedWithValidFrameTree(
      web_contents, /*allow_multiple_frames=*/false));
}

// TODO(crbug.com/40180674): The PDF viewer cannot currently be prerendered
// correctly. This tests that prerendering is cancelled if a PDF is embedded in
// a prerendered page. Once we're able to support this, this test should be
// replaced with one that prerenders the PDF viewer.
IN_PROC_BROWSER_TEST_P(PDFExtensionPrerenderTest,
                       CancelPrerenderWithEmbeddedPdf) {
  const GURL initial_url =
      embedded_test_server()->GetURL("a.test", "/empty.html");
  const GURL pdf_url =
      embedded_test_server()->GetURL("a.test", "/pdf/test-iframe.html");
  WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), initial_url));

  PrerenderAndExpectCancellation(pdf_url);
  if (UseOopif()) {
    EXPECT_FALSE(pdf::PdfViewerStreamManager::FromWebContents(web_contents));
  } else {
    EXPECT_EQ(0U, GetGuestViewManager()->num_guests_created());
  }

  prerender_helper().NavigatePrimaryPage(pdf_url);
  ASSERT_EQ(web_contents->GetLastCommittedURL(), pdf_url);
  if (UseOopif()) {
    EXPECT_TRUE(EnsurePDFHasLoadedInFirstChildWithValidFrameTree(web_contents));
  } else {
    EXPECT_TRUE(GetGuestViewManager()->WaitForSingleGuestViewCreated());
  }
}

// Cross-origin subframe navigations are deferred during prerendering, which
// means that an embedded cross-site PDF will not cause the PDF viewer to be
// created until prerender activation.
IN_PROC_BROWSER_TEST_P(PDFExtensionPrerenderTest,
                       PrerenderWithCrossSiteEmbeddedPdf) {
  const GURL initial_url =
      embedded_test_server()->GetURL("a.test", "/empty.html");
  const GURL pdf_url = embedded_test_server()->GetURL(
      "a.test", "/pdf/test-cross-site-iframe.html");
  WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), initial_url));

  content::test::PrerenderHostRegistryObserver registry_observer(*web_contents);
  prerender_helper().AddPrerenderAsync(pdf_url);
  registry_observer.WaitForTrigger(pdf_url);
  const content::FrameTreeNodeId host_id =
      prerender_helper().GetHostForUrl(pdf_url);
  EXPECT_TRUE(host_id);

  content::test::PrerenderHostObserver prerender_observer(*web_contents,
                                                          host_id);

  if (UseOopif()) {
    // There are two expected navigations in `web_contents`: the navigation to
    // the webpage containing the iframe, and the cross-site navigation to the
    // PDF in the iframe.
    content::TestNavigationObserver navigation_observer(web_contents, 2);
    prerender_helper().NavigatePrimaryPage(pdf_url);
    prerender_observer.WaitForActivation();
    navigation_observer.Wait();

    ASSERT_EQ(web_contents->GetLastCommittedURL(), pdf_url);
    ASSERT_TRUE(pdf::TestPdfViewerStreamManager::FromWebContents(web_contents));
    EXPECT_TRUE(GetTestPdfViewerStreamManager(web_contents)
                    ->WaitUntilPdfLoadedInFirstChild());
  } else {
    prerender_helper().NavigatePrimaryPage(pdf_url);
    prerender_observer.WaitForActivation();

    ASSERT_EQ(web_contents->GetLastCommittedURL(), pdf_url);
    EXPECT_TRUE(GetGuestViewManager()->WaitForSingleGuestViewCreated());
  }
}

class PDFExtensionSubmitFormTest : public PDFExtensionTest {
 public:
  void SetUpOnMainThread() override {
    embedded_test_server()->RegisterRequestMonitor(base::BindLambdaForTesting(
        [this](const net::test_server::HttpRequest& request) {
          if (request.relative_url != "/pdf/test_endpoint")
            return;

          EXPECT_EQ(request.method, net::test_server::METHOD_POST);
          EXPECT_THAT(request.content, StartsWith("\%FDF"));
          ASSERT_TRUE(quit_closure_);
          std::move(quit_closure_).Run();
        }));

    PDFExtensionTest::SetUpOnMainThread();
  }

 protected:
  // Retrieves a `base::RunLoop` and saves its `QuitClosure()`. The test
  // monitors HTTP requests on the IO thread, so `quit_closure_` needs to be set
  // up on the UI thread before the requests can arrive.
  std::unique_ptr<base::RunLoop> CreateFormSubmissionRunLoop() {
    auto run_loop = std::make_unique<base::RunLoop>();
    EXPECT_FALSE(quit_closure_);
    quit_closure_ = run_loop->QuitClosure();
    return run_loop;
  }

 private:
  base::OnceClosure quit_closure_;
};

IN_PROC_BROWSER_TEST_P(PDFExtensionSubmitFormTest, SubmitForm) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/submit_form.pdf"));
  ASSERT_TRUE(extension_host);

  std::unique_ptr<base::RunLoop> run_loop = CreateFormSubmissionRunLoop();

  // Click on the "Submit Form" button.
  SimulateMouseClickAt(
      extension_host, GetEmbedderWebContents(),
      blink::WebInputEvent::kNoModifiers, blink::WebMouseEvent::Button::kLeft,
      ConvertPageCoordToScreenCoord(extension_host, {210, 210}));

  run_loop->Run();
}

class PDFExtensionPrerenderAndFencedFrameTest : public PDFExtensionTest {
 public:
  PDFExtensionPrerenderAndFencedFrameTest() = default;
  ~PDFExtensionPrerenderAndFencedFrameTest() override = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    PDFExtensionTest::SetUpCommandLine(command_line);
    // `prerender_helper_` and `fenced_frame_helper_` has a ScopedFeatureList so
    // we needed to delay its creation until now because PDFExtensionTest
    // also uses a ScopedFeatureList and initialization order matters.
    prerender_helper_ = std::make_unique<content::test::PrerenderTestHelper>(
        base::BindRepeating(&PDFExtensionPrerenderTest::GetActiveWebContents,
                            base::Unretained(this)));
    fenced_frame_helper_ =
        std::make_unique<content::test::FencedFrameTestHelper>();
  }

  content::test::PrerenderTestHelper& prerender_helper() {
    return *prerender_helper_;
  }

  content::test::FencedFrameTestHelper& fenced_frame_helper() {
    return *fenced_frame_helper_;
  }

 private:
  std::unique_ptr<content::test::PrerenderTestHelper> prerender_helper_;
  std::unique_ptr<content::test::FencedFrameTestHelper> fenced_frame_helper_;
};

// TODO(crbug.com/40180674): The PDF viewer cannot currently be prerendered
// correctly. Once this is supported, this test should be re-enabled for
// GuestView PDF viewer and enabled for OOPIF PDF viewer.
IN_PROC_BROWSER_TEST_P(PDFExtensionPrerenderAndFencedFrameTest,
                       DISABLED_LoadPDFInPrerender) {
  if (UseOopif()) {
    GTEST_SKIP();
  }

  GURL url = embedded_test_server()->GetURL("/empty.html");
  ASSERT_TRUE(content::NavigateToURL(GetActiveWebContents(), url));

  TestMimeHandlerViewGuest::RegisterTestGuestViewType(GetGuestViewManager());
  // Set a 1s delay to delay MimeHandlerViewGuest's creation to ensure that the
  // fenced frame is loaded while the PDF stream is not yet consumed.
  const int creation_delay = TestTimeouts::tiny_timeout().InMilliseconds();
  TestMimeHandlerViewGuest::DelayNextCreateWebContents(creation_delay);

  // Load a PDF in the prerender.
  GURL prerender_url = embedded_test_server()->GetURL("/pdf/test.pdf");

  content::test::PrerenderHostRegistryObserver registry_observer(
      *GetActiveWebContents());
  prerender_helper().AddPrerenderAsync(prerender_url);
  registry_observer.WaitForTrigger(prerender_url);

  // Create a fenced frame.
  const GURL fenced_frame_url =
      embedded_test_server()->GetURL("/fenced_frames/title1.html");
  content::RenderFrameHost* fenced_frame_host =
      fenced_frame_helper().CreateFencedFrame(
          GetActiveWebContents()->GetPrimaryMainFrame(), fenced_frame_url);
  ASSERT_TRUE(fenced_frame_host);

  auto* guest_view = GetGuestViewManager()->WaitForSingleGuestViewCreated();
  ASSERT_TRUE(guest_view);
  TestMimeHandlerViewGuest::WaitForGuestLoadStartThenStop(guest_view);

  // Ensure that the fenced frame's navigation should not abort the PDF stream.
  EXPECT_EQ(1U, GetGuestViewManager()->GetCurrentGuestCount());
}

// Test that ensures we cannot navigate a fenced frame to a PDF because PDF
// isn't allowed by default static sandbox flags of fenced frames.
IN_PROC_BROWSER_TEST_P(PDFExtensionPrerenderAndFencedFrameTest,
                       LoadPdfInFencedFrame) {
  ASSERT_TRUE(content::NavigateToURL(
      GetActiveWebContents(), embedded_test_server()->GetURL("/empty.html")));

  // Create a fenced frame and try to navigate to a PDF.
  EXPECT_TRUE(fenced_frame_helper().CreateFencedFrame(
      GetActiveWebContents()->GetPrimaryMainFrame(),
      embedded_test_server()->GetURL("/pdf/test-fenced-frame.pdf"),
      net::Error::ERR_BLOCKED_BY_CLIENT));
  EXPECT_EQ(CountPDFProcesses(), 0);
}

// Like `LoadPdfInFencedFrame`, but without Supports-Loading-Mode headers set.
IN_PROC_BROWSER_TEST_P(PDFExtensionPrerenderAndFencedFrameTest,
                       LoadPdfInFencedFrameWithoutFencedFrameOptIn) {
  ASSERT_TRUE(content::NavigateToURL(
      GetActiveWebContents(), embedded_test_server()->GetURL("/empty.html")));

  // Create a fenced frame and try to navigate to a PDF.
  EXPECT_TRUE(fenced_frame_helper().CreateFencedFrame(
      GetActiveWebContents()->GetPrimaryMainFrame(),
      embedded_test_server()->GetURL("/pdf/test.pdf"),
      net::Error::ERR_BLOCKED_BY_RESPONSE));
  EXPECT_EQ(CountPDFProcesses(), 0);
}

// Test that ensures a fenced frame cannot load a document embedding a PDF
// because PDF isn't allowed in fenced frames.
IN_PROC_BROWSER_TEST_P(PDFExtensionPrerenderAndFencedFrameTest,
                       LoadEmbeddedPdfInFencedFrame) {
  ASSERT_TRUE(content::NavigateToURL(
      GetActiveWebContents(), embedded_test_server()->GetURL("/empty.html")));

  // Create a fenced frame for loading a document with pdf embed(s).
  content::RenderFrameHost* fenced_frame_host =
      fenced_frame_helper().CreateFencedFrame(
          GetActiveWebContents()->GetPrimaryMainFrame(),
          embedded_test_server()->GetURL("/fenced_frames/title1.html"));
  ASSERT_TRUE(fenced_frame_host);

  const GURL pdf_url =
      embedded_test_server()->GetURL("/pdf/test-fenced-frame.pdf");
  // Ensure that the fenced frame cannot load a PDF embedding with <iframe>.
  ASSERT_TRUE(content::ExecJs(
      fenced_frame_host,
      content::JsReplace("let e = document.createElement('iframe');"
                         "e.src = $1;"
                         "e.type = 'application/pdf';"
                         "document.body.appendChild(e);",
                         pdf_url)));
  ASSERT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
  EXPECT_EQ(CountPDFProcesses(), 0);

  // Ensure that the fenced frame cannot load a PDF embedding with <object>.
  ASSERT_TRUE(content::ExecJs(
      fenced_frame_host,
      content::JsReplace("let e = document.createElement('object');"
                         "e.data = $1;"
                         "e.type = 'application/pdf';"
                         "document.body.appendChild(e);",
                         pdf_url)));
  ASSERT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
  EXPECT_EQ(CountPDFProcesses(), 0);

  // Ensure that the fenced frame cannot load a PDF embedding with <embed>.
  ASSERT_TRUE(content::ExecJs(
      fenced_frame_host,
      content::JsReplace("let e = document.createElement('embed');"
                         "e.src = $1;"
                         "e.type = 'application/pdf';"
                         "document.body.appendChild(e);",
                         pdf_url)));
  ASSERT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
  EXPECT_EQ(CountPDFProcesses(), 0);
}

// Exercise a race condition where the profile is destroyed in the middle of a
// PDF navigation and ensure that this doesn't crash.  Specifically,
// `PdfNavigationThrottle` intercepts PDF navigations to PDF stream URLs,
// cancels them, and posts a task to navigate to the original URL instead.
// Triggering profile destruction after this task is posted but before it runs
// has previously led to issues in https://crbug.com/1382761.
// See PDFExtensionOopifTest.PdfNavigationDuringProfileShutdown for the OOPIF
// PDF version.
IN_PROC_BROWSER_TEST_F(PDFExtensionTestWithoutOopifOverride,
                       PdfNavigationDuringProfileShutdown) {
  // Open an Incognito window and navigate it to a page with a PDF embedded in
  // an iframe.
  Browser* incognito = CreateIncognitoBrowser();
  content::WebContents* incognito_contents =
      incognito->tab_strip_model()->GetActiveWebContents();
  incognito_contents->GetController().LoadURL(
      embedded_test_server()->GetURL("/pdf/test-cross-site-iframe.html"),
      content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string());

  // Wait for the MimeHandleView guest to be created.  This should return
  // before the actual PDF navigation in the guest is started.
  GuestViewBase* guest_view =
      GetGuestViewManagerForProfile(incognito->profile())
          ->WaitForSingleGuestViewCreated();
  ASSERT_TRUE(guest_view);

  // Look up the PDF stream URL to which the navigation will take place.
  extensions::MimeHandlerViewGuest* guest =
      extensions::MimeHandlerViewGuest::FromGuestViewBase(guest_view);
  ASSERT_TRUE(guest);
  base::WeakPtr<extensions::StreamContainer> stream = guest->GetStreamWeakPtr();
  EXPECT_TRUE(stream);
  GURL stream_url(stream->stream_url());

  // Use TestNavigationManager to wait for first yield after running
  // DidStartNavigation throttles.  This should be precisely after the
  // navigation to the stream URL gets canceled and the task to start a new
  // navigation to the original URL is scheduled.
  {
    content::TestNavigationManager manager(guest_view->web_contents(),
                                           stream_url);
    ASSERT_TRUE(manager.WaitForFirstYieldAfterDidStartNavigation());
  }

  // Now, close Incognito and destroy its profile.  This is subtle: simply
  // closing the Incognito window and waiting for browser destruction (e.g.,
  // with `ui_test_utils::WaitForBrowserToClose(incognito)`) will trigger
  // asynchronous profile destruction which will allow the PDF task to run
  // before profile destruction is complete, sidestepping the bug in
  // https://crbug.com/1382761.  Instead, use the hard shutdown/restart logic
  // similar to that in `BrowserCloseManager::CloseBrowsers()`, which is used
  // by `chrome::ExitIgnoreUnloadHandlers() and forces the `Browser` and its
  // profile shutdown to complete synchronously, but only on the Incognito
  // Browser object. Note that we can't just use
  // `chrome::ExitIgnoreUnloadHandlers()` here, as that shuts down all Browser
  // objects and the rest of the browser process and appears to be unsupported
  // in tests.
  chrome::CloseWindow(incognito);
  BrowserView* incognito_view = static_cast<BrowserView*>(incognito->window());
  incognito_view->DestroyBrowser();

  // The test succeeds if it doesn't crash when the posted PDF task attempts to
  // run (the task should be canceled/ignored), so wait for this to happen.
  base::RunLoop().RunUntilIdle();
}

// Ensure that extensions do not get multiple bound LocalMainFrames for guest
// views. This is a regression test for crbug.com/1367582.
// Not applicable to OOPIF PDF, since the bug was about iterating over a frame
// twice because of the inner WebContents.
IN_PROC_BROWSER_TEST_F(PDFExtensionTestWithoutOopifOverride,
                       ExtensionsBindingLocalHost) {
  // Load test PDF in first tab.
  const GURL main_url(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));
  auto* primary_main_frame = GetActiveWebContents()->GetPrimaryMainFrame();

  // Verify the PDF has loaded.
  auto* guest_view = GetGuestViewManager()->WaitForSingleGuestViewCreated();
  ASSERT_TRUE(guest_view);
  EXPECT_NE(primary_main_frame, guest_view->GetGuestMainFrame());

  auto* web_contents_observer =
      extensions::ExtensionWebContentsObserver::GetForWebContents(
          GetActiveWebContents());
  primary_main_frame->ForEachRenderFrameHost(
      [web_contents_observer](content::RenderFrameHost* frame_host) {
        web_contents_observer->GetLocalFrame(frame_host);
      });
  auto* guest_view_web_contents_observer =
      extensions::ExtensionWebContentsObserver::GetForWebContents(
          guest_view->web_contents());
  guest_view->GetGuestMainFrame()->ForEachRenderFrameHost(
      [guest_view_web_contents_observer](content::RenderFrameHost* frame_host) {
        guest_view_web_contents_observer->GetLocalFrame(frame_host);
      });

  // Execute some script in each of the frames to ensure the above bindings
  // have been executed in the renderer. They would previously have caused
  // a process crash.
  EXPECT_EQ(1, content::EvalJs(primary_main_frame, "1;").ExtractInt());
  EXPECT_EQ(1, content::EvalJs(guest_view->web_contents(), "1;").ExtractInt());
}

// PDF extension tests for loading the PDF in an incognito browser.
class PDFExtensionIncognitoTest : public PDFExtensionTest {
 protected:
  void SetUpOnMainThread() override {
    PDFExtensionTest::SetUpOnMainThread();
    incognito_browser_ = CreateIncognitoBrowser();
  }

  void TearDownOnMainThread() override {
    incognito_browser_ = nullptr;
    PDFExtensionTest::TearDownOnMainThread();
  }

  Browser* incognito_browser() { return incognito_browser_; }

  content::WebContents* GetIncognitoActiveWebContents() {
    return incognito_browser()->tab_strip_model()->GetActiveWebContents();
  }

 private:
  raw_ptr<Browser> incognito_browser_ = nullptr;
};

// Test that full page PDF viewer successfully loads in incognito.
IN_PROC_BROWSER_TEST_P(PDFExtensionIncognitoTest, IncognitoFullPage) {
  // Load a direct PDF URL full page.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      incognito_browser(), embedded_test_server()->GetURL("/pdf/test.pdf")));

  EXPECT_TRUE(EnsureFullPagePDFHasLoadedWithValidFrameTree(
      GetIncognitoActiveWebContents(), /*allow_multiple_frames=*/false));
}

// Test that an embed-embedded PDF viewer successfully loads in incognito.
IN_PROC_BROWSER_TEST_P(PDFExtensionIncognitoTest, IncognitoEmbed) {
  // Load the HTML containing an embed embedding a PDF.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      incognito_browser(),
      embedded_test_server()->GetURL("/pdf/pdf_embed.html")));

  EXPECT_TRUE(EnsurePDFHasLoadedInFirstChildWithValidFrameTree(
      GetIncognitoActiveWebContents()));
}

// Test that an iframe-embedded PDF viewer successfully loads in incognito.
IN_PROC_BROWSER_TEST_P(PDFExtensionIncognitoTest, IncognitoIframe) {
  if (!UseOopif()) {
    GTEST_SKIP() << "GuestView PDF viewer cannot ensure PDF load in an iframe.";
  }

  // Load the HTML containing an iframe embedding a PDF.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      incognito_browser(),
      embedded_test_server()->GetURL("/pdf/test-iframe.html")));

  // Verify the pdf has loaded. The test will timeout if the PDF fails to
  // load.
  ASSERT_TRUE(GetTestPdfViewerStreamManager(GetIncognitoActiveWebContents())
                  ->WaitUntilPdfLoadedInFirstChild());
}

class PDFExtensionSameSiteProcessTest : public PDFExtensionTest {
 public:
  PDFExtensionSameSiteProcessTest() = default;
  ~PDFExtensionSameSiteProcessTest() override = default;

 protected:
  // Same as `PDFExtensionTestBase::LoadPdfGetExtensionHost()`, but with the PDF
  // content host instead of the PDF extension host.
  content::RenderFrameHost* LoadPdfGetContentHost(const GURL& url) {
    if (!LoadPdf(url)) {
      ADD_FAILURE() << "Failed to load PDF";
      return nullptr;
    }

    return pdf_extension_test_util::GetOnlyPdfPluginFrame(
        GetActiveWebContents());
  }

  // Same as `PDFExtensionTestBase::LoadPdfInNewTabGetExtensionHost()`, but with
  // the PDF content host instead of the PDF extension host.
  content::RenderFrameHost* LoadPdfInNewTabGetContentHost(const GURL& url) {
    if (!LoadPdfInNewTab(url)) {
      ADD_FAILURE() << "Failed to load PDF";
      return nullptr;
    }

    return pdf_extension_test_util::GetOnlyPdfPluginFrame(
        GetActiveWebContents());
  }
};

// Test that multiple tabs containing same-site PDFs don't share a process for
// the PDF content frame when under the process limit.
IN_PROC_BROWSER_TEST_P(PDFExtensionSameSiteProcessTest,
                       SameSitePdfContentFramesInSeparateProcesses) {
  const GURL same_pdf_url = embedded_test_server()->GetURL("/pdf/test.pdf");

  content::RenderFrameHost* content_host1 = LoadPdfGetContentHost(same_pdf_url);
  ASSERT_TRUE(content_host1);

  content::RenderFrameHost* content_host2 =
      LoadPdfInNewTabGetContentHost(same_pdf_url);
  ASSERT_TRUE(content_host2);

  // The content frames should be in separate processes.
  EXPECT_NE(content_host1, content_host2);
  EXPECT_NE(content_host1->GetProcess(), content_host2->GetProcess());
}

// Test that multiple tabs containing same-site PDFs share a process for the PDF
// content frame when at the process limit.
IN_PROC_BROWSER_TEST_P(PDFExtensionSameSiteProcessTest,
                       SameSitePdfContentFramesInSameProcess) {
  // Set the process limit to 1.
  content::RenderProcessHost::SetMaxRendererProcessCount(1);

  const GURL same_pdf_url = embedded_test_server()->GetURL("/pdf/test.pdf");

  content::RenderFrameHost* content_host1 = LoadPdfGetContentHost(same_pdf_url);
  ASSERT_TRUE(content_host1);

  content::RenderFrameHost* content_host2 =
      LoadPdfInNewTabGetContentHost(same_pdf_url);
  ASSERT_TRUE(content_host2);

  // The content frames should be in the same process.
  EXPECT_NE(content_host1, content_host2);
  EXPECT_EQ(content_host1->GetProcess(), content_host2->GetProcess());
}

class PDFExtensionZoomTest : public PDFExtensionTest {
 public:
  PDFExtensionZoomTest() = default;
  ~PDFExtensionZoomTest() override = default;

 protected:
  // Callers must set the default zoom level and load the PDF before calling
  // `TestDefaultZoom()`.
  void TestDefaultZoom(content::RenderFrameHost* extension_host) {
    WebContents* web_contents = GetActiveWebContents();
    auto* main_contents_zoom_controller =
        zoom::ZoomController::FromWebContents(web_contents);
    ASSERT_TRUE(main_contents_zoom_controller);

    // The PDF extension should not use the default zoom level and stay at 0
    // zoom level, since the PDF UI should not be affected by zoom.
    auto* extension_host_zoom_controller =
        UseOopif()
            ? zoom::ZoomController::FromWebContentsAndRenderFrameHost(
                  web_contents, extension_host->GetGlobalId())
            : zoom::ZoomController::FromWebContents(
                  content::WebContents::FromRenderFrameHost(extension_host));
    ASSERT_TRUE(extension_host_zoom_controller);
    EXPECT_NE(main_contents_zoom_controller, extension_host_zoom_controller);

    const double actual_zoom = extension_host_zoom_controller->GetZoomLevel();
    EXPECT_NE(main_contents_zoom_controller->GetZoomLevel(), actual_zoom);
    // A zoom level of 0 corresponds to a zoom factor of 1, or 100%.
    EXPECT_EQ(0, actual_zoom);
  }
};

IN_PROC_BROWSER_TEST_P(PDFExtensionZoomTest, DefaultZoomFullPage) {
  browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(0.5);

  content::RenderFrameHost* extension_host =
      LoadPdfGetExtensionHost(embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  TestDefaultZoom(extension_host);
}

IN_PROC_BROWSER_TEST_P(PDFExtensionZoomTest, DefaultZoomEmbed) {
  browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(0.5);

  content::RenderFrameHost* extension_host =
      LoadPdfInFirstChildGetExtensionHost(
          embedded_test_server()->GetURL("/pdf/pdf_embed.html"));
  ASSERT_TRUE(extension_host);

  TestDefaultZoom(extension_host);
}

// PDF extension tests for the OOPIF PDF viewer.
class PDFExtensionOopifTest : public PDFExtensionTestWithoutOopifOverride {
 public:
  bool UseOopif() const override { return true; }

  pdf::PdfViewerStreamManager* GetPdfViewerStreamManager() {
    return pdf::PdfViewerStreamManager::FromWebContents(GetActiveWebContents());
  }
};

// Test that an embed-embedded PDF can send and receive postMessage() messages
// to and from its embedder.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, OopifPdfPostMessageEmbed) {
  // Load the HTML containing an embed embedding a PDF.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/pdf_embed.html")));

  // `EnsurePDFHasLoaded()` uses postMessage() to check that the PDF has loaded,
  // so calling it is sufficient to check that a postMessage() connection has
  // been established.
  content::RenderFrameHost* embedder_host =
      ChildFrameAt(GetActiveWebContents()->GetPrimaryMainFrame(), 0);
  ASSERT_TRUE(embedder_host);
  ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(embedder_host));
}

// Tests that `FindFullPagePdfExtensionHost` fails to find the PDF extension
// host on a non-PDF page.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest,
                       FindFullPagePdfExtensionHostNonPdf) {
  // Navigate to a non-PDF page.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/title1.html")));

  // Verify that there is no full-page pdf extension host on non-PDF page.
  EXPECT_FALSE(
      pdf_frame_util::FindFullPagePdfExtensionHost(GetActiveWebContents()));
}

// Tests that `FindFullPagePdfExtensionHost` fails to find the PDF extension
// host on an embedded PDF.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest,
                       FindFullPagePdfExtensionHostEmbedPdf) {
  // Load page with embedded PDF and make sure it succeeds.
  ASSERT_TRUE(LoadPdfInFirstChild(
      embedded_test_server()->GetURL("/pdf/pdf_embed.html")));

  // Verify that there is no full-page pdf extension host on embedded PDF.
  EXPECT_FALSE(
      pdf_frame_util::FindFullPagePdfExtensionHost(GetActiveWebContents()));
}

// Tests that `FindFullPagePdfExtensionHost` finds the correct PDF extension
// host on a full-page PDF.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest,
                       FindFullPagePdfExtensionHostFullPagePdf) {
  // Load a full-page PDF and make sure it succeeds.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/test.pdf")));
  auto* web_contents = GetActiveWebContents();
  ASSERT_TRUE(GetTestPdfViewerStreamManager(web_contents)
                  ->WaitUntilPdfLoaded(web_contents->GetPrimaryMainFrame()));

  content::RenderFrameHost* child_frame =
      content::ChildFrameAt(web_contents->GetPrimaryMainFrame(), 0);
  ASSERT_TRUE(child_frame);

  // Verify that `FindFullPagePdfExtensionHost` returns the correct frame.
  EXPECT_EQ(child_frame,
            pdf_frame_util::FindFullPagePdfExtensionHost(web_contents));
}

// Tests that `FindFullPagePdfExtensionHost` finds the correct PDF extension
// host on a full-page PDF loaded with MIME type params.
IN_PROC_BROWSER_TEST_F(
    PDFExtensionOopifTest,
    FindFullPagePdfExtensionHostFullPagePdfWithMimeTypeParam) {
  // Load a full-page PDF with MIME type param and make sure it succeeds.
  const char kPdfFullPageWithMimeTypeParam[] =
      "data:application/pdf;charset=iso-8859-5;base64,"
      "JVBERi0xLjcKJaDypPQKMSAwIG9iaiA8PAogIC9UeXBlIC9DYXRhbG9nCiAgL1BhZ2VzIDIg"
      "MCBSCj4+CmVuZG9iagoyIDAgb2JqIDw8CiAgL1R5cGUgL1BhZ2VzCiAgL0NvdW50IDEKICAv"
      "S2lkcyBbMyAwIFJdCiAgL1Jlc291cmNlcyA8PCA+Pgo+PgplbmRvYmoKMyAwIG9iaiA8PAog"
      "IC9UeXBlIC9QYWdlIAogIC9QYXJlbnQgMiAwIFIKICAvTWVkaWFCb3ggWzAgMCAxMDAgNTBd"
      "Cj4+CmVuZG9iagp4cmVmCjAgNAowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMTUgMDAw"
      "MDAgbiAKMDAwMDAwMDA2OCAwMDAwMCBuIAowMDAwMDAwMTUwIDAwMDAwIG4gCnRyYWlsZXIg"
      "PDwKICAvUm9vdCAxIDAgUgogIC9TaXplIDQKPj4Kc3RhcnR4cmVmCjIyNwolJUVPRgo=";
  EXPECT_TRUE(LoadPdf(GURL(kPdfFullPageWithMimeTypeParam)));

  auto* web_contents = GetActiveWebContents();
  // Validate that the PDF page metadata is set correctly.
  EXPECT_EQ(pdf::kPDFMimeType, web_contents->GetContentsMimeType());
  EXPECT_EQ("ISO-8859-5", web_contents->GetEncoding());

  content::RenderFrameHost* child_frame =
      content::ChildFrameAt(web_contents->GetPrimaryMainFrame(), 0);
  ASSERT_TRUE(child_frame);

  // Verify that `FindFullPagePdfExtensionHost` returns the correct frame,
  // irrespective of MIME type params (Ex: charset in this test).
  EXPECT_EQ(child_frame,
            pdf_frame_util::FindFullPagePdfExtensionHost(web_contents));
}

// Test that re-navigating to the same PDF successfully loads the PDF.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, NavigateToSamePdf) {
  const GURL pdf_url = embedded_test_server()->GetURL("/pdf/test.pdf");
  WebContents* web_contents = GetActiveWebContents();

  // Navigate to the PDF URL.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), pdf_url));

  auto* primary_main_frame1 = web_contents->GetPrimaryMainFrame();
  ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(
      web_contents->GetPrimaryMainFrame()));

  // Make sure the stream has the same URL as the PDF URL.
  ASSERT_TRUE(GetPdfViewerStreamManager());
  base::WeakPtr<extensions::StreamContainer> stream =
      GetPdfViewerStreamManager()->GetStreamContainer(primary_main_frame1);
  ASSERT_TRUE(stream);
  EXPECT_EQ(pdf_url, stream->original_url());

  // Navigate to the same PDF URL.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/pdf/test.pdf")));

  auto* primary_main_frame2 = web_contents->GetPrimaryMainFrame();
  ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(primary_main_frame2));

  // Make sure the stream was replaced by a new stream. The new stream should
  // still have the same URL as the PDF URL.
  ASSERT_TRUE(GetPdfViewerStreamManager());
  EXPECT_FALSE(stream);
  stream = GetPdfViewerStreamManager()->GetStreamContainer(primary_main_frame2);
  ASSERT_TRUE(stream);
  EXPECT_EQ(pdf_url, stream->original_url());
}

// TODO(crbug.com/41495156): Add a test for reloading the same URL with a new
// Content-Security-Policy: sandbox header.

// Test that navigating to a different PDF successfully loads the PDF.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, NavigateToDifferentPdf) {
  const GURL pdf_url = embedded_test_server()->GetURL("/pdf/test.pdf");
  WebContents* web_contents = GetActiveWebContents();

  // Navigate to the PDF URL.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), pdf_url));

  ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(
      web_contents->GetPrimaryMainFrame()));

  // Make sure the stream has the same URL as the PDF URL.
  ASSERT_TRUE(GetPdfViewerStreamManager());
  base::WeakPtr<extensions::StreamContainer> stream =
      GetPdfViewerStreamManager()->GetStreamContainer(
          web_contents->GetPrimaryMainFrame());
  ASSERT_TRUE(stream);
  EXPECT_EQ(pdf_url, stream->original_url());

  const GURL other_pdf_url =
      embedded_test_server()->GetURL("/pdf/test-title.pdf");

  // Navigate to a different PDF URL.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), other_pdf_url));

  ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(
      web_contents->GetPrimaryMainFrame()));

  // Make sure the stream was replaced by a new stream. The new stream should
  // have the new PDF URL.
  ASSERT_TRUE(GetPdfViewerStreamManager());
  EXPECT_FALSE(stream);
  stream = GetPdfViewerStreamManager()->GetStreamContainer(
      web_contents->GetPrimaryMainFrame());
  ASSERT_TRUE(stream);
  EXPECT_EQ(other_pdf_url, stream->original_url());
}

// Test that the inner frames in a full page PDF can't be accessed.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, FailToAccessInnerFramesFullPage) {
  // Load a direct PDF URL full page.
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));

  // Fail to access the inner frames using window.frames and the Document
  // interface.
  EXPECT_EQ(true, content::EvalJs(GetActiveWebContents(),
                                  "window.frames[0] === undefined"));
  EXPECT_EQ(true,
            content::EvalJs(
                GetActiveWebContents(),
                "document.getElementsByTagName('embed')[0] === undefined"));
}

// Test that the inner frames in an embed-embedded PDF can't be accessed.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, FailToAccessInnerFramesEmbed) {
  // Load the HTML containing an embed embedding a PDF.
  ASSERT_TRUE(LoadPdfInFirstChild(
      embedded_test_server()->GetURL("/pdf/pdf_embed.html")));

  EXPECT_EQ(true, content::EvalJs(GetActiveWebContents(),
                                  kNestedWindowFramesUndefinedCheck));
}

// Test that the inner frames in an iframe-embedded PDF can't be accessed.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, FailToAccessInnerFramesIframe) {
  // Load the HTML containing an iframe embedding a PDF.
  ASSERT_TRUE(LoadPdfInFirstChild(
      embedded_test_server()->GetURL("/pdf/test-iframe.html")));

  EXPECT_EQ(true, content::EvalJs(GetActiveWebContents(),
                                  kNestedWindowFramesUndefinedCheck));
}

// Tests that a data URL to a PDF loads successfully.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, LoadDataUrlPdfFullPage) {
  // The data URL to load a simple PDF page.
  const char kDataUrlPdfFullPage[] =
      "data:application/pdf;base64,"
      "JVBERi0xLjcKJaDypPQKMSAwIG9iaiA8PAogIC9UeXBlIC9DYXRhbG9nCiAgL1BhZ2VzIDIg"
      "MCBSCj4+CmVuZG9iagoyIDAgb2JqIDw8CiAgL1R5cGUgL1BhZ2VzCiAgL0NvdW50IDEKICAv"
      "S2lkcyBbMyAwIFJdCiAgL1Jlc291cmNlcyA8PCA+Pgo+PgplbmRvYmoKMyAwIG9iaiA8PAog"
      "IC9UeXBlIC9QYWdlIAogIC9QYXJlbnQgMiAwIFIKICAvTWVkaWFCb3ggWzAgMCAxMDAgNTBd"
      "Cj4+CmVuZG9iagp4cmVmCjAgNAowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMTUgMDAw"
      "MDAgbiAKMDAwMDAwMDA2OCAwMDAwMCBuIAowMDAwMDAwMTUwIDAwMDAwIG4gCnRyYWlsZXIg"
      "PDwKICAvUm9vdCAxIDAgUgogIC9TaXplIDQKPj4Kc3RhcnR4cmVmCjIyNwolJUVPRgo=";
  EXPECT_TRUE(LoadPdf(GURL(kDataUrlPdfFullPage)));
}

// Tests that a data URL to a HTML page embedding a PDF in an embed loads
// successfully.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, LoadDataUrlPdfEmbed) {
  const std::string url =
      embedded_test_server()->GetURL("/pdf/test.pdf").spec();
  const std::string data_url =
      "data:text/html,"
      "<html><body>"
      "<embed type=\"application/pdf\" src=\"" +
      url +
      "\">"
      "</body></html>";
  EXPECT_TRUE(LoadPdfInFirstChild(GURL(data_url)));
}

// Tests that a data URL to a HTML page embedding a PDF in an iframe loads
// successfully.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, LoadDataUrlPdfIframe) {
  const std::string url =
      embedded_test_server()->GetURL("/pdf/test.pdf").spec();
  const std::string data_url =
      "data:text/html,"
      "<html><body>"
      "<iframe type=\"application/pdf\" src=\"" +
      url +
      "\">"
      "</body></html>";
  EXPECT_TRUE(LoadPdfInFirstChild(GURL(data_url)));
}

// If the document.body of the PDF viewer is replaced, there should no longer
// be a PDF stream.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, ReplaceDocumentBody) {
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));
  EXPECT_TRUE(
      pdf::PdfViewerStreamManager::FromWebContents(GetActiveWebContents()));

  // Replace the document.body. The embedder RFH will stay, but the extension
  // and content RFH will be deleted.
  EXPECT_TRUE(
      content::ExecJs(GetActiveWebContents(),
                      "document.body = document.createElement('body');"));

  // The stream should no longer exist.
  EXPECT_FALSE(
      pdf::PdfViewerStreamManager::FromWebContents(GetActiveWebContents()));
}

// If the document.body of the PDF viewer is replaced, any subframes appended
// should be able to navigate.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, ReplaceDocumentBodyWithIframe) {
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));
  WebContents* contents = GetActiveWebContents();
  EXPECT_TRUE(pdf::PdfViewerStreamManager::FromWebContents(contents));

  // Replace the document.body.
  EXPECT_TRUE(content::ExecJs(
      contents,
      "let body = document.createElement('body');"
      "body.innerHTML = '<body><iframe id=\"test_iframe_id\"></iframe></body>';"
      "document.body = body;"));

  // The stream should no longer exist.
  EXPECT_FALSE(pdf::PdfViewerStreamManager::FromWebContents(contents));

  // The iframe should be able to navigate.
  content::TestNavigationObserver navigation_observer(contents);
  EXPECT_TRUE(content::NavigateIframeToURL(
      contents, "test_iframe_id",
      embedded_test_server()->GetURL("/empty.html")));

  EXPECT_TRUE(navigation_observer.last_navigation_succeeded());
}

// Subframes appended to the original document.body should be able to navigate.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, DocumentBodyAppendIframe) {
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));
  WebContents* contents = GetActiveWebContents();

  // Append an iframe to the document.body.
  EXPECT_TRUE(content::ExecJs(contents,
                              "let iframe = document.createElement('iframe');"
                              "iframe.id = 'test_iframe_id';"
                              "document.body.appendChild(iframe);"));

  // The iframe should be able to navigate.
  content::TestNavigationObserver navigation_observer(contents);
  EXPECT_TRUE(content::NavigateIframeToURL(
      contents, "test_iframe_id",
      embedded_test_server()->GetURL("/empty.html")));

  EXPECT_TRUE(navigation_observer.last_navigation_succeeded());
}

// Loading a PDF in a subframe without a corresponding FrameNavigationEntry
// should not cause a crash. See https://crbug.com/358084015 and
// https://crbug.com/40467594.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, SubframePDFMissingFrameEntry) {
  WebContents* contents = GetActiveWebContents();

  // Navigate to a test page, and then navigate same-document.
  const GURL main_url(embedded_test_server()->GetURL("/title1.html"));
  const GURL same_doc_url(embedded_test_server()->GetURL("/title1.html#foo"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));
  content::TestNavigationObserver same_doc_observer(contents);
  EXPECT_TRUE(content::ExecJs(contents, "location.href='#foo';"));
  same_doc_observer.Wait();
  EXPECT_EQ(same_doc_url, contents->GetLastCommittedURL());

  // Append an iframe to the document.body.
  content::TestNavigationObserver iframe_observer(contents);
  EXPECT_TRUE(content::ExecJs(contents,
                              "let iframe = document.createElement('iframe');"
                              "iframe.src = 'title1.html';"
                              "document.body.appendChild(iframe);"));
  iframe_observer.Wait();

  // Go back to the previous same-document entry. There will be no
  // subframe FrameNavigationEntry even though the subframe continues to exist,
  // due to https://crbug.com/40467594.
  content::TestNavigationObserver back_observer(contents);
  contents->GetController().GoBack();
  back_observer.Wait();

  // Loading a PDF in the subframe at this point should not crash.
  content::TestNavigationObserver pdf_observer(contents);
  EXPECT_TRUE(
      content::ExecJs(contents, "frames[0].location.href='/pdf/test.pdf';"));
  pdf_observer.Wait();
  EXPECT_TRUE(EnsurePDFHasLoadedInFirstChildWithValidFrameTree(contents));
}

// Ensure that when the only other PDF instance closes in the middle of another
// PDF's extension frame load, the PDF extension frame can still complete its
// subsequent navigation. See https://crbug.com/1295431.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest,
                       PdfExtensionLoadedWhileOldPdfCloses) {
  const GURL main_url(embedded_test_server()->GetURL("/pdf/test.pdf"));

  // Load a test PDF in the first tab.
  content::RenderFrameHost* extension_host1 = LoadPdfGetExtensionHost(main_url);
  ASSERT_TRUE(extension_host1);
  auto* embedder_host1 = GetActiveWebContents()->GetPrimaryMainFrame();
  EXPECT_NE(embedder_host1, extension_host1);

  // Verify the extension loaded.
  const GURL extension_url(
      "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html");
  EXPECT_EQ(extension_url, extension_host1->GetLastCommittedURL());
  EXPECT_EQ(main_url, embedder_host1->GetLastCommittedURL());

  // Open another tab and navigate it to a same-site non-PDF URL.
  ui_test_utils::TabAddedWaiter add_tab1(browser());
  chrome::NewTab(browser());
  add_tab1.Wait();
  ASSERT_EQ(2, browser()->tab_strip_model()->count());
  WebContents* web_contents2 =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  ASSERT_EQ(web_contents2, GetActiveWebContents());
  const GURL non_pdf_url(embedded_test_server()->GetURL("/title1.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), non_pdf_url));

  // Set up a delay for the PDF extension load.
  CreateTestPdfViewerStreamManager(web_contents2);
  auto* test_pdf_viewer_stream_manager2 =
      GetTestPdfViewerStreamManager(web_contents2);
  test_pdf_viewer_stream_manager2->DelayNextPdfExtensionNavigation();

  // Navigate to the PDF URL. Pause before the PDF extension loads. Navigating
  // with `ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP` only waits for the
  // embedder frame to finish loading, but not the PDF extension frame.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), main_url, WindowOpenDisposition::CURRENT_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  content::RenderFrameHost* embedder_host2 =
      web_contents2->GetPrimaryMainFrame();
  test_pdf_viewer_stream_manager2->WaitUntilPdfExtensionNavigationStarted(
      embedder_host2);

  // Close the first tab, destroying the first PDF while the second PDF is in
  // the middle of initialization. Historically, with GuestView PDF in
  // https://crbug.com/1295431, the extension process exited here and caused a
  // crash when the second PDF resumed.
  ASSERT_EQ(2, browser()->tab_strip_model()->count());
  content::WebContentsDestroyedWatcher destroyed_watcher(
      browser()->tab_strip_model()->GetWebContentsAt(0));
  browser()->tab_strip_model()->CloseWebContentsAt(
      0, TabCloseTypes::CLOSE_USER_GESTURE);
  destroyed_watcher.Wait();
  ASSERT_EQ(1, browser()->tab_strip_model()->count());

  // Resume the PDF load and ensure the second PDF loads without crashing.
  test_pdf_viewer_stream_manager2->ResumePdfExtensionNavigation(embedder_host2);
  ASSERT_TRUE(
      test_pdf_viewer_stream_manager2->WaitUntilPdfLoaded(embedder_host2));

  content::RenderFrameHost* extension_host2 =
      pdf_extension_test_util::GetOnlyPdfExtensionHost(web_contents2);
  ASSERT_TRUE(extension_host2);

  // Verify the extension loaded.
  EXPECT_EQ(extension_url, extension_host2->GetLastCommittedURL());
  EXPECT_EQ(main_url, embedder_host2->GetLastCommittedURL());
}

// Test that the PDF embedder frame can't postMessage() to the PDF content
// frame. OOPIF PDF only, since GuestView PDF's embedder frame doesn't have a
// proxy host to the content frame.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest,
                       BlockPdfEmbedderFramePostMessageToContentFrame) {
  ASSERT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf")));

  auto* web_contents = GetActiveWebContents();
  content::RenderFrameHost* embedder_host = web_contents->GetPrimaryMainFrame();
  content::RenderFrameHost* content_host =
      pdf_extension_test_util::GetOnlyPdfPluginFrame(web_contents);
  ASSERT_TRUE(content_host);

  // The `content::RenderFrameProxyHost` is normally hidden by shadow DOM, but a
  // compromised PDF embedder renderer could try to send a message event to the
  // proxy host. If that occurs, the process hosting the compromised PDF
  // embedder renderer should crash.
  base::HistogramTester histograms;
  content::RenderProcessHostWatcher crash_observer(
      embedder_host->GetProcess(),
      content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);

  blink::TransferableMessage message;
  message.sender_agent_cluster_id = base::UnguessableToken::Create();
  content::SimulateProxyHostPostMessage(embedder_host, content_host,
                                        blink::TransferableMessage());

  crash_observer.Wait();
  histograms.ExpectUniqueSample("Stability.BadMessageTerminated.Content", 319,
                                1);
}

// Exercise a race condition where the profile is destroyed in the middle of a
// PDF navigation and ensure that this doesn't crash.  Specifically,
// `PdfNavigationThrottle` intercepts PDF navigations to PDF stream URLs,
// cancels them, and posts a task to navigate to the original URL instead.
// Triggering profile destruction after this task is posted but before it runs
// has previously led to issues in https://crbug.com/1382761.
// See PDFExtensionTestWithoutOopifOverride.PdfNavigationDuringProfileShutdown
// for the GuestView PDF version.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest,
                       PdfNavigationDuringProfileShutdown) {
  // Open an Incognito window.
  Browser* incognito = CreateIncognitoBrowser();
  content::WebContents* incognito_contents =
      incognito->tab_strip_model()->GetActiveWebContents();

  // Create the `pdf::TestPdfViewerStreamManager` before the PDF navigation,
  // since the test needs to delay the PDF extension URL navigation.
  CreateTestPdfViewerStreamManager(incognito_contents);
  auto* test_pdf_viewer_stream_manager =
      GetTestPdfViewerStreamManager(incognito_contents);
  test_pdf_viewer_stream_manager->DelayNextPdfExtensionNavigation();

  // Navigate the Incognito window to a page with a PDF embedded in an iframe.
  content::TestNavigationObserver navigation_observer(incognito_contents);
  incognito_contents->GetController().LoadURL(
      embedded_test_server()->GetURL("/pdf/test-cross-site-iframe.html"),
      content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string());

  // Wait for the initial set of navigations to the page, the embedded PDF URL,
  // and the PDF extension's about:blank navigation. The PDF extension URL
  // navigation is delayed, so once the about:blank navigation finishes, the
  // test will proceed.
  navigation_observer.Wait();

  content::RenderFrameHost* embedder_host =
      content::ChildFrameAt(incognito_contents, 0);
  ASSERT_TRUE(embedder_host);

  // Look up the PDF stream URL to which the navigation will take place.
  base::WeakPtr<extensions::StreamContainer> stream =
      test_pdf_viewer_stream_manager->GetStreamContainer(embedder_host);
  EXPECT_TRUE(stream);
  GURL stream_url(stream->stream_url());

  // Resume the PDF extension URL navigation.
  test_pdf_viewer_stream_manager->ResumePdfExtensionNavigation(embedder_host);

  // Use TestNavigationManager to wait for first yield after running
  // DidStartNavigation throttles.  This should be precisely after the
  // navigation to the stream URL gets canceled and the task to start a new
  // navigation to the original URL is scheduled.
  {
    content::TestNavigationManager test_navigation_manager(incognito_contents,
                                                           stream_url);
    ASSERT_TRUE(
        test_navigation_manager.WaitForFirstYieldAfterDidStartNavigation());
  }

  // Now, close Incognito and destroy its profile.  This is subtle: simply
  // closing the Incognito window and waiting for browser destruction (e.g.,
  // with `ui_test_utils::WaitForBrowserToClose(incognito)`) will trigger
  // asynchronous profile destruction which will allow the PDF task to run
  // before profile destruction is complete, sidestepping the bug in
  // https://crbug.com/1382761.  Instead, use the hard shutdown/restart logic
  // similar to that in `BrowserCloseManager::CloseBrowsers()`, which is used
  // by `chrome::ExitIgnoreUnloadHandlers() and forces the `Browser` and its
  // profile shutdown to complete synchronously, but only on the Incognito
  // Browser object. Note that we can't just use
  // `chrome::ExitIgnoreUnloadHandlers()` here, as that shuts down all Browser
  // objects and the rest of the browser process and appears to be unsupported
  // in tests.
  chrome::CloseWindow(incognito);

  // The `content::WebContents` needs to be deleted before the browser can be
  // destroyed.
  incognito->tab_strip_model()->DetachAndDeleteWebContentsAt(0);

  BrowserView* incognito_view = static_cast<BrowserView*>(incognito->window());
  incognito_view->DestroyBrowser();

  // The test succeeds if it doesn't crash when the posted PDF task attempts to
  // run (the task should be canceled/ignored), so wait for this to happen.
  base::RunLoop().RunUntilIdle();
}

// Test that the PDF.LoadStatus2 metric is incremented only after the PDF fully
// loads.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest, MetricsPDFLoadStatusPartialLoad) {
  const char kPdfLoadStatusMetric[] = "PDF.LoadStatus2";
  base::HistogramTester histograms;

  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedFullPagePdfWithPdfium, 0);

  auto* web_contents = GetActiveWebContents();
  const GURL main_url(embedded_test_server()->GetURL("/pdf/combobox_form.pdf"));

  // Delay the PDF extension navigation.
  CreateTestPdfViewerStreamManager(web_contents);
  auto* test_pdf_viewer_stream_manager =
      GetTestPdfViewerStreamManager(web_contents);
  test_pdf_viewer_stream_manager->DelayNextPdfExtensionNavigation();

  // Navigate to the PDF URL and wait for the PDF extension navigation to start.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), main_url, WindowOpenDisposition::CURRENT_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  auto* primary_main_frame = web_contents->GetPrimaryMainFrame();
  test_pdf_viewer_stream_manager->WaitUntilPdfExtensionNavigationStarted(
      primary_main_frame);

  // The PDF.LoadStatus2 metric should not be incremented yet.
  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedFullPagePdfWithPdfium, 0);

  // Finish loading the PDF.
  test_pdf_viewer_stream_manager->ResumePdfExtensionNavigation(
      primary_main_frame);
  EXPECT_TRUE(
      test_pdf_viewer_stream_manager->WaitUntilPdfLoaded(primary_main_frame));

  // The PDF.LoadStatus2 metric should be incremented.
  histograms.ExpectBucketCount(kPdfLoadStatusMetric,
                               PDFLoadStatus::kLoadedFullPagePdfWithPdfium, 1);
}

// Test that loading a blob PDF inside an embedder page with strict CSP should
// still have styling.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifTest,
                       EmbedderCSPDoesNotBlockPdfIframeStyling) {
  const GURL main_url(
      embedded_test_server()->GetURL("/pdf/blob_pdf_iframe_csp.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_url));

  content::WebContents* web_contents = GetActiveWebContents();

  // Open the blob PDF in an iframe.
  content::TestNavigationObserver navigation_observer(web_contents);
  ASSERT_TRUE(content::ExecJs(web_contents->GetPrimaryMainFrame(),
                              "openBlobPdfInIframe()"));
  navigation_observer.Wait();
  ASSERT_TRUE(navigation_observer.last_navigation_succeeded());

  content::RenderFrameHost* embedder_host = ChildFrameAt(web_contents, 0);
  ASSERT_TRUE(GetTestPdfViewerStreamManager(web_contents)
                  ->WaitUntilPdfLoaded(embedder_host));

  // The PDF embedder CSS sets the margin to 0px. Without the CSS, the margin
  // would be 8px.
  std::string embedder_margin =
      content::EvalJs(
          embedder_host,
          "window.getComputedStyle(document.body).getPropertyValue('margin')")
          .ExtractString();
  // TODO(crbug.com/343754409): Margin should be 0px.
  EXPECT_EQ("8px", embedder_margin);
}

class PDFExtensionOopifBlockPdfFrameNavigationTest
    : public PDFExtensionOopifTest {
 public:
  // Override PDFExtensionTestBase::SetUpCommandLine() to enable
  // `features::kRenderDocument`, which requires a parameter.
  // TODO(crbug.com/40615943): Remove when RenderDocument reaches the subframe
  // level.
  void SetUpCommandLine(base::CommandLine* command_line) override {
    PDFExtensionTestBase::SetUpCommandLine(command_line);

    feature_list_.InitWithFeaturesAndParameters(
        /*enabled_features=*/{{chrome_pdf::features::kPdfOopif, {}},
                              {features::kRenderDocument,
                               {{"level", "subframe"}}}},
        /*disabled_features=*/{});
  }

  // Test that navigating to `url` fails in the extension host. If `url` is
  // empty, navigates to the same URL.
  void TestBlockNavigationInExtensionHost(const GURL& url) {
    content::RenderFrameHost* extension_host =
        pdf_extension_test_util::GetOnlyPdfExtensionHost(
            GetActiveWebContents());
    ASSERT_TRUE(extension_host);

    content::FrameTreeNodeId frame_tree_node_id =
        extension_host->GetFrameTreeNodeId();
    content::RenderFrameHost* embedder_host = extension_host->GetParent();

    // Navigate to `url`.
    content::TestFrameNavigationObserver extension_nav_observer(extension_host);
    ASSERT_TRUE(content::ExecJs(extension_host,
                                content::JsReplace("location = $1", url)));
    extension_nav_observer.Wait();

    EXPECT_FALSE(extension_nav_observer.last_navigation_succeeded());

    // `extension_host` should be deleted here. As replacement, a new
    // `content::RenderFrameHost` should be created with an error document.
    content::RenderFrameHost* error_host =
        content::ChildFrameAt(embedder_host, 0);
    ASSERT_TRUE(error_host);
    EXPECT_TRUE(error_host->IsErrorDocument());
    EXPECT_EQ(frame_tree_node_id, error_host->GetFrameTreeNodeId());

    // Attempting to navigate the extension host deletes the stream.
    EXPECT_FALSE(GetPdfViewerStreamManager());
  }

  // Test that navigating to `url` fails in the content host.  If `url` is
  // empty, navigates to the same URL.
  void TestBlockNavigationInContentHost(const GURL& url) {
    content::RenderFrameHost* content_host =
        pdf_extension_test_util::GetOnlyPdfPluginFrame(GetActiveWebContents());
    ASSERT_TRUE(content_host);

    content::FrameTreeNodeId frame_tree_node_id =
        content_host->GetFrameTreeNodeId();
    content::RenderFrameHost* extension_host = content_host->GetParent();

    // Navigate to `url`.
    content::TestFrameNavigationObserver content_nav_observer(content_host);
    ASSERT_TRUE(content::ExecJs(content_host,
                                content::JsReplace("location = $1", url)));
    content_nav_observer.Wait();

    EXPECT_FALSE(content_nav_observer.last_navigation_succeeded());

    // `content_host` should be deleted here. As replacement, a new
    // `content::RenderFrameHost` should be created with an error document.
    content::RenderFrameHost* error_host =
        content::ChildFrameAt(extension_host, 0);
    ASSERT_TRUE(error_host);
    EXPECT_TRUE(error_host->IsErrorDocument());
    EXPECT_EQ(frame_tree_node_id, error_host->GetFrameTreeNodeId());

    // Attempting to navigate the content host deletes the stream.
    EXPECT_FALSE(GetPdfViewerStreamManager());
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

// Test that navigations in the inner PDF frames fail if they aren't for PDF
// viewer setup in a full page PDF.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifBlockPdfFrameNavigationTest,
                       NonPdfNavigationFullPage) {
  // Load a direct PDF URL full page.
  const GURL pdf_url = embedded_test_server()->GetURL("/pdf/test.pdf");
  const GURL other_url = embedded_test_server()->GetURL("/simple.html");

  ASSERT_TRUE(LoadPdf(pdf_url));
  TestBlockNavigationInExtensionHost(other_url);

  ASSERT_TRUE(LoadPdf(pdf_url));
  TestBlockNavigationInContentHost(other_url);
}

// Test that navigations in the inner PDF frames fail if they aren't for PDF
// viewer setup in an embed-embedded PDF.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifBlockPdfFrameNavigationTest,
                       NonPdfNavigationEmbed) {
  // Load the HTML containing an embed embedding a PDF.
  const GURL url = embedded_test_server()->GetURL("/pdf/pdf_embed.html");
  const GURL other_url = embedded_test_server()->GetURL("/simple.html");

  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInExtensionHost(other_url);

  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInContentHost(other_url);
}

// Test that navigations in the inner PDF frames fail if they aren't for PDF
// viewer setup in an iframe-embedded PDF.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifBlockPdfFrameNavigationTest,
                       NonPdfNavigationIframe) {
  // Load the HTML containing an iframe embedding a PDF.
  const GURL url = embedded_test_server()->GetURL("/pdf/test-iframe.html");
  const GURL other_url = embedded_test_server()->GetURL("/simple.html");

  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInExtensionHost(other_url);

  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInContentHost(other_url);
}

// Test that after the PDF load in a full page PDF, the PDF extension frame
// cannot re-navigate to the PDF extension URL and the PDF content frame cannot
// re-navigate to the PDF URL.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifBlockPdfFrameNavigationTest,
                       SameUrlNavigationFullPage) {
  // Load a direct PDF URL full page.
  const GURL pdf_url = embedded_test_server()->GetURL("/pdf/test.pdf");

  // Using an empty `GURL` will trigger a navigation to the same URL.
  ASSERT_TRUE(LoadPdf(pdf_url));
  TestBlockNavigationInExtensionHost(GURL());

  ASSERT_TRUE(LoadPdf(pdf_url));
  TestBlockNavigationInContentHost(GURL());
}

// Test that after the PDF load in an embed-embedded PDF, the PDF extension
// frame cannot re-navigate to the PDF extension URL and the PDF content frame
// cannot re-navigate to the PDF URL.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifBlockPdfFrameNavigationTest,
                       SameUrlNavigationEmbed) {
  // Load the HTML containing an embed embedding a PDF.
  const GURL url = embedded_test_server()->GetURL("/pdf/pdf_embed.html");

  // Using an empty `GURL` will trigger a navigation to the same URL.
  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInExtensionHost(GURL());

  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInContentHost(GURL());
}

// Test that after the PDF load in an iframe-embedded PDF, the PDF extension
// frame cannot re-navigate to the PDF extension URL and the PDF content frame
// cannot re-navigate to the PDF URL.
IN_PROC_BROWSER_TEST_F(PDFExtensionOopifBlockPdfFrameNavigationTest,
                       SameUrlNavigationIframe) {
  // Load the HTML containing an iframe embedding a PDF.
  const GURL url = embedded_test_server()->GetURL("/pdf/test-iframe.html");

  // Using an empty `GURL` will trigger a navigation to the same URL.
  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInExtensionHost(GURL());

  ASSERT_TRUE(LoadPdfInFirstChild(url));
  TestBlockNavigationInContentHost(GURL());
}

// Test class that enables the RendererSideContentDecoding feature.
// Used to verify PDF loading behavior, especially with compressed content,
// when this feature is active.
class PDFExtensionTestWithRendererSideContentDecoding
    : public PDFExtensionTest {
 protected:
  std::vector<base::test::FeatureRefAndParams> GetEnabledFeatures()
      const override {
    auto enabled = PDFExtensionTest::GetEnabledFeatures();
    // Enable renderer-side content decoding.
    enabled.push_back({network::features::kRendererSideContentDecoding, {}});
    return enabled;
  }
};

// Verifies that a compressed (gzipped) PDF loads successfully when
// RendererSideContentDecoding is enabled. This tests the fix for
// ensuring plugin-loading payloads aren't mistakenly decoded by the renderer
// due to mishandled content encoding headers (crbug.com/417398061).
IN_PROC_BROWSER_TEST_P(PDFExtensionTestWithRendererSideContentDecoding,
                       CompressedPdf) {
  EXPECT_TRUE(LoadPdf(embedded_test_server()->GetURL("/pdf/test.pdf.gz")));
}

// TODO(crbug.com/40268279): Stop testing both modes after OOPIF PDF viewer
// launches.
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionBlobNavigationTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFPluginDisabledTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionTestWithPartialLoading);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionScrollTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionLinkClickTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionInternalLinkClickTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionSaveTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionSaveWithPolicyTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionClipboardTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionHitTestTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionPrerenderTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionSubmitFormTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(
    PDFExtensionPrerenderAndFencedFrameTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionIncognitoTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionSameSiteProcessTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionZoomTest);
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(
    PDFExtensionTestWithRendererSideContentDecoding);