diff --git a/content/browser/renderer_host/back_forward_cache_impl.h b/content/browser/renderer_host/back_forward_cache_impl.h index 0a57cf725d4830..6bbf42b299e6ec 100644 --- a/content/browser/renderer_host/back_forward_cache_impl.h +++ b/content/browser/renderer_host/back_forward_cache_impl.h @@ -11,6 +11,7 @@ #include #include "base/feature_list.h" +#include "base/gtest_prod_util.h" #include "base/memory/raw_ptr.h" #include "base/memory/raw_ref.h" #include "base/memory/weak_ptr.h" @@ -104,6 +105,28 @@ struct CONTENT_EXPORT BackForwardCacheCanStoreDocumentResultWithTree { // frozen state and is kept in this object. They can potentially be reused // after an history navigation. Reusing a document means swapping it back with // the current_frame_host. +// +// +// BackForwardCache Size & Pruning Logic: +// +// 1. `EnforceCacheSizeLimit()` is called to prune the cache size down on +// storing a new cache entry, or when the renderer process's +// `IsProcessBackgrounded()` state changes. +// A. [Android-only] The number of entries where `HasForegroundedProcess()` +// is true is pruned to `GetForegroundedEntriesCacheSize()`. +// B. Prunes to `GetCacheSize()` entries no matter what kinds of tabs +// BackForwardCache is in. +// C. When a `RenderFrameHost` enters BackForwardCache, it schedules a task +// in `RenderFrameHostImpl::StartBackForwardCacheEvictionTimer()` to +// evicts the outermost frame after +// `kDefaultTimeToLiveInBackForwardCacheInSeconds` seconds. +// 2. In `performance_manager::policies::BFCachePolicy`: +// A. (To Launch) [Desktop-only] On moderate memory pressure, the number of +// entries in a visible tab's cache is pruned to +// `ForegroundCacheSizeOnModeratePressure()`. The number in a non-visible +// tab is pruned to `BackgroundCacheSizeOnModeratePressure()`. +// B. (To Launch) [Desktop-only] On critical memory pressure, the cache is +// cleared. class CONTENT_EXPORT BackForwardCacheImpl : public BackForwardCache, public RenderProcessHostInternalObserver, @@ -361,8 +384,6 @@ class CONTENT_EXPORT BackForwardCacheImpl BackForwardCacheCanStoreDocumentResult& eviction_reason); private: - FRIEND_TEST_ALL_PREFIXES(BackForwardCacheMetricsTest, AllFeaturesCovered); - // Destroys all evicted frames in the BackForwardCache. void DestroyEvictedFrames(); @@ -551,6 +572,13 @@ class CONTENT_EXPORT BackForwardCacheImpl }; base::WeakPtrFactory weak_factory_; + + // For testing: + FRIEND_TEST_ALL_PREFIXES(BackForwardCacheMetricsTest, AllFeaturesCovered); + FRIEND_TEST_ALL_PREFIXES(BackForwardCacheActiveSizeTest, ActiveCacheSize); + FRIEND_TEST_ALL_PREFIXES(BackForwardCacheOverwriteSizeTest, + OverwrittenCacheSize); + FRIEND_TEST_ALL_PREFIXES(BackForwardCacheDefaultSizeTest, DefaultCacheSize); }; // Allow external code to be notified when back-forward cache is disabled for a diff --git a/content/browser/renderer_host/back_forward_cache_impl_unittest.cc b/content/browser/renderer_host/back_forward_cache_impl_unittest.cc index 7e2d8bbe447667..4005ba0daf8991 100644 --- a/content/browser/renderer_host/back_forward_cache_impl_unittest.cc +++ b/content/browser/renderer_host/back_forward_cache_impl_unittest.cc @@ -3,7 +3,13 @@ // found in the LICENSE file. #include "content/browser/renderer_host/back_forward_cache_impl.h" + +#include "base/test/scoped_feature_list.h" +#include "content/test/test_render_frame_host.h" #include "content/test/test_render_view_host.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/blink/public/common/scheduler/web_scheduler_tracked_feature.h" namespace content { @@ -88,4 +94,60 @@ TEST_F(BackForwardCacheImplTest, SecondCrossOriginReachable) { blink::mojom::BFCacheBlocked::kNo); } -} // namespace content \ No newline at end of file +// Covers BackForwardCache's cache size-related values used in Stable. +// See docs/back_forward_cache_size.md for more details. +class BackForwardCacheActiveSizeTest : public ::testing::Test { + protected: + void SetUp() override { + feature_list_.InitWithFeaturesAndParameters( + {{features::kBackForwardCache, + {{"cache_size", "6"}, {"foreground_cache_size", "2"}}}}, + {}); + } + + private: + base::test::ScopedFeatureList feature_list_; +}; + +TEST_F(BackForwardCacheActiveSizeTest, ActiveCacheSize) { + EXPECT_EQ(BackForwardCacheImpl::GetCacheSize(), 6u); + EXPECT_EQ(BackForwardCacheImpl::GetForegroundedEntriesCacheSize(), 2u); + EXPECT_TRUE(BackForwardCacheImpl::UsingForegroundBackgroundCacheSizeLimit()); +} + +// Covers overwriting BackForwardCache's cache size-related values. +// When "cache_size" or "foreground_cache_size" presents in both +// `kBackForwardCacheSize` and `features::kBackForwardCache`, the former should +// take precedence. +class BackForwardCacheOverwriteSizeTest : public ::testing::Test { + protected: + void SetUp() override { + feature_list_.InitWithFeaturesAndParameters( + {{kBackForwardCacheSize, + {{"cache_size", "8"}, {"foreground_cache_size", "4"}}}, + {features::kBackForwardCache, + {{"cache_size", "6"}, {"foreground_cache_size", "2"}}}}, + {}); + } + + private: + base::test::ScopedFeatureList feature_list_; +}; + +TEST_F(BackForwardCacheOverwriteSizeTest, OverwrittenCacheSize) { + EXPECT_EQ(BackForwardCacheImpl::GetCacheSize(), 8u); + EXPECT_EQ(BackForwardCacheImpl::GetForegroundedEntriesCacheSize(), 4u); + EXPECT_TRUE(BackForwardCacheImpl::UsingForegroundBackgroundCacheSizeLimit()); +} + +// Covers BackForwardCache's default cache size-related values. +// Note that these tests don't cover the values configured from Finch. +class BackForwardCacheDefaultSizeTest : public ::testing::Test {}; + +TEST_F(BackForwardCacheDefaultSizeTest, DefaultCacheSize) { + EXPECT_EQ(BackForwardCacheImpl::GetCacheSize(), 1u); + EXPECT_EQ(BackForwardCacheImpl::GetForegroundedEntriesCacheSize(), 0u); + EXPECT_FALSE(BackForwardCacheImpl::UsingForegroundBackgroundCacheSizeLimit()); +} + +} // namespace content