From 79489598b0fb018d5053c94c931969194b80ce4e Mon Sep 17 00:00:00 2001 From: Aleksei Khoroshilov <5928869+goodov@users.noreply.github.com> Date: Tue, 10 Mar 2026 21:52:01 +0700 Subject: [PATCH] containers: Allow NTP to be contained. (#34081) * containers: Allow NTP to be contained. * Review fixes. * Fix include format. --- browser/containers/containers_browsertest.cc | 33 +++++++++++++++++++ .../chrome/browser/tab_contents/tab_util.cc | 20 ++++++----- .../content/browser/site_instance_impl.cc | 28 ++++++++++++++++ .../content/public/browser/site_instance.h | 26 +++++++++++++++ ...ome-browser-tab_contents-tab_util.cc.patch | 12 ------- 5 files changed, 99 insertions(+), 20 deletions(-) create mode 100644 chromium_src/content/browser/site_instance_impl.cc create mode 100644 chromium_src/content/public/browser/site_instance.h delete mode 100644 patches/chrome-browser-tab_contents-tab_util.cc.patch diff --git a/browser/containers/containers_browsertest.cc b/browser/containers/containers_browsertest.cc index 0c5cac0ef9e..b6c137158af 100644 --- a/browser/containers/containers_browsertest.cc +++ b/browser/containers/containers_browsertest.cc @@ -19,6 +19,7 @@ #include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/toolbar_button_provider.h" #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" +#include "chrome/common/webui_url_constants.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" #include "components/content_settings/core/browser/host_content_settings_map.h" @@ -1560,4 +1561,36 @@ IN_PROC_BROWSER_TEST_F(ContainersDisabledAfterRestoreBrowserTest, << local_storage_result; } +IN_PROC_BROWSER_TEST_F(ContainersBrowserTest, + NewTabPageInheritsStoragePartitionConfig) { + const GURL new_tab_url(chrome::kChromeUINewTabURL); + + // Open a new tab page with a container storage partition config + NavigateParams params(browser(), new_tab_url, ui::PAGE_TRANSITION_LINK); + params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; + params.storage_partition_config = content::StoragePartitionConfig::Create( + browser()->profile(), kContainersStoragePartitionDomain, "test-container", + browser()->profile()->IsOffTheRecord()); + ui_test_utils::NavigateToURL(¶ms); + + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + ASSERT_TRUE(web_contents); + + // Verify the storage partition config is set correctly + content::StoragePartition* storage_partition = + web_contents->GetPrimaryMainFrame()->GetStoragePartition(); + ASSERT_TRUE(storage_partition); + + content::StoragePartitionConfig expected_config = + content::StoragePartitionConfig::Create( + browser()->profile(), kContainersStoragePartitionDomain, + "test-container", browser()->profile()->IsOffTheRecord()); + + EXPECT_EQ(expected_config, storage_partition->GetConfig()); + EXPECT_EQ("test-container", storage_partition->GetConfig().partition_name()); + EXPECT_EQ(kContainersStoragePartitionDomain, + storage_partition->GetConfig().partition_domain()); +} + } // namespace containers diff --git a/chromium_src/chrome/browser/tab_contents/tab_util.cc b/chromium_src/chrome/browser/tab_contents/tab_util.cc index 5be0d2a8331..41b55c4a82a 100644 --- a/chromium_src/chrome/browser/tab_contents/tab_util.cc +++ b/chromium_src/chrome/browser/tab_contents/tab_util.cc @@ -6,25 +6,29 @@ #include "chrome/browser/tab_contents/tab_util.h" #include "brave/components/containers/buildflags/buildflags.h" +#include "content/public/browser/site_instance.h" #if BUILDFLAG(ENABLE_CONTAINERS) +// Extend GetSiteInstanceForNewTab to accept an optional StoragePartitionConfig. +// When a config is present it delegates to CreateForFixedStoragePartition; +// otherwise it falls back to CreateForURL. #define GetSiteInstanceForNewTab(...) \ GetSiteInstanceForNewTab( \ __VA_ARGS__, \ std::optional storage_partition_config) -#define BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB \ - if (storage_partition_config) { \ - return SiteInstance::CreateForFixedStoragePartition( \ - profile, url, *storage_partition_config); \ - } -#else -#define BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB +// Redirect CreateForURL to our helper which conditionally calls +// CreateForFixedStoragePartition when a StoragePartitionConfig is present, +// ensuring navigations respect the passed config. +#define CreateForURL(...) \ + CreateForURLWithOptionalFixedStoragePartition(__VA_ARGS__, \ + storage_partition_config) + #endif // BUILDFLAG(ENABLE_CONTAINERS) #include // IWYU pragma: export #if BUILDFLAG(ENABLE_CONTAINERS) +#undef CreateForURL #undef GetSiteInstanceForNewTab -#undef BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB #endif // BUILDFLAG(ENABLE_CONTAINERS) diff --git a/chromium_src/content/browser/site_instance_impl.cc b/chromium_src/content/browser/site_instance_impl.cc new file mode 100644 index 00000000000..60db50860e2 --- /dev/null +++ b/chromium_src/content/browser/site_instance_impl.cc @@ -0,0 +1,28 @@ +/* Copyright (c) 2026 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "content/browser/site_instance_impl.h" + +#include + +namespace content { + +// Used by the containers feature so that SiteInstance::GetSiteInstanceForNewTab +// can apply a container's StoragePartitionConfig when opening a new tab page, +// rather than always falling back to the default partition via CreateForURL. +scoped_refptr +SiteInstance::CreateForURLWithOptionalFixedStoragePartition( + BrowserContext* browser_context, + const GURL& url, + base::optional_ref storage_partition_config) { + if (storage_partition_config) { + return CreateForFixedStoragePartition(browser_context, url, + *storage_partition_config); + } else { + return CreateForURL(browser_context, url); + } +} + +} // namespace content diff --git a/chromium_src/content/public/browser/site_instance.h b/chromium_src/content/public/browser/site_instance.h new file mode 100644 index 00000000000..563f5bb7a4c --- /dev/null +++ b/chromium_src/content/public/browser/site_instance.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2026 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_ +#define BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_ + +#include "base/types/optional_ref.h" + +// Extends SiteInstance with CreateForURLWithOptionalFixedStoragePartition, a +// helper used by the containers feature to apply a StoragePartitionConfig. When +// a config is present it delegates to CreateForFixedStoragePartition; otherwise +// it falls back to CreateForURL. +#define CreateForURL(...) \ + CreateForURL(__VA_ARGS__); \ + static scoped_refptr \ + CreateForURLWithOptionalFixedStoragePartition( \ + __VA_ARGS__, \ + base::optional_ref storage_partition_config) + +#include // IWYU pragma: export + +#undef CreateForURL + +#endif // BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_ diff --git a/patches/chrome-browser-tab_contents-tab_util.cc.patch b/patches/chrome-browser-tab_contents-tab_util.cc.patch deleted file mode 100644 index 44819f6ee9b..00000000000 --- a/patches/chrome-browser-tab_contents-tab_util.cc.patch +++ /dev/null @@ -1,12 +0,0 @@ -diff --git a/chrome/browser/tab_contents/tab_util.cc b/chrome/browser/tab_contents/tab_util.cc -index 4ccf096e97dafd9add0bb46c01397f294d4a6b84..209abdfe6cda1a01eb425a27af3a2aaf375918d7 100644 ---- a/chrome/browser/tab_contents/tab_util.cc -+++ b/chrome/browser/tab_contents/tab_util.cc -@@ -48,6 +48,7 @@ scoped_refptr GetSiteInstanceForNewTab(Profile* profile, - if (!SiteInstance::ShouldAssignSiteForURL(url)) - return nullptr; - -+ BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB - return SiteInstance::CreateForURL(profile, url); - } -