diff options
Diffstat (limited to 'chromium/weblayer/browser/persistence')
5 files changed, 66 insertions, 16 deletions
diff --git a/chromium/weblayer/browser/persistence/browser_persister.h b/chromium/weblayer/browser/persistence/browser_persister.h index 8a67919ef22..fc30523eeff 100644 --- a/chromium/weblayer/browser/persistence/browser_persister.h +++ b/chromium/weblayer/browser/persistence/browser_persister.h @@ -13,7 +13,7 @@ #include <utility> #include <vector> -#include "base/macros.h" +#include "base/memory/raw_ptr.h" #include "base/memory/weak_ptr.h" #include "base/scoped_multi_source_observation.h" #include "components/sessions/content/session_tab_helper_delegate.h" @@ -120,7 +120,7 @@ class BrowserPersister : public sessions::CommandStorageManagerDelegate, void ProcessRestoreCommands( const std::vector<std::unique_ptr<sessions::SessionWindow>>& windows); - BrowserImpl* browser_; + raw_ptr<BrowserImpl> browser_; // ID used for the browser. The sessions code requires each tab to be // associated with a browser. diff --git a/chromium/weblayer/browser/persistence/browser_persister_browsertest.cc b/chromium/weblayer/browser/persistence/browser_persister_browsertest.cc index 5ab10e2aba1..dd726c3fe29 100644 --- a/chromium/weblayer/browser/persistence/browser_persister_browsertest.cc +++ b/chromium/weblayer/browser/persistence/browser_persister_browsertest.cc @@ -8,6 +8,7 @@ #include "base/files/file_path.h" #include "base/files/file_util.h" #include "base/guid.h" +#include "base/memory/raw_ptr.h" #include "base/path_service.h" #include "base/run_loop.h" #include "base/test/bind.h" @@ -83,14 +84,14 @@ class BrowserNavigationObserverImpl : public BrowserRestoreObserver, void OnRestoreCompleted() override { browser_->RemoveBrowserRestoreObserver(this); ASSERT_LT(tab_to_wait_for_, browser_->GetTabs().size()); - ASSERT_EQ(nullptr, tab_); + ASSERT_EQ(nullptr, tab_.get()); tab_ = browser_->GetTabs()[tab_to_wait_for_]; tab_->GetNavigationController()->AddObserver(this); } - Browser* browser_; + raw_ptr<Browser> browser_; const GURL& url_; - Tab* tab_ = nullptr; + raw_ptr<Tab> tab_ = nullptr; const size_t tab_to_wait_for_; std::unique_ptr<TestNavigationObserver> navigation_observer_; base::RunLoop run_loop_; diff --git a/chromium/weblayer/browser/persistence/minimal_browser_persister.cc b/chromium/weblayer/browser/persistence/minimal_browser_persister.cc index fe119d4a728..7f8bdf6d5e6 100644 --- a/chromium/weblayer/browser/persistence/minimal_browser_persister.cc +++ b/chromium/weblayer/browser/persistence/minimal_browser_persister.cc @@ -266,13 +266,16 @@ bool PersistTabStatePrimaryPass(const SessionID& browser_session_id, // commands, false if size exceeded. bool PersistTabStateSecondaryPass(const SessionID& browser_session_id, Tab* tab, + int max_navigations_per_tab, MinimalPersister* builder) { NavigationEntryIterator iterator(tab); if (iterator.at_end()) return true; const SessionID& session_id = GetSessionIDForTab(tab); - for (int i = 0; i < 2; ++i) { + // Subtract 1 from `max_navigations_per_tab` as the first pass wrote a + // navigation. + for (int i = 0; i < max_navigations_per_tab - 1; ++i) { // Skips the navigation that was written during the first pass. if (!iterator.Next()) return true; @@ -313,6 +316,7 @@ int GetActiveTabIndex(BrowserImpl* browser) { } // namespace std::vector<uint8_t> PersistMinimalState(BrowserImpl* browser, + int max_navigations_per_tab, int max_size_in_bytes) { MinimalPersister builder(max_size_in_bytes == 0 ? kMaxSizeInBytes : max_size_in_bytes); @@ -334,16 +338,23 @@ std::vector<uint8_t> PersistMinimalState(BrowserImpl* browser, // . active tabs pending navigation entry, if no pending then last committed. // . remaining tabs pending navigation entry or last committed if no pending. // . active tabs last committed and one navigation before it. - // . remaining tabs last committed and one navigation before it. + // . remaining tabs last committed and a limited number of preceding + // navigations. std::vector<Tab*> tabs = GetTabsInPersistOrder(browser); for (Tab* tab : tabs) { if (!PersistTabStatePrimaryPass(browser_session_id, tab, &builder)) return builder.ToByteArray(); } + // Use a default of 5 for the max number of navigations to persist. + if (max_navigations_per_tab == 0) + max_navigations_per_tab = 5; + for (Tab* tab : tabs) { - if (!PersistTabStateSecondaryPass(browser_session_id, tab, &builder)) + if (!PersistTabStateSecondaryPass(browser_session_id, tab, + max_navigations_per_tab, &builder)) { return builder.ToByteArray(); + } } return builder.ToByteArray(); diff --git a/chromium/weblayer/browser/persistence/minimal_browser_persister.h b/chromium/weblayer/browser/persistence/minimal_browser_persister.h index 7cd7c75f4f0..aef0422cd9f 100644 --- a/chromium/weblayer/browser/persistence/minimal_browser_persister.h +++ b/chromium/weblayer/browser/persistence/minimal_browser_persister.h @@ -16,9 +16,12 @@ class BrowserImpl; // Returns a byte array that can later be used to restore the state (Tabs and // navigations) of a Browser. This does not store the full state, only a // minimal state. For example, it may not include all tabs or all navigations. -// |max_size_in_bytes| is provided for tests and allows specifying the max. -// A value of 0 means use the default max. +// |max_navigations_per_tab| is the max number of navigations to persist per +// tab. Depending upon space requirements, the max number of navigations may not +// be honored. |max_size_in_bytes| is provided for tests and allows specifying +// the max. A value of 0 means use the default max. std::vector<uint8_t> PersistMinimalState(BrowserImpl* browser, + int max_navigations_per_tab = 0, int max_size_in_bytes = 0); // Restores the state previously created via PersistMinimalState(). When diff --git a/chromium/weblayer/browser/persistence/minimal_browser_persister_browsertest.cc b/chromium/weblayer/browser/persistence/minimal_browser_persister_browsertest.cc index 7dd4326d11d..e0a6728400b 100644 --- a/chromium/weblayer/browser/persistence/minimal_browser_persister_browsertest.cc +++ b/chromium/weblayer/browser/persistence/minimal_browser_persister_browsertest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/memory/raw_ptr.h" #include "build/build_config.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_entry.h" @@ -9,6 +10,7 @@ #include "content/public/test/browser_test_utils.h" #include "net/base/filename_util.h" #include "net/test/embedded_test_server/embedded_test_server.h" +#include "third_party/blink/public/common/features.h" #include "weblayer/browser/browser_impl.h" #include "weblayer/browser/profile_impl.h" #include "weblayer/browser/tab_impl.h" @@ -48,10 +50,12 @@ class MinimalBrowserPersisterTest : public WebLayerBrowserTest { // Persists the current state, then recreates the browser. See // BrowserImpl::GetMinimalPersistenceState() for details on // |max_size_in_bytes|, 0 means use the default value. - void RecreateBrowserFromCurrentState(int max_size_in_bytes = 0) { + void RecreateBrowserFromCurrentState( + int max_number_of_navigations_per_tab = 0, + int max_size_in_bytes = 0) { Browser::PersistenceInfo persistence_info; - persistence_info.minimal_state = - browser_impl()->GetMinimalPersistenceState(max_size_in_bytes); + persistence_info.minimal_state = browser_impl()->GetMinimalPersistenceState( + max_number_of_navigations_per_tab, max_size_in_bytes); tab_ = nullptr; browser_ = Browser::Create(GetProfile(), &persistence_info); // There is always at least one tab created (even if restore fails). @@ -65,7 +69,7 @@ class MinimalBrowserPersisterTest : public WebLayerBrowserTest { } std::unique_ptr<Browser> browser_; - TabImpl* tab_ = nullptr; + raw_ptr<TabImpl> tab_ = nullptr; }; IN_PROC_BROWSER_TEST_F(MinimalBrowserPersisterTest, SingleTab) { @@ -144,6 +148,33 @@ IN_PROC_BROWSER_TEST_F(MinimalBrowserPersisterTest, TwoNavs) { EXPECT_EQ(url2(), nav_controller.GetEntryAtIndex(1)->GetURL()); } +IN_PROC_BROWSER_TEST_F(MinimalBrowserPersisterTest, NavigationOverflow) { + NavigateAndWaitForCompletion(url1(), tab_); + NavigateAndWaitForCompletion(url2(), tab_); + const GURL url3 = embedded_test_server()->GetURL("/simple_page3.html"); + NavigateAndWaitForCompletion(url3, tab_); + const GURL url4 = embedded_test_server()->GetURL("/simple_page4.html"); + NavigateAndWaitForCompletion(url4, tab_); + + ASSERT_NO_FATAL_FAILURE(RecreateBrowserFromCurrentState(3)); + + // As a max of 3 navigations was specified, only the last three navigations + // should be restored. + TabImpl* restored_tab = tab_; + EXPECT_EQ(restored_tab, browser_->GetActiveTab()); + TestNavigationObserver observer( + url4, TestNavigationObserver::NavigationEvent::kCompletion, restored_tab); + observer.Wait(); + ASSERT_EQ(3, + restored_tab->GetNavigationController()->GetNavigationListSize()); + content::NavigationController& nav_controller = + restored_tab->web_contents()->GetController(); + EXPECT_EQ(2, nav_controller.GetCurrentEntryIndex()); + EXPECT_EQ(url2(), nav_controller.GetEntryAtIndex(0)->GetURL()); + EXPECT_EQ(url3, nav_controller.GetEntryAtIndex(1)->GetURL()); + EXPECT_EQ(url4, nav_controller.GetEntryAtIndex(2)->GetURL()); +} + // crbug.com/1240904: test is flaky on linux and win. #if defined(OS_LINUX) || defined(OS_WIN) #define MAYBE_Overflow DISABLED_Overflow @@ -156,11 +187,15 @@ IN_PROC_BROWSER_TEST_F(MinimalBrowserPersisterTest, MAYBE_Overflow) { url_string.replace(0, data.size(), data); NavigateAndWaitForCompletion(GURL(url_string), tab_); - ASSERT_NO_FATAL_FAILURE(RecreateBrowserFromCurrentState(2048)); + ASSERT_NO_FATAL_FAILURE(RecreateBrowserFromCurrentState(0, 2048)); TabImpl* restored_tab = tab_; EXPECT_EQ(restored_tab, browser_->GetActiveTab()); - EXPECT_EQ(0, restored_tab->web_contents()->GetController().GetEntryCount()); + if (blink::features::IsInitialNavigationEntryEnabled()) { + EXPECT_EQ(1, restored_tab->web_contents()->GetController().GetEntryCount()); + } else { + EXPECT_EQ(0, restored_tab->web_contents()->GetController().GetEntryCount()); + } EXPECT_TRUE(restored_tab->web_contents()->GetController().GetPendingEntry() == nullptr); } |