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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/file_system_access/file_system_access_lock_manager.h"
#include <cstdlib>
#include <ctime>
#include <memory>
#include <string>
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "content/browser/file_system_access/features.h"
#include "content/browser/file_system_access/file_system_access_manager_impl.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/file_system_access_permission_context.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/test/test_file_system_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features_generated.h"
namespace content {
using LockType = FileSystemAccessLockManager::LockType;
using storage::FileSystemURL;
static constexpr char kTestMountPoint[] = "testfs";
class FileSystemAccessLockManagerTest : public RenderViewHostTestHarness {
public:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
ASSERT_TRUE(dir_.CreateUniqueTempDir());
// Register an external mount point to test support for virtual paths.
// This maps the virtual path a native local path to make these tests work
// on all platforms. We're not testing more complicated ChromeOS specific
// file system backends here.
storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
kTestMountPoint, storage::kFileSystemTypeLocal,
storage::FileSystemMountOption(), dir_.GetPath());
file_system_context_ = storage::CreateFileSystemContextForTesting(
/*quota_manager_proxy=*/nullptr, dir_.GetPath());
chrome_blob_context_ = base::MakeRefCounted<ChromeBlobStorageContext>();
chrome_blob_context_->InitializeOnIOThread(base::FilePath(),
base::FilePath(), nullptr);
manager_ = base::MakeRefCounted<FileSystemAccessManagerImpl>(
file_system_context_, chrome_blob_context_,
/*permission_context=*/nullptr,
/*off_the_record=*/false);
}
void TearDown() override {
manager_.reset();
task_environment()->RunUntilIdle();
EXPECT_TRUE(dir_.Delete());
chrome_blob_context_.reset();
RenderViewHostTestHarness::TearDown();
}
storage::FileSystemURL CreateLocalUrl(const base::FilePath& path) {
return manager_->CreateFileSystemURLFromPath(PathInfo(path));
}
std::unique_ptr<base::test::TestFuture<
scoped_refptr<FileSystemAccessLockManager::LockHandle>>>
TakeLockAsync(
const FileSystemAccessManagerImpl::BindingContext binding_context,
const storage::FileSystemURL& url,
FileSystemAccessLockManager::LockType lock_type) {
auto future = std::make_unique<base::test::TestFuture<
scoped_refptr<FileSystemAccessLockManager::LockHandle>>>();
manager_->TakeLock(binding_context, url, lock_type, future->GetCallback());
return future;
}
scoped_refptr<FileSystemAccessLockManager::LockHandle> TakeLockSync(
const FileSystemAccessManagerImpl::BindingContext binding_context,
const storage::FileSystemURL& url,
FileSystemAccessLockManager::LockType lock_type) {
return TakeLockAsync(binding_context, url, lock_type)->Take();
}
void AssertAncestorLockBehavior(const FileSystemURL& parent_url,
const FileSystemURL& child_url) {
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType ancestor_lock_type = manager_->GetAncestorLockTypeForTesting();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
// Parent cannot take an exclusive lock if child holds an exclusive lock.
{
auto child_lock =
TakeLockSync(kBindingContext, child_url, exclusive_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_FALSE(
TakeLockSync(kBindingContext, parent_url, exclusive_lock_type));
}
// Parent can take an ancestor lock if child holds an exclusive lock.
{
auto child_lock =
TakeLockSync(kBindingContext, child_url, exclusive_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_TRUE(
TakeLockSync(kBindingContext, parent_url, ancestor_lock_type));
}
// Child cannot take an exclusive lock if parent holds an exclusive lock.
{
auto parent_lock =
TakeLockSync(kBindingContext, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_FALSE(
TakeLockSync(kBindingContext, child_url, exclusive_lock_type));
}
// Child can take an exclusive lock if parent holds an ancestor lock.
{
auto parent_lock =
TakeLockSync(kBindingContext, parent_url, ancestor_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_TRUE(
TakeLockSync(kBindingContext, child_url, exclusive_lock_type));
}
// Parent cannot take an exclusive lock if child holds a shared lock.
{
auto child_lock =
TakeLockSync(kBindingContext, child_url, shared_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_FALSE(
TakeLockSync(kBindingContext, parent_url, exclusive_lock_type));
}
// Parent can take an ancestor lock if child holds a shared lock.
{
auto child_lock =
TakeLockSync(kBindingContext, child_url, shared_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_TRUE(
TakeLockSync(kBindingContext, parent_url, ancestor_lock_type));
}
// Child cannot take a shared lock if parent holds an exclusive lock.
{
auto parent_lock =
TakeLockSync(kBindingContext, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_FALSE(TakeLockSync(kBindingContext, child_url, shared_lock_type));
}
// Child can take a shared lock if parent holds an ancestor lock.
{
auto parent_lock =
TakeLockSync(kBindingContext, parent_url, ancestor_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_TRUE(TakeLockSync(kBindingContext, child_url, shared_lock_type));
}
}
protected:
const GURL kTestURL = GURL("https://example.com/test");
const blink::StorageKey kTestStorageKey =
blink::StorageKey::CreateFromStringForTesting("https://example.com/test");
const storage::BucketLocator kTestBucketLocator =
storage::BucketLocator(storage::BucketId(1),
kTestStorageKey,
/*is_default=*/false);
// Default initializing kFrameId simulates a frame that is always active.
const GlobalRenderFrameHostId kFrameId;
const FileSystemAccessManagerImpl::BindingContext kBindingContext = {
kTestStorageKey, kTestURL, kFrameId};
base::ScopedTempDir dir_;
scoped_refptr<storage::FileSystemContext> file_system_context_;
scoped_refptr<ChromeBlobStorageContext> chrome_blob_context_;
scoped_refptr<FileSystemAccessManagerImpl> manager_;
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(FileSystemAccessLockManagerTest, ExclusiveLock) {
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
{
auto exclusive_lock =
TakeLockSync(kBindingContext, url, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Cannot take another lock while the file is exclusively locked.
ASSERT_FALSE(TakeLockSync(kBindingContext, url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(kBindingContext, url, shared_lock_type));
}
// The exclusive lock has been released and should be available to be
// re-acquired.
ASSERT_TRUE(TakeLockSync(kBindingContext, url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SharedLock) {
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type_1 = manager_->CreateSharedLockTypeForTesting();
LockType shared_lock_type_2 = manager_->CreateSharedLockTypeForTesting();
{
auto shared_lock = TakeLockSync(kBindingContext, url, shared_lock_type_1);
ASSERT_TRUE(shared_lock);
// Can take another shared lock of the same type, but not an exclusive lock
// or a shared lock of another type.
ASSERT_TRUE(TakeLockSync(kBindingContext, url, shared_lock_type_1));
ASSERT_FALSE(TakeLockSync(kBindingContext, url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(kBindingContext, url, shared_lock_type_2));
}
// The shared locks have been released and we should be available to acquire
// an exclusive lock.
ASSERT_TRUE(TakeLockSync(kBindingContext, url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SandboxedFile) {
auto url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe("test/foo/bar"));
url.SetBucket(kTestBucketLocator);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
{
auto exclusive_lock =
TakeLockSync(kBindingContext, url, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Cannot take another lock while the file is exclusively locked.
ASSERT_FALSE(TakeLockSync(kBindingContext, url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(kBindingContext, url, shared_lock_type));
}
// The exclusive lock has been released and should be available to be
// re-acquired.
ASSERT_TRUE(TakeLockSync(kBindingContext, url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SandboxedFilesSamePath) {
// Sandboxed files of the same relative path do not lock across sites if the
// BucketLocator is set.
const blink::StorageKey kOtherStorageKey =
blink::StorageKey::CreateFromStringForTesting("https://foo.com/test");
const auto path = base::FilePath::FromUTF8Unsafe("test/foo/bar");
auto url1 = file_system_context_->CreateCrackedFileSystemURL(
kOtherStorageKey, storage::kFileSystemTypeTemporary, path);
url1.SetBucket(kTestBucketLocator);
auto url2 = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, path);
const storage::BucketLocator kOtherBucketLocator(
storage::BucketId(2), kOtherStorageKey,
/*is_default=*/false);
url2.SetBucket(kOtherBucketLocator);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
// Take a lock on the file in the first file system.
auto exclusive_lock1 =
TakeLockSync(kBindingContext, url1, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock1);
ASSERT_FALSE(TakeLockSync(kBindingContext, url1, exclusive_lock_type));
// Can still take a lock on the file in the second file system.
auto exclusive_lock2 =
TakeLockSync(kBindingContext, url2, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock2);
ASSERT_FALSE(TakeLockSync(kBindingContext, url2, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SandboxedFilesDifferentBucket) {
// Sandboxed files of the same relative path do not lock across buckets.
const auto path = base::FilePath::FromUTF8Unsafe("test/foo/bar");
auto url1 = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, path);
url1.SetBucket(kTestBucketLocator);
auto url2 = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, path);
const storage::BucketLocator kOtherBucketLocator(
storage::BucketId(2), kTestStorageKey,
/*is_default=*/false);
url2.SetBucket(kOtherBucketLocator);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
// Take a lock on the file in the first file system.
auto exclusive_lock1 =
TakeLockSync(kBindingContext, url1, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock1);
ASSERT_FALSE(TakeLockSync(kBindingContext, url1, exclusive_lock_type));
// Can still take a lock on the file in the second file system.
auto exclusive_lock2 =
TakeLockSync(kBindingContext, url2, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock2);
ASSERT_FALSE(TakeLockSync(kBindingContext, url2, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, DifferentBackends) {
// We'll use the same path and pretend they're from different backends.
base::FilePath path =
base::FilePath::FromUTF8Unsafe(kTestMountPoint).AppendASCII("foo");
// File on a local file system.
auto local_url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
// File with the same path on an external file system.
auto external_url = manager_->CreateFileSystemURLFromPath(
PathInfo(PathType::kExternal, path));
EXPECT_EQ(local_url.path(), external_url.virtual_path());
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
// Take a lock on the file in the local file system.
auto local_exclusive_lock =
TakeLockSync(kBindingContext, local_url, exclusive_lock_type);
ASSERT_TRUE(local_exclusive_lock);
ASSERT_FALSE(TakeLockSync(kBindingContext, local_url, exclusive_lock_type));
// Can still take a lock on the file in the external file system.
auto external_exclusive_lock =
TakeLockSync(kBindingContext, external_url, exclusive_lock_type);
ASSERT_TRUE(external_exclusive_lock);
ASSERT_FALSE(
TakeLockSync(kBindingContext, external_url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, LockAcrossSites) {
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url1 = FileSystemURL::CreateForTest(kTestStorageKey,
storage::kFileSystemTypeLocal, path);
// Select the same local file from another site.
auto url2 = FileSystemURL::CreateForTest(
blink::StorageKey::CreateFromStringForTesting("https://foo.com/bar"),
storage::kFileSystemTypeLocal, path);
EXPECT_EQ(url1.path(), url2.path());
EXPECT_NE(url1.storage_key(), url2.storage_key());
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
{
auto exclusive_lock =
TakeLockSync(kBindingContext, url1, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Other sites cannot access the file while it is exclusively locked.
ASSERT_FALSE(TakeLockSync(kBindingContext, url2, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(kBindingContext, url2, shared_lock_type));
}
// The exclusive lock has been released and should be available to be
// re-acquired.
ASSERT_TRUE(TakeLockSync(kBindingContext, url2, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, AncestorLocks) {
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url =
manager_->CreateFileSystemURLFromPath(PathInfo(parent_path));
auto child_url = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child")));
AssertAncestorLockBehavior(parent_url, child_url);
}
TEST_F(FileSystemAccessLockManagerTest, AncestorLocksExternal) {
base::FilePath parent_path =
base::FilePath::FromUTF8Unsafe(kTestMountPoint).AppendASCII("foo");
auto parent_url = manager_->CreateFileSystemURLFromPath(
PathInfo(PathType::kExternal, parent_path));
auto child_url = manager_->CreateFileSystemURLFromPath(
PathInfo(PathType::kExternal, parent_path.AppendASCII("child")));
AssertAncestorLockBehavior(parent_url, child_url);
}
TEST_F(FileSystemAccessLockManagerTest, AncestorLocksSandboxed) {
auto parent_path = base::FilePath::FromUTF8Unsafe("test/foo/bar");
auto parent_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, parent_path);
parent_url.SetBucket(kTestBucketLocator);
auto child_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
parent_path.AppendASCII("child"));
child_url.SetBucket(kTestBucketLocator);
AssertAncestorLockBehavior(parent_url, child_url);
}
TEST_F(FileSystemAccessLockManagerTest, AncestorWithSameName) {
{
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url =
manager_->CreateFileSystemURLFromPath(PathInfo(parent_path));
auto child_url = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("foo")));
AssertAncestorLockBehavior(parent_url, child_url);
}
{
base::FilePath parent_path =
base::FilePath::FromUTF8Unsafe(kTestMountPoint).AppendASCII("foo");
auto parent_url = manager_->CreateFileSystemURLFromPath(
PathInfo(PathType::kExternal, parent_path));
auto child_url = manager_->CreateFileSystemURLFromPath(
PathInfo(PathType::kExternal, parent_path.AppendASCII("foo")));
AssertAncestorLockBehavior(parent_url, child_url);
}
{
auto parent_path = base::FilePath::FromUTF8Unsafe("test/foo/bar");
auto parent_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, parent_path);
parent_url.SetBucket(kTestBucketLocator);
auto child_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
parent_path.AppendASCII("foo"));
child_url.SetBucket(kTestBucketLocator);
AssertAncestorLockBehavior(parent_url, child_url);
}
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheExclusive) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_1;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_2;
{
auto exclusive_lock =
TakeLockSync(bf_cache_context, url, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Cannot take another lock of any type while the page is active.
ASSERT_FALSE(TakeLockSync(active_context, url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(active_context, url, shared_lock_type));
// Entering into the BFCache should not evict the page.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// Taking a lock of a contentious type will not return synchronously, but
// will start eviction and create a pending lock.
manager_->TakeLock(active_context, url, shared_lock_type,
pending_future_1.GetCallback());
ASSERT_FALSE(pending_future_1.IsReady());
EXPECT_TRUE(rfh->is_evicted_from_back_forward_cache());
// Taking a lock that's not contentious with the pending lock will also
// create a pending lock.
manager_->TakeLock(active_context, url, shared_lock_type,
pending_future_2.GetCallback());
ASSERT_FALSE(pending_future_2.IsReady());
// Taking a lock that's contentious with the pending lock will fail if the
// pending lock is still held by an active page.
ASSERT_FALSE(TakeLockSync(active_context, url, exclusive_lock_type));
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future_1.IsReady());
ASSERT_TRUE(pending_future_1.Take());
ASSERT_TRUE(pending_future_2.IsReady());
ASSERT_TRUE(pending_future_2.Take());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheShared) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type_1 = manager_->CreateSharedLockTypeForTesting();
LockType shared_lock_type_2 = manager_->CreateSharedLockTypeForTesting();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_1;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_2;
{
auto shared_lock = TakeLockSync(bf_cache_context, url, shared_lock_type_1);
ASSERT_TRUE(shared_lock);
// Can only take shared locks of the same type.
ASSERT_FALSE(TakeLockSync(active_context, url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(active_context, url, shared_lock_type_2));
ASSERT_TRUE(TakeLockSync(active_context, url, shared_lock_type_1));
// Entering into the BFCache should not evict the page. The lock should not
// have been released.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
{
// Taking a shared lock of the same type should succeed and not evict the
// page.
auto shared_lock_2 =
TakeLockSync(active_context, url, shared_lock_type_1);
ASSERT_TRUE(shared_lock_2);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// While there's an active page holding the lock, taking a lock of a
// contentious type will still fail.
ASSERT_FALSE(TakeLockSync(active_context, url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(active_context, url, shared_lock_type_2));
}
// When only inactive pages hold the lock, taking a lock of a contentious
// type will evict the page and create the lock asynchronously. The new lock
// is pending in the lock manager until the evicting locks are destroyed.
manager_->TakeLock(active_context, url, shared_lock_type_2,
pending_future_1.GetCallback());
ASSERT_FALSE(pending_future_1.IsReady());
// Taking a lock that's not contentious with the pending lock will also
// create a pending lock.
manager_->TakeLock(active_context, url, shared_lock_type_2,
pending_future_2.GetCallback());
ASSERT_FALSE(pending_future_2.IsReady());
// Taking a lock that's contentious with the pending lock will fail if the
// pending lock is still held by an active page.
ASSERT_FALSE(TakeLockSync(active_context, url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(active_context, url, shared_lock_type_1));
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future_1.IsReady());
ASSERT_TRUE(pending_future_1.Take());
ASSERT_TRUE(pending_future_2.IsReady());
ASSERT_TRUE(pending_future_2.Take());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheTakeChildThenParent) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url =
manager_->CreateFileSystemURLFromPath(PathInfo(parent_path));
auto child_url = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child")));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_1;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_2;
{
auto child_lock =
TakeLockSync(bf_cache_context, child_url, shared_lock_type);
ASSERT_TRUE(child_lock);
// Entering into the BFCache should not evict the page. The lock should
// not have been released.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// When only inactive pages hold the child lock, taking a lock on an
// ancestor will evict the lock and create the new lock asynchronously.
// The new lock is pending in the lock manager until the evicting locks
// are destroyed.
manager_->TakeLock(active_context, parent_url, shared_lock_type,
pending_future_1.GetCallback());
ASSERT_FALSE(pending_future_1.IsReady());
// Taking a lock that's not contentious with the pending lock will also
// create a pending lock.
manager_->TakeLock(active_context, parent_url, shared_lock_type,
pending_future_2.GetCallback());
ASSERT_FALSE(pending_future_2.IsReady());
// Taking a lock that's contentious with the pending lock will fail if the
// pending lock is still held by an active page.
ASSERT_FALSE(TakeLockSync(active_context, parent_url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(active_context, child_url, shared_lock_type));
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future_1.IsReady());
ASSERT_TRUE(pending_future_1.Take());
ASSERT_TRUE(pending_future_2.IsReady());
ASSERT_TRUE(pending_future_2.Take());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheTakeParentThenChild) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url =
manager_->CreateFileSystemURLFromPath(PathInfo(parent_path));
auto child_url_1 = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child1")));
auto child_url_2 = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child2")));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_1;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_2;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future_3;
{
auto parent_lock =
TakeLockSync(bf_cache_context, parent_url, shared_lock_type);
ASSERT_TRUE(parent_lock);
// Entering into the BFCache should not evict the page. The lock should
// not have been released.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// When only inactive pages hold the parent lock, taking a lock on a
// descendant will evict the lock and create the new lock asynchronously.
// The new lock is pending in the lock manager until the evicting locks are
// destroyed.
manager_->TakeLock(active_context, child_url_1, shared_lock_type,
pending_future_1.GetCallback());
ASSERT_FALSE(pending_future_1.IsReady());
// Taking a lock that's not contentious with the pending lock will also
// create a pending lock.
manager_->TakeLock(active_context, child_url_1, shared_lock_type,
pending_future_2.GetCallback());
ASSERT_FALSE(pending_future_2.IsReady());
// Taking a lock where there isn't an existing lock but its a child of a
// pending lock will create the lock asynchronously.
manager_->TakeLock(active_context, child_url_2, exclusive_lock_type,
pending_future_3.GetCallback());
ASSERT_FALSE(pending_future_1.IsReady());
// Taking a lock that's contentious with a pending lock will fail if the
// pending lock is still held by an active page.
ASSERT_FALSE(
TakeLockSync(active_context, child_url_1, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(active_context, child_url_2, shared_lock_type));
ASSERT_FALSE(TakeLockSync(active_context, parent_url, shared_lock_type));
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future_1.IsReady());
ASSERT_TRUE(pending_future_1.Take());
ASSERT_TRUE(pending_future_2.IsReady());
ASSERT_TRUE(pending_future_2.Take());
ASSERT_TRUE(pending_future_3.IsReady());
ASSERT_TRUE(pending_future_3.Take());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheEvictPendingLockRoot) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_and_evicting_future;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future;
{
auto shared_lock = TakeLockSync(bf_cache_context, url, exclusive_lock_type);
ASSERT_TRUE(shared_lock);
// Entering into the BFCache should not evict the page. The lock should not
// have been released.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// Reuse the bf_cache_context as if it were another page.
//
// This works because `FileSystemAccessManagerImpl` doesn't check if the
// context is inactive when `TakeLock` is called. But now any `Lock` taken
// with `bf_cache_context_2` will be held only by an inactive pages.
auto& bf_cache_context_2 = bf_cache_context;
// When only inactive pages hold the lock, taking a lock of a contentious
// type will evict the page and create the lock asynchronously. The new lock
// is pending in the lock manager until the evicting locks are destroyed.
manager_->TakeLock(bf_cache_context_2, url, exclusive_lock_type,
pending_and_evicting_future.GetCallback());
ASSERT_FALSE(pending_and_evicting_future.IsReady());
EXPECT_TRUE(rfh->is_evicted_from_back_forward_cache());
// If only inactive pages hold the pending lock, then taking a lock of a
// contentious type will also evict the pending lock and create the new lock
// asynchronously.
manager_->TakeLock(active_context, url, shared_lock_type,
pending_future.GetCallback());
ASSERT_FALSE(pending_future.IsReady());
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future.IsReady());
ASSERT_TRUE(pending_future.Take());
// The pending lock that got evicted will not have its callback run.
ASSERT_FALSE(pending_and_evicting_future.IsReady());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheEvictDescendantPendingLockRoot) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url =
manager_->CreateFileSystemURLFromPath(PathInfo(parent_path));
auto child_url = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child")));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_and_evicting_future;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future;
{
auto child_lock =
TakeLockSync(bf_cache_context, child_url, exclusive_lock_type);
ASSERT_TRUE(child_lock);
// Entering into the BFCache should not evict the page. The lock should not
// have been released.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// Reuse the bf_cache_context as if it were another page.
//
// This works because `FileSystemAccessManagerImpl` doesn't check if the
// context is inactive when `TakeLock` is called. But now any `Lock` taken
// with `bf_cache_context_2` will be held only by an inactive pages.
auto& bf_cache_context_2 = bf_cache_context;
// When only inactive pages hold the child lock, taking a contentious lock
// on the child will evict the page and create the new lock asynchronously.
// The new child lock is pending in the lock manager until the old child
// lock is evicted.
manager_->TakeLock(bf_cache_context_2, child_url, exclusive_lock_type,
pending_and_evicting_future.GetCallback());
ASSERT_FALSE(pending_and_evicting_future.IsReady());
EXPECT_TRUE(rfh->is_evicted_from_back_forward_cache());
// If only inactive pages hold the pending child lock, then taking a lock on
// an ancestor will also evict the pending lock and create the ancestor lock
// asynchronously.
manager_->TakeLock(bf_cache_context_2, parent_url, exclusive_lock_type,
pending_future.GetCallback());
ASSERT_FALSE(pending_future.IsReady());
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future.IsReady());
ASSERT_TRUE(pending_future.Take());
// The pending lock that got evicted will not have its callback run.
ASSERT_FALSE(pending_and_evicting_future.IsReady());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheEvictAncestorPendingLockRoot) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url =
manager_->CreateFileSystemURLFromPath(PathInfo(parent_path));
auto child_url = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child")));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_and_evicting_future;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future;
{
auto child_lock =
TakeLockSync(bf_cache_context, child_url, exclusive_lock_type);
ASSERT_TRUE(child_lock);
// Entering into the BFCache should not evict the page. The lock should not
// have been released.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// Reuse the bf_cache_context as if it were another page.
//
// This works because `FileSystemAccessManagerImpl` doesn't check if the
// context is inactive when `TakeLock` is called. But now any `Lock` taken
// with `bf_cache_context_2` will be held only by an inactive pages.
auto& bf_cache_context_2 = bf_cache_context;
// When only inactive pages hold the child lock, taking a lock on an
// ancestor will evict the page and create the lock asynchronously. The
// ancestor lock is pending in the lock manager until the child lock is
// evicted.
manager_->TakeLock(bf_cache_context_2, parent_url, shared_lock_type,
pending_and_evicting_future.GetCallback());
ASSERT_FALSE(pending_and_evicting_future.IsReady());
EXPECT_TRUE(rfh->is_evicted_from_back_forward_cache());
// If only inactive pages hold the pending ancestor lock, then taking a lock
// on a descendant of it will evict the pending ancestor lock and create the
// descendant lock asynchronously.
manager_->TakeLock(active_context, child_url, exclusive_lock_type,
pending_future.GetCallback());
ASSERT_FALSE(pending_future.IsReady());
// Taking a lock that's contentious with a pending lock will fail if the
// pending lock is still held by an active page.
ASSERT_FALSE(TakeLockSync(active_context, parent_url, shared_lock_type));
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future.IsReady());
ASSERT_TRUE(pending_future.Take());
// The pending locks that got evicted will not have its callback run.
ASSERT_FALSE(pending_and_evicting_future.IsReady());
}
TEST_F(FileSystemAccessLockManagerTest,
BFCacheEvictMultipleDescendantPendingLockRoot) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url =
manager_->CreateFileSystemURLFromPath(PathInfo(parent_path));
auto child_url_1 = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child1")));
auto child_url_2 = manager_->CreateFileSystemURLFromPath(
PathInfo(parent_path.AppendASCII("child2")));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_and_evicting_future_1;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_and_evicting_future_2;
base::test::TestFuture<scoped_refptr<FileSystemAccessLockManager::LockHandle>>
pending_future;
{
auto child_lock_1 =
TakeLockSync(bf_cache_context, child_url_1, exclusive_lock_type);
ASSERT_TRUE(child_lock_1);
{
auto child_lock_2 =
TakeLockSync(bf_cache_context, child_url_2, exclusive_lock_type);
ASSERT_TRUE(child_lock_2);
// Entering into the BFCache should not evict the page. The lock should
// not have been released.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// Reuse the bf_cache_context as if it were another page.
//
// This works because `FileSystemAccessManagerImpl` doesn't check if the
// context is inactive when `TakeLock` is called. But now any `Lock` taken
// with `bf_cache_context_2` will be held only by an inactive pages.
auto& bf_cache_context_2 = bf_cache_context;
// When only inactive pages hold the child locks, taking a contentious
// locks on them will evict the page and create the new locks
// asynchronously. The new child locks are pending in the lock manager
// until the old child locks are evicted.
manager_->TakeLock(bf_cache_context_2, child_url_1, exclusive_lock_type,
pending_and_evicting_future_1.GetCallback());
ASSERT_FALSE(pending_and_evicting_future_1.IsReady());
manager_->TakeLock(bf_cache_context_2, child_url_2, exclusive_lock_type,
pending_and_evicting_future_2.GetCallback());
ASSERT_FALSE(pending_and_evicting_future_2.IsReady());
EXPECT_TRUE(rfh->is_evicted_from_back_forward_cache());
// If only inactive pages hold the pending child locks, then taking a lock
// on an ancestor will also evict the pending locks and create the
// ancestor lock asynchronously.
manager_->TakeLock(bf_cache_context_2, parent_url, exclusive_lock_type,
pending_future.GetCallback());
ASSERT_FALSE(pending_future.IsReady());
}
// Both child locks must be evicted before the parent lock can be created.
ASSERT_FALSE(pending_future.IsReady());
// The pending lock that got evicted will not have its callback run.
ASSERT_FALSE(pending_and_evicting_future_2.IsReady());
}
// Once the lock we're evicting has been destroyed, the callbacks for the
// pending locks will run with a handle for the new lock.
ASSERT_TRUE(pending_future.IsReady());
ASSERT_TRUE(pending_future.Take());
// The pending locks that got evicted will not have their callbacks run.
ASSERT_FALSE(pending_and_evicting_future_1.IsReady());
ASSERT_FALSE(pending_and_evicting_future_2.IsReady());
}
TEST_F(FileSystemAccessLockManagerTest,
BFCachePendingLockDestroyedOnPromotion) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
// The document is initially in active state.
EXPECT_EQ(rfh->GetLifecycleState(), RenderFrameHost::LifecycleState::kActive);
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
auto active_context = kBindingContext;
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
auto exclusive_lock =
TakeLockSync(bf_cache_context, url, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Entering into the BFCache should not evict the page.
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
EXPECT_FALSE(rfh->is_evicted_from_back_forward_cache());
// Taking a lock of a contentious type will not return synchronously, but
// will start eviction and create a pending lock.
bool pending_lock_callback_run = false;
auto pending_callback = base::BindOnce(
[](bool* pending_lock_callback_run,
scoped_refptr<FileSystemAccessLockManager::LockHandle> lock_handle) {
// Resetting the `lock_handle` will destroy the promoted Pending
// Lock since its the only `LockHandle` to it.
lock_handle.reset();
*pending_lock_callback_run = true;
},
&pending_lock_callback_run);
manager_->TakeLock(active_context, url, shared_lock_type,
std::move(pending_callback));
EXPECT_TRUE(rfh->is_evicted_from_back_forward_cache());
// Resetting the `exclusive_lock` destroys the exclusive lock since its the
// only LockHandle to it. This promotes the Pending Lock to Taken but it is
// destroyed before its pending callbacks return.
EXPECT_FALSE(pending_lock_callback_run);
exclusive_lock.reset();
EXPECT_TRUE(pending_lock_callback_run);
}
TEST_F(FileSystemAccessLockManagerTest,
BFCacheDescendantPendingLockDestroyedOnPromotion) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
auto active_context = kBindingContext;
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
base::FilePath parent = dir_.GetPath().AppendASCII("parent");
auto parent_url = CreateLocalUrl(parent);
auto child_url = CreateLocalUrl(parent.AppendASCII("child"));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
auto parent_lock =
TakeLockSync(bf_cache_context, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
// Evict the `parent_lock` to create a Pending child lock. Destroy its future
// so its handle is destroyed on promotion.
auto child_future =
TakeLockAsync(active_context, child_url, exclusive_lock_type);
child_future.reset();
// Finish evicting the original `parent_lock` causing child_future to be
// promoted but immediately destroyed on promotion.
parent_lock.reset();
}
TEST_F(FileSystemAccessLockManagerTest,
BFCacheMultipleDescendantPendingLockDestroyedOnPromotion) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
auto active_context = kBindingContext;
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
base::FilePath parent = dir_.GetPath().AppendASCII("parent");
auto parent_url = CreateLocalUrl(parent);
auto child1_url = CreateLocalUrl(parent.AppendASCII("child1"));
auto child2_url = CreateLocalUrl(parent.AppendASCII("child2"));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
auto parent_lock =
TakeLockSync(bf_cache_context, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
// Evict the `parent_lock` to create a Pending child1 lock. Destroy its future
// so its handle is destroyed on promotion.
auto child1_future =
TakeLockAsync(active_context, child1_url, exclusive_lock_type);
child1_future.reset();
// Create a child2 lock which is Pending since the ancestor Lock is Pending on
// `parent_lock` being evicted. Destroy its future so its handle is destroyed
// on promotion.
auto child2_future =
TakeLockAsync(active_context, child2_url, exclusive_lock_type);
child2_future.reset();
// Finish evicting the original `parent_lock` causing child_future1 and
// child_future2 to be promoted but immediately destroyed on promotion.
parent_lock.reset();
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheEvictPendingChild) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
auto active_context = kBindingContext;
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
base::FilePath parent = dir_.GetPath().AppendASCII("parent");
auto parent_url = CreateLocalUrl(parent);
auto child_url = CreateLocalUrl(parent.AppendASCII("ipsum1074"));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
auto parent_lock =
TakeLockSync(bf_cache_context, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
// Evict the `parent_lock` to create a Pending child lock.
auto child_future1 =
TakeLockAsync(bf_cache_context, child_url, exclusive_lock_type);
// Evict the Pending child lock to create another Pending child lock.
auto child_future2 =
TakeLockAsync(active_context, child_url, exclusive_lock_type);
// Finish evicting the original `parent_lock` causing child_future2 to be
// promoted, but not child_future1.
parent_lock.reset();
ASSERT_FALSE(child_future1->IsReady());
ASSERT_TRUE(child_future2->IsReady() && child_future2->Take());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheEvictAncestorOfPendingTree) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
auto active_context = kBindingContext;
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
base::FilePath parent = dir_.GetPath().AppendASCII("parent");
base::FilePath child = parent.AppendASCII("child");
auto parent_url = CreateLocalUrl(parent);
auto child_url = CreateLocalUrl(child);
auto grandchild_url = CreateLocalUrl(child.AppendASCII("grandchild"));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
auto child_lock = TakeLockSync(bf_cache_context, child_url, shared_lock_type);
ASSERT_TRUE(child_lock);
// Evict the `child_lock` to create a Pending grandchild lock.
auto grandchild_future =
TakeLockAsync(bf_cache_context, grandchild_url, exclusive_lock_type);
// Evict the Pending grandchild lock to create a Pending parent lock.
auto parent_future =
TakeLockAsync(bf_cache_context, parent_url, exclusive_lock_type);
// Finish evicting the original `child_lock` causing parent_future to be
// promoted, but not grandchild_future.
child_lock.reset();
ASSERT_FALSE(grandchild_future->IsReady());
ASSERT_TRUE(parent_future->IsReady() && parent_future->Take());
}
TEST_F(FileSystemAccessLockManagerTest,
BFCacheEvictPendingAncestorOfEvictedLock) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
auto active_context = kBindingContext;
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
base::FilePath parent = dir_.GetPath().AppendASCII("parent");
base::FilePath child = parent.AppendASCII("child");
auto parent_url = CreateLocalUrl(parent);
auto child_url = CreateLocalUrl(child);
auto grandchild_url = CreateLocalUrl(child.AppendASCII("grandchild"));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
auto parent_lock =
TakeLockSync(bf_cache_context, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
// Evict the `parent_lock` to create a Pending grandchild lock.
auto grandchild_future1 =
TakeLockAsync(bf_cache_context, grandchild_url, exclusive_lock_type);
// Evict the Pending grandchild lock to create another Pending grandchild
// lock.
auto grandchild_future2 =
TakeLockAsync(bf_cache_context, grandchild_url, exclusive_lock_type);
// Evict the Pending grandchild lock to create a Pending child lock.
auto child_future =
TakeLockAsync(bf_cache_context, child_url, exclusive_lock_type);
// Finish evicting the original `parent_lock` causing child_future to be
// promoted, but not grandchild_future1 or grandchild_future2.
parent_lock.reset();
ASSERT_FALSE(grandchild_future1->IsReady());
ASSERT_FALSE(grandchild_future2->IsReady());
ASSERT_TRUE(child_future->IsReady() && child_future->Take());
}
TEST_F(FileSystemAccessLockManagerTest,
BFCacheEvictPendingDescendantOfEvictedLock) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
auto active_context = kBindingContext;
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
base::FilePath parent = dir_.GetPath().AppendASCII("parent");
base::FilePath child = parent.AppendASCII("child");
auto parent_url = CreateLocalUrl(parent);
auto child_url = CreateLocalUrl(child);
auto grandchild_url = CreateLocalUrl(child.AppendASCII("grandchild"));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
auto parent_lock =
TakeLockSync(bf_cache_context, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
// Evict the `parent_lock` to create a Pending grandchild lock.
auto grandchild_future1 =
TakeLockAsync(bf_cache_context, grandchild_url, exclusive_lock_type);
// Evict the Pending grandchild lock to create a Pending child lock.
auto child_future =
TakeLockAsync(bf_cache_context, child_url, exclusive_lock_type);
// Evict the Pending child lock to create a Pending grandchild lock.
auto grandchild_future2 =
TakeLockAsync(bf_cache_context, grandchild_url, exclusive_lock_type);
// Finish evicting the original `parent_lock` causing grandchild_future2 to be
// promoted, but not grandchild_future1 or child_future.
parent_lock.reset();
ASSERT_FALSE(grandchild_future1->IsReady());
ASSERT_FALSE(child_future->IsReady());
ASSERT_TRUE(grandchild_future2->IsReady() && grandchild_future2->Take());
}
TEST_F(FileSystemAccessLockManagerTest, BFCacheEvictPendingTree) {
RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(main_rfh());
auto active_context = kBindingContext;
auto bf_cache_context = FileSystemAccessManagerImpl::BindingContext(
kTestStorageKey, kTestURL, rfh->GetGlobalId());
rfh->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kInBackForwardCache);
base::FilePath parent = dir_.GetPath().AppendASCII("parent");
auto parent_url = CreateLocalUrl(parent);
auto child_url = CreateLocalUrl(parent.AppendASCII("child"));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
auto parent_lock =
TakeLockSync(bf_cache_context, parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
// Evict the `parent_lock` by taking a lock on its child.
auto child_future1 =
TakeLockAsync(bf_cache_context, child_url, exclusive_lock_type);
// Evict the Pending child lock to create a new Pending child lock.
auto child_future2 =
TakeLockAsync(bf_cache_context, child_url, exclusive_lock_type);
// Evict the Pending ancestor parent lock of of the Pending child to create a
// new Pending exclusive lock on the parent.
auto parent_future =
TakeLockAsync(bf_cache_context, parent_url, exclusive_lock_type);
// Finish evicting the original `parent_lock` causing parent_future to be
// promoted, but not child_future1 or child_future2.
parent_lock.reset();
ASSERT_FALSE(child_future1->IsReady());
ASSERT_FALSE(child_future2->IsReady());
ASSERT_TRUE(parent_future->IsReady() && parent_future->Take());
}
TEST_F(FileSystemAccessLockManagerTest,
LocksCanExistAfterFileSystemAccessManagerIsDestroyed) {
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(PathInfo(path));
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
auto exclusive_lock = TakeLockSync(kBindingContext, url, exclusive_lock_type);
base::WeakPtr<FileSystemAccessLockManager> lock_manager_weak_ptr =
manager_->GetLockManagerWeakPtrForTesting();
// Destroy the `FileSystemAccessManager` which holds a `scoped_refptr` to the
// `FileSystemAccessLockManager`.
manager_.reset();
// The `FileSystemAccessLockManager` stays alive despite the
// `FileSystemAccessManager` being destroyed.
ASSERT_TRUE(lock_manager_weak_ptr);
// The `exclusive_lock` is the only thing keeping the
// `FileSystemAccessLockManager` alive. So destroying it should destroy the
// `FileSystemAccessLockManager`.
exclusive_lock.reset();
ASSERT_FALSE(lock_manager_weak_ptr);
}
} // namespace content
|