tauri-runtime-cef 3.0.0-alpha.0

Tauri runtime interface for Chromium Embedded Framework
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
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

#![allow(clippy::arc_with_non_send_sync)]
#![allow(clippy::too_many_arguments)]

use std::{
  collections::HashMap,
  fmt,
  fs::create_dir_all,
  path::PathBuf,
  sync::{
    Arc, Mutex,
    atomic::{AtomicBool, AtomicPtr, AtomicU32, Ordering},
    mpsc::{self, Receiver, Sender},
  },
  time::Duration,
};

use cef::*;
use raw_window_handle::{DisplayHandle, HasDisplayHandle};
use tauri_runtime::{
  DeviceEventFilter, Error, EventLoopProxy, ExitRequestedEventAction, Result, RunEvent, Runtime,
  RuntimeHandle, RuntimeInitArgs, UserEvent,
  dpi::PhysicalPosition,
  monitor::Monitor,
  webview::{DetachedWebview, PendingWebview},
  window::{
    DetachedWindow, DragDropEvent, PendingWindow, RawWindow, WebviewEvent, WindowEvent, WindowId,
  },
};
use tauri_utils::Theme;
use winit::{
  application::ApplicationHandler,
  data_transfer::{DataTransferId, TypeHint},
  event::{StartCause, WindowEvent as WinitWindowEvent},
  event_loop::{
    ActiveEventLoop, DndAction, EventLoop, EventLoopBuilder, EventLoopProxy as WinitEventLoopProxy,
  },
  window::WindowId as WinitWindowId,
};

use crate::DebugEnvironment;
use crate::external_message_pump::CefExternalPump;
use crate::platform::EventLoopExt;
use crate::{
  cef_impl::{client as browser_client, ipc, request_handler},
  macros::wrap_with_args,
  webview::{
    self, AppWebview, CefWebviewAttributes, CefWebviewDispatcher, Webview, WebviewMessage,
    create_webview_detached,
  },
  window::{
    AppWindow, CefWindowDispatcher, WindowMessage, create_window_detached,
    winit_monitor_to_tauri_monitor, winit_theme_to_tauri_theme,
  },
  window_handle::SendRawDisplayHandle,
};
#[cfg(any(
  target_os = "linux",
  target_os = "dragonfly",
  target_os = "freebsd",
  target_os = "netbsd",
  target_os = "openbsd"
))]
use winit::platform::gtk4::EventLoopBuilderExtGtk4;
#[cfg(target_os = "macos")]
use winit::platform::macos::EventLoopBuilderExtMacOS;
#[cfg(windows)]
use winit::platform::windows::EventLoopBuilderExtWindows;

/// Customizes the CEF settings before initialization, see [`Cef::with_settings`].
type SettingsCallback = dyn FnOnce(&mut cef::Settings) + Send + Sync;

/// The `cef` crate used by this runtime, re-exported for convenience.
///
/// # Stability
///
/// The cef crate follows the Chromium Embedded Framework interface and there is
/// no API stability guarantees. The crate will be updated frequently, usually
/// in minor releases when a known breaking change is discovered.
pub use cef;

/// Which key Chromium uses to encrypt the little it stores encrypted.
///
/// Chromium's `os_crypt` layer encrypts **cookies and saved passwords** only. Every
/// other piece of web storage — `localStorage`, IndexedDB, Cache Storage, service worker
/// registrations — is written to the cache directory unencrypted whichever variant you
/// pick here, exactly as it is under wry's WebKitGTK and WebView2 backends.
///
/// The default, [`SecretStorage::Auto`], skips the OS secret store in development builds
/// (`tauri::is_dev()`) and keeps it in release builds. What it skips differs per
/// platform:
///
/// - on macOS, `os_crypt` stores a random key in a shared "Chromium Safe Storage"
///   keychain item whose ACL is bound to the code signature of the process that reads
///   it. Ad-hoc-signed development builds get a new signature on every rebuild, so macOS
///   puts up the keychain password prompt again after every `cargo build`.
/// - on Linux, `os_crypt` asks the D-Bus secret portal, libsecret or KWallet for the
///   key, which pops a keyring-unlock dialog the first time an app runs.
///
/// A release build that has to run where there is no secret store at all — a headless
/// session, a container, a CI image — needs [`SecretStorage::Mock`], because there `Auto`
/// asks for a store that is not there.
///
/// # Security
///
/// The mock keychain (`--use-mock-keychain`) and the Linux `basic` password store do not
/// derive a secret key: they encrypt with a key derived from a **hard-coded constant**
/// compiled into Chromium (`mock_password` and `peanuts` respectively). Both constants
/// are public, so cookies encrypted with them have **no meaningful protection at rest** —
/// anyone who can read the cache directory can decrypt them.
///
/// # Switching modes invalidates stored cookies
///
/// Cookies encrypted with one key cannot be read back with another, and development and
/// release builds share the same default cache directory
/// (`{user cache}/{identifier}/cef`). Moving an app between [`SecretStorage::Mock`] and
/// [`SecretStorage::System`] — including the implicit move [`SecretStorage::Auto`] makes
/// when a dev build is followed by a release build — therefore drops the cookies stored
/// under the previous key, logging users out. Set [`Cef::root_cache_path`] to separate
/// the two if that matters.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SecretStorage {
  /// Skip the OS secret store in development builds (`tauri::is_dev()`) and use it in
  /// release builds: `--use-mock-keychain` on macOS and `--password-store=basic` on
  /// Linux, in development only. Windows is untouched and keeps using DPAPI.
  #[default]
  Auto,
  /// Always encrypt with Chromium's hard-coded constant: `--use-mock-keychain` on macOS,
  /// `--password-store=basic` on Linux. Windows is untouched and keeps using DPAPI.
  ///
  /// Read the security note on [`SecretStorage`] before shipping this in a release
  /// build: the key is a public constant, so the cookie jar is effectively unprotected.
  Mock,
  /// Always use the operating system secret store, on every platform and in every build
  /// profile. Appends no switch at all.
  System,
}

/// What to do with Chromium's process sandbox.
///
/// Defaults to [`SandboxPolicy::Auto`], which keeps the sandbox wherever the runtime can.
///
/// # Windows does not have a sandbox here yet
///
/// **Whatever this policy says, a Windows build currently runs unsandboxed.** CEF wants a
/// sandbox broker pointer that, since Chromium M138, only a binary built with Chromium's
/// own toolchain can create; CEF supplies prebuilt `bootstrap.exe` hosts for that, and
/// they load the application as a DLL, which a Tauri application is not. Given a null
/// broker CEF sets `no_sandbox` itself, so there is no configuration here that changes
/// the outcome — only whether the runtime warns about it ([`Self::Auto`]) or refuses to
/// start ([`Self::Required`]).
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SandboxPolicy {
  /// Keep the sandbox wherever it can be kept, and log a warning naming the reason
  /// wherever it cannot.
  ///
  /// It cannot be kept in two situations: on Windows, always, for the reason above; and
  /// on Linux or BSD when the application runs from an AppImage on a system that has
  /// neither the setuid `chrome-sandbox` helper nor usable unprivileged user namespaces,
  /// where the alternative is not an unsandboxed application but no application at all,
  /// since Chromium aborts with "No usable sandbox!".
  ///
  /// macOS always keeps it.
  #[default]
  Auto,
  /// Never run without a sandbox: fail startup instead.
  ///
  /// Pick this when running unsandboxed is not an acceptable outcome and a hard failure
  /// is preferable. On Linux the user can then install the setuid helper, point
  /// `CHROME_DEVEL_SANDBOX` at one, or re-enable unprivileged user namespaces; on Windows
  /// there is nothing they can do, so this always fails there.
  Required,
  /// Always run without a sandbox, on every platform.
  ///
  /// Every renderer then runs with the full privileges of the user, so a compromised
  /// renderer is a compromised account. Useful for containers and CI images that cannot
  /// provide a sandbox, not for shipped applications.
  Disabled,
}

/// Whether Chromium's DevTools protocol server is reachable, and how.
///
/// This is the server behind `chrome://inspect`, not the DevTools window: it drives the
/// browser from outside the process, so anything that can reach it can read and rewrite
/// every page the application shows.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RemoteDebugging {
  /// No server. The runtime additionally pins Chromium's
  /// `devtools.remote_debugging.allowed` preference off, so a `--remote-debugging-port`
  /// or `--remote-debugging-pipe` that reaches Chromium another way is refused too.
  #[default]
  Disabled,
  /// Listen on a TCP port, as `--remote-debugging-port` does.
  ///
  /// The port must be between 1024 and 65535; CEF ignores anything else. The port number
  /// is also written to `DevToolsActivePort` in the cache directory.
  ///
  /// Chromium accepts a WebSocket connection from `localhost` and from any origin named
  /// in `allowed_origins` (`--remote-allow-origins`); leave that empty unless a browser
  /// page has to attach.
  ///
  /// A listening port is reachable by every process on the machine, and the protocol has
  /// no authentication. Prefer [`Self::Pipe`] where the debugger is a child process.
  Port {
    /// TCP port to listen on.
    port: u16,
    /// Origins allowed to open a WebSocket connection, beyond `localhost`.
    allowed_origins: Vec<String>,
  },
  /// Speak the protocol over inherited file descriptors instead of a socket, as
  /// `--remote-debugging-pipe` does.
  ///
  /// Reachable only by the process that launched this one, so it exposes nothing to the
  /// rest of the machine.
  Pipe,
}

/// Whether this application may open a DevTools window at all.
///
/// Application-wide, and combined with the per-webview `WebviewAttributes::devtools`:
/// either one saying no is a no. Both are enforced on the paths this runtime owns — the
/// context menu entries, the F12 and Ctrl+Shift+I chords, the `IDC_DEV_TOOLS` commands
/// and `Webview::open_devtools`.
///
/// # Why it is not Chromium's own preference
///
/// Chromium has a profile preference for exactly this, `devtools.availability`, and
/// `DevToolsWindow::AllowDevToolsFor` consults it on every path that opens a DevTools
/// window — including the ones this runtime does not own, such as a Chrome-owned popup.
/// This policy deliberately does not reach for it, and the runtime pins it to its
/// default instead.
///
/// CEF gates more than the window on that preference: `SendDevToolsMessage` is refused on
/// a profile carrying `kDisallowed`, and refused *silently* — the send still reports
/// success, and no result or event ever reaches a registered observer. This runtime
/// drives its own startup over the DevTools protocol (the document-start scripts, the
/// per-webview user agent) and holds each webview's first navigation until that round
/// trip answers, so setting the preference leaves every window stuck on a blank
/// placeholder. A webview's `on_dev_tools_protocol` would go silent with it.
///
/// So the DevTools *protocol* stays available whatever this policy says. An application
/// that has to close that path too wants [`RemoteDebugging`], which is what exposes the
/// protocol to anything outside the process.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DevToolsPolicy {
  /// Allow DevTools in a build that could open them anyway — a debug build, or one with
  /// the `devtools` feature — and refuse them otherwise.
  #[default]
  Auto,
  /// Allow DevTools, whatever the build profile.
  ///
  /// Per-webview `WebviewAttributes::devtools` still applies; this only stops the runtime
  /// from refusing DevTools application-wide.
  Allowed,
  /// Refuse DevTools, whatever the build profile.
  Disallowed,
}

/// What to do about a navigation to a server whose TLS certificate does not validate.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CertificateErrorPolicy {
  /// Show Chrome's SSL interstitial, which offers the user a way to proceed anyway.
  ///
  /// This is Chromium's own behaviour and the runtime's default, because an application
  /// that loads third-party content — an OAuth or SSO flow is the standing case — behaves
  /// the way the user's browser would.
  #[default]
  ChromeInterstitial,
  /// Cancel the request. No interstitial, and no way for the user to override.
  ///
  /// The hardened choice for an application that only ever loads origins it controls:
  /// there, a certificate error is either a misconfiguration or an interception, and
  /// neither is something to let a user click through.
  Cancel,
}

/// How Chromium resolves the proxy for every request.
///
/// Written as the Chromium `proxy` preference, the same one the `ProxySettings`
/// enterprise policy sets. `WebviewAttributes::proxy_url` sets the same preference for one
/// webview; whichever is applied last to a given request context wins.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProxyConfig {
  /// Use the operating system's proxy configuration.
  ///
  /// Chromium's default, and on Linux the one that reads `http_proxy` and its siblings
  /// out of the environment.
  #[default]
  System,
  /// Connect directly, ignoring any system proxy.
  Direct,
  /// Discover a proxy through WPAD.
  AutoDetect,
  /// Fetch a proxy auto-config script from `url`.
  PacScript {
    /// URL of the PAC script.
    url: String,
  },
  /// Use a fixed proxy.
  FixedServers {
    /// Proxy server, as `scheme://host:port` — for example `socks5://127.0.0.1:9050`.
    /// A bare `host:port` means HTTP.
    server: String,
    /// Semicolon-delimited hosts that bypass the proxy, as the `--proxy-bypass-list`
    /// switch spells them.
    bypass_list: Option<String>,
  },
}

impl ProxyConfig {
  /// The `proxy` preference value Chromium expects for this configuration.
  fn to_preference(&self) -> serde_json::Value {
    match self {
      Self::System => serde_json::json!({ "mode": "system" }),
      Self::Direct => serde_json::json!({ "mode": "direct" }),
      Self::AutoDetect => serde_json::json!({ "mode": "auto_detect" }),
      Self::PacScript { url } => serde_json::json!({ "mode": "pac_script", "pac_url": url }),
      Self::FixedServers {
        server,
        bypass_list,
      } => {
        let mut value = serde_json::json!({ "mode": "fixed_servers", "server": server });
        if let Some(bypass_list) = bypass_list
          && let Some(object) = value.as_object_mut()
        {
          object.insert(
            "bypass_list".to_string(),
            serde_json::Value::String(bypass_list.clone()),
          );
        }
        value
      }
    }
  }
}

/// When a page may start playing media on its own.
///
/// Applied through Chromium's `--autoplay-policy` switch.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AutoplayPolicy {
  /// Chromium's own default, which on desktop requires the user to have interacted with
  /// the document before audible media plays.
  #[default]
  Default,
  /// Let a page play media without any user interaction.
  ///
  /// What a kiosk, a media player or a signage application wants, and what makes a
  /// hostile page able to make noise on its own.
  NoUserGestureRequired,
  /// Require a gesture on the media element itself.
  UserGestureRequired,
  /// Require the user to have interacted with the document.
  DocumentUserActivationRequired,
}

impl AutoplayPolicy {
  /// The `--autoplay-policy` value, or [`None`] to leave the switch off.
  fn as_switch_value(self) -> Option<&'static str> {
    match self {
      Self::Default => None,
      Self::NoUserGestureRequired => Some("no-user-gesture-required"),
      Self::UserGestureRequired => Some("user-gesture-required"),
      Self::DocumentUserActivationRequired => Some("document-user-activation-required"),
    }
  }
}

/// Which local network interfaces WebRTC may reveal to a page.
///
/// Applied through Chromium's `--webrtc-ip-handling-policy` switch. Chromium's default
/// already hides local IP addresses behind mDNS hostnames, so this only matters for an
/// application that wants to go further.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum WebRtcIpHandling {
  /// Chromium's default.
  #[default]
  Default,
  /// Offer both public and private interfaces, which reveals the machine's LAN address.
  DefaultPublicAndPrivateInterfaces,
  /// Offer only the public interface.
  DefaultPublicInterfaceOnly,
  /// Refuse any UDP that does not go through the configured proxy. The strongest of the
  /// four, and the one most likely to break a call outright.
  DisableNonProxiedUdp,
}

impl WebRtcIpHandling {
  /// The `--webrtc-ip-handling-policy` value, or [`None`] to leave the switch off.
  fn as_switch_value(self) -> Option<&'static str> {
    match self {
      Self::Default => None,
      Self::DefaultPublicAndPrivateInterfaces => Some("default_public_and_private_interfaces"),
      Self::DefaultPublicInterfaceOnly => Some("default_public_interface_only"),
      Self::DisableNonProxiedUdp => Some("disable_non_proxied_udp"),
    }
  }
}

/// Selects and configures the CEF runtime.
///
/// Pass it to `tauri::Builder::runtime` to run the application with CEF:
///
/// ```rust,no_run
/// tauri::Builder::default().runtime(
///   tauri_runtime_cef::Cef::default().command_line_arg("disable-gpu", None::<String>),
/// );
/// ```
#[derive(Default)]
pub struct Cef {
  command_line_args: Vec<(String, Option<String>)>,
  disabled_features: Vec<String>,
  enabled_features: Vec<String>,
  deep_link_schemes: Vec<String>,
  cache_path: Option<PathBuf>,
  api_version: Option<i32>,
  secret_storage: SecretStorage,
  profile_preferences: Vec<(String, serde_json::Value)>,
  global_preferences: Vec<(String, serde_json::Value)>,
  content_settings: Vec<(cef::ContentSettingTypes, cef::ContentSettingValues)>,
  allow_chromium_command_line_args: bool,
  log_file: Option<PathBuf>,
  log_severity: Option<LogSeverity>,
  log_items: Option<LogItems>,
  locale: Option<String>,
  accept_language_list: Option<String>,
  user_agent: Option<String>,
  user_agent_product: Option<String>,
  javascript_flags: Option<String>,
  chrome_policy_id: Option<String>,
  persist_session_cookies: bool,
  remote_debugging: RemoteDebugging,
  devtools: DevToolsPolicy,
  debug_environment: DebugEnvironment,
  certificate_errors: CertificateErrorPolicy,
  sandbox: SandboxPolicy,
  settings_callback: Option<Box<SettingsCallback>>,
}

impl fmt::Debug for Cef {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("Cef")
      .field("command_line_args", &self.command_line_args)
      .field("disabled_features", &self.disabled_features)
      .field("enabled_features", &self.enabled_features)
      .field("deep_link_schemes", &self.deep_link_schemes)
      .field("cache_path", &self.cache_path)
      .field("api_version", &self.api_version)
      .field("secret_storage", &self.secret_storage)
      .field("profile_preferences", &self.profile_preferences)
      .field("global_preferences", &self.global_preferences)
      .field("content_settings", &self.content_settings)
      .field(
        "allow_chromium_command_line_args",
        &self.allow_chromium_command_line_args,
      )
      .field("log_file", &self.log_file)
      .field("log_severity", &self.log_severity)
      .field("log_items", &self.log_items)
      .field("locale", &self.locale)
      .field("accept_language_list", &self.accept_language_list)
      // The user agent can carry an application identifier but nothing secret; the
      // JavaScript flags and policy id are likewise plain configuration.
      .field("user_agent", &self.user_agent)
      .field("user_agent_product", &self.user_agent_product)
      .field("javascript_flags", &self.javascript_flags)
      .field("chrome_policy_id", &self.chrome_policy_id)
      .field("persist_session_cookies", &self.persist_session_cookies)
      .field("remote_debugging", &self.remote_debugging)
      .field("devtools", &self.devtools)
      .field("debug_environment", &self.debug_environment)
      .field("certificate_errors", &self.certificate_errors)
      .field("sandbox", &self.sandbox)
      .field("settings_callback", &self.settings_callback.is_some())
      .finish()
  }
}

impl Cef {
  /// Sets a callback to customize the settings passed to [`cef::initialize`].
  ///
  /// If called more than once, only the last callback is used.
  #[must_use]
  pub fn with_settings<F>(mut self, callback: F) -> Self
  where
    F: FnOnce(&mut cef::Settings) + Send + Sync + 'static,
  {
    self.settings_callback = Some(Box::new(callback));
    self
  }

  /// Appends one command line argument passed to CEF.
  ///
  /// The argument is applied to the **browser process only**. CEF warns that modifying
  /// the command line of a non-browser process "may result in undefined behavior
  /// including crashes", and Chromium already forwards to each child process the
  /// switches it needs.
  #[must_use]
  pub fn command_line_arg<K: Into<String>, V: Into<String>>(
    mut self,
    key: K,
    value: Option<V>,
  ) -> Self {
    self
      .command_line_args
      .push((key.into(), value.map(Into::into)));
    self
  }

  /// Appends a list of command line arguments passed to CEF.
  ///
  /// Like [`Self::command_line_arg`], these are applied to the browser process only.
  #[must_use]
  pub fn command_line_args<K: Into<String>, V: Into<String>>(
    mut self,
    args: impl IntoIterator<Item = (K, Option<V>)>,
  ) -> Self {
    self
      .command_line_args
      .extend(args.into_iter().map(|(k, v)| (k.into(), v.map(Into::into))));
    self
  }

  /// Appends a list of deep link schemes detected by CEF's on_already_running_app_relaunch hook.
  ///
  /// Deep links defined by the core deep-link plugin on the Tauri configuration are automatically added.
  #[must_use]
  pub fn deep_link_schemes<S: Into<String>>(
    mut self,
    schemes: impl IntoIterator<Item = S>,
  ) -> Self {
    self
      .deep_link_schemes
      .extend(schemes.into_iter().map(Into::into));
    self
  }

  /// Directory used for CEF disk cache (`Settings::cache_path`).
  ///
  /// If unspecified, defaults to `{user cache}/{app identifier}/cef`.
  #[must_use]
  pub fn root_cache_path<P: AsRef<std::path::Path>>(mut self, path: P) -> Self {
    self.cache_path = Some(path.as_ref().to_path_buf());
    self
  }

  /// CEF API version this process declares (`cef_api_hash`), defaulting to
  /// `cef::sys::CEF_API_VERSION_LAST`.
  #[must_use]
  pub fn cef_api_version(mut self, version: i32) -> Self {
    self.api_version = Some(version);
    self
  }

  /// Which key Chromium uses to encrypt cookies and saved passwords at rest.
  ///
  /// Defaults to [`SecretStorage::Auto`]: development builds skip the OS secret store —
  /// the macOS keychain prompt is replaced by a mock keychain, and Linux skips the D-Bus
  /// secret portal, libsecret and KWallet — while release builds use it. Windows keeps
  /// using DPAPI throughout.
  ///
  /// Nothing but cookies and saved passwords is affected. The mock keychain and the
  /// Linux `basic` store encrypt with a hard-coded, publicly known constant, so reach
  /// for [`SecretStorage::Mock`] only when a release build has to run where no secret
  /// store exists at all. Switching between key sources makes previously stored cookies
  /// unreadable. See [`SecretStorage`] for the details.
  #[must_use]
  pub fn secret_storage(mut self, storage: SecretStorage) -> Self {
    self.secret_storage = storage;
    self
  }

  /// Sets one boolean Chromium profile preference on every webview's request context.
  ///
  /// Applied after the runtime's own defaults, so it can turn a preference the runtime
  /// disabled back on as well as turn something else off. Calling it twice for the same
  /// preference keeps the last value.
  ///
  /// The runtime disables a handful of Chrome browser features that have no place in an
  /// application webview — the "Save password?" and address and credit-card bubbles
  /// (`credentials_enable_service`, `profile.password_manager_leak_detection`,
  /// `autofill.profile_enabled`, `autofill.credit_card_enabled`), the translate bubble
  /// (`translate.enabled`), and two background requests to Google
  /// (`alternate_error_pages.enabled`, `search.suggest_enabled`). An application that
  /// wants one of them names it here.
  ///
  /// Safe Browsing (`safebrowsing.enabled`) is left on by the runtime; an application
  /// whose webview only ever loads its own content can switch it off here.
  ///
  /// Preference names are Chromium's own, and which ones a given Chrome build registers
  /// as writable varies. A preference this build refuses is logged at debug and skipped.
  ///
  /// ```no_run
  /// # use tauri_runtime_cef::Cef;
  /// Cef::default()
  ///   .profile_preference("credentials_enable_service", true)
  ///   .profile_preference("safebrowsing.enabled", false);
  /// ```
  #[must_use]
  pub fn profile_preference<K: Into<String>>(mut self, name: K, enabled: bool) -> Self {
    self
      .profile_preferences
      .push((name.into(), serde_json::Value::Bool(enabled)));
    self
  }

  /// Sets one Chromium profile preference of any type on every webview's request context.
  ///
  /// [`Self::profile_preference`] covers the common boolean case; this one takes the
  /// integers, strings, lists and dictionaries the rest of Chromium's preferences are made
  /// of. The value must have the type Chromium registered the preference with — a string
  /// where an integer belongs is refused and logged at debug.
  ///
  /// Preferences worth knowing about, beyond the ones with a typed option of their own:
  ///
  /// | Preference | Type | What it does |
  /// |---|---|---|
  /// | `printing.enabled` | bool | whether `window.print()` opens Chrome's print preview |
  /// | `download.default_directory` | string | where downloads land |
  /// | `download.prompt_for_download` | bool | whether every download asks first |
  /// | `enable_do_not_track` | bool | sends the `DNT` header |
  /// | `enable_referrers` | bool | sends `Referer` at all |
  /// | `dns_over_https.mode` | string | `off`, `automatic` or `secure` |
  /// | `dns_over_https.templates` | string | space-delimited DoH server templates |
  /// | `profile.cookie_controls_mode` | int | `1` blocks third-party cookies |
  /// | `hardware.audio_capture_enabled` | bool | a hard kill switch for the microphone |
  /// | `hardware.video_capture_enabled` | bool | the same for the camera |
  ///
  /// ```no_run
  /// # use tauri_runtime_cef::Cef;
  /// Cef::default()
  ///   .profile_preference_value("dns_over_https.mode", "secure")
  ///   .profile_preference_value("profile.cookie_controls_mode", 1);
  /// ```
  #[must_use]
  pub fn profile_preference_value<K: Into<String>, V: Into<serde_json::Value>>(
    mut self,
    name: K,
    value: V,
  ) -> Self {
    self.profile_preferences.push((name.into(), value.into()));
    self
  }

  /// Sets one preference in Chromium's local state, the store shared by every profile.
  ///
  /// Most preferences belong to a profile and are set with
  /// [`Self::profile_preference_value`]; a handful — `devtools.remote_debugging.allowed`
  /// and `hardware_acceleration_mode.enabled` among them — live here instead. Applied once
  /// the CEF context is initialized.
  #[must_use]
  pub fn global_preference<K: Into<String>, V: Into<serde_json::Value>>(
    mut self,
    name: K,
    value: V,
  ) -> Self {
    self.global_preferences.push((name.into(), value.into()));
    self
  }

  /// Sets the default value of one Chromium content setting for every origin.
  ///
  /// A content setting is the stored answer behind a permission: with a default of
  /// [`ContentSettingValues::BLOCK`](cef::ContentSettingValues::BLOCK) a page cannot ask
  /// at all, and with [`ALLOW`](cef::ContentSettingValues::ALLOW) it is granted without a
  /// prompt. That makes this the application-wide policy that
  /// `WebviewAttributes::on_permission_request` is not: the handler answers one request at
  /// a time, this decides what can be requested.
  ///
  /// Applied to every webview's request context after it initializes, and stored in the
  /// profile, so it also governs the origins the user has already answered for.
  ///
  /// Chromium refuses to write a default onto an off-the-record profile, so this has no
  /// effect on a webview built with `WebviewAttributes::incognito`. Such a webview keeps
  /// Chromium's own defaults, and `WebviewAttributes::on_permission_request` is the way to
  /// answer for it.
  ///
  /// ```no_run
  /// # use tauri_runtime_cef::Cef;
  /// use tauri_runtime_cef::cef::{ContentSettingTypes, ContentSettingValues};
  ///
  /// Cef::default()
  ///   // An application webview has no business asking for these.
  ///   .default_content_setting(ContentSettingTypes::NOTIFICATIONS, ContentSettingValues::BLOCK)
  ///   .default_content_setting(ContentSettingTypes::GEOLOCATION, ContentSettingValues::BLOCK)
  ///   // Device access a desktop application rarely wants a page to reach.
  ///   .default_content_setting(ContentSettingTypes::USB_GUARD, ContentSettingValues::BLOCK)
  ///   .default_content_setting(ContentSettingTypes::SERIAL_GUARD, ContentSettingValues::BLOCK)
  ///   .default_content_setting(ContentSettingTypes::HID_GUARD, ContentSettingValues::BLOCK);
  /// ```
  ///
  /// Blocking [`JAVASCRIPT_JIT`](cef::ContentSettingTypes::JAVASCRIPT_JIT) is worth
  /// knowing about separately: it runs V8 without its optimizing compilers, which removes
  /// the largest single source of exploitable memory bugs in a renderer at a real cost in
  /// JavaScript performance. It is the same lever as Chrome's `DefaultJavaScriptJitSetting`
  /// policy.
  #[must_use]
  pub fn default_content_setting(
    mut self,
    content_type: cef::ContentSettingTypes,
    value: cef::ContentSettingValues,
  ) -> Self {
    self.content_settings.push((content_type, value));
    self
  }

  /// Lets Chromium read switches off the process command line in release builds.
  ///
  /// Release builds ignore them by default (`Settings::command_line_args_disabled`),
  /// because otherwise anyone who can start the shipped executable can also start it
  /// with `--remote-debugging-port` and drive the app over the DevTools protocol, or
  /// with `--disable-web-security`, `--proxy-server`, `--host-resolver-rules` or
  /// `--ssl-key-log-file` — Chromium honours every one of them. Development builds
  /// (`tauri::is_dev()`) always keep the command line enabled.
  ///
  /// Enable this only if the application genuinely needs users to pass Chromium
  /// switches. It is not a complete lockdown either way: the network service reads the
  /// `SSLKEYLOGFILE` environment variable regardless of this setting.
  ///
  /// Switches configured through [`Self::command_line_arg`] are unaffected, because CEF
  /// clears Chromium's command line before applying its own settings and before calling
  /// `on_before_command_line_processing`. Tauri's own CLI parsing and its cold-start deep
  /// link handling read `std::env::args()`, which Chromium never touches, and are
  /// unaffected too. Deep links delivered to an *already running* instance do go through
  /// Chromium's process singleton, so the runtime restores the deep link URL onto the
  /// cleared command line to keep them working.
  #[must_use]
  pub fn allow_chromium_command_line_args(mut self, allow: bool) -> Self {
    self.allow_chromium_command_line_args = allow;
    self
  }

  /// File Chromium and CEF write their log to (`Settings::log_file`).
  ///
  /// Defaults to `cef.log` inside the cache directory (see [`Self::root_cache_path`]).
  /// With no log file configured, CEF writes a `debug.log` into the *main executable
  /// directory* on Windows and Linux, which for an installed application is often not
  /// even writable.
  ///
  /// The default also overrides the macOS convention of
  /// `~/Library/Logs/<app name>_debug.log`; pass that path explicitly to keep it.
  #[must_use]
  pub fn log_file<P: AsRef<std::path::Path>>(mut self, path: P) -> Self {
    self.log_file = Some(path.as_ref().to_path_buf());
    self
  }

  /// Lowest severity Chromium and CEF write to the log file (`Settings::log_severity`).
  ///
  /// Defaults to [`cef::LogSeverity::WARNING`] in release builds — CEF's own default is
  /// `INFO`, which is chatty enough to grow the log file of a long-running application —
  /// and to [`cef::LogSeverity::DEFAULT`] in development builds (`tauri::is_dev()`),
  /// where the informational messages are usually what you want.
  ///
  /// [`cef::LogSeverity::DISABLE`] does not turn logging off entirely: CEF maps it to a
  /// FATAL-only minimum level, so nothing is written to the log file but FATAL messages
  /// still go to stderr.
  #[must_use]
  pub fn log_severity(mut self, severity: LogSeverity) -> Self {
    self.log_severity = Some(severity);
    self
  }

  /// Locale Chromium loads its own localized resources for (`Settings::locale`),
  /// as an ISO language code such as `en-US` or `pt-BR`.
  ///
  /// Leave unset — the default — unless you know the matching pak file ships with the
  /// application. Tauri's bundler packages **only the `en-US` locale pak**, so naming any
  /// other locale leaves Chromium unable to load the localized strings it uses for its
  /// own UI (context menus, error pages, form controls). This does not affect the
  /// application's own content, nor which languages a website is asked for — that is
  /// [`Self::accept_language_list`].
  #[must_use]
  pub fn locale<S: Into<String>>(mut self, locale: S) -> Self {
    self.locale = Some(locale.into());
    self
  }

  /// Comma-delimited list of languages sent as the `Accept-Language` header and reported
  /// through `navigator.language` (`Settings::accept_language_list`), for example
  /// `en-US,en,pt-BR`.
  ///
  /// Defaults to CEF's own value, which is derived from [`Self::locale`].
  #[must_use]
  pub fn accept_language_list<S: Into<String>>(mut self, languages: S) -> Self {
    self.accept_language_list = Some(languages.into());
    self
  }

  /// What to do with Chromium's process sandbox.
  ///
  /// Defaults to [`SandboxPolicy::Auto`], which keeps the sandbox except when the
  /// application runs from an AppImage on a Linux or BSD system that offers no way to
  /// sandbox at all — AppImages cannot ship the setuid `chrome-sandbox` helper the deb
  /// and rpm bundlers install, and distributions such as Ubuntu 23.10 and later restrict
  /// the unprivileged user namespaces Chromium would otherwise fall back to. Without the
  /// escape hatch Chromium aborts at startup with "No usable sandbox!".
  ///
  /// See [`SandboxPolicy`] for the other variants.
  #[must_use]
  pub fn sandbox(mut self, policy: SandboxPolicy) -> Self {
    self.sandbox = policy;
    self
  }

  /// Adds names to Chromium's `--disable-features` list, keeping what is already there.
  ///
  /// Use this rather than `command_line_arg("disable-features", ...)`. Chromium stores a
  /// switch by name and the last value appended replaces the previous one, so a raw
  /// `--disable-features` does not add to the list — it *becomes* the list, dropping the
  /// entries CEF put there to keep Chrome from crashing at startup and to keep renderers
  /// from being killed on the runtime's own requests.
  #[must_use]
  pub fn disable_features<S: Into<String>>(
    mut self,
    features: impl IntoIterator<Item = S>,
  ) -> Self {
    self
      .disabled_features
      .extend(features.into_iter().map(Into::into));
    self
  }

  /// Adds names to Chromium's `--enable-features` list, keeping what is already there.
  ///
  /// See [`Self::disable_features`] for why the raw switch is the wrong tool.
  #[must_use]
  pub fn enable_features<S: Into<String>>(mut self, features: impl IntoIterator<Item = S>) -> Self {
    self
      .enabled_features
      .extend(features.into_iter().map(Into::into));
    self
  }

  /// Whether Chromium and CEF may read their diagnostic environment variables.
  ///
  /// Chromium honours `SSLKEYLOGFILE` — which writes the keys that decrypt every TLS
  /// session the application makes — and CEF honours three variables that redirect crash
  /// reports, whose minidumps carry process memory. Neither group is reachable through a
  /// CEF setting, so each is refused where Chromium reads it: the key log through an
  /// empty `--ssl-key-log-file`, which Chromium consults ahead of the variable, and the
  /// crash overrides by taking them out of the environment before CEF starts.
  ///
  /// Defaults to [`DebugEnvironment::Auto`]: honoured in development builds
  /// (`tauri::is_dev()`), refused in release builds.
  #[must_use]
  pub fn debug_environment(mut self, policy: DebugEnvironment) -> Self {
    self.debug_environment = policy;
    self
  }

  /// Whether Chromium's DevTools protocol server runs, and how it is reached.
  ///
  /// Defaults to [`RemoteDebugging::Disabled`], which additionally pins Chromium's
  /// `devtools.remote_debugging.allowed` preference off so the server is refused even if
  /// the switch reaches Chromium another way.
  ///
  /// See [`RemoteDebugging`] for what each transport exposes.
  #[must_use]
  pub fn remote_debugging(mut self, remote_debugging: RemoteDebugging) -> Self {
    self.remote_debugging = remote_debugging;
    self
  }

  /// Whether this application may open a DevTools window at all.
  ///
  /// Defaults to [`DevToolsPolicy::Auto`], which refuses them in a build that could not
  /// open them anyway. See [`DevToolsPolicy`] for how this combines with the per-webview
  /// `WebviewAttributes::devtools`, and for why it leaves the DevTools protocol alone.
  #[must_use]
  pub fn devtools(mut self, policy: DevToolsPolicy) -> Self {
    self.devtools = policy;
    self
  }

  /// What to do about a navigation to a server whose TLS certificate does not validate.
  ///
  /// Defaults to [`CertificateErrorPolicy::ChromeInterstitial`], which is Chromium's own
  /// behaviour: an interstitial the user can click through.
  #[must_use]
  pub fn certificate_errors(mut self, policy: CertificateErrorPolicy) -> Self {
    self.certificate_errors = policy;
    self
  }

  /// How Chromium resolves the proxy for every request.
  ///
  /// Defaults to [`ProxyConfig::System`], Chromium's own behaviour.
  #[must_use]
  pub fn proxy(mut self, proxy: ProxyConfig) -> Self {
    self
      .profile_preferences
      .push(("proxy".to_string(), proxy.to_preference()));
    self
  }

  /// When a page may start playing media on its own.
  #[must_use]
  pub fn autoplay(mut self, policy: AutoplayPolicy) -> Self {
    if let Some(value) = policy.as_switch_value() {
      self
        .command_line_args
        .push(("--autoplay-policy".to_string(), Some(value.to_string())));
    }
    self
  }

  /// Which local network interfaces WebRTC may reveal to a page.
  #[must_use]
  pub fn webrtc_ip_handling(mut self, policy: WebRtcIpHandling) -> Self {
    if let Some(value) = policy.as_switch_value() {
      self.command_line_args.push((
        "--webrtc-ip-handling-policy".to_string(),
        Some(value.to_string()),
      ));
    }
    self
  }

  /// Whether Chromium's spell checker runs.
  ///
  /// On by default. The first use of a language downloads its dictionary from Google's
  /// `redirector.gvt1.com`, which is the only reason an application that never shows an
  /// editable field might want it off. The remote spelling *service*, which would send
  /// typed text to Google, is off either way.
  #[must_use]
  pub fn spell_checking(mut self, enabled: bool) -> Self {
    self.profile_preferences.push((
      "browser.enable_spellchecking".to_string(),
      serde_json::Value::Bool(enabled),
    ));
    self
  }

  /// Whether Chromium's Safe Browsing protection runs.
  ///
  /// On by default, and worth keeping on for any webview that loads content the
  /// application does not control — an OAuth or SSO flow, an embedded third-party page.
  /// Standard protection checks a locally stored hash-prefix database rather than calling
  /// Google per navigation, and keeping it updated is the periodic request an application
  /// that only ever loads its own content might want to be rid of.
  #[must_use]
  pub fn safe_browsing(mut self, enabled: bool) -> Self {
    self.profile_preferences.push((
      "safebrowsing.enabled".to_string(),
      serde_json::Value::Bool(enabled),
    ));
    self
  }

  /// Whether Chromium's component updater runs.
  ///
  /// On by default, and it is the mechanism that keeps the certificate revocation set,
  /// the Certificate Transparency log list and the download file-type policies current —
  /// security data that goes stale. Turn it off only for a deployment that has no route
  /// to `update.googleapis.com` at all.
  #[must_use]
  pub fn component_updates(mut self, enabled: bool) -> Self {
    if !enabled {
      self
        .command_line_args
        .push(("--disable-component-update".to_string(), None));
    }
    self
  }

  /// Value returned as the `User-Agent` header and `navigator.userAgent`, for every
  /// webview in the process (`CefSettings.user_agent`).
  ///
  /// Replacing the whole string drops the Chrome and platform tokens sites branch on, so
  /// prefer [`Self::user_agent_product`], which keeps them. A single webview can override
  /// this through `WebviewAttributes::user_agent`.
  #[must_use]
  pub fn user_agent<S: Into<String>>(mut self, user_agent: S) -> Self {
    self.user_agent = Some(user_agent.into());
    self
  }

  /// Product token spliced into Chromium's own User-Agent string, such as `MyApp/1.2.0`
  /// (`CefSettings.user_agent_product`).
  ///
  /// Ignored when [`Self::user_agent`] is set.
  #[must_use]
  pub fn user_agent_product<S: Into<String>>(mut self, product: S) -> Self {
    self.user_agent_product = Some(product.into());
    self
  }

  /// Whether session cookies survive a restart (`CefSettings.persist_session_cookies`).
  ///
  /// Off by default, matching a browser: a session cookie is dropped when the application
  /// exits. A desktop application that should keep users signed in across restarts wants
  /// this on, and should know that it writes those cookies to the cache directory, where
  /// they are only as protected as [`SecretStorage`] makes them.
  #[must_use]
  pub fn persist_session_cookies(mut self, persist: bool) -> Self {
    self.persist_session_cookies = persist;
    self
  }

  /// Flags passed to V8 (`CefSettings.javascript_flags`), such as
  /// `--max-old-space-size=512`.
  ///
  /// Use this rather than `command_line_arg("js-flags", ...)`, which replaces the value
  /// CEF derives from this setting instead of adding to it.
  #[must_use]
  pub fn javascript_flags<S: Into<String>>(mut self, flags: S) -> Self {
    self.javascript_flags = Some(flags.into());
    self
  }

  /// Enables Chrome policy management, reading policies from the platform location this
  /// identifier names (`CefSettings.chrome_policy_id`).
  ///
  /// The identifier is a registry key on Windows (`SOFTWARE\\Policies\\Vendor\\App`), a
  /// bundle identifier on macOS, and a directory on Linux (`/etc/opt/vendor/app/policies`).
  /// Set it for an application deployed by an IT department that has to configure it
  /// centrally; leave it unset otherwise, since it lets whoever controls that location
  /// change the application's behaviour.
  #[must_use]
  pub fn chrome_policy_id<S: Into<String>>(mut self, policy_id: S) -> Self {
    self.chrome_policy_id = Some(policy_id.into());
    self
  }

  /// Which fields CEF prepends to each line of the log file (`CefSettings.log_items`).
  ///
  /// Defaults to CEF's own choice.
  #[must_use]
  pub fn log_items(mut self, items: LogItems) -> Self {
    self.log_items = Some(items);
    self
  }
}

impl<T: UserEvent> tauri_runtime::RuntimeInitAttrs<T> for Cef {
  type Runtime = CefRuntime<T>;

  fn apply_config(&mut self, config: &tauri_utils::config::Config) -> Result<()> {
    if let Some(plugin_config) = config
      .plugins
      .0
      .get("deep-link")
      .and_then(|config| config.get("desktop").cloned())
    {
      #[derive(serde::Deserialize)]
      #[serde(untagged)]
      enum DesktopDeepLinks {
        One(tauri_utils::config::DeepLinkProtocol),
        List(Vec<tauri_utils::config::DeepLinkProtocol>),
      }

      let protocols: DesktopDeepLinks =
        serde_json::from_value(plugin_config).map_err(tauri_runtime::Error::Json)?;
      let schemes = match protocols {
        DesktopDeepLinks::One(protocol) => protocol.schemes,
        DesktopDeepLinks::List(protocols) => protocols
          .into_iter()
          .flat_map(|protocol| protocol.schemes)
          .collect(),
      };

      self.deep_link_schemes.extend(schemes);
    }
    Ok(())
  }
}

impl<T: UserEvent> From<Cef> for tauri_runtime::dynamic::DynRuntimeInitAttrs<T> {
  fn from(attrs: Cef) -> Self {
    Self::new(attrs)
  }
}

/// Information about the CEF webview that requested a new window.
pub struct NewWindowOpener {
  source_url: Option<url::Url>,
}

impl NewWindowOpener {
  pub(crate) fn new(source_url: Option<url::Url>) -> Self {
    Self { source_url }
  }

  /// The opener's main-frame URL at the native popup request, when available.
  ///
  /// CEF supplies this directly from the callback's browser. Reading a blocking
  /// webview getter from that callback can deadlock the UI thread because CEF's
  /// external message pump may run outside a winit dispatch callback.
  pub fn source_url(&self) -> Option<&url::Url> {
    self.source_url.as_ref()
  }
}

impl std::fmt::Debug for NewWindowOpener {
  fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    // The URL can carry credentials and tokens, so only its presence is shown.
    formatter
      .debug_struct("NewWindowOpener")
      .field("source_url_observed", &self.source_url.is_some())
      .finish()
  }
}

#[derive(Clone, Debug)]
pub struct EventProxy<T: UserEvent> {
  context: RuntimeContext<T>,
}

impl<T: UserEvent> EventLoopProxy<T> for EventProxy<T> {
  fn send_event(&self, event: T) -> Result<()> {
    self.context.send_message(Message::UserEvent(event))
  }
}

#[derive(Clone)]
pub(crate) struct RuntimeContext<T: UserEvent> {
  pub(crate) sender: Sender<Message<T>>,
  pub(crate) proxy: WinitEventLoopProxy,
  main_thread_id: std::thread::ThreadId,
  next_window_id: Arc<AtomicU32>,
  next_webview_id: Arc<AtomicU32>,
  next_window_event_id: Arc<AtomicU32>,
  next_webview_event_id: Arc<AtomicU32>,
  current_dispatch: Arc<MainThreadDispatchSlot<T>>,
  pub(crate) app_wide_theme: Arc<Mutex<Option<Theme>>>,
  pub(crate) cef_pump: CefExternalPump,
  /// Root cache path passed to [`cef::Settings::cache_path`] during
  /// [`cef::initialize`]. Per-webview `data_directory` profiles must resolve
  /// under this root for CEF request contexts to be accepted.
  pub(crate) cache_path: Arc<PathBuf>,
  /// Chromium profile preferences the application asked for, applied to every
  /// webview's request context after the runtime's own defaults. See
  /// [`Cef::profile_preference`].
  pub(crate) profile_preferences: Arc<Vec<(String, serde_json::Value)>>,
  /// Default content settings the application asked for, applied to every webview's
  /// request context. See [`Cef::default_content_setting`].
  pub(crate) content_settings: Arc<Vec<(cef::ContentSettingTypes, cef::ContentSettingValues)>>,
  /// What to do about a navigation whose TLS certificate does not validate. See
  /// [`Cef::certificate_errors`].
  pub(crate) certificate_errors: CertificateErrorPolicy,
  /// Whether [`Cef::devtools`] lets this application open DevTools at all. Combined with
  /// the per-webview `WebviewAttributes::devtools`, which can only narrow it further.
  pub(crate) devtools_allowed: bool,
}

/// Scoped access to the current winit callback state.
///
/// `ActiveEventLoop` is only borrowed during `ApplicationHandler` callbacks, but
/// setup-time runtime messages may synchronously need it. While a callback is
/// active, this slot lets main-thread `send_message` handle work immediately;
/// other threads still queue and wake the loop. The slot stores an atomic
/// pointer to guard-owned state so lookup is lock-free. The guard restores the
/// previous pointer before dropping that state, so the raw pointers are never
/// treated as valid beyond their callback.
#[derive(Clone, Copy)]
struct MainThreadDispatch<T: UserEvent> {
  app: *mut WinitCefApp<T>,
  event_loop: *const dyn ActiveEventLoop,
}

struct MainThreadDispatchSlot<T: UserEvent> {
  current: AtomicPtr<MainThreadDispatch<T>>,
}

impl<T: UserEvent> MainThreadDispatchSlot<T> {
  fn install(&self, dispatch: &mut MainThreadDispatch<T>) -> *mut MainThreadDispatch<T> {
    self.current.swap(dispatch, Ordering::AcqRel)
  }

  fn restore(&self, current: *mut MainThreadDispatch<T>, previous: *mut MainThreadDispatch<T>) {
    let installed = self.current.swap(previous, Ordering::AcqRel);
    debug_assert_eq!(installed, current);
  }

  fn current(&self) -> Option<&MainThreadDispatch<T>> {
    let current = self.current.load(Ordering::Acquire);
    if current.is_null() {
      None
    } else {
      // SAFETY: the pointer targets the boxed dispatch state owned by
      // `MainThreadDispatchGuard`, whose allocation remains stable while the
      // guard is moved. The slot is restored before that state is dropped, and
      // it is only read by `send_message` after verifying that it is running on
      // the runtime main thread.
      Some(unsafe { &*current })
    }
  }
}

impl<T: UserEvent> Default for MainThreadDispatchSlot<T> {
  fn default() -> Self {
    Self {
      current: AtomicPtr::new(std::ptr::null_mut()),
    }
  }
}

struct MainThreadDispatchGuard<T: UserEvent> {
  context: RuntimeContext<T>,
  dispatch: Box<MainThreadDispatch<T>>,
  previous: *mut MainThreadDispatch<T>,
}

impl<T: UserEvent> Drop for MainThreadDispatchGuard<T> {
  fn drop(&mut self) {
    self
      .context
      .current_dispatch
      .restore(self.dispatch.as_mut(), self.previous);
  }
}

/// Whether handling `message` ends in the application's `RunEvent` callback.
///
/// Those messages must never be handled inline by [`handle_main_thread_message`]: see
/// the deadlock it documents.
fn dispatches_run_event<T: UserEvent>(message: &Message<T>) -> bool {
  match message {
    Message::UserEvent(_) | Message::Opened(_) => true,
    #[cfg(target_os = "macos")]
    Message::Reopen { .. } => true,
    _ => false,
  }
}

#[allow(clippy::result_large_err)]
fn handle_main_thread_message<T: UserEvent>(
  context: &RuntimeContext<T>,
  message: Message<T>,
) -> std::result::Result<(), Message<T>> {
  // A message that reaches the application's run callback goes through the event loop
  // even on the main thread, because the main thread may be several frames deep inside
  // a callback already — anything that spins a nested loop, such as muda's GTK4 context
  // menu, gets here from the closure it is blocking on. Running the callback from there
  // re-enters the application while it holds locks meant for one pass of the loop:
  // `Window::popup_menu` reaches the runtime through a plugin command, which holds
  // Tauri's plugin store lock across the popup and blocks a worker on the main thread,
  // while `on_event_loop_event` takes that same lock to deliver the menu event —
  // deadlocking both threads. Queueing keeps the callback where the loop can only be in
  // one pass at a time, and matches what the wry runtime's proxy does.
  if dispatches_run_event(&message) {
    return Err(message);
  }

  let Some(dispatch) = context.current_dispatch.current() else {
    return Err(message);
  };

  // SAFETY: `WinitCefApp::install_current_dispatch` stores pointers to the currently
  // executing winit application handler and event-loop callback. This function
  // is only called on the runtime main thread while that callback is active.
  let app = unsafe { &mut *dispatch.app };
  let event_loop = unsafe { &*dispatch.event_loop };

  app.handle_message(event_loop, message);

  Ok(())
}

impl<T: UserEvent> fmt::Debug for RuntimeContext<T> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("RuntimeContext").finish()
  }
}

impl<T: UserEvent> RuntimeContext<T> {
  pub(crate) fn send_message(&self, message: Message<T>) -> Result<()> {
    let message = if self.is_main_thread() {
      match handle_main_thread_message(self, message) {
        Ok(()) => return Ok(()),
        Err(message) => message,
      }
    } else {
      message
    };

    self
      .sender
      .send(message)
      .map_err(|_| Error::FailedToSendMessage)?;
    self.proxy.wake_up();
    Ok(())
  }

  pub(crate) fn is_main_thread(&self) -> bool {
    std::thread::current().id() == self.main_thread_id
  }

  /// Run `f` on the main (event-loop) thread.
  ///
  /// When called from the main thread we execute `f` inline instead of posting
  /// it to the channel. Tauri implements several blocking getters as
  /// `run_on_main_thread(|| { .. tx.send(..) }); rx.recv()` (e.g.
  /// `Window::add_child`). Those run during `setup`, which the runtime drives
  /// from winit's `can_create_surfaces`; posting the closure instead of running
  /// it inline would deadlock because the loop cannot drain the task while the
  /// main thread blocks on `rx.recv()`.
  pub(crate) fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
    if self.is_main_thread() {
      f();
      Ok(())
    } else {
      self.send_message(Message::Task(Box::new(f)))
    }
  }

  pub(crate) fn next_window_id(&self) -> WindowId {
    self.next_window_id.fetch_add(1, Ordering::Relaxed).into()
  }

  pub(crate) fn next_webview_id(&self) -> u32 {
    self.next_webview_id.fetch_add(1, Ordering::Relaxed)
  }

  pub(crate) fn next_window_event_id(&self) -> u32 {
    self.next_window_event_id.fetch_add(1, Ordering::Relaxed)
  }

  pub(crate) fn next_webview_event_id(&self) -> u32 {
    self.next_webview_event_id.fetch_add(1, Ordering::Relaxed)
  }
}

pub(crate) type AfterWindowCreationCallback = Box<dyn for<'a> Fn(RawWindow<'a>) + Send>;

pub(crate) enum Message<T: UserEvent> {
  EventLoop(EventLoopMessage),
  BrowserClosed(WindowId, u32),
  PopupPending(crate::popup::PopupRequest, Arc<crate::popup::PopupFamily>),
  PopupCreated(
    crate::popup::PopupRequest,
    i32,
    Arc<crate::popup::PopupFamily>,
  ),
  PopupAborted(crate::popup::PopupRequest),
  PopupClosed(i32),
  /// CEF handed us the teardown of a webview's browser, keyed by the webview's
  /// process-unique id. See `TauriCefChildLifeSpanHandler::do_close`.
  #[cfg(any(target_os = "macos", windows))]
  DestroyWebviewHostWindow(u32),
  Opened(Vec<url::Url>),
  #[cfg(target_os = "macos")]
  Reopen {
    has_visible_windows: bool,
  },
  #[cfg(target_os = "macos")]
  AccessibilityChanged {
    enabled: bool,
  },
  CreateWindow {
    window_id: WindowId,
    webview_id: Option<u32>,
    pending: Box<PendingWindow<T, CefRuntime<T>>>,
    after_window_creation: Option<AfterWindowCreationCallback>,
    result_tx: Sender<Result<()>>,
  },
  CreateWebview {
    window_id: WindowId,
    webview_id: u32,
    pending: Box<PendingWebview<T, CefRuntime<T>>>,
    result_tx: Sender<Result<()>>,
  },
  Window {
    window_id: WindowId,
    message: WindowMessage,
  },
  Webview {
    window_id: WindowId,
    webview_id: u32,
    message: WebviewMessage,
  },
  NavigateFirstWebview {
    window_id: WindowId,
    url: String,
  },
  DragDropScriptEvent {
    window_id: WindowId,
    webview_id: u32,
    target: browser_client::DragDropEventTarget,
    drag_drop_state: Arc<Mutex<browser_client::DragDropState>>,
    event: browser_client::DragDropScriptEvent,
  },
  Task(Box<dyn FnOnce() + Send>),
  RequestExit(i32),
  UserEvent(T),
}

fn device_event_filter_to_winit(filter: DeviceEventFilter) -> winit::event_loop::DeviceEvents {
  match filter {
    DeviceEventFilter::Always => winit::event_loop::DeviceEvents::Never,
    DeviceEventFilter::Unfocused => winit::event_loop::DeviceEvents::WhenFocused,
    DeviceEventFilter::Never => winit::event_loop::DeviceEvents::Always,
  }
}

pub(crate) enum EventLoopMessage {
  SetTheme(Option<Theme>),
  SetDeviceEventFilter(DeviceEventFilter),
  PrimaryMonitor(Sender<Result<Option<Monitor>>>),
  MonitorFromPoint(Sender<Result<Option<Monitor>>>, f64, f64),
  AvailableMonitors(Sender<Result<Vec<Monitor>>>),
  CursorPosition(Sender<Result<PhysicalPosition<f64>>>),
  DisplayHandle(Sender<std::result::Result<SendRawDisplayHandle, raw_window_handle::HandleError>>),
  #[cfg(target_os = "macos")]
  SetActivationPolicy(tauri_runtime::ActivationPolicy),
  #[cfg(target_os = "macos")]
  SetDockVisibility(bool),
  #[cfg(target_os = "macos")]
  ShowApplication,
  #[cfg(target_os = "macos")]
  HideApplication,
}

#[derive(Debug)]
pub(crate) struct WinitDragDropState {
  id: DataTransferId,
  paths: Option<Vec<PathBuf>>,
  paths_requested: bool,
  enter_position: Option<PhysicalPosition<f64>>,
  latest_position: Option<PhysicalPosition<f64>>,
  enter_emitted: bool,
  drop_pending: bool,
}

impl WinitDragDropState {
  fn position(&self) -> PhysicalPosition<f64> {
    self
      .latest_position
      .or(self.enter_position)
      .unwrap_or_default()
  }
}

fn pending_native_drag_enter(
  native_drag_drop: &mut Option<WinitDragDropState>,
) -> Option<DragDropEvent> {
  native_drag_drop.as_mut().and_then(|state| {
    if state.enter_emitted {
      return None;
    }

    let paths = state.paths.clone()?;
    let position = state.position();

    state.enter_emitted = true;
    Some(DragDropEvent::Enter { paths, position })
  })
}

fn pending_native_drag_drop(
  native_drag_drop: &mut Option<WinitDragDropState>,
) -> Option<DragDropEvent> {
  native_drag_drop.as_mut().and_then(|state| {
    if !state.drop_pending || !state.enter_emitted {
      return None;
    }

    let paths = state.paths.clone()?;
    let position = state.position();

    Some(DragDropEvent::Drop { paths, position })
  })
}

fn request_native_drag_paths(
  event_loop: &dyn ActiveEventLoop,
  native_drag_drop: &mut Option<WinitDragDropState>,
) {
  let Some(state) = native_drag_drop else {
    return;
  };
  let id = state.id;
  if state.paths.is_some() || state.paths_requested {
    return;
  }

  if event_loop
    .fetch_data_transfer(id, &TypeHint::UriList)
    .is_err()
  {
    *native_drag_drop = None;
    let _ = event_loop.set_valid_dnd_actions(id, &[]);
  } else if let Some(state) = native_drag_drop {
    state.paths_requested = true;
  }
}

macro_rules! event_loop_getter {
  ($self:ident, $variant:ident) => {{
    let (tx, rx) = mpsc::channel();
    match $self
      .context
      .send_message(Message::EventLoop(EventLoopMessage::$variant(tx)))
    {
      Ok(()) => rx.recv().map_err(|_| Error::FailedToReceiveMessage),
      Err(error) => Err(error),
    }
  }};
}

fn find_monitor_from_point(
  monitors: impl Iterator<Item = winit::monitor::MonitorHandle>,
  x: f64,
  y: f64,
) -> Option<winit::monitor::MonitorHandle> {
  monitors.into_iter().find(|monitor| {
    let pos = monitor.position().unwrap_or_default();
    let size = monitor
      .current_video_mode()
      .map(|mode| mode.size())
      .unwrap_or_default();
    x >= pos.x as f64
      && x <= pos.x as f64 + size.width as f64
      && y >= pos.y as f64
      && y <= pos.y as f64 + size.height as f64
  })
}

#[cfg(target_os = "macos")]
fn is_cef_helper_process() -> bool {
  const HELPER_SUFFIXES: &[&str] = &[
    " Helper (GPU)",
    " Helper (Renderer)",
    " Helper (Plugin)",
    " Helper (Alerts)",
    " Helper",
  ];

  std::env::current_exe()
    .ok()
    .and_then(|path| {
      path
        .file_name()
        .and_then(|name| name.to_str())
        .map(|name| HELPER_SUFFIXES.iter().any(|suffix| name.ends_with(suffix)))
    })
    .unwrap_or_default()
}

pub(crate) struct AppState<T: UserEvent> {
  pub(crate) windows: HashMap<WindowId, AppWindow>,
  /// Windows that are already closed as far as the application is concerned,
  /// kept alive only so their native handle outlives the CEF browsers they
  /// host. See [`WinitCefApp::close_window`].
  pub(crate) closing_windows: Vec<AppWindow>,
  pub(crate) winid_id_to_window_id_map: HashMap<WinitWindowId, WindowId>,
  pub(crate) callback: Box<dyn FnMut(RunEvent<T>)>,
  pub(crate) live_browsers: usize,
  live_popups: HashMap<i32, Arc<crate::popup::PopupFamily>>,
  pending_popups: Vec<(crate::popup::PopupRequest, Arc<crate::popup::PopupFamily>)>,
  pub(crate) exiting: bool,
}

pub(crate) struct WinitCefApp<T: UserEvent> {
  pub(crate) context: RuntimeContext<T>,
  receiver: Receiver<Message<T>>,
  pub(crate) state: AppState<T>,
  pub(crate) scheme_registry: request_handler::SchemeRegistry,
}

impl<T: UserEvent> WinitCefApp<T> {
  fn new(
    context: RuntimeContext<T>,
    receiver: Receiver<Message<T>>,
    callback: Box<dyn FnMut(RunEvent<T>)>,
    scheme_registry: request_handler::SchemeRegistry,
  ) -> Self {
    Self {
      context,
      receiver,
      state: AppState {
        windows: HashMap::new(),
        closing_windows: Vec::new(),
        winid_id_to_window_id_map: HashMap::new(),
        callback,
        live_browsers: 0,
        live_popups: HashMap::new(),
        pending_popups: Vec::new(),
        exiting: false,
      },
      scheme_registry,
    }
  }

  fn run_callback(&mut self, event: RunEvent<T>) {
    (self.state.callback)(event);
  }

  fn install_current_dispatch(
    &mut self,
    event_loop: &dyn ActiveEventLoop,
  ) -> MainThreadDispatchGuard<T> {
    let mut dispatch = Box::new(MainThreadDispatch {
      app: self as *mut _,
      event_loop: event_loop as *const _,
    });

    let previous = self.context.current_dispatch.install(dispatch.as_mut());

    MainThreadDispatchGuard {
      context: self.context.clone(),
      dispatch,
      previous,
    }
  }

  fn drain_messages(&mut self, event_loop: &dyn ActiveEventLoop) {
    while let Ok(message) = self.receiver.try_recv() {
      self.handle_message(event_loop, message);
    }
  }

  fn handle_message(&mut self, event_loop: &dyn ActiveEventLoop, message: Message<T>) {
    match message {
      Message::EventLoop(message) => self.handle_event_loop_message(event_loop, message),
      Message::PopupPending(request, family) => {
        self.state.pending_popups.push((request, family));
      }
      Message::PopupCreated(request, id, family) => {
        self
          .state
          .pending_popups
          .retain(|(pending, _)| !pending.is_same(&request));
        self.state.live_popups.insert(id, family);
      }
      Message::PopupAborted(request) => {
        self
          .state
          .pending_popups
          .retain(|(pending, _)| !pending.is_same(&request));
        self.exit_if_done(event_loop);
      }
      Message::PopupClosed(id) => {
        self.state.live_popups.remove(&id);
        self.exit_if_done(event_loop);
      }
      Message::BrowserClosed(_window_id, webview_id) => {
        // Standalone webview.close() and app shutdown keep the child in state
        // until this callback, so cleanup happens here. Individual window
        // teardown removes child bookkeeping first; then this message is only
        // the lifecycle acknowledgement that lets live_browsers drain.
        //
        // The window_id baked into the browser's handlers can be stale after a
        // reparent, so locate the webview by its process-unique id across every
        // window rather than trusting the message's window_id — otherwise a
        // reparented webview's scheme-handler entries would leak and its
        // AppWebview would linger in the target window forever.
        let closed = self.state.windows.iter_mut().find_map(|(id, appwindow)| {
          appwindow
            .children
            .iter()
            .position(|child| child.webview_id == webview_id)
            .map(|index| {
              let child = appwindow.children.remove(index);
              (*id, child, appwindow.children.is_empty())
            })
        });

        let mut emptied_window = None;
        if let Some((window_id, child, was_last)) = closed {
          self.remove_scheme_handler_entries(&child);
          if was_last {
            emptied_window = Some(window_id);
          }
        } else {
          // The webview belonged to a window that is already closing: its
          // registry entries went with `close_window`, and the native window is
          // only being held open for CEF. This acknowledgement is what releases
          // it, once it is the last browser the window was hosting.
          for appwindow in &mut self.state.closing_windows {
            appwindow
              .children
              .retain(|child| child.webview_id != webview_id);
          }
          self
            .state
            .closing_windows
            .retain(|appwindow| !appwindow.children.is_empty());
        }

        self.state.live_browsers = self.state.live_browsers.saturating_sub(1);

        // A window that just lost its last webview has nothing left to show, so
        // it follows the webview out through the regular close path — listeners
        // still get `CloseRequested` and can keep the empty window around.
        // `close_window` runs the exit check itself.
        if let Some(window_id) = emptied_window {
          self.request_window_close(window_id, event_loop);
        } else {
          self.exit_if_done(event_loop);
        }
      }
      #[cfg(any(target_os = "macos", windows))]
      Message::DestroyWebviewHostWindow(webview_id) => {
        // Destroying the browser's own child view/window is what completes the
        // close CEF handed over in `do_close`; CEF acknowledges it with
        // `BrowserClosed`, which is where the bookkeeping is dropped. Same
        // reasoning as there for searching every window by webview id.
        //
        // Closing windows are searched too: they hold their children until CEF
        // acknowledges them, and this destruction is what makes CEF do that.
        if let Some(child) = self
          .state
          .windows
          .values()
          .chain(self.state.closing_windows.iter())
          .flat_map(|appwindow| appwindow.children.iter())
          .find(|child| child.webview_id == webview_id)
        {
          child.destroy_host_window();
        }
      }
      Message::CreateWindow {
        window_id,
        webview_id,
        pending,
        after_window_creation,
        result_tx,
      } => {
        let result = self.create_window(
          event_loop,
          window_id,
          webview_id,
          pending,
          after_window_creation,
        );
        let _ = result_tx.send(result);
      }
      Message::CreateWebview {
        window_id,
        webview_id,
        pending,
        result_tx,
      } => {
        let _ = result_tx.send(self.create_webview(window_id, webview_id, *pending));
      }
      Message::Window { window_id, message } => {
        self.handle_window_message(event_loop, window_id, message)
      }
      Message::Webview {
        window_id,
        webview_id,
        message,
      } => self.handle_webview_message(window_id, webview_id, message),
      Message::NavigateFirstWebview { window_id, url } => {
        self.navigate_first_webview(window_id, &url)
      }
      Message::DragDropScriptEvent {
        window_id,
        webview_id,
        target,
        drag_drop_state,
        event,
      } => {
        if let Some(event) = browser_client::event_from_script_event(&drag_drop_state, event) {
          self.emit_drag_drop_event(window_id, webview_id, target, event);
        }
      }
      Message::Task(task) => task(),
      Message::RequestExit(code) => {
        if self.request_exit(Some(code)) {
          self.close_all_browsers();
          self.exit_if_done(event_loop);
        }
      }
      Message::Opened(urls) => self.run_callback(RunEvent::Opened { urls }),
      #[cfg(target_os = "macos")]
      Message::Reopen {
        has_visible_windows,
      } => self.run_callback(RunEvent::Reopen {
        has_visible_windows,
      }),
      #[cfg(target_os = "macos")]
      Message::AccessibilityChanged { enabled } => self.set_browsers_accessibility_state(enabled),
      Message::UserEvent(event) => self.run_callback(RunEvent::UserEvent(event)),
    }
  }

  fn handle_event_loop_message(
    &mut self,
    event_loop: &dyn ActiveEventLoop,
    message: EventLoopMessage,
  ) {
    match message {
      EventLoopMessage::SetTheme(theme) => {
        *self.context.app_wide_theme.lock().unwrap() = theme;
        for appwindow in self.state.windows.values_mut() {
          appwindow.set_theme(theme);
        }
      }
      EventLoopMessage::PrimaryMonitor(tx) => {
        let monitor = event_loop
          .primary_monitor()
          .map(|monitor| winit_monitor_to_tauri_monitor(&monitor));
        let _ = tx.send(Ok(monitor));
      }
      EventLoopMessage::MonitorFromPoint(tx, x, y) => {
        let monitor = find_monitor_from_point(event_loop.available_monitors(), x, y)
          .map(|monitor| winit_monitor_to_tauri_monitor(&monitor));
        let _ = tx.send(Ok(monitor));
      }
      EventLoopMessage::AvailableMonitors(tx) => {
        let monitors = event_loop
          .available_monitors()
          .map(|monitor| winit_monitor_to_tauri_monitor(&monitor))
          .collect();
        let _ = tx.send(Ok(monitors));
      }
      EventLoopMessage::SetDeviceEventFilter(filter) => {
        event_loop.listen_device_events(device_event_filter_to_winit(filter));
      }
      EventLoopMessage::CursorPosition(tx) => {
        let _ = tx.send(event_loop.cursor_position());
      }
      EventLoopMessage::DisplayHandle(tx) => {
        let handle = event_loop
          .display_handle()
          .map(|handle| SendRawDisplayHandle(handle.as_raw()));
        let _ = tx.send(handle);
      }
      #[cfg(target_os = "macos")]
      EventLoopMessage::SetActivationPolicy(activation_policy) => {
        event_loop.set_activation_policy(activation_policy)
      }
      #[cfg(target_os = "macos")]
      EventLoopMessage::SetDockVisibility(visible) => event_loop.set_dock_visibility(visible),
      #[cfg(target_os = "macos")]
      EventLoopMessage::ShowApplication => event_loop.show_application(),
      #[cfg(target_os = "macos")]
      EventLoopMessage::HideApplication => event_loop.hide_application(),
    }
  }

  /// Removes the webview's `(browser_id, scheme)` entries from the scheme registry.
  fn remove_scheme_handler_entries(&self, child: &AppWebview) {
    let mut registry = self.scheme_registry.lock().unwrap();
    for scheme in child.uri_scheme_protocols.keys() {
      registry.remove(&(child.browser_id, scheme.clone()));
    }
  }

  fn emit_drag_drop_event(
    &mut self,
    window_id: WindowId,
    webview_id: u32,
    target: browser_client::DragDropEventTarget,
    event: DragDropEvent,
  ) {
    match target {
      browser_client::DragDropEventTarget::Window => {
        self.emit_window_event(window_id, WindowEvent::DragDrop(event));
      }
      browser_client::DragDropEventTarget::Webview => {
        self.emit_webview_event(window_id, webview_id, WebviewEvent::DragDrop(event));
      }
    }
  }

  fn emit_window_event(&mut self, window_id: WindowId, event: WindowEvent) {
    let Some(appwindow) = self.state.windows.get(&window_id) else {
      return;
    };
    let label = appwindow.label.clone();
    let listeners = appwindow.listeners.clone();

    self.run_callback(RunEvent::WindowEvent {
      label,
      event: event.clone(),
    });

    {
      let listeners = listeners.lock().unwrap();
      for handler in listeners.values() {
        handler(&event);
      }
    }
  }

  fn emit_webview_event(&mut self, window_id: WindowId, webview_id: u32, event: WebviewEvent) {
    let Some(appwindow) = self.state.windows.get(&window_id) else {
      return;
    };
    let Some(child) = appwindow
      .children
      .iter()
      .find(|child| child.webview_id == webview_id)
    else {
      return;
    };
    let label = child.label.clone();
    let listeners = child.listeners.clone();

    self.run_callback(RunEvent::WebviewEvent {
      label,
      event: event.clone(),
    });

    {
      let listeners = listeners.lock().unwrap();
      for handler in listeners.values() {
        handler(&event);
      }
    }
  }

  fn request_exit(&mut self, code: Option<i32>) -> bool {
    // if we already exiting, don't request exit again
    if self.state.exiting {
      return false;
    }

    let (tx, rx) = mpsc::channel();
    self.run_callback(RunEvent::ExitRequested { code, tx });

    if matches!(rx.try_recv(), Ok(ExitRequestedEventAction::Prevent)) {
      false
    } else {
      self.state.exiting = true;
      true
    }
  }

  pub(crate) fn close_window(&mut self, window_id: WindowId, event_loop: &dyn ActiveEventLoop) {
    if !self.state.windows.contains_key(&window_id) {
      return;
    }
    // Every close path funnels through here, and this is the last point at which
    // the window can still be named: the maps below are what `emit_window_event`
    // and winit's own `Destroyed` both resolve a window through, and winit
    // reports the destruction only once the window below has been dropped.
    // Without this, `WindowEvent::Destroyed` never reaches the application, and
    // it is what Tauri unregisters a window on — so a window closed while others
    // stay open would keep its label taken and keep appearing in `Manager`'s
    // window list forever.
    if !self.state.exiting {
      self.emit_window_event(window_id, WindowEvent::Destroyed);
    }
    let Some(appwindow) = self.state.windows.remove(&window_id) else {
      return;
    };
    self
      .state
      .winid_id_to_window_id_map
      .remove(&appwindow.window.id());
    // The window is gone from state, so BrowserClosed will not find these
    // children later. Clean registry entries while we still hold them; the CEF
    // shutdown drain is still enforced by live_browsers.
    for child in &appwindow.children {
      self.remove_scheme_handler_entries(child);
      child.popup_family.close_all();
      // DevTools is a browser of its own, living in a window CEF owns and
      // parents to this one. Closing the browser it inspects does not take it
      // down first, so ask for it explicitly — exactly what a webview-level
      // close does — instead of leaving CEF to discover its window is gone.
      child.host.close_dev_tools();
      child.host.close_browser(1);
    }

    if appwindow.children.is_empty() {
      // Nothing is left to close, so the native window goes now.
      drop(appwindow);
    } else {
      // `close_browser` only *starts* the close: CEF still has to tear down the
      // browser's own child window, and it reports that back through
      // `on_before_close`. Destroying the native window we parented it to before
      // that point pulls the ground out from under a browser Chromium is still
      // compositing — with DevTools attached the surface outlives the window
      // long enough for the GPU process to fault on it and restart. So keep the
      // window alive until `BrowserClosed` accounts for every child, and only
      // hide it here: the application already saw `Destroyed`, so the wait must
      // not be visible to the user.
      appwindow.window.set_visible(false);
      self.state.closing_windows.push(appwindow);
    }

    // A window still waiting on its browsers holds `live_browsers` above zero,
    // so this cannot exit the loop out from under a close in flight.
    self.exit_if_done(event_loop);
  }

  pub(crate) fn request_window_close(
    &mut self,
    window_id: WindowId,
    event_loop: &dyn ActiveEventLoop,
  ) {
    // Avoid requesting window close if we already exisitng
    if self.state.exiting {
      self.close_window(window_id, event_loop);
      return;
    }

    let (tx, rx) = mpsc::channel();
    let Some(appwindow) = self.state.windows.get(&window_id) else {
      return;
    };
    let label = appwindow.label.clone();
    let listeners = appwindow.listeners.clone();

    {
      let listeners = listeners.lock().unwrap();
      for handler in listeners.values() {
        handler(&WindowEvent::CloseRequested {
          signal_tx: tx.clone(),
        });
      }
    }

    self.run_callback(RunEvent::WindowEvent {
      label,
      event: WindowEvent::CloseRequested { signal_tx: tx },
    });

    if !matches!(rx.try_recv(), Ok(true)) {
      self.close_window(window_id, event_loop);
    }
  }

  fn navigate_first_webview(&self, window_id: WindowId, url: &str) {
    let Some(frame) = self
      .state
      .windows
      .get(&window_id)
      .and_then(|window| window.children.first())
      .and_then(|webview| webview.browser.main_frame())
    else {
      return;
    };

    frame.load_url(Some(&CefString::from(url)));
  }

  fn close_all_browsers(&mut self) {
    // Keep each child reachable until CEF acknowledges its close. On macOS and
    // Windows, do_close queues DestroyWebviewHostWindow, which needs this state
    // to destroy the native child view and trigger on_before_close. Dropping
    // the windows here can strand live_browsers and prevent process exit.
    for appwindow in self.state.windows.values() {
      for child in &appwindow.children {
        child.popup_family.close_all();
        child.host.close_dev_tools();
        child.host.close_browser(1);
      }
    }
  }

  #[cfg(target_os = "macos")]
  fn set_browsers_accessibility_state(&self, enabled: bool) {
    let state = if enabled {
      State::ENABLED
    } else {
      State::DISABLED
    };
    for appwindow in self.state.windows.values() {
      for child in &appwindow.children {
        child.host.set_accessibility_state(state);
      }
    }
  }

  fn exit_if_done(&mut self, event_loop: &dyn ActiveEventLoop) {
    // A reservation is normally resolved by `PopupCreated` or `PopupAborted`,
    // but CEF discards popups without always reporting the abort — the opener
    // can be torn down first, or the abort can arrive for a browser its opener
    // no longer matches. Teardown (window close, app shutdown, the root's own
    // native close) revokes the family, and a revoked family never admits a
    // popup again, so its reservations are dead and must not hold the process
    // open. Reservations of live families still gate the exit until CEF
    // resolves them.
    self
      .state
      .pending_popups
      .retain(|(_, family)| !family.is_revoked());

    if self.state.live_browsers != 0
      || !self.state.live_popups.is_empty()
      || !self.state.pending_popups.is_empty()
    {
      return;
    }

    if self.state.exiting || (self.state.windows.is_empty() && self.request_exit(None)) {
      self.run_callback(RunEvent::Exit);
      event_loop.exit();
    }
  }
}

impl<T: UserEvent> ApplicationHandler for WinitCefApp<T> {
  fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
    let _guard = self.install_current_dispatch(event_loop);
    self.drain_messages(event_loop);
  }

  fn new_events(&mut self, event_loop: &dyn ActiveEventLoop, cause: StartCause) {
    let _guard = self.install_current_dispatch(event_loop);
    match cause {
      StartCause::Init => {
        self.run_callback(RunEvent::Ready);
        self.context.cef_pump.do_work();
      }
      // Match wry/tao, which emit `Resumed` on each `Poll` start cause.
      StartCause::Poll => self.run_callback(RunEvent::Resumed),
      _ => {}
    }
  }

  fn proxy_wake_up(&mut self, event_loop: &dyn ActiveEventLoop) {
    let _guard = self.install_current_dispatch(event_loop);
    self.drain_messages(event_loop);
  }

  fn about_to_wait(&mut self, event_loop: &dyn ActiveEventLoop) {
    let _guard = self.install_current_dispatch(event_loop);
    self.apply_pending_activations();
    #[cfg(any(
      target_os = "linux",
      target_os = "dragonfly",
      target_os = "freebsd",
      target_os = "netbsd",
      target_os = "openbsd"
    ))]
    self.apply_pending_host_layouts();
    self.run_callback(RunEvent::MainEventsCleared);
  }

  fn window_event(
    &mut self,
    event_loop: &dyn ActiveEventLoop,
    winit_id: WinitWindowId,
    event: WinitWindowEvent,
  ) {
    let _guard = self.install_current_dispatch(event_loop);
    let Some(window_id) = self.state.winid_id_to_window_id_map.get(&winit_id).copied() else {
      return;
    };
    let Some(appwindow) = self.state.windows.get_mut(&window_id) else {
      return;
    };

    match event {
      WinitWindowEvent::CloseRequested => self.request_window_close(window_id, event_loop),

      // Reached only when the native window went away without a close request of
      // its own; `close_window` emits `Destroyed` for every path, this one
      // included.
      WinitWindowEvent::Destroyed => self.close_window(window_id, event_loop),
      WinitWindowEvent::SurfaceResized(size) => {
        webview::layout_app_window(appwindow);
        self.emit_window_event(window_id, WindowEvent::Resized(size));
      }
      WinitWindowEvent::ScaleFactorChanged {
        scale_factor,
        surface_size_writer,
      } => {
        let new_inner_size = surface_size_writer
          .surface_size()
          .unwrap_or_else(|_| appwindow.window.surface_size());
        webview::layout_app_window(appwindow);
        self.emit_window_event(
          window_id,
          WindowEvent::ScaleFactorChanged {
            scale_factor,
            new_inner_size,
          },
        );
      }
      WinitWindowEvent::Moved(pos) => {
        self.emit_window_event(
          window_id,
          WindowEvent::Moved(PhysicalPosition::new(pos.x, pos.y)),
        );
      }
      WinitWindowEvent::Focused(focused) => {
        self.emit_window_event(window_id, WindowEvent::Focused(focused));
      }
      WinitWindowEvent::ThemeChanged(theme) => {
        let system_theme = winit_theme_to_tauri_theme(theme);
        if let Some(explicit_theme) = appwindow.preferred_theme() {
          appwindow.set_theme(Some(explicit_theme));
        } else {
          // Following the system: the appearance changed without going through
          // `set_theme`, so the titlebar rebuild still has to be undone.
          #[cfg(target_os = "macos")]
          appwindow.reapply_traffic_light_position_after_appearance_change();
        }
        self.emit_window_event(window_id, WindowEvent::ThemeChanged(system_theme));
      }
      #[cfg(windows)]
      WinitWindowEvent::RedrawRequested => {
        appwindow.draw_background_surface();
      }
      WinitWindowEvent::DragEntered { id, position } => {
        let has_file_paths = event_loop
          .data_transfer(id)
          .map(|data_transfer| data_transfer.has_type(&TypeHint::UriList))
          .unwrap_or(false);

        if has_file_paths {
          appwindow.native_drag_drop = Some(WinitDragDropState {
            id,
            paths: None,
            paths_requested: false,
            enter_position: position,
            latest_position: position,
            enter_emitted: false,
            drop_pending: false,
          });
          let _ = event_loop.set_valid_dnd_actions(id, &[DndAction::Copy]);
          request_native_drag_paths(event_loop, &mut appwindow.native_drag_drop);
        } else {
          appwindow.native_drag_drop = None;
          let _ = event_loop.set_valid_dnd_actions(id, &[]);
        }
      }
      WinitWindowEvent::DragPosition { id, position, .. } => {
        if let Some(state) = appwindow
          .native_drag_drop
          .as_mut()
          .filter(|state| state.id == id)
        {
          state.latest_position = Some(position);
          state.enter_position.get_or_insert(position);
        }

        let enter_event = pending_native_drag_enter(&mut appwindow.native_drag_drop);

        let over_event = appwindow
          .native_drag_drop
          .as_ref()
          .filter(|state| state.id == id && state.enter_emitted)
          .map(|_| DragDropEvent::Over { position });

        if let Some(event) = enter_event {
          self.emit_window_event(window_id, WindowEvent::DragDrop(event));
        }
        if let Some(event) = over_event {
          self.emit_window_event(window_id, WindowEvent::DragDrop(event));
        }
      }
      WinitWindowEvent::DragDropped { id, .. } => {
        if let Some(state) = appwindow
          .native_drag_drop
          .as_mut()
          .filter(|state| state.id == id)
        {
          state.drop_pending = true;
        }

        request_native_drag_paths(event_loop, &mut appwindow.native_drag_drop);

        let enter_event = pending_native_drag_enter(&mut appwindow.native_drag_drop);
        let drop_event = pending_native_drag_drop(&mut appwindow.native_drag_drop);

        let drop_emitted = drop_event.is_some();
        if drop_emitted {
          appwindow.native_drag_drop = None;
        }

        if let Some(event) = enter_event {
          self.emit_window_event(window_id, WindowEvent::DragDrop(event));
        }
        if let Some(event) = drop_event {
          self.emit_window_event(window_id, WindowEvent::DragDrop(event));
        }
      }
      WinitWindowEvent::DragLeft { id } => {
        let entered = appwindow
          .native_drag_drop
          .as_ref()
          .is_some_and(|state| state.id == id && state.enter_emitted);

        appwindow.native_drag_drop = None;

        if entered {
          self.emit_window_event(window_id, WindowEvent::DragDrop(DragDropEvent::Leave));
        }
      }
      WinitWindowEvent::DataTransferReceived { id, value, .. } => {
        let mut reject_drag = false;

        if let Some(state) = appwindow
          .native_drag_drop
          .as_mut()
          .filter(|state| state.id == id)
        {
          match value.try_as_file_paths() {
            Ok(paths) if !paths.is_empty() => state.paths = Some(paths),
            Ok(_) => reject_drag = state.drop_pending,
            Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {}
            Err(_) => reject_drag = state.drop_pending,
          }
        }

        if reject_drag {
          appwindow.native_drag_drop = None;
          let _ = event_loop.set_valid_dnd_actions(id, &[]);
          return;
        }

        let enter_event = pending_native_drag_enter(&mut appwindow.native_drag_drop);
        let drop_event = pending_native_drag_drop(&mut appwindow.native_drag_drop);

        let drop_emitted = drop_event.is_some();
        if drop_emitted {
          appwindow.native_drag_drop = None;
        }

        if let Some(event) = enter_event {
          self.emit_window_event(window_id, WindowEvent::DragDrop(event));
        }
        if let Some(event) = drop_event {
          self.emit_window_event(window_id, WindowEvent::DragDrop(event));
        }
      }
      _ => {}
    }
  }
}

/// Picks the deep link URLs out of a process command line.
///
/// An argument qualifies when it parses as a URL whose scheme is one of `schemes`,
/// matched the same exact way `BrowserProcessHandler::on_already_running_app_relaunch`
/// matches it on the receiving end. Everything else is dropped: the point of
/// [`Cef::allow_chromium_command_line_args`] being off is that no other argument
/// survives onto Chromium's command line.
fn deep_link_arguments<I>(args: I, schemes: &[String]) -> Vec<String>
where
  I: IntoIterator<Item = String>,
{
  args
    .into_iter()
    .filter(|arg| {
      url::Url::parse(arg).is_ok_and(|url| schemes.iter().any(|scheme| scheme == url.scheme()))
    })
    .collect()
}

/// Appends `args` to `command_line`, as a switch with a value, a bare switch or a
/// positional argument depending on how each entry looks.
///
/// A bare name with no value is only recognised as a switch when it is spelled with its
/// `--` prefix; without one it is a positional argument. This runtime's own entries are
/// therefore all spelled `--switch`, values included — Chromium strips the prefix off the
/// key it stores, so both spellings reach the same switch.
fn append_command_line_args(command_line: &mut CommandLine, args: &[(String, Option<String>)]) {
  for (arg, value) in args {
    if let Some(value) = value {
      command_line.append_switch_with_value(
        Some(&CefString::from(arg.as_str())),
        Some(&CefString::from(value.as_str())),
      );
    } else if arg.starts_with("-") {
      command_line.append_switch(Some(&CefString::from(arg.as_str())));
    } else {
      command_line.append_argument(Some(&CefString::from(arg.as_str())));
    }
  }
}

wrap_with_args! {
  wrap_app => TauriCefAppArgs;

  struct TauriCefApp<T: UserEvent> {
    context: RuntimeContext<T>,
    context_initialized: Arc<AtomicBool>,
    deep_link_schemes: Vec<String>,
    // Whether the deep link URL this process was launched with has to be put back
    // onto Chromium's command line. See `on_before_command_line_processing`.
    restore_deep_link_arguments: bool,
    // Switches applied whatever process type `on_before_command_line_processing` reports.
    //
    // Deliberately tiny: `cef_app_t::on_before_command_line_processing` warns that
    // "modifying the command-line arguments for non-browser processes may result in
    // undefined behavior including crashes", so only switches we know a child process
    // must see itself belong here.
    internal_command_line_args: Vec<(String, Option<String>)>,
    // Switches applied only when the reported process type is the browser one.
    //
    // Chromium already forwards to each child the switches it needs, so anything that
    // is only read in the browser process - and everything the embedding application
    // supplied through `Cef::command_line_arg` - goes here. The application's own
    // switches are appended last so they win over the runtime's defaults.
    browser_command_line_args: Vec<(String, Option<String>)>,
    // Names merged into `--disable-features` and `--enable-features` rather than
    // appended over them.
    //
    // CEF fills `--disable-features` before it calls this hook with a list that keeps
    // Chrome from crashing at startup and renderers from being killed on the runtime's
    // own requests, and Chromium's command line replaces a switch value rather than
    // extending it. See `crate::switches::append_merged_switch`.
    disabled_features: Vec<String>,
    enabled_features: Vec<String>,
  }

  impl App {
    fn render_process_handler(&self) -> Option<RenderProcessHandler> {
      Some(ipc::TauriRenderProcessHandler::new())
    }

    fn browser_process_handler(&self) -> Option<BrowserProcessHandler> {
      Some(browser_client::TauriCefBrowserProcessHandler::new(
        self.context.clone(),
        self.context_initialized.clone(),
        self.deep_link_schemes.clone(),
      ))
    }

    fn on_before_command_line_processing(
      &self,
      process_type: Option<&CefString>,
      command_line: Option<&mut CommandLine>,
    ) {
      let Some(command_line) = command_line else {
        return;
      };

      append_command_line_args(command_line, &self.internal_command_line_args);

      // The browser process is the one launched without a `--type` switch, so CEF hands
      // us an empty (or absent) process type for it.
      let is_browser_process = process_type.is_none_or(|ty| ty.to_string().is_empty());
      if is_browser_process {
        // A second launch of an already-running application is a browser process too,
        // so `Settings::command_line_args_disabled` clears its command line before
        // Chromium's process singleton relays it to the first instance, losing the
        // `myapp://...` URL. Putting it back here happens after CEF's clear and before
        // the singleton. Only deep links are restored; every other argument stays
        // dropped, which is the point of the lockdown.
        if self.restore_deep_link_arguments {
          for deep_link in deep_link_arguments(std::env::args().skip(1), &self.deep_link_schemes) {
            command_line.append_argument(Some(&CefString::from(deep_link.as_str())));
          }
        }

        append_command_line_args(command_line, &self.browser_command_line_args);

        // Last, and merging rather than replacing: CEF has already written its own
        // `--disable-features` by this point, and appending over it would drop entries
        // Chrome needs to start at all.
        crate::switches::append_merged_switch(
          command_line,
          "disable-features",
          &self.disabled_features,
        );
        crate::switches::append_merged_switch(
          command_line,
          "enable-features",
          &self.enabled_features,
        );
      }
    }
  }
}

pub fn run_cef_helper_process() {
  let args = cef::args::Args::new();

  // A helper the browser process launched with `--no-sandbox` must not enter the sandbox
  // here: the browser dropped it deliberately, and entering it anyway would only make the
  // library load below fail.
  #[cfg(target_os = "macos")]
  let _sandbox = (!crate::sandbox::launched_without_sandbox()).then(|| {
    let mut sandbox = cef::sandbox::Sandbox::new();
    sandbox.initialize(args.as_main_args());
    sandbox
  });

  #[cfg(target_os = "macos")]
  let _loader = {
    let loader = cef::library_loader::LibraryLoader::new(&std::env::current_exe().unwrap(), true);
    assert!(loader.load());
    loader
  };

  let _ = cef::api_hash(sys::CEF_API_VERSION_LAST, 0);
  let mut app = TauriCefHelperApp::new();
  let _ = cef::execute_process(
    Some(args.as_main_args()),
    Some(&mut app),
    std::ptr::null_mut(),
  );
}

wrap_app! {
  struct TauriCefHelperApp;

  impl App {
    fn render_process_handler(&self) -> Option<RenderProcessHandler> {
      Some(ipc::TauriRenderProcessHandler::new())
    }
  }
}

#[derive(Debug, Clone)]
pub struct CefRuntimeHandle<T: UserEvent> {
  context: RuntimeContext<T>,
}

impl<T: UserEvent> RuntimeHandle<T> for CefRuntimeHandle<T> {
  type Runtime = CefRuntime<T>;

  fn create_proxy(&self) -> <Self::Runtime as Runtime<T>>::EventLoopProxy {
    EventProxy {
      context: self.context.clone(),
    }
  }

  #[cfg(target_os = "macos")]
  fn set_activation_policy(
    &self,
    activation_policy: tauri_runtime::ActivationPolicy,
  ) -> Result<()> {
    let message = Message::EventLoop(EventLoopMessage::SetActivationPolicy(activation_policy));
    self.context.send_message(message)
  }

  #[cfg(target_os = "macos")]
  fn set_dock_visibility(&self, visible: bool) -> Result<()> {
    let message = Message::EventLoop(EventLoopMessage::SetDockVisibility(visible));
    self.context.send_message(message)
  }

  fn request_exit(&self, code: i32) -> Result<()> {
    self.context.send_message(Message::RequestExit(code))
  }

  /// Returns the URL for a custom scheme.
  ///
  /// CEF always uses `http://<scheme>.localhost` or `https://<scheme>.localhost`.
  fn custom_scheme_url(&self, scheme: &str, https: bool) -> String {
    format!(
      "{}://{scheme}.localhost",
      if https { "https" } else { "http" }
    )
  }

  fn webview_version(&self) -> Result<String> {
    crate::webview_version()
  }

  fn create_window<F: Fn(RawWindow<'_>) + Send + 'static>(
    &self,
    pending: PendingWindow<T, Self::Runtime>,
    after_window_creation: Option<F>,
  ) -> Result<DetachedWindow<T, Self::Runtime>> {
    create_window_detached(&self.context, pending, after_window_creation)
  }

  fn create_webview(
    &self,
    window_id: WindowId,
    pending: PendingWebview<T, Self::Runtime>,
  ) -> Result<DetachedWebview<T, Self::Runtime>> {
    create_webview_detached(&self.context, window_id, pending)
  }

  fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
    self.context.run_on_main_thread(f)
  }

  fn display_handle(
    &self,
  ) -> std::result::Result<DisplayHandle<'_>, raw_window_handle::HandleError> {
    let raw = event_loop_getter!(self, DisplayHandle)
      .map_err(|_| raw_window_handle::HandleError::Unavailable)??;
    // SAFETY: the descriptor was produced by the live event loop on its own
    // thread; the borrowed handle is valid for as long as the runtime is.
    Ok(unsafe { DisplayHandle::borrow_raw(raw.0) })
  }

  fn primary_monitor(&self) -> Result<Option<Monitor>> {
    event_loop_getter!(self, PrimaryMonitor)?
  }

  fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
    let (tx, rx) = mpsc::channel();
    self
      .context
      .send_message(Message::EventLoop(EventLoopMessage::MonitorFromPoint(
        tx, x, y,
      )))?;
    rx.recv().map_err(|_| Error::FailedToReceiveMessage)?
  }

  fn available_monitors(&self) -> Result<Vec<Monitor>> {
    event_loop_getter!(self, AvailableMonitors)?
  }

  fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
    event_loop_getter!(self, CursorPosition)?
  }

  fn set_theme(&self, theme: Option<Theme>) {
    let message = Message::EventLoop(EventLoopMessage::SetTheme(theme));
    let _ = self.context.send_message(message);
  }

  #[cfg(target_os = "macos")]
  fn show(&self) -> Result<()> {
    let message = Message::EventLoop(EventLoopMessage::ShowApplication);
    self.context.send_message(message)
  }

  #[cfg(target_os = "macos")]
  fn hide(&self) -> Result<()> {
    let message = Message::EventLoop(EventLoopMessage::HideApplication);
    self.context.send_message(message)
  }

  fn set_device_event_filter(&self, filter: DeviceEventFilter) {
    let message = Message::EventLoop(EventLoopMessage::SetDeviceEventFilter(filter));
    let _ = self.context.send_message(message);
  }

  #[cfg(any(target_os = "macos", target_os = "ios"))]
  fn fetch_data_store_identifiers<F: FnOnce(Vec<[u8; 16]>) + Send + 'static>(
    &self,
    cb: F,
  ) -> Result<()> {
    cb(Vec::new());
    Ok(())
  }

  #[cfg(any(target_os = "macos", target_os = "ios"))]
  fn remove_data_store<F: FnOnce(Result<()>) + Send + 'static>(
    &self,
    _uuid: [u8; 16],
    cb: F,
  ) -> Result<()> {
    cb(Ok(()));
    Ok(())
  }
}

pub struct CefRuntime<T: UserEvent = tauri::EventLoopMessage> {
  event_loop: EventLoop,
  receiver: Receiver<Message<T>>,
  context: RuntimeContext<T>,
  scheme_registry: request_handler::SchemeRegistry,
  #[cfg(target_os = "macos")]
  _app_delegate: Option<objc2::rc::Retained<crate::platform::macos::AppDelegate>>,
}

impl<T: UserEvent> fmt::Debug for CefRuntime<T> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("CefRuntime").finish()
  }
}

/// CEF installs Chromium's shutdown signal handlers for SIGINT/SIGTERM/SIGHUP
/// inside `cef::initialize`. Those handlers hand the signal to Chrome's own exit
/// machinery, which never quits the winit loop this runtime owns, so the process
/// just ignores the signal. Because the handler disarms itself on delivery, it
/// takes a second Ctrl+C to kill the app by default disposition.
///
/// Put the process's original signal policy back so termination signals behave
/// the way they do for any other app (and the way they already do under
/// `tauri-runtime-wry`, which installs no handlers at all).
#[cfg(any(
  target_os = "linux",
  target_os = "dragonfly",
  target_os = "freebsd",
  target_os = "openbsd",
  target_os = "netbsd"
))]
struct TerminationSignals {
  sigint: Option<libc::sigaction>,
  sigterm: Option<libc::sigaction>,
  sighup: Option<libc::sigaction>,
}

#[cfg(any(
  target_os = "linux",
  target_os = "dragonfly",
  target_os = "freebsd",
  target_os = "openbsd",
  target_os = "netbsd"
))]
impl TerminationSignals {
  fn capture() -> Self {
    Self {
      sigint: Self::capture_one(libc::SIGINT),
      sigterm: Self::capture_one(libc::SIGTERM),
      sighup: Self::capture_one(libc::SIGHUP),
    }
  }

  /// Must run *after* `cef::initialize`, which is the only place CEF installs
  /// these handlers; nothing reinstalls them later, so one restore is enough.
  fn restore(&self) {
    Self::restore_one(libc::SIGINT, self.sigint);
    Self::restore_one(libc::SIGTERM, self.sigterm);
    Self::restore_one(libc::SIGHUP, self.sighup);
  }

  fn capture_one(sig: libc::c_int) -> Option<libc::sigaction> {
    let mut action = std::mem::MaybeUninit::<libc::sigaction>::uninit();
    if unsafe { libc::sigaction(sig, std::ptr::null(), action.as_mut_ptr()) } == 0 {
      Some(unsafe { action.assume_init() })
    } else {
      None
    }
  }

  fn restore_one(sig: libc::c_int, previous: Option<libc::sigaction>) {
    let Some(previous) = previous else {
      return;
    };

    unsafe { libc::sigaction(sig, &previous, std::ptr::null_mut()) };
  }
}

impl<T: UserEvent> CefRuntime<T> {
  fn init(
    mut event_loop_builder: EventLoopBuilder,
    runtime_args: RuntimeInitArgs<Cef>,
  ) -> Result<Self> {
    // Snapshot before CEF can touch anything, so we can tell an embedder's own
    // signal policy apart from the handlers CEF installs in `cef::initialize`.
    #[cfg(any(
      target_os = "linux",
      target_os = "dragonfly",
      target_os = "freebsd",
      target_os = "openbsd",
      target_os = "netbsd"
    ))]
    let pre_cef_signals = TerminationSignals::capture();

    let args = cef::args::Args::new();

    #[cfg(target_os = "macos")]
    let is_helper = is_cef_helper_process();

    #[cfg(target_os = "macos")]
    let (_sandbox, _loader) = {
      // As in `run_cef_helper_process`: only a helper enters the sandbox, and only when
      // the browser process that launched it did not already drop the sandbox.
      let sandbox = if is_helper && !crate::sandbox::launched_without_sandbox() {
        let mut sandbox = cef::sandbox::Sandbox::new();
        sandbox.initialize(args.as_main_args());
        Some(sandbox)
      } else {
        None
      };

      let loader =
        cef::library_loader::LibraryLoader::new(&std::env::current_exe().unwrap(), is_helper);
      assert!(loader.load());

      (sandbox, loader)
    };

    #[cfg(target_os = "macos")]
    if !is_helper {
      crate::platform::macos::setup_application();
    }

    // The CEF API version table must be initialized before any other CEF call
    // (e.g. `args.as_cmd_line()` below), otherwise the process crashes with no
    // diagnostics.
    let version = runtime_args
      .runtime_init_attrs
      .api_version
      .unwrap_or(sys::CEF_API_VERSION_LAST);
    let _ = cef::api_hash(version, 0);

    // Handle CEF subprocesses (renderer/GPU/utility) before any browser-only
    // setup such as building the event loop, creating cache directories, or the
    // runtime context. The browser (main) process has no `type` switch;
    // subprocesses are launched with one (e.g. `--type=renderer`).
    let is_browser_process = args
      .as_cmd_line()
      .map(|cmd| cmd.has_switch(Some(&CefString::from("type"))) != 1)
      .unwrap_or(true);

    if !is_browser_process {
      let mut helper_app = TauriCefHelperApp::new();
      let ret = cef::execute_process(
        Some(args.as_main_args()),
        Some(&mut helper_app),
        std::ptr::null_mut(),
      );
      // A subprocess finished its work; exit with its exit code instead of
      // falling through to browser runtime initialization.
      std::process::exit(ret.max(0));
    }

    let Cef {
      command_line_args,
      disabled_features,
      enabled_features,
      deep_link_schemes,
      cache_path: cache_path_override,
      secret_storage,
      mut profile_preferences,
      mut global_preferences,
      content_settings,
      allow_chromium_command_line_args,
      log_file,
      log_severity,
      log_items,
      locale,
      accept_language_list,
      user_agent,
      user_agent_product,
      javascript_flags,
      chrome_policy_id,
      persist_session_cookies,
      remote_debugging,
      devtools: devtools_policy,
      debug_environment,
      certificate_errors,
      sandbox: sandbox_policy,
      settings_callback,
      // Already applied, above, before the first CEF call.
      api_version: _,
    } = runtime_args.runtime_init_attrs;

    // CEF reads its crash-reporter overrides from `BasicStartupComplete`, which
    // `cef::initialize` below reaches, and every child process inherits this environment.
    // `SSLKEYLOGFILE` is answered further down, on the command line.
    crate::environment::remove_crash_reporter_overrides(debug_environment, tauri::is_dev());

    // The application's own switches are the only ones that reach Chromium in a release
    // build, so a switch that turns off a security boundary got there deliberately —
    // say so rather than silently obeying.
    crate::switches::warn_about_dangerous_switches(&command_line_args);
    crate::switches::warn_about_replacing_switches(&command_line_args);

    // Switches every process gets, and switches only the browser process gets. See the
    // `TauriCefApp` fields for why the split exists.
    #[allow(unused_mut)]
    let mut internal_command_line_args: Vec<(String, Option<String>)> = Vec::new();
    #[allow(unused_mut)]
    let mut browser_command_line_args: Vec<(String, Option<String>)> = Vec::new();

    // `os_crypt` only ever runs in the browser process, so these are browser-only
    // switches. See `SecretStorage` for what each one costs.
    #[cfg(target_os = "macos")]
    {
      let mock_keychain = match secret_storage {
        SecretStorage::Auto => tauri::is_dev(),
        SecretStorage::Mock => true,
        SecretStorage::System => false,
      };
      if mock_keychain {
        browser_command_line_args.push(("--use-mock-keychain".to_string(), None));
      }
    }
    #[cfg(any(
      target_os = "linux",
      target_os = "dragonfly",
      target_os = "freebsd",
      target_os = "netbsd",
      target_os = "openbsd"
    ))]
    {
      // `basic` skips the D-Bus secret portal, libsecret and KWallet key providers, any
      // of which can block startup on a keyring-unlock dialog or fail outright in a
      // headless session. `Auto` splits the same way it does for the macOS keychain.
      let basic_password_store = match secret_storage {
        SecretStorage::Auto => tauri::is_dev(),
        SecretStorage::Mock => true,
        SecretStorage::System => false,
      };
      if basic_password_store {
        browser_command_line_args.push(("--password-store".to_string(), Some("basic".to_string())));
      }
    }

    // One decision on every platform, so a lost sandbox is always something the policy
    // asked for and is always logged. On Linux and the BSDs the policy is also weighed
    // against the system, because Chromium aborts with "No usable sandbox!" when its
    // zygote host finds neither usable unprivileged user namespaces nor the setuid
    // `chrome-sandbox` helper, so an AppImage on a system that restricts namespaces
    // cannot start at all.
    let no_sandbox = {
      let decision = crate::sandbox::resolve_sandbox_decision(sandbox_policy);
      match decision {
        crate::sandbox::SandboxDecision::Keep => false,
        crate::sandbox::SandboxDecision::Disable(reason) => {
          log::warn!(
            "running Chromium without a sandbox: {}. A compromised renderer process runs with the full privileges of the current user.",
            reason.message()
          );
          true
        }
        crate::sandbox::SandboxDecision::Refuse(reason) => {
          log::error!(
            "refusing to start: SandboxPolicy::Required asked for a Chromium sandbox, but {}.",
            reason.message()
          );
          return Err(Error::CreateWebview(
            format!(
              "SandboxPolicy::Required cannot be honored: {}",
              reason.message()
            )
            .into(),
          ));
        }
      }
    };
    // Windows encrypts with DPAPI, which needs no switch and prompts for nothing.
    #[cfg(windows)]
    let _ = secret_storage;

    // The DevTools gate, applied to every path this runtime owns: the context menu
    // entries, the F12 and Ctrl+Shift+I chords, the `IDC_DEV_TOOLS` commands and
    // `Webview::open_devtools`. See `DevToolsPolicy` for why it stops there.
    let devtools_allowed = match devtools_policy {
      DevToolsPolicy::Auto => cfg!(debug_assertions) || cfg!(feature = "devtools"),
      DevToolsPolicy::Allowed => true,
      DevToolsPolicy::Disallowed => false,
    };

    // Never the disallowing value, whatever the policy says: CEF gates
    // `SendDevToolsMessage` on this same preference, and refuses it silently — the send
    // reports success and no result or event is ever delivered. This runtime drives its
    // own startup over the DevTools protocol (the document-start scripts, the per-webview
    // user agent) and defers each webview's first navigation until that round trip
    // answers, so a profile carrying `kDisallowed` leaves every window stuck on its blank
    // placeholder.
    //
    // Written as the default rather than simply left alone because Chromium persists it
    // in the profile on disk: a profile an earlier version wrote `kDisallowed` into stays
    // broken on every later run, in a debug build as much as a release one, until
    // something writes it back.
    profile_preferences.insert(
      0,
      (
        crate::cef_impl::preferences::DEVTOOLS_AVAILABILITY.to_string(),
        crate::cef_impl::preferences::DEVTOOLS_ALLOWED.into(),
      ),
    );

    // The DevTools protocol server drives the browser from outside the process, so it is
    // refused two ways: the switch is only appended when the application asked for it,
    // and the local-state preference is pinned off otherwise so a switch that reaches
    // Chromium another way is refused as well.
    let remote_debugging_enabled = match &remote_debugging {
      RemoteDebugging::Disabled => false,
      RemoteDebugging::Pipe => {
        browser_command_line_args.push(("--remote-debugging-pipe".to_string(), None));
        true
      }
      RemoteDebugging::Port {
        port,
        allowed_origins,
      } => {
        // Chromium ignores a port below 1024, which would otherwise leave the application
        // believing it had a debugger it does not have.
        if *port < 1024 {
          log::warn!(
            "ignoring RemoteDebugging::Port {{ port: {port} }}: only ports between 1024 and 65535 are accepted"
          );
          false
        } else {
          browser_command_line_args.push((
            "--remote-debugging-port".to_string(),
            Some(port.to_string()),
          ));
          if !allowed_origins.is_empty() {
            browser_command_line_args.push((
              "--remote-allow-origins".to_string(),
              Some(allowed_origins.join(",")),
            ));
          }
          log::warn!(
            "the Chrome DevTools protocol is listening on port {port}. Anything that can \
             reach it can read and rewrite every page this application shows."
          );
          true
        }
      }
    };
    // Chromium consults `SSLKEYLOGFILE` only when this switch is absent, and an empty
    // value creates no key logger, so this is how the variable is refused without writing
    // to the process environment. Appended before the application's own switches so an
    // application that deliberately passes `--ssl-key-log-file` still wins.
    if crate::environment::neutralizes_tls_key_log(debug_environment, tauri::is_dev()) {
      browser_command_line_args.push(("--ssl-key-log-file".to_string(), Some(String::new())));
    }

    // Pinned off whenever no transport was actually configured, including the rejected
    // port above: `RemoteDebuggingServer` consults this before it starts a server for
    // either transport, so it also refuses a switch that reaches Chromium another way.
    if !remote_debugging_enabled {
      global_preferences.insert(
        0,
        (
          crate::cef_impl::preferences::REMOTE_DEBUGGING_ALLOWED.to_string(),
          serde_json::Value::Bool(false),
        ),
      );
    }

    let cache_path = cache_path_override.unwrap_or_else(|| {
      let cache_base = dirs::cache_dir().unwrap_or_else(std::env::temp_dir);
      cache_base.join(&runtime_args.identifier).join("cef")
    });
    let _ = create_dir_all(&cache_path);

    // Force X11 usage on Linux.
    //
    // Applied to every process type rather than only the browser one: it is not certain
    // that Chromium propagates `ozone-platform` to the GPU process, and getting it wrong
    // there breaks rendering outright.
    #[cfg(any(
      target_os = "linux",
      target_os = "dragonfly",
      target_os = "freebsd",
      target_os = "netbsd",
      target_os = "openbsd"
    ))]
    {
      internal_command_line_args.push(("--ozone-platform".to_string(), Some("x11".to_string())));
      // CEF integration below uses XIDs for child windows/reparenting, so GDK must not honor an
      // inherited `GDK_BACKEND=wayland`. `set_allowed_backends` alone is not enough: GDK reads
      // `GDK_BACKEND` first and only intersects it with the allowed list, so an inherited
      // `wayland` would leave no backend to open a display with.
      //
      // SAFETY: `std::env::set_var` is only unsafe because another thread may be reading the
      // environment concurrently. This runs during runtime initialization, before any GTK, CEF or
      // Tauri thread that could read it has been spawned. Note the value is inherited by child
      // processes the app spawns later, which is intended for CEF's own subprocesses.
      unsafe { std::env::set_var("GDK_BACKEND", "x11") };
      gtk::gdk::set_allowed_backends("x11");
      event_loop_builder.with_gtk4();

      // the GTK pointers this runtime hands out are GTK 4 objects, whichever bindings the `tauri`
      // crate was compiled against.
      tauri_runtime::gtk::declare_version(tauri_runtime::gtk::Version::V4);
    }

    #[cfg(windows)]
    if let Some(hook) = runtime_args.msg_hook {
      use winit::platform::windows::EventLoopBuilderExtWindows;
      event_loop_builder.with_msg_hook(hook);
    }

    #[cfg(target_os = "macos")]
    event_loop_builder.with_default_menu(false);

    let event_loop = event_loop_builder
      .build()
      .map_err(|_| Error::CreateWindow)?;
    let proxy = event_loop.create_proxy();
    let (sender, receiver) = mpsc::channel();
    let context_initialized = Arc::new(AtomicBool::new(false));
    let cef_pump = CefExternalPump::new();
    let context = RuntimeContext {
      sender: sender.clone(),
      proxy: proxy.clone(),
      main_thread_id: std::thread::current().id(),
      next_window_id: Default::default(),
      next_webview_id: Default::default(),
      next_window_event_id: Default::default(),
      next_webview_event_id: Default::default(),
      current_dispatch: Default::default(),
      app_wide_theme: Default::default(),
      cef_pump,
      cache_path: Arc::new(cache_path.clone()),
      profile_preferences: Arc::new(profile_preferences),
      content_settings: Arc::new(content_settings),
      certificate_errors,
      devtools_allowed,
    };

    internal_command_line_args.push(("--no-first-run".to_string(), None));

    // Appended last so an application switch overrides a runtime default with the same
    // name: Chromium's command line keeps the last value appended for a given switch.
    browser_command_line_args.extend(command_line_args);

    // Shipped applications ignore Chromium switches passed on their own command line:
    // otherwise anyone able to launch the app can also launch it with
    // `--remote-debugging-port` and drive it over the DevTools protocol, or with
    // `--disable-web-security`, `--proxy-server`, `--host-resolver-rules` or
    // `--ssl-key-log-file`, all of which Chromium honours. CEF clears the command line
    // before applying `Settings` and before calling `on_before_command_line_processing`,
    // so the switches this runtime and the application configure still take effect, and
    // Tauri's own CLI parsing reads `std::env::args()`, which Chromium never touches.
    // The clear does break the *relaunch* deep link path, which
    // `TauriCefApp::on_before_command_line_processing` restores.
    let command_line_args_disabled = !(allow_chromium_command_line_args || tauri::is_dev());

    let mut app = TauriCefApp::build(TauriCefAppArgs {
      context: context.clone(),
      context_initialized: context_initialized.clone(),
      deep_link_schemes,
      restore_deep_link_arguments: command_line_args_disabled,
      internal_command_line_args,
      browser_command_line_args,
      disabled_features,
      enabled_features,
    });

    // Subprocesses already exited above, so this must be the browser process;
    // `execute_process` returns -1 there to signal normal startup should follow.
    let ret = cef::execute_process(
      Some(args.as_main_args()),
      Some(&mut app),
      std::ptr::null_mut(),
    );
    assert_eq!(
      ret, -1,
      "CEF browser process unexpectedly returned from execute_process"
    );

    // Chromium drops a `debug.log` next to the *main executable* when no log file is
    // configured, which for an installed application is often not even writable. Keep it
    // next to the rest of the runtime's state instead.
    let log_file = log_file.unwrap_or_else(|| cache_path.join("cef.log"));
    // CEF logs at INFO by default, which grows that file quickly in a long-running app.
    let log_severity = log_severity.unwrap_or(if tauri::is_dev() {
      LogSeverity::DEFAULT
    } else {
      LogSeverity::WARNING
    });

    let mut settings = cef::Settings {
      // Only this, never a `--no-sandbox` push of our own: CEF appends that switch itself
      // from the setting, before `on_before_command_line_processing` runs, so the setting
      // and the switch cannot end up disagreeing.
      no_sandbox: no_sandbox as std::os::raw::c_int,
      cache_path: cache_path.to_string_lossy().to_string().as_str().into(),
      command_line_args_disabled: command_line_args_disabled as std::os::raw::c_int,
      log_file: log_file.to_string_lossy().to_string().as_str().into(),
      log_severity,
      persist_session_cookies: persist_session_cookies as std::os::raw::c_int,
      external_message_pump: 1,
      ..Default::default()
    };

    if let Some(log_items) = log_items {
      settings.log_items = log_items;
    }

    // Left at CEF's defaults unless the application asked for something else: the bundler
    // only ships the `en-US` locale pak, so any other locale would leave Chromium without
    // its localized resources.
    if let Some(locale) = locale {
      settings.locale = locale.as_str().into();
    }

    // With neither of these set CEF appends `--lang=en-US` and derives the accept-language
    // list from it, so every user of every CEF application reports `navigator.language ===
    // "en-US"` and asks servers for English. The locale pak constraint above does not
    // apply here — this is a list of language codes, not a resource bundle — so the
    // runtime answers with what the user actually asked their system for.
    let accept_language_list =
      accept_language_list.or_else(crate::locale::system_accept_language_list);
    if let Some(accept_language_list) = accept_language_list {
      settings.accept_language_list = accept_language_list.as_str().into();
    }

    // `user_agent` wins over `user_agent_product` in CEF, so setting both is the
    // application contradicting itself; say so rather than silently dropping one.
    if let Some(user_agent) = &user_agent {
      settings.user_agent = user_agent.as_str().into();
      if user_agent_product.is_some() {
        log::warn!(
          "ignoring the CEF user agent product: Cef::user_agent replaces the whole User-Agent string, including the product token"
        );
      }
    } else if let Some(user_agent_product) = &user_agent_product {
      settings.user_agent_product = user_agent_product.as_str().into();
    }

    if let Some(javascript_flags) = &javascript_flags {
      settings.javascript_flags = javascript_flags.as_str().into();
    }
    if let Some(chrome_policy_id) = &chrome_policy_id {
      settings.chrome_policy_id = chrome_policy_id.as_str().into();
    }

    if let Some(callback) = settings_callback {
      callback(&mut settings);
    }
    if cef::initialize(
      Some(args.as_main_args()),
      Some(&settings),
      Some(&mut app),
      std::ptr::null_mut(),
    ) != 1
    {
      return Err(Error::WebviewRuntimeNotInstalled);
    }

    #[cfg(any(
      target_os = "linux",
      target_os = "dragonfly",
      target_os = "freebsd",
      target_os = "openbsd",
      target_os = "netbsd"
    ))]
    pre_cef_signals.restore();

    #[cfg(target_os = "macos")]
    let app_delegate = if !is_helper {
      use crate::platform::macos::AppDelegateEvent;

      let context_ = context.clone();
      let handler = Box::new(move |event| match event {
        AppDelegateEvent::TryTerminate => {
          let _ = context_.send_message(Message::RequestExit(0));
        }
        AppDelegateEvent::Reopen {
          has_visible_windows,
        } => {
          let _ = context_.send_message(Message::Reopen {
            has_visible_windows,
          });
        }
        AppDelegateEvent::AccessibilityChanged { enabled } => {
          let _ = context_.send_message(Message::AccessibilityChanged { enabled });
        }
        AppDelegateEvent::OpenURLs { urls } => {
          let _ = context_.send_message(Message::Opened(urls));
        }
      });
      let app_delegate = crate::platform::macos::set_application_event_handler(handler);
      Some(app_delegate)
    } else {
      None
    };

    // Wait for the CEF context to initialize before returning, so that the runtime is ready to create browsers.
    while !context_initialized.load(Ordering::SeqCst) {
      context.cef_pump.do_work();
      std::thread::sleep(Duration::from_millis(1));
    }

    // Local state exists only once the context is initialized, and
    // `preference_manager_get_global` has to be called on the browser UI thread — which
    // is this one, since the runtime drives CEF from the main thread through an external
    // message pump.
    crate::cef_impl::preferences::apply_global_preferences(&global_preferences);

    Ok(Self {
      event_loop,
      receiver,
      context,
      scheme_registry: Default::default(),
      #[cfg(target_os = "macos")]
      _app_delegate: app_delegate,
    })
  }
}

impl<T: UserEvent> Runtime<T> for CefRuntime<T> {
  type WindowDispatcher = CefWindowDispatcher<T>;
  type WebviewDispatcher = CefWebviewDispatcher<T>;
  type Handle = CefRuntimeHandle<T>;
  type EventLoopProxy = EventProxy<T>;
  type RuntimeWebviewAttributes = CefWebviewAttributes;
  type Webview = Webview;
  type RuntimeInitAttrs = Cef;
  type WindowOpener = NewWindowOpener;

  fn new(args: RuntimeInitArgs<Self::RuntimeInitAttrs>) -> Result<Self> {
    Self::init(EventLoopBuilder::default(), args)
  }

  #[cfg(any(
    windows,
    target_os = "linux",
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
  ))]
  fn new_any_thread(args: RuntimeInitArgs<Self::RuntimeInitAttrs>) -> Result<Self> {
    let mut event_loop_builder = EventLoopBuilder::default();
    event_loop_builder.with_any_thread(true);
    Self::init(event_loop_builder, args)
  }

  fn create_proxy(&self) -> Self::EventLoopProxy {
    EventProxy {
      context: self.context.clone(),
    }
  }

  fn handle(&self) -> Self::Handle {
    CefRuntimeHandle {
      context: self.context.clone(),
    }
  }

  fn create_window<F: Fn(RawWindow<'_>) + Send + 'static>(
    &self,
    pending: PendingWindow<T, Self>,
    after_window_creation: Option<F>,
  ) -> Result<DetachedWindow<T, Self>> {
    create_window_detached(&self.context, pending, after_window_creation)
  }

  fn create_webview(
    &self,
    window_id: WindowId,
    pending: PendingWebview<T, Self>,
  ) -> Result<DetachedWebview<T, Self>> {
    create_webview_detached(&self.context, window_id, pending)
  }

  fn primary_monitor(&self) -> Option<Monitor> {
    event_loop_getter!(self, PrimaryMonitor)
      .flatten()
      .ok()
      .unwrap_or_default()
  }

  fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
    let (tx, rx) = mpsc::channel();
    self
      .context
      .send_message(Message::EventLoop(EventLoopMessage::MonitorFromPoint(
        tx, x, y,
      )))
      .and_then(|_| rx.recv().map_err(|_| Error::FailedToReceiveMessage))
      .ok()?
      .ok()
      .unwrap_or_default()
  }

  fn available_monitors(&self) -> Vec<Monitor> {
    event_loop_getter!(self, AvailableMonitors)
      .flatten()
      .ok()
      .unwrap_or_default()
  }

  fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
    event_loop_getter!(self, CursorPosition)?
  }

  fn set_theme(&self, theme: Option<Theme>) {
    let message = Message::EventLoop(EventLoopMessage::SetTheme(theme));
    let _ = self.context.send_message(message);
  }

  #[cfg(target_os = "macos")]
  fn set_activation_policy(&mut self, activation_policy: tauri_runtime::ActivationPolicy) {
    let message = Message::EventLoop(EventLoopMessage::SetActivationPolicy(activation_policy));
    let _ = self.context.send_message(message);
  }

  #[cfg(target_os = "macos")]
  fn set_dock_visibility(&mut self, visible: bool) {
    let message = Message::EventLoop(EventLoopMessage::SetDockVisibility(visible));
    let _ = self.context.send_message(message);
  }

  #[cfg(target_os = "macos")]
  fn show(&self) {
    let message = Message::EventLoop(EventLoopMessage::ShowApplication);
    let _ = self.context.send_message(message);
  }

  #[cfg(target_os = "macos")]
  fn hide(&self) {
    let message = Message::EventLoop(EventLoopMessage::HideApplication);
    let _ = self.context.send_message(message);
  }

  fn set_device_event_filter(&mut self, filter: DeviceEventFilter) {
    self
      .event_loop
      .listen_device_events(device_event_filter_to_winit(filter));
  }

  fn run_iteration<F: FnMut(RunEvent<T>) + 'static>(&mut self, mut callback: F) {
    while let Ok(message) = self.receiver.try_recv() {
      if let Message::UserEvent(event) = message {
        callback(RunEvent::UserEvent(event));
      }
    }
    self.context.cef_pump.do_work();
    callback(RunEvent::MainEventsCleared);
  }

  fn run_return<F: FnMut(RunEvent<T>) + 'static>(self, callback: F) -> i32 {
    self.run(callback);
    // TODO: return the exit code from the runtime, if possible. For now, always return 0
    0
  }

  fn run<F: FnMut(RunEvent<T>) + 'static>(self, callback: F) {
    let app = WinitCefApp::new(
      self.context,
      self.receiver,
      Box::new(callback),
      self.scheme_registry,
    );
    let _ = self.event_loop.run_app(app);
    cef::shutdown();
  }
}

#[cfg(test)]
mod configuration_tests {
  use super::*;

  #[test]
  fn a_fixed_proxy_is_spelled_the_way_chromium_spells_it() {
    let preference = ProxyConfig::FixedServers {
      server: "socks5://127.0.0.1:9050".to_string(),
      bypass_list: Some("*.internal".to_string()),
    }
    .to_preference();

    assert_eq!(
      preference,
      serde_json::json!({
        "mode": "fixed_servers",
        "server": "socks5://127.0.0.1:9050",
        "bypass_list": "*.internal",
      })
    );
  }

  #[test]
  fn a_fixed_proxy_without_a_bypass_list_omits_the_key() {
    // Chromium rejects the whole `proxy` dictionary when it carries a key the mode does
    // not accept, so an absent bypass list must be absent rather than empty.
    let preference = ProxyConfig::FixedServers {
      server: "http://proxy:8080".to_string(),
      bypass_list: None,
    }
    .to_preference();

    assert_eq!(
      preference,
      serde_json::json!({ "mode": "fixed_servers", "server": "http://proxy:8080" })
    );
  }

  #[test]
  fn the_modeless_proxy_configurations_carry_only_a_mode() {
    for (config, mode) in [
      (ProxyConfig::System, "system"),
      (ProxyConfig::Direct, "direct"),
      (ProxyConfig::AutoDetect, "auto_detect"),
    ] {
      assert_eq!(config.to_preference(), serde_json::json!({ "mode": mode }));
    }
    assert_eq!(
      ProxyConfig::PacScript {
        url: "http://wpad/proxy.pac".to_string()
      }
      .to_preference(),
      serde_json::json!({ "mode": "pac_script", "pac_url": "http://wpad/proxy.pac" })
    );
  }

  #[test]
  fn the_default_policies_append_no_switch_at_all() {
    // Chromium's own default is not one of the named values, so `Default` has to mean
    // "leave the switch off" rather than "pass the default explicitly".
    assert_eq!(AutoplayPolicy::default().as_switch_value(), None);
    assert_eq!(WebRtcIpHandling::default().as_switch_value(), None);
  }

  #[test]
  fn the_named_policies_use_chromiums_own_spelling() {
    assert_eq!(
      AutoplayPolicy::NoUserGestureRequired.as_switch_value(),
      Some("no-user-gesture-required"),
      "autoplay values are hyphenated"
    );
    assert_eq!(
      WebRtcIpHandling::DisableNonProxiedUdp.as_switch_value(),
      Some("disable_non_proxied_udp"),
      "WebRTC values are underscored"
    );
  }

  #[test]
  fn remote_debugging_is_off_by_default() {
    assert_eq!(RemoteDebugging::default(), RemoteDebugging::Disabled);
  }

  #[test]
  fn the_defaults_are_the_conservative_ones() {
    let cef = Cef::default();
    assert_eq!(cef.devtools, DevToolsPolicy::Auto);
    assert_eq!(cef.debug_environment, DebugEnvironment::Auto);
    assert_eq!(cef.sandbox, SandboxPolicy::Auto);
    assert!(
      !cef.allow_chromium_command_line_args,
      "a shipped application must ignore Chromium switches on its command line"
    );
    assert!(
      !cef.persist_session_cookies,
      "a session cookie is dropped on exit, as it is in a browser"
    );
    assert_eq!(
      cef.certificate_errors,
      CertificateErrorPolicy::ChromeInterstitial
    );
  }

  #[test]
  fn a_typed_option_is_just_a_preference() {
    // The typed options and the escape hatch write the same store, so an application can
    // reach anything the typed set does not cover.
    let cef = Cef::default().safe_browsing(false).spell_checking(false);
    assert!(
      cef
        .profile_preferences
        .iter()
        .any(|(name, value)| name == "safebrowsing.enabled" && value == &serde_json::json!(false))
    );
    assert!(
      cef
        .profile_preferences
        .iter()
        .any(|(name, value)| name == "browser.enable_spellchecking"
          && value == &serde_json::json!(false))
    );
  }

  #[test]
  fn a_later_preference_wins_over_an_earlier_one() {
    // They are applied in order, so the last one written is the one that sticks.
    let cef = Cef::default()
      .safe_browsing(false)
      .profile_preference("safebrowsing.enabled", true);
    let values: Vec<_> = cef
      .profile_preferences
      .iter()
      .filter(|(name, _)| name == "safebrowsing.enabled")
      .map(|(_, value)| value.clone())
      .collect();
    assert_eq!(
      values,
      [serde_json::json!(false), serde_json::json!(true)],
      "both are kept, in call order, so the application's last word wins"
    );
  }
}

#[cfg(test)]
mod deep_link_argument_tests {
  use super::deep_link_arguments;

  fn schemes() -> Vec<String> {
    vec!["myapp".to_string(), "my-other-app".to_string()]
  }

  fn filter(args: &[&str]) -> Vec<String> {
    deep_link_arguments(args.iter().map(|arg| (*arg).to_string()), &schemes())
  }

  #[test]
  fn keeps_configured_deep_links_in_order() {
    assert_eq!(
      filter(&["myapp://open/one", "my-other-app://open/two"]),
      vec![
        "myapp://open/one".to_string(),
        "my-other-app://open/two".to_string(),
      ]
    );
  }

  #[test]
  fn drops_everything_that_is_not_a_configured_deep_link() {
    // The lockdown exists so that none of these reach Chromium's command line, and a
    // URL with an unconfigured scheme is not this application's deep link either.
    assert!(
      filter(&[
        "--remote-debugging-port=9222",
        "--disable-web-security",
        "/home/user/document.txt",
        "not a url",
        "",
        "https://example.com",
        "otherapp://open",
      ])
      .is_empty()
    );
  }

  #[test]
  fn an_empty_scheme_list_keeps_nothing() {
    assert!(deep_link_arguments(["myapp://open".to_string()], &[]).is_empty());
  }

  #[test]
  fn scheme_matching_is_exact() {
    // `on_already_running_app_relaunch` compares schemes the same way, so anything
    // matched loosely here would be re-appended and then ignored on the other end.
    // `Url::parse` lowercases the scheme it reports, hence the upper-case spelling
    // below still matching.
    assert_eq!(filter(&["MYAPP://open"]), vec!["MYAPP://open".to_string()]);
    assert!(filter(&["myapp2://open", "myap://open"]).is_empty());
  }
}