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
content / browser / back_forward_cache_basics_browsertest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <array>
#include "content/browser/back_forward_cache_browsertest.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/navigation_handle_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
// This file contains back/forward-cache tests that test basic functionality,
// e.g. navigation, different responses and document structures.
// Almost everything in here could have been written as a JS-only WPT. It was
// forked from
// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/back_forward_cache_browsertest.cc;drc=804bb57be3441b6291c11e34d8f901e2b1c0b430
//
// When adding tests here consider adding a WPT intead. See
// third_party/blink/web_tests/external/wpt/html/browsers/browsing-the-web/back-forward-cache/README.md
using testing::_;
using testing::Each;
using testing::ElementsAre;
using testing::Not;
using testing::UnorderedElementsAreArray;
namespace content {
namespace {
void InsertSubFrameWithUrl(RenderFrameHost* rfh, std::string url) {
std::string insert_script = base::StringPrintf(
R"(
const iframeElement = document.createElement("iframe");
iframeElement.src = "%s";
document.body.appendChild(iframeElement);
)",
url.c_str());
ASSERT_TRUE(ExecJs(rfh, insert_script));
}
} // namespace
using NotRestoredReason = BackForwardCacheMetrics::NotRestoredReason;
using NotRestoredReasons =
BackForwardCacheCanStoreDocumentResult::NotRestoredReasons;
// Navigate from A to B and go back.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Basic) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
url::Origin origin_a = url::Origin::Create(url_a);
url::Origin origin_b = url::Origin::Create(url_b);
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
// 2) Navigate to B.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImpl* rfh_b = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b);
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_EQ(rfh_a->GetVisibilityState(), PageVisibilityState::kHidden);
EXPECT_EQ(origin_a, rfh_a->GetLastCommittedOrigin());
EXPECT_EQ(origin_b, rfh_b->GetLastCommittedOrigin());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
EXPECT_EQ(rfh_b->GetVisibilityState(), PageVisibilityState::kVisible);
// 3) Go back to A.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_EQ(origin_a, rfh_a->GetLastCommittedOrigin());
EXPECT_EQ(origin_b, rfh_b->GetLastCommittedOrigin());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_EQ(rfh_a->GetVisibilityState(), PageVisibilityState::kVisible);
EXPECT_TRUE(rfh_b->IsInBackForwardCache());
EXPECT_EQ(rfh_b->GetVisibilityState(), PageVisibilityState::kHidden);
ExpectRestored(FROM_HERE);
}
// Navigate from A to B and go back.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, BasicDocumentInitiated) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
// 2) Navigate to B.
ASSERT_TRUE(NavigateToURLFromRenderer(shell(), url_b));
RenderFrameHostImpl* rfh_b = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b);
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
// The two pages are using different BrowsingInstances.
EXPECT_FALSE(rfh_a->GetSiteInstance()->IsRelatedSiteInstance(
rfh_b->GetSiteInstance()));
// 3) Go back to A.
EXPECT_TRUE(ExecJs(shell(), "history.back();"));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_TRUE(rfh_b->IsInBackForwardCache());
ExpectRestored(FROM_HERE);
}
// Navigate from back and forward repeatedly.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
NavigateBackForwardRepeatedly) {
// Do not check for unexpected messages because the input task queue is not
// currently frozen, causing flakes in this test: crbug.com/1099395.
DoNotFailForUnexpectedMessagesWhileCached();
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
// 2) Navigate to B.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImpl* rfh_b = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b);
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
// 3) Go back to A.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_TRUE(rfh_b->IsInBackForwardCache());
ExpectRestored(FROM_HERE);
// 4) Go forward to B.
ASSERT_TRUE(HistoryGoForward(web_contents()));
EXPECT_EQ(rfh_b, current_frame_host());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
ExpectRestored(FROM_HERE);
// 5) Go back to A.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_TRUE(rfh_b->IsInBackForwardCache());
ExpectRestored(FROM_HERE);
// 6) Go forward to B.
ASSERT_TRUE(HistoryGoForward(web_contents()));
EXPECT_EQ(rfh_b, current_frame_host());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
ExpectRestored(FROM_HERE);
}
// The current page can't enter the BackForwardCache if another page can script
// it. This can happen when one document opens a popup using window.open() for
// instance. It prevents the BackForwardCache from being used.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, WindowOpen) {
// This test assumes cross-site navigation staying in the same
// BrowsingInstance to use a different SiteInstance. Otherwise, it will
// timeout at step 2).
if (!SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
return;
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
base::HistogramTester histograms;
// 1) Navigate to A and open a popup.
ASSERT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
Shell* popup = OpenPopup(rfh_a.get(), url_a, "");
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
rfh_a->GetBackForwardCacheMetrics()->SetObserverForTesting(this);
// 2) Navigate to B. The previous document can't enter the BackForwardCache,
// because of the popup.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_a.get(), url_b));
ASSERT_TRUE(rfh_a.WaitUntilRenderFrameDeleted());
RenderFrameHostImplWrapper rfh_b(current_frame_host());
EXPECT_EQ(2u, rfh_b->GetSiteInstance()->GetRelatedActiveContentsCount());
// 3) Go back to A. The previous document can't enter the BackForwardCache,
// because of the popup.
ASSERT_TRUE(ExecJs(rfh_b.get(), "history.back();"));
ASSERT_TRUE(rfh_b.WaitUntilRenderFrameDeleted());
ExpectNotRestored({NotRestoredReason::kRelatedActiveContentsExist,
NotRestoredReason::kBrowsingInstanceNotSwapped},
{},
{ShouldSwapBrowsingInstance::kNo_HasRelatedActiveContents},
{}, {}, FROM_HERE);
// Make sure that the tree result also has the same reasons.
EXPECT_THAT(
GetTreeResult()->GetDocumentResult(),
MatchesDocumentResult(
NotRestoredReasons({NotRestoredReason::kRelatedActiveContentsExist,
NotRestoredReason::kBrowsingInstanceNotSwapped}),
BlockListedFeatures()));
// Make sure that histograms for related active contents are updated with info
// about A's related active contents.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
2, 1);
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kPotentiallySyncAccessible,
1);
// 4) Make the popup drop the window.opener connection. It happens when the
// user does an omnibox-initiated navigation, which happens in a new
// BrowsingInstance.
RenderFrameHostImplWrapper rfh_a_new(current_frame_host());
EXPECT_EQ(2u, rfh_a_new->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_a_new->GetSiteInstance()->GetActiveDocumentCount(
rfh_a_new->last_committed_url_derived_site_info()));
ASSERT_TRUE(NavigateToURL(popup, url_b));
EXPECT_EQ(1u, rfh_a_new->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(1u, rfh_a_new->GetSiteInstance()->GetActiveDocumentCount(
rfh_a_new->last_committed_url_derived_site_info()));
// 5) Navigate to B again. As the scripting relationship with the popup is
// now severed, the current page (|rfh_a_new|) can enter back-forward cache.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_a_new.get(), url_b));
EXPECT_FALSE(rfh_a_new.IsRenderFrameDeleted());
EXPECT_TRUE(rfh_a_new->IsInBackForwardCache());
// 6) Go back to A. The current document can finally enter the
// BackForwardCache, because it is alone in its BrowsingInstance and has never
// been related to any other document.
RenderFrameHostImplWrapper rfh_b_new(current_frame_host());
ASSERT_TRUE(ExecJs(rfh_b_new.get(), "history.back();"));
ASSERT_TRUE(WaitForLoadStop(web_contents()));
EXPECT_FALSE(rfh_b_new.IsRenderFrameDeleted());
EXPECT_TRUE(rfh_b_new->IsInBackForwardCache());
// Expect the histograms aren't updated, since a BrowsingInstance swap
// happened.
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
1);
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
1);
}
// Similar with WindowOpen above, but open a cross-site page in the popup.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, WindowOpenCrossSite) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
base::HistogramTester histograms;
// 1) Navigate to A and open a cross-site popup B.
ASSERT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
Shell* popup = OpenPopup(rfh_a.get(), url_b, "");
WebContentsImpl* popup_contents =
static_cast<WebContentsImpl*>(popup->web_contents());
EXPECT_TRUE(WaitForLoadStop(popup_contents));
FrameTreeNode* popup_root = popup_contents->GetPrimaryFrameTree().root();
EXPECT_EQ(popup_root->opener(), web_contents()->GetPrimaryFrameTree().root());
// The popup and the original tab are related active contents.
RenderFrameHostImplWrapper rfh_b(popup_root->current_frame_host());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_b->GetSiteInstance()->GetRelatedActiveContentsCount());
// A and B use different SiteInstances if site isolation is turned on, and
// shares the same SiteInstance otherwise.
if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) {
EXPECT_NE(rfh_a->GetSiteInstance(), rfh_b->GetSiteInstance());
} else {
EXPECT_EQ(rfh_a->GetSiteInstance(), rfh_b->GetSiteInstance());
}
EXPECT_TRUE(rfh_a->GetSiteInstance()->IsRelatedSiteInstance(
rfh_b->GetSiteInstance()));
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
EXPECT_EQ(1u, rfh_b->GetSiteInstance()->GetActiveDocumentCount(
rfh_b->last_committed_url_derived_site_info()));
// 2) Navigate to C in the popup. The B document can't enter the
// BackForwardCache, because of the opener.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_b.get(), url_c));
if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
ShouldCreateNewHostForAllFrames()) {
ASSERT_TRUE(rfh_b.WaitUntilRenderFrameDeleted());
} else {
ASSERT_FALSE(rfh_b->IsInBackForwardCache());
}
RenderFrameHostImplWrapper rfh_c(popup_root->current_frame_host());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_c->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_TRUE(rfh_a->GetSiteInstance()->IsRelatedSiteInstance(
rfh_c->GetSiteInstance()));
// 3) Go back to B. It should not be restored from the back/forward cache.
ASSERT_TRUE(HistoryGoBack(popup_contents));
// Make sure that histograms for related active contents are updated with info
// about B's related active contents.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
2, 1);
// A and B are different sites (even though they might share the same
// SiteInstance when site isolation is turned off), which means B can't be
// synchronously accessible by other pages.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kNoSyncAccess,
1);
}
// Similar with WindowOpenCrossSite above, but navigate the popup same-site
// instead of cross site.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
WindowOpenCrossSiteNavigateSameSite) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b1(embedded_test_server()->GetURL("b.com", "/title1.html"));
GURL url_b2(embedded_test_server()->GetURL("b.com", "/title2.html"));
base::HistogramTester histograms;
// 1) Navigate to A and open a cross-site popup B1.
ASSERT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
Shell* popup = OpenPopup(rfh_a.get(), url_b1, "");
WebContentsImpl* popup_contents =
static_cast<WebContentsImpl*>(popup->web_contents());
EXPECT_TRUE(WaitForLoadStop(popup_contents));
FrameTreeNode* popup_root = popup_contents->GetPrimaryFrameTree().root();
EXPECT_EQ(popup_root->opener(), web_contents()->GetPrimaryFrameTree().root());
// The popup and the original tab are related active contents.
RenderFrameHostImplWrapper rfh_b1(popup_root->current_frame_host());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_b1->GetSiteInstance()->GetRelatedActiveContentsCount());
// A and B use different SiteInstances if site isolation is turned on, and
// shares the same SiteInstance otherwise.
if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) {
EXPECT_NE(rfh_a->GetSiteInstance(), rfh_b1->GetSiteInstance());
} else {
EXPECT_EQ(rfh_a->GetSiteInstance(), rfh_b1->GetSiteInstance());
}
EXPECT_TRUE(rfh_a->GetSiteInstance()->IsRelatedSiteInstance(
rfh_b1->GetSiteInstance()));
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
EXPECT_EQ(1u, rfh_b1->GetSiteInstance()->GetActiveDocumentCount(
rfh_b1->last_committed_url_derived_site_info()));
// 2) Navigate to B2 in the popup. The B1 document can't enter the
// BackForwardCache, because of the opener.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_b1.get(), url_b2));
if (ShouldCreateNewHostForAllFrames()) {
ASSERT_TRUE(rfh_b1.WaitUntilRenderFrameDeleted());
} else {
ASSERT_FALSE(rfh_b1->IsInBackForwardCache());
}
RenderFrameHostImplWrapper rfh_b2(popup_root->current_frame_host());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_b2->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_TRUE(rfh_a->GetSiteInstance()->IsRelatedSiteInstance(
rfh_b2->GetSiteInstance()));
// 3) Go back to B1. It should not be restored from the back/forward cache.
ASSERT_TRUE(HistoryGoBack(popup_contents));
// Make sure that histograms for related active contents are updated with info
// about B1's related active contents.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
2, 1);
// A and B1 are different sites (even though they might share the same
// SiteInstance when site isolation is turned off), which means B1 can't be
// synchronously accessible by other pages. Note: Even though B1 and B2
// are same site and end up using the same SiteInstance and BrowsingInstance,
// the documents are not active at the same time (as B2 replaces B1), so those
// aren't counted as potentially having synchronous access to each other.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kNoSyncAccess,
1);
}
// Similar with WindowOpen above, but open a cross-site page that has an iframe
// that is same-site with the opener.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
WindowOpenCrossSiteWithSameSiteChild) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b_with_a_iframe(embedded_test_server()->GetURL(
"b.com", "/cross_site_iframe_factory.html?b(a)"));
GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
base::HistogramTester histograms;
// 1) Navigate to A and open a cross-site popup B.
ASSERT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
Shell* popup = OpenPopup(rfh_a.get(), url_b_with_a_iframe, "");
WebContentsImpl* popup_contents =
static_cast<WebContentsImpl*>(popup->web_contents());
EXPECT_TRUE(WaitForLoadStop(popup_contents));
FrameTreeNode* popup_root = popup_contents->GetPrimaryFrameTree().root();
EXPECT_EQ(popup_root->opener(), web_contents()->GetPrimaryFrameTree().root());
// The popup and the original tab are related active contents.
RenderFrameHostImplWrapper rfh_b(popup_root->current_frame_host());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_b->GetSiteInstance()->GetRelatedActiveContentsCount());
// The opener A and the iframe A uses the same SiteInstance. Meanwhile, A and
// B use different SiteInstances if site isolation is turned on, and shares
// the same SiteInstance otherwise.
RenderFrameHostImplWrapper rfh_a_iframe(
rfh_b->child_at(0)->current_frame_host());
EXPECT_EQ(rfh_a->GetSiteInstance(), rfh_a_iframe->GetSiteInstance());
if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) {
EXPECT_NE(rfh_a->GetSiteInstance(), rfh_b->GetSiteInstance());
} else {
EXPECT_EQ(rfh_a->GetSiteInstance(), rfh_b->GetSiteInstance());
}
EXPECT_TRUE(rfh_a->GetSiteInstance()->IsRelatedSiteInstance(
rfh_b->GetSiteInstance()));
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
EXPECT_EQ(2u, rfh_a_iframe->GetSiteInstance()->GetActiveDocumentCount(
rfh_a_iframe->last_committed_url_derived_site_info()));
EXPECT_EQ(1u, rfh_b->GetSiteInstance()->GetActiveDocumentCount(
rfh_b->last_committed_url_derived_site_info()));
// 2) Navigate to C in the popup. The B document can't enter the
// BackForwardCache, because of the opener.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_b.get(), url_c));
if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
ShouldCreateNewHostForAllFrames()) {
ASSERT_TRUE(rfh_b.WaitUntilRenderFrameDeleted());
} else {
ASSERT_FALSE(rfh_b->IsInBackForwardCache());
}
RenderFrameHostImplWrapper rfh_c(popup_root->current_frame_host());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_c->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_TRUE(rfh_a->GetSiteInstance()->IsRelatedSiteInstance(
rfh_c->GetSiteInstance()));
// 3) Go back to B. It should not be restored from the back/forward cache.
ASSERT_TRUE(HistoryGoBack(popup_contents));
// Make sure that histograms for related active contents are updated with info
// about B's related active contents.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
2, 1);
// As there is another document in another page that uses A's SiteInstance,
// A is marked as potentially synchronously accessible from another page.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kPotentiallySyncAccessible,
1);
}
// A popup will prevent a page from entering BFCache. Test that after closing a
// popup, the page is not stopped from entering. This tries to close the popup
// at the last moment.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, WindowOpenThenClose) {
net::test_server::ControllableHttpResponse response(embedded_test_server(),
"/title2.html");
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.test", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.test", "/title2.html"));
base::HistogramTester histograms;
// Navigate to A.
ASSERT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
// Open a popup.
Shell* popup = OpenPopup(rfh_a.get(), url_a, "");
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
// Start navigating to B, the response will be delayed.
TestNavigationObserver observer(web_contents());
shell()->LoadURL(url_b);
// When the request is received, close the popup.
response.WaitForRequest();
RenderFrameHostImplWrapper rfh_popup(
popup->web_contents()->GetPrimaryMainFrame());
ASSERT_TRUE(ExecJs(rfh_popup.get(), "window.close();"));
ASSERT_TRUE(rfh_popup.WaitUntilRenderFrameDeleted());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(1u, rfh_a->GetSiteInstance()->GetActiveDocumentCount(
rfh_a->last_committed_url_derived_site_info()));
// Send the response.
response.Send(net::HTTP_OK, "text/html", "foo");
response.Done();
observer.Wait();
// A is in BFCache.
EXPECT_EQ(0u, rfh_a->GetSiteInstance()->GetRelatedActiveContentsCount());
ASSERT_TRUE(rfh_a->IsInBackForwardCache());
// Go back.
ASSERT_TRUE(HistoryGoBack(web_contents()));
// A is restored from BFCache.
EXPECT_FALSE(rfh_a.IsRenderFrameDeleted());
ExpectRestored(FROM_HERE);
// Expect no related active contents.
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
0);
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
0);
}
enum class NavigationSiteType {
kSameSite,
kCrossSite,
};
class BackForwardCacheBrowserTestWithVaryingNavigationSite
: public BackForwardCacheBrowserTest,
public ::testing::WithParamInterface<NavigationSiteType> {
protected:
bool NavigateSameSite() {
return GetParam() == NavigationSiteType::kSameSite;
}
};
INSTANTIATE_TEST_SUITE_P(All,
BackForwardCacheBrowserTestWithVaryingNavigationSite,
::testing::Values(NavigationSiteType::kSameSite,
NavigationSiteType::kCrossSite));
// Tests that RelatedActiveContents-related metrics is not logged when the
// BrowsingInstance swap didn't happen, but not because of the existence of
// related active contents, but due to other BFCache blockers.
IN_PROC_BROWSER_TEST_P(BackForwardCacheBrowserTestWithVaryingNavigationSite,
RelatedActiveContentsLoggingOnPageWithBlockingFeature) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_1(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_2(embedded_test_server()->GetURL(
NavigateSameSite() ? "a.com" : "b.com", "/title2.html"));
base::HistogramTester histograms;
// 1) Navigate to `url_1`.
EXPECT_TRUE(NavigateToURL(shell(), url_1));
RenderFrameHostImplWrapper rfh_url_1(current_frame_host());
scoped_refptr<SiteInstance> rfh_url_1_si = rfh_url_1->GetSiteInstance();
ASSERT_EQ(1u, rfh_url_1_si->GetRelatedActiveContentsCount());
// 2) Mark the `url_1` document as using a feature that's blocking BFCache.
rfh_url_1->UseDummyStickyBackForwardCacheDisablingFeatureForTesting();
// 3) Navigate to `url_2`. The BrowsingInstance stays the same, as `rfh_url_1`
// is not eligible for back/forward cache because of the sticky feature, so
// there's no need to do a BrowsingInstance swap.
ASSERT_TRUE(NavigateToURLFromRenderer(shell(), url_2));
ASSERT_TRUE(current_frame_host()->GetSiteInstance()->IsRelatedSiteInstance(
rfh_url_1_si.get()));
// 4) Go back to `url_1`.
ASSERT_TRUE(HistoryGoBack(web_contents()));
// 5) The page should not be restored from BFCache, and should not log
// RelatedActiveContents histogram entries.
ExpectNotRestored(
{NotRestoredReason::kBlocklistedFeatures,
NotRestoredReason::kBrowsingInstanceNotSwapped},
{blink::scheduler::WebSchedulerTrackedFeature::kDummy},
{ShouldSwapBrowsingInstance::kNo_NotNeededForBackForwardCache}, {}, {},
FROM_HERE);
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"Count2",
0);
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
0);
}
// Tests that RelatedActiveContents-related metrics is logged when the
// BrowsingInstance swap didn't happen because it has related active contents
// alongside other BFCache blockers.
IN_PROC_BROWSER_TEST_P(
BackForwardCacheBrowserTestWithVaryingNavigationSite,
RelatedActiveContentsLoggingOnPageWithBlockingFeatureAndRAC) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_1(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_2(embedded_test_server()->GetURL(
NavigateSameSite() ? "a.com" : "b.com", "/title2.html"));
base::HistogramTester histograms;
// 1) Navigate to `url_1`.
ASSERT_TRUE(NavigateToURL(shell(), url_1));
RenderFrameHostImplWrapper rfh_url_1(current_frame_host());
scoped_refptr<SiteInstance> rfh_url_1_si = rfh_url_1->GetSiteInstance();
ASSERT_EQ(1u, rfh_url_1_si->GetRelatedActiveContentsCount());
// 2) Open a popup so that the related active contents count increases.
Shell* popup = OpenPopup(rfh_url_1.get(), url_2, "");
WebContentsImpl* popup_contents =
static_cast<WebContentsImpl*>(popup->web_contents());
ASSERT_TRUE(WaitForLoadStop(popup_contents));
ASSERT_EQ(2u, rfh_url_1_si->GetRelatedActiveContentsCount());
// 3) Mark the `url_1` document as using a feature that's blocking BFCache.
rfh_url_1->UseDummyStickyBackForwardCacheDisablingFeatureForTesting();
// 4) Navigate to `url_2`. The BrowsingInstance stays the same, as `rfh_url_1`
// is not eligible for back/forward cache because of the sticky feature and
// the opener, so there's no need to do a BrowsingInstance swap.
ASSERT_TRUE(NavigateToURLFromRenderer(shell(), url_2));
ASSERT_TRUE(current_frame_host()->GetSiteInstance()->IsRelatedSiteInstance(
rfh_url_1_si.get()));
// 5) Go back to `url_1`.
ASSERT_TRUE(HistoryGoBack(web_contents()));
// 6) The page should not be restored from BFCache, but it should log
// RelatedActiveContents metrics because the related active contents
// count is > 1.
if (ShouldCreateNewHostForAllFrames() ||
(!NavigateSameSite() &&
SiteIsolationPolicy::UseDedicatedProcessesForAllSites())) {
ExpectNotRestored(
{NotRestoredReason::kRelatedActiveContentsExist,
NotRestoredReason::kBlocklistedFeatures,
NotRestoredReason::kBrowsingInstanceNotSwapped},
{blink::scheduler::WebSchedulerTrackedFeature::kDummy},
{ShouldSwapBrowsingInstance::kNo_HasRelatedActiveContents}, {}, {},
FROM_HERE);
} else {
ExpectNotRestored(
{NotRestoredReason::kBrowsingInstanceNotSwapped}, {},
{ShouldSwapBrowsingInstance::kNo_HasRelatedActiveContents}, {}, {},
FROM_HERE);
}
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"Count2",
2, 1);
if (NavigateSameSite()) {
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kPotentiallySyncAccessible,
1);
} else {
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kNoSyncAccess,
1);
}
}
// Test that if a page has an openee sharing the same BrowsingInstance but it
// is no longer active (pending deletion, etc) it won't prevent the opener from
// getting BFCached.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
WindowOpen_SameSitePopupPendingDeletion) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a1(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_a2(embedded_test_server()->GetURL("a.com", "/title2.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
base::HistogramTester histograms;
// 1) Navigate to A1 and open a popup to A2.
ASSERT_TRUE(NavigateToURL(shell(), url_a1));
RenderFrameHostImplWrapper rfh_a1(current_frame_host());
Shell* popup = OpenPopup(rfh_a1.get(), url_a2, "");
RenderFrameHostImplWrapper popup_rfh_a2(
popup->web_contents()->GetPrimaryMainFrame());
EXPECT_EQ(2u, rfh_a1->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u,
popup_rfh_a2->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_TRUE(popup_rfh_a2->GetSiteInstance()->IsRelatedSiteInstance(
rfh_a1->GetSiteInstance()));
// 2) Navigate to B in the opener. A1 can't enter the BackForwardCache,
// because of the popup.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_a1.get(), url_b));
if (ShouldCreateNewHostForAllFrames() ||
SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) {
ASSERT_TRUE(rfh_a1.WaitUntilRenderFrameDeleted());
} else {
ASSERT_FALSE(rfh_a1->IsInBackForwardCache());
}
// 3) Go back to A1. The related active contents relationship with the popup
// is retained.
ASSERT_TRUE(HistoryGoBack(web_contents()));
RenderFrameHostImplWrapper rfh_a1_new(current_frame_host());
EXPECT_EQ(2u, rfh_a1_new->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u,
popup_rfh_a2->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_TRUE(popup_rfh_a2->GetSiteInstance()->IsRelatedSiteInstance(
rfh_a1_new->GetSiteInstance()));
// Make sure that histograms for related active contents are updated with info
// about A1's related active contents.
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
2, 1);
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kPotentiallySyncAccessible,
1);
// 4) Make the popup drop the window.opener connection. It happens when the
// user does a cross-site omnibox-initiated navigation, which gets a new
// BrowsingInstance. Also, make sure that we keep the popup's A2 RFH around
// after navigation, to test if it is still counted as an opener that
// prevents BFCaching of A1, by preventing it from getting deleted.
popup_rfh_a2->DoNotDeleteForTesting();
ASSERT_TRUE(NavigateToURL(popup, url_b));
EXPECT_TRUE(popup_rfh_a2->IsPendingDeletion());
// Verify that the related active contents relationship is gone now that A2 is
// no longer active.
RenderFrameHostImplWrapper popup_rfh_b(
popup->web_contents()->GetPrimaryMainFrame());
EXPECT_FALSE(popup_rfh_b->GetSiteInstance()->IsRelatedSiteInstance(
rfh_a1_new->GetSiteInstance()));
EXPECT_EQ(1u,
popup_rfh_b->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(1u, rfh_a1_new->GetSiteInstance()->GetRelatedActiveContentsCount());
// 5) Navigate to B again from A1. As the scripting relationship with the
// popup is now severed, A1 can enter back-forward cache.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_a1_new.get(), url_b));
EXPECT_TRUE(rfh_a1_new->IsInBackForwardCache());
// 6) Go back to A1. Expect the histograms aren't updated, since a
// BrowsingInstance swap happened.
ASSERT_TRUE(HistoryGoBack(web_contents()));
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
1);
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
1);
}
// Tests that a page that has a same-site same-process but unrelated page (no
// opener relationship) won't be treated as having a potential sync accessor.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
WindowOpen_UnrelatedSameSiteAndProcessTab) {
// Ensure that A1 and A2 will share the same renderer process when possible.
RenderProcessHost::SetMaxRendererProcessCount(1);
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a1(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_a2(embedded_test_server()->GetURL("a.com", "/title2.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
base::HistogramTester histograms;
// 1) Navigate to A1 and open an unrelated new tab to A2.
ASSERT_TRUE(NavigateToURL(shell(), url_a1));
Shell* shell2 =
Shell::CreateNewWindow(shell()->web_contents()->GetBrowserContext(),
url_a2, nullptr, gfx::Size());
ASSERT_TRUE(WaitForLoadStop(shell2->web_contents()));
// A1 and A2 do not share the same BrowsingInstance, and are not related
// active contents. However, they might share the same process due to the
// process limit.
RenderFrameHostImplWrapper rfh_a1(current_frame_host());
RenderFrameHostImplWrapper rfh_a2(
shell2->web_contents()->GetPrimaryMainFrame());
EXPECT_EQ(1u, rfh_a1->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(1u, rfh_a2->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_FALSE(rfh_a2->GetSiteInstance()->IsRelatedSiteInstance(
rfh_a1->GetSiteInstance()));
if (!SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) {
EXPECT_EQ(rfh_a1->GetProcess(), rfh_a2->GetProcess());
}
// 2) Navigate to B in the opener. A1 can enter BackForwardCache because it
// has no related active contents.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_a1.get(), url_b));
EXPECT_TRUE(rfh_a1->IsInBackForwardCache());
// 3) Go back to A1, restoring the page from BackForwardCache. No related
// active contents metrics will be logged.
ASSERT_TRUE(HistoryGoBack(web_contents()));
ExpectRestored(FROM_HERE);
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
0);
histograms.ExpectTotalCount(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
0);
// 4) Now, open a B popup from A1. A1 and B should be related active contents,
// while A2 stays unaffected.
Shell* popup = OpenPopup(rfh_a1.get(), url_b, "");
RenderFrameHostImplWrapper rfh_b(
popup->web_contents()->GetPrimaryMainFrame());
EXPECT_EQ(2u, rfh_a1->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(1u, rfh_a2->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_EQ(2u, rfh_b->GetSiteInstance()->GetRelatedActiveContentsCount());
EXPECT_TRUE(rfh_b->GetSiteInstance()->IsRelatedSiteInstance(
rfh_a1->GetSiteInstance()));
// 5) Navigate to B again from A1, now A1 can't enter BackForwardCache because
// it has related active contents.
ASSERT_TRUE(NavigateToURLFromRenderer(rfh_a1.get(), url_b));
if (ShouldCreateNewHostForAllFrames() ||
SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) {
ASSERT_TRUE(rfh_a1.WaitUntilRenderFrameDeleted());
} else {
ASSERT_FALSE(rfh_a1->IsInBackForwardCache());
}
// 3) Go back to A1, restoring the page from BackForwardCache. Related active
// contents metrics will be logged.
ASSERT_TRUE(HistoryGoBack(web_contents()));
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents.Count2",
2, 1);
histograms.ExpectUniqueSample(
"BackForwardCache.HistoryNavigationOutcome.RelatedActiveContents."
"IsPotentiallySyncAccessible2",
BackForwardCacheMetrics::RelatedActiveContentsSyncAccessInfo::
kNoSyncAccess,
1);
}
// Navigate from A(B) to C and go back.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, BasicIframe) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
// 1) Navigate to A(B).
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameHostImpl* rfh_b = rfh_a->child_at(0)->current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b);
// 2) Navigate to C.
EXPECT_TRUE(NavigateToURL(shell(), url_c));
RenderFrameHostImpl* rfh_c = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_c(rfh_c);
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_TRUE(rfh_b->IsInBackForwardCache());
EXPECT_FALSE(rfh_c->IsInBackForwardCache());
// 3) Go back to A(B).
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_FALSE(delete_observer_rfh_c.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
EXPECT_TRUE(rfh_c->IsInBackForwardCache());
ExpectRestored(FROM_HERE);
}
// Similar to BackForwardCacheBrowserTest.SubframeSurviveCache*
// Test case: url_1(b2) -> c3 -> url_1(b2)
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, SubframeSurviveCache1) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
std::vector<RenderFrameDeletedObserver*> rfh_observer;
// 1) Navigate to url_1(b2).
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* url_1 = current_frame_host();
RenderFrameHostImpl* b2 = url_1->child_at(0)->current_frame_host();
RenderFrameDeletedObserver url_1_observer(url_1), b2_observer(b2);
rfh_observer.insert(rfh_observer.end(), {&url_1_observer, &b2_observer});
EXPECT_TRUE(ExecJs(b2, "window.alive = 'I am alive';"));
// 2) Navigate to c3.
EXPECT_TRUE(NavigateToURL(shell(), url_c));
RenderFrameHostImpl* c3 = current_frame_host();
RenderFrameDeletedObserver c3_observer(c3);
rfh_observer.push_back(&c3_observer);
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_THAT(Elements({url_1, b2}), Each(InBackForwardCache()));
EXPECT_THAT(c3, Not(InBackForwardCache()));
// 3) Go back to url_1(b2).
ASSERT_TRUE(HistoryGoBack(web_contents()));
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_THAT(Elements({url_1, b2}), Each(Not(InBackForwardCache())));
EXPECT_THAT(c3, InBackForwardCache());
// Even after a new IPC round trip with the renderer, b2 must still be alive.
EXPECT_EQ("I am alive", EvalJs(b2, "window.alive"));
EXPECT_FALSE(b2_observer.deleted());
ExpectRestored(FROM_HERE);
}
// Similar to BackForwardCacheBrowserTest.SubframeSurviveCache*
// Test case: url_1(b2) -> b3 -> url_1(b2).
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, SubframeSurviveCache2) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
std::vector<RenderFrameDeletedObserver*> rfh_observer;
// 1) Navigate to url_1(b2).
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* url_1 = current_frame_host();
RenderFrameHostImpl* b2 = url_1->child_at(0)->current_frame_host();
RenderFrameDeletedObserver url_1_observer(url_1), b2_observer(b2);
rfh_observer.insert(rfh_observer.end(), {&url_1_observer, &b2_observer});
EXPECT_TRUE(ExecJs(b2, "window.alive = 'I am alive';"));
// 2) Navigate to b3.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImpl* b3 = current_frame_host();
RenderFrameDeletedObserver b3_observer(b3);
rfh_observer.push_back(&b3_observer);
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_THAT(Elements({url_1, b2}), Each(InBackForwardCache()));
EXPECT_THAT(b3, Not(InBackForwardCache()));
// 3) Go back to url_1(b2).
ASSERT_TRUE(HistoryGoBack(web_contents()));
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_EQ(url_1, current_frame_host());
EXPECT_THAT(Elements({url_1, b2}), Each(Not(InBackForwardCache())));
EXPECT_THAT(b3, InBackForwardCache());
// Even after a new IPC round trip with the renderer, b2 must still be alive.
EXPECT_EQ("I am alive", EvalJs(b2, "window.alive"));
EXPECT_FALSE(b2_observer.deleted());
ExpectRestored(FROM_HERE);
}
// Similar to BackForwardCacheBrowserTest.tSubframeSurviveCache*
// Test case: url_1(b2) -> b3(a4) -> url_1(b2) -> b3(a4)
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, SubframeSurviveCache3) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_b(embedded_test_server()->GetURL(
"b.com", "/cross_site_iframe_factory.html?b(a)"));
std::vector<RenderFrameDeletedObserver*> rfh_observer;
// 1) Navigate to url_1(b2).
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* url_1 = current_frame_host();
RenderFrameHostImpl* b2 = url_1->child_at(0)->current_frame_host();
RenderFrameDeletedObserver url_1_observer(url_1), b2_observer(b2);
rfh_observer.insert(rfh_observer.end(), {&url_1_observer, &b2_observer});
EXPECT_TRUE(ExecJs(b2, "window.alive = 'I am alive';"));
// 2) Navigate to b3(a4)
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImpl* b3 = current_frame_host();
RenderFrameHostImpl* a4 = b3->child_at(0)->current_frame_host();
RenderFrameDeletedObserver b3_observer(b3), a4_observer(a4);
rfh_observer.insert(rfh_observer.end(), {&b3_observer, &a4_observer});
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_THAT(Elements({url_1, b2}), Each(InBackForwardCache()));
EXPECT_THAT(Elements({b3, a4}), Each(Not(InBackForwardCache())));
EXPECT_TRUE(ExecJs(a4, "window.alive = 'I am alive';"));
// 3) Go back to url_1(b2).
ASSERT_TRUE(HistoryGoBack(web_contents()));
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_EQ(url_1, current_frame_host());
EXPECT_THAT(Elements({url_1, b2}), Each(Not(InBackForwardCache())));
EXPECT_THAT(Elements({b3, a4}), Each(InBackForwardCache()));
// Even after a new IPC round trip with the renderer, b2 must still be alive.
EXPECT_EQ("I am alive", EvalJs(b2, "window.alive"));
EXPECT_FALSE(b2_observer.deleted());
ExpectRestored(FROM_HERE);
// 4) Go forward to b3(a4).
ASSERT_TRUE(HistoryGoForward(web_contents()));
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_EQ(b3, current_frame_host());
EXPECT_THAT(Elements({url_1, b2}), Each(InBackForwardCache()));
EXPECT_THAT(Elements({b3, a4}), Each(Not(InBackForwardCache())));
// Even after a new IPC round trip with the renderer, a4 must still be alive.
EXPECT_EQ("I am alive", EvalJs(a4, "window.alive"));
EXPECT_FALSE(a4_observer.deleted());
ExpectRestored(FROM_HERE);
}
// Similar to BackForwardCacheBrowserTest.SubframeSurviveCache*
// Test case: url_1(b2) -> b3 -> a4 -> b5 -> url_1(b2).
IN_PROC_BROWSER_TEST_F(HighCacheSizeBackForwardCacheBrowserTest,
SubframeSurviveCache4) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_ab(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
std::vector<RenderFrameDeletedObserver*> rfh_observer;
// 1) Navigate to url_1(b2).
EXPECT_TRUE(NavigateToURL(shell(), url_ab));
RenderFrameHostImpl* url_1 = current_frame_host();
RenderFrameHostImpl* b2 = url_1->child_at(0)->current_frame_host();
RenderFrameDeletedObserver url_1_observer(url_1), b2_observer(b2);
rfh_observer.insert(rfh_observer.end(), {&url_1_observer, &b2_observer});
EXPECT_TRUE(ExecJs(b2, "window.alive = 'I am alive';"));
// 2) Navigate to b3.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImpl* b3 = current_frame_host();
RenderFrameDeletedObserver b3_observer(b3);
rfh_observer.push_back(&b3_observer);
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_THAT(Elements({url_1, b2}), Each(InBackForwardCache()));
EXPECT_THAT(b3, Not(InBackForwardCache()));
// 3) Navigate to a4.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* a4 = current_frame_host();
RenderFrameDeletedObserver a4_observer(a4);
rfh_observer.push_back(&a4_observer);
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
// 4) Navigate to b5
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImpl* b5 = current_frame_host();
RenderFrameDeletedObserver b5_observer(b5);
rfh_observer.push_back(&b5_observer);
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_THAT(Elements({url_1, b2, b3, a4}), Each(InBackForwardCache()));
EXPECT_THAT(b5, Not(InBackForwardCache()));
// 3) Go back to url_1(b2).
ASSERT_TRUE(HistoryGoToOffset(web_contents(), -3));
EXPECT_EQ(url_1, current_frame_host());
ASSERT_THAT(rfh_observer, Each(Not(Deleted())));
EXPECT_THAT(Elements({b3, a4, b5}), Each(InBackForwardCache()));
EXPECT_THAT(Elements({url_1, b2}), Each(Not(InBackForwardCache())));
// Even after a new IPC round trip with the renderer, b2 must still be alive.
EXPECT_EQ("I am alive", EvalJs(b2, "window.alive"));
EXPECT_FALSE(b2_observer.deleted());
}
// Check that unload event handlers are not dispatched when the page goes
// into BackForwardCache.
IN_PROC_BROWSER_TEST_F(BackForwardCacheUnloadBrowserTest,
ConfirmUnloadEventNotFired) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
// 2) Set unload handler and check the title.
EXPECT_TRUE(ExecJs(rfh_a,
"document.title = 'loaded!';"
"window.addEventListener('unload', () => {"
" document.title = 'unloaded!';"
"});"));
{
std::u16string title_when_loaded = u"loaded!";
TitleWatcher title_watcher(web_contents(), title_when_loaded);
EXPECT_EQ(title_watcher.WaitAndGetTitle(), title_when_loaded);
}
// 3) Navigate to B.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImpl* rfh_b = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b);
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
// 4) Go back to A and check the title again.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_TRUE(rfh_b->IsInBackForwardCache());
{
std::u16string title_when_loaded = u"loaded!";
TitleWatcher title_watcher(web_contents(), title_when_loaded);
EXPECT_EQ(title_watcher.WaitAndGetTitle(), title_when_loaded);
}
}
class BackForwardCacheStillLoadingBrowserTest
: public BackForwardCacheBrowserTest,
public ::testing::WithParamInterface<TestFrameType> {
protected:
std::string GetMainFramePath() {
switch (GetParam()) {
case TestFrameType::kMainFrame:
return "/controlled";
case TestFrameType::kSubFrame:
return "/back_forward_cache/controllable_subframe.html";
case TestFrameType::kSubFrameOfSubframe:
return "/back_forward_cache/controllable_subframe_of_subframe.html";
}
}
int GetNavigationCount() {
switch (GetParam()) {
case TestFrameType::kMainFrame:
return 1;
case TestFrameType::kSubFrame:
return 2;
case TestFrameType::kSubFrameOfSubframe:
return 3;
}
}
};
INSTANTIATE_TEST_SUITE_P(All,
BackForwardCacheStillLoadingBrowserTest,
::testing::Values(TestFrameType::kMainFrame,
TestFrameType::kSubFrame,
TestFrameType::kSubFrameOfSubframe));
IN_PROC_BROWSER_TEST_P(BackForwardCacheStillLoadingBrowserTest,
DoesNotCacheIfFrameStillLoading) {
std::string controlled_path("/controlled");
net::test_server::ControllableHttpResponse response(embedded_test_server(),
controlled_path);
ASSERT_TRUE(embedded_test_server()->Start());
bool testing_main_frame = GetParam() == TestFrameType::kMainFrame;
GURL main_frame_url(
embedded_test_server()->GetURL("a.com", GetMainFramePath()));
// 1) Navigate to a page with a frame that loads partially but never
// completes. We need the navigation of the partial frame to complete to avoid
// extra blocking reasons from occurring.
TestNavigationObserver observer(web_contents(), GetNavigationCount());
observer.set_wait_event(
TestNavigationObserver::WaitEvent::kNavigationFinished);
shell()->LoadURL(main_frame_url);
response.WaitForRequest();
response.Send(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"\r\n"
"<html><body>...");
observer.WaitForNavigationFinished();
// 2) Navigate away.
RenderFrameHostImplWrapper rfh_a(current_frame_host());
rfh_a->DisableUnloadTimerForTesting();
shell()->LoadURL(embedded_test_server()->GetURL("b.com", "/title1.html"));
ASSERT_TRUE(WaitForLoadStop(web_contents()));
// The page should not have been added to cache, since it had a subframe
// that was still loading at the time it was navigated away from.
ASSERT_TRUE(rfh_a.WaitUntilRenderFrameDeleted());
// 3) Go back. If this is the main frame, then going back will get a 404.
ASSERT_NE(HistoryGoBack(web_contents()), testing_main_frame);
ExpectNotRestored({NotRestoredReason::kLoading}, {}, {}, {}, {}, FROM_HERE);
}
class BackForwardCacheStillNavigatingBrowserTest
: public BackForwardCacheBrowserTest,
public ::testing::WithParamInterface<TestFrameType> {
protected:
std::string GetMainFramePath() {
switch (GetParam()) {
case TestFrameType::kMainFrame:
NOTREACHED_IN_MIGRATION();
return "";
case TestFrameType::kSubFrame:
return "/back_forward_cache/controllable_subframe.html";
case TestFrameType::kSubFrameOfSubframe:
return "/back_forward_cache/controllable_subframe_of_subframe.html";
}
}
};
INSTANTIATE_TEST_SUITE_P(All,
BackForwardCacheStillNavigatingBrowserTest,
::testing::Values(TestFrameType::kSubFrame,
TestFrameType::kSubFrameOfSubframe));
IN_PROC_BROWSER_TEST_P(BackForwardCacheStillNavigatingBrowserTest,
DoesNotCacheNavigatingSubframe) {
net::test_server::ControllableHttpResponse response(embedded_test_server(),
"/controlled");
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to a page with an iframe that loads forever.
GURL url(embedded_test_server()->GetURL("a.com", GetMainFramePath()));
shell()->LoadURL(url);
// Wait for the iframe request to arrive, and leave it hanging with no
// response.
response.WaitForRequest();
RenderFrameHostImplWrapper rfh_a(current_frame_host());
// If the "DOMContentLoaded" event has not fired, it will cause BFCache to be
// blocked. We are not
ASSERT_EQ(42, EvalJs(rfh_a.get(), "domContentLoaded"));
// 2) Navigate away.
shell()->LoadURL(embedded_test_server()->GetURL("b.com", "/title1.html"));
EXPECT_TRUE(WaitForLoadStop(web_contents()));
// The page should not have been added to cache, since it had a subframe that
// was still loading at the time it was navigated away from.
ASSERT_TRUE(rfh_a.WaitUntilRenderFrameDeleted());
// 3) Go back.
ASSERT_TRUE(HistoryGoBack(web_contents()));
ExpectNotRestored({NotRestoredReason::kSubframeIsNavigating}, {}, {}, {}, {},
FROM_HERE);
}
// Check that a frame with an invalid url doesn't affect the back-forward cache
// usage.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
FrameWithInvalidURLDoesntAffectCache) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title2.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
// 2) Create some subframes which have an invalid URL
// and thus won't commit a document.
InsertSubFrameWithUrl(rfh_a, "javascript:false");
InsertSubFrameWithUrl(rfh_a, "blob:");
InsertSubFrameWithUrl(rfh_a, "file:///");
// wrongly typed scheme
InsertSubFrameWithUrl(rfh_a, "htt://");
for (size_t i = 0; i < rfh_a->child_count(); i++) {
RenderFrameHostImpl* rfh_subframe =
rfh_a->child_at(i)->current_frame_host();
EXPECT_FALSE(rfh_subframe->IsDOMContentLoaded());
EXPECT_FALSE(rfh_subframe->has_committed_any_navigation());
}
// 3) Navigate to B.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
// The page A should be stored in the back-forward cache.
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
// 4) Go back.
ASSERT_TRUE(HistoryGoBack(web_contents()));
// The page A should be restored from the back-forward cache.
ExpectRestored(FROM_HERE);
}
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, DoesNotCacheIfHttpError) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL error_url(embedded_test_server()->GetURL("a.com", "/page404.html"));
GURL url(embedded_test_server()->GetURL("b.com", "/title1.html"));
// Navigate to an error page.
EXPECT_TRUE(NavigateToURL(shell(), error_url));
EXPECT_EQ(net::HTTP_NOT_FOUND, current_frame_host()->last_http_status_code());
RenderFrameDeletedObserver delete_rfh_a(current_frame_host());
// Navigate away.
EXPECT_TRUE(NavigateToURL(shell(), url));
// The page did not return 200 (OK), so it shouldn't have been cached.
delete_rfh_a.WaitUntilDeleted();
// Go back.
ASSERT_TRUE(HistoryGoBack(web_contents()));
ExpectNotRestored({NotRestoredReason::kHTTPStatusNotOK}, {}, {}, {}, {},
FROM_HERE);
}
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
DoesNotCacheIfPageUnreachable) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL error_url(embedded_test_server()->GetURL("a.com", "/empty.html"));
GURL url(embedded_test_server()->GetURL("b.com", "/title1.html"));
std::unique_ptr<URLLoaderInterceptor> url_interceptor =
URLLoaderInterceptor::SetupRequestFailForURL(error_url,
net::ERR_DNS_TIMED_OUT);
// Start with a successful navigation to a document.
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_EQ(net::HTTP_OK, current_frame_host()->last_http_status_code());
// Navigate to an error page.
NavigationHandleObserver observer(shell()->web_contents(), error_url);
EXPECT_FALSE(NavigateToURL(shell(), error_url));
EXPECT_TRUE(observer.is_error());
EXPECT_EQ(net::ERR_DNS_TIMED_OUT, observer.net_error_code());
EXPECT_EQ(GURL(kUnreachableWebDataURL), shell()
->web_contents()
->GetPrimaryMainFrame()
->GetSiteInstance()
->GetSiteURL());
EXPECT_EQ(net::OK, current_frame_host()->last_http_status_code());
RenderFrameDeletedObserver delete_rfh_a(current_frame_host());
// Navigate away.
EXPECT_TRUE(NavigateToURL(shell(), url));
// The page had a networking error, so it shouldn't have been cached.
delete_rfh_a.WaitUntilDeleted();
// Go back.
web_contents()->GetController().GoBack();
EXPECT_FALSE(WaitForLoadStop(shell()->web_contents()));
ExpectNotRestored(
{NotRestoredReason::kHTTPStatusNotOK, NotRestoredReason::kErrorDocument},
{}, {}, {}, {}, FROM_HERE);
}
// Tests the events are fired when going back from the cache.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Events) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/back_forward_cache/record_events.html"));
GURL url_b(embedded_test_server()->GetURL(
"b.com", "/back_forward_cache/record_events.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
// At A, a page-show event is recorded for the first loading.
MatchEventList(rfh_a.get(), ListValueOf("window.pageshow"));
content::FetchHistogramsFromChildProcesses();
// 2) Navigate to B.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImplWrapper rfh_b(current_frame_host());
EXPECT_FALSE(rfh_a.IsRenderFrameDeleted());
EXPECT_FALSE(rfh_b.IsRenderFrameDeleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
// TODO(yuzus): Post message to the frozen page, and make sure that the
// messages arrive after the page visibility events, not before them.
// As |rfh_a| is in back-forward cache, we cannot get the event list of A.
// At B, a page-show event is recorded for the first loading.
MatchEventList(rfh_b.get(), ListValueOf("window.pageshow"));
content::FetchHistogramsFromChildProcesses();
// 3) Go back to A. Confirm that expected events are fired.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(rfh_a.IsRenderFrameDeleted());
EXPECT_FALSE(rfh_b.IsRenderFrameDeleted());
EXPECT_EQ(rfh_a.get(), current_frame_host());
// visibilitychange events are added twice per each because it is fired for
// both window and document.
MatchEventList(
rfh_a.get(),
ListValueOf("window.pageshow", "window.pagehide.persisted",
"document.visibilitychange", "window.visibilitychange",
"document.freeze", "document.resume",
"document.visibilitychange", "window.visibilitychange",
"window.pageshow.persisted"));
}
// Tests the events are fired for subframes when going back from the cache.
// Test case: a(b) -> c -> a(b)
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, EventsForSubframes) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
// 1) Navigate to A(B).
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameHostImpl* rfh_b = rfh_a->child_at(0)->current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b);
StartRecordingEvents(rfh_a);
StartRecordingEvents(rfh_b);
// 2) Navigate to C.
EXPECT_TRUE(NavigateToURL(shell(), url_c));
RenderFrameHostImpl* rfh_c = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_c(rfh_c);
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_TRUE(rfh_b->IsInBackForwardCache());
EXPECT_FALSE(rfh_c->IsInBackForwardCache());
// TODO(yuzus): Post message to the frozen page, and make sure that the
// messages arrive after the page visibility events, not before them.
// 3) Go back to A(B). Confirm that expected events are fired on the subframe.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_FALSE(delete_observer_rfh_c.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
EXPECT_TRUE(rfh_c->IsInBackForwardCache());
// visibilitychange events are added twice per each because it is fired for
// both window and document.
MatchEventList(
rfh_a,
ListValueOf("window.pagehide.persisted", "document.visibilitychange",
"window.visibilitychange", "document.freeze",
"document.resume", "document.visibilitychange",
"window.visibilitychange", "window.pageshow.persisted"));
MatchEventList(
rfh_b,
ListValueOf("window.pagehide.persisted", "document.visibilitychange",
"window.visibilitychange", "document.freeze",
"document.resume", "document.visibilitychange",
"window.visibilitychange", "window.pageshow.persisted"));
}
// Tests the events are fired when going back from the cache.
// Same as: BackForwardCacheBrowserTest.Events, but with a document-initiated
// navigation. This is a regression test for https://crbug.com/1000324
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
EventsAfterDocumentInitiatedNavigation) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
StartRecordingEvents(rfh_a);
// 2) Navigate to B.
ASSERT_TRUE(NavigateToURLFromRenderer(shell(), url_b));
RenderFrameHostImpl* rfh_b = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b);
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
// TODO(yuzus): Post message to the frozen page, and make sure that the
// messages arrive after the page visibility events, not before them.
// 3) Go back to A. Confirm that expected events are fired.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_FALSE(delete_observer_rfh_b.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
// visibilitychange events are added twice per each because it is fired for
// both window and document.
MatchEventList(
rfh_a,
ListValueOf("window.pagehide.persisted", "document.visibilitychange",
"window.visibilitychange", "document.freeze",
"document.resume", "document.visibilitychange",
"window.visibilitychange", "window.pageshow.persisted"));
}
// Track the events dispatched when a page is deemed ineligible for back-forward
// cache after we've dispatched the 'pagehide' event with persisted set to true.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
EventsForPageIneligibleAfterPagehidePersisted) {
ASSERT_TRUE(CreateHttpsServer()->Start());
GURL url_1(https_server()->GetURL("a.com", kBlockingPagePath));
GURL url_2(https_server()->GetURL("a.com", "/title2.html"));
// 1) Navigate to |url_1|.
EXPECT_TRUE(NavigateToURL(shell(), url_1));
RenderFrameHostImpl* rfh_1 = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_1(rfh_1);
// 2) The page uses a non-sticky blocklisted feature, so that we
// would still do a RFH swap on same-site navigation and fire the 'pagehide'
// event during commit of the new page with 'persisted' set to true, but the
// page will not be eligible for back-forward cache after commit.
EXPECT_TRUE(ExecJs(rfh_1, R"(
window.onpagehide = (e) => {
console.log("onagepagehide", e.persisted);
localStorage.setItem('pagehide_persisted',
e.persisted ? 'true' : 'false');
}
document.onvisibilitychange = () => {
localStorage.setItem('visibilitychange',
document.visibilityState);
}
)"));
// 3) Navigate to |url_2|.
EXPECT_TRUE(NavigateToURL(shell(), url_2));
// |rfh_1| will not get into the back-forward cache and eventually get deleted
// because it uses a blocklisted feature.
delete_observer_rfh_1.WaitUntilDeleted();
EXPECT_EQ("true",
GetLocalStorage(current_frame_host(), "pagehide_persisted"));
EXPECT_EQ("hidden",
GetLocalStorage(current_frame_host(), "visibilitychange"));
}
// Track the events dispatched when a page is deemed ineligible for back-forward
// cache before we've dispatched the pagehide event on it.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
EventsForPageIneligibleBeforePagehide) {
ASSERT_TRUE(CreateHttpsServer()->Start());
GURL url_1(https_server()->GetURL("a.com", "/title1.html"));
GURL url_2(https_server()->GetURL("b.com", "/title2.html"));
// 1) Navigate to |url_1|.
EXPECT_TRUE(NavigateToURL(shell(), url_1));
RenderFrameHostImpl* rfh_1 = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_1(rfh_1);
// 2) Use a dummy sticky blocklisted feature, so that the page is known to be
// ineligible for bfcache at commit time, before we dispatch pagehide event.
rfh_1->UseDummyStickyBackForwardCacheDisablingFeatureForTesting();
EXPECT_TRUE(ExecJs(rfh_1, R"(
window.onpagehide = (e) => {
if (!e.persisted) {
window.domAutomationController.send('pagehide.not_persisted');
}
}
document.onvisibilitychange = () => {
if (document.visibilityState == 'hidden') {
window.domAutomationController.send('visibilitychange.hidden');
}
}
)"));
DOMMessageQueue dom_message_queue(shell()->web_contents());
// 3) Navigate to |url_2|.
EXPECT_TRUE(NavigateToURL(shell(), url_2));
// |rfh_1| will not get into the back-forward cache and eventually get deleted
// because it uses a blocklisted feature.
delete_observer_rfh_1.WaitUntilDeleted();
// "pagehide", "visibilitychange", and "unload" events will be dispatched.
int num_messages_received = 0;
const auto expected_messages = std::to_array<std::string>({
"\"pagehide.not_persisted\"",
"\"visibilitychange.hidden\"",
});
std::string message;
while (dom_message_queue.PopMessage(&message)) {
EXPECT_EQ(expected_messages[num_messages_received], message);
num_messages_received++;
}
EXPECT_EQ(num_messages_received, 2);
}
enum class StickinessType {
kSticky,
kNonSticky,
};
class BackForwardCacheBrowserTestWithVaryingFrameAndFeatureStickinessType
: public BackForwardCacheBrowserTest,
public ::testing::WithParamInterface<
testing::tuple<TestFrameType, StickinessType>> {};
INSTANTIATE_TEST_SUITE_P(
All,
BackForwardCacheBrowserTestWithVaryingFrameAndFeatureStickinessType,
::testing::Combine(::testing::Values(TestFrameType::kMainFrame,
TestFrameType::kSubFrame),
::testing::Values(StickinessType::kSticky,
StickinessType::kNonSticky)));
// Test pagehide's persisted value and whether the page can be BFCached when a
// sticky/non-sticky feature is used on the mainframe/subframe.
//
// TODO(crbug.com/40913015): Flaky on all platforms.
IN_PROC_BROWSER_TEST_P(
BackForwardCacheBrowserTestWithVaryingFrameAndFeatureStickinessType,
DISABLED_TestPagehidePersistedValue) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
// 1) Navigate to A(B).
EXPECT_TRUE(NavigateToURL(shell(), url_a));
TestFrameType parameter_frame = std::get<0>(GetParam());
StickinessType use_sticky_feature = std::get<1>(GetParam());
// Depending on the parameter, pick the mainframe or subframe to add a
// blocking feature.
RenderFrameHostImplWrapper rfh_with_blocking_feature(
parameter_frame == TestFrameType::kSubFrame
? current_frame_host()->child_at(0)->current_frame_host()
: current_frame_host());
// 2) Mark the document as using a feature that's either sticky or non-sticky,
// depending on the test parameter.
if (use_sticky_feature == StickinessType::kSticky) {
rfh_with_blocking_feature.get()
->UseDummyStickyBackForwardCacheDisablingFeatureForTesting();
} else {
EXPECT_TRUE(ExecJs(rfh_with_blocking_feature.get(),
"window.foo = new BroadcastChannel('foo');"));
}
// 3) Install the pagehide handler in A to know pagehide.persisted status
// after navigating to B.
EXPECT_TRUE(ExecJs(current_frame_host(), R"(
window.onpagehide = (e) => {
localStorage.setItem('pagehide_persisted',
e.persisted ? 'true' : 'false');
}
)"));
// 4) Navigate to B.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
// 5) Go back to the previous page.
ASSERT_TRUE(HistoryGoBack(web_contents()));
// 6) If the page is using a sticky feature at pagehide, it can never be put
// into BFCache no matter what pagehide does, so pagehide's persisted is
// false. Meanwhile, if the page is using a non-sticky feature at pagehide, it
// can still be put into BFCache if the pagehide event removes the feature's
// usage, so pagehide's persisted is true, since the page might still get into
// BFCache.
EXPECT_EQ(use_sticky_feature == StickinessType::kSticky ? "false" : "true",
GetLocalStorage(current_frame_host(), "pagehide_persisted"));
// 7) Confirm that the page was not restored from the BFCache in both the
// sticky and non-sticky cases.
blink::scheduler::WebSchedulerTrackedFeature expected_feature =
(use_sticky_feature == StickinessType::kSticky)
? blink::scheduler::WebSchedulerTrackedFeature::kDummy
: blink::scheduler::WebSchedulerTrackedFeature::kBroadcastChannel;
ExpectNotRestored({NotRestoredReason::kBlocklistedFeatures},
{expected_feature}, {}, {}, {}, FROM_HERE);
}
IN_PROC_BROWSER_TEST_F(HighCacheSizeBackForwardCacheBrowserTest,
CanCacheMultiplesPagesOnSameDomain) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_1(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b2(embedded_test_server()->GetURL("b.com", "/title1.html"));
GURL url_a3(embedded_test_server()->GetURL("a.com", "/title2.html"));
GURL url_b4(embedded_test_server()->GetURL("b.com", "/title2.html"));
// 1) Navigate to url_1.
EXPECT_TRUE(NavigateToURL(shell(), url_1));
RenderFrameHostImpl* rfh_url_1 = current_frame_host();
// 2) Navigate to B2.
EXPECT_TRUE(NavigateToURL(shell(), url_b2));
RenderFrameHostImpl* rfh_b2 = current_frame_host();
EXPECT_TRUE(rfh_url_1->IsInBackForwardCache());
// 3) Navigate to A3.
EXPECT_TRUE(NavigateToURL(shell(), url_a3));
RenderFrameHostImpl* rfh_a3 = current_frame_host();
EXPECT_TRUE(rfh_url_1->IsInBackForwardCache());
EXPECT_TRUE(rfh_b2->IsInBackForwardCache());
// url_1 and A3 shouldn't be treated as the same site instance.
EXPECT_NE(rfh_url_1->GetSiteInstance(), rfh_a3->GetSiteInstance());
// 4) Navigate to B4.
// Make sure we can store url_1 and A3 in the cache at the same time.
EXPECT_TRUE(NavigateToURL(shell(), url_b4));
RenderFrameHostImpl* rfh_b4 = current_frame_host();
EXPECT_TRUE(rfh_url_1->IsInBackForwardCache());
EXPECT_TRUE(rfh_b2->IsInBackForwardCache());
EXPECT_TRUE(rfh_a3->IsInBackForwardCache());
// 5) Go back to A3.
// Make sure we can restore A3, while url_1 remains in the cache.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_TRUE(rfh_url_1->IsInBackForwardCache());
EXPECT_TRUE(rfh_b2->IsInBackForwardCache());
EXPECT_TRUE(rfh_b4->IsInBackForwardCache());
EXPECT_EQ(rfh_a3, current_frame_host());
// B2 and B4 shouldn't be treated as the same site instance.
EXPECT_NE(rfh_b2->GetSiteInstance(), rfh_b4->GetSiteInstance());
// 6) Do a history navigation back to url_1.
// Make sure we can restore url_1, while coming from A3.
ASSERT_TRUE(HistoryGoToIndex(web_contents(), 0));
EXPECT_TRUE(rfh_b2->IsInBackForwardCache());
EXPECT_TRUE(rfh_b4->IsInBackForwardCache());
EXPECT_TRUE(rfh_a3->IsInBackForwardCache());
EXPECT_EQ(rfh_url_1, current_frame_host());
}
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Encoding) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/back_forward_cache/charset_windows-1250.html"));
GURL url_b(embedded_test_server()->GetURL(
"b.com", "/back_forward_cache/charset_utf-8.html"));
url::Origin origin_a = url::Origin::Create(url_a);
url::Origin origin_b = url::Origin::Create(url_b);
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
EXPECT_EQ(web_contents()->GetEncoding(), "windows-1250");
EXPECT_TRUE(NavigateToURL(shell(), url_b));
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_EQ(web_contents()->GetEncoding(), "UTF-8");
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_EQ(web_contents()->GetEncoding(), "windows-1250");
}
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
DoesNotCacheCrossSiteHttpPost) {
SetupCrossSiteRedirector(embedded_test_server());
ASSERT_TRUE(embedded_test_server()->Start());
// Note we do a cross-site post because same-site navigations of any kind
// aren't cached currently.
GURL form_url(embedded_test_server()->GetURL(
"a.com", "/form_that_posts_cross_site.html"));
GURL redirect_target_url(embedded_test_server()->GetURL("x.com", "/echoall"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
// Navigate to the page with form that posts via 307 redirection to
// |redirect_target_url| (cross-site from |form_url|).
EXPECT_TRUE(NavigateToURL(shell(), form_url));
// Submit the form.
TestNavigationObserver form_post_observer(shell()->web_contents(), 1);
EXPECT_TRUE(ExecJs(shell(), "document.getElementById('text-form').submit()"));
form_post_observer.Wait();
// Verify that we arrived at the expected, redirected location.
EXPECT_EQ(redirect_target_url,
shell()->web_contents()->GetLastCommittedURL());
RenderFrameDeletedObserver delete_observer_rfh(current_frame_host());
// Navigate away. |redirect_target_url|'s page should not be cached.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
delete_observer_rfh.WaitUntilDeleted();
}
// On windows, the expected value is off by ~20ms. In order to get the
// feature out to canary, the test is disabled for WIN.
// TODO(crbug.com/40657468): Fix this for Win.
// TODO(crbug.com/40767606): Flaky on other platforms.
// Make sure we are exposing the duration between back navigation's
// navigationStart and the page's original navigationStart through pageshow
// event's timeStamp, and that we aren't modifying
// performance.timing.navigationStart.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, DISABLED_NavigationStart) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/back_forward_cache/record_navigation_start_time_stamp.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
double initial_page_show_time_stamp =
EvalJs(shell(), "window.initialPageShowTimeStamp").ExtractDouble();
EXPECT_DOUBLE_EQ(
initial_page_show_time_stamp,
EvalJs(shell(), "window.latestPageShowTimeStamp").ExtractDouble());
double initial_navigation_start =
EvalJs(shell(), "window.initialNavigationStart").ExtractDouble();
// 2) Navigate to B. A should be in the back forward cache.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
// 3) Navigate back and expect everything to be restored.
NavigationHandleObserver observer(web_contents(), url_a);
base::TimeTicks time_before_navigation = base::TimeTicks::Now();
double js_time_before_navigation =
EvalJs(shell(), "performance.now()").ExtractDouble();
ASSERT_TRUE(HistoryGoBack(web_contents()));
base::TimeTicks time_after_navigation = base::TimeTicks::Now();
double js_time_after_navigation =
EvalJs(shell(), "performance.now()").ExtractDouble();
// The navigation start time should be between the time we saved just before
// calling GoBack() and the time we saved just after calling GoBack().
base::TimeTicks back_navigation_start = observer.navigation_start();
EXPECT_LT(time_before_navigation, back_navigation_start);
EXPECT_GT(time_after_navigation, back_navigation_start);
// Check JS values. window.initialNavigationStart should not change.
EXPECT_DOUBLE_EQ(
initial_navigation_start,
EvalJs(shell(), "window.initialNavigationStart").ExtractDouble());
// performance.timing.navigationStart should not change.
EXPECT_DOUBLE_EQ(
initial_navigation_start,
EvalJs(shell(), "performance.timing.navigationStart").ExtractDouble());
// window.initialPageShowTimeStamp should not change.
EXPECT_DOUBLE_EQ(
initial_page_show_time_stamp,
EvalJs(shell(), "window.initialPageShowTimeStamp").ExtractDouble());
// window.latestPageShowTimeStamp should be updated with the timestamp of the
// last pageshow event, which occurs after the page is restored. This should
// be greater than the initial pageshow event's timestamp.
double latest_page_show_time_stamp =
EvalJs(shell(), "window.latestPageShowTimeStamp").ExtractDouble();
EXPECT_LT(initial_page_show_time_stamp, latest_page_show_time_stamp);
// |latest_page_show_time_stamp| should be the duration between initial
// navigation start and |back_navigation_start|. Note that since
// performance.timing.navigationStart returns a 64-bit integer instead of
// double, we might be losing somewhere between 0 to 1 milliseconds of
// precision, hence the usage of EXPECT_NEAR.
EXPECT_NEAR(
(back_navigation_start - base::TimeTicks::UnixEpoch()).InMillisecondsF(),
latest_page_show_time_stamp + initial_navigation_start, 1.0);
// Expect that the back navigation start value calculated from the JS results
// are between time taken before & after navigation, just like
// |before_navigation_start|.
EXPECT_LT(js_time_before_navigation, latest_page_show_time_stamp);
EXPECT_GT(js_time_after_navigation, latest_page_show_time_stamp);
}
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
CanUseCacheWhenNavigatingAwayToErrorPage) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL error_url(embedded_test_server()->GetURL("b.com", "/empty.html"));
auto url_interceptor = URLLoaderInterceptor::SetupRequestFailForURL(
error_url, net::ERR_DNS_TIMED_OUT);
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
// 2) Navigate to an error page and expect the old page to be stored in
// bfcache.
EXPECT_FALSE(NavigateToURL(shell(), error_url));
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
// 3) Navigate back and expect the page to be restored from bfcache.
ASSERT_TRUE(HistoryGoBack(web_contents()));
}
// RenderFrameHostImpl::coep_reporter() must be preserved when doing a back
// navigation using the BackForwardCache.
// Regression test for https://crbug.com/1102285.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, CoepReporter) {
ASSERT_TRUE(CreateHttpsServer()->Start());
GURL url_a(https_server()->GetURL("a.com",
"/set-header?"
"Cross-Origin-Embedder-Policy-Report-Only: "
"require-corp; report-to%3d\"a\""));
GURL url_b(https_server()->GetURL("b.com", "/title1.html"));
// Navigate to a document that set RenderFrameHostImpl::coep_reporter().
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
EXPECT_TRUE(rfh_a->coep_reporter());
// Navigate away and back using the BackForwardCache. The
// RenderFrameHostImpl::coep_reporter() must still be there.
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
EXPECT_TRUE(NavigateToURL(shell(), url_b));
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_TRUE(rfh_a->coep_reporter());
}
// RenderFrameHostImpl::coop_reporter() must be preserved when doing a back
// navigation using the BackForwardCache.
// Regression test for https://crbug.com/1102285.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, CoopReporter) {
ASSERT_TRUE(CreateHttpsServer()->Start());
GURL url_a(https_server()->GetURL("a.com",
"/set-header?"
"Cross-Origin-Opener-Policy-Report-Only: "
"same-origin; report-to%3d\"a\""));
GURL url_b(https_server()->GetURL("b.com", "/title1.html"));
// Navigate to a document that set RenderFrameHostImpl::coop_reporter().
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
EXPECT_TRUE(rfh_a->coop_access_report_manager()->coop_reporter());
// Navigate away and back using the BackForwardCache. The
// RenderFrameHostImpl::coop_reporter() must still be there.
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
EXPECT_TRUE(NavigateToURL(shell(), url_b));
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_TRUE(rfh_a->coop_access_report_manager()->coop_reporter());
}
// RenderFrameHostImpl::cross_origin_embedder_policy() must be preserved when
// doing a back navigation using the BackForwardCache.
// Regression test for https://crbug.com/1021846.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Coep) {
ASSERT_TRUE(CreateHttpsServer()->Start());
GURL url_a(https_server()->GetURL(
"a.com", "/set-header?Cross-Origin-Embedder-Policy: require-corp"));
GURL url_b(https_server()->GetURL("b.com", "/title1.html"));
// Navigate to a document that sets COEP.
network::CrossOriginEmbedderPolicy coep;
coep.value = network::mojom::CrossOriginEmbedderPolicyValue::kRequireCorp;
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImpl* rfh_a = current_frame_host();
EXPECT_EQ(coep, rfh_a->cross_origin_embedder_policy());
// Navigate away and back using the BackForwardCache.
// RenderFrameHostImpl::cross_origin_embedder_policy() should return the same
// result.
RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a);
EXPECT_TRUE(NavigateToURL(shell(), url_b));
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_FALSE(delete_observer_rfh_a.deleted());
EXPECT_EQ(rfh_a, current_frame_host());
EXPECT_EQ(coep, rfh_a->cross_origin_embedder_policy());
}
// Tests that pagehide and visibilitychange handlers of the old RFH are run for
// bfcached pages.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
PagehideAndVisibilitychangeRuns) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_1(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_2(embedded_test_server()->GetURL("b.com", "/title2.html"));
GURL url_3(embedded_test_server()->GetURL("a.com", "/title2.html"));
WebContentsImpl* web_contents =
static_cast<WebContentsImpl*>(shell()->web_contents());
// 1) Navigate to |url_1|.
EXPECT_TRUE(NavigateToURL(shell(), url_1));
RenderFrameHostImpl* main_frame_1 = web_contents->GetPrimaryMainFrame();
// Create a pagehide handler that sets item "pagehide_storage" and a
// visibilitychange handler that sets item "visibilitychange_storage" in
// localStorage.
EXPECT_TRUE(ExecJs(main_frame_1,
R"(
localStorage.setItem('pagehide_storage', 'not_dispatched');
var dispatched_pagehide = false;
window.onpagehide = function(e) {
if (dispatched_pagehide) {
// We shouldn't dispatch pagehide more than once.
localStorage.setItem('pagehide_storage', 'dispatched_more_than_once');
} else if (!e.persisted) {
localStorage.setItem('pagehide_storage', 'wrong_persisted');
} else {
localStorage.setItem('pagehide_storage', 'dispatched_once');
}
dispatched_pagehide = true;
}
localStorage.setItem('visibilitychange_storage', 'not_dispatched');
var dispatched_visibilitychange = false;
document.onvisibilitychange = function(e) {
if (dispatched_visibilitychange) {
// We shouldn't dispatch visibilitychange more than once.
localStorage.setItem('visibilitychange_storage',
'dispatched_more_than_once');
} else if (document.visibilityState != 'hidden') {
// We should dispatch the event when the visibilityState is 'hidden'.
localStorage.setItem('visibilitychange_storage', 'not_hidden');
} else {
localStorage.setItem('visibilitychange_storage', 'dispatched_once');
}
dispatched_visibilitychange = true;
}
)"));
// 2) Navigate cross-site to |url_2|. We need to navigate cross-site to make
// sure we won't run pagehide and visibilitychange during new page's commit,
// which is tested in ProactivelySwapBrowsingInstancesSameSiteTest.
EXPECT_TRUE(NavigateToURL(shell(), url_2));
// |main_frame_1| should be in the back-forward cache.
EXPECT_TRUE(main_frame_1->IsInBackForwardCache());
// 3) Navigate to |url_3| which is same-origin with |url_1|, so we can check
// the localStorage values.
EXPECT_TRUE(NavigateToURL(shell(), url_3));
RenderFrameHostImpl* main_frame_3 = web_contents->GetPrimaryMainFrame();
// Check that the value for 'pagehide_storage' and 'visibilitychange_storage'
// are set correctly.
EXPECT_TRUE(
WaitForLocalStorage(main_frame_3, "pagehide_storage", "dispatched_once"));
EXPECT_TRUE(WaitForLocalStorage(main_frame_3, "visibilitychange_storage",
"dispatched_once"));
}
// Tests that the history value saved in the renderer is updated correctly when
// a page gets restored from the back-forward cache through browser-initiated
// navigation.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
RendererHistory_BrowserInitiated) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url1(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url2(embedded_test_server()->GetURL("a.com", "/title1.html"));
// 1) Go to |url1|, then |url2|. Both pages should have script to save the
// history.length value when getting restored from the back-forward cache.
EXPECT_TRUE(NavigateToURL(shell(), url1));
FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root();
FrameTreeNode* subframe = root->child_at(0);
std::string restore_time_length_saver_script =
"var resumeLength = -1;"
"var pageshowLength = -1;"
"document.onresume = () => {"
" resumeLength = history.length;"
"};"
"window.onpageshow = () => {"
" pageshowLength = history.length;"
"};";
EXPECT_TRUE(ExecJs(root, restore_time_length_saver_script));
EXPECT_TRUE(ExecJs(subframe, restore_time_length_saver_script));
// We should have one history entry.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 1);
EXPECT_EQ(EvalJs(subframe, "history.length").ExtractInt(), 1);
EXPECT_TRUE(NavigateToURL(shell(), url2));
EXPECT_TRUE(ExecJs(root, restore_time_length_saver_script));
// We should have two history entries.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 2);
// 2) Go back to |url1|, browser-initiated.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_EQ(web_contents()->GetLastCommittedURL(), url1);
// We should still have two history entries, and recorded the correct length
// when the 'resume' and 'pageshow' events were dispatched.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "resumeLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "pageshowLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(subframe, "history.length").ExtractInt(), 2);
EXPECT_EQ(EvalJs(subframe, "resumeLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(subframe, "pageshowLength").ExtractInt(), 2);
// 3) Go forward to |url2|, browser-initiated.
ASSERT_TRUE(HistoryGoForward(web_contents()));
EXPECT_EQ(web_contents()->GetLastCommittedURL(), url2);
// We should still have two history entries, and recorded the correct length
// when the 'resume' and 'pageshow' events were dispatched.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "resumeLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "pageshowLength").ExtractInt(), 2);
}
// Tests that the history value saved in the renderer is updated correctly when
// a page gets restored from the back-forward cache through renderer-initiated
// navigation.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
RendererHistory_RendererInitiated) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url1(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url2(embedded_test_server()->GetURL("a.com", "/title1.html"));
// 1) Go to |url1|, then |url2|. Both pages should have script to save the
// history.length value when getting restored from the back-forward cache.
EXPECT_TRUE(NavigateToURL(shell(), url1));
FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root();
FrameTreeNode* subframe = root->child_at(0);
std::string restore_time_length_saver_script =
"var resumeLength = -1;"
"var pageshowLength = -1;"
"document.onresume = () => {"
" resumeLength = history.length;"
"};"
"window.onpageshow = () => {"
" pageshowLength = history.length;"
"};";
EXPECT_TRUE(ExecJs(root, restore_time_length_saver_script));
EXPECT_TRUE(ExecJs(subframe, restore_time_length_saver_script));
// We should have one history entry.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 1);
EXPECT_EQ(EvalJs(subframe, "history.length").ExtractInt(), 1);
EXPECT_TRUE(NavigateToURL(shell(), url2));
EXPECT_TRUE(ExecJs(root, restore_time_length_saver_script));
// We should have two history entries.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 2);
// 2) Go back to |url1|, renderer-initiated.
EXPECT_TRUE(ExecJs(root, "history.back()"));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
EXPECT_EQ(web_contents()->GetLastCommittedURL(), url1);
// We should still have two history entries, and recorded the correct length
// when the 'resume' and 'pageshow' events were dispatched.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "resumeLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "pageshowLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(subframe, "history.length").ExtractInt(), 2);
EXPECT_EQ(EvalJs(subframe, "resumeLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(subframe, "pageshowLength").ExtractInt(), 2);
// 3) Go forward to |url2|, renderer-initiated.
EXPECT_TRUE(ExecJs(root, "history.forward()"));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
EXPECT_EQ(web_contents()->GetLastCommittedURL(), url2);
// We should still have two history entries, and recorded the correct length
// when the 'resume' and 'pageshow' events were dispatched.
EXPECT_EQ(EvalJs(root, "history.length").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "resumeLength").ExtractInt(), 2);
EXPECT_EQ(EvalJs(root, "pageshowLength").ExtractInt(), 2);
}
// Check that an eligible page is cached when navigating to about:blank.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
NavigatingToAboutBlankDoesNotPreventCaching) {
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to a.com,
GURL url_a(embedded_test_server()->GetURL("a.com", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), url_a));
// 2) Navigate to about:blank.
GURL blank_url(url::kAboutBlankURL);
EXPECT_TRUE(NavigateToURL(shell(), blank_url));
// 3) Navigate back to a.com.
ASSERT_TRUE(HistoryGoBack(web_contents()));
ExpectRestored(FROM_HERE);
}
// Check that the response 204 No Content doesn't affect back-forward cache.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, NoContent) {
ASSERT_TRUE(embedded_test_server()->Start());
NavigationControllerImpl& controller = web_contents()->GetController();
// 1) Navigate to a.com.
GURL url_a(embedded_test_server()->GetURL("a.com", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), url_a));
EXPECT_EQ(1, controller.GetEntryCount());
EXPECT_EQ(url_a, controller.GetLastCommittedEntry()->GetURL());
// 2) Navigate to b.com
GURL url_b(embedded_test_server()->GetURL("b.com", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), url_b));
EXPECT_EQ(2, controller.GetEntryCount());
EXPECT_EQ(url_b, controller.GetLastCommittedEntry()->GetURL());
// 3) Navigate to c.com with 204 No Content, then the URL will still be b.com.
GURL url_c(embedded_test_server()->GetURL("c.com", "/echo?status=204"));
EXPECT_TRUE(NavigateToURL(shell(), url_c, url_b));
EXPECT_EQ(2, controller.GetEntryCount());
EXPECT_EQ(url_b, controller.GetLastCommittedEntry()->GetURL());
// 4) Navigate back to a.com.
ASSERT_TRUE(HistoryGoBack(web_contents()));
EXPECT_EQ(2, controller.GetEntryCount());
EXPECT_EQ(url_a, controller.GetLastCommittedEntry()->GetURL());
ExpectRestored(FROM_HERE);
}
// A testing subclass that limits the cache size to 1 for ease of testing
// evictions.
class CacheSizeOneBackForwardCacheBrowserTest
: public BackForwardCacheBrowserTest {
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
EnableFeatureAndSetParams(features::kBackForwardCache, "cache_size",
base::NumberToString(1));
BackForwardCacheBrowserTest::SetUpCommandLine(command_line);
}
};
IN_PROC_BROWSER_TEST_F(CacheSizeOneBackForwardCacheBrowserTest,
ReplacedNavigationEntry) {
// Set the bfcache value to 1 to ensure that the test fails if a page
// that replaces the current history entry is stored in back-forward cache.
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.test", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.test", "/title1.html"));
GURL url_c(embedded_test_server()->GetURL("c.test", "/title1.html"));
// 1) Navigate to A.
EXPECT_TRUE(NavigateToURL(shell(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
// 2) Navigate to B.
EXPECT_TRUE(NavigateToURL(shell(), url_b));
RenderFrameHostImplWrapper rfh_b(current_frame_host());
EXPECT_FALSE(rfh_a.IsRenderFrameDeleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_b->IsInBackForwardCache());
// 3) Navigate to a new page by replacing the location. The old page can't
// be navigated back to and we should not store it in the back-forward
// cache.
EXPECT_TRUE(
ExecJs(shell(), JsReplace("window.location.replace($1);", url_c)));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
RenderFrameHostImplWrapper rfh_c(current_frame_host());
// 4) Confirm A is still in BackForwardCache and it wasn't evicted due to the
// cache size limit, which would happen if we tried to store a new page in the
// cache in the previous step.
EXPECT_FALSE(rfh_a.IsRenderFrameDeleted());
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
// 5) Confirm that navigating backwards goes back to A.
ASSERT_TRUE(HistoryGoBack(shell()->web_contents()));
EXPECT_EQ(rfh_a.get(), current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_EQ(rfh_a->GetVisibilityState(), PageVisibilityState::kVisible);
// Go forward again, should return to C
ASSERT_TRUE(HistoryGoForward(shell()->web_contents()));
EXPECT_EQ(rfh_c.get(), current_frame_host());
EXPECT_EQ(rfh_c->GetVisibilityState(), PageVisibilityState::kVisible);
}
// Server redirect happens when doing history navigation, causing a SiteInstance
// swap and a new navigation entry. Ensure that the reasons from the old entry
// are copied to the new one and reported.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, ServerRedirect) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
// Navigate to a.com. This time the redirect does not happen.
ASSERT_TRUE(NavigateToURL(web_contents(), url_a));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
EXPECT_EQ(url_a, rfh_a->GetLastCommittedURL());
// Replace the history URL to server-redirect.
std::string replace_state =
"window.history.replaceState(null, '', '/server-redirect?" +
url_b.spec() + "');";
EXPECT_TRUE(ExecJs(rfh_a.get(), replace_state));
// Navigate to c.com.
EXPECT_TRUE(NavigateToURL(shell(), url_c));
RenderFrameHostImplWrapper rfh_b(current_frame_host());
EvictByJavaScript(rfh_a.get());
// Navigate back, going back to b.com instead of a.com because of server
// redirect.
ASSERT_TRUE(HistoryGoBack(web_contents()));
RenderFrameHostImplWrapper rfh_a_redirect(current_frame_host());
EXPECT_EQ(url_b, rfh_a_redirect->GetLastCommittedURL());
// Make sure that the eviction reason is recorded.
ExpectNotRestored({NotRestoredReason::kJavaScriptExecution}, {}, {}, {}, {},
FROM_HERE);
}
class BackForwardCacheBrowsingContextStateBrowserTest
: public BackForwardCacheBrowserTest,
public ::testing::WithParamInterface<bool> {
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
IsolateAllSitesForTesting(command_line);
if (GetParam()) {
EnableFeatureAndSetParams(
features::kNewBrowsingContextStateOnBrowsingContextGroupSwap, "", "");
}
BackForwardCacheBrowserTest::SetUpCommandLine(command_line);
}
};
INSTANTIATE_TEST_SUITE_P(All,
BackForwardCacheBrowsingContextStateBrowserTest,
::testing::Bool());
// Check that if a RenderViewHost is removed after the page has entered
// the back/forward cache we don't crash.
IN_PROC_BROWSER_TEST_P(BackForwardCacheBrowsingContextStateBrowserTest,
SlowUnloadHandlerInIframe) {
DoNotFailForUnexpectedMessagesWhileCached();
ASSERT_TRUE(embedded_test_server()->Start());
GURL initial_url(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
GURL url_d(embedded_test_server()->GetURL("d.com", "/title1.html"));
// 1) Navigate on a page with an iframe.
EXPECT_TRUE(NavigateToURL(shell(), initial_url));
RenderFrameHostImplWrapper rfh_a(current_frame_host());
RenderFrameHostImplWrapper rfh_b(rfh_a->child_at(0)->current_frame_host());
// 2) Act like we have a slow unload handler.
auto unload_ack_filter = base::BindRepeating([] { return true; });
rfh_b->SetUnloadACKCallbackForTesting(unload_ack_filter);
rfh_b->DisableUnloadTimerForTesting();
// 3) Navigate the inner iframe to a new origin.
EXPECT_TRUE(NavigateToURLFromRenderer(rfh_a->child_at(0), url_c));
RenderFrameHostImplWrapper rfh_c(rfh_a->child_at(0)->current_frame_host());
// 4) Now navigate away.
EXPECT_TRUE(NavigateToURL(shell(), url_d));
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_TRUE(rfh_c->IsInBackForwardCache());
EXPECT_EQ(RenderFrameHostImpl::LifecycleStateImpl::kRunningUnloadHandlers,
rfh_b->lifecycle_state());
rfh_b->SetUnloadACKCallbackForTesting(base::NullCallback());
rfh_b->OnUnloadACK();
EXPECT_TRUE(rfh_a->IsInBackForwardCache());
EXPECT_TRUE(rfh_b.IsDestroyed());
EXPECT_FALSE(rfh_c.IsDestroyed());
EXPECT_TRUE(rfh_c->IsInBackForwardCache());
// 5) This iterates each RenderViewHost and should not crash.
web_contents()->WasHidden();
// 6) Confirm that navigating backwards restores the page from
// back/forward cache.
ASSERT_TRUE(HistoryGoBack(shell()->web_contents()));
EXPECT_EQ(rfh_a.get(), current_frame_host());
EXPECT_FALSE(rfh_a->IsInBackForwardCache());
EXPECT_FALSE(rfh_c->IsInBackForwardCache());
ExpectRestored(FROM_HERE);
EXPECT_EQ(
" Site A ------------ proxies for B\n"
" +--Site B ------- proxies for A\n"
"Where A = http://a.com/\n"
" B = http://c.com/",
DepictFrameTree(current_frame_host()->frame_tree_node()));
}
} // namespace content