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
content / browser / navigation_transitions / back_forward_transition_animator.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/navigation_transitions/back_forward_transition_animator.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/ranges.h"
#include "cc/slim/layer.h"
#include "cc/slim/solid_color_layer.h"
#include "cc/slim/surface_layer.h"
#include "cc/slim/ui_resource_layer.h"
#include "content/browser/navigation_transitions/back_forward_transition_animation_manager_android.h"
#include "content/browser/navigation_transitions/progress_bar.h"
#include "content/browser/renderer_host/compositor_impl_android.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot.h"
#include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_cache.h"
#include "content/browser/renderer_host/navigation_transitions/navigation_transition_config.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_android.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/browser/web_contents/web_contents_view_android.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents_delegate.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "ui/android/window_android.h"
#include "ui/display/screen.h"
#include "ui/events/back_gesture_event.h"
namespace content {
namespace {
using CacheHitOrMissReason = NavigationTransitionData::CacheHitOrMissReason;
using NavigationDirection =
BackForwardTransitionAnimationManager::NavigationDirection;
using AnimationStage = BackForwardTransitionAnimationManager::AnimationStage;
using SwitchSpringReason = PhysicsModel::SwitchSpringReason;
using SwipeEdge = ui::BackGestureEventSwipeEdge;
using IgnoringInputReason = BackForwardTransitionAnimator::IgnoringInputReason;
using AnimationAbortReason =
BackForwardTransitionAnimator::AnimationAbortReason;
static constexpr base::TimeDelta kDismissScreenshotAfter = base::Seconds(4);
void ResetTransformForLayer(cc::slim::Layer* layer) {
CHECK(layer);
auto transform = layer->transform();
transform.MakeIdentity();
layer->SetTransform(transform);
}
bool ShouldUseFallbackScreenshot(
BackForwardTransitionAnimationManagerAndroid* animation_manager,
NavigationEntryImpl* destination_entry) {
bool use_fallback_screenshot = true;
auto* screenshot = static_cast<NavigationEntryScreenshot*>(
destination_entry->GetUserData(NavigationEntryScreenshot::kUserDataKey));
auto cache_hit_or_miss_reason =
destination_entry->navigation_transition_data()
.cache_hit_or_miss_reason();
if (screenshot) {
gfx::Size screenshot_size = screenshot->dimensions_without_compression();
gfx::Size screen_size = animation_manager->web_contents_view_android()
->GetNativeView()
->GetPhysicalBackingSize();
use_fallback_screenshot = screenshot_size != screen_size;
if (screenshot_size != screen_size) {
cache_hit_or_miss_reason = NavigationTransitionData::
CacheHitOrMissReason::kCacheMissScreenshotOrientation;
} else {
CHECK_EQ(cache_hit_or_miss_reason.value(),
NavigationTransitionData::CacheHitOrMissReason::kCacheHit);
}
}
// TODO(crbug.com/355454946): Consider other ways to capture `kCacheColdStart`
// metric.
UMA_HISTOGRAM_ENUMERATION("Navigation.GestureTransition.CacheHitOrMissReason",
cache_hit_or_miss_reason.value_or(
CacheHitOrMissReason::kCacheMissColdStart));
return use_fallback_screenshot;
}
const char* IgnoringInputReasonToString(IgnoringInputReason reason) {
switch (reason) {
case IgnoringInputReason::kAnimationInvokedOccurred:
return "kAnimationInvokedOccurred";
case IgnoringInputReason::kAnimationCanceledOccurred:
return "kAnimationCanceledOccurred";
case IgnoringInputReason::kNoOccurrence:
return "kNoOccurrence";
}
NOTREACHED();
}
const char* AnimationAbortReasonToString(AnimationAbortReason abort_reason) {
switch (abort_reason) {
case AnimationAbortReason::kRenderWidgetHostDestroyed:
return "kRenderWidgetHostDestroyed";
case AnimationAbortReason::kMainCommitOnSubframeTransition:
return "kMainCommitOnSubframeTransition";
case AnimationAbortReason::kNewCommitInPrimaryMainFrame:
return "kNewCommitInPrimaryMainFrame";
case AnimationAbortReason::kCrossOriginRedirect:
return "kCrossOriginRedirect";
case AnimationAbortReason::kNewCommitWhileDisplayingInvokeAnimation:
return "kNewCommitWhileDisplayingInvokeAnimation";
case AnimationAbortReason::kNewCommitWhileDisplayingCanceledAnimation:
return "kNewCommitWhileDisplayingCanceledAnimation";
case AnimationAbortReason::kNewCommitWhileWaitingForNewRendererToDraw:
return "kNewCommitWhileWaitingForNewRendererToDraw";
case AnimationAbortReason::
kNewCommitWhileWaitingForContentForNavigationEntryShown:
return "kNewCommitWhileWaitingForContentForNavigationEntryShown";
case AnimationAbortReason::kNewCommitWhileDisplayingCrossFadeAnimation:
return "kNewCommitWhileDisplayingCrossFadeAnimation";
case AnimationAbortReason::kNewCommitWhileWaitingForBeforeUnloadResponse:
return "kNewCommitWhileWaitingForBeforeUnloadResponse";
case AnimationAbortReason::kMultipleNavigationRequestsCreated:
return "kMultipleNavigationRequestsCreated";
case AnimationAbortReason::kNavigationEntryDeletedBeforeCommit:
return "kNavigationEntryDeletedBeforeCommit";
case AnimationAbortReason::kPostNavigationFirstFrameTimeout:
return "kPostNavigationFirstFrameTimeout";
case AnimationAbortReason::kChainedBack:
return "kChainedBack";
case AnimationAbortReason::kDetachedFromWindow:
return "kDetachedFromWindow";
case AnimationAbortReason::kRootWindowVisibilityChanged:
return "kRootWindowVisibilityChanged";
case AnimationAbortReason::kCompositorDetached:
return "kCompositorDetached";
case AnimationAbortReason::kAnimationManagerDestroyed:
return "kAnimationManagerDestroyed";
case BackForwardTransitionAnimator::AnimationAbortReason::
kPhysicalSizeChanged:
return "kPhysicalSizeChanged";
}
NOTREACHED();
}
//========================== Fitted animation timeline =========================
//
// The animations for `OnGestureProgressed` are driven purely by user gestures.
// We use `gfx::KeyframeEffect` for progressing the animation in response by
// setting up a fitted animation timeline (one second) and mapping gesture
// progress to the corresponding time value.
//
// The timeline for the scrim animation is also a function of layer's position.
// We also use this fitted timeline for scrim.
//
// Note: The timing function is linear.
static constexpr base::TimeTicks kFittedStart;
static constexpr base::TimeDelta kFittedTimelineDuration = base::Seconds(1);
base::TimeTicks GetFittedTimeTicksForForegroundProgress(float progress) {
return kFittedStart + kFittedTimelineDuration * progress;
}
// 0-indexed as the value will be stored in a bitset.
enum class TargetProperty {
kScrim = 0,
kCrossFade,
kFaviconOpacity,
kFaviconPosition,
};
template <typename KeyFrameType>
struct KeyFrame {
base::TimeDelta time;
KeyFrameType value;
};
// Each `KeyFrame` is interpolated using a linear function.
template <typename KeyFrameType, std::size_t Size>
struct LinearModelConfig {
TargetProperty target_property;
std::array<KeyFrame<KeyFrameType>, Size> key_frames;
};
//============================= Crossfade animation ============================
static constexpr base::TimeDelta kCrossfadeDuration = base::Milliseconds(100);
static constexpr LinearModelConfig<float, 2u> kCrossFadeAnimation{
.target_property = TargetProperty::kCrossFade,
.key_frames = {KeyFrame{
.time = base::TimeDelta(),
.value = 1.0f,
},
KeyFrame{
.time = kCrossfadeDuration,
.value = 0.0f,
}}};
//=============================== Scrim animation ==============================
// The scrim range is from 0.2 to 0 in dark mode and 0.1 to 0 in light mode. The
// scrim value is a linear function of the top layer's position.
static constexpr LinearModelConfig<float, 2u> kScrimAnimationLightMode{
.target_property = TargetProperty::kScrim,
.key_frames = {KeyFrame{
.time = base::TimeDelta(),
.value = 0.1f,
},
KeyFrame{
.time = kFittedTimelineDuration,
.value = 0.0f,
}}};
static constexpr LinearModelConfig<float, 2u> kScrimAnimationDarkMode{
.target_property = TargetProperty::kScrim,
.key_frames = {KeyFrame{
.time = base::TimeDelta(),
.value = 0.2f,
},
KeyFrame{
.time = kFittedTimelineDuration,
.value = 0.0f,
}}};
template <typename KeyFrameType, std::size_t Size>
void AddLinearModelToEffect(
LinearModelConfig<KeyFrameType, Size> config,
std::conditional_t<std::is_same<KeyFrameType, float>::value,
gfx::FloatAnimationCurve::Target,
gfx::TransformAnimationCurve::Target>* target,
gfx::KeyframeEffect& effect) {
using CurveType = std::conditional_t<std::is_same<KeyFrameType, float>::value,
gfx::KeyframedFloatAnimationCurve,
gfx::KeyframedTransformAnimationCurve>;
using KeyframeType =
std::conditional_t<std::is_same<KeyFrameType, float>::value,
gfx::FloatKeyframe, gfx::TransformKeyframe>;
auto curve = CurveType::Create();
for (size_t i = 0; i < Size; ++i) {
const auto& keyframe = config.key_frames.at(i);
curve->AddKeyframe(KeyframeType::Create(/*time=*/keyframe.time,
/*value=*/keyframe.value,
/*timing_function=*/nullptr));
}
curve->set_target(target);
auto model = gfx::KeyframeModel::Create(
/*curve=*/std::move(curve),
/*keyframe_model_id=*/effect.GetNextKeyframeModelId(),
/*target_property_id=*/
static_cast<int>(config.target_property));
effect.AddKeyframeModel(std::move(model));
}
//================================ Fallback UX =================================
//
// Size of the favicon's rounded rectangle background.
constexpr static int kRRectSizeDip = 56;
// Radius of the rounded rectangle.
constexpr static float kRRectRadiusDip = 20.f;
// Relative position of the favicon with respect to the rounded rectangle.
constexpr static int kFaviconPosDip = 16;
static constexpr LinearModelConfig<float, 4u> kRRectOpacityModel{
.target_property = TargetProperty::kFaviconOpacity,
// The opacity is 0.f until 25% progress, and reaches 1.f at 50% progress.
.key_frames = {
KeyFrame{
.time = base::TimeDelta(),
.value = 0.f,
},
KeyFrame{
.time = kFittedTimelineDuration * 0.25,
.value = 0.0f,
},
KeyFrame{
.time = kFittedTimelineDuration * 0.5,
.value = 1.f,
},
KeyFrame{
.time = kFittedTimelineDuration,
.value = 1.f,
},
}};
scoped_refptr<cc::slim::SolidColorLayer> AddRoundedRectangle(
cc::slim::Layer* parent,
int size_px,
float corner_radius_px,
SkColor4f color) {
auto rrect = cc::slim::SolidColorLayer::Create();
// The motion of the fallback UX is driven by the `effect_`. The first ever
// `OnGestureProgressed()` call at the end will move the rrect to its desired
// starting position.
rrect->SetPosition(gfx::PointF(0.f, 0.f));
rrect->SetBounds(gfx::Size(size_px, size_px));
rrect->SetRoundedCorner(gfx::RoundedCornersF(
corner_radius_px, corner_radius_px, corner_radius_px, corner_radius_px));
rrect->SetBackgroundColor(color);
rrect->SetIsDrawable(true);
parent->AddChild(rrect);
return rrect;
}
static constexpr float kFloatTolerance = 0.001f;
[[nodiscard]] bool AlmostEqual(float a, float b) {
return base::IsApproximatelyEqual(a, b, kFloatTolerance);
}
[[nodiscard]] bool IsLessThanOrEqual(float a, float b) {
return a < b || AlmostEqual(a, b);
}
[[nodiscard]] bool IsGreaterThanOrEqual(float a, float b) {
return a > b || AlmostEqual(a, b);
}
} // namespace
std::unique_ptr<BackForwardTransitionAnimator>
BackForwardTransitionAnimator::Factory::Create(
WebContentsViewAndroid* web_contents_view_android,
NavigationControllerImpl* controller,
const ui::BackGestureEvent& gesture,
NavigationDirection nav_direction,
SwipeEdge initiating_edge,
NavigationEntryImpl* destination_entry,
SkBitmap embedder_content,
BackForwardTransitionAnimationManagerAndroid* animation_manager) {
return base::WrapUnique(new BackForwardTransitionAnimator(
web_contents_view_android, controller, gesture, nav_direction,
initiating_edge, destination_entry, std::move(embedder_content),
animation_manager));
}
BackForwardTransitionAnimator::~BackForwardTransitionAnimator() {
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::~BackForwardTransitionAnimator");
CHECK(IsTerminalState()) << StateToString(state_);
switch (ignoring_input_reason_) {
case IgnoringInputReason::kAnimationInvokedOccurred: {
base::UmaHistogramCounts100(
"Navigation.GestureTransition.IgnoredInputCount.AnimationInvoked."
"OnDestination",
ignored_inputs_count_.animation_invoked_on_destination);
base::UmaHistogramCounts100(
"Navigation.GestureTransition.IgnoredInputCount.AnimationInvoked."
"OnSource",
ignored_inputs_count_.animation_invoked_on_source);
break;
}
case IgnoringInputReason::kAnimationCanceledOccurred: {
base::UmaHistogramCounts100(
"Navigation.GestureTransition.IgnoredInputCount.AnimationCanceled."
"OnDestination",
ignored_inputs_count_.animation_canceled_on_destination);
base::UmaHistogramCounts100(
"Navigation.GestureTransition.IgnoredInputCount.AnimationCanceled."
"OnSource",
ignored_inputs_count_.animation_canceled_on_source);
break;
}
case IgnoringInputReason::kNoOccurrence:
break;
}
ResumeDialogs();
ResetTransformForLayer(animation_manager_->web_contents_view_android()
->parent_for_web_page_widgets());
// TODO(crbug.com/40283503): If there is the old visual state hovering
// above the RWHV layer, we need to remove that as well.
if (screenshot_layer_) {
screenshot_scrim_->RemoveFromParent();
screenshot_scrim_.reset();
screenshot_layer_->RemoveFromParent();
screenshot_layer_.reset();
}
ResetLiveOverlayLayer();
if (!fallback_ux_) {
CHECK_NE(ui_resource_id_, cc::UIResourceClient::kUninitializedUIResourceId);
DeleteUIResource(ui_resource_id_);
if (navigation_state_ != NavigationState::kCommitted) {
CHECK(screenshot_);
animation_manager_->navigation_controller()
->GetNavigationEntryScreenshotCache()
->SetScreenshot(nullptr, std::move(screenshot_),
is_copied_from_embedder_);
} else {
// If the navigation has committed then the destination entry is active.
// We don't persist the screenshot for the active entry.
}
}
// This can happen if the navigation started for this gesture was committed
// but another navigation or gesture started before the destination renderer
// produced its first frame.
if (new_render_widget_host_) {
CHECK_EQ(state_, State::kAnimationAborted) << StateToString(state_);
UnregisterNewFrameActivationObserver();
}
}
// protected.
BackForwardTransitionAnimator::BackForwardTransitionAnimator(
WebContentsViewAndroid* web_contents_view_android,
NavigationControllerImpl* controller,
const ui::BackGestureEvent& gesture,
NavigationDirection nav_direction,
SwipeEdge initiating_edge,
NavigationEntryImpl* destination_entry,
SkBitmap embedder_content,
BackForwardTransitionAnimationManagerAndroid* animation_manager)
: nav_direction_(nav_direction),
initiating_edge_(initiating_edge),
destination_entry_id_(destination_entry->GetUniqueID()),
animation_manager_(animation_manager),
is_copied_from_embedder_(destination_entry->navigation_transition_data()
.is_copied_from_embedder()),
device_scale_factor_(animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow()
->GetDipScale()),
physics_model_(GetViewportWidthPx(),
web_contents_view_android->GetNativeView()->GetDipScale()),
latest_progress_gesture_(gesture) {
if (ShouldUseFallbackScreenshot(animation_manager_, destination_entry)) {
fallback_ux_ = {
.color_config = animation_manager_->web_contents_view_android()
->web_contents()
->GetDelegate()
->GetBackForwardTransitionFallbackUXConfig(),
.start_px = CalculateRRectStartPx(),
.end_px = CalculateRRectEndPx(),
};
}
state_ = State::kStarted;
SetupForScreenshotPreview(std::move(embedder_content));
ProcessState();
}
void BackForwardTransitionAnimator::OnGestureProgressed(
const ui::BackGestureEvent& gesture) {
TRACE_EVENT(TRACE_DISABLED_BY_DEFAULT("navigation"),
"BackForwardTransitionAnimator::OnGestureProgressed", "progress",
gesture.progress());
CHECK_EQ(state_, State::kStarted);
// `gesture.progress()` goes from 0.0 to 1.0 regardless of the edge being
// swiped.
CHECK(IsGreaterThanOrEqual(gesture.progress(), 0.f));
CHECK(IsLessThanOrEqual(gesture.progress(), 1.f));
float progress_delta =
gesture.progress() - latest_progress_gesture_.progress();
const float movement = progress_delta * GetViewportWidthPx();
latest_progress_gesture_ = gesture;
const PhysicsModel::Result result =
physics_model_.OnGestureProgressed(movement, base::TimeTicks::Now());
CHECK(!result.done);
// The gesture animations are never considered "finished".
bool animations_finished = SetLayerTransformationAndTickEffect(result);
CHECK(!animations_finished);
}
void BackForwardTransitionAnimator::OnGestureCancelled() {
CHECK_EQ(state_, State::kStarted);
StartInputSuppression(IgnoringInputReason::kAnimationCanceledOccurred);
AdvanceAndProcessState(State::kDisplayingCancelAnimation);
}
void BackForwardTransitionAnimator::OnGestureInvoked() {
CHECK_EQ(state_, State::kStarted);
StartInputSuppression(IgnoringInputReason::kAnimationInvokedOccurred);
if (!StartNavigationAndTrackRequest()) {
// `BackForwardTransitionAnimationManagerAndroid` will destroy `this` upon
// return if the animation is aborted.
if (state_ != State::kAnimationAborted) {
AdvanceAndProcessState(State::kDisplayingCancelAnimation);
}
return;
}
CHECK(tracked_request_);
if (!tracked_request_->is_primary_main_frame) {
// We have suppressed the dialogs when the user has started swiping because
// we don't want any dialogs to disrupt the gesture. For subframe
// navigations, resume the dialogs as soon as the navigation starts as we
// don't want to suppress any dialogs from the main frame.
ResumeDialogs();
}
// `StartNavigationAndTrackRequest()` sets `navigation_state_`.
if (navigation_state_ == NavigationState::kBeforeUnloadDispatched) {
AdvanceAndProcessState(State::kDisplayingCancelAnimation);
return;
}
CHECK_EQ(navigation_state_, NavigationState::kStarted);
AdvanceAndProcessState(State::kDisplayingInvokeAnimation);
}
void BackForwardTransitionAnimator::OnContentForNavigationEntryShown() {
// Might be called multiple times if user swipes again before NTP fade
// has finished.
if (state_ != State::kWaitingForContentForNavigationEntryShown) {
TRACE_EVENT(
"browser,navigation",
"BackForwardTransitionAnimator::OnContentForNavigationEntryShown");
return;
}
if (!embedder_live_content_clone_) {
// The embedder has finished cross-fading from the screenshot to the new
// content. Unregister `this` from the `RenderWidgetHost` to stop the
// `OnRenderWidgetHostDestroyed()` notification.
CHECK(new_render_widget_host_);
new_render_widget_host_->RemoveObserver(animation_manager_);
new_render_widget_host_ = nullptr;
}
AdvanceAndProcessState(State::kAnimationFinished);
}
AnimationStage BackForwardTransitionAnimator::GetCurrentAnimationStage() {
switch (state_) {
case State::kDisplayingInvokeAnimation:
return AnimationStage::kInvokeAnimation;
case State::kWaitingForContentForNavigationEntryShown:
return AnimationStage::kWaitingForEmbedderContentForCommittedEntry;
case State::kAnimationFinished:
case State::kAnimationAborted:
return AnimationStage::kNone;
default:
return AnimationStage::kOther;
}
}
void BackForwardTransitionAnimator::OnAnimate(
base::TimeTicks frame_begin_time) {
bool animation_finished = false;
switch (state_) {
case State::kDisplayingCancelAnimation: {
PhysicsModel::Result result = physics_model_.OnAnimate(frame_begin_time);
std::ignore = SetLayerTransformationAndTickEffect(result);
animation_finished = result.done;
break;
}
case State::kDisplayingInvokeAnimation: {
PhysicsModel::Result result = physics_model_.OnAnimate(frame_begin_time);
animation_finished = SetLayerTransformationAndTickEffect(result);
if (progress_bar_) {
progress_bar_->Animate(frame_begin_time);
}
break;
}
case State::kDisplayingCrossFadeAnimation: {
// The cross-fade model.
CHECK_EQ(effect_.keyframe_models().size(), 1U);
effect_.Tick(frame_begin_time);
// `Tick()` has the side effect of removing all the finished models. At
// the last frame of `OnFloatAnimated()`, the model is still running, but
// is immediately removed after the `Tick()` WITHOUT advancing to the
// finished or pending deletion state.
animation_finished = effect_.keyframe_models().empty();
break;
}
case State::kStarted:
case State::kWaitingForBeforeUnloadResponse:
case State::kWaitingForNewRendererToDraw:
case State::kWaitingForContentForNavigationEntryShown:
case State::kAnimationFinished:
case State::kAnimationAborted:
return;
}
if (animation_finished) {
switch (state_) {
case State::kDisplayingInvokeAnimation: {
CHECK_EQ(navigation_state_, NavigationState::kCommitted);
OnInvokeAnimationDisplayed();
break;
}
case State::kDisplayingCancelAnimation: {
OnCancelAnimationDisplayed();
break;
}
case State::kDisplayingCrossFadeAnimation: {
OnCrossFadeAnimationDisplayed();
break;
}
case State::kStarted:
case State::kWaitingForBeforeUnloadResponse:
case State::kWaitingForNewRendererToDraw:
case State::kWaitingForContentForNavigationEntryShown:
case State::kAnimationFinished:
case State::kAnimationAborted:
NOTREACHED_IN_MIGRATION();
break;
}
} else {
animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow()
->SetNeedsAnimate();
}
}
void BackForwardTransitionAnimator::OnRenderWidgetHostDestroyed(
RenderWidgetHost* widget_host) {
if (widget_host != new_render_widget_host_) {
return;
}
// The subscribed `RenderWidgetHost` is getting destroyed. We must cancel the
// transition and reset everything. This can happen for a client redirect,
// where Viz never activates a frame from the committed renderer.
AbortAnimation(AnimationAbortReason::kRenderWidgetHostDestroyed);
}
// This is only called after we subscribe to the new `RenderWidgetHost` when the
// navigation is ready to commit, meaning this method won't be called for
// 204/205/Download navigations, and won't be called if the navigation is
// cancelled.
void BackForwardTransitionAnimator::OnRenderFrameMetadataChangedAfterActivation(
base::TimeTicks activation_time) {
CHECK(tracked_request_);
// We shouldn't get this notification for subframe navigations because we
// never subscribe to the `RenderWidgetHost` for subframes.
//
// This is for simplicity: non-OOPIF / VideoSubmitter subframes share the same
// `RenderWidgetHost` with the embedder thus it's difficult to differentiate
// the frames submitted from a subframe vs from its embedder. For subframe
// navigations, we play the cross-fade animation as soon as the invoke
// animation has finished (see `DidFinishNavigation()`'s treatment for
// subframes).
CHECK(tracked_request_->is_primary_main_frame);
// `new_render_widget_host_` and
// `primary_main_frame_navigation_entry_item_sequence_number_` are set when
// the navigation is ready to commit.
CHECK(new_render_widget_host_);
CHECK_NE(primary_main_frame_navigation_entry_item_sequence_number_,
cc::RenderFrameMetadata::kInvalidItemSequenceNumber);
// Viz can activate the frame before the DidCommit message arrives at the
// browser (kStarted), since we start to get this notification when the
// browser tells the renderer to commit the navigation.
CHECK(navigation_state_ == NavigationState::kCommitted ||
navigation_state_ == NavigationState::kStarted);
// Again this notification is only received after the browser tells the
// renderer to commit the navigation. So we must have started playing the
// invoke animation, or the invoke animation has finished.
CHECK(state_ == State::kDisplayingInvokeAnimation ||
state_ == State::kWaitingForNewRendererToDraw)
<< StateToString(state_);
CHECK(!viz_has_activated_first_frame_)
<< "OnRenderFrameMetadataChangedAfterActivation can only be called once.";
if (auto last_render_frame_metadata_sequence_number =
new_render_widget_host_->render_frame_metadata_provider()
->LastRenderFrameMetadata()
.primary_main_frame_item_sequence_number;
last_render_frame_metadata_sequence_number !=
primary_main_frame_navigation_entry_item_sequence_number_) {
// We shouldn't dismiss the screenshot if the activated frame isn't what we
// are expecting.
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::"
"OnRenderFrameMetadataChangedAfterActivation",
"this.sequence_number",
primary_main_frame_navigation_entry_item_sequence_number_,
"LastRenderFrameMetadata.sequence_number",
last_render_frame_metadata_sequence_number);
return;
}
viz_has_activated_first_frame_ = true;
// No longer interested in any other compositor frame submission
// notifications. We can safely dismiss the previewed screenshot now.
UnregisterNewFrameActivationObserver();
if (state_ == State::kWaitingForNewRendererToDraw) {
// Only display the crossfade animation if the old page is completely out of
// the viewport.
AdvanceAndProcessState(State::kDisplayingCrossFadeAnimation);
}
}
// We only use `DidStartNavigation()` for signalling that the renderer has acked
// the BeforeUnload message to proceed (begin) the navigation.
void BackForwardTransitionAnimator::DidStartNavigation(
NavigationHandle* navigation_handle) {
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::DidStartNavigation",
"navigation_id", navigation_handle->GetNavigationId());
// We need to set this state here since for same-document navigations, the
// commit message is sent before the animator starts tracking the navigation.
if (is_starting_navigation_) {
NavigationRequest::From(navigation_handle)
->set_was_initiated_by_animated_transition();
}
if (!tracked_request_) {
// We could reach here for an early-commit navigation:
// - The animator only tracks the request's ID after `GoToIndex()` returns.
// - In early commit, `DidStartNavigation()` is called during `GoToIndex()`.
//
// Early return here and let `StartNavigationAndTrackRequest()` to set the
// `navigation_state_`.
return;
}
if (tracked_request_->navigation_id != navigation_handle->GetNavigationId()) {
return;
}
CHECK_EQ(navigation_state_, NavigationState::kBeforeUnloadDispatched);
navigation_state_ = NavigationState::kBeforeUnloadAckedProceed;
CHECK(state_ == State::kWaitingForBeforeUnloadResponse ||
state_ == State::kDisplayingCancelAnimation);
AdvanceAndProcessState(State::kDisplayingInvokeAnimation);
}
void BackForwardTransitionAnimator::ReadyToCommitNavigation(
NavigationHandle* navigation_handle) {
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::ReadyToCommitNavigation",
"navigation_id", navigation_handle->GetNavigationId());
CHECK(!navigation_handle->IsSameDocument());
if (!tracked_request_ ||
tracked_request_->navigation_id != navigation_handle->GetNavigationId()) {
// A unrelated navigation is ready to commit. This is possible with
// NavigationQueuing. We ignore the unrelated navigation request.
return;
}
if (!tracked_request_->is_primary_main_frame) {
// We don't subscribe to the new widget host for subframes, nor clone the
// old surface layer.
return;
}
SubscribeToNewRenderWidgetHost(
static_cast<NavigationRequest*>(navigation_handle));
// Clone the Surface of the outgoing page for same-RFH navigations. We need to
// this sooner for these navigations since the SurfaceID is updated when
// sending the commit message.
// For cross-RFH navigations, this is done as a part of processing the
// DidCommit ack from the renderer.
auto* navigation_request = NavigationRequest::From(navigation_handle);
auto* old_rfh = RenderFrameHostImpl::FromID(
navigation_request->GetPreviousRenderFrameHostId());
auto* new_rfh = navigation_request->GetRenderFrameHost();
// Ignore early swap cases for example crashed pages. They are same-RFH
// navigations but the current SurfaceID of this RFH doesn't refer to content
// from the old Document.
if (navigation_request->early_render_frame_host_swap_type() ==
NavigationRequest::EarlyRenderFrameHostSwapType::kNone &&
old_rfh == new_rfh) {
MaybeCloneOldSurfaceLayer(old_rfh->GetView());
}
}
// - For a primary main frame navigation, we only use `DidFinishNavigation()`
// for navigations that never commit (204/205/downloads), or the cancelled /
// replaced navigations. For a committed navigation, everything is set in
// `OnDidNavigatePrimaryMainFramePreCommit()`, which is before the old
// `RenderViewHost` is swapped out.
//
// - For subframe navigation, we bring the fallback UX to the full viewport when
// the subframe navigation commits.
void BackForwardTransitionAnimator::DidFinishNavigation(
NavigationHandle* navigation_handle) {
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::DidFinishNavigation",
"navigation_id", navigation_handle->GetNavigationId());
// If we haven't started tracking a navigation, or if `navigation_handle`
// isn't what we tracked, or if this `navigation_handle` has committed, ignore
// it.
//
// TODO(https://crbug.com/357060513): If we are tracking a subframe request
// from subframe A while subframe B navigates, the request in subframe B is
// ignored completely. We should decide what to do before launch.
if (!tracked_request_ ||
tracked_request_->navigation_id != navigation_handle->GetNavigationId()) {
return;
}
if (navigation_handle->HasCommitted()) {
if (navigation_handle->IsInPrimaryMainFrame()) {
// If this is a committed primary main frame navigation request, we must
// have already set the states in
// `OnDidNavigatePrimaryMainFramePreCommit()`.
CHECK(tracked_request_->is_primary_main_frame);
CHECK_EQ(navigation_state_, NavigationState::kCommitted);
} else {
// If this is a committed subframe request, animate the fallback UX to
// occupy the full viewport.
CHECK(!tracked_request_->is_primary_main_frame);
navigation_state_ = NavigationState::kCommitted;
physics_model_.OnNavigationFinished(/*navigation_committed=*/true);
CHECK_EQ(state_, State::kDisplayingInvokeAnimation);
// Signals that when the invoke animation finishes, play the cross-fade
// animation directly.
viz_has_activated_first_frame_ = true;
}
return;
}
CHECK_EQ(state_, State::kDisplayingInvokeAnimation);
CHECK_EQ(navigation_state_, NavigationState::kStarted);
navigation_state_ = NavigationState::kCancelled;
physics_model_.OnNavigationFinished(/*navigation_committed=*/false);
// 204/205/Download, or the ongoing navigation is cancelled. We need
// to animate the old page back.
if (old_surface_clone_) {
// We might already have cloned the old surface. Reset it since we don't
// need it.
old_surface_clone_->RemoveFromParent();
old_surface_clone_.reset();
}
// TODO(crbug.com/41482488): We might need a better UX than
// just display the cancel animation.
AdvanceAndProcessState(State::kDisplayingCancelAnimation);
}
void BackForwardTransitionAnimator::OnDidNavigatePrimaryMainFramePreCommit(
NavigationRequest* navigation_request,
RenderFrameHostImpl* old_host,
RenderFrameHostImpl* new_host) {
TRACE_EVENT(
"browser,navigation",
"BackForwardTransitionAnimator::OnDidNavigatePrimaryMainFramePreCommit");
// If a navigation commits in the primary main frame while we are tracking the
// subframe requests, abort the animation immediately.
if (tracked_request_ && !tracked_request_->is_primary_main_frame) {
AbortAnimation(AnimationAbortReason::kMainCommitOnSubframeTransition);
return;
}
CHECK(navigation_request->IsInPrimaryMainFrame());
std::optional<AnimationAbortReason> abort_reason;
switch (state_) {
case State::kStarted:
CHECK(!tracked_request_);
CHECK_EQ(navigation_state_, NavigationState::kNotStarted);
// A new navigation finished in the primary main frame while the user is
// swiping across the screen. For simplicity, destroy this class if the
// new navigation was from the primary main frame.
abort_reason = AnimationAbortReason::kNewCommitInPrimaryMainFrame;
break;
case State::kDisplayingInvokeAnimation: {
// We can only get to `kDisplayingInvokeAnimation` if we have started
// tracking the request.
CHECK(tracked_request_);
if (navigation_state_ == NavigationState::kStarted) {
if (tracked_request_->navigation_id !=
navigation_request->GetNavigationId()) {
// A previously pending navigation has committed since we started
// tracking our gesture navigation. Ignore this committed navigation.
return;
}
// Resume the dialogs. When the transition starts we deferred the
// dialogs. Now the old page was unloaded and we need to resume the
// dialogs immediately so we don't accidentally defer the dialogs on the
// new page.
ResumeDialogs();
// Before we display the crossfade animation to show the new page, we
// need to check if the new page matches the origin of the screenshot.
// We are not allowed to cross-fade from a screenshot of A.com to a page
// of B.com.
bool land_on_error_page = navigation_request->DidEncounterError();
bool different_commit_origin = false;
const auto& original_url = navigation_request->GetOriginalRequestURL();
const auto& committed_url = navigation_request->GetURL();
// The origin comparison is tricky because we do not know the precise
// origin of the initial `NavigationRequest` (which depends on response
// headers like CSP sandbox). It is reasonable to allow the animation to
// proceed if the origins derived from the URL remains same-origin at
// the end of the navigation, even if there is a sandboxing difference
// that leads to an opaque origin. Also, URLs that can inherit origins
// (e.g., about:blank) do not generally redirect, so it should be safe
// to ignore inherited origins. Thus, we compare origins derived from
// the URLs, after first checking whether the URL itself remains
// unchanged (to account for URLs with opaque origins that won't appear
// equal to each other, like data: URLs). This addresses concerns about
// converting between URLs and origins (see
// https://chromium.googlesource.com/chromium/src/+/main/docs/security/origin-vs-url.md).
if (original_url != committed_url) {
different_commit_origin =
!url::Origin::Create(original_url)
.IsSameOriginWith(url::Origin::Create(committed_url));
}
if (!land_on_error_page && different_commit_origin) {
abort_reason = AnimationAbortReason::kCrossOriginRedirect;
break;
}
// Our gesture navigation has committed.
navigation_state_ = NavigationState::kCommitted;
physics_model_.OnNavigationFinished(/*navigation_committed=*/true);
if (land_on_error_page) {
// TODO(crbug.com/41482489): Implement a different UX if we
// decide not show the animation at all (i.e. abort animation early
// when we receive the response header).
}
// We need to check if hosts have changed, since they could have stayed
// the same if the old page was early-swapped out, which can happen in
// navigations from a crashed page.
//
// This is done sooner (in ReadyToCommit) for same-RFH navigations
// since the SurfaceID changes before DidCommit for these navigations.
if (old_host != new_host) {
MaybeCloneOldSurfaceLayer(old_host->GetView());
}
} else {
// Our navigation has already committed while a second navigation
// commits. This can be a client redirect: A.com -> B.com and B.com's
// document redirects to C.com, while we are still playing the post
// commit-pending invoke animation to bring B.com's screenshot to the
// center of the viewport.
CHECK_EQ(navigation_state_, NavigationState::kCommitted);
abort_reason =
AnimationAbortReason::kNewCommitWhileDisplayingInvokeAnimation;
}
break;
}
case State::kDisplayingCancelAnimation: {
// We won't reach `NavigationState::kBeforeUnloadDispatched` because
// if the request is blocked on BeforeUnload ack is cancelled, we will
// receive `OnUnstartedNavigationCancelled()` where we advance
// `navigation_state_` to `NavigationState::kCancelledBeforeStart`.
CHECK(navigation_state_ == NavigationState::kNotStarted ||
navigation_state_ == NavigationState::kBeforeUnloadDispatched ||
navigation_state_ == NavigationState::kCancelled ||
navigation_state_ == NavigationState::kCancelledBeforeStart)
<< NavigationStateToString(navigation_state_);
// A navigation finished while we are displaying the cancel animation.
// For simplicity, destroy `this` and reset everything.
abort_reason =
AnimationAbortReason::kNewCommitWhileDisplayingCanceledAnimation;
break;
}
case State::kWaitingForNewRendererToDraw:
// Our navigation has already committed while a second navigation commits.
// This can be a client redirect: A.com -> B.com and B.com's document
// redirects to C.com, before B.com's renderer even submits a new frame.
CHECK_EQ(navigation_state_, NavigationState::kCommitted);
CHECK(tracked_request_);
abort_reason =
AnimationAbortReason::kNewCommitWhileWaitingForNewRendererToDraw;
break;
case State::kWaitingForContentForNavigationEntryShown:
// Our navigation has already committed while waiting for a native
// entry to be finished drawing by the embedder.
CHECK_EQ(navigation_state_, NavigationState::kCommitted);
CHECK(tracked_request_);
abort_reason = AnimationAbortReason::
kNewCommitWhileWaitingForContentForNavigationEntryShown;
break;
case State::kDisplayingCrossFadeAnimation: {
// Our navigation has already committed while a second navigation commits.
// This can be a client redirect: A.com -> B.com and B.com's document
// redirects to C.com, while we are cross-fading from B.com's screenshot
// to whatever is underneath the screenshot.
CHECK_EQ(navigation_state_, NavigationState::kCommitted);
CHECK(tracked_request_);
abort_reason =
AnimationAbortReason::kNewCommitWhileDisplayingCrossFadeAnimation;
break;
}
case State::kWaitingForBeforeUnloadResponse:
abort_reason =
AnimationAbortReason::kNewCommitWhileWaitingForBeforeUnloadResponse;
break;
case State::kAnimationFinished:
case State::kAnimationAborted:
NOTREACHED_IN_MIGRATION()
<< "No navigations can commit during the animator's destruction "
"because the destruction is atomic.";
break;
}
if (abort_reason) {
AbortAnimation(abort_reason.value());
}
}
// TODO(https://crbug.com/357094180): We should cancel the transition if a
// unrelated request shows a beforeunload dialog.
void BackForwardTransitionAnimator::OnNavigationCancelledBeforeStart(
NavigationHandle* navigation_handle) {
if (!tracked_request_ ||
tracked_request_->navigation_id != navigation_handle->GetNavigationId()) {
// A unrelated request is cancelled before start.
TRACE_EVENT(
"browser,navigation",
"BackForwardTransitionAnimator::OnNavigationCancelledBeforeStart",
"navigation_id", navigation_handle->GetNavigationId());
return;
}
// For now only a BeforeUnload can defer the start of a navigation.
//
// NOTE: Even if the renderer acks the BeforeUnload message to proceed the
// navigation, the navigation can still fail (see the early out in
// BeginNavigationImpl()). However the animator's `navigation_state_` will
// remain `NavigationState::kBeforeUnloadDispatched` because we only advance
// from `NavigationState::kBeforeUnloadDispatched` to the next state at
// `DidStartNavigation()`. In other words, if for any reason the navigation
// fails after the renderer's ack, the below CHECK_EQ still holds.
CHECK_EQ(navigation_state_, NavigationState::kBeforeUnloadDispatched);
navigation_state_ = NavigationState::kCancelledBeforeStart;
if (state_ == State::kWaitingForBeforeUnloadResponse) {
// The cancel animation has already finished.
AdvanceAndProcessState(State::kAnimationFinished);
} else {
// Let the cancel animation finish playing. We will advance to
// `State::kAnimationFinished`.
CHECK_EQ(state_, State::kDisplayingCancelAnimation);
}
}
void BackForwardTransitionAnimator::MaybeRecordIgnoredInput(
const blink::WebInputEvent& event) {
if (event.GetType() != blink::WebInputEvent::Type::kTouchStart) {
return;
}
CHECK(blink::WebInputEvent::IsTouchEventType(event.GetType()));
const auto& touch_event = static_cast<const blink::WebTouchEvent&>(event);
for (auto& touch : touch_event.touches) {
// Only counting initial press touch instances.
if (touch.state != blink::mojom::TouchState::kStatePressed) {
continue;
}
const auto touch_position_x =
touch.PositionInScreen().x() * device_scale_factor_;
const auto touch_position_y =
touch.PositionInScreen().y() * device_scale_factor_;
bool on_destination = false;
gfx::Rect viewport_rect =
gfx::Rect(animation_manager_->web_contents_view_android()
->GetNativeView()
->GetPhysicalBackingSize());
if (nav_direction_ == NavigationDirection::kForward) {
// In forward navigations, the screenshot is on top so, count the touch
// event if it hits the screenshot.
on_destination = screenshot_layer_->transform()
.MapRect(viewport_rect)
.Contains(touch_position_x, touch_position_y);
} else {
// In back navigations, the live page is on top so, count the touch event
// if it hits the live page.
on_destination = !animation_manager_->web_contents_view_android()
->parent_for_web_page_widgets()
->transform()
.MapRect(viewport_rect)
.Contains(touch_position_x, touch_position_y);
}
switch (ignoring_input_reason_) {
case IgnoringInputReason::kAnimationInvokedOccurred: {
if (on_destination) {
++ignored_inputs_count_.animation_invoked_on_destination;
} else {
++ignored_inputs_count_.animation_invoked_on_source;
}
break;
}
case IgnoringInputReason::kAnimationCanceledOccurred: {
if (on_destination) {
++ignored_inputs_count_.animation_canceled_on_destination;
} else {
++ignored_inputs_count_.animation_canceled_on_source;
}
break;
}
case IgnoringInputReason::kNoOccurrence:
break;
}
}
}
void BackForwardTransitionAnimator::AbortAnimation(
AnimationAbortReason abort_reason) {
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::AbortAnimation", "abort_reason",
AnimationAbortReasonToString(abort_reason));
base::UmaHistogramEnumeration(
"Navigation.GestureTransition.AnimationAbortReason", abort_reason);
AdvanceAndProcessState(State::kAnimationAborted);
}
bool BackForwardTransitionAnimator::IsTerminalState() {
return state_ == State::kAnimationFinished ||
state_ == State::kAnimationAborted;
}
void BackForwardTransitionAnimator::OnFloatAnimated(
const float& value,
int target_property_id,
gfx::KeyframeModel* keyframe_model) {
TRACE_EVENT(TRACE_DISABLED_BY_DEFAULT("navigation"),
"BackForwardTransitionAnimator::OnFloatAnimated", "value", value,
"property_id", target_property_id);
TargetProperty property = static_cast<TargetProperty>(target_property_id);
switch (property) {
case TargetProperty::kScrim: {
CHECK(screenshot_scrim_);
auto scrim = SkColors::kBlack;
scrim.fA = value;
screenshot_scrim_->SetBackgroundColor(scrim);
return;
}
case TargetProperty::kCrossFade: {
CHECK(screenshot_layer_);
screenshot_layer_->SetOpacity(value);
return;
}
case TargetProperty::kFaviconOpacity: {
CHECK(rounded_rectangle_);
rounded_rectangle_->SetOpacity(value);
return;
}
case TargetProperty::kFaviconPosition: {
break;
}
}
NOTREACHED_IN_MIGRATION();
}
void BackForwardTransitionAnimator::OnTransformAnimated(
const gfx::TransformOperations& transform,
int target_property_id,
gfx::KeyframeModel* keyframe_model) {
TRACE_EVENT(TRACE_DISABLED_BY_DEFAULT("navigation"),
"BackForwardTransitionAnimator::OnTransformAnimated",
"property_id", target_property_id, "transform",
transform.Apply().ToString());
TargetProperty property = static_cast<TargetProperty>(target_property_id);
switch (property) {
case TargetProperty::kFaviconPosition: {
CHECK(fallback_ux_);
CHECK(rounded_rectangle_);
rounded_rectangle_->SetTransform(transform.Apply());
return;
}
case TargetProperty::kScrim:
case TargetProperty::kCrossFade:
case TargetProperty::kFaviconOpacity:
break;
}
NOTREACHED_IN_MIGRATION();
}
void BackForwardTransitionAnimator::OnCancelAnimationDisplayed() {
CHECK_EQ(effect_.keyframe_models()[0]->TargetProperty(),
static_cast<int>(TargetProperty::kScrim));
if (navigation_state_ == NavigationState::kBeforeUnloadDispatched) {
AdvanceAndProcessState(State::kWaitingForBeforeUnloadResponse);
return;
}
effect_.RemoveAllKeyframeModels();
if (embedder_live_content_clone_) {
AdvanceAndProcessState(State::kWaitingForContentForNavigationEntryShown);
} else {
AdvanceAndProcessState(State::kAnimationFinished);
}
}
void BackForwardTransitionAnimator::OnInvokeAnimationDisplayed() {
ResetLiveOverlayLayer();
if (progress_bar_) {
progress_bar_->GetLayer()->RemoveFromParent();
progress_bar_.reset();
}
// The scrim timeline is a function of the top layer's position. At the end of
// the invoke animation, the top layer is completely out of the viewport, so
// the `KeyFrameModel` for the scrim should also be exhausted and removed.
CHECK(effect_.keyframe_models().empty());
if (is_copied_from_embedder_) {
AdvanceAndProcessState(State::kWaitingForContentForNavigationEntryShown);
} else if (viz_has_activated_first_frame_) {
AdvanceAndProcessState(State::kDisplayingCrossFadeAnimation);
} else {
AdvanceAndProcessState(State::kWaitingForNewRendererToDraw);
}
}
void BackForwardTransitionAnimator::OnCrossFadeAnimationDisplayed() {
CHECK(effect_.keyframe_models().empty());
AdvanceAndProcessState(State::kAnimationFinished);
}
// static.
bool BackForwardTransitionAnimator::CanAdvanceTo(State from, State to) {
switch (from) {
case State::kStarted:
return to == State::kDisplayingCancelAnimation ||
to == State::kDisplayingInvokeAnimation ||
to == State::kAnimationAborted;
case State::kWaitingForBeforeUnloadResponse:
return to == State::kDisplayingInvokeAnimation ||
to == State::kAnimationFinished || to == State::kAnimationAborted;
case State::kDisplayingInvokeAnimation:
return to == State::kDisplayingCrossFadeAnimation ||
to == State::kWaitingForNewRendererToDraw ||
// A second navigation replaces the current one, or the user hits
// the stop button.
to == State::kDisplayingCancelAnimation ||
to == State::kWaitingForContentForNavigationEntryShown ||
to == State::kAnimationAborted;
case State::kWaitingForNewRendererToDraw:
return to == State::kDisplayingCrossFadeAnimation ||
to == State::kAnimationAborted;
case State::kWaitingForContentForNavigationEntryShown:
return to == State::kAnimationFinished || to == State::kAnimationAborted;
case State::kDisplayingCrossFadeAnimation:
return to == State::kAnimationFinished || to == State::kAnimationAborted;
case State::kDisplayingCancelAnimation:
return to == State::kAnimationFinished ||
// The cancel animation has finished for a dispatched BeforeUnload
// message.
to == State::kWaitingForBeforeUnloadResponse ||
// The renderer acks the BeforeUnload message to proceed the
// navigation, BEFORE the cancel animation finishes.
to == State::kDisplayingInvokeAnimation ||
to == State::kWaitingForContentForNavigationEntryShown ||
to == State::kAnimationAborted;
case State::kAnimationFinished:
case State::kAnimationAborted:
NOTREACHED();
}
}
// static.
const char* BackForwardTransitionAnimator::StateToString(State state) {
switch (state) {
case State::kStarted:
return "kStarted";
case State::kDisplayingCancelAnimation:
return "kDisplayingCancelAnimation";
case State::kDisplayingInvokeAnimation:
return "kDisplayingInvokeAnimation";
case State::kWaitingForNewRendererToDraw:
return "kWaitingForNewRendererToDraw";
case State::kWaitingForContentForNavigationEntryShown:
return "kWaitingForContentForNavigationEntryShown";
case State::kDisplayingCrossFadeAnimation:
return "kDisplayingCrossFadeAnimation";
case State::kAnimationFinished:
return "kAnimationFinished";
case State::kWaitingForBeforeUnloadResponse:
return "kWaitingForBeforeUnloadResponse";
case State::kAnimationAborted:
return "kAnimationAborted";
}
NOTREACHED();
}
// static.
const char* BackForwardTransitionAnimator::NavigationStateToString(
NavigationState state) {
switch (state) {
case NavigationState::kNotStarted:
return "kNotStarted";
case NavigationState::kBeforeUnloadDispatched:
return "kBeforeUnloadDispatched";
case NavigationState::kBeforeUnloadAckedProceed:
return "kBeforeUnloadAckedProceed";
case NavigationState::kCancelledBeforeStart:
return "kCancelledBeforeStart";
case NavigationState::kStarted:
return "kStarted";
case NavigationState::kCommitted:
return "kCommitted";
case NavigationState::kCancelled:
return "kCancelled";
}
NOTREACHED();
}
void BackForwardTransitionAnimator::
InitializeEffectForGestureProgressAnimation() {
// The KeyFrameModel for scrim is added when we set up the screenshot layer,
// at which we must have no models yet.
CHECK(effect_.keyframe_models().empty());
const blink::web_pref::WebPreferences& web_prefs =
animation_manager_->web_contents_view_android()
->web_contents()
->GetOrCreateWebPreferences();
if (web_prefs.preferred_color_scheme ==
blink::mojom::PreferredColorScheme::kDark) {
AddLinearModelToEffect(kScrimAnimationDarkMode, this, effect_);
} else {
AddLinearModelToEffect(kScrimAnimationLightMode, this, effect_);
}
if (rounded_rectangle_) {
CHECK(fallback_ux_);
AddLinearModelToEffect(kRRectOpacityModel, this, effect_);
gfx::TransformOperations start;
start.AppendTranslate(fallback_ux_->start_px.x(),
fallback_ux_->start_px.y(), 0.f);
gfx::TransformOperations end;
end.AppendTranslate(fallback_ux_->end_px.x(), fallback_ux_->end_px.y(),
0.f);
AddLinearModelToEffect(
LinearModelConfig<gfx::TransformOperations, 2u>{
.target_property = TargetProperty::kFaviconPosition,
.key_frames =
{
KeyFrame{
.time = base::TimeDelta(),
.value = start,
},
KeyFrame{
.time = kFittedTimelineDuration,
.value = end,
},
},
},
this, effect_);
}
}
void BackForwardTransitionAnimator::InitializeEffectForCrossfadeAnimation() {
// Before we add the cross-fade model, the scrim model must have finished.
CHECK(effect_.keyframe_models().empty());
AddLinearModelToEffect(kCrossFadeAnimation, this, effect_);
}
void BackForwardTransitionAnimator::AdvanceAndProcessState(State state) {
CHECK(CanAdvanceTo(state_, state))
<< "Cannot advance from " << StateToString(state_) << " to "
<< StateToString(state);
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::AdvanceAndProcessState", "from",
StateToString(state_), "to", StateToString(state));
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::AdvanceAndProcessState",
"navigation_state", NavigationStateToString(navigation_state_));
auto previous_animation_stage = GetCurrentAnimationStage();
state_ = state;
if (previous_animation_stage != GetCurrentAnimationStage()) {
animation_manager_->OnAnimationStageChanged();
}
ProcessState();
}
void BackForwardTransitionAnimator::ProcessState() {
switch (state_) {
case State::kStarted: {
DeferDialogs();
break;
// `this` will be waiting for the `OnGestureProgressed` call.
}
case State::kDisplayingCancelAnimation: {
switch (navigation_state_) {
case NavigationState::kNotStarted: {
// When the user lifts the finger and signals not to start the
// navigation.
physics_model_.SwitchSpringForReason(
SwitchSpringReason::kGestureCancelled);
ResumeDialogs();
break;
}
case NavigationState::kBeforeUnloadDispatched: {
// Notify the physics model we need to animate the active page back to
// the center of the viewport because the browser has asked the
// renderer to ack the BeforeUnload message. The renderer may need to
// show a prompt to ask for the user input.
physics_model_.SwitchSpringForReason(
SwitchSpringReason::kBeforeUnloadDispatched);
// We don't resume the TAB dialog if there is a BeforeUnload pending.
break;
}
case NavigationState::kCancelledBeforeStart: {
// The user has interacted with the prompt to not start the
// navigation. We are waiting for the ongoing cancel animation to
// finish.
ResumeDialogs();
break;
}
case NavigationState::kCancelled: {
// When the ongoing navigation is cancelled because the user hits stop
// or the navigation was replaced by another navigation,
// `OnDidFinishNavigation()` has already notified the physics model to
// switch to the cancel spring.
ResumeDialogs();
break;
}
case NavigationState::kBeforeUnloadAckedProceed:
case NavigationState::kStarted:
case NavigationState::kCommitted:
NOTREACHED_IN_MIGRATION()
<< NavigationStateToString(navigation_state_);
break;
}
CHECK(animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow());
animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow()
->SetNeedsAnimate();
break;
}
case State::kDisplayingInvokeAnimation: {
if (navigation_state_ == NavigationState::kBeforeUnloadAckedProceed) {
// Notify the physics model that the renderer has ack'ed BeforeUnload
// and the navigation shall proceed.
physics_model_.SwitchSpringForReason(
SwitchSpringReason::kBeforeUnloadAckProceed);
navigation_state_ = NavigationState::kStarted;
} else {
// Else, we must have started the navigation.
CHECK_EQ(navigation_state_, NavigationState::kStarted);
physics_model_.SwitchSpringForReason(
SwitchSpringReason::kGestureInvoked);
}
CHECK(animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow());
SetupProgressBar();
animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow()
->SetNeedsAnimate();
break;
};
case State::kWaitingForBeforeUnloadResponse: {
// No-op. Waiting for the renderer's ack before we can proceed with the
// navigation and animation or cancel everything.
break;
}
case State::kWaitingForNewRendererToDraw: {
dismiss_screenshot_timer_.Start(
FROM_HERE, kDismissScreenshotAfter,
base::BindOnce(
&BackForwardTransitionAnimator::OnPostNavigationFirstFrameTimeout,
weak_ptr_factory_.GetWeakPtr()));
// No-op. Waiting for `OnRenderFrameMetadataChangedAfterActivation()`.
break;
}
case State::kWaitingForContentForNavigationEntryShown:
// No-op.
break;
case State::kDisplayingCrossFadeAnimation: {
dismiss_screenshot_timer_.Stop();
// Before we start displaying the crossfade animation,
// `parent_for_web_page_widgets()` is completely out of the viewport. This
// layer is reused for new content. For this reason, before we can start
// the cross-fade we need to bring it back to the center of the viewport.
ResetTransformForLayer(animation_manager_->web_contents_view_android()
->parent_for_web_page_widgets());
ResetTransformForLayer(screenshot_layer_.get());
// Move the screenshot to the very top, so we can cross-fade from the
// screenshot (top) into the active page (bottom).
InsertLayersInOrder();
InitializeEffectForCrossfadeAnimation();
CHECK(animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow());
animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow()
->SetNeedsAnimate();
break;
}
case State::kAnimationFinished:
case State::kAnimationAborted:
break;
}
}
void BackForwardTransitionAnimator::SetupForScreenshotPreview(
SkBitmap embedder_content) {
NavigationControllerImpl* nav_controller =
animation_manager_->navigation_controller();
auto* destination_entry =
nav_controller->GetEntryWithUniqueID(destination_entry_id_);
CHECK(destination_entry);
auto* preview = static_cast<NavigationEntryScreenshot*>(
destination_entry->GetUserData(NavigationEntryScreenshot::kUserDataKey));
CHECK(fallback_ux_ ||
preview->navigation_entry_id() == destination_entry_id_);
// The layers can be reused. We need to make sure there is no ongoing
// transform on the layer of the current `WebContents`'s view.
auto transform = animation_manager_->web_contents_view_android()
->parent_for_web_page_widgets()
->transform();
CHECK(transform.IsIdentity()) << transform.ToString();
if (fallback_ux_) {
auto screenshot_layer = cc::slim::SolidColorLayer::Create();
screenshot_layer->SetBackgroundColor(
fallback_ux_->color_config.background_color);
screenshot_layer_ = std::move(screenshot_layer);
} else {
auto* cache = nav_controller->GetNavigationEntryScreenshotCache();
screenshot_ = cache->RemoveScreenshot(destination_entry);
ui_resource_id_ = CreateUIResource(screenshot_.get());
auto screenshot_layer = cc::slim::UIResourceLayer::Create();
screenshot_layer->SetUIResourceId(ui_resource_id_);
screenshot_layer_ = std::move(screenshot_layer);
}
screenshot_layer_->SetIsDrawable(true);
screenshot_layer_->SetPosition(gfx::PointF(0.f, 0.f));
screenshot_layer_->SetBounds(animation_manager_->web_contents_view_android()
->GetNativeView()
->GetPhysicalBackingSize());
screenshot_scrim_ = cc::slim::SolidColorLayer::Create();
screenshot_scrim_->SetBounds(screenshot_layer_->bounds());
screenshot_scrim_->SetIsDrawable(true);
screenshot_scrim_->SetBackgroundColor(SkColors::kTransparent);
// Makes sure `screenshot_scrim_` is drawn on top of `screenshot_layer_`.
screenshot_layer_->AddChild(screenshot_scrim_);
screenshot_scrim_->SetContentsOpaque(false);
// Add the rounded rectangle and the favicon. We need to do this after setting
// up the scrim because the scrim shouldn't be applied to the rounded
// rectangle and the favicon.
const auto& favicon_bitmap =
destination_entry->navigation_transition_data().favicon();
// Do not draw the rrect if we don't have a valid bitmap.
bool should_draw_rrect = fallback_ux_ && !favicon_bitmap.drawsNothing();
if (should_draw_rrect) {
auto favicon = cc::slim::UIResourceLayer::Create();
auto favicon_width = favicon_bitmap.width();
auto favicon_height = favicon_bitmap.height();
favicon->SetBitmap(favicon_bitmap);
favicon->SetIsDrawable(true);
favicon->SetPosition(
gfx::PointF(DipToPx(kFaviconPosDip), DipToPx(kFaviconPosDip)));
favicon->SetBounds(gfx::Size(favicon_width, favicon_height));
rounded_rectangle_ =
AddRoundedRectangle(screenshot_layer_.get(), DipToPx(kRRectSizeDip),
DipToPx(kRRectRadiusDip),
fallback_ux_->color_config.rounded_rectangle_color);
rounded_rectangle_->AddChild(std::move(favicon));
}
SetUpEmbedderContentLayerIfNeeded(std::move(embedder_content));
// This inserts the screenshot layer into the layer tree.
InsertLayersInOrder();
// Set up `effect_`.
InitializeEffectForGestureProgressAnimation();
// Calling `OnGestureProgressed` manually. This will ask the physics model to
// move the layers to their respective initial positions.
OnGestureProgressed(latest_progress_gesture_);
}
void BackForwardTransitionAnimator::SetupProgressBar() {
const auto& progress_bar_config =
animation_manager_->web_contents_view_android()
->GetNativeView()
->GetWindowAndroid()
->GetProgressBarConfig();
if (!progress_bar_config.ShouldDisplay()) {
return;
}
progress_bar_ =
std::make_unique<ProgressBar>(GetViewportWidthPx(), progress_bar_config);
// The progress bar should draw on top of the scrim (if any).
screenshot_layer_->AddChild(progress_bar_->GetLayer());
}
bool BackForwardTransitionAnimator::StartNavigationAndTrackRequest() {
CHECK(fallback_ux_ || screenshot_);
CHECK(!tracked_request_);
CHECK_EQ(navigation_state_, NavigationState::kNotStarted);
NavigationControllerImpl* nav_controller =
animation_manager_->navigation_controller();
int index = nav_controller->GetEntryIndexWithUniqueID(destination_entry_id_);
if (index == -1) {
return false;
}
std::vector<base::WeakPtr<NavigationRequest>> requests;
{
CHECK(!is_starting_navigation_);
base::AutoReset reset(&is_starting_navigation_, true);
requests = nav_controller->GoToIndexAndReturnAllRequests(index);
}
if (requests.empty()) {
// The gesture did not create any navigation requests.
return false;
}
for (const auto& request : requests) {
request->set_was_initiated_by_animated_transition();
if (request->IsInPrimaryMainFrame()) {
TrackRequest(std::move(request));
return true;
}
}
if (requests.size() > 1U) {
AbortAnimation(AnimationAbortReason::kMultipleNavigationRequestsCreated);
return false;
}
CHECK(!tracked_request_);
CHECK_EQ(navigation_state_, NavigationState::kNotStarted);
TrackRequest(std::move(requests[0]));
CHECK(tracked_request_);
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::StartNavigationAndTrackRequest",
"tracked_request", tracked_request_.value().navigation_id,
"is_primary_main_frame",
tracked_request_.value().is_primary_main_frame);
return true;
}
void BackForwardTransitionAnimator::TrackRequest(
base::WeakPtr<NavigationRequest> created_request) {
CHECK(created_request);
// The resulting `NavigationRequest` must be associated with the intended
// `NavigationEntry`, to safely start the animation.
//
// NOTE: A `NavigationRequest` does not always have a `NavigationEntry`, since
// the entry can be deleted at any time (e.g., clearing history), even during
// a pending navigation. It's fine to CHECK the entry here because we just
// created the requests in the same stack. No code yet had a chance to delete
// the entry.
CHECK(created_request->GetNavigationEntry());
int request_entry_id = created_request->GetNavigationEntry()->GetUniqueID();
// `destination_entry_id_` is initialized in the same stack as
// `GoToIndexAndReturnAllRequests()`. Thus they must equal.
CHECK_EQ(destination_entry_id_, request_entry_id);
tracked_request_ = TrackedRequest{
.navigation_id = created_request->GetNavigationId(),
.is_primary_main_frame = created_request->IsInPrimaryMainFrame(),
};
if (created_request->IsNavigationStarted()) {
navigation_state_ = NavigationState::kStarted;
if (created_request->IsSameDocument() &&
created_request->IsInPrimaryMainFrame()) {
// For same-doc navigations, we clone the old surface layer and subscribe
// to the widget host immediately after sending the "CommitNavigation"
// message. Once the browser receives the renderer's "DidCommitNavigation"
// message, it is too late to make a clone or subscribe to the widget
// host.
MaybeCloneOldSurfaceLayer(
created_request->GetRenderFrameHost()->GetView());
SubscribeToNewRenderWidgetHost(created_request.get());
}
} else {
CHECK(!created_request->IsSameDocument());
CHECK(created_request->IsWaitingForBeforeUnload());
navigation_state_ = NavigationState::kBeforeUnloadDispatched;
}
}
BackForwardTransitionAnimator::ComputedAnimationValues
BackForwardTransitionAnimator::ComputeAnimationValues(
const PhysicsModel::Result& result) {
ComputedAnimationValues values;
const auto viewport_width_px = GetViewportWidthPx();
values.progress =
std::abs(result.foreground_offset_physical) / viewport_width_px;
if (nav_direction_ == NavigationDirection::kForward) {
// The physics model assumes the background comes in from slightly outside
// the viewport. But in forward navigations the live page is in the
// background, it starts fully in the viewport, and moves slightly
// offscreen. So shift the live page so that it starts in the viewport.
float start_from_origin =
-PhysicsModel::kScreenshotInitialPositionRatio * viewport_width_px;
values.live_page_offset_px =
result.background_offset_physical + start_from_origin;
// The physics model assumes the foreground starts fully in the viewport and
// slides out. In a forward navigation the foreground is the screenshot and
// comes from fully out of the viewport so offset it by the viewport width
// to make it animate from fully out to fully in.
values.screenshot_offset_px =
result.foreground_offset_physical - viewport_width_px;
} else {
values.live_page_offset_px = result.foreground_offset_physical;
values.screenshot_offset_px = result.background_offset_physical;
}
// Swipes from the right edge will travel in the opposite direction.
if (initiating_edge_ == SwipeEdge::RIGHT) {
values.live_page_offset_px *= -1;
values.screenshot_offset_px *= -1;
}
CHECK(IsGreaterThanOrEqual(values.progress, 0.f));
CHECK(IsLessThanOrEqual(values.progress, 1.f));
return values;
}
cc::UIResourceId BackForwardTransitionAnimator::CreateUIResource(
cc::UIResourceClient* client) {
// A Window is detached from the NativeView if the tab is not currently
// displayed. It would be an error to use any of the APIs in this file.
ui::WindowAndroid* window = animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow();
CHECK(window);
// Guaranteed to have a compositor as long as the window is attached.
ui::WindowAndroidCompositor* compositor = window->GetCompositor();
CHECK(compositor);
return static_cast<CompositorImpl*>(compositor)->CreateUIResource(client);
}
void BackForwardTransitionAnimator::DeleteUIResource(
cc::UIResourceId resource_id) {
ui::WindowAndroid* window = animation_manager_->web_contents_view_android()
->GetTopLevelNativeWindow();
CHECK(window);
ui::WindowAndroidCompositor* compositor = window->GetCompositor();
CHECK(compositor);
static_cast<CompositorImpl*>(compositor)->DeleteUIResource(ui_resource_id_);
}
bool BackForwardTransitionAnimator::SetLayerTransformationAndTickEffect(
const PhysicsModel::Result& result) {
TRACE_EVENT(
TRACE_DISABLED_BY_DEFAULT("navigation"),
"BackForwardTransitionAnimator::SetLayerTransformationAndTickEffect");
// Mirror for RTL if needed and swap the layers for forward navigations.
ComputedAnimationValues values = ComputeAnimationValues(result);
screenshot_layer_->SetTransform(
gfx::Transform::MakeTranslation(values.screenshot_offset_px, 0.f));
const auto live_page_transform =
gfx::Transform::MakeTranslation(values.live_page_offset_px, 0.f);
animation_manager_->web_contents_view_android()
->parent_for_web_page_widgets()
->SetTransform(live_page_transform);
if (old_surface_clone_) {
// TODO(https://crbug.com/371043197): Remove once it's fixed.
SCOPED_CRASH_KEY_STRING64("PredBack", "nav_state",
NavigationStateToString(navigation_state_));
SCOPED_CRASH_KEY_STRING64("PredBack", "state", StateToString(state_));
CHECK(navigation_state_ == NavigationState::kCommitted ||
navigation_state_ == NavigationState::kStarted)
<< NavigationStateToString(navigation_state_);
CHECK_EQ(state_, State::kDisplayingInvokeAnimation);
old_surface_clone_->SetTransform(live_page_transform);
} else if (embedder_live_content_clone_) {
embedder_live_content_clone_->SetTransform(live_page_transform);
}
effect_.Tick(GetFittedTimeTicksForForegroundProgress(values.progress));
return result.done && effect_.keyframe_models().empty();
}
void BackForwardTransitionAnimator::MaybeCloneOldSurfaceLayer(
RenderWidgetHostViewBase* old_main_frame_view) {
// The old View must be still alive (and its renderer).
CHECK(old_main_frame_view);
CHECK(!old_surface_clone_);
if (embedder_live_content_clone_) {
return;
}
const auto* old_surface_layer =
static_cast<RenderWidgetHostViewAndroid*>(old_main_frame_view)
->GetSurfaceLayer();
old_surface_clone_ = cc::slim::SurfaceLayer::Create();
// Use a zero deadline because this is a copy of a surface being actively
// shown. The surface textures are ready (i.e. won't be GC'ed) because
// `old_surface_clone_` references to them.
old_surface_clone_->SetSurfaceId(old_surface_layer->surface_id(),
cc::DeadlinePolicy::UseSpecifiedDeadline(0));
old_surface_clone_->SetPosition(old_surface_layer->position());
old_surface_clone_->SetBounds(old_surface_layer->bounds());
old_surface_clone_->SetTransform(old_surface_layer->transform());
old_surface_clone_->SetIsDrawable(true);
// Inserts the clone layer into the layer tree.
InsertLayersInOrder();
}
void BackForwardTransitionAnimator::SetUpEmbedderContentLayerIfNeeded(
SkBitmap bitmap) {
if (bitmap.empty()) {
return;
}
embedder_live_content_clone_ = cc::slim::UIResourceLayer::Create();
embedder_live_content_clone_->SetBitmap(bitmap);
embedder_live_content_clone_->SetIsDrawable(true);
embedder_live_content_clone_->SetPosition(gfx::PointF(0.f, 0.f));
embedder_live_content_clone_->SetBounds(
animation_manager_->web_contents_view_android()
->GetNativeView()
->GetPhysicalBackingSize());
}
// TODO(crbug.com/350750205): Refactor this function and
// `OnRenderFrameMetadataChangedAfterActivation` to the manager
void BackForwardTransitionAnimator::SubscribeToNewRenderWidgetHost(
NavigationRequest* navigation_request) {
CHECK(!new_render_widget_host_);
if (!navigation_request->GetNavigationEntry()) {
// Error case: The navigation entry is deleted when the navigation is ready
// to commit. Abort the transition.
AbortAnimation(AnimationAbortReason::kNavigationEntryDeletedBeforeCommit);
return;
}
auto* new_host = navigation_request->GetRenderFrameHost();
CHECK(new_host);
new_render_widget_host_ = new_host->GetRenderWidgetHost();
new_render_widget_host_->AddObserver(animation_manager_);
CHECK_EQ(primary_main_frame_navigation_entry_item_sequence_number_,
cc::RenderFrameMetadata::kInvalidItemSequenceNumber);
if (is_copied_from_embedder_) {
// The embedder will be responsible for cross-fading from the screenshot
// to the new content. We don't register
// `RenderFrameMetadataProvider::Observer` and do not set
// `primary_main_frame_navigation_entry_item_sequence_number_`.
return;
}
new_render_widget_host_->render_frame_metadata_provider()->AddObserver(
animation_manager_);
FrameNavigationEntry* frame_nav_entry =
static_cast<NavigationEntryImpl*>(
navigation_request->GetNavigationEntry())
->GetFrameEntry(new_host->frame_tree_node());
// This is a session history of the primary main frame. We must have a
// valid `FrameNavigationEntry`.
CHECK(frame_nav_entry);
CHECK_NE(frame_nav_entry->item_sequence_number(), -1);
primary_main_frame_navigation_entry_item_sequence_number_ =
frame_nav_entry->item_sequence_number();
}
void BackForwardTransitionAnimator::UnregisterNewFrameActivationObserver() {
new_render_widget_host_->render_frame_metadata_provider()->RemoveObserver(
animation_manager_);
new_render_widget_host_->RemoveObserver(animation_manager_);
new_render_widget_host_ = nullptr;
}
int BackForwardTransitionAnimator::GetViewportWidthPx() const {
return animation_manager_->web_contents_view_android()
->GetNativeView()
->GetPhysicalBackingSize()
.width();
}
int BackForwardTransitionAnimator::GetViewportHeightPx() const {
return animation_manager_->web_contents_view_android()
->GetNativeView()
->GetPhysicalBackingSize()
.height();
}
void BackForwardTransitionAnimator::StartInputSuppression(
IgnoringInputReason ignoring_input_reason) {
TRACE_EVENT("browser,navigation",
"BackForwardTransitionAnimator::StartInputSuppression", "reason",
IgnoringInputReasonToString(ignoring_input_reason));
CHECK(!ignore_input_scope_);
ignoring_input_reason_ = ignoring_input_reason;
ignore_input_scope_.emplace(animation_manager_->web_contents_view_android()
->web_contents()
->IgnoreInputEvents(
/*audit_callback=*/std::nullopt));
}
void BackForwardTransitionAnimator::InsertLayersInOrder() {
// The layer order when navigating backwards (successive lines decrease in
// z-order):
//
// WebContentsViewAndroid::view_->GetLayer()
// |- `embedder_live_content_clone_`
// |- `old_surface_clone_` (only set during the invoke animation
// and when `embedder_live_content_clone_` is not set).
// |- parent_for_web_page_widgets_ (RWHVAndroid, Overscroll etc).
// |- progress_bar_ (child of screenshot_layer_,
// only during invoke animation)
// |- rrect_layer_ (child of screenshot_layer_, if fallback UX is used)
// |- screenshot_scrim_ (child of screenshot_layer_)
// |- screenshot_layer_
//
// And when navigating forwards:
//
// WebContentsViewAndroid::view_->GetLayer()
// |- progress_bar_
// |- rrect_layer_ (if fallback UX is used)
// |- screenshot_scrim_
// |- screenshot_layer_
// |- old_surface_clone_
// |- parent_for_web_page_widgets_
//
// Finally, in both cases -- when the navigation is about to complete -- the
// screenshot layer is placed over top of the new live page so that the cross
// fade animation can smoothly transition to the live page:
//
// WebContentsViewAndroid::view_->GetLayer()
// |- screenshot_scrim_
// |- screenshot_layer_
// |- parent_for_web_page_widgets_
// This class' layers are removed and reinserted relative to the
// parent_for_web_page_widgets layer to ensure the ordering is always
// up-to-date after this call. Remove both layers first, before any
// re-inserting, to avoid having to bookkeep the changing
// web_page_widgets_index.
CHECK(screenshot_layer_);
if (screenshot_layer_->parent()) {
screenshot_layer_->RemoveFromParent();
}
if (embedder_live_content_clone_) {
embedder_live_content_clone_->RemoveFromParent();
} else if (old_surface_clone_) {
old_surface_clone_->RemoveFromParent();
}
cc::slim::Layer* parent_layer =
animation_manager_->web_contents_view_android()
->parent_for_web_page_widgets()
->parent();
const std::vector<scoped_refptr<cc::slim::Layer>> layers =
parent_layer->children();
auto itr =
base::ranges::find(layers, animation_manager_->web_contents_view_android()
->parent_for_web_page_widgets());
CHECK(itr != layers.end());
std::ptrdiff_t web_page_widgets_index = std::distance(layers.begin(), itr);
// The screenshot layer is shown below the live web page when navigating
// backwards and above it when navigating forwards. The screenshot is always
// on top when cross-fading.
bool screenshot_on_top = nav_direction_ == NavigationDirection::kForward ||
state_ == State::kDisplayingCrossFadeAnimation;
std::ptrdiff_t screenshot_index =
screenshot_on_top ? web_page_widgets_index + 1 : web_page_widgets_index;
parent_layer->InsertChild(screenshot_layer_.get(), screenshot_index);
if (!screenshot_on_top) {
++web_page_widgets_index;
}
if (embedder_live_content_clone_) {
// The embedder live content clone is used only when there is a visible
// native view corresponding to the currently committed navigation entry.
parent_layer->InsertChild(embedder_live_content_clone_.get(),
web_page_widgets_index + 1);
} else if (old_surface_clone_) {
// The old page clone is used only when the old live page is swapped out so
// may be null at other times.
// The clone is no longer needed when cross-fading - the screenshot layer
// must always be on top at this time.
CHECK_NE(state_, State::kDisplayingCrossFadeAnimation);
// Since the clone represents the old live page it must maintain the
// ordering relative to the screenshot noted above but must also be shown
// above the live web page layer. Since the web page widget is already
// ordered relative to the screenshot, order it directly on top of it.
parent_layer->InsertChild(old_surface_clone_.get(),
web_page_widgets_index + 1);
}
}
void BackForwardTransitionAnimator::OnPostNavigationFirstFrameTimeout() {
CHECK_EQ(state_, State::kWaitingForNewRendererToDraw);
CHECK_EQ(navigation_state_, NavigationState::kCommitted);
AbortAnimation(AnimationAbortReason::kPostNavigationFirstFrameTimeout);
animation_manager_->OnPostNavigationFirstFrameTimeout();
}
void BackForwardTransitionAnimator::ResetLiveOverlayLayer() {
if (embedder_live_content_clone_) {
CHECK(!old_surface_clone_);
embedder_live_content_clone_->RemoveFromParent();
embedder_live_content_clone_.reset();
return;
}
// There is no `old_surface_clone_` when navigating from a crashed page.
if (old_surface_clone_) {
old_surface_clone_->RemoveFromParent();
old_surface_clone_.reset();
}
}
gfx::PointF BackForwardTransitionAnimator::CalculateRRectStartPx() const {
float y_start = (GetViewportHeightPx() - DipToPx(kRRectSizeDip)) / 2.f;
/* LTR, left edge back nav. The rrect starts at 25%*W px w.r.t. the
screenshot.
screenshot live page screenshot live page
▲ ▲ ▲ ▲
│ │ │ │
┌─┼──┌─────────────┼─┐ ┌───┼───────────┌────────────┼──┐
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ ┌────┐ │ │ │ ┌────┐ │ │
│ │ │ │ │ │ │ │ │ │
│25% │ │ │ │ │ │ │ │ │
│ └────┘ │ │ │ └────┘ │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
└────└───────────────┘ └───────────────└───────────────┘
start stop
*/
if (initiating_edge_ == SwipeEdge::LEFT &&
nav_direction_ == NavigationDirection::kBackward) {
return gfx::PointF(std::abs(GetViewportWidthPx() *
PhysicsModel::kScreenshotInitialPositionRatio),
y_start);
}
/* LTR, right edge forward nav. The rrect starts at 0px w.r.t. the screenshot.
live page screenshot live page screenshot
▲ ▲ ▲ ▲
│ │ │ │
┌──┼───────────┌─────────┼────┐ ┌─┼───┌──────────────┼──┐
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ ┌─────┐ │ │ │ ┌─────┐ │
│ │ │ │ │ │ │ ││ │
│ │ │ │ │ │ │ ││ │
│ └─────┘ │ │ │ └─────┘ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
└──────────────└──────────────┘ └─────└──────────┴──────┘
start stop
*/
else if (initiating_edge_ == SwipeEdge::RIGHT &&
nav_direction_ == NavigationDirection::kForward) {
return gfx::PointF(0.f, y_start);
}
/* RTL, right edge back nav. The rrect starts at (1-25%)*W px w.r.t the
screenshot layer.
live page screenshot live page screenshot
▲ ▲ ▲ ▲
│ │ │ │
┌─┼───┌──────────────┼──┐ ┌───┼────────────┌─────────┼──────┐
│ │ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │
│ │ │ 25% │ │ │ │
│ │ ┌──────┐ │ │ ┌──────┐ │
│ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │
│ │ └──────┘ │ │ └──────┘ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
└─────└──────────┴──────┘ └────────────────└────────────────┘
start stop
*/
else if (initiating_edge_ == SwipeEdge::RIGHT &&
nav_direction_ == NavigationDirection::kBackward) {
return gfx::PointF(
GetViewportWidthPx() -
std::abs(GetViewportWidthPx() *
PhysicsModel::kScreenshotInitialPositionRatio),
y_start);
}
/* RTL, left edge forward nav. The rrect starts at W-w px w.r.t the
screenshot, where w is the width of the rrect.
screenshot live page screenshot live page
▲ ▲ ▲ ▲
│ │ │ │
┌──┼───────────┌─────────┼────┐ ┌─┼───┌──────────────┼──┐
│ │ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ ┌─────┐ │ │ ┌─────┐ │ │
│ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │
│ └─────┘ │ │ └─────┘ │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
└──────────────└──────────────┘ └─────└──────────┴──────┘
start stop
*/
else if (initiating_edge_ == SwipeEdge::LEFT &&
nav_direction_ == NavigationDirection::kForward) {
return gfx::PointF(GetViewportWidthPx() - DipToPx(kRRectSizeDip), y_start);
} else {
NOTREACHED_NORETURN();
}
}
gfx::PointF BackForwardTransitionAnimator::CalculateRRectEndPx() const {
return gfx::PointF((GetViewportWidthPx() - DipToPx(kRRectSizeDip)) / 2.f,
(GetViewportHeightPx() - DipToPx(kRRectSizeDip)) / 2.f);
}
int BackForwardTransitionAnimator::DipToPx(int dip) const {
return gfx::ScaleToFlooredSize(gfx::Size(dip, dip), device_scale_factor_)
.width();
}
void BackForwardTransitionAnimator::DeferDialogs() {
CHECK_EQ(deferred_dialog_token_,
ui::ModalDialogManagerBridge::kInvalidDialogToken);
auto* dialog_manager = animation_manager_->web_contents_view_android()
->GetNativeView()
->GetWindowAndroid()
->GetModalDialogManagerBridge();
// We don't always have a dialog manager (i.e., content_browsertests).
if (dialog_manager) {
deferred_dialog_token_ = dialog_manager->SuspendModalDialog(
ui::ModalDialogManagerBridge::ModalDialogType::kTab);
}
}
void BackForwardTransitionAnimator::ResumeDialogs() {
if (deferred_dialog_token_ ==
ui::ModalDialogManagerBridge::kInvalidDialogToken) {
return;
}
auto* dialog_manager = animation_manager_->web_contents_view_android()
->GetNativeView()
->GetWindowAndroid()
->GetModalDialogManagerBridge();
if (dialog_manager) {
dialog_manager->ResumeModalDialog(
ui::ModalDialogManagerBridge::ModalDialogType::kTab,
deferred_dialog_token_);
}
deferred_dialog_token_ = ui::ModalDialogManagerBridge::kInvalidDialogToken;
}
} // namespace content