File: input_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (2507 lines) | stat: -rw-r--r-- 100,397 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/input/input_handler.h"

#include <string>
#include <utility>
#include <vector>

#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/types/optional_ref.h"
#include "build/build_config.h"
#include "cc/base/features.h"
#include "cc/input/browser_controls_offset_manager.h"
#include "cc/input/browser_controls_offset_tag_modifications.h"
#include "cc/input/scroll_elasticity_helper.h"
#include "cc/input/scroll_snap_data.h"
#include "cc/input/scroll_utils.h"
#include "cc/input/scrollbar_controller.h"
#include "cc/input/snap_selection_strategy.h"
#include "cc/layers/viewport.h"
#include "cc/trees/compositor_commit_data.h"
#include "cc/trees/latency_info_swap_promise_monitor.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "cc/trees/layer_tree_impl.h"
#include "cc/trees/layer_tree_settings.h"
#include "cc/trees/property_tree.h"
#include "cc/trees/scroll_node.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace cc {

namespace {

enum SlowScrollMetricThread { MAIN_THREAD, CC_THREAD };

InputHandlerClient::ScrollEventDispatchMode GetScrollEventDispatchMode() {
  const std::string mode_name = ::features::kScrollEventDispatchMode.Get();
  if (mode_name ==
      ::features::kScrollEventDispatchModeDispatchScrollEventsImmediately) {
    return InputHandlerClient::ScrollEventDispatchMode::
        kDispatchScrollEventsImmediately;
  } else if (mode_name ==
             ::features::
                 kScrollEventDispatchModeUseScrollPredictorForEmptyQueue) {
    return InputHandlerClient::ScrollEventDispatchMode::
        kUseScrollPredictorForEmptyQueue;
  } else if (mode_name ==
             ::features::
                 kScrollEventDispatchModeUseScrollPredictorForDeadline) {
    return InputHandlerClient::ScrollEventDispatchMode::
        kUseScrollPredictorForDeadline;
  } else if (mode_name ==
             ::features::
                 kScrollEventDispatchModeDispatchScrollEventsUntilDeadline) {
    return InputHandlerClient::ScrollEventDispatchMode::
        kDispatchScrollEventsUntilDeadline;
  }

  return InputHandlerClient::ScrollEventDispatchMode::kEnqueueScrollEvents;
}

std::string GetScrollInputTypeSuffix(ui::ScrollInputType input_type) {
  switch (input_type) {
    case ui::ScrollInputType::kTouchscreen:
      return "Touchscreen";
    case ui::ScrollInputType::kWheel:
      return "Wheel";
    case ui::ScrollInputType::kAutoscroll:
      return "Autoscroll";
    case ui::ScrollInputType::kScrollbar:
      return "Scrollbar";
  }
  NOTREACHED();
}

}  // namespace

// The minimum amount of scroll delta that must be consumed before we consider
// a scroll to have happened.
// TODO(tdresser): Use a more rational epsilon. See crbug.com/510550 for
// details.
const float kScrollEpsilon = 0.1f;

// static
base::WeakPtr<InputHandler> InputHandler::Create(
    CompositorDelegateForInput& compositor_delegate) {
  auto input_handler = std::make_unique<InputHandler>(compositor_delegate);
  base::WeakPtr<InputHandler> input_handler_weak = input_handler->AsWeakPtr();
  compositor_delegate.BindToInputHandler(std::move(input_handler));
  return input_handler_weak;
}

InputHandler::InputHandler(CompositorDelegateForInput& compositor_delegate)
    : compositor_delegate_(compositor_delegate),
      scrollbar_controller_(std::make_unique<ScrollbarController>(
          &compositor_delegate_->GetImplDeprecated())) {}

InputHandler::~InputHandler() = default;

//
// =========== InputHandler Interface
//

base::WeakPtr<InputHandler> InputHandler::AsWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

void InputHandler::BindToClient(InputHandlerClient* client) {
  DCHECK(input_handler_client_ == nullptr);
  input_handler_client_ = client;
  input_handler_client_->SetPrefersReducedMotion(prefers_reduced_motion_);
  if (base::FeatureList::IsEnabled(::features::kWaitForLateScrollEvents)) {
    input_handler_client_->SetScrollEventDispatchMode(
        GetScrollEventDispatchMode(),
        ::features::kWaitForLateScrollEventsDeadlineRatio.Get());
  }
}

InputHandler::ScrollStatus InputHandler::ScrollBegin(ScrollState* scroll_state,
                                                     ui::ScrollInputType type) {
  DCHECK(scroll_state);
  DCHECK(scroll_state->delta_x() == 0 && scroll_state->delta_y() == 0);

  InputHandler::ScrollStatus scroll_status;
  TRACE_EVENT0("cc", "InputHandler::ScrollBegin");

  // If this ScrollBegin is non-animated then ensure we cancel any ongoing
  // animated scrolls.
  // TODO(bokan): This preserves existing behavior when we had diverging
  // paths for animated and non-animated scrolls but we should probably
  // decide when it best makes sense to cancel a scroll animation (maybe
  // ScrollBy is a better place to do it).
  if (scroll_state->delta_granularity() ==
      ui::ScrollGranularity::kScrollByPrecisePixel) {
    if (ScrollNode* animating_node =
            GetAnimatingNodeForCurrentScrollingNode()) {
      compositor_delegate_->ScrollAnimationAbort(animating_node->element_id);
    }
    if (ScrollNode* scroll_node = CurrentlyScrollingNode()) {
      ClearAnimatingSnapTargetsForElement(scroll_node->element_id);
    }
  }

  if (CurrentlyScrollingNode() && type == latched_scroll_type_) {
    // It's possible we haven't yet cleared the CurrentlyScrollingNode if we
    // received a GSE but we're still animating the last scroll. If that's the
    // case, we'll simply un-defer the GSE and continue latching to the same
    // node.
    DCHECK(deferred_scroll_end_);
    deferred_scroll_end_ = false;
    scroll_status.raster_inducing =
        GetScrollTree().CanRealizeScrollsOnPendingTree(
            *CurrentlyScrollingNode());
    return scroll_status;
  }

  ScrollNode* scrolling_node = nullptr;

  // TODO(bokan): ClearCurrentlyScrollingNode shouldn't happen in ScrollBegin,
  // this should only happen in ScrollEnd. We should DCHECK here that the state
  // is cleared instead. https://crbug.com/1016229
  //
  // TODO(b/329346768): Validate that this is no longer needed.
  ClearCurrentlyScrollingNode();

  ElementId target_element_id = scroll_state->target_element_id();
  ScrollTree& scroll_tree = GetScrollTree();

  if (target_element_id && (!scroll_state->main_thread_hit_tested_reasons() ||
                            scroll_state->is_scrollbar_interaction())) {
    TRACE_EVENT_INSTANT0("cc", "Latched scroll node provided",
                         TRACE_EVENT_SCOPE_THREAD);
    // If the caller passed in an element_id we can skip all the hit-testing
    // bits and provide a node straight-away.
    scrolling_node = scroll_tree.FindNodeFromElementId(target_element_id);
  } else {
    ScrollNode* starting_node = nullptr;
    if (target_element_id) {
      TRACE_EVENT_INSTANT0("cc", "Unlatched scroll node provided",
                           TRACE_EVENT_SCOPE_THREAD);
      // We had an element id but we should still perform the walk up the
      // scroll tree from the targeted node to latch to a scroller that can
      // scroll in the given direction. This mode is only used when scroll
      // unification is enabled and the targeted scroller comes back from a
      // main thread hit test.
      DCHECK(scroll_state->main_thread_hit_tested_reasons());
      starting_node = scroll_tree.FindNodeFromElementId(target_element_id);

      if (!starting_node) {
        // The main thread sent us an element_id that the compositor doesn't
        // have a scroll node for. This can happen in some racy conditions, a
        // freshly created scroller hasn't yet been committed or a
        // scroller-destroying commit beats the hit test back to the compositor
        // thread. However, these cases shouldn't be user perceptible.
        scroll_status.thread = InputHandler::ScrollThread::kScrollIgnored;
        return scroll_status;
      }
    } else {  // !target_element_id
      TRACE_EVENT_INSTANT0("cc", "Hit Testing for ScrollNode",
                           TRACE_EVENT_SCOPE_THREAD);
      gfx::Point viewport_point(scroll_state->position_x(),
                                scroll_state->position_y());
      gfx::PointF device_viewport_point =
          gfx::ScalePoint(gfx::PointF(viewport_point),
                          compositor_delegate_->DeviceScaleFactor());

      // The client should have discarded the scroll when the hit test came back
      // with an invalid element id.
      CHECK(!scroll_state->main_thread_hit_tested_reasons());

      ScrollHitTestResult scroll_hit_test =
          HitTestScrollNode(device_viewport_point);

      if (!scroll_hit_test.hit_test_successful) {
        // This result tells the client that the compositor doesn't have
        // enough information to target this scroll. The client should
        // perform a hit test in Blink and call this method again, with the
        // ElementId of the hit-tested scroll node.
        TRACE_EVENT_INSTANT0("cc", "Request Main Thread Hit Test",
                             TRACE_EVENT_SCOPE_THREAD);
        scroll_status.thread = InputHandler::ScrollThread::kScrollOnImplThread;
        DCHECK(scroll_hit_test.main_thread_hit_test_reasons);
        scroll_status.main_thread_hit_test_reasons =
            scroll_hit_test.main_thread_hit_test_reasons;
        CHECK(MainThreadScrollingReason::AreHitTestReasons(
            scroll_status.main_thread_hit_test_reasons));
        return scroll_status;
      }

      starting_node = scroll_hit_test.scroll_node;
    }

    // The above finds the ScrollNode that's hit by the given point but we
    // still need to walk up the scroll tree looking for the first node that
    // can consume delta from the scroll state.
    scrolling_node = FindNodeToLatch(scroll_state, starting_node, type);

    // When using fluent overlay scrollbars and a subscroller receives a scroll
    // event, but the scroll chains up to a different node, we want to flash the
    // scrollbars to show that the node is scrollable.
    if (scrolling_node &&
        compositor_delegate_->GetSettings().enable_fluent_overlay_scrollbar &&
        scrolling_node->element_id != starting_node->element_id) {
      compositor_delegate_->WillScrollContent(starting_node->element_id);
    }
  }

  if (!scrolling_node) {
    if (compositor_delegate_->GetSettings().is_for_embedded_frame) {
      // OOPIFs or fenced frames never have a viewport scroll node so if we
      // can't scroll we need to be bubble up to the parent frame. This happens
      // by returning kScrollIgnored.
      TRACE_EVENT_INSTANT0("cc",
                           "Ignored - No ScrollNode (OOPIF or FencedFrame)",
                           TRACE_EVENT_SCOPE_THREAD);
    } else {
      // If we didn't hit a layer above we'd usually fallback to the
      // viewport scroll node. However, there may not be one if a scroll
      // is received before the root layer has been attached. Chrome now
      // drops input until the first commit is received so this probably
      // can't happen in a typical browser session but there may still be
      // configurations where input is allowed prior to a commit.
      TRACE_EVENT_INSTANT0("cc", "Ignored - No ScrollNode",
                           TRACE_EVENT_SCOPE_THREAD);
    }
    scroll_status.thread = InputHandler::ScrollThread::kScrollIgnored;
    return scroll_status;
  }

  DCHECK_EQ(scroll_status.thread,
            InputHandler::ScrollThread::kScrollOnImplThread);
  DCHECK(scrolling_node);

  ActiveTree().SetCurrentlyScrollingNode(scrolling_node);
  scroll_status.main_thread_repaint_reasons =
      scroll_tree.GetMainThreadRepaintReasons(*scrolling_node);
  CHECK(MainThreadScrollingReason::AreRepaintReasons(
      scroll_status.main_thread_repaint_reasons));
  scroll_status.raster_inducing =
      scroll_tree.CanRealizeScrollsOnPendingTree(*scrolling_node);

  DidLatchToScroller(*scroll_state, type);

  // If the viewport is scrolling and it cannot consume any delta hints, the
  // scroll event will need to get bubbled if the viewport is for a guest or
  // oopif.
  if (GetViewport().ShouldScroll(*CurrentlyScrollingNode())) {
    outer_viewport_consumed_delta_ = false;
    inner_viewport_consumed_delta_ = false;
    if (!GetViewport().CanScroll(*CurrentlyScrollingNode(), *scroll_state)) {
      // TODO(crbug.com/40735567): This is a temporary workaround for GuestViews
      // as they create viewport nodes and want to bubble scroll if the
      // viewport cannot scroll in the given delta directions. There should be
      // a parameter to ThreadInputHandler to specify whether unused delta is
      // consumed by the viewport or bubbles to the parent.
      scroll_status.viewport_cannot_scroll = true;
    }
  }

  return scroll_status;
}

InputHandler::ScrollStatus InputHandler::RootScrollBegin(
    ScrollState* scroll_state,
    ui::ScrollInputType type) {
  TRACE_EVENT0("cc", "InputHandler::RootScrollBegin");
  if (!OuterViewportScrollNode()) {
    InputHandler::ScrollStatus scroll_status;
    scroll_status.thread = InputHandler::ScrollThread::kScrollIgnored;
    return scroll_status;
  }

  scroll_state->data()->set_current_native_scrolling_element(
      OuterViewportScrollNode()->element_id);
  InputHandler::ScrollStatus scroll_status = ScrollBegin(scroll_state, type);

  // Since we provided an ElementId, there should never be a need to perform a
  // hit test.
  DCHECK(!scroll_status.main_thread_hit_test_reasons);

  return scroll_status;
}

InputHandlerScrollResult InputHandler::ScrollUpdate(
    ScrollState scroll_state,
    base::TimeDelta delayed_by) {
  // The current_native_scrolling_element should only be set for ScrollBegin.
  DCHECK(!scroll_state.data()->current_native_scrolling_element());
  TRACE_EVENT2("cc", "InputHandler::ScrollUpdate", "dx", scroll_state.delta_x(),
               "dy", scroll_state.delta_y());

  if (!CurrentlyScrollingNode())
    return InputHandlerScrollResult();

  const ScrollNode& scroll_node = *CurrentlyScrollingNode();
  last_scroll_update_state_ = scroll_state;

  // Snap on update if interacting with the scrollbar track or arrow buttons.
  // Interactions with the scrollbar thumb have kScrollByPrecisePixel
  // granularity.
  if (scroll_state.is_scrollbar_interaction() &&
      scroll_state.delta_granularity() !=
          ui::ScrollGranularity::kScrollByPrecisePixel) {
    AdjustScrollDeltaForScrollbarSnap(scroll_state);
  }

  gfx::Vector2dF resolvedScrollDelta = ResolveScrollGranularityToPixels(
      scroll_node,
      gfx::Vector2dF(scroll_state.delta_x(), scroll_state.delta_y()),
      scroll_state.delta_granularity());

  scroll_state.data()->delta_x = resolvedScrollDelta.x();
  scroll_state.data()->delta_y = resolvedScrollDelta.y();
  // The decision of whether or not we'll animate a scroll comes down to
  // whether the granularity is specified in precise pixels or not. Thus we
  // need to preserve a precise granularity if that's what was specified; all
  // others are animated and so can be resolved to regular pixels.
  if (scroll_state.delta_granularity() !=
      ui::ScrollGranularity::kScrollByPrecisePixel) {
    scroll_state.data()->delta_granularity =
        ui::ScrollGranularity::kScrollByPixel;
  }

  compositor_delegate_->WillScrollContent(scroll_node.element_id);

  float initial_top_controls_offset =
      compositor_delegate_->GetBrowserControlsTopOffset();

  ScrollLatchedScroller(scroll_state, delayed_by);

  delta_consumed_for_scroll_gesture_ |=
      scroll_state.delta_consumed_for_scroll_sequence();

  // Mark the input as having caused a scroll for the purposes of metrics even
  // if the scroll only affected browser controls.
  bool did_scroll_anything =
      std::abs(scroll_state.delta_x() - resolvedScrollDelta.x()) >
          kScrollEpsilon ||
      std::abs(scroll_state.delta_y() - resolvedScrollDelta.y()) >
          kScrollEpsilon;
  if (did_scroll_anything) {
    compositor_delegate_->DidScrollForMetrics();
  }

  bool did_scroll_content_x = scroll_state.caused_scroll_x();
  bool did_scroll_content_y = scroll_state.caused_scroll_y();
  bool did_scroll_content = did_scroll_content_x || did_scroll_content_y;
  if (did_scroll_content) {
    bool is_animated_scroll = ShouldAnimateScroll(scroll_state);
    compositor_delegate_->DidScrollContent(
        scroll_node.element_id, is_animated_scroll, resolvedScrollDelta);
  }

  SetNeedsCommit();

  // Scrolling along an axis resets accumulated root overscroll for that axis.
  if (did_scroll_content_x) {
    accumulated_root_overscroll_.set_x(0);
  }
  if (did_scroll_content_y) {
    accumulated_root_overscroll_.set_y(0);
  }

  gfx::Vector2dF unused_root_delta;
  if (GetViewport().ShouldScroll(scroll_node)) {
    unused_root_delta =
        gfx::Vector2dF(scroll_state.delta_x(), scroll_state.delta_y());
  }

  // When inner viewport is unscrollable, disable overscrolls.
  if (auto* inner_viewport_scroll_node = InnerViewportScrollNode()) {
    unused_root_delta =
        UserScrollableDelta(*inner_viewport_scroll_node, unused_root_delta);
  }

  accumulated_root_overscroll_ += unused_root_delta;

  bool did_scroll_top_controls =
      initial_top_controls_offset !=
      compositor_delegate_->GetBrowserControlsTopOffset();

  InputHandlerScrollResult scroll_result;
  scroll_result.did_scroll = did_scroll_content || did_scroll_top_controls;
  scroll_result.did_overscroll_root = !unused_root_delta.IsZero();
  scroll_result.accumulated_root_overscroll = accumulated_root_overscroll_;
  scroll_result.unused_scroll_delta = unused_root_delta;
  scroll_result.overscroll_behavior =
      scroll_state.is_scroll_chain_cut()
          ? OverscrollBehavior(OverscrollBehavior::Type::kNone)
          : ActiveTree().overscroll_behavior();

  if (scroll_result.did_scroll) {
    // Scrolling can change the root scroll offset, so inform the synchronous
    // input handler.
    UpdateRootLayerStateForSynchronousInputHandler();
  }

  scroll_result.current_visual_offset = GetVisualScrollOffset(scroll_node);
  float scale_factor = ActiveTree().page_scale_factor_for_scroll();
  scroll_result.current_visual_offset.Scale(scale_factor);

  if (GetScrollTree().ShouldRealizeScrollsOnMain(scroll_node)) {
    scroll_result.needs_main_thread_repaint = true;
  }

  // Run animations which need to respond to updated scroll offset.
  compositor_delegate_->TickScrollAnimations();

  return scroll_result;
}

void InputHandler::AdjustScrollDeltaForScrollbarSnap(
    ScrollState& scroll_state) {
  ScrollNode* scroll_node = CurrentlyScrollingNode();
  if (!scroll_node || !scroll_node->snap_container_data)
    return;

  gfx::PointF current_position = GetVisualScrollOffset(*scroll_node);
  std::unique_ptr<SnapSelectionStrategy> strategy;
  const SnapContainerData& data = scroll_node->snap_container_data.value();
  if (scroll_state.delta_granularity() ==
      ui::ScrollGranularity::kScrollByPage) {
    strategy = SnapSelectionStrategy::CreateForPageScroll(
        current_position,
        gfx::Vector2dF(scroll_state.delta_x(), scroll_state.delta_y()),
        PageSize(*scroll_node),
        /*use_fractional_offsets=*/true);
  } else {
    // Ideally, scrollbar track and arrow interactions would always have
    // kScrollByPage and kScrollByLine, respectively. Native scrollbars have
    // kScrollByPixel granularity currently.
    strategy = SnapSelectionStrategy::CreateForDirection(
        current_position,
        ResolveScrollGranularityToPixels(
            *scroll_node,
            gfx::Vector2dF(scroll_state.delta_x(), scroll_state.delta_y()),
            scroll_state.delta_granularity()),
        /*use_fractional_offsets=*/true);
  }

  SnapPositionData snap = data.FindSnapPosition(*strategy);
  if (snap.type == SnapPositionData::Type::kNone) {
    return;
  }

  scroll_state.data()->delta_x = snap.position.x() - current_position.x();
  scroll_state.data()->delta_y = snap.position.y() - current_position.y();
  scroll_state.data()->delta_granularity =
      ui::ScrollGranularity::kScrollByPixel;
}

void InputHandler::InsertPendingScrollendContainer(
    const ElementId& element_id) {
  ScrollNode* outer_viewport_scroll_node = OuterViewportScrollNode();
  // Ensure that we insert the outer viewport scroll node only if it actually
  // consumed a delta.
  if ((!outer_viewport_scroll_node ||
       outer_viewport_scroll_node->element_id != element_id) ||
      outer_viewport_consumed_delta_) {
    pending_scrollend_containers_.insert(element_id);
  }

  if (outer_viewport_scroll_node) {
    // Reset {inner,outer}_consumed_delta_ only if the element for which we
    // know the scroll has ended, |element_id|, corresponds to the viewport
    // (which would be the outer viewport).
    if (outer_viewport_scroll_node->element_id == element_id) {
      if (outer_viewport_consumed_delta_) {
        outer_viewport_consumed_delta_ = false;
      }
      if (inner_viewport_consumed_delta_) {
        pending_scrollend_containers_.insert(
            InnerViewportScrollNode()->element_id);
        inner_viewport_consumed_delta_ = false;
      }
    }
  }
}

void InputHandler::ScrollEnd(bool should_snap) {
  ScrollEnd(nullptr, should_snap);
}

void InputHandler::ScrollEnd(ScrollNode* scroll_node, bool should_snap) {
  ScrollNode* latched_node = CurrentlyScrollingNode();

  auto end_of_scroll_cleanup = [&]() {
    compositor_delegate_->ScrollEnd();
    deferred_scroll_end_ = false;
    snap_fling_state_ = kNoFling;
    snap_strategy_.reset();
  };

  // If |scroll_node| exists, it, and not |CurrentlyScrollingNode()|, is the
  // ScrollNode for which ScrollEnd is being invoked.
  if (scroll_node && scroll_node != latched_node) {
    // This call to ScrollEnd marks the end of a snap animation on a ScrollNode
    // we are no longer latched to.
    DCHECK(features::MultiImplOnlyScrollAnimationsSupported());
    DCHECK(!should_snap);

    InsertPendingScrollendContainer(scroll_node->element_id);
    // Only reset scrollbar controller and tell browser controls about this
    // ScrollEnd if we haven't latched onto and are actively scrolling something
    // else.
    if (!latched_node) {
      scrollbar_controller_->ResetState();
      end_of_scroll_cleanup();
    }
    snap_animation_data_map_.erase(scroll_node->element_id);
  } else if (latched_node) {
    scrollbar_controller_->ResetState();

    // Note that if we deferred the scroll end then we should not snap. We will
    // snap once we deliver the deferred scroll end.
    if (GetAnimatingNodeForCurrentScrollingNode()) {
      DCHECK(!deferred_scroll_end_);
      deferred_scroll_end_ = true;
      return;
    }

    if (should_snap && SnapAtScrollEnd(SnapReason::kGestureScrollEnd)) {
      deferred_scroll_end_ = true;
      return;
    }

    DCHECK(latched_scroll_type_.has_value());

    // Only indicate that the scroll gesture ended if scrolling actually
    // occurred so that we don't fire a "scrollend" event.
    if (did_scroll_x_for_scroll_gesture_ || did_scroll_y_for_scroll_gesture_) {
      scroll_gesture_did_end_ = true;
      if (features::MultiImplOnlyScrollAnimationsSupported()) {
        InsertPendingScrollendContainer(latched_node->element_id);
      }
    }

    end_of_scroll_cleanup();
    snap_animation_data_map_.erase(latched_node->element_id);
    ClearCurrentlyScrollingNode();
  } else {
    scrollbar_controller_->ResetState();
    return;
  }

  SetNeedsCommit();
}

void InputHandler::RecordScrollBegin(
    ui::ScrollInputType input_type,
    ScrollBeginThreadState scroll_start_state) {
  last_scroll_begin_time_ = base::TimeTicks::Now();

  auto tracker_type = GetTrackerTypeForScroll(input_type);
  DCHECK_NE(tracker_type, FrameSequenceTrackerType::kMaxType);

  // The main-thread is the 'scrolling thread' if:
  //   (1) the scroll is driven by the main thread, or
  //   (2) the scroll is driven by the compositor, but blocked on the main
  //       thread.
  // Otherwise, the compositor-thread is the 'scrolling thread'.
  // TODO(crbug.com/40122138): We should also count 'main thread' as the
  // 'scrolling thread' if the layer being scrolled has scroll-event handlers.
  FrameInfo::SmoothEffectDrivingThread scrolling_thread;
  switch (scroll_start_state) {
    case ScrollBeginThreadState::kScrollingOnCompositor:
      scrolling_thread = FrameInfo::SmoothEffectDrivingThread::kCompositor;
      break;
    case ScrollBeginThreadState::kScrollingOnMain:
    case ScrollBeginThreadState::kScrollingOnCompositorBlockedOnMain:
    case ScrollBeginThreadState::kRasterInducingScrollBlockedOnMain:
      scrolling_thread = FrameInfo::SmoothEffectDrivingThread::kMain;
      break;
    case ScrollBeginThreadState::kRasterInducingScroll:
      scrolling_thread = FrameInfo::SmoothEffectDrivingThread::kRaster;
      break;
  }
  compositor_delegate_->StartScrollSequence(tracker_type, scrolling_thread);
}

void InputHandler::RecordScrollEnd(ui::ScrollInputType input_type) {
  compositor_delegate_->StopSequence(GetTrackerTypeForScroll(input_type));
  base::UmaHistogramTimes(
      "Event.Scroll." + GetScrollInputTypeSuffix(input_type),
      base::TimeTicks::Now() - last_scroll_begin_time_);
}

InputHandlerPointerResult InputHandler::MouseMoveAt(
    const gfx::Point& viewport_point) {
  InputHandlerPointerResult result =
      scrollbar_controller_->HandlePointerMove(gfx::PointF(viewport_point));

  // Early out if there are no animation controllers and avoid the hit test.
  // This happens on platforms without animated scrollbars.
  if (!compositor_delegate_->HasAnimatedScrollbars())
    return result;

  gfx::PointF device_viewport_point = gfx::ScalePoint(
      gfx::PointF(viewport_point), compositor_delegate_->DeviceScaleFactor());

  ScrollHitTestResult hit_test = HitTestScrollNode(device_viewport_point);

  ScrollNode* scroll_node = hit_test.scroll_node;

  // The hit test can fail in some cases, e.g. we don't know if a region of a
  // squashed layer has content or is empty.
  if (!hit_test.hit_test_successful || !scroll_node)
    return result;

  // Scrollbars for the viewport are registered with the outer viewport layer.
  if (scroll_node->scrolls_inner_viewport)
    scroll_node = OuterViewportScrollNode();

  ElementId scroll_element_id = scroll_node->element_id;

  if (scroll_element_id != scroll_element_id_mouse_currently_over_) {
    compositor_delegate_->ScrollbarAnimationMouseLeave(
        scroll_element_id_mouse_currently_over_);
    scroll_element_id_mouse_currently_over_ = scroll_element_id;
  }

  compositor_delegate_->ScrollbarAnimationMouseMove(scroll_element_id,
                                                    device_viewport_point);
  return result;
}

PointerResultType InputHandler::HitTest(const gfx::PointF& viewport_point) {
  return scrollbar_controller_->HitTest(viewport_point)
             ? PointerResultType::kScrollbarScroll
             : PointerResultType::kUnhandled;
}

InputHandlerPointerResult InputHandler::MouseDown(
    const gfx::PointF& viewport_point,
    bool shift_modifier) {
  if (compositor_delegate_->ScrollbarAnimationMouseDown(
          scroll_element_id_mouse_currently_over_)) {
    scroll_element_id_mouse_currently_captured_ =
        scroll_element_id_mouse_currently_over_;
  }
  return scrollbar_controller_->HandlePointerDown(viewport_point,
                                                  shift_modifier);
}

InputHandlerPointerResult InputHandler::MouseUp(
    const gfx::PointF& viewport_point) {
  if (scroll_element_id_mouse_currently_captured_) {
    compositor_delegate_->ScrollbarAnimationMouseUp(
        scroll_element_id_mouse_currently_captured_);

    scroll_element_id_mouse_currently_captured_ = ElementId();
  }
  return scrollbar_controller_->HandlePointerUp(viewport_point);
}

void InputHandler::MouseLeave() {
  compositor_delegate_->DidMouseLeave();
  scroll_element_id_mouse_currently_over_ = ElementId();
}

ElementId InputHandler::FindFrameElementIdAtPoint(
    const gfx::PointF& viewport_point) {
  gfx::PointF device_viewport_point = gfx::ScalePoint(
      gfx::PointF(viewport_point), compositor_delegate_->DeviceScaleFactor());
  return ActiveTree().FindFrameElementIdAtPoint(device_viewport_point);
}

void InputHandler::RequestUpdateForSynchronousInputHandler() {
  UpdateRootLayerStateForSynchronousInputHandler();
}

void InputHandler::SetSynchronousInputHandlerRootScrollOffset(
    const gfx::PointF& root_content_offset) {
  TRACE_EVENT2("cc", "InputHandler::SetSynchronousInputHandlerRootScrollOffset",
               "offset_x", root_content_offset.x(), "offset_y",
               root_content_offset.y());

  gfx::Vector2dF physical_delta =
      root_content_offset - GetViewport().TotalScrollOffset();
  physical_delta.Scale(ActiveTree().page_scale_factor_for_scroll());

  gfx::Vector2dF consumed_delta =
      GetViewport()
          .ScrollBy(physical_delta,
                    /*viewport_point=*/gfx::Point(),
                    /*is_direct_manipulation=*/false,
                    /*affect_browser_controls=*/false,
                    /*scroll_outer_viewport=*/true)
          .consumed_delta;
  if (consumed_delta.IsZero()) {
    return;
  }

  compositor_delegate_->DidScrollContent(OuterViewportScrollNode()->element_id,
                                         /*animated=*/false,
                                         consumed_delta);
  SetNeedsCommit();

  // After applying the synchronous input handler's scroll offset, tell it what
  // we ended up with.
  UpdateRootLayerStateForSynchronousInputHandler();

  compositor_delegate_->SetNeedsFullViewportRedraw();
}

void InputHandler::PinchGestureBegin(const gfx::Point& anchor,
                                     ui::ScrollInputType source) {
  DCHECK(source == ui::ScrollInputType::kTouchscreen ||
         source == ui::ScrollInputType::kWheel);

  pinch_gesture_active_ = true;
  pinch_gesture_end_should_clear_scrolling_node_ = !CurrentlyScrollingNode();

  TRACE_EVENT_INSTANT1("cc", "SetCurrentlyScrollingNode PinchGestureBegin",
                       TRACE_EVENT_SCOPE_THREAD, "isNull",
                       !OuterViewportScrollNode());

  // Some unit tests don't setup viewport scroll nodes but do initiate a pinch
  // zoom gesture. Ideally, those tests should either create the viewport
  // scroll nodes or avoid simulating a pinch gesture.
  if (OuterViewportScrollNode()) {
    ActiveTree().SetCurrentlyScrollingNode(OuterViewportScrollNode());

    ScrollStateData scroll_state_data;
    scroll_state_data.position_x = anchor.x();
    scroll_state_data.position_y = anchor.y();
    scroll_state_data.is_beginning = true;
    scroll_state_data.delta_granularity =
        ui::ScrollGranularity::kScrollByPrecisePixel;
    scroll_state_data.is_direct_manipulation =
        source == ui::ScrollInputType::kTouchscreen;
    ScrollState state(scroll_state_data);

    DidLatchToScroller(state, source);
  }

  compositor_delegate_->PinchBegin();
  compositor_delegate_->DidStartPinchZoom();
}

void InputHandler::PinchGestureUpdate(float magnify_delta,
                                      const gfx::Point& anchor) {
  TRACE_EVENT0("cc", "InputHandler::PinchGestureUpdate");
  if (!InnerViewportScrollNode())
    return;
  has_pinch_zoomed_ = true;
  GetViewport().PinchUpdate(magnify_delta, anchor);
  SetNeedsCommit();
  compositor_delegate_->DidUpdatePinchZoom();
  // Pinching can change the root scroll offset, so inform the synchronous input
  // handler.
  UpdateRootLayerStateForSynchronousInputHandler();
}

void InputHandler::PinchGestureEnd(const gfx::Point& anchor) {
  // Some tests create a pinch gesture without creating a viewport scroll node.
  // In those cases, PinchGestureBegin will not latch to a scroll node.
  DCHECK(latched_scroll_type_.has_value() || !CurrentlyScrollingNode());
  bool snap_to_min = latched_scroll_type_.has_value() &&
                     latched_scroll_type_ == ui::ScrollInputType::kWheel;
  pinch_gesture_active_ = false;
  if (pinch_gesture_end_should_clear_scrolling_node_) {
    pinch_gesture_end_should_clear_scrolling_node_ = false;
    ClearCurrentlyScrollingNode();
  }
  GetViewport().PinchEnd(anchor, snap_to_min);
  compositor_delegate_->PinchEnd();
  SetNeedsCommit();
  compositor_delegate_->DidEndPinchZoom();
}

void InputHandler::SetNeedsAnimateInput() {
  compositor_delegate_->SetNeedsAnimateInput();
}

bool InputHandler::IsCurrentlyScrollingViewport() const {
  auto* node = CurrentlyScrollingNode();
  if (node && GetViewport().ShouldScroll(*node)) {
    return true;
  } else if (features::MultiImplOnlyScrollAnimationsSupported()) {
    // In the snap phase of a scroll gesture, InputHandler will de-latch from
    // from the snapping ScrollNode (which, for viewport scrolls, is recorded as
    // outer viewport scrolls). While this animation is ongoing, we consider
    // InputHandler to still be scrolling the viewport, despite having
    // de-latched from it.
    if (auto* outer_viewport_node = OuterViewportScrollNode()) {
      return IsAnimatingForSnap(outer_viewport_node->element_id);
    }
  }
  return false;
}

EventListenerProperties InputHandler::GetEventListenerProperties(
    EventListenerClass event_class) const {
  return ActiveTree().event_listener_properties(event_class);
}

bool InputHandler::HasBlockingWheelEventHandlerAt(
    const gfx::Point& viewport_point) const {
  gfx::PointF device_viewport_point = gfx::ScalePoint(
      gfx::PointF(viewport_point), compositor_delegate_->DeviceScaleFactor());

  LayerImpl* layer_impl_with_wheel_event_handler =
      ActiveTree().FindLayerThatIsHitByPointInWheelEventHandlerRegion(
          device_viewport_point);

  return layer_impl_with_wheel_event_handler;
}

InputHandler::TouchStartOrMoveEventListenerType
InputHandler::EventListenerTypeForTouchStartOrMoveAt(
    const gfx::Point& viewport_point,
    TouchAction* out_touch_action) {
  gfx::PointF device_viewport_point = gfx::ScalePoint(
      gfx::PointF(viewport_point), compositor_delegate_->DeviceScaleFactor());

  LayerImpl* layer_impl_with_touch_handler =
      ActiveTree().FindLayerThatIsHitByPointInTouchHandlerRegion(
          device_viewport_point);

  if (layer_impl_with_touch_handler == nullptr) {
    if (out_touch_action)
      *out_touch_action = TouchAction::kAuto;
    return InputHandler::TouchStartOrMoveEventListenerType::kNoHandler;
  }

  if (out_touch_action) {
    gfx::Transform layer_screen_space_transform =
        layer_impl_with_touch_handler->ScreenSpaceTransform();
    // Getting here indicates that |layer_impl_with_touch_handler| is non-null,
    // which means that the |hit| in FindClosestMatchingLayer() is true, which
    // indicates that the inverse is available.
    gfx::Transform inverse_layer_screen_space =
        layer_screen_space_transform.GetCheckedInverse();
    bool clipped = false;
    gfx::PointF hit_test_point_in_layer_space = MathUtil::ProjectPoint(
        inverse_layer_screen_space, device_viewport_point, &clipped);
    const auto& region = layer_impl_with_touch_handler->touch_action_region();
    gfx::Point point = gfx::ToRoundedPoint(hit_test_point_in_layer_space);
    *out_touch_action = region.GetAllowedTouchAction(point);
  }

  if (!IsCurrentlyScrolling()) {
    return InputHandler::TouchStartOrMoveEventListenerType::kHandler;
  }

  // Check if the touch start (or move) hits on the current scrolling layer or
  // its descendant. layer_impl_with_touch_handler is the layer hit by the
  // pointer and has an event handler, otherwise it is null. We want to compare
  // the most inner layer we are hitting on which may not have an event listener
  // with the actual scrolling layer.
  LayerImpl* layer_impl =
      ActiveTree().FindLayerThatIsHitByPoint(device_viewport_point);

  ScrollNode* currently_scroll_node = CurrentlyScrollingNode();
  if (currently_scroll_node &&
      IsScrolledBy(layer_impl, currently_scroll_node)) {
    return InputHandler::TouchStartOrMoveEventListenerType::
        kHandlerOnScrollingLayer;
  } else if (features::MultiImplOnlyScrollAnimationsSupported() &&
             !snap_animation_data_map_.empty()) {
    // In the snap phase of a scroll gesture on a snap container, InputHandler
    // will de-latch from from the snapping ScrollNode. While this animation is
    // ongoing, we consider InputHandler to still be scrolling the node, despite
    // having de-latched from it.
    ScrollTree& scroll_tree = GetScrollTree();
    for (const auto& entry : snap_animation_data_map_) {
      // Empty targets means not snap-animating..
      if (entry.second.animating_snap_target_ids_ ==
          TargetSnapAreaElementIds()) {
        continue;
      }
      if (ScrollNode* animating_node =
              scroll_tree.FindNodeFromElementId(entry.first)) {
        if (IsScrolledBy(layer_impl, animating_node)) {
          return InputHandler::TouchStartOrMoveEventListenerType::
              kHandlerOnScrollingLayer;
        }
      }
    }
  }
  return InputHandler::TouchStartOrMoveEventListenerType::kHandler;
}

std::unique_ptr<LatencyInfoSwapPromiseMonitor>
InputHandler::CreateLatencyInfoSwapPromiseMonitor(ui::LatencyInfo* latency) {
  return compositor_delegate_->CreateLatencyInfoSwapPromiseMonitor(latency);
}

std::unique_ptr<EventsMetricsManager::ScopedMonitor>
InputHandler::GetScopedEventMetricsMonitor(
    EventsMetricsManager::ScopedMonitor::DoneCallback done_callback) {
  return compositor_delegate_->GetScopedEventMetricsMonitor(
      std::move(done_callback));
}

ScrollElasticityHelper* InputHandler::CreateScrollElasticityHelper() {
  DCHECK(!scroll_elasticity_helper_);
  if (compositor_delegate_->GetSettings().enable_elastic_overscroll) {
    scroll_elasticity_helper_.reset(
        ScrollElasticityHelper::CreateForLayerTreeHostImpl(
            &compositor_delegate_->GetImplDeprecated()));
  }
  return scroll_elasticity_helper_.get();
}

void InputHandler::DestroyScrollElasticityHelper() {
  // Remove any stretch before destroying helper.
  scroll_elasticity_helper_->SetStretchAmount(gfx::Vector2dF());
  scroll_elasticity_helper_.reset();
}

bool InputHandler::GetScrollOffsetForLayer(ElementId element_id,
                                           gfx::PointF* offset) {
  ScrollTree& scroll_tree = GetScrollTree();
  ScrollNode* scroll_node = scroll_tree.FindNodeFromElementId(element_id);
  if (!scroll_node)
    return false;
  *offset = scroll_tree.current_scroll_offset(element_id);
  return true;
}

bool InputHandler::ScrollLayerTo(ElementId element_id,
                                 const gfx::PointF& offset) {
  ScrollTree& scroll_tree = GetScrollTree();
  ScrollNode* scroll_node = scroll_tree.FindNodeFromElementId(element_id);
  if (!scroll_node)
    return false;

  scroll_tree.ScrollBy(*scroll_node,
                       offset - scroll_tree.current_scroll_offset(element_id),
                       &ActiveTree());
  return true;
}

std::optional<gfx::PointF> InputHandler::ConstrainFling(gfx::PointF original) {
  gfx::PointF fling = original;
  if (fling_snap_constrain_x_) {
    fling.set_x(std::clamp(fling.x(), fling_snap_constrain_x_->GetMin(),
                           fling_snap_constrain_x_->GetMax()));
  }
  if (fling_snap_constrain_y_) {
    fling.set_y(std::clamp(fling.y(), fling_snap_constrain_y_->GetMin(),
                           fling_snap_constrain_y_->GetMax()));
  }
  return original == fling ? std::nullopt : std::make_optional(fling);
}

double InputHandler::PredictViewportBoundsDelta(
    gfx::Vector2dF scroll_distance) {
  // This adjustment is just an estimate. If we're wrong about where to aim a
  // snap fling curve, SnapAtScrollEnd will probably take us to a good place.
  // And if all else fails, the main thread will fix things in SnapAfterLayout
  // which runs after cc has stopped scrolling. But it does look nicer when no
  // corrections are needed, so we try to achieve that in the common cases.

  // The outer_viewport_container_bounds_delta is how much the true viewport
  // size currently differs from what Blink thinks it is.
  double current_bounds_delta = GetScrollTree()
                                    .property_trees()
                                    ->outer_viewport_container_bounds_delta()
                                    .y();
  return compositor_delegate_->PredictViewportBoundsDelta(current_bounds_delta,
                                                          scroll_distance);
}

bool InputHandler::GetSnapFlingInfoAndSetAnimatingSnapTarget(
    const gfx::Vector2dF& current_delta,
    const gfx::Vector2dF& natural_displacement_in_viewport,
    gfx::PointF* out_initial_position,
    gfx::PointF* out_target_position) {
  ScrollNode* scroll_node = CurrentlyScrollingNode();
  if (!scroll_node || !scroll_node->snap_container_data.has_value() ||
      snap_fling_state_ == kNativeFling) {
    return false;
  }
  SnapContainerData& data = scroll_node->snap_container_data.value();

  float scale_factor = ActiveTree().page_scale_factor_for_scroll();
  gfx::Vector2dF current_delta_in_content =
      gfx::ScaleVector2d(current_delta, 1.f / scale_factor);
  gfx::Vector2dF snap_displacement =
      gfx::ScaleVector2d(natural_displacement_in_viewport, 1.f / scale_factor);

  gfx::PointF current_offset = GetVisualScrollOffset(*scroll_node);
  gfx::PointF new_offset = current_offset + current_delta_in_content;

  if (snap_fling_state_ == kConstrainedNativeFling) {
    if (std::optional<gfx::PointF> constrained = ConstrainFling(new_offset)) {
      snap_displacement = *constrained - current_offset;
    } else {
      return false;
    }
  }

  // CC side always uses fractional scroll deltas.
  bool use_fractional_offsets = true;
  std::unique_ptr<SnapSelectionStrategy> strategy =
      SnapSelectionStrategy::CreateForDisplacement(
          current_offset, snap_displacement, use_fractional_offsets);

  double snapport_height_adjustment =
      scroll_node->scrolls_outer_viewport
          ? PredictViewportBoundsDelta(snap_displacement)
          : 0;
  SnapPositionData snap = data.FindSnapPositionWithViewportAdjustment(
      *strategy, snapport_height_adjustment);
  if (snap.type == SnapPositionData::Type::kNone) {
    snap_fling_state_ = kNativeFling;
    return false;
  }

  if (snap_fling_state_ == kNoFling &&
      snap.type == SnapPositionData::Type::kCovered) {
    fling_snap_constrain_x_ = snap.covered_range_x;
    fling_snap_constrain_y_ = snap.covered_range_y;
    if (!ConstrainFling(new_offset)) {
      snap_fling_state_ = kConstrainedNativeFling;
      return false;
    }
  }
  snap_strategy_ = std::move(strategy);

  *out_initial_position = current_offset;
  *out_target_position = snap.position;

  out_target_position->Scale(scale_factor);
  out_initial_position->Scale(scale_factor);

  EnsureSnapAnimationData(scroll_node->element_id);
  SetAnimatingSnapTargetsForElement(scroll_node->element_id,
                                    snap.target_element_ids);
  snap_fling_state_ = kSnapFling;
  return true;
}

void InputHandler::ScrollEndForSnapFling(bool did_finish) {
  ScrollNode* scroll_node = CurrentlyScrollingNode();
  // When a snap fling animation reaches its intended target then we update the
  // scrolled node's snap targets. This also ensures blink learns about the new
  // snap targets for this scrolling element.
  if (did_finish && scroll_node &&
      scroll_node->snap_container_data.has_value()) {
    TargetSnapAreaElementIds target_ids =
        GetAnimatingSnapTargetsForElement(scroll_node->element_id);
    scroll_node->snap_container_data.value().SetTargetSnapAreaElementIds(
        target_ids);
    updated_snapped_elements_[scroll_node->element_id] = target_ids;
    SetNeedsCommit();
  }

  if (scroll_node) {
    ClearAnimatingSnapTargetsForElement(scroll_node->element_id);
  }
  ScrollEnd(true /* should_snap */);
}

void InputHandler::NotifyInputEvent(bool is_fling) {
  compositor_delegate_->NotifyInputEvent(is_fling);
}

//
// =========== InputDelegateForCompositor Interface
//

void InputHandler::ProcessCommitDeltas(
    CompositorCommitData* commit_data,
    const MutatorHost* main_thread_mutator_host) {
  DCHECK(commit_data);
  if (ActiveTree().LayerListIsEmpty())
    return;

  ElementId inner_viewport_scroll_element_id =
      InnerViewportScrollNode() ? InnerViewportScrollNode()->element_id
                                : ElementId();

  base::flat_map<ElementId, TargetSnapAreaElementIds> snapped_elements;
  updated_snapped_elements_.swap(snapped_elements);

  // Scroll commit data is stored in the scroll tree so it has its own method
  // for getting it.
  // TODO(bokan): It's a bug that CollectScrollDeltas is here, it means the
  // compositor cannot commit scroll changes without an InputHandler which it
  // should be able to. To move it back, we'll need to split out the
  // |snapped_elements| part of ScrollTree::CollectScrollDeltas though which is
  // an input responsibility.
  GetScrollTree().CollectScrollDeltas(
      commit_data, inner_viewport_scroll_element_id,
      compositor_delegate_->GetSettings().commit_fractional_scroll_deltas,
      snapped_elements, main_thread_mutator_host);

  // Record and reset scroll source flags.
  DCHECK(!commit_data->manipulation_info);
  if (has_scrolled_by_wheel_)
    commit_data->manipulation_info |= kManipulationInfoWheel;
  if (has_scrolled_by_touch_)
    commit_data->manipulation_info |= kManipulationInfoTouch;
  if (has_scrolled_by_precisiontouchpad_)
    commit_data->manipulation_info |= kManipulationInfoPrecisionTouchPad;
  if (has_pinch_zoomed_)
    commit_data->manipulation_info |= kManipulationInfoPinchZoom;
  if (has_scrolled_by_scrollbar_)
    commit_data->manipulation_info |= kManipulationInfoScrollbar;

  has_scrolled_by_wheel_ = false;
  has_scrolled_by_touch_ = false;
  has_scrolled_by_precisiontouchpad_ = false;
  has_pinch_zoomed_ = false;
  has_scrolled_by_scrollbar_ = false;

  commit_data->scroll_end_data.scroll_gesture_did_end = scroll_gesture_did_end_;
  scroll_gesture_did_end_ = false;

  commit_data->overscroll_delta = overscroll_delta_for_main_thread_;
  overscroll_delta_for_main_thread_ = gfx::Vector2dF();

  if (snap_strategy_) {
    commit_data->snap_strategy = snap_strategy_->Clone();
  }

  // Use the |last_latched_scroller_| rather than the
  // |CurrentlyScrollingNode| since the latter may be cleared by a GSE before
  // we've committed these values to the main thread.
  // TODO(bokan): This is wrong - if we also started a scroll this frame then
  // this will clear this value for that scroll. https://crbug.com/1116780.
  commit_data->scroll_latched_element_id = last_latched_scroller_;
  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    commit_data->scroll_end_data.done_containers =
        std::move(pending_scrollend_containers_);
    if (commit_data->scroll_end_data.done_containers.contains(
            last_latched_scroller_)) {
      last_latched_scroller_ = ElementId();
    }
  } else if (commit_data->scroll_end_data.scroll_gesture_did_end) {
    last_latched_scroller_ = ElementId();
    commit_data->scroll_end_data.gesture_affects_outer_viewport_scroll =
        outer_viewport_consumed_delta_;
    outer_viewport_consumed_delta_ = false;
    commit_data->scroll_end_data.gesture_affects_inner_viewport_scroll =
        inner_viewport_consumed_delta_;
    inner_viewport_consumed_delta_ = false;
  }
}

void InputHandler::TickAnimations(base::TimeTicks monotonic_time) {
  if (input_handler_client_) {
    // This does not set did_animate, because if the InputHandlerClient
    // changes anything it will be through the InputHandler interface which
    // does SetNeedsRedraw.
    input_handler_client_->Animate(monotonic_time);
  }
}

void InputHandler::WillShutdown() {
  if (input_handler_client_) {
    input_handler_client_.ExtractAsDangling()->WillShutdown();
  }

  if (scroll_elasticity_helper_)
    scroll_elasticity_helper_.reset();
}

void InputHandler::WillDraw() {
  if (input_handler_client_)
    input_handler_client_->ReconcileElasticOverscrollAndRootScroll();
}

void InputHandler::WillBeginImplFrame(const viz::BeginFrameArgs& args) {
  if (input_handler_client_) {
    scrollbar_controller_->WillBeginImplFrame();
    input_handler_client_->DeliverInputForBeginFrame(args);
  }
}

void InputHandler::DidCommit() {
  // In high latency mode commit cannot finish within the same frame. We need to
  // flush input here to make sure they got picked up by |PrepareTiles()|.
  if (input_handler_client_ && compositor_delegate_->IsInHighLatencyMode())
    input_handler_client_->DeliverInputForHighLatencyMode();
}

void InputHandler::DidActivatePendingTree() {
  // The previous scrolling node might no longer exist in the new tree.
  if (!CurrentlyScrollingNode())
    ClearCurrentlyScrollingNode();

  // Activation can change the root scroll offset, so inform the synchronous
  // input handler.
  UpdateRootLayerStateForSynchronousInputHandler();
}

void InputHandler::DidFinishImplFrame() {
  if (input_handler_client_) {
    input_handler_client_->DidFinishImplFrame();
  }
}

void InputHandler::OnBeginImplFrameDeadline() {
  if (!IsCurrentlyScrolling()) {
    return;
  }
  if (input_handler_client_) {
    input_handler_client_->DeliverInputForDeadline();
  }
}

void InputHandler::RootLayerStateMayHaveChanged() {
  UpdateRootLayerStateForSynchronousInputHandler();
}

void InputHandler::DidRegisterScrollbar(ElementId scroll_element_id,
                                        ScrollbarOrientation orientation) {
  scrollbar_controller_->DidRegisterScrollbar(scroll_element_id, orientation);
}

void InputHandler::DidUnregisterScrollbar(ElementId scroll_element_id,
                                          ScrollbarOrientation orientation) {
  scrollbar_controller_->DidUnregisterScrollbar(scroll_element_id, orientation);
}

void InputHandler::ScrollOffsetAnimationFinished(ElementId element_id) {
  TRACE_EVENT0("cc", "InputHandler::ScrollOffsetAnimationFinished");
  ScrollNode* finished_node = GetScrollTree().FindNodeFromElementId(element_id);
  bool inner_viewport_animating =
      InnerViewportScrollNode() && finished_node == InnerViewportScrollNode();
  if (inner_viewport_animating) {
    // When the inner viewport node is animating, it is the outer viewport
    // scroll node that is tracked as the CurrentlyScrollingNode and is
    // associated with the snap targets.
    finished_node = OuterViewportScrollNode();
  }

  if (!finished_node) {
    return;
  }

  bool was_animating_for_snap = IsAnimatingForSnap(finished_node->element_id);
  ScrollNode* latched_node = CurrentlyScrollingNode();

  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    // With MultiImplOnlyScrollAnimationsSupported, the node that was animating
    // might not be the currently scrolling node.
    // The only instance in which we expect that the animating node is not the
    // currently latched node is if this was a snap animation (during which we
    // de-latch from the animating node).
    DCHECK(finished_node == latched_node || was_animating_for_snap);
  } else {
    DCHECK(finished_node == latched_node);
  }

  // ScrollOffsetAnimationFinished is called in two cases:
  //  1- smooth scrolling animation is over (IsAnimatingForSnap == false).
  //  2- snap scroll animation is over (IsAnimatingForSnap == true).
  //
  //  Only for case (1) we should check and run snap scroll animation if needed.
  // The end of a scroll offset animation means that the scrolling node is at
  // the target offset.
  if (was_animating_for_snap) {
    if (finished_node && finished_node->snap_container_data.has_value()) {
      TargetSnapAreaElementIds target_ids =
          GetAnimatingSnapTargetsForElement(finished_node->element_id);
      finished_node->snap_container_data.value().SetTargetSnapAreaElementIds(
          target_ids);
      updated_snapped_elements_[finished_node->element_id] = target_ids;
      SetNeedsCommit();
    }

    ClearAnimatingSnapTargetsForElement(finished_node->element_id);
    if (latched_node != finished_node) {
      // Finish the scroll for a non-latched scroll node.
      ScrollEnd(finished_node, false);
      return;
    }
  } else if (SnapAtScrollEnd(SnapReason::kScrollOffsetAnimationFinished)) {
    return;
  }

  ClearAnimatingSnapTargetsForElement(finished_node ? finished_node->element_id
                                                    : element_id);

  // Call scrollEnd with the deferred scroll end state when the scroll animation
  // completes after GSE arrival.
  if (deferred_scroll_end_) {
    ScrollEnd(/*should_snap=*/false);
    return;
  }
}

void InputHandler::SetPrefersReducedMotion(bool prefers_reduced_motion) {
  if (prefers_reduced_motion_ == prefers_reduced_motion)
    return;
  prefers_reduced_motion_ = prefers_reduced_motion;

  if (input_handler_client_)
    input_handler_client_->SetPrefersReducedMotion(prefers_reduced_motion_);
}

bool InputHandler::IsCurrentlyScrolling() const {
  if (CurrentlyScrollingNode()) {
    return true;
  }

  // In the snap phase of a scroll gesture on a snap container, InputHandler
  // will de-latch from from the snapping ScrollNode. While this animation is
  // ongoing, we consider InputHandler to still be scrolling the node, despite
  // having de-latched from it.
  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    for (const auto& entry : snap_animation_data_map_) {
      // Empty targets means not snap-animating.
      if (entry.second.animating_snap_target_ids_ !=
          TargetSnapAreaElementIds()) {
        return true;
      }
    }
  }

  return false;
}

ActivelyScrollingType InputHandler::GetActivelyScrollingType() const {
  const ScrollNode* currently_scrolling_node = CurrentlyScrollingNode();
  if (currently_scrolling_node && last_scroll_update_state_ &&
      delta_consumed_for_scroll_gesture_) {
    if (ShouldAnimateScroll(last_scroll_update_state_.value())) {
      return ActivelyScrollingType::kAnimated;
    }
    return ActivelyScrollingType::kPrecise;
  }

  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    // In the snap phase of a scroll gesture on a snap container, InputHandler
    // will de-latch from from the snapping ScrollNode. While this animation is
    // ongoing, we consider InputHandler to still be scrolling the node, despite
    // having de-latched from it.
    for (const auto& entry : snap_animation_data_map_) {
      if (entry.second.animating_snap_target_ids_ !=
          TargetSnapAreaElementIds()) {
        return ActivelyScrollingType::kAnimated;
      }
    }
  }

  return ActivelyScrollingType::kNone;
}

bool InputHandler::IsHandlingTouchSequence() const {
  return is_handling_touch_sequence_;
}

bool InputHandler::IsCurrentScrollMainRepainted() const {
  const ScrollNode* scroll_node = CurrentlyScrollingNode();
  if (scroll_node) {
    uint32_t repaint_reasons =
        GetScrollTree().GetMainThreadRepaintReasons(*scroll_node);
    if (repaint_reasons != MainThreadScrollingReason::kNotScrollingOnMain) {
      return true;
    }
  }

  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    // Ensure InputHandler factors in snap animations (during which
    // InputHandler de-latches from the ScrollNode) when queried about
    // nodes it's scrolling which require main thread repaints.
    const auto& scroll_tree = GetScrollTree();
    for (const auto& entry : snap_animation_data_map_) {
      if (entry.second.animating_snap_target_ids_ ==
          TargetSnapAreaElementIds()) {
        continue;
      }
      if (const ScrollNode* animating_node =
              scroll_tree.FindNodeFromElementId(entry.first)) {
        uint32_t repaint_reasons =
            GetScrollTree().GetMainThreadRepaintReasons(*animating_node);
        if (repaint_reasons != MainThreadScrollingReason::kNotScrollingOnMain) {
          return true;
        }
      }
    }
  }

  return false;
}

bool InputHandler::HasQueuedInput() const {
  if (input_handler_client_) {
    return input_handler_client_->HasQueuedInput();
  }
  return false;
}

ScrollNode* InputHandler::CurrentlyScrollingNode() {
  return GetScrollTree().CurrentlyScrollingNode();
}

const ScrollNode* InputHandler::CurrentlyScrollingNode() const {
  return GetScrollTree().CurrentlyScrollingNode();
}

ScrollTree& InputHandler::GetScrollTree() {
  return compositor_delegate_->GetScrollTree();
}

ScrollTree& InputHandler::GetScrollTree() const {
  return compositor_delegate_->GetScrollTree();
}

ScrollNode* InputHandler::InnerViewportScrollNode() const {
  return ActiveTree().InnerViewportScrollNode();
}

ScrollNode* InputHandler::OuterViewportScrollNode() const {
  return ActiveTree().OuterViewportScrollNode();
}

Viewport& InputHandler::GetViewport() const {
  return compositor_delegate_->GetImplDeprecated().viewport();
}

void InputHandler::SetNeedsCommit() {
  compositor_delegate_->SetNeedsCommit();
}

LayerTreeImpl& InputHandler::ActiveTree() {
  DCHECK(compositor_delegate_->GetImplDeprecated().active_tree());
  return *compositor_delegate_->GetImplDeprecated().active_tree();
}

LayerTreeImpl& InputHandler::ActiveTree() const {
  DCHECK(compositor_delegate_->GetImplDeprecated().active_tree());
  return *compositor_delegate_->GetImplDeprecated().active_tree();
}

FrameSequenceTrackerType InputHandler::GetTrackerTypeForScroll(
    ui::ScrollInputType input_type) const {
  switch (input_type) {
    case ui::ScrollInputType::kWheel:
      return FrameSequenceTrackerType::kWheelScroll;
    case ui::ScrollInputType::kTouchscreen:
      return FrameSequenceTrackerType::kTouchScroll;
    case ui::ScrollInputType::kScrollbar:
      return FrameSequenceTrackerType::kScrollbarScroll;
    case ui::ScrollInputType::kAutoscroll:
      return FrameSequenceTrackerType::kMaxType;
  }
}

gfx::Size InputHandler::PageSize(const ScrollNode& scroll_node) const {
  gfx::SizeF scroller_size = gfx::SizeF(scroll_node.container_bounds);
  gfx::SizeF viewport_size(compositor_delegate_->VisualDeviceViewportSize());

  // Convert from rootframe coordinates to screen coordinates (physical
  // pixels if --use-zoom-for-dsf enabled, DIPs otherwise).
  scroller_size.Scale(compositor_delegate_->PageScaleFactor());

  // Convert from physical pixels to screen coordinates (if --use-zoom-for-dsf
  // enabled, `DeviceScaleFactor()` returns 1).
  viewport_size.InvScale(compositor_delegate_->DeviceScaleFactor());

  return gfx::Size(std::min(scroller_size.width(), viewport_size.width()),
                   std::min(scroller_size.height(), viewport_size.height()));
}

float InputHandler::LineStep() const {
  return kPixelsPerLineStep * ActiveTree().painted_device_scale_factor();
}

// TODO(mehdika): There is some redundancy between this function and
// ScrollbarController::GetScrollDistanceForScrollbarPart, these two need to be
// kept in sync.
gfx::Vector2dF InputHandler::ResolveScrollGranularityToPixels(
    const ScrollNode& scroll_node,
    const gfx::Vector2dF& scroll_delta,
    ui::ScrollGranularity granularity) {
  gfx::Vector2dF pixel_delta = scroll_delta;

  if (granularity == ui::ScrollGranularity::kScrollByPage) {
    gfx::SizeF scroller_size = gfx::SizeF(scroll_node.container_bounds);
    gfx::SizeF viewport_size(compositor_delegate_->VisualDeviceViewportSize());

    // Convert from rootframe coordinates to screen coordinates (physical
    // pixels if --use-zoom-for-dsf enabled, DIPs otherwise).
    scroller_size.Scale(compositor_delegate_->PageScaleFactor());

    // Convert from physical pixels to screen coordinates (if --use-zoom-for-dsf
    // enabled, `DeviceScaleFactor()` returns 1).
    viewport_size.InvScale(compositor_delegate_->DeviceScaleFactor());

    pixel_delta.Scale(kMinFractionToStepWhenPaging);
    pixel_delta = ScrollUtils::ResolveScrollPercentageToPixels(
        pixel_delta, scroller_size, viewport_size);
  }

  if (granularity == ui::ScrollGranularity::kScrollByLine) {
    pixel_delta.Scale(LineStep(), LineStep());
  }

  return pixel_delta;
}

InputHandler::ScrollHitTestResult InputHandler::HitTestScrollNode(
    const gfx::PointF& device_viewport_point) const {
  ScrollHitTestResult result;
  result.scroll_node = nullptr;
  result.hit_test_successful = false;

  std::vector<const LayerImpl*> layers =
      ActiveTree().FindLayersUpToFirstScrollableOrOpaqueToHitTest(
          device_viewport_point);

  const LayerImpl* first_scrollable_or_opaque_to_hit_test_layer = nullptr;
  if (!layers.empty()) {
    if (compositor_delegate_->GetSettings().enable_hit_test_opaqueness) {
      if (layers.back()->OpaqueToHitTest()) {
        first_scrollable_or_opaque_to_hit_test_layer = layers.back();
      }
    } else if (layers.back()->IsScrollerOrScrollbar()) {
      first_scrollable_or_opaque_to_hit_test_layer = layers.back();
    }
  }
  ScrollNode* node_to_scroll = nullptr;

  // Go through each layer up to (and including) the scroller. Any may block
  // scrolling if they come from outside the scroller's scroll-subtree or if we
  // hit a non-fast-scrolling-region.
  for (const auto* layer_impl : layers) {
    if (!IsInitialScrollHitTestReliable(
            layer_impl, first_scrollable_or_opaque_to_hit_test_layer,
            node_to_scroll)) {
      TRACE_EVENT_INSTANT0("cc", "Failed Hit Test", TRACE_EVENT_SCOPE_THREAD);
      result.main_thread_hit_test_reasons =
          MainThreadScrollingReason::kFailedHitTest;
      return result;
    }

    // If we hit a main thread hit test region, that means there's some reason
    // we can't scroll in this region. Primarily, because there's another
    // scroller there that isn't composited and we don't know about so we'll
    // return failure.
    if (ActiveTree().PointHitsMainThreadScrollHitTestRegion(
            device_viewport_point, *layer_impl)) {
      result.main_thread_hit_test_reasons =
          MainThreadScrollingReason::kMainThreadScrollHitTestRegion;
      return result;
    }

    if (ElementId scroll_element_id = ActiveTree().PointHitsNonCompositedScroll(
            device_viewport_point, *layer_impl)) {
      node_to_scroll = GetScrollTree().FindNodeFromElementId(scroll_element_id);
      CHECK(node_to_scroll);
      break;
    }
  }

  // It's theoretically possible to hit no layers or only non-scrolling layers.
  // e.g. an API hit test outside the viewport, or sending a scroll to an OOPIF
  // that does not have overflow. If we made it to here, we also don't have any
  // non-fast scroll regions. Fallback to scrolling the viewport.
  if (!node_to_scroll) {
    result.hit_test_successful = true;
    if (InnerViewportScrollNode())
      result.scroll_node = GetNodeToScroll(InnerViewportScrollNode());

    return result;
  }

  result.scroll_node = node_to_scroll;
  result.hit_test_successful = true;
  return result;
}

ScrollNode* InputHandler::GetNodeToScroll(ScrollNode* node) const {
  // The root and the secondary root are sentinel nodes and don't contribute to
  // scrolling.
  if (node->id <= kSecondaryRootPropertyNodeId) {
    return nullptr;
  }

  // Blink has a notion of a "root scroller", which is the scroller in a page
  // that is considered to host the main content. Typically this will be the
  // document/LayoutView contents; however, in some situations Blink may choose
  // a sub-scroller (div, iframe) that should scroll with "viewport" behavior.
  // The "root scroller" is the node designated as the outer viewport in CC.
  // See third_party/blink/renderer/core/page/scrolling/README.md for details.
  //
  // "Viewport" scrolling ensures generation of overscroll events, top controls
  // movement, as well as correct multi-viewport panning in pinch-zoom and
  // other scenarios.  We use the viewport's outer scroll node to represent the
  // viewport in the scroll chain and apply scroll delta using CC's Viewport
  // class.
  //
  // Scrolling from position: fixed layers will chain directly up to the inner
  // viewport. Whether that should use the outer viewport (and thus the
  // Viewport class) to scroll or not depends on the root scroller scenario
  // because we don't want setting a root scroller to change the scroll chain
  // order. The |prevent_viewport_scrolling_from_inner| bit is used to
  // communicate that context.
  DCHECK(!node->prevent_viewport_scrolling_from_inner ||
         node->scrolls_inner_viewport);

  if (node->scrolls_inner_viewport &&
      !node->prevent_viewport_scrolling_from_inner) {
    DCHECK(OuterViewportScrollNode());
    return OuterViewportScrollNode();
  }

  return node;
}

ScrollNode* InputHandler::GetNodeToScrollForLayer(
    const LayerImpl* layer) const {
  if (layer->IsScrollbarLayer()) {
    // If we hit a scrollbar layer, get the ScrollNode from its associated
    // scrolling layer, rather than directly from the scrollbar layer. The
    // latter would return the parent scroller's ScrollNode.
    if (auto* scroll_node = GetScrollTree().FindNodeFromElementId(
            ToScrollbarLayer(layer)->scroll_element_id())) {
      return GetNodeToScroll(scroll_node);
    }
    return nullptr;
  }
  return GetNodeToScroll(GetScrollTree().Node(layer->scroll_tree_index()));
}

bool InputHandler::IsInitialScrollHitTestReliable(
    const LayerImpl* layer_impl,
    const LayerImpl* first_scrollable_or_opaque_to_hit_test_layer,
    ScrollNode*& out_node_to_scroll) const {
  ScrollNode* scroll_node = GetNodeToScrollForLayer(layer_impl);

  if (layer_impl == first_scrollable_or_opaque_to_hit_test_layer) {
    out_node_to_scroll = scroll_node;
    return true;
  }

  // If there's a scrolling layer, we should also have a closest scroll node,
  // and vice versa. Otherwise, the hit test is not reliable.
  if ((first_scrollable_or_opaque_to_hit_test_layer && !scroll_node) ||
      (scroll_node && !first_scrollable_or_opaque_to_hit_test_layer)) {
    return false;
  }
  if (!first_scrollable_or_opaque_to_hit_test_layer && !scroll_node) {
    // It's ok if we have neither.
    out_node_to_scroll = nullptr;
    return true;
  }

  // If `first_scrollable_or_opaque_to_hit_test_layer` and `layer_impl` will
  // scroll the same scroll node, the hit test has not escaped to other areas
  // of the scroll tree and is reliable so far.
  if (scroll_node ==
      GetNodeToScrollForLayer(first_scrollable_or_opaque_to_hit_test_layer)) {
    out_node_to_scroll = scroll_node;
    return true;
  }

  return false;
}

gfx::Vector2dF InputHandler::ComputeScrollDelta(const ScrollNode& scroll_node,
                                                const gfx::Vector2dF& delta) {
  ScrollTree& scroll_tree = GetScrollTree();
  float scale_factor = compositor_delegate_->PageScaleFactor();

  gfx::Vector2dF adjusted_scroll(delta);
  adjusted_scroll.InvScale(scale_factor);
  adjusted_scroll = UserScrollableDelta(scroll_node, adjusted_scroll);

  gfx::PointF old_offset =
      scroll_tree.current_scroll_offset(scroll_node.element_id);
  gfx::PointF new_offset = scroll_tree.ClampScrollOffsetToLimits(
      old_offset + adjusted_scroll, scroll_node);

  return new_offset - old_offset;
}

bool InputHandler::CalculateLocalScrollDeltaAndStartPoint(
    const ScrollNode& scroll_node,
    const gfx::PointF& viewport_point,
    const gfx::Vector2dF& viewport_delta,
    gfx::Vector2dF* out_local_scroll_delta,
    gfx::PointF* out_local_start_point /*= nullptr*/) {
  if (scroll_node.transform_id == kInvalidPropertyNodeId) {
    return false;
  }

  // Layers with non-invertible screen space transforms should not have passed
  // the scroll hit test in the first place.
  const gfx::Transform screen_space_transform =
      GetScrollTree().ScreenSpaceTransform(scroll_node.id);
  // TODO(shawnsingh): With the advent of impl-side scrolling for non-root
  // layers, we may need to explicitly handle uninvertible transforms here.
  gfx::Transform inverse_screen_space_transform =
      screen_space_transform.GetCheckedInverse();

  float scale_from_viewport_to_screen_space =
      compositor_delegate_->DeviceScaleFactor();
  gfx::PointF screen_space_point =
      gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);

  gfx::Vector2dF screen_space_delta = viewport_delta;
  screen_space_delta.Scale(scale_from_viewport_to_screen_space);

  // Project the scroll start and end points to local layer space to find the
  // scroll delta in layer coordinates.
  bool start_clipped, end_clipped;
  gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
  gfx::PointF local_start_point = MathUtil::ProjectPoint(
      inverse_screen_space_transform, screen_space_point, &start_clipped);
  gfx::PointF local_end_point = MathUtil::ProjectPoint(
      inverse_screen_space_transform, screen_space_end_point, &end_clipped);
  DCHECK(out_local_scroll_delta);
  *out_local_scroll_delta = local_end_point - local_start_point;

  if (out_local_start_point)
    *out_local_start_point = local_start_point;

  if (start_clipped || end_clipped)
    return false;

  return true;
}

gfx::Vector2dF InputHandler::ScrollNodeWithViewportSpaceDelta(
    const ScrollNode& scroll_node,
    const gfx::PointF& viewport_point,
    const gfx::Vector2dF& viewport_delta) {
  ScrollTree& scroll_tree = GetScrollTree();
  gfx::PointF local_start_point;
  gfx::Vector2dF local_scroll_delta;
  if (!CalculateLocalScrollDeltaAndStartPoint(
          scroll_node, viewport_point, viewport_delta, &local_scroll_delta,
          &local_start_point)) {
    return gfx::Vector2dF();
  }

  bool scrolls_outer_viewport = scroll_node.scrolls_outer_viewport;
  TRACE_EVENT2("cc", "ScrollNodeWithViewportSpaceDelta", "delta_y",
               local_scroll_delta.y(), "is_outer", scrolls_outer_viewport);

  // Apply the scroll delta.
  gfx::PointF previous_offset =
      scroll_tree.current_scroll_offset(scroll_node.element_id);
  scroll_tree.ScrollBy(scroll_node, local_scroll_delta, &ActiveTree());
  gfx::Vector2dF scrolled =
      scroll_tree.current_scroll_offset(scroll_node.element_id) -
      previous_offset;

  TRACE_EVENT_INSTANT1("cc", "ConsumedDelta", TRACE_EVENT_SCOPE_THREAD, "y",
                       scrolled.y());

  // Get the end point in the layer's content space so we can apply its
  // ScreenSpaceTransform.
  gfx::PointF actual_local_end_point = local_start_point + scrolled;

  // Calculate the applied scroll delta in viewport space coordinates.
  bool end_clipped;
  const gfx::Transform screen_space_transform =
      scroll_tree.ScreenSpaceTransform(scroll_node.id);
  gfx::PointF actual_screen_space_end_point = MathUtil::MapPoint(
      screen_space_transform, actual_local_end_point, &end_clipped);
  DCHECK(!end_clipped);
  if (end_clipped)
    return gfx::Vector2dF();

  float scale_from_viewport_to_screen_space =
      compositor_delegate_->DeviceScaleFactor();
  gfx::PointF actual_viewport_end_point = gfx::ScalePoint(
      actual_screen_space_end_point, 1.f / scale_from_viewport_to_screen_space);
  return actual_viewport_end_point - viewport_point;
}

gfx::Vector2dF InputHandler::ScrollNodeWithLocalDelta(
    const ScrollNode& scroll_node,
    const gfx::Vector2dF& local_delta) const {
  bool scrolls_outer_viewport = scroll_node.scrolls_outer_viewport;
  TRACE_EVENT2("cc", "ScrollNodeWithLocalDelta", "delta_y", local_delta.y(),
               "is_outer", scrolls_outer_viewport);
  float page_scale_factor = compositor_delegate_->PageScaleFactor();

  ScrollTree& scroll_tree = GetScrollTree();
  gfx::PointF previous_offset =
      scroll_tree.current_scroll_offset(scroll_node.element_id);
  gfx::Vector2dF delta = local_delta;
  delta.InvScale(page_scale_factor);
  scroll_tree.ScrollBy(scroll_node, delta, &ActiveTree());
  gfx::Vector2dF scrolled =
      scroll_tree.current_scroll_offset(scroll_node.element_id) -
      previous_offset;
  gfx::Vector2dF consumed_scroll(scrolled.x(), scrolled.y());
  consumed_scroll.Scale(page_scale_factor);
  TRACE_EVENT_INSTANT1("cc", "ConsumedDelta", TRACE_EVENT_SCOPE_THREAD, "y",
                       consumed_scroll.y());

  return consumed_scroll;
}

// TODO(danakj): Make this into two functions, one with delta, one with
// viewport_point, no bool required.
gfx::Vector2dF InputHandler::ScrollSingleNode(const ScrollNode& scroll_node,
                                              const gfx::Vector2dF& delta,
                                              const gfx::Point& viewport_point,
                                              bool is_direct_manipulation) {
  gfx::Vector2dF adjusted_delta = UserScrollableDelta(scroll_node, delta);

  // Events representing direct manipulation of the screen (such as gesture
  // events) need to be transformed from viewport coordinates to local layer
  // coordinates so that the scrolling contents exactly follow the user's
  // finger. In contrast, events not representing direct manipulation of the
  // screen (such as wheel events) represent a fixed amount of scrolling so we
  // can just apply them directly, but the page scale factor is applied to the
  // scroll delta.
  if (is_direct_manipulation) {
    // For touch-scroll we need to scale the delta here, as the transform tree
    // won't know anything about the external page scale factors used by OOPIFs.
    gfx::Vector2dF scaled_delta(adjusted_delta);
    scaled_delta.InvScale(ActiveTree().external_page_scale_factor());
    return ScrollNodeWithViewportSpaceDelta(
        scroll_node, gfx::PointF(viewport_point), scaled_delta);
  }
  return ScrollNodeWithLocalDelta(scroll_node, adjusted_delta);
}

ScrollNode* InputHandler::GetAnimatingNodeForCurrentScrollingNode() {
  ScrollNode* scroll_node = CurrentlyScrollingNode();
  if (!scroll_node) {
    return nullptr;
  }

  if (compositor_delegate_->ElementHasImplOnlyScrollAnimation(
          scroll_node->element_id)) {
    return scroll_node;
  }

  // Usually the CurrentlyScrollingNode will be the currently animating
  // one. The one exception is the inner viewport. Scrolling the combined
  // viewport will always set the outer viewport as the currently scrolling
  // node. However, if an animation is created on the inner viewport we
  // must use it when updating the animation curve.
  ScrollNode* inner_viewport_scroll_node = InnerViewportScrollNode();
  if (scroll_node->scrolls_outer_viewport && inner_viewport_scroll_node) {
    if (compositor_delegate_->ElementHasImplOnlyScrollAnimation(
            inner_viewport_scroll_node->element_id)) {
      return inner_viewport_scroll_node;
    }
  }

  return nullptr;
}

void InputHandler::ScrollLatchedScroller(ScrollState& scroll_state,
                                         base::TimeDelta delayed_by) {
  DCHECK(CurrentlyScrollingNode());
  DCHECK(latched_scroll_type_.has_value());

  ScrollNode& scroll_node = *CurrentlyScrollingNode();
  const gfx::Vector2dF delta(scroll_state.delta_x(), scroll_state.delta_y());
  TRACE_EVENT2("cc", "InputHandler::ScrollLatchedScroller", "delta_x",
               delta.x(), "delta_y", delta.y());
  gfx::Vector2dF applied_delta;
  gfx::Vector2dF delta_applied_to_content;
  std::optional<gfx::PointF> snap_strategy_offset;

  if (ShouldAnimateScroll(scroll_state)) {
    DCHECK(!scroll_state.is_in_inertial_phase());

    if (ScrollNode* animating_scroll_node =
            GetAnimatingNodeForCurrentScrollingNode()) {
      TRACE_EVENT_INSTANT0("cc", "UpdateExistingAnimation",
                           TRACE_EVENT_SCOPE_THREAD);

      // See comment in GetAnimatingNodeForCurrentScrollingNode for explanation
      // of this DCHECK.
      DCHECK(animating_scroll_node->id == scroll_node.id ||
             animating_scroll_node->scrolls_inner_viewport);

      snap_strategy_offset = ScrollAnimationUpdateTarget(*animating_scroll_node,
                                                         delta, delayed_by);

      if (snap_strategy_offset) {
        // Because we updated the animation target, consume delta so we notify
        // the `LatencyInfoSwapPromiseMonitor` to tell it that something
        // happened that will cause a swap in the future. This will happen
        // within the scope of the dispatch of a gesture scroll update input
        // event. If we don't notify during the handling of the input event, the
        // `LatencyInfo` associated with the input event will not be added as a
        // swap promise and we won't get any swap results.
        applied_delta = delta;
      } else {
        TRACE_EVENT_INSTANT0("cc", "Didn't Update Animation",
                             TRACE_EVENT_SCOPE_THREAD);
      }
    } else {
      TRACE_EVENT_INSTANT0("cc", "CreateNewAnimation",
                           TRACE_EVENT_SCOPE_THREAD);
      if (scroll_node.scrolls_outer_viewport) {
        auto result = GetViewport().ScrollAnimated(delta, delayed_by);
        applied_delta = result.consumed_delta;
        SetViewportConsumedDelta(result);
      } else {
        applied_delta = ComputeScrollDelta(scroll_node, delta);
        compositor_delegate_->ScrollAnimationCreate(scroll_node, applied_delta,
                                                    delayed_by);
      }
      gfx::PointF current_scroll_offset = GetVisualScrollOffset(scroll_node);
      snap_strategy_offset = GetScrollTree().ClampScrollOffsetToLimits(
          current_scroll_offset + applied_delta, scroll_node);
    }

    // Animated scrolling always applied only to the content (i.e. not to the
    // browser controls).
    delta_applied_to_content = delta;
  } else {
    gfx::Point viewport_point(scroll_state.position_x(),
                              scroll_state.position_y());
    if (GetViewport().ShouldScroll(scroll_node)) {
      // |scrolls_outer_viewport| will only ever be false if the scroll chains
      // up to the viewport without going through the outer viewport scroll
      // node. This is because we normally terminate the scroll chain at the
      // outer viewport node.  For example, if we start scrolling from an
      // element that's not a descendant of the root scroller. In these cases we
      // want to scroll *only* the inner viewport -- to allow panning while
      // zoomed -- but still use Viewport::ScrollBy to also move browser
      // controls if needed.
      ViewportScrollResult result = GetViewport().ScrollBy(
          delta, viewport_point, scroll_state.is_direct_manipulation(),
          latched_scroll_type_ != ui::ScrollInputType::kWheel,
          scroll_node.scrolls_outer_viewport);

      applied_delta = result.consumed_delta;
      delta_applied_to_content = result.content_scrolled_delta;
      SetViewportConsumedDelta(result);
    } else {
      applied_delta = ScrollSingleNode(scroll_node, delta, viewport_point,
                                       scroll_state.is_direct_manipulation());
    }
    snap_strategy_offset = GetVisualScrollOffset(scroll_node);
  }
  overscroll_delta_for_main_thread_ += delta - applied_delta;

  // If the layer wasn't able to move, try the next one in the hierarchy.
  bool scrolled = std::abs(applied_delta.x()) > kScrollEpsilon;
  scrolled = scrolled || std::abs(applied_delta.y()) > kScrollEpsilon;
  if (!scrolled) {
    // TODO(bokan): This preserves existing behavior by not allowing tiny
    // scrolls to produce overscroll but is inconsistent in how delta gets
    // chained up. We need to clean this up.
    if (scroll_node.scrolls_outer_viewport)
      scroll_state.ConsumeDelta(applied_delta.x(), applied_delta.y());
    return;
  }

  if (!GetViewport().ShouldScroll(scroll_node)) {
    // If the applied delta is within 45 degrees of the input
    // delta, bail out to make it easier to scroll just one layer
    // in one direction without affecting any of its parents.
    float angle_threshold = 45;
    if (MathUtil::SmallestAngleBetweenVectors(applied_delta, delta) <
        angle_threshold) {
      applied_delta = delta;
    } else {
      // Allow further movement only on an axis perpendicular to the direction
      // in which the layer moved.
      applied_delta = MathUtil::ProjectVector(delta, applied_delta);
    }
    delta_applied_to_content = applied_delta;
  }

  scroll_state.set_caused_scroll(
      std::abs(delta_applied_to_content.x()) > kScrollEpsilon,
      std::abs(delta_applied_to_content.y()) > kScrollEpsilon);
  scroll_state.ConsumeDelta(applied_delta.x(), applied_delta.y());

  did_scroll_x_for_scroll_gesture_ |= scroll_state.caused_scroll_x();
  did_scroll_y_for_scroll_gesture_ |= scroll_state.caused_scroll_y();

  if (snap_strategy_offset && !scroll_state.is_in_inertial_phase()) {
    // We use |last_scroll_update_state_| instead of |scroll_state| as that more
    // closely matches what InputHandler::SnapAtScrollend would use.
    //
    // We validate that `last_scroll_update_state_` exists before using it. As
    // we have seen rare crashes with it null. We do not use
    // `std::optional::value_or` here as that performs a copy of the
    // alternative. Which will rarely ever be needed.
    snap_strategy_ =
        CreateSnapStrategy(last_scroll_update_state_.has_value()
                               ? last_scroll_update_state_.value()
                               : scroll_state,
                           snap_strategy_offset.value(),
                           SnapReason::kScrollOffsetAnimationFinished);
  }
}

bool InputHandler::CanPropagate(ScrollNode* scroll_node, float x, float y) {
  return (x == 0 || scroll_node->overscroll_behavior.x ==
                        OverscrollBehavior::Type::kAuto) &&
         (y == 0 || scroll_node->overscroll_behavior.y ==
                        OverscrollBehavior::Type::kAuto);
}

ScrollNode* InputHandler::FindNodeToLatch(ScrollState* scroll_state,
                                          ScrollNode* starting_node,
                                          ui::ScrollInputType type) {
  ScrollTree& scroll_tree = GetScrollTree();
  ScrollNode* scroll_node = nullptr;
  ScrollNode* first_scrollable_node = nullptr;
  for (ScrollNode* cur_node = starting_node; cur_node;
       cur_node = scroll_tree.parent(cur_node)) {
    if (GetViewport().ShouldScroll(*cur_node)) {
      // Don't chain scrolls past a viewport node. Once we reach that, we
      // should scroll using the appropriate viewport node which may not be
      // |cur_node|.
      scroll_node = GetNodeToScroll(cur_node);
      break;
    }

    // A scroll container allows chaining ​if​ overscroll-behavior is set to
    // auto on both axes, ​or if​ the Feature Flag is disabled. When the
    // scroll container does not allow chaining, we should not skip it, as we
    // may need to latch to it.
    bool scroll_container_allows_chaining =
        !base::FeatureList::IsEnabled(
            ::features::kOverscrollBehaviorRespectedOnAllScrollContainers) ||
        (cur_node->overscroll_behavior.x == OverscrollBehavior::Type::kAuto &&
         cur_node->overscroll_behavior.y == OverscrollBehavior::Type::kAuto);

    if (!cur_node->user_scrollable_horizontal &&
        !cur_node->user_scrollable_vertical &&
        scroll_container_allows_chaining) {
      continue;
    }

    if (!first_scrollable_node) {
      first_scrollable_node = cur_node;
    }

    if (CanConsumeDelta(*scroll_state, *cur_node)) {
      scroll_node = cur_node;
      break;
    }

    float delta_x = scroll_state->is_beginning() ? scroll_state->delta_x_hint()
                                                 : scroll_state->delta_x();
    float delta_y = scroll_state->is_beginning() ? scroll_state->delta_y_hint()
                                                 : scroll_state->delta_y();

    if (!CanPropagate(cur_node, delta_x, delta_y)) {
      // If we reach a node with non-auto overscroll-behavior and we still
      // haven't latched, we must latch to it. Consider a fully scrolled node
      // with non-auto overscroll-behavior: we are not allowed to further
      // chain scroll delta passed to it in the current direction but if we
      // reverse direction we should scroll it so we must be latched to it.
      scroll_node = cur_node;
      scroll_state->set_is_scroll_chain_cut(true);
      break;
    }
  }

  // If the root scroller can not consume delta in an autoscroll, latch on
  // to the top most autoscrollable scroller. See https://crbug.com/969150
  if ((type == ui::ScrollInputType::kAutoscroll) && first_scrollable_node) {
    // If scroll_node is nullptr or delta can not be consumed
    if (!(scroll_node && CanConsumeDelta(*scroll_state, *scroll_node)))
      scroll_node = first_scrollable_node;
  }

  return scroll_node;
}

void InputHandler::UpdateRootLayerStateForSynchronousInputHandler() {
  if (!input_handler_client_)
    return;
  input_handler_client_->UpdateRootLayerStateForSynchronousInputHandler(
      ActiveTree().TotalScrollOffset(), ActiveTree().TotalMaxScrollOffset(),
      ActiveTree().ScrollableSize(), ActiveTree().current_page_scale_factor(),
      ActiveTree().min_page_scale_factor(),
      ActiveTree().max_page_scale_factor());
}

void InputHandler::DidLatchToScroller(const ScrollState& scroll_state,
                                      ui::ScrollInputType type) {
  DCHECK(CurrentlyScrollingNode());
  deferred_scroll_end_ = false;
  compositor_delegate_->ScrollBegin();
  if (ScrollNode* animating_node = GetAnimatingNodeForCurrentScrollingNode()) {
    compositor_delegate_->ScrollAnimationAbort(animating_node->element_id);
  }

  last_latched_scroller_ = CurrentlyScrollingNode()->element_id;
  latched_scroll_type_ = type;
  last_scroll_begin_state_ = scroll_state;

  ClearAnimatingSnapTargetsForElement(last_latched_scroller_);

  compositor_delegate_->DidStartScroll();

  UpdateScrollSourceInfo(scroll_state, type);
}

bool InputHandler::CanConsumeDelta(const ScrollState& scroll_state,
                                   const ScrollNode& scroll_node) {
  gfx::Vector2dF delta_to_scroll;
  if (scroll_state.is_beginning()) {
    delta_to_scroll = gfx::Vector2dF(scroll_state.delta_x_hint(),
                                     scroll_state.delta_y_hint());
  } else {
    delta_to_scroll =
        gfx::Vector2dF(scroll_state.delta_x(), scroll_state.delta_y());
  }

  if (delta_to_scroll == gfx::Vector2dF())
    return true;

  if (scroll_state.is_direct_manipulation()) {
    gfx::Vector2dF local_scroll_delta;
    if (!CalculateLocalScrollDeltaAndStartPoint(
            scroll_node,
            gfx::PointF(scroll_state.position_x(), scroll_state.position_y()),
            delta_to_scroll, &local_scroll_delta)) {
      return false;
    }
    delta_to_scroll = local_scroll_delta;
  } else {
    delta_to_scroll = ResolveScrollGranularityToPixels(
        scroll_node, delta_to_scroll, scroll_state.delta_granularity());
  }

  if (ComputeScrollDelta(scroll_node, delta_to_scroll) != gfx::Vector2dF())
    return true;

  return false;
}

bool InputHandler::ShouldAnimateScroll(const ScrollState& scroll_state) const {
  if (!compositor_delegate_->GetSettings().enable_smooth_scroll)
    return false;

  bool has_precise_scroll_deltas = scroll_state.delta_granularity() ==
                                   ui::ScrollGranularity::kScrollByPrecisePixel;

  return !has_precise_scroll_deltas;
}

bool InputHandler::SnapAtScrollEnd(SnapReason reason) {
  ScrollNode* scroll_node = CurrentlyScrollingNode();
  if (!scroll_node || !scroll_node->snap_container_data.has_value())
    return false;

  SnapContainerData& data = scroll_node->snap_container_data.value();
  gfx::PointF current_position = GetVisualScrollOffset(*scroll_node);

  // You might think that if a scroll never received a scroll update we
  // could just drop the snap. However, if the GSB+GSE arrived while we were
  // mid-snap from a previous gesture, this would leave the scroller at a
  // non-snap-point.
  DCHECK(last_scroll_update_state_ || last_scroll_begin_state_);
  ScrollState& last_scroll_state = last_scroll_update_state_
                                       ? *last_scroll_update_state_
                                       : *last_scroll_begin_state_;

  if (!snap_strategy_ || last_scroll_state.is_in_inertial_phase()) {
    // If this was a fling, SnapFlingController would not have had the
    // correct final scroll position with which to create the snap
    // strategy.
    snap_strategy_ =
        CreateSnapStrategy(last_scroll_state, current_position, reason);
  }

  double snapport_height_adjustment =
      scroll_node->scrolls_outer_viewport
          ? PredictViewportBoundsDelta(gfx::Vector2dF())
          : 0;
  SnapPositionData snap = data.FindSnapPositionWithViewportAdjustment(
      *snap_strategy_, snapport_height_adjustment);
  if (snap.type == SnapPositionData::Type::kNone) {
    // Ensure we retain the ids of any element we were previously snapped to and
    // are still snapped to in case of scrolls in an axis where no snapping
    // happens.
    if (reason == SnapReason::kScrollOffsetAnimationFinished) {
      EnsureSnapAnimationData(CurrentlyScrollingNode()->element_id);
      SetAnimatingSnapTargetsForElement(CurrentlyScrollingNode()->element_id,
                                        snap.target_element_ids);
    } else if (data.SetTargetSnapAreaElementIds(snap.target_element_ids)) {
      updated_snapped_elements_[scroll_node->element_id] =
          snap.target_element_ids;
    }
    return false;
  }

  // TODO(bokan): Why only on the viewport?
  if (GetViewport().ShouldScroll(*scroll_node)) {
    compositor_delegate_->WillScrollContent(scroll_node->element_id);
  }

  gfx::Vector2dF delta = snap.position - current_position;
  bool did_animate = false;
  if (scroll_node->scrolls_outer_viewport) {
    gfx::Vector2dF scaled_delta(delta);
    scaled_delta.Scale(compositor_delegate_->PageScaleFactor());
    auto result = GetViewport().ScrollAnimated(scaled_delta, base::TimeDelta());
    gfx::Vector2dF consumed_delta = result.consumed_delta;
    did_animate = !consumed_delta.IsZero();
    SetViewportConsumedDelta(result);
  } else {
    did_animate = compositor_delegate_->ScrollAnimationCreate(
        *scroll_node, delta, base::TimeDelta());
  }
  DCHECK(!IsAnimatingForSnap(CurrentlyScrollingNode()->element_id));
  if (did_animate) {
    if (features::MultiImplOnlyScrollAnimationsSupported()) {
      // Forget the scroll container that is currently
      // latched so that any scroll gesture that occurs during the snap
      // animation will be allowed to scroll the appropriate container.
      ClearCurrentlyScrollingNode();
    }
    EnsureSnapAnimationData(scroll_node->element_id);
    // The updated snap target will be set when the animation is completed.
    SetAnimatingSnapTargetsForElement(scroll_node->element_id,
                                      snap.target_element_ids);
  } else if (data.SetTargetSnapAreaElementIds(snap.target_element_ids)) {
    updated_snapped_elements_[scroll_node->element_id] =
        snap.target_element_ids;
    SetNeedsCommit();
  }
  return did_animate;
}

bool InputHandler::IsAnimatingForSnap(ElementId element_id) const {
  return GetAnimatingSnapTargetsForElement(element_id) !=
         TargetSnapAreaElementIds();
}

gfx::PointF InputHandler::GetVisualScrollOffset(
    const ScrollNode& scroll_node) const {
  if (scroll_node.scrolls_outer_viewport)
    return GetViewport().TotalScrollOffset();
  return GetScrollTree().current_scroll_offset(scroll_node.element_id);
}

void InputHandler::ClearCurrentlyScrollingNode() {
  TRACE_EVENT0("cc", "InputHandler::ClearCurrentlyScrollingNode");
  ClearAnimatingSnapTargetsForElement(CurrentlyScrollingNode()
                                          ? CurrentlyScrollingNode()->element_id
                                          : ElementId());
  ActiveTree().ClearCurrentlyScrollingNode();
  accumulated_root_overscroll_ = gfx::Vector2dF();
  did_scroll_x_for_scroll_gesture_ = false;
  did_scroll_y_for_scroll_gesture_ = false;
  delta_consumed_for_scroll_gesture_ = false;
  latched_scroll_type_.reset();
  last_scroll_update_state_.reset();
  last_scroll_begin_state_.reset();
  compositor_delegate_->DidEndScroll();
}

std::optional<gfx::PointF> InputHandler::ScrollAnimationUpdateTarget(
    const ScrollNode& scroll_node,
    const gfx::Vector2dF& scroll_delta,
    base::TimeDelta delayed_by) {
  // TODO(bokan): Remove |scroll_node| as a parameter and just use the value
  // coming from |mutator_host|.
  DCHECK(compositor_delegate_->ElementHasImplOnlyScrollAnimation(
      scroll_node.element_id));

  float scale_factor = compositor_delegate_->PageScaleFactor();
  gfx::Vector2dF adjusted_delta =
      gfx::ScaleVector2d(scroll_delta, 1.f / scale_factor);
  adjusted_delta = UserScrollableDelta(scroll_node, adjusted_delta);

  std::optional<gfx::PointF> animation_target =
      compositor_delegate_->UpdateImplAnimationScrollTargetWithDelta(
          adjusted_delta, scroll_node.id, delayed_by, scroll_node.element_id);
  if (animation_target) {
    compositor_delegate_->DidUpdateScrollAnimationCurve();

    // The animation is no longer targeting a snap position. By clearing the
    // target, this will ensure that we attempt to resnap at the end of this
    // animation.
    ClearAnimatingSnapTargetsForElement(scroll_node.element_id);
  }

  return animation_target;
}

void InputHandler::UpdateScrollSourceInfo(const ScrollState& scroll_state,
                                          ui::ScrollInputType type) {
  if (type == ui::ScrollInputType::kWheel &&
      scroll_state.delta_granularity() ==
          ui::ScrollGranularity::kScrollByPrecisePixel) {
    has_scrolled_by_precisiontouchpad_ = true;
  } else if (type == ui::ScrollInputType::kWheel) {
    has_scrolled_by_wheel_ = true;
  } else if (type == ui::ScrollInputType::kTouchscreen) {
    has_scrolled_by_touch_ = true;
  } else if (type == ui::ScrollInputType::kScrollbar) {
    has_scrolled_by_scrollbar_ = true;
  }
}

// Return true if scrollable node for 'ancestor' is the same as 'child' or an
// ancestor along the scroll tree.
bool InputHandler::IsScrolledBy(LayerImpl* child, ScrollNode* ancestor) {
  DCHECK(ancestor && (ancestor->user_scrollable_horizontal ||
                      ancestor->user_scrollable_vertical));
  if (!child)
    return false;
  DCHECK_EQ(child->layer_tree_impl(), &ActiveTree());
  ScrollTree& scroll_tree = GetScrollTree();
  for (ScrollNode* scroll_node = scroll_tree.Node(child->scroll_tree_index());
       scroll_node; scroll_node = scroll_tree.parent(scroll_node)) {
    if (scroll_node->id == ancestor->id)
      return true;
  }
  return false;
}

gfx::Vector2dF InputHandler::UserScrollableDelta(
    const ScrollNode& node,
    const gfx::Vector2dF& delta) const {
  gfx::Vector2dF adjusted_delta = delta;
  if (!node.user_scrollable_horizontal)
    adjusted_delta.set_x(0);
  if (!node.user_scrollable_vertical)
    adjusted_delta.set_y(0);

  return adjusted_delta;
}

bool InputHandler::ScrollbarScrollIsActive() {
  return scrollbar_controller_->ScrollbarScrollIsActive();
}

void InputHandler::SetDeferBeginMainFrame(bool defer_begin_main_frame) const {
  compositor_delegate_->SetDeferBeginMainFrame(defer_begin_main_frame);
}

void InputHandler::UpdateBrowserControlsState(
    BrowserControlsState constraints,
    BrowserControlsState current,
    bool animate,
    base::optional_ref<const BrowserControlsOffsetTagModifications>
        offset_tag_modifications) {
  compositor_delegate_->UpdateBrowserControlsState(
      constraints, current, animate, offset_tag_modifications);
}

void InputHandler::SetIsHandlingTouchSequence(bool is_handling_touch_sequence) {
  // We should not attempt to start handling a touch sequence twice.
  DCHECK(!is_handling_touch_sequence || !is_handling_touch_sequence_);
  is_handling_touch_sequence_ = is_handling_touch_sequence;
}

bool InputHandler::CurrentScrollNeedsFrameAlignment() const {
  // We need frame-aligned handling of GestureScrollUpdate if an animation
  // is linked to the scroll position.  If we update the scroll offset between
  // tick and draw, then things will be out of sync in the drawn frame.
  const ScrollNode* node = CurrentlyScrollingNode();
  if (node &&
      compositor_delegate_->HasScrollLinkedAnimation(node->element_id)) {
    return true;
  }

  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    // Ensure InputHandler factors in snap animations (during which
    // InputHandler de-latches from the ScrollNode) when queried about
    // nodes it's scrolling which need frame alignment.
    const auto& scroll_tree = GetScrollTree();
    for (const auto& entry : snap_animation_data_map_) {
      if (entry.second.animating_snap_target_ids_ ==
          TargetSnapAreaElementIds()) {
        continue;
      }
      if (const ScrollNode* animating_node =
              scroll_tree.FindNodeFromElementId(entry.first)) {
        if (compositor_delegate_->HasScrollLinkedAnimation(
                animating_node->element_id)) {
          return true;
        }
      }
    }
  }
  return false;
}

std::unique_ptr<SnapSelectionStrategy> InputHandler::CreateSnapStrategy(
    const ScrollState& scroll_state,
    const gfx::PointF& current_offset,
    SnapReason snap_reason) const {
  const gfx::Vector2dF scroll_delta = scroll_state.DeltaOrHint();
  if (latched_scroll_type_ == ui::ScrollInputType::kWheel &&
      scroll_state.delta_granularity() !=
          ui::ScrollGranularity::kScrollByPrecisePixel &&
      !scroll_delta.IsZero() &&
      snap_reason == SnapReason::kScrollOffsetAnimationFinished) {
    // This was an imprecise wheel scroll so use direction snapping.
    // Note: gesture scroll end is delayed in anticipation of future wheel
    // scrolls so it is fired well after the scroll ends as opposed to precise
    // touch devices where we fire it as soon as the user lifts their finger.
    // TODO(crbug.com/40762499): The directional scroll should probably be
    // triggered at gesture scroll begin to improve responsiveness.
    return SnapSelectionStrategy::CreateForDirection(current_offset,
                                                     scroll_delta, true);
  } else {
    return SnapSelectionStrategy::CreateForEndPosition(
        current_offset, did_scroll_x_for_scroll_gesture_,
        did_scroll_y_for_scroll_gesture_);
  }
}

void InputHandler::SetViewportConsumedDelta(
    const ViewportScrollResult& result) {
  if (std::abs(result.outer_viewport_scrolled_delta.x()) > kScrollEpsilon ||
      std::abs(result.outer_viewport_scrolled_delta.y()) > kScrollEpsilon) {
    outer_viewport_consumed_delta_ = true;
  }
  if (std::abs(result.inner_viewport_scrolled_delta.x()) > kScrollEpsilon ||
      std::abs(result.inner_viewport_scrolled_delta.y()) > kScrollEpsilon) {
    inner_viewport_consumed_delta_ = true;
  }
}

TargetSnapAreaElementIds InputHandler::GetAnimatingSnapTargetsForElement(
    ElementId element_id) const {
  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    auto entry = snap_animation_data_map_.find(element_id);
    if (entry != snap_animation_data_map_.end()) {
      return entry->second.animating_snap_target_ids_;
    }
    return TargetSnapAreaElementIds();
  } else {
    return scroll_animating_snap_target_ids_;
  }
}

void InputHandler::SetAnimatingSnapTargetsForElement(
    ElementId element_id,
    TargetSnapAreaElementIds target_ids) {
  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    auto entry = snap_animation_data_map_.find(element_id);
    if (entry != snap_animation_data_map_.end()) {
      entry->second.animating_snap_target_ids_ = target_ids;
    }
  } else {
    scroll_animating_snap_target_ids_ = target_ids;
  }
}

void InputHandler::ClearAnimatingSnapTargetsForElement(ElementId element_id) {
  SetAnimatingSnapTargetsForElement(element_id);
}

void InputHandler::EnsureSnapAnimationData(ElementId element_id) {
  if (features::MultiImplOnlyScrollAnimationsSupported()) {
    if (!snap_animation_data_map_.contains(element_id)) {
      snap_animation_data_map_.insert_or_assign(element_id,
                                                SnapAnimationData());
    }
  }
}

}  // namespace cc