Skip to content

Commit

Permalink
Fixed extension's newtab is not loaded when it's restored during the …
Browse files Browse the repository at this point in the history
…startup

fix brave/brave-browser#30890

As we sanitize serialzed navigation(brave/brave-browser#30890),
encoded page state is empty for all chrome url.
So, new page state is created with virtual url when it's restored during the startup.
ContentSerializedNavigationBuilder::ToNavigationEntry() set new page state if it's empty.
When new page state is created, navigation's virtual url is created.
If url is re-written when NavigationEntryImpl is created, that re-written url is ignored.
To fix this, including url info only for chrome url's encoded page state.
  • Loading branch information
simonhong committed Jun 14, 2023
1 parent 0b6be82 commit 186a3bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
13 changes: 10 additions & 3 deletions browser/sessions/brave_session_restore_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
#include "net/dns/mock_host_resolver.h"
#include "services/network/public/cpp/network_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/page_state/page_state.h"
#include "third_party/blink/public/common/page_state/page_state_serialization.h"

using BraveSesstionRestoreBrowserTest = InProcessBrowserTest;
using BraveSessionRestoreBrowserTest = InProcessBrowserTest;

IN_PROC_BROWSER_TEST_F(BraveSesstionRestoreBrowserTest, Serialization) {
IN_PROC_BROWSER_TEST_F(BraveSessionRestoreBrowserTest, Serialization) {
auto* tab_model = browser()->tab_strip_model();
auto* web_contents = tab_model->GetActiveWebContents();
SessionService* const session_service =
Expand Down Expand Up @@ -53,7 +55,12 @@ IN_PROC_BROWSER_TEST_F(BraveSesstionRestoreBrowserTest, Serialization) {
const auto& serialized_navigation = windows[0]->tabs[0]->navigations[1];
EXPECT_EQ(serialized_navigation.virtual_url(),
GURL("chrome://newtab/"));
EXPECT_TRUE(serialized_navigation.encoded_page_state().empty());

// Check encoded data is not empty but only has url info.
EXPECT_EQ(blink::PageState::CreateFromURL(GURL("chrome://newtab/"))
.ToEncodedData(),
serialized_navigation.encoded_page_state());
EXPECT_FALSE(serialized_navigation.encoded_page_state().empty());
loop.Quit();
}));
loop.Run();
Expand Down
3 changes: 3 additions & 0 deletions chromium_src/components/sessions/content/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_rules = [
"+third_party/blink/public/common/page_state/page_state_serialization.h",
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "components/sessions/content/content_serialized_navigation_driver.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "content/public/common/url_constants.h"
#include "third_party/blink/public/common/page_state/page_state_serialization.h"

#define GetSanitizedPageStateForPickle \
GetSanitizedPageStateForPickle_ChromiumImpl
Expand All @@ -17,9 +18,28 @@
namespace sessions {
std::string ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
const sessions::SerializedNavigationEntry* navigation) const {
if (navigation->virtual_url().SchemeIs(content::kChromeUIScheme))
return std::string();
return GetSanitizedPageStateForPickle_ChromiumImpl(navigation);
auto page_state = GetSanitizedPageStateForPickle_ChromiumImpl(navigation);
if (!navigation->virtual_url().SchemeIs(content::kChromeUIScheme)) {
return page_state;
}

// chrome url can be re-written when it's restored during the tab but
// re-written url is ignored when encoded page state is empty.
// Sanitize all but make url info is persisted.
// In ContentSerializedNavigationBuilder::ToNavigationEntry(), re-written url
// created by NavigationEntryImpl's ctor
// is reset by creating new page state with navigation's virtual_url.
blink::ExplodedPageState exploded;
if (blink::DecodePageState(page_state, &exploded)) {
blink::ExplodedPageState sanitized_exploded;
sanitized_exploded.top.url_string = exploded.top.url_string;
std::string encoded_data;
blink::EncodePageState(sanitized_exploded, &encoded_data);
return blink::PageState::CreateFromEncodedData(encoded_data)
.ToEncodedData();
}

return std::string();
}

} // namespace sessions

0 comments on commit 186a3bd

Please sign in to comment.